* 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
)
| 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 |
no test coverage detected