-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateOrUpdateDataChart.mjs
89 lines (76 loc) · 2.22 KB
/
createOrUpdateDataChart.mjs
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
89
import fs from "node:fs/promises";
import { access, writeFileSync, readFileSync } from "node:fs";
function createOrUpdateDataChart(file, source) {
//read last crux
const url = new URL(source, import.meta.url);
const json = readFileSync(url, "utf-8");
const data = JSON.parse(json);
// update or create
access(file, (err) => {
if (err) {
const newData = prepareEmptyDataForChart(data);
let ds = JSON.stringify(newData);
writeFileSync(file, ds);
console.log("created");
} else {
const url = new URL(file, import.meta.url);
const json = readFileSync(url, "utf-8");
const allFCP = JSON.parse(json);
const updateAllFCP = updateCharData(data, allFCP);
let ns = JSON.stringify(updateAllFCP);
writeFileSync(file, ns);
console.log("updated");
}
});
}
function getMetric(data, metric) {
const dataConverted = data.metrics.map((item) => {
let obj = item[metric] || {};
obj.url = item.url.replace("https://", "");
return obj;
});
let metricByUrl = {};
dataConverted.forEach((item) => {
metricByUrl[item.url] = item.p75;
});
return metricByUrl;
}
// push new data to data chart
function updateCharData(data, dataFromFile) {
Object.keys(dataFromFile).forEach((metric) => {
const metricData = getMetric(data, metric);
dataFromFile[metric].series.map((item) => {
item.data.push(metricData[item.name]);
return item;
});
dataFromFile[metric].categories.push(data.params.date);
});
return dataFromFile;
}
//prepare data for chart for the first time
function prepareEmptyDataForChart(data) {
let tmpl = {};
["FCP", "LCP", "INP", "CLS", "TTFB"].forEach((metric) => {
tmpl[metric] = {
series: [],
categories: [],
};
const metricData = getMetric(data, metric);
Object.keys(metricData).forEach((url) => {
tmpl[metric].series.push({
name: url,
data: [metricData[url]],
});
});
tmpl[metric].categories.push(data.params.date);
});
return tmpl;
}
createOrUpdateDataChart(
"public/data-chart/crux-origin-all.json",
"src/_data/ecommerce-pl-origin.json"
);
createOrUpdateDataChart(
"public/data-chart/crux-home-all.json",
"src/_data/ecommerce-pl-url.json"
);