Add First version
This commit is contained in:
parent
7d8f14c843
commit
bfe15c545e
60
basics.js
Normal file
60
basics.js
Normal file
@ -0,0 +1,60 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
|
||||
module.exports.getDate = function (formated) {
|
||||
|
||||
var date = new Date()
|
||||
|
||||
// [Date Format] - Format de la date
|
||||
|
||||
var gmonth = date.getMonth() + 1
|
||||
var gday = date.getDate()
|
||||
var gHour = date.getHours()
|
||||
var gMinute = date.getMinutes()
|
||||
var gSecondes = date.getSeconds()
|
||||
|
||||
|
||||
if(date.getMonth() + 1 < 10) {
|
||||
gmonth = "0" + (date.getMonth() + 1)
|
||||
}
|
||||
|
||||
if(date.getDate() + 1 <= 10) {
|
||||
gday = "0" + date.getDate()
|
||||
}
|
||||
|
||||
if(date.getHours() + 1 <= 10) {
|
||||
gHour = "0" + date.getHours()
|
||||
}
|
||||
|
||||
if(date.getMinutes() + 1 <= 10) {
|
||||
gMinute = "0" + date.getMinutes()
|
||||
}
|
||||
|
||||
if(date.getSeconds() + 1 <= 10) {
|
||||
gSecondes = "0" + date.getSeconds()
|
||||
}
|
||||
|
||||
if(!formated) {
|
||||
return gday + "/" + gmonth + " - " + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
|
||||
} else {
|
||||
return date.getFullYear() + "-" + gmonth + "-" + gday + "-" + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports.getMetricsFile = () => {
|
||||
if(!fs.existsSync(path.join(__dirname, "/metrics.json"))) {
|
||||
fs.writeFileSync(path.join(__dirname, "/metrics.json"), JSON.stringify([], null, 2))
|
||||
} else {
|
||||
return JSON.parse(fs.readFileSync(path.join(__dirname, "/metrics.json")))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports.saveMetricsFile = (data) => {
|
||||
fs.writeFileSync(path.join(__dirname, "/metrics.json"), JSON.stringify(data, null, 2))
|
||||
}
|
107
main.js
Normal file
107
main.js
Normal file
@ -0,0 +1,107 @@
|
||||
//WebMetrik - Web Analytics
|
||||
//Git : https://git.raphix.fr/lib/webmetrik
|
||||
//Author : Raphix
|
||||
|
||||
const basics = require('./basics.js');
|
||||
const fs = require('fs');
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
7
metrics.json
Normal file
7
metrics.json
Normal file
@ -0,0 +1,7 @@
|
||||
[
|
||||
{
|
||||
"name": "test",
|
||||
"description": "test metric",
|
||||
"value": 20
|
||||
}
|
||||
]
|
9
package.json
Normal file
9
package.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "webmetrik",
|
||||
"version": "0.0.1",
|
||||
"description": "NodeJS - npm package - Make metrics and make it available by making a httpServer",
|
||||
"main": "main.js",
|
||||
"keywords": [],
|
||||
"author": "Raphix",
|
||||
"license": "ISC"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user