mirror of
https://github.com/zaphar/Heracles.git
synced 2025-07-23 04:29:48 -04:00
feat: filter by labels in dataset
This commit is contained in:
parent
49e27a9184
commit
d26be4dbc5
116
static/lib.js
116
static/lib.js
@ -23,11 +23,16 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
#step_duration;
|
#step_duration;
|
||||||
#d3TickFormat = "~s";
|
#d3TickFormat = "~s";
|
||||||
#targetNode = null;
|
#targetNode = null;
|
||||||
|
#menuContainer = null;
|
||||||
|
#filterSelectElements = {};
|
||||||
|
#filterLabels = {};
|
||||||
|
#filteredLabelSets = {};
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.#width = 800;
|
this.#width = 800;
|
||||||
this.#height = 600;
|
this.#height = 600;
|
||||||
this.#pollSeconds = 30;
|
this.#pollSeconds = 30;
|
||||||
|
this.#menuContainer = this.appendChild(document.createElement('div'));
|
||||||
this.#targetNode = this.appendChild(document.createElement("div"));
|
this.#targetNode = this.appendChild(document.createElement("div"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +67,7 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
default: // do nothing;
|
default: // do nothing;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
this.resetInterval();
|
this.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
@ -74,9 +79,10 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
this.#duration = this.getAttribute('duration') || null;
|
this.#duration = this.getAttribute('duration') || null;
|
||||||
this.#step_duration = this.getAttribute('step-duration') || null;
|
this.#step_duration = this.getAttribute('step-duration') || null;
|
||||||
this.#d3TickFormat = this.getAttribute('d3-tick-format') || this.#d3TickFormat;
|
this.#d3TickFormat = this.getAttribute('d3-tick-format') || this.#d3TickFormat;
|
||||||
this.resetInterval()
|
var self = this;
|
||||||
|
this.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
this.stopInterval()
|
this.stopInterval()
|
||||||
}
|
}
|
||||||
@ -94,12 +100,18 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resetInterval() {
|
reset(updateOnly) {
|
||||||
this.stopInterval()
|
var self = this;
|
||||||
if (this.#uri) {
|
self.stopInterval()
|
||||||
this.updateGraph();
|
self.fetchData().then((data) => {
|
||||||
}
|
if (!updateOnly) {
|
||||||
this.#intervalId = setInterval(() => this.updateGraph(), 1000 * this.#pollSeconds);
|
self.getLabelsForData(data);
|
||||||
|
self.buildFilterMenu();
|
||||||
|
}
|
||||||
|
self.updateGraph(data).then(() => {
|
||||||
|
self.#intervalId = setInterval(() => self.updateGraph(), 1000 * self.#pollSeconds);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static registerElement() {
|
static registerElement() {
|
||||||
@ -117,6 +129,8 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchData() {
|
async fetchData() {
|
||||||
|
// TODO(zaphar): Can we do some massaging on these
|
||||||
|
// to get the full set of labels and possible values?
|
||||||
const response = await fetch(this.getUri());
|
const response = await fetch(this.getUri());
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
@ -137,8 +151,80 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateGraph() {
|
populateFilterData(labels) {
|
||||||
const data = await this.fetchData();
|
for (var key in labels) {
|
||||||
|
const label = this.#filterLabels[key];
|
||||||
|
if (label) {
|
||||||
|
if (!label.includes(labels[key])) {
|
||||||
|
this.#filterLabels[key].push(labels[key]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.#filterLabels[key] = [labels[key]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buildSelectElement(key) {
|
||||||
|
var id = key + "-select" + Math.random();
|
||||||
|
const element = document.createElement("div");
|
||||||
|
const label = document.createElement("label");
|
||||||
|
label.innerText = key + ": ";
|
||||||
|
label.setAttribute("for", id);
|
||||||
|
element.appendChild(label);
|
||||||
|
const select = document.createElement("select");
|
||||||
|
select.setAttribute("name", id);
|
||||||
|
select.setAttribute("multiple", true);
|
||||||
|
const optElement = document.createElement("option");
|
||||||
|
const optValue = "Select " + key;
|
||||||
|
optElement.innerText = optValue;
|
||||||
|
select.appendChild(optElement);
|
||||||
|
for (var opt of this.#filterLabels[key]) {
|
||||||
|
const optElement = document.createElement("option");
|
||||||
|
optElement.setAttribute("value", opt);
|
||||||
|
optElement.innerText = opt;
|
||||||
|
select.appendChild(optElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
select.onchange = function(evt) {
|
||||||
|
evt.stopPropagation();
|
||||||
|
var filteredValues = [];
|
||||||
|
for (var opt of evt.target.selectedOptions) {
|
||||||
|
filteredValues.push(opt.getAttribute("value"));
|
||||||
|
}
|
||||||
|
self.#filteredLabelSets[key] = filteredValues;
|
||||||
|
self.reset(true);
|
||||||
|
};
|
||||||
|
element.appendChild(select);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
buildFilterMenu() {
|
||||||
|
// We need to maintain a stable order for these
|
||||||
|
var children = [];
|
||||||
|
for (var key of Object.keys(this.#filterLabels).sort()) {
|
||||||
|
const element = this.#filterSelectElements[key] || this.buildSelectElement(key);
|
||||||
|
children.push(element);
|
||||||
|
}
|
||||||
|
this.#menuContainer.replaceChildren(...children);
|
||||||
|
}
|
||||||
|
|
||||||
|
getLabelsForData(data) {
|
||||||
|
for (var subplot of data) {
|
||||||
|
if (subplot.Series) {
|
||||||
|
for (const triple of subplot.Series) {
|
||||||
|
const labels = triple[0];
|
||||||
|
this.populateFilterData(labels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateGraph(maybeData) {
|
||||||
|
var data = maybeData;
|
||||||
|
if (!data) {
|
||||||
|
data = await this.fetchData();
|
||||||
|
}
|
||||||
const config = {
|
const config = {
|
||||||
legend: {
|
legend: {
|
||||||
orientation: 'h'
|
orientation: 'h'
|
||||||
@ -155,8 +241,14 @@ class TimeseriesGraph extends HTMLElement {
|
|||||||
const default_yaxis = "y" + subplotCount
|
const default_yaxis = "y" + subplotCount
|
||||||
if (subplot.Series) {
|
if (subplot.Series) {
|
||||||
// https://plotly.com/javascript/reference/scatter/
|
// https://plotly.com/javascript/reference/scatter/
|
||||||
for (const triple of subplot.Series) {
|
loopSeries: for (const triple of subplot.Series) {
|
||||||
const labels = triple[0];
|
const labels = triple[0];
|
||||||
|
for (var label in labels) {
|
||||||
|
var show = this.#filteredLabelSets[label];
|
||||||
|
if (show && !show.includes(labels[label])) {
|
||||||
|
continue loopSeries;
|
||||||
|
}
|
||||||
|
}
|
||||||
const meta = triple[1];
|
const meta = triple[1];
|
||||||
const yaxis = meta["named_axis"] || default_yaxis;
|
const yaxis = meta["named_axis"] || default_yaxis;
|
||||||
// https://plotly.com/javascript/reference/layout/yaxis/
|
// https://plotly.com/javascript/reference/layout/yaxis/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user