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

Method ingest

packages/db/src/buffers/session-buffer.ts:132–169  ·  view source on GitHub ↗

* Ingest one event into the session lifecycle. * * Reads the device's current session (if any), decides whether the event * extends it, opens a brand-new one, or boundaries (gap > 30min) → close * the old session and start a fresh one. Writes the updated session blob, * updates the wa

(
    payload: IServiceCreateEventPayload
  )

Source from the content-addressed store, hash-verified

130 * themselves are skipped — they are derived signals, not session activity.
131 */
132 async ingest(
133 payload: IServiceCreateEventPayload
134 ): Promise<SessionIngestResult | null> {
135 if (!payload.projectId || !payload.deviceId) return null;
136 if (payload.name === 'session_start' || payload.name === 'session_end') {
137 return null;
138 }
139
140 try {
141 const existing = await this.getExistingSession({
142 projectId: payload.projectId,
143 deviceId: payload.deviceId,
144 });
145
146 const eventTimeMs = payload.createdAt.getTime();
147 const isBoundary =
148 existing &&
149 eventTimeMs - fromClickhouseDate(existing.ended_at).getTime() >
150 SESSION_TIMEOUT_MS;
151
152 if (existing && !isBoundary) {
153 const { current, chRows } = this.extendSession(existing, payload);
154 await this.persist(current, chRows);
155 return { kind: 'extend', current };
156 }
157
158 const current = this.newSession(payload);
159 await this.persist(current, [current]);
160
161 if (existing && isBoundary) {
162 return { kind: 'boundary', current, closed: existing };
163 }
164 return { kind: 'new', current };
165 } catch (error) {
166 this.logger.error({ err: error }, 'Failed to ingest session');
167 return null;
168 }
169 }
170
171 /**
172 * Remove a closed session from Redis. Atomically gated on `sessionId` via

Callers 2

incomingEventFunction · 0.80

Calls 6

getExistingSessionMethod · 0.95
extendSessionMethod · 0.95
persistMethod · 0.95
newSessionMethod · 0.95
fromClickhouseDateFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected