annotate src/MetricRow.ts @ 13:3014db0a5500 default tip

mv board to proj/micro, rename this repo with dashes
author drewp@bigasterisk.com
date Fri, 28 Jun 2024 17:08:09 -0700
parents 7de586bf19ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
1 import { LitElement, PropertyValueMap, css, html } from "lit";
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
2 import { customElement, property, state } from "lit/decorators.js";
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
3
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
4
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
5 @customElement("metric-row")
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
6 export class MetricRow extends LitElement {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
7 static styles = [
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
8 css`
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
9 :host > div {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
10 border: solid #eee;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
11 border-width: 1px 0;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
12 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
13 :host > div > span {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
14 display: inline-block;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
15 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
16 span.label {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
17 width: 20em;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
18 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
19 span.vmval {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
20 width: 2em;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
21 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
22 span.asof {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
23 width: 15em;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
24 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
25 .vmval {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
26 font-weight: bold;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
27 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
28 .asof {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
29 color: gray;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
30 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
31 `,
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
32 ];
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
33 @property() label: string = "??";
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
34 @property() q: string = "";
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
35 @state() resultTime?: Date;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
36 @state() resultValue?: string;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
37 render() {
10
7de586bf19ff fix one vmui path (but this makes the page more broken)
drewp@bigasterisk.com
parents: 5
diff changeset
38 const graphUrl = new URL("/m/vmselect/0/vmui/vmui/?#/");
5
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
39
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
40 graphUrl.searchParams.append("g0.expr", this.q);
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
41 graphUrl.searchParams.append("g0.range_input", "6h");
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
42 graphUrl.searchParams.append("g0.relative_time", "last_6_hours");
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
43 return html`<div>
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
44 <span class='label'>${this.label}</span>
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
45 <span class="vmval"> ${this.resultValue || "..."} </span>
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
46 <span class="asof">${this.resultTime ? html`as of ${this.resultTime.toLocaleString("sv")}` : []}</span>
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
47 <span class='graph'><a href="${graphUrl.toString()}">[graph]</a>
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
48 </div>`;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
49 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
50
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
51 protected async update(changedProperties: PropertyValueMap<this>) {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
52 if (changedProperties.has("q") && this.q) {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
53 this.getMetricValue(this.q);
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
54 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
55 super.update(changedProperties);
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
56 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
57
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
58 async getMetricValue(q: string) {
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
59 const vmapi = "https://bigasterisk.com/m/prometheus/api/v1";
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
60 const queryUrl = new URL(vmapi + "/query");
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
61 queryUrl.searchParams.append("query", q);
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
62 const resp = await (await fetch(queryUrl)).json();
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
63 const v = resp.data.result[0].value;
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
64 this.resultTime = new Date(v[0] * 1000);
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
65 this.resultValue = v[1];
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
66 }
9eaa993ed373 monitoring
drewp@bigasterisk.com
parents:
diff changeset
67 }