monitai 0.0.1 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ import 'preact/hooks';
2
+
3
+ type SourceStatus = 'idle' | 'loading' | 'ready' | 'error';
4
+ interface SourceEntry<T = unknown> {
5
+ id: string;
6
+ description: string;
7
+ data: T | null;
8
+ status: SourceStatus;
9
+ error?: string;
10
+ updatedAt: number | null;
11
+ intervalMs: number;
12
+ /** Manual poller pause (action override); false by default */
13
+ isPaused: boolean;
14
+ }
15
+ interface SummaryResult {
16
+ text: string;
17
+ severity: 'normal' | 'warning' | 'critical';
18
+ generatedAt: number;
19
+ basedOnSources: string[];
20
+ }
21
+ interface DashboardSnapshot {
22
+ sources: Record<string, SourceEntry>;
23
+ summary: SummaryResult | null;
24
+ serverStartedAt: number;
25
+ }
26
+ type StoreEvent = {
27
+ type: 'source:updated';
28
+ source: SourceEntry;
29
+ } | {
30
+ type: 'summary:updated';
31
+ summary: SummaryResult;
32
+ };
33
+ type SourceFetcherWithParams<P, T> = (params: P) => Promise<T>;
34
+ /** Overloads for publicProps inference */
35
+ type DefineSourceConfig<P extends Record<string, any> = Record<string, any>, T = unknown, SafeP = Record<string, never>> = {
36
+ id: string | ((params: P) => string);
37
+ interval?: string | number;
38
+ description?: string | ((params: P) => string);
39
+ publicProps?: readonly (keyof P)[] | ((params: P) => SafeP);
40
+ fetcher?: SourceFetcherWithParams<P, T>;
41
+ };
42
+ type SourceDefinition<P extends Record<string, any> = Record<string, any>, T = unknown, SafeP = Record<string, never>> = DefineSourceConfig<P, T, SafeP> & {
43
+ __isMonitaiSource: true;
44
+ /** Resolved public params type (set by defineSource helpers) */
45
+ __safeProps?: SafeP;
46
+ };
47
+ /** Serialized / hydrated source descriptor (no secrets, no fetcher) */
48
+ interface SourceRefJson<T = unknown, SafeP = Record<string, unknown>> {
49
+ __isSourceRef: true;
50
+ id: string;
51
+ params: SafeP;
52
+ initialData: T | null;
53
+ }
54
+ interface SourceRef<T = unknown, SafeP = Record<string, unknown>> extends SourceRefJson<T, SafeP> {
55
+ toJSON(): SourceRefJson<T, SafeP>;
56
+ }
57
+ /** Standard single-source island props */
58
+ type WithSource<T = unknown, SafeP = Record<string, any>> = {
59
+ source: SourceRef<T, SafeP>;
60
+ };
61
+ type UseSourceResult<T = unknown> = {
62
+ data: T | null;
63
+ status: SourceStatus;
64
+ error?: string;
65
+ updatedAt: number | null;
66
+ isPaused: boolean;
67
+ };
68
+ /** @deprecated use defineSource + refSource */
69
+ type SourceFetcher<T = unknown> = () => Promise<T>;
70
+ /** @deprecated */
71
+ interface CreateSourceOptions<T = unknown> {
72
+ id: string;
73
+ description: string;
74
+ interval: string | number;
75
+ fetcher: SourceFetcher<T>;
76
+ }
77
+
78
+ declare function useSource<T = unknown>(source: SourceRef<T> | string, initialData?: T | null): UseSourceResult<T>;
79
+ declare function useSummary(initialSummary?: SummaryResult | null): {
80
+ summary: SummaryResult | null;
81
+ isLoading: boolean;
82
+ refresh: () => Promise<void>;
83
+ };
84
+ declare function useAction<Input = void, Output = unknown>(actionId: string): {
85
+ execute: (input: Input) => Promise<Output | null>;
86
+ isPending: boolean;
87
+ error: string | null;
88
+ data: Output | null;
89
+ };
90
+
91
+ export { type CreateSourceOptions as C, type DashboardSnapshot as D, type SourceEntry as S, type UseSourceResult as U, type WithSource as W, type SummaryResult as a, type StoreEvent as b, type SourceFetcher as c, type SourceDefinition as d, type SourceRef as e, type DefineSourceConfig as f, type SourceRefJson as g, type SourceStatus as h, useSource as i, useSummary as j, useAction as u };
@@ -0,0 +1,2 @@
1
+ export { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'preact/hooks';
2
+ export { D as DashboardSnapshot, S as SourceEntry, e as SourceRef, g as SourceRefJson, h as SourceStatus, b as StoreEvent, a as SummaryResult, U as UseSourceResult, W as WithSource, u as useAction, i as useSource, j as useSummary } from './client-Dq7v7zCx.js';
package/dist/client.js ADDED
@@ -0,0 +1,26 @@
1
+ import {
2
+ useAction,
3
+ useSource,
4
+ useSummary
5
+ } from "./chunk-63FHD5ZT.js";
6
+
7
+ // src/client.ts
8
+ import {
9
+ useState,
10
+ useEffect,
11
+ useRef,
12
+ useMemo,
13
+ useCallback,
14
+ useContext
15
+ } from "preact/hooks";
16
+ export {
17
+ useAction,
18
+ useCallback,
19
+ useContext,
20
+ useEffect,
21
+ useMemo,
22
+ useRef,
23
+ useSource,
24
+ useState,
25
+ useSummary
26
+ };
@@ -0,0 +1,163 @@
1
+ export { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'preact/hooks';
2
+ import { S as SourceEntry, D as DashboardSnapshot, a as SummaryResult, b as StoreEvent, c as SourceFetcher, d as SourceDefinition, e as SourceRef, C as CreateSourceOptions } from './client-Dq7v7zCx.js';
3
+ export { f as DefineSourceConfig, g as SourceRefJson, h as SourceStatus, U as UseSourceResult, W as WithSource, u as useAction, i as useSource, j as useSummary } from './client-Dq7v7zCx.js';
4
+ import { ComponentType } from 'preact';
5
+
6
+ type StoreListener = (event: StoreEvent) => void;
7
+ interface StateStoreAdapter {
8
+ registerSource(config: {
9
+ id: string;
10
+ description: string;
11
+ intervalMs: number;
12
+ }): void;
13
+ registerSourceWithMeta(config: {
14
+ id: string;
15
+ description: string;
16
+ intervalMs: number;
17
+ paramsHash: string;
18
+ }): boolean;
19
+ touchSource(id: string): void;
20
+ markAllStale(): void;
21
+ removeStaleSources(): string[];
22
+ setSourceData(id: string, data: unknown): void;
23
+ setSourceError(id: string, error: string): void;
24
+ setSourceLoading(id: string): void;
25
+ setSourcePaused(id: string, paused: boolean): void;
26
+ resetAllPaused(): void;
27
+ getSource<T = unknown>(id: string): SourceEntry<T> | undefined;
28
+ getSnapshot(): DashboardSnapshot;
29
+ setSummary(summary: SummaryResult): void;
30
+ subscribe(listener: StoreListener): () => void;
31
+ }
32
+
33
+ declare class LlmAgent {
34
+ private store;
35
+ private timer;
36
+ private lastHash;
37
+ private lastRunAt;
38
+ private running;
39
+ private enabled;
40
+ private minIntervalMs;
41
+ private loopMs;
42
+ constructor(store: StateStoreAdapter);
43
+ start(): void;
44
+ stop(): void;
45
+ triggerSummary(force?: boolean): Promise<SummaryResult | null>;
46
+ private schedule;
47
+ private run;
48
+ private callLlm;
49
+ }
50
+
51
+ declare class ServerPoller {
52
+ private store;
53
+ constructor(store: StateStoreAdapter);
54
+ reset(): void;
55
+ unregister(id: string): void;
56
+ pause(sourceId: string): void;
57
+ resume(sourceId: string): void;
58
+ /** Fire one fetch tick immediately (keeps schedule unless paused). */
59
+ triggerNow(id: string): void;
60
+ register(id: string, intervalMs: number, fetcher: SourceFetcher, options?: {
61
+ immediate?: boolean;
62
+ }): void;
63
+ private tick;
64
+ }
65
+
66
+ type ActionContext = {
67
+ store: StateStoreAdapter;
68
+ poller: ServerPoller;
69
+ agent: LlmAgent;
70
+ };
71
+ type ActionConfig<Input = unknown, Output = unknown> = {
72
+ id: string;
73
+ rateLimitMs?: number;
74
+ /** Default 65536 (64 KB) */
75
+ maxBodyBytes?: number;
76
+ handler: (input: Input, ctx: ActionContext) => Promise<Output> | Output;
77
+ };
78
+ /** Pure factory — no side effects. Register via createPage({ actions }). */
79
+ declare function defineAction<Input = unknown, Output = unknown>(config: ActionConfig<Input, Output>): ActionConfig<Input, Output>;
80
+
81
+ type Runtime = {
82
+ store: StateStoreAdapter;
83
+ poller: ServerPoller;
84
+ agent: LlmAgent;
85
+ };
86
+ declare function getRuntime(): Runtime;
87
+
88
+ type PageRenderContext = {
89
+ snapshot: DashboardSnapshot;
90
+ };
91
+ type PageRenderFn = (ctx?: PageRenderContext) => any | Promise<any>;
92
+ type MonitaiPage = {
93
+ __isMonitaiPage: true;
94
+ render: PageRenderFn;
95
+ title?: string;
96
+ /** Heterogeneous action list — Input/Output vary per action */
97
+ actions?: ActionConfig<any, any>[];
98
+ getRuntime: typeof getRuntime;
99
+ };
100
+ declare function createPage(renderFn: PageRenderFn, options?: {
101
+ title?: string;
102
+ actions?: ActionConfig<any, any>[];
103
+ }): MonitaiPage;
104
+
105
+ type InferSafeFromKeys<P extends Record<string, any>, K extends keyof P> = Pick<P, K>;
106
+ /**
107
+ * Overloads:
108
+ * 1) `publicProps: ['x'] as const` without explicit type args → precise Pick<P, K>
109
+ * 2) `publicProps` as function → SafeP from return type
110
+ * 3) Explicit `defineSource<P, T>(…)` (2 type args) → SafeP = any
111
+ * (TS won't infer a 3rd type param when only P,T are written; avoid the old
112
+ * overload with `publicProps?: undefined` that rejected real arrays.)
113
+ */
114
+ declare function defineSource<P extends Record<string, any>, T, const K extends keyof P>(config: {
115
+ id: string | ((params: P) => string);
116
+ interval?: string | number;
117
+ description?: string | ((params: P) => string);
118
+ publicProps: readonly K[];
119
+ fetcher?: (params: P) => Promise<T>;
120
+ }): SourceDefinition<P, T, InferSafeFromKeys<P, K>>;
121
+ declare function defineSource<P extends Record<string, any>, T, SafeP extends Record<string, any>>(config: {
122
+ id: string | ((params: P) => string);
123
+ interval?: string | number;
124
+ description?: string | ((params: P) => string);
125
+ publicProps: (params: P) => SafeP;
126
+ fetcher?: (params: P) => Promise<T>;
127
+ }): SourceDefinition<P, T, SafeP>;
128
+ declare function defineSource<P extends Record<string, any>, T = unknown>(config: {
129
+ id: string | ((params: P) => string);
130
+ interval?: string | number;
131
+ description?: string | ((params: P) => string);
132
+ publicProps?: readonly (keyof P)[] | ((params: P) => Record<string, any>);
133
+ fetcher?: (params: P) => Promise<T>;
134
+ }): SourceDefinition<P, T, any>;
135
+
136
+ /**
137
+ * Bind a concrete source instance (server-only).
138
+ * Serializes to { __isSourceRef, id, params: safeParams, initialData }.
139
+ */
140
+ declare function refSource<P extends Record<string, any>, T, SafeP = Record<string, never>>(serverConfig: SourceDefinition<P, T, SafeP>, params: P): SourceRef<T, SafeP>;
141
+ declare function isSourceRef(value: unknown): value is SourceRef;
142
+
143
+ declare function resetIslandInstanceCounter(): void;
144
+ declare function getRegisteredIslandNames(): string[];
145
+ declare function clearRegisteredIslandNames(): void;
146
+ /**
147
+ * Refresh SourceRef.initialData from store; deep-walk plain objects/arrays.
148
+ * Cycle-safe via WeakSet.
149
+ */
150
+ declare function resolvePropsWithSources<P>(props: P): P;
151
+ /**
152
+ * Bind a view for use in main.tsx. Sources via refSource props only.
153
+ * Signature: island(name, View) — no serverConfig argument.
154
+ */
155
+ declare function island<P extends Record<string, any> = Record<string, any>>(name: string, View: ComponentType<P>): (props: P) => any;
156
+
157
+ type SourceHandle<T = unknown> = {
158
+ id: string;
159
+ get: () => SourceEntry<T> | undefined;
160
+ };
161
+ declare function createSource<T = unknown>(options: CreateSourceOptions<T>): SourceHandle<T>;
162
+
163
+ export { type ActionConfig, type ActionContext, CreateSourceOptions, DashboardSnapshot, type MonitaiPage, type PageRenderContext, SourceDefinition, SourceEntry, type SourceHandle, SourceRef, StoreEvent, SummaryResult, clearRegisteredIslandNames, createPage, createSource, defineAction, defineSource, getRegisteredIslandNames, isSourceRef, island, refSource, resetIslandInstanceCounter, resolvePropsWithSources };
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ import {
2
+ clearRegisteredIslandNames,
3
+ getRegisteredIslandNames,
4
+ getRuntime,
5
+ getStore,
6
+ isSourceRef,
7
+ island,
8
+ parseInterval,
9
+ refSource,
10
+ resetIslandInstanceCounter,
11
+ resolvePropsWithSources
12
+ } from "./chunk-JU76LRZA.js";
13
+ import {
14
+ useAction,
15
+ useSource,
16
+ useSummary
17
+ } from "./chunk-63FHD5ZT.js";
18
+
19
+ // src/index.ts
20
+ import {
21
+ useState,
22
+ useEffect,
23
+ useRef,
24
+ useMemo,
25
+ useCallback,
26
+ useContext
27
+ } from "preact/hooks";
28
+
29
+ // src/create-page.ts
30
+ function createPage(renderFn, options) {
31
+ return {
32
+ __isMonitaiPage: true,
33
+ render: renderFn,
34
+ title: options?.title,
35
+ actions: options?.actions,
36
+ getRuntime
37
+ };
38
+ }
39
+
40
+ // src/define-source.ts
41
+ function defineSource(config) {
42
+ return {
43
+ ...config,
44
+ __isMonitaiSource: true
45
+ };
46
+ }
47
+
48
+ // src/define-action.ts
49
+ function defineAction(config) {
50
+ return {
51
+ maxBodyBytes: 65536,
52
+ ...config
53
+ };
54
+ }
55
+
56
+ // src/create-source.ts
57
+ function createSource(options) {
58
+ if (typeof window !== "undefined") {
59
+ return {
60
+ id: options.id,
61
+ get: () => void 0
62
+ };
63
+ }
64
+ const intervalMs = parseInterval(options.interval);
65
+ const { store, poller } = getRuntime();
66
+ store.registerSource({
67
+ id: options.id,
68
+ description: options.description,
69
+ intervalMs
70
+ });
71
+ poller.register(options.id, intervalMs, options.fetcher);
72
+ return {
73
+ id: options.id,
74
+ get: () => getStore().getSource(options.id)
75
+ };
76
+ }
77
+ export {
78
+ clearRegisteredIslandNames,
79
+ createPage,
80
+ createSource,
81
+ defineAction,
82
+ defineSource,
83
+ getRegisteredIslandNames,
84
+ isSourceRef,
85
+ island,
86
+ refSource,
87
+ resetIslandInstanceCounter,
88
+ resolvePropsWithSources,
89
+ useAction,
90
+ useCallback,
91
+ useContext,
92
+ useEffect,
93
+ useMemo,
94
+ useRef,
95
+ useSource,
96
+ useState,
97
+ useSummary
98
+ };
@@ -0,0 +1,3 @@
1
+ declare function hydrateIslands(registry: Record<string, any>): void;
2
+
3
+ export { hydrateIslands };
@@ -0,0 +1,19 @@
1
+ // src/internal/island-runtime.ts
2
+ import { h, hydrate } from "preact";
3
+ function hydrateIslands(registry) {
4
+ document.querySelectorAll("monitai-island").forEach((el) => {
5
+ const name = el.getAttribute("data-name");
6
+ const uid = el.getAttribute("data-uid");
7
+ const Component = name ? registry[name] : null;
8
+ if (!Component || !uid) return;
9
+ const scriptEl = document.querySelector(
10
+ `script[data-island-props="${CSS.escape(uid)}"]`
11
+ );
12
+ const props = scriptEl?.textContent ? JSON.parse(scriptEl.textContent) : {};
13
+ scriptEl?.remove();
14
+ hydrate(h(Component, props), el);
15
+ });
16
+ }
17
+ export {
18
+ hydrateIslands
19
+ };
package/package.json CHANGED
@@ -1,7 +1,63 @@
1
- {
2
- "name": "monitai",
3
- "version": "0.0.1",
4
- "description": "TypeScript framework for building interactive data apps and dashboards",
5
- "main": "index.js",
6
- "license": "MIT"
7
- }
1
+ {
2
+ "name": "monitai",
3
+ "version": "0.4.4",
4
+ "description": "CLI framework for ops dashboards: Preact islands, server sources, SSE, and actions on node:http + esbuild",
5
+ "author": "sergaph <simasle@yandex.com>",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "bin": {
11
+ "monitai": "./dist/cli.js"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.js",
16
+ "types": "./dist/index.d.ts"
17
+ },
18
+ "./client": {
19
+ "import": "./dist/client.js",
20
+ "types": "./dist/client.d.ts"
21
+ },
22
+ "./internal/island-runtime": "./dist/internal/island-runtime.js"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "template",
27
+ "LICENSE",
28
+ "README.md"
29
+ ],
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
33
+ "keywords": [
34
+ "dashboard",
35
+ "preact",
36
+ "ssr",
37
+ "islands",
38
+ "sse",
39
+ "ops",
40
+ "monitoring",
41
+ "esbuild"
42
+ ],
43
+ "scripts": {
44
+ "build": "tsup src/index.ts src/client.ts src/cli.ts src/internal/island-runtime.ts --format esm --dts --clean",
45
+ "prepublishOnly": "npm run build"
46
+ },
47
+ "dependencies": {
48
+ "@clack/prompts": "^0.7.0",
49
+ "@tailwindcss/cli": "^4.3.5",
50
+ "esbuild": "^0.25.0",
51
+ "preact": "^10.24.0",
52
+ "preact-render-to-string": "^6.5.0",
53
+ "tailwindcss": "^4.3.5"
54
+ },
55
+ "peerDependencies": {
56
+ "preact": "^10.0.0"
57
+ },
58
+ "devDependencies": {
59
+ "@types/node": "^22.0.0",
60
+ "tsup": "^8.3.0",
61
+ "typescript": "^5.6.0"
62
+ }
63
+ }
@@ -0,0 +1,16 @@
1
+ # Server bind
2
+ HOST=0.0.0.0
3
+ PORT=3000
4
+
5
+ # Demo weather source secret (server-only; never reaches HTML / client.js)
6
+ WEATHER_API_KEY=
7
+
8
+ # LLM summary agent (optional). Without a key the agent stays disabled.
9
+ MONITAI_LLM_API_KEY=
10
+ # MONITAI_LLM_BASE_URL=https://api.openai.com/v1
11
+ # MONITAI_LLM_MODEL=gpt-4o-mini
12
+ # MONITAI_LLM_LOOP_MS=120000
13
+ # MONITAI_LLM_MIN_INTERVAL_MS=60000
14
+
15
+ # Only vars with this prefix are inlined into the client bundle:
16
+ # MONITAI_PUBLIC_APP_NAME=Monitai
@@ -0,0 +1,8 @@
1
+ node_modules/
2
+ .monitai/
3
+ dist/
4
+ .env
5
+ .env.local
6
+ .env.production
7
+ *.log
8
+ .DS_Store
@@ -0,0 +1,18 @@
1
+ import { useState } from 'monitai'
2
+
3
+ /** UI-only island — no refSource */
4
+ export default function CounterView(props: { initialCount?: number }) {
5
+ const [count, setCount] = useState(props.initialCount ?? 0)
6
+ return (
7
+ <div class="p-4 border border-gray-300 rounded-lg inline-flex items-center gap-3">
8
+ <span class="font-medium">Counter: {count}</span>
9
+ <button
10
+ type="button"
11
+ class="px-3 py-1 text-sm rounded bg-gray-900 text-white hover:bg-gray-700"
12
+ onClick={() => setCount((c) => c + 1)}
13
+ >
14
+ +1
15
+ </button>
16
+ </div>
17
+ )
18
+ }
@@ -0,0 +1,36 @@
1
+ import { defineAction } from 'monitai'
2
+ import type {
3
+ ServiceMetricsData,
4
+ SimulateIncidentInput,
5
+ SimulateIncidentOutput,
6
+ } from '../service-monitor/service-monitor.types'
7
+
8
+ export const simulateIncident = defineAction<
9
+ SimulateIncidentInput,
10
+ SimulateIncidentOutput
11
+ >({
12
+ id: 'simulate-incident',
13
+ rateLimitMs: 500,
14
+ handler: async (input, ctx) => {
15
+ const current = ctx.store.getSource<ServiceMetricsData>(
16
+ input.sourceId,
17
+ )?.data
18
+ if (!current) throw new Error('Service source is not initialized')
19
+
20
+ if (input.mode === 'recover') {
21
+ ctx.poller.resume(input.sourceId)
22
+ return { ok: true }
23
+ }
24
+
25
+ ctx.poller.pause(input.sourceId)
26
+ ctx.store.setSourceData(input.sourceId, {
27
+ ...current,
28
+ status: 'degraded',
29
+ latencyMs: 1850,
30
+ message: '503 Service Unavailable (manual incident simulation)',
31
+ updatedAt: Date.now(),
32
+ })
33
+
34
+ return { ok: true }
35
+ },
36
+ })
@@ -0,0 +1,50 @@
1
+ import { useSource, useAction, type WithSource } from 'monitai'
2
+ import type {
3
+ ServiceMetricsData,
4
+ SimulateIncidentInput,
5
+ SimulateIncidentOutput,
6
+ } from '../service-monitor/service-monitor.types'
7
+
8
+ export default function IncidentSimulatorView({
9
+ source,
10
+ }: WithSource<ServiceMetricsData>) {
11
+ const { data, isPaused } = useSource(source)
12
+ const { execute, isPending, error } = useAction<
13
+ SimulateIncidentInput,
14
+ SimulateIncidentOutput
15
+ >('simulate-incident')
16
+
17
+ return (
18
+ <div class="p-4 border rounded-lg bg-gray-50 space-y-3">
19
+ <p class="text-sm text-gray-600 m-0">
20
+ Control panel for source <strong>{source.id}</strong> (now:{' '}
21
+ {data?.status ?? '—'})
22
+ </p>
23
+ <div class="flex gap-2">
24
+ <button
25
+ type="button"
26
+ disabled={isPending || data?.status === 'degraded'}
27
+ onClick={() =>
28
+ void execute({ sourceId: source.id, mode: 'incident' })
29
+ }
30
+ class="px-3 py-1.5 bg-red-600 text-white text-sm rounded disabled:opacity-50"
31
+ >
32
+ Simulate outage (503)
33
+ </button>
34
+ {isPaused && (
35
+ <button
36
+ type="button"
37
+ disabled={isPending}
38
+ onClick={() =>
39
+ void execute({ sourceId: source.id, mode: 'recover' })
40
+ }
41
+ class="px-3 py-1.5 bg-green-600 text-white text-sm rounded disabled:opacity-50"
42
+ >
43
+ Restore service
44
+ </button>
45
+ )}
46
+ </div>
47
+ {error && <p class="text-xs text-red-600 m-0">{error}</p>}
48
+ </div>
49
+ )
50
+ }
@@ -0,0 +1,24 @@
1
+ import { defineSource } from 'monitai'
2
+ import type {
3
+ ServiceMetricsData,
4
+ ServiceMonitorParams,
5
+ } from './service-monitor.types'
6
+
7
+ export default defineSource({
8
+ id: (params: ServiceMonitorParams) =>
9
+ `service:${params.serviceName.toLowerCase()}`,
10
+ interval: '5s',
11
+ description: (params: ServiceMonitorParams) =>
12
+ `Availability and latency metrics for ${params.serviceName}`,
13
+ publicProps: ['serviceName'] as const,
14
+ fetcher: async (params: ServiceMonitorParams): Promise<ServiceMetricsData> => {
15
+ const latencyMs = Math.floor(25 + Math.random() * 35)
16
+ return {
17
+ serviceName: params.serviceName,
18
+ status: 'online',
19
+ latencyMs,
20
+ message: 'Service is operating normally',
21
+ updatedAt: Date.now(),
22
+ }
23
+ },
24
+ })
@@ -0,0 +1,20 @@
1
+ export interface ServiceMonitorParams {
2
+ serviceName: string
3
+ }
4
+
5
+ export interface ServiceMetricsData {
6
+ serviceName: string
7
+ status: 'online' | 'degraded'
8
+ latencyMs: number
9
+ message: string
10
+ updatedAt: number
11
+ }
12
+
13
+ export interface SimulateIncidentInput {
14
+ sourceId: string
15
+ mode: 'incident' | 'recover'
16
+ }
17
+
18
+ export interface SimulateIncidentOutput {
19
+ ok: boolean
20
+ }