Skip to content

Commit

Permalink
Merge pull request #102 from infinimesh/dev-mocks-fixes
Browse files Browse the repository at this point in the history
fixed timeseries requests in mocks
  • Loading branch information
slntopp authored Mar 20, 2024
2 parents d28cb8d + f358b51 commit 47faf1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
9 changes: 5 additions & 4 deletions mocks/es/devices.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { v4 as uuidv4 } from 'uuid'
import { JsonValue, Struct } from '@bufbuild/protobuf'
import { createPromiseClient, createRouterTransport, ConnectError, Code } from '@connectrpc/connect'
import { DevicesService, NamespacesService } from '../../build/es/node/node_connect'
import { DeleteResponse, EmptyMessage, TokenResponse } from '../../build/es//node/node_pb'
import { Access, Nodes, Node, Role } from '../../build/es/node/access/access_pb'
import { Certificate, CreateResponse, Device, Devices } from '../../build/es/node/devices/devices_pb'
import { transport as nsTransport } from './namespaces.ts'
import { v4 as uuidv4 } from 'uuid'

export const transport = createRouterTransport(({ service }) => {
const namespaceApi = createPromiseClient(NamespacesService, nsTransport)
const devices = new Map<string, Device>()
const devicesByNs = new Map<string, Device[]>()
const nodes = new Map<string, Node[]>()

function changeDevice(key: string, uuid: string, value?: any) {
function changeDevice(key: string, uuid: string, value?: JsonValue) {
const device = devices.get(uuid) ?? new Device({ uuid })

if (value ?? true) value = !device[key]
for (const devices of devicesByNs.values()) {
const device: any = devices.find(({ uuid: id }) => id === uuid)
const device = devices.find(({ uuid: id }) => id === uuid)

if (device) {
device[key] = value
Expand Down Expand Up @@ -49,7 +50,7 @@ export const transport = createRouterTransport(({ service }) => {
tags: Array.from({ length }).map((_, i) => `tag ${i}`),
access: new Access({ namespace: key, level, role }),
enabled: true,
config: {}
config: new Struct({})
})

devices.set(device.uuid, device)
Expand Down
6 changes: 3 additions & 3 deletions mocks/es/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const transport = createRouterTransport(({ service }) => {

const namespaces = new Map<string, Namespace>()
const accountsList = new Map<string, Account>()
const accountsByNs: any = new Map<string, Account[]>()
const nodes: any = new Map<string, Node[]>()
const accountsByNs = new Map<string, Account[]>()
const nodes = new Map<string, Node[]>()

const role = Math.floor(Math.random() * 2 + 1)
const level = Math.floor(Math.random() * 4 + 1)
Expand Down Expand Up @@ -88,7 +88,7 @@ export const transport = createRouterTransport(({ service }) => {
},
join(request) {
const account = accountsByNs.get(request.namespace)?.find(
({ uuid }: any) => uuid === request.account
({ uuid }) => uuid === request.account
)

if (account?.access) {
Expand Down
42 changes: 20 additions & 22 deletions mocks/es/timeseries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Value } from '@bufbuild/protobuf'
import { createPromiseClient, createRouterTransport } from '@connectrpc/connect'
import { TimeseriesService } from '../../build/es/timeseries/timeseries_connect'
import {
Expand All @@ -13,11 +12,11 @@ export const transport = createRouterTransport(({ service }) => {
const metrics = new Map<string, Map<string, DataPoint[]>>()

function getField() {
return [50, 40, 30, 20, 10].map((num) => {
return [100, 90, 80, 70, 60, 50, 40, 30, 20, 10].map((num) => {
const ts = BigInt(Date.now() - num * 3600 * 1000)
const value = Value.fromJson(Math.floor(Math.random() * 101))
const value = Math.floor(Math.random() * 101)

return new DataPoint({ ts, value: +value })
return new DataPoint({ ts, value })
})
}

Expand All @@ -26,8 +25,8 @@ export const transport = createRouterTransport(({ service }) => {

metrics.get(uuid)?.forEach((value, name) => {
const dataPoints = value.length
let earliest: DataPoint = value[0]
let latest: DataPoint = value[0]
let earliest = value[0]
let latest = value[0]

value.forEach((point) => {
if (point.ts < earliest.ts) {
Expand All @@ -54,21 +53,21 @@ export const transport = createRouterTransport(({ service }) => {
})

service(TimeseriesService, {
read(request: any) {
read(request) {
const fieldsInfo: MetricInfo[] = []

if (!metrics.has(request.device)) {
metrics.set(request.device, new Map())
}

request.fields?.forEach((field: any) => {
request.metrics?.forEach((metric) => {
const fields = metrics.get(request.device)

const dataPoints = fields?.get(field)?.filter(({ ts }) =>
const dataPoints = fields?.get(metric)?.filter(({ ts }) =>
ts >= request.from && (request.to) ? ts <= request.to : true
)
const dto = { field, dataPoints }
fieldsInfo.push(new MetricInfo(dto))

fieldsInfo.push(new MetricInfo({ metric, dataPoints }))
})

return new ReadResponse({ metricsInfo: fieldsInfo })
Expand Down Expand Up @@ -99,32 +98,31 @@ export const transport = createRouterTransport(({ service }) => {

return new StatResponse({ deviceMetrics })
},
write(request: any) {
write(request) {
const dataPoint = request.dataPoint ?? new DataPoint()
const metric = new Map([[request.field, [dataPoint]]])
const metric = new Map([[request.metric, [dataPoint]]])
const value = metrics.get(request.device)

if (!value) {
metrics.set(request.device, metric)
} else if (!value.has(request.field)) {
value.set(request.field, [dataPoint])
} else if (!value.has(request.metric)) {
value.set(request.metric, [dataPoint])
} else {
value.get(request.field)?.push(dataPoint)
value.get(request.metric)?.push(dataPoint)
}

return new WriteResponse({ result: true, ts: BigInt(Date.now()) })
},
writeBulk(request: any) {
const dataPoint = request.dataPoint ?? new DataPoint()
const metric = new Map([[request.field, [dataPoint]]])
writeBulk(request) {
const metric = new Map([[request.metric, request.dataPoints]])
const value = metrics.get(request.device)

if (!value) {
metrics.set(request.device, metric)
} else if (!value.has(request.field)) {
value.set(request.field, [dataPoint])
} else if (!value.has(request.metric)) {
value.set(request.metric, request.dataPoints)
} else {
value.get(request.field)?.push(dataPoint)
value.get(request.metric)?.push(...request.dataPoints)
}

return new WriteBulkResponse({ result: true })
Expand Down

0 comments on commit 47faf1e

Please sign in to comment.