|
1 | | -import { CONDUIT_PORT } from '../constants'; |
2 | | -import { AppState, Packet } from '../types'; |
| 1 | +import { Fabricate } from 'fabricate.js'; |
| 2 | +import { |
| 3 | + AppState, DataPoint, Device, DeviceApp, |
| 4 | + Packet, |
| 5 | +} from '../types'; |
| 6 | +import { BUCKET_SIZE, CONDUIT_PORT, FLEET_HOST } from '../constants'; |
| 7 | +import { shortDateTime, sortAppByName } from '../util'; |
| 8 | + |
| 9 | +declare const fabricate: Fabricate<AppState>; |
| 10 | + |
| 11 | +/** Extra Y for visibility */ |
| 12 | +const Y_EXTRA = 1.1; |
3 | 13 |
|
4 | 14 | /** |
5 | 15 | * Send a conduit packet. |
@@ -54,3 +64,153 @@ export const sendConduitPacket = async ( |
54 | 64 | throw error; |
55 | 65 | } |
56 | 66 | }; |
| 67 | + |
| 68 | +/** |
| 69 | + * Re-load the devices list data. |
| 70 | + * |
| 71 | + * @param {object} state - App state. |
| 72 | + */ |
| 73 | +export const fetchFleetList = async (state: AppState) => { |
| 74 | + const { token } = state; |
| 75 | + fabricate.update({ devices: [] }); |
| 76 | + |
| 77 | + try { |
| 78 | + // Can't use sendConduitPacket, not a device by name |
| 79 | + const res = await fetch(`http://${FLEET_HOST}:${CONDUIT_PORT}/conduit`, { |
| 80 | + method: 'POST', |
| 81 | + headers: { 'Content-Type': 'application/json' }, |
| 82 | + body: JSON.stringify({ |
| 83 | + to: 'attic', |
| 84 | + topic: 'get', |
| 85 | + message: { app: 'conduit', key: 'fleetList' }, |
| 86 | + auth: token || '', |
| 87 | + }), |
| 88 | + }); |
| 89 | + const { message } = await res.json(); |
| 90 | + fabricate.update({ devices: message.value }); |
| 91 | + } catch (err) { |
| 92 | + console.error(err); |
| 93 | + alert(err); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * Load apps for all fleet devices. |
| 99 | + * |
| 100 | + * @param {AppState} state - App state. |
| 101 | + * @param {Device} device - Device to use. |
| 102 | + */ |
| 103 | +export const fetchDeviceApps = async (state: AppState, device: Device) => { |
| 104 | + const { deviceName } = device!; |
| 105 | + |
| 106 | + fabricate.update({ selectedDeviceApps: [] }); |
| 107 | + |
| 108 | + try { |
| 109 | + const { message } = await sendConduitPacket( |
| 110 | + state, |
| 111 | + { to: 'conduit', topic: 'getApps' }, |
| 112 | + deviceName, |
| 113 | + ); |
| 114 | + |
| 115 | + let selectedDeviceApps: DeviceApp[] = []; |
| 116 | + if (message && message.error) { |
| 117 | + console.error(message.error); |
| 118 | + selectedDeviceApps = []; |
| 119 | + } else if (!message) { |
| 120 | + console.error('No response in fetchApps'); |
| 121 | + selectedDeviceApps = []; |
| 122 | + } else { |
| 123 | + selectedDeviceApps = (message as DeviceApp[]).sort(sortAppByName); |
| 124 | + } |
| 125 | + |
| 126 | + fabricate.update({ selectedDeviceApps }); |
| 127 | + } catch (err: unknown) { |
| 128 | + console.error(err); |
| 129 | + fabricate.update({ selectedDeviceApps: [] }); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +/** |
| 134 | + * Fetch data for a metric. |
| 135 | + * |
| 136 | + * @param {AppState} state - App state. |
| 137 | + * @param {string} name - Metric name. |
| 138 | + */ |
| 139 | +export const fetchMetric = async (state: AppState, name: string) => { |
| 140 | + const dataKey = fabricate.buildKey('metricData', name); |
| 141 | + fabricate.update( |
| 142 | + dataKey, |
| 143 | + { |
| 144 | + buckets: [], |
| 145 | + minTime: 0, |
| 146 | + maxTime: 0, |
| 147 | + minValue: 0, |
| 148 | + maxValue: 0, |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
| 152 | + const res = await sendConduitPacket( |
| 153 | + state, |
| 154 | + { |
| 155 | + to: 'monitor', |
| 156 | + topic: 'getMetricToday', |
| 157 | + message: { name }, |
| 158 | + }, |
| 159 | + ); |
| 160 | + const { message: newHistory } = res; |
| 161 | + if (res.error) console.log(res); |
| 162 | + if (!newHistory.length) return; |
| 163 | + |
| 164 | + const type = Array.isArray(newHistory[0][1]) ? 'array' : 'number'; |
| 165 | + |
| 166 | + const minTime = shortDateTime(newHistory[0][0]); |
| 167 | + const maxTime = shortDateTime(newHistory[newHistory.length - 1][0]); |
| 168 | + let minValue = 0; |
| 169 | + let maxValue = 0; |
| 170 | + |
| 171 | + if (type === 'number') { |
| 172 | + // Aggregate values |
| 173 | + minValue = name.includes('Perc') |
| 174 | + ? 0 |
| 175 | + : newHistory.reduce( |
| 176 | + // @ts-expect-error handled with 'type' |
| 177 | + (acc: number, [, value]: MetricPoint) => (value < acc ? value : acc), |
| 178 | + 9999999, |
| 179 | + ); |
| 180 | + maxValue = name.includes('Perc') |
| 181 | + ? Math.round(100 * Y_EXTRA) |
| 182 | + : Math.round( |
| 183 | + newHistory.reduce( |
| 184 | + // @ts-expect-error handled with 'type' |
| 185 | + (acc: number, [, value]: MetricPoint) => (value > acc ? value : acc), |
| 186 | + 0, |
| 187 | + ) * Y_EXTRA, |
| 188 | + ); |
| 189 | + } else { |
| 190 | + throw new Error('Unexpected metric data type'); |
| 191 | + } |
| 192 | + |
| 193 | + // Average into buckets |
| 194 | + const copy = [...newHistory]; |
| 195 | + const buckets: DataPoint[] = []; |
| 196 | + while (copy.length) { |
| 197 | + const points = copy.splice(0, BUCKET_SIZE); |
| 198 | + const avgIndex = Math.floor(points.length / 2); |
| 199 | + buckets.push({ |
| 200 | + value: points.reduce((acc, [, value]) => acc + value, 0) / points.length, |
| 201 | + timestamp: points[avgIndex][0], |
| 202 | + dateTime: new Date(points[avgIndex][0]).toISOString(), |
| 203 | + }); |
| 204 | + } |
| 205 | + |
| 206 | + fabricate.update( |
| 207 | + dataKey, |
| 208 | + { |
| 209 | + buckets, |
| 210 | + minTime, |
| 211 | + maxTime, |
| 212 | + minValue, |
| 213 | + maxValue, |
| 214 | + }, |
| 215 | + ); |
| 216 | +}; |
0 commit comments