Каркас 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:
134
packages/khbuild_assistant_prep/src/inputs/queries.ts
Normal file
134
packages/khbuild_assistant_prep/src/inputs/queries.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import type { Queryable } from "khb_common";
|
||||
|
||||
/** Параметры сбора по одному проекту. Даты - yyyy-mm-dd по Tampa. */
|
||||
export interface InputParams {
|
||||
jtJobId: string;
|
||||
managerMembershipId: string;
|
||||
today: string;
|
||||
weekStart: string;
|
||||
weekEnd: string;
|
||||
nextWeekStart: string;
|
||||
nextWeekEnd: string;
|
||||
/** Граница "ближайших N рабочих дней" для входа (4). */
|
||||
horizon: string;
|
||||
}
|
||||
|
||||
export interface TaskRow {
|
||||
jt_task_id: string;
|
||||
name: string;
|
||||
start_date: string | null;
|
||||
end_date: string | null;
|
||||
is_todo: boolean;
|
||||
is_purchase: boolean;
|
||||
assignees: string[];
|
||||
related_task_id: string | null;
|
||||
}
|
||||
|
||||
const COLS = `
|
||||
t.id as jt_task_id, t.name, t.is_todo,
|
||||
to_char(t.start_date, 'YYYY-MM-DD') as start_date, to_char(t.end_date, 'YYYY-MM-DD') as end_date,
|
||||
exists (select 1 from purchase_task_ref p where lower(p.task_name) = lower(t.name)) as is_purchase,
|
||||
coalesce((select array_agg(m.user_name order by m.user_name) from jt_task_assignment a
|
||||
join jt_membership m on m.id = a.membership_id where a.task_id = t.id), '{}') as assignees`;
|
||||
|
||||
const MANAGER_TASKS = `
|
||||
from jt_task t
|
||||
join jt_task_assignment ma on ma.task_id = t.id and ma.membership_id = $2
|
||||
where t.job_id = $1 and not t.is_deleted and not t.is_group and coalesce(t.progress, 0) < 1`;
|
||||
|
||||
/** Вход (1): не завершённые работы менеджера на этой неделе и просроченные. */
|
||||
export async function input1(db: Queryable, p: InputParams): Promise<TaskRow[]> {
|
||||
const { rows } = await db.query(
|
||||
`select ${COLS}, null::text as related_task_id ${MANAGER_TASKS} and not t.is_todo
|
||||
and t.end_date is not null and (t.end_date < $3 or (coalesce(t.start_date, t.end_date) <= $5 and t.end_date >= $4))
|
||||
order by t.end_date, t.name`,
|
||||
[p.jtJobId, p.managerMembershipId, p.today, p.weekStart, p.weekEnd],
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** Вход (2): todo менеджера на этой неделе и просроченные. */
|
||||
export async function input2(db: Queryable, p: InputParams): Promise<TaskRow[]> {
|
||||
const { rows } = await db.query(
|
||||
`select ${COLS}, null::text as related_task_id ${MANAGER_TASKS} and t.is_todo
|
||||
and coalesce(t.end_date, t.start_date) is not null
|
||||
and (coalesce(t.end_date, t.start_date) < $3 or coalesce(t.end_date, t.start_date) between $4 and $5)
|
||||
order by coalesce(t.end_date, t.start_date), t.name`,
|
||||
[p.jtJobId, p.managerMembershipId, p.today, p.weekStart, p.weekEnd],
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** Вход (3): todo менеджера на следующую неделю. */
|
||||
export async function input3(db: Queryable, p: InputParams): Promise<TaskRow[]> {
|
||||
const { rows } = await db.query(
|
||||
`select ${COLS}, null::text as related_task_id ${MANAGER_TASKS} and t.is_todo
|
||||
and coalesce(t.end_date, t.start_date) between $3 and $4
|
||||
order by coalesce(t.end_date, t.start_date), t.name`,
|
||||
[p.jtJobId, p.managerMembershipId, p.nextWeekStart, p.nextWeekEnd],
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
const OTHERS = `
|
||||
where t.job_id = $1 and not t.is_deleted and not t.is_group and coalesce(t.progress, 0) < 1
|
||||
and not exists (select 1 from jt_task_assignment a where a.task_id = t.id and a.membership_id = $2)`;
|
||||
|
||||
/**
|
||||
* Вход (4): работы других исполнителей (особенно закупки), зависящие (прямо или через цепочку до 5 звеньев)
|
||||
* от задач менеджера из (1) или (2) и стартующие не позже горизонта (включая просроченные).
|
||||
*/
|
||||
export async function input4(db: Queryable, p: InputParams, managerTaskIds: string[]): Promise<TaskRow[]> {
|
||||
if (!managerTaskIds.length) return [];
|
||||
const { rows } = await db.query(
|
||||
`with recursive dep(root, id, depth) as (
|
||||
select s, d.task_id, 1 from unnest($3::text[]) s join jt_task_dependency d on d.depends_on_task_id = s
|
||||
union all
|
||||
select dep.root, d.task_id, dep.depth + 1 from dep join jt_task_dependency d on d.depends_on_task_id = dep.id where dep.depth < 5
|
||||
)
|
||||
select distinct on (t.id) ${COLS}, dep.root as related_task_id
|
||||
from dep join jt_task t on t.id = dep.id ${OTHERS}
|
||||
and coalesce(t.start_date, t.end_date) is not null and coalesce(t.start_date, t.end_date) <= $4
|
||||
order by t.id, dep.depth`,
|
||||
[p.jtJobId, p.managerMembershipId, managerTaskIds, p.horizon],
|
||||
);
|
||||
return rows.sort((a, b) => String(a.start_date ?? a.end_date).localeCompare(String(b.start_date ?? b.end_date)) || a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
/** Вход (5): не завершённые работы других исполнителей, от которых зависят работы менеджера из (1) или (2). */
|
||||
export async function input5(db: Queryable, p: InputParams, managerTaskIds: string[]): Promise<TaskRow[]> {
|
||||
if (!managerTaskIds.length) return [];
|
||||
const { rows } = await db.query(
|
||||
`with recursive dep(root, id, depth) as (
|
||||
select s, d.depends_on_task_id, 1 from unnest($3::text[]) s join jt_task_dependency d on d.task_id = s
|
||||
union all
|
||||
select dep.root, d.depends_on_task_id, dep.depth + 1 from dep join jt_task_dependency d on d.task_id = dep.id where dep.depth < 5
|
||||
)
|
||||
select distinct on (t.id) ${COLS}, dep.root as related_task_id
|
||||
from dep join jt_task t on t.id = dep.id ${OTHERS}
|
||||
order by t.id, dep.depth`,
|
||||
[p.jtJobId, p.managerMembershipId, managerTaskIds],
|
||||
);
|
||||
return rows.sort((a, b) => String(a.end_date ?? a.start_date).localeCompare(String(b.end_date ?? b.start_date)) || a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
export interface ReminderSummary {
|
||||
jt_task_id: string | null;
|
||||
reminders: number;
|
||||
last_reminded_at: string;
|
||||
last_reply_outcome: string | null;
|
||||
last_reply_summary: string | null;
|
||||
}
|
||||
|
||||
/** Вход (7): компактная история напоминаний за последние days дней: по задаче - сколько раз напоминали и чем ответил менеджер. */
|
||||
export async function input7(db: Queryable, assistantProjectId: string, since: Date): Promise<ReminderSummary[]> {
|
||||
const { rows } = await db.query(
|
||||
`select jt_task_id, count(*)::int as reminders, max(reminded_at) as last_reminded_at,
|
||||
(array_agg(reply_outcome order by reminded_at desc))[1] as last_reply_outcome,
|
||||
(array_agg(reply_summary order by reminded_at desc))[1] as last_reply_summary
|
||||
from reminder_history where assistant_project_id = $1 and reminded_at >= $2
|
||||
group by jt_task_id order by max(reminded_at) desc`,
|
||||
[assistantProjectId, since],
|
||||
);
|
||||
return rows.map((r) => ({ ...r, last_reminded_at: (r.last_reminded_at as Date).toISOString() }));
|
||||
}
|
||||
Reference in New Issue
Block a user