Skip to content

Commit

Permalink
Merge branch 'master' of github.com:infinimesh/proto
Browse files Browse the repository at this point in the history
  • Loading branch information
slntopp committed Feb 16, 2024
2 parents 08b66c4 + ec95216 commit 518c6b5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
connectrpc.com/connect v1.14.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/grpc v1.61.0
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe h1:
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
Expand Down
4 changes: 2 additions & 2 deletions mocks/es/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const transport = createRouterTransport(({ service }) => {
const nodes = new Map<string, Node[]>()

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

for (const devices of devicesByNs.values()) {
const device = devices.find(({ uuid: id }) => id === uuid)
const device: any = devices.find(({ uuid: id }) => id === uuid)

if (device) {
device[key] = value
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 = new Map<string, Account[]>()
const nodes = new Map<string, Node[]>()
const accountsByNs: any = new Map<string, Account[]>()
const nodes: any = 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 }) => uuid === request.account
({ uuid }: any) => uuid === request.account
)

if (account?.access) {
Expand Down
63 changes: 63 additions & 0 deletions mocks/es/shadows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Struct, Timestamp } from '@bufbuild/protobuf'
import { createPromiseClient, createRouterTransport } from '@connectrpc/connect'
import { DevicesService, ShadowService } from 'infinimesh-proto/build/es/node/node_connect'
import { QueryRequest } from 'infinimesh-proto/build/es/node/node_pb'
import { ConnectionState, GetResponse, Shadow, State } from 'infinimesh-proto/build/es/shadow/shadow_pb'
import { transport as devicesTransport } from './devices.ts'

export const transport = createRouterTransport(({ service }) => {
const devicesApi = createPromiseClient(DevicesService, devicesTransport)
const shadows = new Map<string, Shadow>()

devicesApi.list(new QueryRequest()).then(({ devices }) => {
devices.forEach(({ uuid }) => {
const length = Math.floor(Math.random() * 6)
const timestamp = new Timestamp({ seconds: BigInt(Math.round(Date.now() / 1000)) })

shadows.set(uuid, new Shadow({
connection: new ConnectionState({
clientId: `${length}`, connected: true, timestamp
}),
desired: new State({ data: new Struct(), timestamp }),
reported: new State({ data: new Struct(), timestamp }),
device: uuid
}))
})
})

service(ShadowService, {
get (request) {
return new GetResponse({
shadows: request.pool.map((uuid) => shadows.get(uuid)) as Shadow[]
})
},
patch (request) {
shadows.set(request.device, request)
return new Shadow(request)
},
remove (request) {
const result = shadows.get(request.device)

shadows.delete(request.device)
return new Shadow(result)
},
streamShadow (request) {
return {
[Symbol.asyncIterator]: () => ({
next: async () => (request.devices.length > 0)
? { value: new Shadow(shadows.get(request.devices.pop() as string)), done: false }
: { value: null, done: true }
})
}
},
streamShadowSync (request) {
return {
[Symbol.asyncIterator]: () => ({
next: async () => (request.devices.length < 1)
? { value: new Shadow(shadows.get(request.devices.pop() as string)), done: false }
: { value: null, done: true }
})
}
}
})
})
24 changes: 12 additions & 12 deletions mocks/es/timeseries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export const transport = createRouterTransport(({ service }) => {
const devicesApi = createPromiseClient(DevicesService, devicesTransport)
const metrics = new Map<string, Map<string, DataPoint[]>>()

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

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

function getMetrics (uuid: string) {
function getMetrics(uuid: string) {
const metricsList: Metric[] = []

metrics.get(uuid)?.forEach((value, name) => {
Expand Down Expand Up @@ -54,26 +54,26 @@ export const transport = createRouterTransport(({ service }) => {
})

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

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

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

const dataPoints = fields?.get(field)?.filter(({ ts }) =>
ts >= request.from && (request.to) ? ts <= request.to : true
)

fieldsInfo.push(new MetricInfo({ field, dataPoints }))
const dto = { field, dataPoints }
fieldsInfo.push(new MetricInfo(dto))
})

return new ReadResponse({ fieldsInfo })
return new ReadResponse({ metricsInfo: fieldsInfo })
},
async stat (request) {
async stat(request) {
await new Promise((resolve) => setTimeout(resolve, 300))
const deviceMetrics: DeviceMetric[] = []

Expand All @@ -99,7 +99,7 @@ export const transport = createRouterTransport(({ service }) => {

return new StatResponse({ deviceMetrics })
},
write (request) {
write(request: any) {
const dataPoint = request.dataPoint ?? new DataPoint()
const metric = new Map([[request.field, [dataPoint]]])
const value = metrics.get(request.device)
Expand All @@ -114,7 +114,7 @@ export const transport = createRouterTransport(({ service }) => {

return new WriteResponse({ result: true, ts: BigInt(Date.now()) })
},
writeBulk (request) {
writeBulk(request: any) {
const dataPoint = request.dataPoint ?? new DataPoint()
const metric = new Map([[request.field, [dataPoint]]])
const value = metrics.get(request.device)
Expand All @@ -129,7 +129,7 @@ export const transport = createRouterTransport(({ service }) => {

return new WriteBulkResponse({ result: true })
},
async flush (request) {
async flush(request) {
if (request.namespace) {
const { devices } = await devicesApi.list(new QueryRequest({
namespace: request.namespace
Expand Down

0 comments on commit 518c6b5

Please sign in to comment.