From adfb11cd1b2408859b3acf408fa1c4b5ebd4543a Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 5 Sep 2019 09:17:05 -0500 Subject: [PATCH] Reshuffle strucutre of connectivity state tree branch --- frontend/__test_support__/fake_state/bot.ts | 9 ++++++--- frontend/app.tsx | 2 +- .../__tests__/reducer_edge_status_test.ts | 4 ++-- .../__tests__/reducer_reset_network_test.ts | 4 ++-- .../__tests__/reducer_resource_ready_test.ts | 2 +- frontend/connectivity/interfaces.ts | 5 ++++- frontend/connectivity/reducer.ts | 18 +++++++++++------- frontend/controls/state_to_props.ts | 2 +- frontend/devices/connectivity/generate_data.ts | 6 +++--- frontend/devices/reducer.ts | 11 +++++++---- frontend/devices/state_to_props.ts | 6 +++--- frontend/farm_designer/move_to.tsx | 2 +- .../farmware/__tests__/state_to_props_test.tsx | 2 +- frontend/farmware/state_to_props.ts | 2 +- frontend/redux/state_fetch_middleware.ts | 2 +- 15 files changed, 45 insertions(+), 32 deletions(-) diff --git a/frontend/__test_support__/fake_state/bot.ts b/frontend/__test_support__/fake_state/bot.ts index ab200f4e8..f6ff46e38 100644 --- a/frontend/__test_support__/fake_state/bot.ts +++ b/frontend/__test_support__/fake_state/bot.ts @@ -56,8 +56,11 @@ export let bot: Everything["bot"] = { "dirty": false, "currentOSVersion": "3.1.6", "connectivity": { - "bot.mqtt": undefined, - "user.mqtt": undefined, - "user.api": undefined, + uptime: { + "bot.mqtt": undefined, + "user.mqtt": undefined, + "user.api": undefined, + }, + pings: {} } }; diff --git a/frontend/app.tsx b/frontend/app.tsx index 7da572a84..86f29454c 100644 --- a/frontend/app.tsx +++ b/frontend/app.tsx @@ -118,7 +118,7 @@ export class App extends React.Component { const currentPage = getPathArray()[2]; const { location_data, mcu_params } = this.props.bot.hardware; const { sync_status } = this.props.bot.hardware.informational_settings; - const bot2mqtt = this.props.bot.connectivity["bot.mqtt"]; + const bot2mqtt = this.props.bot.connectivity.uptime["bot.mqtt"]; return
{!syncLoaded && } diff --git a/frontend/connectivity/__tests__/reducer_edge_status_test.ts b/frontend/connectivity/__tests__/reducer_edge_status_test.ts index b8c25183e..b0cb349a5 100644 --- a/frontend/connectivity/__tests__/reducer_edge_status_test.ts +++ b/frontend/connectivity/__tests__/reducer_edge_status_test.ts @@ -6,7 +6,7 @@ describe("connectivityReducer", () => { const state = connectivityReducer(DEFAULT_STATE, networkUp("user.mqtt", undefined, "tests")); expect(state).toBeDefined(); - const x = state && state["user.mqtt"]; + const x = state && state.uptime["user.mqtt"]; if (x) { expect(x.state).toBe("up"); expect(x.at).toBeTruthy(); @@ -18,7 +18,7 @@ describe("connectivityReducer", () => { it("goes down", () => { const state = connectivityReducer(DEFAULT_STATE, networkDown("user.api", undefined, "tests")); - const x = state && state["user.api"]; + const x = state && state.uptime["user.api"]; if (x) { expect(x.state).toBe("down"); expect(x.at).toBeTruthy(); diff --git a/frontend/connectivity/__tests__/reducer_reset_network_test.ts b/frontend/connectivity/__tests__/reducer_reset_network_test.ts index 77b350d0f..d67d23579 100644 --- a/frontend/connectivity/__tests__/reducer_reset_network_test.ts +++ b/frontend/connectivity/__tests__/reducer_reset_network_test.ts @@ -6,11 +6,11 @@ describe("Connectivity Reducer - RESET_NETWORK", () => { it("clears all network status", () => { const action: ReduxAction<{}> = resetNetwork(); const result = connectivityReducer(DEFAULT_STATE, action); - const values: (keyof typeof DEFAULT_STATE)[] = [ + const values: (keyof typeof DEFAULT_STATE["uptime"])[] = [ "bot.mqtt", "user.mqtt", "user.api" ]; - values.map((x) => expect(result[x]).toBeUndefined()); + values.map((x) => expect(result.uptime[x]).toBeUndefined()); }); }); diff --git a/frontend/connectivity/__tests__/reducer_resource_ready_test.ts b/frontend/connectivity/__tests__/reducer_resource_ready_test.ts index a557f3603..d7ce52928 100644 --- a/frontend/connectivity/__tests__/reducer_resource_ready_test.ts +++ b/frontend/connectivity/__tests__/reducer_resource_ready_test.ts @@ -8,6 +8,6 @@ describe("Connectivity Reducer - RESOURCE_READY", () => { device.body.last_saw_mq = "Tue, 03 Oct 2017 09:00:00 -0500"; const action = resourceReady("Device", device); const result = connectivityReducer(DEFAULT_STATE, action); - expect(result["bot.mqtt"]).not.toBe(undefined); + expect(result.uptime["bot.mqtt"]).not.toBe(undefined); }); }); diff --git a/frontend/connectivity/interfaces.ts b/frontend/connectivity/interfaces.ts index 61315c021..82b989caf 100644 --- a/frontend/connectivity/interfaces.ts +++ b/frontend/connectivity/interfaces.ts @@ -25,7 +25,10 @@ type ConnectionRecord = Record; /** Mapping of known connection status. * An `undefined` value means we don't know. */ -export type ConnectionState = ConnectionRecord; +export type ConnectionState = { + uptime: ConnectionRecord; + pings: {} +}; export interface UpdateMqttData { status: "UPDATE" diff --git a/frontend/connectivity/reducer.ts b/frontend/connectivity/reducer.ts index ec2041eb1..4fbfa807f 100644 --- a/frontend/connectivity/reducer.ts +++ b/frontend/connectivity/reducer.ts @@ -7,28 +7,32 @@ import { SyncBodyContents } from "../sync/actions"; import { arrayUnwrap } from "../resources/util"; export const DEFAULT_STATE: ConnectionState = { - "bot.mqtt": undefined, - "user.mqtt": undefined, - "user.api": undefined + uptime: { + "bot.mqtt": undefined, + "user.mqtt": undefined, + "user.api": undefined + }, + pings: { + } }; export let connectivityReducer = generateReducer(DEFAULT_STATE) .add(Actions.NETWORK_EDGE_CHANGE, (s, { payload }) => { - s[payload.name] = payload.status; + s.uptime[payload.name] = payload.status; return s; }) .add>(Actions.RESOURCE_READY, (s, a) => { const d = arrayUnwrap(a.payload.body); if (d && d.kind === "Device") { - s["bot.mqtt"] = computeBestTime(s["bot.mqtt"], d && d.body.last_saw_mq); + s.uptime["bot.mqtt"] = computeBestTime(s.uptime["bot.mqtt"], d && d.body.last_saw_mq); } return s; }) .add(Actions.RESET_NETWORK, (s, _) => { - type Keys = (keyof ConnectionState)[]; + type Keys = (keyof ConnectionState["uptime"])[]; const keys: Keys = ["bot.mqtt", "user.mqtt", "user.api"]; - keys.map(x => (s[x] = undefined)); + keys.map(x => (s.uptime[x] = undefined)); return s; }); diff --git a/frontend/controls/state_to_props.ts b/frontend/controls/state_to_props.ts index 286dff8fc..9640d2cc9 100644 --- a/frontend/controls/state_to_props.ts +++ b/frontend/controls/state_to_props.ts @@ -34,7 +34,7 @@ export function mapStateToProps(props: Everything): Props { bot: props.bot, peripherals: uniq(selectAllPeripherals(props.resources.index)), sensors: uniq(selectAllSensors(props.resources.index)), - botToMqttStatus: getStatus(props.bot.connectivity["bot.mqtt"]), + botToMqttStatus: getStatus(props.bot.connectivity.uptime["bot.mqtt"]), firmwareSettings: fwConfig || mcu_params, getWebAppConfigVal: getWebAppConfigValue(() => props), shouldDisplay, diff --git a/frontend/devices/connectivity/generate_data.ts b/frontend/devices/connectivity/generate_data.ts index ec5172a64..6265a24b1 100644 --- a/frontend/devices/connectivity/generate_data.ts +++ b/frontend/devices/connectivity/generate_data.ts @@ -17,9 +17,9 @@ export const connectivityData = (props: ConnectivityDataProps) => { /** A record of all the things we know about connectivity right now. */ const data: Record = { - userMQTT: browserToMQTT(props.bot.connectivity["user.mqtt"]), - userAPI: browserToAPI(props.bot.connectivity["user.api"]), - botMQTT: botToMQTT(props.bot.connectivity["bot.mqtt"]), + userMQTT: browserToMQTT(props.bot.connectivity.uptime["user.mqtt"]), + userAPI: browserToAPI(props.bot.connectivity.uptime["user.api"]), + botMQTT: botToMQTT(props.bot.connectivity.uptime["bot.mqtt"]), botAPI: botToAPI(props.device.body.last_saw_api), botFirmware: botToFirmware(fwVersion), }; diff --git a/frontend/devices/reducer.ts b/frontend/devices/reducer.ts index 53551efa0..bf9b7f23d 100644 --- a/frontend/devices/reducer.ts +++ b/frontend/devices/reducer.ts @@ -73,9 +73,12 @@ export let initialState = (): BotState => ({ currentBetaOSVersion: undefined, minOsFeatureData: undefined, connectivity: { - "bot.mqtt": undefined, - "user.mqtt": undefined, - "user.api": undefined + uptime: { + "bot.mqtt": undefined, + "user.mqtt": undefined, + "user.api": undefined + }, + pings: {} } }); @@ -152,7 +155,7 @@ export let botReducer = generateReducer(initialState()) s.hardware.informational_settings.sync_status = undefined; break; case "up": - const currentState = s.connectivity["bot.mqtt"]; + const currentState = s.connectivity.uptime["bot.mqtt"]; // Going from "down" to "up" const backOnline = currentState && currentState.state === "down"; backOnline && unstash(s); diff --git a/frontend/devices/state_to_props.ts b/frontend/devices/state_to_props.ts index 40fd12768..983281205 100644 --- a/frontend/devices/state_to_props.ts +++ b/frontend/devices/state_to_props.ts @@ -33,9 +33,9 @@ export function mapStateToProps(props: Everything): Props { ? reduceFarmwareEnv(props.resources.index) : props.bot.hardware.user_env; return { - userToApi: props.bot.connectivity["user.api"], - userToMqtt: props.bot.connectivity["user.mqtt"], - botToMqtt: props.bot.connectivity["bot.mqtt"], + userToApi: props.bot.connectivity.uptime["user.api"], + userToMqtt: props.bot.connectivity.uptime["user.mqtt"], + botToMqtt: props.bot.connectivity.uptime["bot.mqtt"], deviceAccount: getDeviceAccountSettings(props.resources.index), auth: props.auth, bot: props.bot, diff --git a/frontend/farm_designer/move_to.tsx b/frontend/farm_designer/move_to.tsx index bca47c10c..1c0c4e820 100644 --- a/frontend/farm_designer/move_to.tsx +++ b/frontend/farm_designer/move_to.tsx @@ -19,7 +19,7 @@ import { isBotOnline } from "../devices/must_be_online"; import { getStatus } from "../connectivity/reducer_support"; export function mapStateToProps(props: Everything): MoveToProps { - const botToMqttStatus = getStatus(props.bot.connectivity["bot.mqtt"]); + const botToMqttStatus = getStatus(props.bot.connectivity.uptime["bot.mqtt"]); const { sync_status } = props.bot.hardware.informational_settings; return { chosenLocation: props.resources.consumers.farm_designer.chosenLocation, diff --git a/frontend/farmware/__tests__/state_to_props_test.tsx b/frontend/farmware/__tests__/state_to_props_test.tsx index e5c6755ed..a65459756 100644 --- a/frontend/farmware/__tests__/state_to_props_test.tsx +++ b/frontend/farmware/__tests__/state_to_props_test.tsx @@ -136,7 +136,7 @@ describe("mapStateToProps()", () => { it("returns bot status", () => { const state = fakeState(); state.bot.hardware.informational_settings.sync_status = "sync_now"; - state.bot.connectivity["bot.mqtt"] = { state: "up", at: "" }; + state.bot.connectivity.uptime["bot.mqtt"] = { state: "up", at: "" }; const props = mapStateToProps(state); expect(props.syncStatus).toEqual("sync_now"); expect(props.botToMqttStatus).toEqual("up"); diff --git a/frontend/farmware/state_to_props.ts b/frontend/farmware/state_to_props.ts index 2b64f2760..37b842550 100644 --- a/frontend/farmware/state_to_props.ts +++ b/frontend/farmware/state_to_props.ts @@ -107,7 +107,7 @@ export function mapStateToProps(props: Everything): FarmwareProps { .reverse() .value(); - const botToMqttStatus = getStatus(props.bot.connectivity["bot.mqtt"]); + const botToMqttStatus = getStatus(props.bot.connectivity.uptime["bot.mqtt"]); const syncStatus = props.bot.hardware.informational_settings.sync_status; return { diff --git a/frontend/redux/state_fetch_middleware.ts b/frontend/redux/state_fetch_middleware.ts index 2e11f7133..f0323aaf4 100644 --- a/frontend/redux/state_fetch_middleware.ts +++ b/frontend/redux/state_fetch_middleware.ts @@ -9,7 +9,7 @@ const stateFetchMiddleware: Middleware = (store) => (next) => (action: any) => { // tslint:disable-next-line:no-any const s: Everything = store.getState() as any; - maybeRefresh(s.bot.connectivity["bot.mqtt"]); + maybeRefresh(s.bot.connectivity.uptime["bot.mqtt"]); next(action); };