Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 09:44:50 +03:00
commit ebb1cb7950
101 changed files with 4971 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
import type { PaveClient } from "./pave-client.js";
/** Строки зеркал JobTread (без snapshot_ts - его добавляет mirror при записи). */
export interface JtJob { id: string; number: string | null; name: string; status: string | null; closed_on: string | null }
export interface JtMembership { id: string; user_name: string; role: string | null }
export interface JtTaskType { id: string; name: string }
export interface JtTask {
id: string; job_id: string | null; parent_task_id: string | null; task_type_id: string | null; name: string;
description: string | null; start_date: string | null; end_date: string | null; progress: number | null;
is_todo: boolean; is_group: boolean; is_deleted: boolean;
}
export interface JtTaskDependency { task_id: string; depends_on_task_id: string }
export interface JtTaskAssignment { task_id: string; membership_id: string }
/** Таблицы зеркала JobTread и загрузчики. Каждый загрузчик независим - ошибка одного не влияет на остальные. */
export const JT_TABLES = [
"jt_job", "jt_membership", "jt_task_type", "jt_task", "jt_task_dependency", "jt_task_assignment",
] as const;
export type JtTable = (typeof JT_TABLES)[number];
interface RawTask {
id: string; name: string; description: string | null; startDate: string | null; endDate: string | null;
progress: number | null; isToDo: boolean; isGroup: boolean;
taskType: { id: string } | null; parentTask: { id: string } | null; job: { id: string } | null;
}
const TASK_FIELDS = {
id: {}, name: {}, description: {}, startDate: {}, endDate: {}, progress: {}, isToDo: {}, isGroup: {},
taskType: { id: {} }, parentTask: { id: {} }, job: { id: {} },
};
export function mapTask(t: RawTask): JtTask {
return {
id: t.id, job_id: t.job?.id ?? null, parent_task_id: t.parentTask?.id ?? null, task_type_id: t.taskType?.id ?? null,
name: t.name, description: t.description ?? null, start_date: t.startDate ?? null, end_date: t.endDate ?? null,
progress: t.progress ?? null, is_todo: !!t.isToDo, is_group: !!t.isGroup, is_deleted: false,
};
}
export class JobtreadSource {
constructor(private readonly pave: PaveClient) {}
async loadJobs(): Promise<JtJob[]> {
const raw = await this.pave.pageAll<{ id: string; number: string | null; name: string; status: string | null; closedOn: string | null }>(
"jobs", {}, { id: {}, number: {}, name: {}, status: {}, closedOn: {} },
);
return raw.map((j) => ({ id: j.id, number: j.number ?? null, name: j.name, status: j.status ?? null, closed_on: j.closedOn ?? null }));
}
async loadMemberships(): Promise<JtMembership[]> {
const raw = await this.pave.pageAll<{ id: string; role: { name: string } | null; user: { name: string } | null }>(
"memberships", {}, { id: {}, role: { name: {} }, user: { name: {} } },
);
return raw.map((m) => ({ id: m.id, user_name: m.user?.name ?? "", role: m.role?.name ?? null }));
}
async loadTaskTypes(): Promise<JtTaskType[]> {
return this.pave.pageAll<JtTaskType>("taskTypes", {}, { id: {}, name: {} });
}
async loadTasks(): Promise<JtTask[]> {
const raw = await this.pave.pageAll<RawTask>("tasks", {}, TASK_FIELDS, 100);
return raw.map(mapTask);
}
async loadTaskDependencies(): Promise<JtTaskDependency[]> {
const raw = await this.pave.pageAll<{ task: { id: string } | null; dependsOnTask: { id: string } | null }>(
"taskDependencies", {}, { task: { id: {} }, dependsOnTask: { id: {} } }, 100,
);
return raw.filter((d) => d.task && d.dependsOnTask).map((d) => ({ task_id: d.task!.id, depends_on_task_id: d.dependsOnTask!.id }));
}
async loadTaskAssignments(): Promise<JtTaskAssignment[]> {
const raw = await this.pave.pageAll<{ task: { id: string } | null; membership: { id: string } | null }>(
"taskAssignments", {}, { task: { id: {} }, membership: { id: {} } }, 100,
);
return raw.filter((a) => a.task && a.membership).map((a) => ({ task_id: a.task!.id, membership_id: a.membership!.id }));
}
}

View File

@@ -0,0 +1,63 @@
export interface PaveOptions {
grantKey: string;
organizationId: string;
endpoint?: string;
fetchFn?: typeof fetch;
retries?: number;
}
/** Минимальный клиент JobTread Pave API: JSON-запрос с grantKey, повторы при сетевых/5xx/429 ошибках. */
export class PaveClient {
private readonly endpoint: string;
private readonly fetchFn: typeof fetch;
private readonly retries: number;
constructor(private readonly opts: PaveOptions) {
this.endpoint = opts.endpoint ?? "https://api.jobtread.com/pave";
this.fetchFn = opts.fetchFn ?? fetch;
this.retries = opts.retries ?? 3;
}
get organizationId(): string {
return this.opts.organizationId;
}
async query<T = unknown>(query: Record<string, unknown>): Promise<T> {
const body = JSON.stringify({ query: { $: { grantKey: this.opts.grantKey }, ...query } });
let lastErr: unknown;
for (let attempt = 0; attempt <= this.retries; attempt++) {
try {
const res = await this.fetchFn(this.endpoint, { method: "POST", body });
const text = await res.text();
if (res.status === 429 || res.status >= 500) throw new Error(`Pave HTTP ${res.status}: ${text.slice(0, 200)}`);
if (!res.ok) throw new PaveError(`Pave HTTP ${res.status}: ${text.slice(0, 500)}`);
return JSON.parse(text) as T;
} catch (e) {
if (e instanceof PaveError) throw e;
lastErr = e;
await new Promise((r) => setTimeout(r, 500 * 2 ** attempt));
}
}
throw lastErr instanceof Error ? lastErr : new Error(String(lastErr));
}
/** Читает все страницы коннекции organization.<field>. */
async pageAll<N>(field: string, args: Record<string, unknown>, nodeFields: Record<string, unknown>, size = 50): Promise<N[]> {
const out: N[] = [];
let page: string | null = null;
do {
const res: { organization: Record<string, { nodes: N[]; nextPage: string | null }> } = await this.query({
organization: {
$: { id: this.opts.organizationId },
[field]: { $: { ...args, size, ...(page ? { page } : {}) }, nodes: nodeFields, nextPage: {} },
},
});
const conn = res.organization[field]!;
out.push(...conn.nodes);
page = conn.nextPage;
} while (page);
return out;
}
}
/** Ошибка, повторять которую бессмысленно (4xx). */
export class PaveError extends Error {}