|
| 1 | +import { DecisionOutcomesPerDayQueryDto, type DecisionOutcomesPerDayResponseDto } from 'marble-api'; |
| 2 | +import z from 'zod'; |
| 3 | + |
| 4 | +export const decisionOutcomesPerDay = z.object({ |
| 5 | + absolute: z.array( |
| 6 | + z.object({ |
| 7 | + date: z.iso.datetime(), |
| 8 | + approve: z.number(), |
| 9 | + blockAndReview: z.number(), |
| 10 | + decline: z.number(), |
| 11 | + review: z.number(), |
| 12 | + total: z.number(), |
| 13 | + }), |
| 14 | + ), |
| 15 | + ratio: z.array( |
| 16 | + z.object({ |
| 17 | + date: z.iso.datetime(), |
| 18 | + approve: z.number(), |
| 19 | + blockAndReview: z.number(), |
| 20 | + decline: z.number(), |
| 21 | + review: z.number(), |
| 22 | + }), |
| 23 | + ), |
| 24 | +}); |
| 25 | + |
| 26 | +export const triggerFilter = z.object({ |
| 27 | + field: z.uuidv4(), |
| 28 | + op: z.enum(['=', '!=', '>', '>=', '<', '<=']), |
| 29 | + values: z.array(z.string()), |
| 30 | +}); |
| 31 | + |
| 32 | +export const decisionOutcomesPerDayQuery = z.object({ |
| 33 | + start: z.date(), |
| 34 | + end: z.date(), |
| 35 | + scenarioId: z.uuidv4(), |
| 36 | + scenarioVersion: z.number().optional(), |
| 37 | + trigger: z.array(triggerFilter), |
| 38 | +}); |
| 39 | + |
| 40 | +export type DecisionOutcomesPerDay = z.infer<typeof decisionOutcomesPerDay>; |
| 41 | +export type DecisionOutcomesPerDayQuery = z.infer<typeof decisionOutcomesPerDayQuery>; |
| 42 | + |
| 43 | +function toUtcDayKey(date: Date): string { |
| 44 | + const y = date.getUTCFullYear(); |
| 45 | + const m = `${date.getUTCMonth() + 1}`.padStart(2, '0'); |
| 46 | + const d = `${date.getUTCDate()}`.padStart(2, '0'); |
| 47 | + return `${y}-${m}-${d}`; |
| 48 | +} |
| 49 | + |
| 50 | +// TODO: Helper function to be moved to a utils file |
| 51 | +function startOfUtcDay(date: Date): Date { |
| 52 | + return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())); |
| 53 | +} |
| 54 | + |
| 55 | +export function findMissingDays( |
| 56 | + data: DecisionOutcomesPerDayResponseDto[], |
| 57 | + start: Date, |
| 58 | + end: Date, |
| 59 | +): string[] { |
| 60 | + const startDay = startOfUtcDay(start); |
| 61 | + const endDay = startOfUtcDay(end); |
| 62 | + |
| 63 | + const existing = new Set(data.map((d) => toUtcDayKey(new Date(d.date)))); |
| 64 | + |
| 65 | + const missing: string[] = []; |
| 66 | + for (let t = startDay.getTime(); t <= endDay.getTime(); t += 24 * 60 * 60 * 1000) { |
| 67 | + const key = toUtcDayKey(new Date(t)); |
| 68 | + if (!existing.has(key)) missing.push(key); |
| 69 | + } |
| 70 | + return missing; |
| 71 | +} |
| 72 | + |
| 73 | +// Fill the days without data with zero values between [start, end] |
| 74 | +export function fillMissingDays( |
| 75 | + data: DecisionOutcomesPerDayResponseDto[], |
| 76 | + start: Date, |
| 77 | + end: Date, |
| 78 | +): DecisionOutcomesPerDayResponseDto[] { |
| 79 | + const missing = findMissingDays(data, start, end); |
| 80 | + const filled = [ |
| 81 | + ...data, |
| 82 | + ...missing.map((key) => ({ |
| 83 | + date: `${key}T00:00:00.000Z`, |
| 84 | + approve: 0, |
| 85 | + block_and_review: 0, |
| 86 | + decline: 0, |
| 87 | + review: 0, |
| 88 | + })), |
| 89 | + ]; |
| 90 | + filled.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); |
| 91 | + return filled; |
| 92 | +} |
| 93 | + |
| 94 | +export const transformDecisionOutcomesPerDayQuery = decisionOutcomesPerDayQuery.transform( |
| 95 | + (val): DecisionOutcomesPerDayQueryDto => { |
| 96 | + return { |
| 97 | + start: val.start.toISOString(), |
| 98 | + end: val.end.toISOString(), |
| 99 | + scenario_id: val.scenarioId, |
| 100 | + scenario_versions: val.scenarioVersion ? [val.scenarioVersion] : [], |
| 101 | + trigger: val.trigger, |
| 102 | + }; |
| 103 | + }, |
| 104 | +); |
| 105 | + |
| 106 | +export const adaptDecisionOutcomesPerDay = z.array(z.any()).transform( |
| 107 | + (val: DecisionOutcomesPerDayResponseDto[]): DecisionOutcomesPerDay => ({ |
| 108 | + absolute: val.map((v) => ({ |
| 109 | + date: v.date, |
| 110 | + approve: v.approve, |
| 111 | + blockAndReview: v.block_and_review, |
| 112 | + decline: v.decline, |
| 113 | + review: v.review, |
| 114 | + total: v.approve + v.block_and_review + v.decline + v.review, |
| 115 | + })), |
| 116 | + ratio: val.map((v) => { |
| 117 | + const total = v.approve + v.block_and_review + v.decline + v.review; |
| 118 | + return { |
| 119 | + date: v.date, |
| 120 | + approve: total ? (100 * v.approve) / total : 0, |
| 121 | + blockAndReview: total ? (100 * v.block_and_review) / total : 0, |
| 122 | + decline: total ? (100 * v.decline) / total : 0, |
| 123 | + review: total ? (100 * v.review) / total : 0, |
| 124 | + }; |
| 125 | + }), |
| 126 | + }), |
| 127 | +); |
0 commit comments