profiler charts

This commit is contained in:
Jan Prochazka
2022-12-18 13:48:24 +01:00
parent 9a2631dc09
commit 2e37788471
9 changed files with 110 additions and 45 deletions

View File

@@ -193,46 +193,83 @@ module.exports = {
},
extractTimelineChart_meta: true,
async extractTimelineChart({ jslid, formatterFunction, measures }) {
const formater = requirePluginFunction(formatterFunction);
const datastore = new JsonLinesDatastore(getJslFileName(jslid), formater);
async extractTimelineChart({ jslid, timestampFunction, aggregateFunction, measures }) {
const timestamp = requirePluginFunction(timestampFunction);
const aggregate = requirePluginFunction(aggregateFunction);
const datastore = new JsonLinesDatastore(getJslFileName(jslid));
let mints = null;
let maxts = null;
// pass 1 - counts stats, time range
await datastore.enumRows(row => {
if (!mints || row.ts < mints) mints = row.ts;
if (!maxts || row.ts > maxts) maxts = row.ts;
const ts = timestamp(row);
if (!mints || ts < mints) mints = ts;
if (!maxts || ts > maxts) maxts = ts;
return true;
});
const minTime = new Date(mints).getTime();
const maxTime = new Date(maxts).getTime();
const duration = maxTime - minTime;
const STEPS = 100;
const step = duration / STEPS;
const labels = _.range(STEPS).map(i => new Date(minTime + step / 2 + step * i));
let stepCount = duration > 100 * 1000 ? STEPS : Math.round((maxTime - minTime) / 1000);
if (stepCount < 2) {
stepCount = 2;
}
const stepDuration = duration / stepCount;
const labels = _.range(stepCount).map(i => new Date(minTime + stepDuration / 2 + stepDuration * i));
const datasets = measures.map(m => ({
label: m.label,
data: Array(STEPS).fill(0),
// const datasets = measures.map(m => ({
// label: m.label,
// data: Array(stepCount).fill(0),
// }));
const mproc = measures.map(m => ({
...m,
}));
const data = Array(stepCount)
.fill(0)
.map(() => ({}));
// pass 2 - count measures
await datastore.enumRows(row => {
if (!mints || row.ts < mints) mints = row.ts;
if (!maxts || row.ts > maxts) maxts = row.ts;
for (let i = 0; i < measures.length; i++) {
const part = Math.round((new Date(row.ts).getTime() - minTime) / step);
datasets[i].data[part] += row[measures[i].field];
const ts = timestamp(row);
let part = Math.round((new Date(ts).getTime() - minTime) / stepDuration);
if (part < 0) part = 0;
if (part >= stepCount) part - stepCount - 1;
if (data[part]) {
data[part] = aggregate(data[part], row, stepDuration);
}
return true;
});
datastore._closeReader();
// const measureByField = _.fromPairs(measures.map((m, i) => [m.field, i]));
// for (let mindex = 0; mindex < measures.length; mindex++) {
// for (let stepIndex = 0; stepIndex < stepCount; stepIndex++) {
// const measure = measures[mindex];
// if (measure.perSecond) {
// datasets[mindex].data[stepIndex] /= stepDuration / 1000;
// }
// if (measure.perField) {
// datasets[mindex].data[stepIndex] /= datasets[measureByField[measure.perField]].data[stepIndex];
// }
// }
// }
// for (let i = 0; i < measures.length; i++) {
// if (measures[i].hidden) {
// datasets[i] = null;
// }
// }
return {
labels,
datasets,
datasets: mproc.map(m => ({
label: m.label,
data: data.map(d => d[m.field] || 0),
})),
};
},
};