//WebMetrik - Web Analytics //Git : https://git.raphix.fr/lib/webmetrik //Author : Raphix const basics = require('./basics.js'); const fs = require('fs'); module.exports.setMetricFile = (path) => { basics.setMetricsFile(path); const metricsFile = basics.getMetricsFile(); } module.exports.Metric = class { name; description; value; constructor(name, description) { const metricsFile = basics.getMetricsFile(); //Check if the metric already exists and set the value and description of the metric if(basics.getMetricsFile().findIndex(x => x.name == name) != -1) { this.name = name; this.description = basics.getMetricsFile()[basics.getMetricsFile().findIndex(x => x.name == name)].description; this.value = basics.getMetricsFile()[basics.getMetricsFile().findIndex(x => x.name == name)].value; } else { this.name = name; this.description = description; this.value = 0; //save it to the file basics.saveMetricsFile([...basics.getMetricsFile(), {name: this.name, description: this.description, value: this.value}]) } } setValue(value) { //get the value of the metric from the file and set it to the value of the metric this.value = basics.getMetricsFile()[basics.getMetricsFile().findIndex(x => x.name == this.name)].value; //save it to the file basics.saveMetricsFile(basics.getMetricsFile().map(x => { if(x.name == this.name) { x.value = value; } return x; })) } getValue() { //get the value of the metric from the file and set it to the value of the metric this.value = basics.getMetricsFile()[basics.getMetricsFile().findIndex(x => x.name == this.name)].value; return this.value; } setDescription(description) { //get the value of the metric from the file and set it to the value of the metric this.value = basics.getMetricsFile()[basics.getMetricsFile().findIndex(x => x.name == this.name)].value; //save it to the file basics.saveMetricsFile(basics.getMetricsFile().map(x => { if(x.name == this.name) { x.description = description; } return x; })) } clearMetric() { //delete the metric from the file basics.saveMetricsFile(basics.getMetricsFile().filter(x => x.name != this.name)) } } module.exports.publishMetrics = (port, privateKey) => { const http = require('http'); const url = require('url'); const server = http.createServer((req, res) => { const parsedUrl = url.parse(req.url, true); const pathname = parsedUrl.pathname; const query = parsedUrl.query; if(pathname == "/metrics") { if(query.privatekey == privateKey) { res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(basics.getMetricsFile())); } else { res.writeHead(403, {'Content-Type': 'application/json'}); res.end(JSON.stringify({error: "Invalid private key"})); } } else { res.writeHead(404, {'Content-Type': 'application/json'}); res.end(JSON.stringify({error: "Invalid path"})); } }) server.listen(port, () => { console.log("WebMetrik is listening on port " + port); }) } // CHeck if the metric exitst and delete it module.exports.deleteMetric = (name) => { if(basics.getMetricsFile().findIndex(x => x.name == name) != -1) { basics.saveMetricsFile(basics.getMetricsFile().filter(x => x.name != name)) } }