Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
57
packages/khbuild_assistant_prep/src/inputs/package-writer.ts
Normal file
57
packages/khbuild_assistant_prep/src/inputs/package-writer.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { withTx, type Pool } from "khb_common";
|
||||
import type { PackageItem } from "./build.js";
|
||||
|
||||
/**
|
||||
* Хэш содержимого пакета. Вход (7) (история напоминаний) в хэш не входит: за полдня в 11:50 он может измениться
|
||||
* только из-за самих напоминаний ассистента, а не из-за изменения данных стройки.
|
||||
*/
|
||||
export function contentHash(items: PackageItem[]): string {
|
||||
const stable = items
|
||||
.filter((i) => i.input_no !== 7)
|
||||
.map((i) => [i.input_no, i.jt_task_id, i.name, i.start_date, i.end_date, i.is_overdue, i.is_purchase, i.assignees, i.related_task_id, i.days_until_start])
|
||||
.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
|
||||
return createHash("sha256").update(JSON.stringify(stable)).digest("hex");
|
||||
}
|
||||
|
||||
export type PackageKind = "morning" | "noon";
|
||||
|
||||
export interface WrittenPackage {
|
||||
id: string;
|
||||
hash: string;
|
||||
changedSinceMorning: boolean | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Пишет пакет входных данных проекта на день (повторная запись за ту же дату и вид заменяет прежний).
|
||||
* Для noon сравнивает хэш с утренним пакетом: нет утреннего -> changed = true.
|
||||
*/
|
||||
export async function writePackage(
|
||||
pool: Pool,
|
||||
opts: { assistantProjectId: string; date: string; kind: PackageKind; sourceSnapshotTs: Date | null; items: PackageItem[] },
|
||||
): Promise<WrittenPackage> {
|
||||
const hash = contentHash(opts.items);
|
||||
let changed: boolean | null = null;
|
||||
if (opts.kind === "noon") {
|
||||
const m = await pool.query("select content_hash from ap_input_package where assistant_project_id = $1 and package_date = $2 and kind = 'morning'", [opts.assistantProjectId, opts.date]);
|
||||
changed = m.rows[0] ? m.rows[0].content_hash !== hash : true;
|
||||
}
|
||||
const id = await withTx(pool, async (c) => {
|
||||
await c.query("delete from ap_input_package where assistant_project_id = $1 and package_date = $2 and kind = $3", [opts.assistantProjectId, opts.date, opts.kind]);
|
||||
const { rows } = await c.query(
|
||||
`insert into ap_input_package (assistant_project_id, package_date, kind, source_snapshot_ts, content_hash, item_count, changed_since_morning)
|
||||
values ($1,$2,$3,$4,$5,$6,$7) returning id`,
|
||||
[opts.assistantProjectId, opts.date, opts.kind, opts.sourceSnapshotTs, hash, opts.items.length, changed],
|
||||
);
|
||||
const pkgId = rows[0].id as string;
|
||||
for (const it of opts.items) {
|
||||
await c.query(
|
||||
`insert into ap_input_package_item (package_id, input_no, jt_task_id, name, start_date, end_date, is_overdue, is_purchase, assignees, related_task_id, days_until_start, payload)
|
||||
values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)`,
|
||||
[pkgId, it.input_no, it.jt_task_id, it.name, it.start_date, it.end_date, it.is_overdue, it.is_purchase, it.assignees, it.related_task_id, it.days_until_start, it.payload ? JSON.stringify(it.payload) : null],
|
||||
);
|
||||
}
|
||||
return pkgId;
|
||||
});
|
||||
return { id, hash, changedSinceMorning: changed };
|
||||
}
|
||||
Reference in New Issue
Block a user