-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.js
44 lines (36 loc) · 1.11 KB
/
producer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { connectionOptions } from './options'
import { ScynetTypes } from './protobufs'
import { unixToDate } from './util'
import { Kafka } from 'kafkajs'
const kafka = new Kafka({
clientId: 'cryptocompare-harvester',
brokers: connectionOptions.kafkaBrokers
})
// Producing
const producer = kafka.producer()
function encodeBlob (values) {
let shape = []
let testArray = values
while (Array.isArray(testArray)) {
shape.push(testArray.length)
testArray = testArray[0]
}
let blob = { data: values, shape: { dimension: shape } }
return ScynetTypes.Blob.encode(blob).finish()
}
export async function connect (uuid, shape) {
await producer.connect()
}
export var agents = []
export function addProducer (uuid, shape) {
let topic = uuid + ''
agents.push({ uuid, shape })
console.log(`Added agent ${uuid} (topic ${topic}) with shape ${shape}!`)
return function (key, values) {
// console.log(`Agent ${uuid} produced value ${values}\t at ${key} (${unixToDate(key | 0).toISOString()})`)
producer.send({
topic,
messages: [{ key: key.toString(), value: encodeBlob(values) }]
})
}
}