Restore metrics page behavior

This commit is contained in:
Timothy Baldridge 2021-12-01 15:53:32 -07:00
parent 46ee87553c
commit 913b67daf3
3 changed files with 32 additions and 21 deletions

View File

@ -84,6 +84,9 @@ public class MetricsController : ControllerBase
}
private static byte[] EOL = {(byte)'\n'};
private static byte[] LBRACKET = {(byte)'['};
private static byte[] RBRACKET = {(byte)']'};
private static byte[] COMMA = {(byte) ','};
[HttpGet]
[Route("dump")]
@ -143,6 +146,8 @@ public class MetricsController : ControllerBase
Response.Headers.ContentType = "application/json";
var row = new Dictionary<string, object>();
await Response.Body.WriteAsync(LBRACKET);
for (var d = fromDate; d <= toDate; d = d.AddDays(1))
{
row["_Timestamp"] = d;
@ -155,8 +160,12 @@ public class MetricsController : ControllerBase
}
await JsonSerializer.SerializeAsync(Response.Body, row);
await Response.Body.WriteAsync(EOL);
if (d != toDate)
await Response.Body.WriteAsync(COMMA);
}
await Response.Body.WriteAsync(RBRACKET);
}

View File

@ -52,6 +52,7 @@ public class Metrics
var keys = new Dictionary<string, int>();
int GetMetricKey(string key)
{
if (string.IsNullOrWhiteSpace(key)) return -1;
if (keys.TryGetValue(key, out var v))
return v;
keys.Add(key, keys.Count);

View File

@ -40,20 +40,19 @@
<script>
var getReport = function (subject, callback) {
$.getJSON("/metrics/report/" + subject, callback)
var getReport = function (subject, from, callback) {
$.getJSON("/metrics/report/?action=" + subject + "&from=" + from + "&to=now", callback)
}
var makeChart = function (ele, group) {
var result_fn = function (data) {
var data = _.filter(data, series => _.some(series.values, v => v > 1));
var labels = _.uniq(_.flatten(_.map(data, series => series.labels)));
var datasets = _.map(data, series => {
var labels = _.map(data, f => f._Timestamp);
var datasets = _.map(_.filter(Object.keys(data[0]), key => key !== "_Timestamp"), key => {
return {
label: series.seriesName,
label: key,
fill: false,
data: _.last(series.values, 90)
data: _.map(data, row => row[key])
}
});
var ctx = document.getElementById(ele).getContext('2d');
@ -71,22 +70,21 @@
options: {scales: {xAxes: [{stacked: true}], yAxes: [{stacked: true}]}}
});
};
getReport(group, result_fn);
getReport(group, "30 days ago", result_fn);
};
var makePieChart = function (ele, group) {
var result_fn = function (data) {
var data = _.filter(data, series => _.some(series.values, v => v > 2));
var labels = _.map(data, series => series.seriesName);
var datasets = {
data: _.map(data, series => {
return _.reduce(series.values, (x, y) => x + y, 0)
})
};
console.log(datasets);
console.log(labels);
const counts = {};
for (var row of data) {
for (var key of Object.keys(row)) {
if (key === "_Timestamp") continue;
counts[key] = (counts[key] || 0) + row[key]
}
}
var ctx = document.getElementById(ele).getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
@ -94,15 +92,18 @@
// The data for our dataset
data: {
labels: labels,
datasets: [datasets]
labels: Object.keys(counts),
datasets: [{
label: "Data",
data: Object.values(counts)
}]
},
// Configuration options go here
options: {}
});
};
getReport(group, result_fn)
getReport(group, "10 years ago", result_fn)
};
makeChart("begin_download_chart", "downloading");