view src/main.ts @ 5:9eaa993ed373

monitoring
author drewp@bigasterisk.com
date Sun, 15 Oct 2023 18:47:45 -0700
parents 4365c72c59f6
children
line wrap: on
line source

import { LitElement, TemplateResult, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
export { SgSource, SgView, StreamedGraph } from "@bigasterisk/streamed-graph";
export { MetricRow } from "./MetricRow";

@customElement("fd-page")
export class FdPage extends LitElement {
  static styles = [
    css`
      :host {
        display: flex;
        flex-direction: column;
        height: 100vh;
      }
      button {
        margin: 10px;
        padding: 20px;
      }
    `,
  ];
  @property() status: {} = { "loading...": "" };
  @property() lastCommand: {} = { command: "none" };
  connectedCallback(): void {
    super.connectedCallback();
    this.getStatus();
  }

  async getStatus() {
    this.status = await (await fetch("api/status")).json();
    setTimeout(() => {
      requestAnimationFrame(() => {
        this.getStatus();
      });
    }, 1000);
  }
  render() {
    return html`
      <h1>Front door lock</h1>

      <div>Status: ${JSON.stringify(this.status)} ${JSON.stringify(this.lastCommand)}</div>
      <div>
        <button @click=${() => this.simple("unlock")}>Unlock</button>
        <button @click=${() => this.simple("stayUnlocked")}>Unlock and stay unlocked</button>
        <button @click=${() => this.simple("lock")}>Lock</button>
      </div>
      <streamed-graph expanded="true">
        <sg-source url="./api/graph"></sg-source>
        <sg-source url="./view.n3"></sg-source>
        <sg-view uri="#view"></sg-view>
      </streamed-graph>
      <p>
        <a href="metrics">metrics</a> | <a href="api/graph">graph</a> |
        <a href="https://bigasterisk.com/k/clusters/local/namespaces/default/deployments/front-door-lock">deploy</a> |
        <a href="https://bigasterisk.com/k/clusters/local/namespaces/default/deployments/front-door-lock/logs">logs</a> |
        <a href="https://bigasterisk.com/vmalert/groups#group-14459482342649697182">alert group</a>
      </p>
      <p>
        <metric-row label="reader esp32: mqtt connected  " q='hw_connected{job="fingerprint"}'></metric-row>
        <metric-row label="reader service: up            " q='up{job="fingerprint"}'></metric-row>
        <metric-row label="reader service: mqtt connected" q='mqtt_connected{job="fingerprint"}'></metric-row>
        <metric-row label="Lock service (this page): up  " q='up{job="front-door-lock"}'></metric-row>
        <metric-row label="Lock service: mqtt connected  " q='mqtt_connected{job="front-door-lock"}'></metric-row>
        <metric-row label="Lock esp32: mqtt-connected    " q='hw_connected{job="front-door-lock"}'></metric-row>
      </p>
      <bigast-loginbar></bigast-loginbar>
    `;
  }
  async simple(command: string) {
    const t1 = Date.now();
    this.lastCommand = { command: command, sentAt: t1 };
    var status;
    try {
      const resp = await fetch(`api/simple/${command}`, { method: "PUT" });
      status = resp.status;
    } catch (e) {
      status = "" + e;
    }
    const t2 = Date.now();
    this.lastCommand = { command: command, sentAt: t1, tookMs: t2 - t1, status: status };
  }
}