diff --git a/src/Models/Apis/RequestDumps.hs b/src/Models/Apis/RequestDumps.hs index 2c8a61dcd..47ea248c3 100644 --- a/src/Models/Apis/RequestDumps.hs +++ b/src/Models/Apis/RequestDumps.hs @@ -503,7 +503,7 @@ validateSqlQuery query = `T.isInfixOf` lowerQuery -selectLogTable :: (DB es, Log :> es, Time.Time :> es) => Projects.ProjectId -> [Section] -> Text -> Maybe UTCTime -> (Maybe UTCTime, Maybe UTCTime) -> [Text] -> Maybe Sources -> Maybe Text -> Eff es (Either Text (V.Vector (V.Vector AE.Value), [Text], Int)) +selectLogTable :: (DB es, Log :> es, Time.Time :> es) => Projects.ProjectId -> [Section] -> Text -> Maybe UTCTime -> (Maybe UTCTime, Maybe UTCTime) -> [Text] -> Maybe Sources -> Maybe Text -> Eff es (Either Text (V.Vector (V.Vector AE.Value), [Text], Int, Text)) selectLogTable pid queryAST queryText cursorM dateRange projectedColsByUser source targetSpansM = do now <- Time.currentTime let (q, queryComponents) = queryASTToComponents ((defSqlQueryCfg pid now source targetSpansM){cursorM, dateRange, projectedColsByUser, source, targetSpansM}) queryAST @@ -530,7 +530,7 @@ selectLogTable pid queryAST queryText cursorM dateRange projectedColsByUser sour firstRow <- logItemsV V.!? 0 AE.Number n <- firstRow V.!? (V.length firstRow - 1) pure $ round n - pure $ Right (V.map dropLast logItemsV, queryComponents.toColNames, count) + pure $ Right (V.map dropLast logItemsV, queryComponents.toColNames, count, q) selectChildSpansAndLogs :: (DB es, Time.Time :> es) => Projects.ProjectId -> [Text] -> V.Vector Text -> (Maybe UTCTime, Maybe UTCTime) -> V.Vector Text -> Eff es [V.Vector AE.Value] diff --git a/src/Pages/Bots/Utils.hs b/src/Pages/Bots/Utils.hs index f87bee6fd..5f688c7ce 100644 --- a/src/Pages/Bots/Utils.hs +++ b/src/Pages/Bots/Utils.hs @@ -58,7 +58,7 @@ contentTypeHeader :: Text -> Data.Effectful.Wreq.Options -> Data.Effectful.Wreq. contentTypeHeader contentType = header "Content-Type" .~ [encodeUtf8 contentType] -handleTableResponse :: BotType -> Either Text (V.Vector (V.Vector AE.Value), [Text], Int) -> EnvConfig -> Projects.ProjectId -> Text -> AE.Value +handleTableResponse :: BotType -> Either Text (V.Vector (V.Vector AE.Value), [Text], Int, Text) -> EnvConfig -> Projects.ProjectId -> Text -> AE.Value handleTableResponse target tableAsVecE envCfg projectId query = case tableAsVecE of Left err -> case target of @@ -66,7 +66,7 @@ handleTableResponse target tableAsVecE envCfg projectId query = WhatsApp -> AE.object ["body" AE..= "Error processing query"] Slack -> AE.object ["text" AE..= "Error processing query: "] Right tableAsVec -> do - let (requestVecs, colNames, resultCount) = tableAsVec + let (requestVecs, colNames, resultCount, _sql) = tableAsVec colIdxMap = listToIndexHashMap colNames tableData = recsVecToTableData requestVecs colIdxMap url' = envCfg.hostUrl <> "p/" <> projectId.toText <> "/log_explorer?query=" <> decodeUtf8 (urlEncode True $ encodeUtf8 query) diff --git a/src/Pages/LogExplorer/Log.hs b/src/Pages/LogExplorer/Log.hs index eb6977e7a..fe114f22b 100644 --- a/src/Pages/LogExplorer/Log.hs +++ b/src/Pages/LogExplorer/Log.hs @@ -393,7 +393,7 @@ apiLogH pid queryM' cols' cursorM' sinceM fromM toM layoutM sourceM targetSpansM tableAsVecE <- if shouldSkipLoad - then pure $ Right (V.empty, ["timestamp", "summary", "duration"], 0) + then pure $ Right (V.empty, ["timestamp", "summary", "duration"], 0, "") else fetchLogs -- FIXME: we're silently ignoring parse errors and the likes. @@ -454,7 +454,7 @@ apiLogH pid queryM' cols' cursorM' sinceM fromM toM layoutM sourceM targetSpansM case tableAsVecM of Just tableAsVec -> do - let (requestVecs, colNames, resultCount) = tableAsVec + let (requestVecs, colNames, resultCount, generatedSql) = tableAsVec curatedColNames = nubOrd $ curateCols summaryCols colNames colIdxMap = listToIndexHashMap colNames reqLastCreatedAtM = (\r -> lookupVecTextByKey r colIdxMap "timestamp") =<< (requestVecs V.!? (V.length requestVecs - 1)) @@ -523,6 +523,7 @@ apiLogH pid queryM' cols' cursorM' sinceM fromM toM layoutM sourceM targetSpansM , targetPattern = pTargetM , project = project , teams + , generatedSql = Just generatedSql } let jsonResponse = LogsGetJson finalVecs colors nextLogsURL resetLogsURL recentLogsURL curatedColNames colIdxMap resultCount @@ -610,6 +611,7 @@ data ApiLogsPageData = ApiLogsPageData , targetPattern :: Maybe Text , project :: Projects.Project , teams :: V.Vector ManageMembers.Team + , generatedSql :: Maybe Text } @@ -672,6 +674,21 @@ apiLogsPage page = do , patternSelected = page.targetPattern } + -- SQL preview for debugging + whenJust page.generatedSql \sql -> + unless (T.null sql) do + details_ [class_ "mt-2 text-xs text-textWeak"] do + summary_ [class_ "cursor-pointer hover:text-textStrong select-none"] "Show generated SQL" + div_ [class_ "space-y-1 p-3 bg-fillWeaker rounded-lg font-mono text-xs mt-2"] do + div_ [class_ "flex justify-between items-center"] do + span_ [class_ "text-textWeak font-sans"] "Generated SQL Query" + button_ + [ class_ "text-textBrand hover:underline font-sans text-xs" + , term "_" [text| on click writeText(`${T.replace "`" "\\`" sql}`) to the navigator's clipboard then set my.innerText to 'Copied!' then wait 1.5s then set my.innerText to 'Copy' |] + ] + "Copy" + pre_ [class_ "whitespace-pre-wrap break-all bg-fillWeak p-2 rounded overflow-x-auto max-h-48"] $ toHtml sql + div_ [class_ "timeline flex flex-row gap-4 mt-3 group-has-[.no-chart:checked]/pg:hidden group-has-[.toggle-chart:checked]/pg:hidden w-full min-h-36 ", style_ "aspect-ratio: 10 / 1;"] do let patternTarget = fromMaybe "log_pattern" page.targetPattern nm = fromMaybe "Log" $ viaNonEmpty head $ T.splitOn "_" patternTarget diff --git a/src/Pkg/AI.hs b/src/Pkg/AI.hs index 036a6550e..c3b798876 100644 --- a/src/Pkg/AI.hs +++ b/src/Pkg/AI.hs @@ -762,4 +762,5 @@ runKqlAndFormat config kqlQuery cols formatResult = case parseQueryToAST kqlQuer Left parseErr -> pure $ "Error: Query parse failed - " <> show parseErr Right queryAST -> do resultE <- selectLogTable config.projectId queryAST kqlQuery Nothing config.timeRange cols Nothing Nothing - pure $ either ("Error: Query execution failed - " <>) formatResult resultE + -- Drop the SQL query (4th element) since we only need the first 3 for formatting + pure $ either ("Error: Query execution failed - " <>) (formatResult . \(vecs, names, count, _sql) -> (vecs, names, count)) resultE