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[] EOL = {(byte)'\n'};
private static byte[] LBRACKET = {(byte)'['};
private static byte[] RBRACKET = {(byte)']'};
private static byte[] COMMA = {(byte) ','};
[HttpGet] [HttpGet]
[Route("dump")] [Route("dump")]
@ -143,6 +146,8 @@ public class MetricsController : ControllerBase
Response.Headers.ContentType = "application/json"; Response.Headers.ContentType = "application/json";
var row = new Dictionary<string, object>(); var row = new Dictionary<string, object>();
await Response.Body.WriteAsync(LBRACKET);
for (var d = fromDate; d <= toDate; d = d.AddDays(1)) for (var d = fromDate; d <= toDate; d = d.AddDays(1))
{ {
row["_Timestamp"] = d; row["_Timestamp"] = d;
@ -155,8 +160,12 @@ public class MetricsController : ControllerBase
} }
await JsonSerializer.SerializeAsync(Response.Body, row); await JsonSerializer.SerializeAsync(Response.Body, row);
await Response.Body.WriteAsync(EOL); 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>(); var keys = new Dictionary<string, int>();
int GetMetricKey(string key) int GetMetricKey(string key)
{ {
if (string.IsNullOrWhiteSpace(key)) return -1;
if (keys.TryGetValue(key, out var v)) if (keys.TryGetValue(key, out var v))
return v; return v;
keys.Add(key, keys.Count); keys.Add(key, keys.Count);

View File

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