Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions packages/data-sdk/src/Fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,32 @@ export class Fleet {
if (!Array.isArray(streamNameOrStreamNames)) {
streamNames = [streamNameOrStreamNames];
}
const data = await fetch(`${FORMANT_API_URL}/v1/queries/queries`, {
method: "POST",
body: JSON.stringify({
const query = JSON.parse(
JSON.stringify({
deviceIds,
end: end.toISOString(),
names: streamNames,
start: start.toISOString(),
start,
end,
tags,
}),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + Authentication.token,
},
});
const telemetry = await data.json();
return telemetry.items;
})
);
let nextToken = null;
let results: any = { items: [] };
do {
if (nextToken) query.next = nextToken;
const data = await fetch(`${FORMANT_API_URL}/v1/queries/queries`, {
method: "POST",
body: JSON.stringify(query),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + Authentication.token,
},
});
const telemetry = await data.json();
nextToken = telemetry.next;
results.items = [...results.items, ...telemetry.items];
} while (nextToken !== undefined);
return results.items;
}

static async getFileUrl(uuid: string) {
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-sdk/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export { default as useDevice } from "./useDevice";
export { default as useQueryTelemetry } from "./useQueryTelemetry";
export { default as useQueryTelemetryByDays } from "./useQueryTelemetryByDays";
export { default as useLatestTelemetry } from "./useLatestTelemetry";
export { default as useModuleDataListener } from "./useModuleDataListener";

64 changes: 64 additions & 0 deletions packages/ui-sdk/src/hooks/useModuleDataListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useEffect, useState } from "react";
import { App, ModuleData } from "@formant/data-sdk";

interface data {
agentId: string;
deviceId: string;
name: string;
tags: { [key: string]: string };
points: [number, any][];
type: string;
}

interface Stream {
loading: boolean;
tooMuchData: boolean;
data: data[];
type: string;
}

type Streams = { [key: string]: any };

//Intended to be use for custom modules within Formant

const useModuleDataListener = () => {
const [streams, setStreams] = useState<Streams>({});
useEffect(() => {
App.addModuleDataListener(_cleanData);
}, []);

const _cleanData = (moduleData: ModuleData) => {
let streams: any = Object.values(moduleData.streams);
//check is is there any stream coming in
if (streams.length === 0) {
throw new Error("No streams.");
}
setStreams(
streams.reduce((_: any, stream: Stream) => {
if (stream === undefined) {
throw new Error("No stream.");
}
if (stream.loading) {
return undefined;
}
if (stream.tooMuchData) {
throw new Error("Too much data.");
}
if (stream.data.length === 0) {
throw new Error("No data.");
}
const latestPoint = stream.data[0].points.at(-1);

if (!latestPoint) {
throw new Error("No datapoints.");
}

return { ..._, [stream.data[0].name]: latestPoint[1] };
}, {})
);
};
//Returns an object with stream name as key and last know value
return streams;
};

export default useModuleDataListener;