Spaces:
Running
Running
File size: 1,766 Bytes
ecdd7f6 | 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | const fs = require('fs');
const fetch = require('node-fetch');
const { logger } = require('./logging');
const TELEMETRY_PATH = 'telemetry.log';
const PROCESS_ID = Math.floor(Math.random() * 1e10).toString(16);
let receivedCount = 0;
let telemetry = {};
function count(label) {
receive(PROCESS_ID, label, 1);
}
function receive(pid, label, count) {
if (!pid || !label || !count) {
return;
}
if (!telemetry[pid]) {
telemetry[pid] = {};
}
if (!telemetry[pid][label]) {
telemetry[pid][label] = 0;
}
telemetry[pid][label] += count;
receivedCount++;
}
function write() {
if (!receivedCount) {
return;
}
logger.info(`Writing ${receivedCount} telemetry records...`);
telemetry.timestamp = new Date().getTime();
if (process.env.WRITE_TELEMETRY_TO_CONSOLE) {
logger.info('Telemetry', JSON.stringify(telemetry));
} else {
fs.appendFileSync(TELEMETRY_PATH, JSON.stringify(telemetry) + '\n');
}
telemetry = {};
receivedCount = 0;
}
function send() {
if (!telemetry[PROCESS_ID]) {
return;
}
const data = {
pid: PROCESS_ID,
chartCount: telemetry[PROCESS_ID].chartCount,
qrCount: telemetry[PROCESS_ID].qrCount,
};
try {
fetch('https://quickchart.io/telemetry', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'content-type': 'application/json',
},
});
} catch (err) {}
telemetry = {};
}
if (process.env.ENABLE_TELEMETRY_WRITE) {
logger.info('Telemetry writing is enabled');
setInterval(() => {
write();
}, 1000 * 60 * 60 * 1);
}
if (!process.env.DISABLE_TELEMETRY) {
logger.info('Telemetry is enabled');
setInterval(() => {
send();
}, 1000 * 60 * 60 * 12);
}
module.exports = {
count,
receive,
write,
};
|