[UNSTABLE] Switch connectivity panel to use UNIX instead of JSON dates

pull/1405/head
Rick Carlino 2019-09-05 10:16:25 -05:00
parent adfb11cd1b
commit a0ebd30a32
13 changed files with 46 additions and 44 deletions

View File

@ -10,15 +10,15 @@ import { autoSync, routeMqttData } from "../auto_sync";
import { handleInbound } from "../auto_sync_handle_inbound";
const NOW = new Date();
const SHORT_TIME_LATER = new Date(NOW.getTime() + 500);
const LONGER_TIME_LATER = new Date(NOW.getTime() + 5000);
const SHORT_TIME_LATER = new Date(NOW.getTime() + 500).getTime();
const LONGER_TIME_LATER = new Date(NOW.getTime() + 5000).getTime();
describe("dispatchNetworkUp", () => {
const NOW_UP = networkUp("bot.mqtt", NOW.toJSON(), "tests");
const LATER_UP = networkUp("bot.mqtt", LONGER_TIME_LATER.toJSON(), "tests");
const NOW_UP = networkUp("bot.mqtt", NOW.getTime(), "tests");
const LATER_UP = networkUp("bot.mqtt", LONGER_TIME_LATER, "tests");
it("calls redux directly", () => {
dispatchNetworkUp("bot.mqtt", NOW, "tests");
dispatchNetworkUp("bot.mqtt", NOW.getTime(), "tests");
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
dispatchNetworkUp("bot.mqtt", SHORT_TIME_LATER, "tests");
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_UP);
@ -28,11 +28,11 @@ describe("dispatchNetworkUp", () => {
});
describe("dispatchNetworkDown", () => {
const NOW_DOWN = networkDown("user.api", NOW.toJSON(), "tests");
const LATER_DOWN = networkDown("user.api", LONGER_TIME_LATER.toJSON(), "tests");
const NOW_DOWN = networkDown("user.api", NOW.getTime(), "tests");
const LATER_DOWN = networkDown("user.api", LONGER_TIME_LATER, "tests");
it("calls redux directly", () => {
dispatchNetworkDown("user.api", NOW, "tests");
dispatchNetworkDown("user.api", NOW.getTime(), "tests");
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);
dispatchNetworkDown("user.api", SHORT_TIME_LATER, "tests");
expect(mockRedux.store.dispatch).toHaveBeenLastCalledWith(NOW_DOWN);

View File

@ -7,11 +7,11 @@ import { ConnectionStatus } from "../interfaces";
const NOW = "Tue, 03 Oct 2017 09:00:00 -0500";
const LATER = "Wed, 04 Oct 2017 09:00:00 -0500";
const LATER_JSON = moment(LATER).toJSON();
const NOW_JSON = moment(NOW).toJSON();
const LATER_JSON = moment(LATER).toDate().getTime();
const NOW_UNIX = (new Date(NOW)).getTime();
describe("computeBestTime()", () => {
const STUB: ConnectionStatus = { state: "down", at: NOW_JSON };
const STUB: ConnectionStatus = { state: "down", at: NOW_UNIX };
it("returns same input when `last_saw_mq` is unavailable", () => {
expect(computeBestTime(undefined, undefined)).toBe(undefined);
@ -43,6 +43,6 @@ describe("getStatus()", () => {
});
it("returns status.state when given a ConnectionStatus object", () => {
expect(getStatus({ at: NOW, state: "up" })).toBe("up");
expect(getStatus({ at: NOW_UNIX, state: "up" })).toBe("up");
});
});

View File

@ -5,7 +5,7 @@ import { ReduxAction } from "../redux/interfaces";
type NetChange = ReduxAction<EdgeStatus>;
const change = (state: "up" | "down") =>
(name: Edge, at = (new Date()).toJSON(), why: string): NetChange => {
(name: Edge, at = (new Date()).getTime(), why: string): NetChange => {
return {
type: Actions.NETWORK_EDGE_CHANGE,
payload: { name, status: { state, at }, why }

View File

@ -1,7 +1,6 @@
import { store } from "../redux/store";
import { networkUp, networkDown } from "./actions";
import { Edge } from "./interfaces";
import { timestamp } from "../util";
/* ABOUT THIS FILE: These functions allow us to mark the network as up or
down from anywhere within the app (even outside of React-Redux). I usually avoid
@ -25,16 +24,14 @@ function bumpThrottle(edge: Edge, now: number) {
lastCalledAt[edge] = now;
}
export let dispatchNetworkUp = (edge: Edge, at = new Date(), why: string) => {
const unix = timestamp(at);
if (shouldThrottle(edge, unix)) { return; }
store.dispatch(networkUp(edge, at.toJSON(), why));
bumpThrottle(edge, unix);
export let dispatchNetworkUp = (edge: Edge, at = (new Date()).getTime(), why: string) => {
if (shouldThrottle(edge, at)) { return; }
store.dispatch(networkUp(edge, at, why));
bumpThrottle(edge, at);
};
export let dispatchNetworkDown = (edge: Edge, at = new Date(), why: string) => {
const unix = timestamp(at);
if (shouldThrottle(edge, unix)) { return; }
store.dispatch(networkDown(edge, at.toJSON(), why));
bumpThrottle(edge, unix);
export let dispatchNetworkDown = (edge: Edge, at = (new Date()).getTime(), why: string) => {
if (shouldThrottle(edge, at)) { return; }
store.dispatch(networkDown(edge, at, why));
bumpThrottle(edge, at);
};

View File

@ -5,7 +5,7 @@ export type NetworkState = "up" | "down";
/** Description of a connection between two points on the network. */
export interface ConnectionStatus {
state: NetworkState;
at: string;
at: number;
}
export interface EdgeStatus {

View File

@ -18,12 +18,13 @@ export function getStatus(cs: ConnectionStatus | undefined): "up" | "down" {
* unable to make a determination. */
export function computeBestTime(cs: ConnectionStatus | undefined,
lastSawMq: string | undefined): ConnectionStatus | undefined {
const left = m(cs ? cs.at : lastSawMq);
const right = m(lastSawMq);
// Only use the `last_saw_mq` time if it is more recent than the local
// timestamp.
// don't bother guessing if info is unavailable
const guess: ConnectionStatus = {
at: maxDate(m(cs ? cs.at : lastSawMq), m(lastSawMq)),
at: Math.max(left.toDate().getTime(), right.toDate().getTime()),
state: getStatus(cs)
};
return isString(lastSawMq) ? guess : cs;

View File

@ -48,7 +48,7 @@ describe("<Devices/>", () => {
it("has correct connection status", () => {
const p = fakeProps();
p.botToMqtt = { at: "123", state: "up" };
p.botToMqtt = { at: 123, state: "up" };
const wrapper = shallow(<Devices {...p} />);
expect(wrapper.find(FarmbotOsSettings).props().botToMqttLastSeen)
.toEqual("123");

View File

@ -13,7 +13,7 @@ export interface Props {
export class DiagnosticDumpRow extends React.Component<Props, {}> {
get ticket() { return this.props.diag.body.ticket_identifier; }
get age() { return ago(this.props.diag.body.created_at); }
get age() { return ago(new Date(this.props.diag.body.created_at).getTime()); }
destroy = () => this.props.dispatch(destroy(this.props.diag.uuid));

View File

@ -20,7 +20,7 @@ describe("botToAPI()", () => {
});
it("handles unknown connectivity", () => {
const result = botToAPI(undefined, moment());
const result = botToAPI(undefined, (new Date()).getTime());
expect(result.connectionStatus).toBeFalsy();
expect(result.children).toContain("No messages seen yet.");
});
@ -28,7 +28,7 @@ describe("botToAPI()", () => {
describe("botToMQTT()", () => {
const DEFAULT_STATE: ConnectionStatus = {
at: "2017-09-27T07:52:37.003-05:00",
at: new Date("2017-09-27T07:52:37.003-05:00").getTime(),
state: "up"
};
function stat(input: Partial<ConnectionStatus> = {}): ConnectionStatus {
@ -48,7 +48,7 @@ describe("botToMQTT()", () => {
expect(result.children).toContain("No recent messages.");
});
});
const NOW = moment().toJSON();
const NOW = (new Date()).getTime();
describe("browserToMQTT()", () => {
it("handles connectivity", () => {
const output = browserToMQTT({ state: "up", at: NOW });
@ -100,7 +100,7 @@ describe("browserToAPI()", () => {
it("handles connectivity", () => {
const result = browserToAPI({
state: "up",
at: moment().toISOString()
at: (new Date).getTime()
});
expect(result.connectionStatus).toBeTruthy();
expect(result.children).toContain("Last message seen a few seconds ago.");
@ -109,7 +109,7 @@ describe("browserToAPI()", () => {
it("handles loss of connectivity", () => {
const result = browserToAPI({
state: "down",
at: moment().toISOString()
at: (new Date).getTime()
});
expect(result.connectionStatus).toBeFalsy();
expect(result.children).toContain("Last message seen a few seconds ago");

View File

@ -13,7 +13,7 @@ const SIX_HOURS = HOUR * 6;
const NOT_SEEN = t("No messages seen yet.");
export function ago(input: string) {
export function ago(input: number) {
return moment(new Date(input)).fromNow();
}
@ -26,14 +26,15 @@ function statusOf(stat: ConnectionStatus | undefined): boolean | undefined {
}
export function botToAPI(stat: string | undefined,
now = moment()): StatusRowProps {
now = (new Date).getTime()): StatusRowProps {
const connectionStatus =
(stat ? ((now - new Date(stat).getTime()) < SIX_HOURS) : false);
return {
connectionName: "botAPI",
from: "FarmBot",
to: "Web App",
connectionStatus: stat ? (now.diff(moment(stat)) < SIX_HOURS) : false,
children: stat ? t("Last message seen ") + `${ago(stat)}.` : NOT_SEEN
connectionStatus,
children: stat ? t("Last message seen ") + `${ago(new Date(stat).getTime())}.` : NOT_SEEN
};
}

View File

@ -31,7 +31,7 @@ export class Devices extends React.Component<Props, {}> {
alerts={this.props.alerts}
bot={this.props.bot}
timeSettings={this.props.timeSettings}
botToMqttLastSeen={botToMqttLastSeen}
botToMqttLastSeen={new Date(botToMqttLastSeen).toJSON()}
botToMqttStatus={botToMqttStatus}
sourceFbosConfig={this.props.sourceFbosConfig}
shouldDisplay={this.props.shouldDisplay}

View File

@ -136,7 +136,10 @@ describe("mapStateToProps()", () => {
it("returns bot status", () => {
const state = fakeState();
state.bot.hardware.informational_settings.sync_status = "sync_now";
state.bot.connectivity.uptime["bot.mqtt"] = { state: "up", at: "" };
state.bot.connectivity.uptime["bot.mqtt"] = {
state: "up",
at: (new Date()).getTime()
};
const props = mapStateToProps(state);
expect(props.syncStatus).toEqual("sync_now");
expect(props.botToMqttStatus).toEqual("up");

View File

@ -19,10 +19,10 @@ describe("createRefreshTrigger", () => {
it("calls the bot when going from down => up", () => {
const go = createRefreshTrigger();
go({ at: "?", state: "down" });
go({ at: "?", state: "down" });
go({ at: 0, state: "down" });
go({ at: 0, state: "down" });
expect(changeLastClientConnected).not.toHaveBeenCalled();
go({ at: "?", state: "up" });
go({ at: 0, state: "up" });
expect(changeLastClientConnected).toHaveBeenCalled();
expect(maybeGetDevice).toHaveBeenCalled();
});