add back set to current values.

This commit is contained in:
Первов Артем
2026-06-19 09:10:49 +03:00
parent 05a9e0e535
commit f5ed7fa9be

View File

@@ -25,7 +25,7 @@ export class IngestService {
private readonly config: ConfigService,
) {}
/** Проверяет батч и записывает сырые точки в существующую таблицу history. */
/** Проверяет батч, пишет сырую историю и обновляет последний снимок current для UI/SSE. */
async ingestPoints(points: IngestPointDto[]): Promise<IngestResult> {
if (!points.length) {
return { processed: 0 };
@@ -54,6 +54,36 @@ export class IngestService {
],
);
await this.db.query(
`
INSERT INTO current (edge, tag, value, "createdAt", "updatedAt")
SELECT DISTINCT ON (edge, tag)
edge,
tag,
value,
time AS "createdAt",
time AS "updatedAt"
FROM unnest(
$1::timestamp[],
$2::varchar[],
$3::varchar[],
$4::double precision[]
) AS point(time, edge, tag, value)
ORDER BY edge ASC, tag ASC, time DESC
ON CONFLICT (edge, tag)
DO UPDATE SET
value = EXCLUDED.value,
"updatedAt" = EXCLUDED."updatedAt"
WHERE current."updatedAt" <= EXCLUDED."updatedAt"
`,
[
normalized.map((point) => point.time),
normalized.map((point) => point.edge),
normalized.map((point) => point.tag),
normalized.map((point) => point.value),
],
);
return { processed: normalized.length };
}