diff --git a/src/Models/Apis/LogQueries.hs b/src/Models/Apis/LogQueries.hs index f0eb2268a..c5e47788e 100644 --- a/src/Models/Apis/LogQueries.hs +++ b/src/Models/Apis/LogQueries.hs @@ -42,6 +42,7 @@ import Data.Time (UTCTime, addUTCTime, diffUTCTime) import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds) import Data.Time.Format import Data.Time.Format.ISO8601 (iso8601Show) +import Data.UUID qualified as UUID import Data.Vector qualified as V import Database.PostgreSQL.Simple.FromField (FromField) import Database.PostgreSQL.Simple.Newtypes (Aeson (..)) @@ -434,6 +435,9 @@ data SessionRow = SessionRow , landingUrl :: Maybe Text , userAgent :: Maybe Text , firstError :: Maybe Text + , hasReplay :: Bool + -- ^ Whether a screen recording exists for this session (a + -- @projects.replay_sessions@ row). The Replay action is hidden without one. } deriving stock (Generic, Show) @@ -754,6 +758,16 @@ fetchSessions enableTfReads pid queryAST dateRange sortByM skip = do <> [HI.sql| DESC NULLS LAST|] rawRows :: [RawSessionRow] <- Hasql.withHasqlTimefusion enableTfReads $ Hasql.interp q Log.logTrace "fetchSessions: query done" $ AE.object ["rows" AE..= length rawRows] + -- Which of this page's sessions actually have a screen recording. Lives in + -- Postgres (projects.replay_sessions), never in telemetry, so it's a second + -- lookup over the page's ids only. Non-UUID session keys (sessions derived + -- from user id/email) can never have a recording, so they're not even asked for. + replayedIds :: [UUID.UUID] <- case mapMaybe (UUID.fromText . (.sessionId)) rawRows of + [] -> pure [] + sids -> Hasql.interp [HI.sql| SELECT session_id FROM projects.replay_sessions WHERE project_id = #{pid} AND session_id = ANY(#{sids}::uuid[]) |] + -- Keyed on the parsed UUID, not its text: an SDK that emits an uppercase id + -- round-trips through UUID.toText as lowercase and would never match itself. + let replayed = S.fromList replayedIds let allBuckets = concatMap (\r -> map fromIntegral $ V.toList r.bis) rawRows range = bucketRange allBuckets -- Densify the header's over-time buckets across the full picker range so @@ -800,6 +814,7 @@ fetchSessions enableTfReads pid queryAST dateRange sortByM skip = do , landingUrl = r.landingUrl , userAgent = r.userAgent , firstError = r.firstError + , hasReplay = maybe False (`S.member` replayed) (UUID.fromText r.sessionId) } pure (summary, total, map toRowWithVolume rawRows) diff --git a/src/Pages/LogExplorer/Log.hs b/src/Pages/LogExplorer/Log.hs index ef1d68bf1..d71056511 100644 --- a/src/Pages/LogExplorer/Log.hs +++ b/src/Pages/LogExplorer/Log.hs @@ -1383,9 +1383,11 @@ instance AE.ToJSON SessionsView where ] where -- Full session id (not truncated): the client feeds it into /replay_session/{id}, a Servant UUID capture. + -- The tag is what renders the Replay action, so it's emitted only for + -- sessions that actually have a recording. summaryParts = catMaybes - [ Just $ tag "session" "right-neutral" s.sessionId + [ tag "session" "right-neutral" s.sessionId <$ guard s.hasReplay , Just $ field "user" (LogQueries.sessionUserDisplay s.userEmail s.userName s.userId) , field "url" <$> mfilter (not . T.null) s.landingUrl , field "device" <$> mfilter (not . T.null) s.userAgent diff --git a/test/integration/Opentelemetry/GrpcIngestionSpec.hs b/test/integration/Opentelemetry/GrpcIngestionSpec.hs index 32846410b..01b81dfbf 100644 --- a/test/integration/Opentelemetry/GrpcIngestionSpec.hs +++ b/test/integration/Opentelemetry/GrpcIngestionSpec.hs @@ -44,7 +44,7 @@ import Proto.Opentelemetry.Proto.Metrics.V1.Metrics_Fields qualified as PMF import Proto.Opentelemetry.Proto.Resource.V1.Resource_Fields qualified as PRF import Relude import System.Config (AuthContext (metricCatalogBuffer)) -import Test.Hspec (Spec, aroundAll, describe, expectationFailure, it, sequential, shouldBe, shouldContain, shouldNotContain, shouldSatisfy, shouldThrow) +import Test.Hspec (Spec, aroundAll, describe, expectationFailure, it, sequential, shouldBe, shouldContain, shouldNotContain, shouldNotSatisfy, shouldSatisfy, shouldThrow) pid :: Projects.ProjectId @@ -628,6 +628,37 @@ spec = sequential $ aroundAll withTestResources do cols `shouldContain` ["timestamp"] hasMore `shouldBe` False + -- Regression: the Replay action rendered on every session row, including + -- sessions the SDK never recorded a screen for — clicking it opened a + -- player that could only say "no recorded events". The action is driven by + -- the `session` summary tag, now emitted only when a replay row exists. + it "Test 11.4b: Replay action is emitted only for sessions with a recording" $ \tr -> do + key <- createTestAPIKey tr pid "sessions-replay-key" + recorded <- UUIDV4.nextRandom + plain <- UUIDV4.nextRandom + -- Emitted uppercase: the match is on the parsed UUID, so a case the SDK + -- chose must not decide whether the Replay action appears. + shouty <- UUIDV4.nextRandom + ingestSessionEvent tr key "GET /recorded" [("session.id", UUID.toText recorded)] False frozenTime + ingestSessionEvent tr key "GET /plain" [("session.id", UUID.toText plain)] False frozenTime + ingestSessionEvent tr key "GET /shouty" [("session.id", T.toUpper (UUID.toText shouty))] False frozenTime + void + $ withPool tr.trPool + $ DBT.executeMany [sql| INSERT INTO projects.replay_sessions (session_id, project_id) VALUES (?, ?) |] [(recorded, pid), (shouty, pid)] + void $ runAllBackgroundJobs frozenTime tr.trATCtx + view <- queryLogsViz tr Nothing "sessions" + case view of + Log.SessionsView _ sessionRows _ -> do + let replayOf sid = (.hasReplay) <$> V.find (\s -> T.toLower s.sessionId == UUID.toText sid) sessionRows + replayOf recorded `shouldBe` Just True + replayOf plain `shouldBe` Just False + replayOf shouty `shouldBe` Just True + -- ... and the payload the client renders the button from follows suit. + let j = decodeUtf8 (AE.encode view) :: Text + sessionTag sid = "session;right-neutral\8658" <> UUID.toText sid + j `shouldSatisfy` T.isInfixOf (sessionTag recorded) + j `shouldNotSatisfy` T.isInfixOf (sessionTag plain) + it "Test 11.5: expand endpoint rejects missing key with 400" $ \tr -> do let (timeFrom, timeTo) = testTimeRange ( do