Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.0 KB
Raw Blame
import { z } from "zod";

import type { StatsProvider } from "../types";

const triliumStatsResponseSchema = z
  .object({
    version: z.object({ app: z.string().min(1) }).passthrough(),
    database: z.object({ activeNotes: z.number().finite().int().nonnegative() }).passthrough(),
    statistics: z.object({ databaseSizeBytes: z.number().finite().nonnegative() }).passthrough(),
  })
  .passthrough();

export const triliumStatsProvider = {
  metrics: [
    { key: "version", label: "Version", unit: "text" },
    { key: "notesCount", label: "Notes", unit: "count" },
    { key: "dbSize", label: "Database size", unit: "bytes" },
  ],
  async fetchAsync(context) {
    const response = await context.requestAsync("/etapi/metrics?format=json", {
      signal: context.signal,
    });
    const stats = triliumStatsResponseSchema.parse(response);

    return {
      version: stats.version.app,
      notesCount: stats.database.activeNotes,
      dbSize: stats.statistics.databaseSizeBytes,
    };
  },
} satisfies StatsProvider;