Let io = require('socket.io-client')
// let {influxdb, point, httperror, clientoptions} = await import('@influxdata/influxdb-client')
// import {influxdb, point, httperror, clientoptions} from '@influxdata/influxdb-client'
// let {pingapi} = await import('@influxdata/influxdb-client-apis')
Const influxdb = require('@influxdata/influxdb-client').influxdb;
Const point = require('@influxdata/influxdb-client').point;
Const httperror = require('@influxdata/influxdb-client').httperror;
Const clientoptions = require('@influxdata/influxdb-client').clientoptions;

Const username = <lmao>
Const password = <lmao>
Const database = 'archive'
Const retentionpolicy = 'autogen'

Const bucket = `${database}/${retentionpolicy}`
Const influxdb = new influxdb({ url: 'http://localhost:8086', token: `${username}:${password}`,})
Const writeapi = influxdb.getwriteapi('', bucket)
// console.log(process.argv[2]);
// console.log(`https://tracker.archiveteam.org:8081/${process.argv[2]}-log`)
// process.exit(0)
Project = process.argv[2];

Settimeout(function(){

Console.log("5 mins target reached exiting - ", project)
Process.exit(0)

}, 1000 * 60 * 5); //one hour 1000 * 60 * 60

Const host = `http://tracker.archiveteam.org:8080/${project}-log`
// console.log("type: ", typeof(host))
// console.log("host: ", host)
// process.exit(0)

Let socket = io.connect(host, {'reconnection': true, 'reconnectiondelay': 0, 'reconnectionattempts': infinity})
I = 0
Console.log("starting data collection - ", project);
Socket.on('log_message', function incoming(data) {
Json = json.parse(data);
Let points = [];
// console.log(data)
// console.log('message from server:', json);
Points.push(new point(project).floatfield('size', json.megabytes).tag('downloader', json.downloader));
Points.push(new point(project).floatfield('items', json.items.length).tag('downloader', json.downloader));
Points.push(new point(project).tag('version', json.version).stringfield('version', json.version));
Points.push(new point(project).floatfield('rtt_real', json.stats.values.rtt_real));
Points.push(new point(project).intfield('done', json.stats.values.done_counter));
Points.push(new point(project).floatfield('irsr', json.stats.values.item_request_serve_rate));
Points.push(new point(project).intfield('claims', json.counts.out));
Points.push(new point(project).intfield('todo', json.counts.todo));
Points.push(new point(project).intfield('secondary', json.stats.queues['todo:secondary']));
Points.push(new point(project).intfield('redo', json.stats.queues['todo:redo']));
Points.push(new point(project).intfield('unretrievable', json.stats.queues['unretrievable']));
Points.push(new point(project).intfield('backfeed', json.stats.queues['todo:backfeed']));
Points.push(new point(project).floatfield('ifr', json.stats.values.item_fail_rate));
Points.push(new point(project).floatfield('rr', json.stats.values.reclaim_rate));
Points.push(new point(project).floatfield('rsr', json.stats.values.reclaim_serve_rate));
Writeapi.writepoints(points);
// console.log('pushed: ', points);
// console.log('mbs: ', json.megabytes)
// console.log('item count: ', json.items.length)
// console.log('version:', json.version)
// console.log('downloader:', json.downloader)
// console.log('rtt_real: ', json.stats.values.rtt_real)
// console.log('done counter:', json.stats.values.done_counter)
// console.log('item_request_serve_rate:', json.stats.values.item_request_serve_rate)
// console.log('claims:', json.counts.out)
// console.log('todo', json.counts.todo)
I++;
Writeapi.flush();
// if(i > 30000)
// {
// writeapi.flush().then(r => process.exit(0));
// console.log('exiting => ', i);
// }
});

Socket.on("connect_error", (err) => {
Console.log(`connect_error due to ${err.message}`);
});