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}`);
});