From a33b548555845504a943c5a9149ac1c8a8fea014 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Thu, 25 May 2023 09:53:04 +0200 Subject: [PATCH] Add Meilisearch widget --- packages/backend/src/daemons/server-stats.ts | 11 ++++ packages/backend/src/db/meilisearch.ts | 16 ++++-- .../src/server/api/endpoints/server-info.ts | 12 +++++ .../client/src/widgets/server-metric/disk.vue | 18 +++++++ .../src/widgets/server-metric/index.vue | 8 ++- .../src/widgets/server-metric/meilisearch.vue | 51 +++++++++++++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 packages/client/src/widgets/server-metric/meilisearch.vue diff --git a/packages/backend/src/daemons/server-stats.ts b/packages/backend/src/daemons/server-stats.ts index b0bf1288fd..c7aaea035b 100644 --- a/packages/backend/src/daemons/server-stats.ts +++ b/packages/backend/src/daemons/server-stats.ts @@ -1,6 +1,7 @@ import si from "systeminformation"; import Xev from "xev"; import * as osUtils from "os-utils"; +import meilisearch from "../db/meilisearch"; const ev = new Xev(); @@ -24,6 +25,7 @@ export default function () { const memStats = await mem(); const netStats = await net(); const fsStats = await fs(); + const meilisearchStats = await meilisearchStatus(); const stats = { cpu: roundCpu(cpu), @@ -39,6 +41,7 @@ export default function () { r: round(Math.max(0, fsStats.rIO_sec ?? 0)), w: round(Math.max(0, fsStats.wIO_sec ?? 0)), }, + meilisearch: meilisearchStats }; ev.emit("serverStats", stats); log.unshift(stats); @@ -77,3 +80,11 @@ async function fs() { const data = await si.disksIO().catch(() => ({ rIO_sec: 0, wIO_sec: 0 })); return data || { rIO_sec: 0, wIO_sec: 0 }; } + +async function meilisearchStatus() { + if (meilisearch) { + return meilisearch.serverStats(); + } else { + return null; + } +} diff --git a/packages/backend/src/db/meilisearch.ts b/packages/backend/src/db/meilisearch.ts index f404b47214..f3561b8077 100644 --- a/packages/backend/src/db/meilisearch.ts +++ b/packages/backend/src/db/meilisearch.ts @@ -1,4 +1,4 @@ -import { MeiliSearch } from 'meilisearch'; +import {Health, MeiliSearch, Stats } from 'meilisearch'; import { dbLogger } from "./logger.js"; import config from "@/config/index.js"; @@ -16,7 +16,7 @@ const host = hasConfig ? config.meilisearch.host ?? "localhost" : ""; const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; -const client = new MeiliSearch({ +const client : MeiliSearch = new MeiliSearch({ host: `http://${host}:${port}`, apiKey: auth, }) @@ -58,6 +58,16 @@ export default hasConfig ? { userHost: note.userHost, channelId: note.channelId, } - ]) + ]); }, + serverStats: async () => { + let health : Health = await client.health(); + let stats: Stats = await client.getStats(); + + return { + health: health.status, + size: stats.databaseSize, + indexed_count: stats.indexes["posts"].numberOfDocuments + } + } } : null; diff --git a/packages/backend/src/server/api/endpoints/server-info.ts b/packages/backend/src/server/api/endpoints/server-info.ts index 1ce27e2621..4b2a4078a6 100644 --- a/packages/backend/src/server/api/endpoints/server-info.ts +++ b/packages/backend/src/server/api/endpoints/server-info.ts @@ -1,6 +1,7 @@ import * as os from "node:os"; import si from "systeminformation"; import define from "../define.js"; +import meilisearch from "../../../db/meilisearch"; export const meta = { requireCredential: false, @@ -18,6 +19,7 @@ export const paramDef = { export default define(meta, paramDef, async () => { const memStats = await si.mem(); const fsStats = await si.fsSize(); + const meilisearchStats = await meilisearchStatus(); return { machine: os.hostname(), @@ -32,5 +34,15 @@ export default define(meta, paramDef, async () => { total: fsStats[0].size, used: fsStats[0].used, }, + meilisearch: meilisearchStats + }; }); + +async function meilisearchStatus() { + if (meilisearch) { + return meilisearch.serverStats(); + } else { + return null; + } +} diff --git a/packages/client/src/widgets/server-metric/disk.vue b/packages/client/src/widgets/server-metric/disk.vue index 67ea398c16..0457cf7f0e 100644 --- a/packages/client/src/widgets/server-metric/disk.vue +++ b/packages/client/src/widgets/server-metric/disk.vue @@ -8,6 +8,12 @@

Used: {{ bytes(used, 1) }}

+
+
+

MeiliSearch

+ +
+