MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / createEngine

Function createEngine

packages/db/src/services/insights/engine.ts:69–318  ·  view source on GitHub ↗
(args: {
  store: InsightStore;
  modules: InsightModule[];
  db: any;
  logger?: Pick<Console, 'info' | 'warn' | 'error'>;
  config: EngineConfig;
})

Source from the content-addressed store, hash-verified

67}
68
69export function createEngine(args: {
70 store: InsightStore;
71 modules: InsightModule[];
72 db: any;
73 logger?: Pick<Console, 'info' | 'warn' | 'error'>;
74 config: EngineConfig;
75}) {
76 const { store, modules, db, config } = args;
77 const logger = args.logger ?? console;
78
79 function isProjectOldEnoughForWindow(
80 projectCreatedAt: Date | null | undefined,
81 baselineStart: Date,
82 ): boolean {
83 if (!projectCreatedAt) return true; // best-effort; don't block if unknown
84 return projectCreatedAt.getTime() <= baselineStart.getTime();
85 }
86
87 async function runProject(opts: {
88 projectId: string;
89 cadence: Cadence;
90 now: Date;
91 projectCreatedAt?: Date | null;
92 }): Promise<void> {
93 const { projectId, cadence, now, projectCreatedAt } = opts;
94 const projLogger = logger;
95 const eligible = modules.filter((m) => m.cadence.includes(cadence));
96
97 for (const mod of eligible) {
98 const windows = mod.windows ?? DEFAULT_WINDOWS;
99 for (const windowKind of windows) {
100 let window: ReturnType<typeof resolveWindow>;
101 let ctx: ComputeContext;
102 try {
103 window = resolveWindow(windowKind, now);
104 if (
105 !isProjectOldEnoughForWindow(projectCreatedAt, window.baselineStart)
106 ) {
107 continue;
108 }
109 // Initialize cache for this module+window combination.
110 // Cache is automatically garbage collected when context goes out of scope.
111 const cache = new Map<string, any>();
112 ctx = {
113 projectId,
114 window,
115 db,
116 now,
117 logger: projLogger,
118 clix: createCachedClix(db, cache),
119 };
120 } catch (e) {
121 projLogger.error('[insights] failed to create compute context', {
122 projectId,
123 module: mod.key,
124 windowKind,
125 err: e,
126 });

Callers 1

insightsProjectJobFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected