@dudousxd/nestjs-telescope
Version:
Laravel Telescope-style observability console for NestJS — core: watchers, recorder, correlation, SQLite store, headless API.
86 lines • 4.78 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
// packages/core/src/metrics/timeseries.service.ts
import { Inject, Injectable, Optional } from '@nestjs/common';
import { EntryType } from '../entry/entry.js';
import { TELESCOPE_STORAGE } from '../nest/telescope.options.js';
import { floorToBucket, isRollupStore } from '../rollup/rollup-store.js';
import { collectEntriesInWindow } from './collect-window.js';
import { timeseriesFromRollups } from './timeseries-from-rollups.js';
import { bucketTimeseries } from './timeseries.js';
/** Metric universe queried when no explicit `type` filter is supplied. */
const BUILTIN_METRICS = Object.values(EntryType);
const DEFAULT_BUCKETS = 60;
const MAX_BUCKETS = 500;
/** Computes a throughput time-series by aggregating stored entries over a
* window. Reads the store on demand — no separate time-series capture. */
let TimeseriesService = class TimeseriesService {
storage;
pageSize;
scanCap;
constructor(storage, options) {
this.storage = storage;
this.pageSize = options?.pageSize;
this.scanCap = options?.scanCap;
}
async getTimeseries(query) {
if (!Number.isFinite(query.windowMs) || query.windowMs <= 0) {
throw new RangeError(`windowMs must be a positive, finite number (got ${query.windowMs}).`);
}
const buckets = Math.min(MAX_BUCKETS, Math.max(1, Math.floor(query.buckets ?? DEFAULT_BUCKETS)));
const windowEnd = new Date();
const windowStart = new Date(windowEnd.getTime() - query.windowMs);
// Fast path: read pre-aggregated rollups when the store supports them and
// the query is rollup-answerable (no tag filter — rollups aggregate across
// tags). Falls through to the raw scan otherwise; the output matches.
if (isRollupStore(this.storage) && query.tag === undefined) {
return this.fromRollups(this.storage, query, windowStart, windowEnd, buckets);
}
const baseQuery = {
after: windowStart,
// bucketTimeseries only reads createdAt + type, so scan content-less.
omitContent: true,
...(query.type !== undefined ? { type: query.type } : {}),
...(query.tag !== undefined ? { tag: query.tag } : {}),
};
const { entries, scanned, truncated } = await collectEntriesInWindow(this.storage, baseQuery, {
...(this.pageSize !== undefined ? { pageSize: this.pageSize } : {}),
...(this.scanCap !== undefined ? { scanCap: this.scanCap } : {}),
});
const report = bucketTimeseries(entries, windowStart, windowEnd, buckets);
return { ...report, scanned, truncated };
}
/**
* Rollup-backed assembly. Queries the 1-minute rollup buckets covering the
* window for the relevant metrics (the single `type` if filtered, else the
* builtin metric universe) and re-buckets them into the report's requested
* `buckets`. `scanned` reflects the number of rollup rows read; rollups never
* truncate (they are bounded by minute granularity, not raw volume).
*/
async fromRollups(store, query, windowStart, windowEnd, buckets) {
const metrics = query.type !== undefined ? [query.type] : BUILTIN_METRICS;
const fromBucket = floorToBucket(windowStart.getTime());
const toBucket = floorToBucket(windowEnd.getTime());
const rollups = await store.queryRollups(metrics, fromBucket, toBucket);
const report = timeseriesFromRollups(rollups, windowStart, windowEnd, buckets);
return { ...report, scanned: rollups.length, truncated: false };
}
};
TimeseriesService = __decorate([
Injectable(),
__param(0, Inject(TELESCOPE_STORAGE)),
__param(1, Optional()),
__metadata("design:paramtypes", [Object, Object])
], TimeseriesService);
export { TimeseriesService };
//# sourceMappingURL=timeseries.service.js.map