forked from sagnik/Project_Velocity
feat/#28 (#29)
Co-authored-by: Sayan Datta <sayan@Sayans-MacBook-Air.local> Reviewed-on: sagnik/Project_Velocity#29
This commit is contained in:
@@ -610,26 +610,49 @@ async def session_heartbeat(
|
||||
pool = _pool(request)
|
||||
import json
|
||||
async with pool.acquire() as conn:
|
||||
row = await conn.fetchrow(
|
||||
existing_session_id = await conn.fetchval(
|
||||
"""
|
||||
INSERT INTO surface_sessions (tenant_id, user_id, surface_type, app_version, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5::jsonb)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING session_id
|
||||
SELECT session_id
|
||||
FROM surface_sessions
|
||||
WHERE tenant_id=$1 AND user_id=$2 AND surface_type=$3
|
||||
AND ended_at IS NULL
|
||||
AND last_active_at > NOW() - INTERVAL '30 minutes'
|
||||
ORDER BY last_active_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
user.role, user.user_id, body.surface_type, body.app_version,
|
||||
json.dumps(body.metadata),
|
||||
user.role, user.user_id, body.surface_type,
|
||||
)
|
||||
# Update last_active + screen_sequence for existing session (within 30 min)
|
||||
if body.screen and row is None:
|
||||
|
||||
if existing_session_id:
|
||||
await conn.execute(
|
||||
"""
|
||||
UPDATE surface_sessions
|
||||
SET last_active_at=NOW(),
|
||||
screen_sequence = array_append(screen_sequence, $1)
|
||||
WHERE tenant_id=$2 AND user_id=$3 AND surface_type=$4
|
||||
AND last_active_at > NOW() - INTERVAL '30 minutes'
|
||||
app_version=$1,
|
||||
metadata=$2::jsonb,
|
||||
screen_sequence = CASE
|
||||
WHEN $3::text IS NULL THEN screen_sequence
|
||||
ELSE array_append(screen_sequence, $3::text)
|
||||
END
|
||||
WHERE session_id=$4
|
||||
""",
|
||||
body.screen, user.role, user.user_id, body.surface_type,
|
||||
body.app_version, json.dumps(body.metadata), body.screen, existing_session_id,
|
||||
)
|
||||
else:
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO surface_sessions (
|
||||
tenant_id, user_id, surface_type, app_version, metadata, screen_sequence
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5::jsonb,
|
||||
CASE
|
||||
WHEN $6::text IS NULL THEN '{}'::text[]
|
||||
ELSE ARRAY[$6::text]
|
||||
END
|
||||
)
|
||||
""",
|
||||
user.role, user.user_id, body.surface_type, body.app_version,
|
||||
json.dumps(body.metadata), body.screen,
|
||||
)
|
||||
return {"status": "ok", "timestamp": _now()}
|
||||
|
||||
Reference in New Issue
Block a user