The chart query was generating invalid SQL like `(count(*) AS count_)::float`
because the float cast wrapped the entire expression including the alias.
Fixed by adding ::float cast directly in Count/CountIf/DCount Display instances,
so the alias is correctly placed after the cast: `count(*)::float AS count_`
Add missing SLTrace case to severityStyle and severityText functions
in SummaryGenerator.hs. This fixes the CI build failure caused by
-Werror=incomplete-patterns.
* Add comprehensive KQL reference documentation
Document all KQL operators and functions supported in Monoscope:
- Comparison, string, membership, and logical operators
- Pipe operators (where, summarize, sort, take)
- Aggregation functions including percentiles and conditional counts
- Scalar functions (strcat, coalesce, iff, case)
- Date/time functions (now, ago, bin, bin_auto)
- Field reference syntax including nested JSON and OTel attributes
- Extensive query examples for common use cases
* Add KQL-standard default column aliases for aggregations
- Generate default aliases like count_, sum_field when no explicit alias provided
- Update Display instance to include AS alias clause in generated SQL
- Document default column naming conventions in KQL reference
- Clarify matches/=~ as syntax variants for regex matching
* Improve KQL documentation clarity and completeness
- Clarify has vs contains operators with examples
- Fix bin_auto() time range table notation
- Add Limitations & Notes section covering:
- Unsupported KQL features
- Null handling behavior
- Type coercion rules
- Performance tips
---------
Co-authored-by: Claude <noreply@anthropic.com>
* Validate and normalize log severity levels on ingestion
Invalid severity values (e.g., metric units like "ms", "{vacuums}") are
now stored as NULL instead of polluting log level facets. Valid levels
are normalized to uppercase, and common aliases are mapped:
- WARNING -> WARN
- INFORMATION -> INFO
- CRITICAL -> FATAL
- TRACE -> DEBUG
* Add SLTrace level and consolidate severity parsing functions
- Add TRACE as first-class severity level per OpenTelemetry spec
- Consolidate parseSeverityLevel and normalizeSeverityLevel using shared lookup table
- Reduce 24 lines of case statements to 8 lines
* Auto-format code with fourmolu
* Use Map for O(log n) severity lookup and reduce redundancy
- Replace list with Map.Map for efficient lookup
- Separate canonical levels from aliases to avoid repetition
- Add INLINE pragmas for hot path functions
- Clearer non-point-free style for readability
* Auto-format code with fourmolu
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* Optimize query editor performance with aggressive Monaco settings
- Add schema resolution caching (nestedCache, valueCache) to avoid
redundant field lookups on every keystroke
- Add completion request cancellation via version counter to abandon
stale requests when user types faster than completions resolve
- Add character-based pre-checks before running expensive regex patterns
- Disable Monaco features that add per-keystroke overhead:
- accessibilitySupport, matchBrackets, links, contextmenu
- hover, parameterHints, inlayHints, codeLens, lightbulb
- occurrencesHighlight, selectionHighlight, colorDecorators
- Use requestIdleCallback in onDidChangeModelContent to defer
non-critical work off the main thread
- Remove showSuggestions state update from keystroke path to
reduce Lit component re-renders
* Fix query editor performance issues from code review
- Add cache invalidation to setDynamicResolver to prevent stale results
- Add fast-path includes('.') check before regex in updateSuggestions/insertCompletion
- Precompile whitespace and word boundary regexes used in hot loop
- Fix redundant mouseover re-renders by checking if selectedIndex changed
- Clean up global resize/keydown event listeners in disconnectedCallback
* Simplify filterAndSlice and fix requestIdleCallback fallback
- Revert filterAndSlice to simpler filter().slice(0,5) pattern
- Fix requestIdleCallback fallback - setTimeout doesn't accept options object
* Add widget-integrated alerts feature
Enable creating and managing alerts directly from dashboard widget details:
- Add widget_id, dashboard_id, and recovery threshold columns to query_monitors
- Add Alerts tab to widget expand view with full configuration form
- Render threshold marklines on charts (configurable: always/on_breach/never)
- Show alert status indicator on widget headers (bell icon with colors)
- Add source column to Alerts page showing Widget vs Log Explorer origin
- Sync widget query changes to linked alerts automatically
- Delete alerts when widgets are removed from dashboards
- Support recovery thresholds (hysteresis) to prevent alert flapping
- Populate widget alert status at render time for real-time indicators
* Auto-format code with fourmolu
* Fix widget alerts: clone on duplicate, sync queries, persist status
- Clone alerts when duplicating widgets with new alert ID tied to new widget
- Sync widget query changes to associated alert's logQuery and logQueryAsSql
- Prevent direct query modification for widget-tied alerts
- Add current_status column to track alert state (migration 0026)
- Extract normalizeWidgetId, alertActiveWindow, mkMarkLine helpers
- Use stored current_status in getWidgetAlertStatuses instead of computing
* Auto-format code with fourmolu
* Fix widget alerts: add missing current_status field and lowercase status values
- Add current_status to SQL queries in getAlertsByTeamHandle, getActiveQueryMonitors,
queryMonitorByWidgetId, queryMonitorsByDashboardId
- Fix case sensitivity: use lowercase status values to match SQL constraint
- Remove redundant pass statement, simplify wasAlerting/wasWarning with helper
* Cache current_value to avoid eval() on every dashboard load
- Add current_value column to query_monitors table
- Store evaluated value during background job instead of computing on read
- Replace eval(log_query_as_sql) with cached current_value in getWidgetAlertStatuses
- Remove unnecessary null guard from deleteMonitorsByWidgetIds
- Use foldMap for statusMap construction
* Simplify monitor status logic and extract updateTabBySlug helper
- Refactor monitorStatus to use breached/recovered predicates instead of
duplicated comparison logic for triggerLessThan
- Use currentStatus field directly instead of time-based alerting checks
- Extract updateTabBySlug helper in Dashboards to reduce code duplication
* Auto-format code with fourmolu
* Consolidate widget alert migrations into single file
* Move showThresholdLines from monitor to widget configuration
showThresholdLines is a display setting that belongs in the widget JSON,
not the monitor database table. This change:
- Removes show_threshold_lines column from query_monitors table
- Updates widgetAlertUpsertH to save showThresholdLines to the widget
- Widget already has this field in its JSON schema
* Use DOUBLE PRECISION for threshold and metric values
Support decimal values (e.g., 0.5, 32.5°C) in monitor thresholds:
- Migration: Convert alert_threshold, warning_threshold to DOUBLE PRECISION
- Migration: New columns use DOUBLE PRECISION (recovery thresholds, current_value)
- Haskell: Update all threshold/value fields from Int to Double
- Remove fromIntegral conversions in BackgroundJobs
* Code cleanup: simplify BackgroundJobs and add NOT NULL constraints
- Simplify sum/map to list comprehension: sum [v | Only v <- results]
- Inline wasAlerting/wasWarning in monitorStatus call
- Use void consistently instead of _ <- pattern
- Simplify breached/recovered predicates in monitorStatus
- Use && instead of , in guards for clarity
- Add NOT NULL to current_status and current_value columns
* Auto-format code with fourmolu
* Fix: remove stale show_threshold_lines from SELECT queries
* Fix: rename 'first' variable to avoid shadowing Bifunctor.first
* Refactor: use _select from pg-entity instead of manual column lists
* Refactor: address code review feedback for alerts-in-widgets
- Add MonitorStatus ADT (MSNormal/MSWarning/MSAlerting) replacing string literals
- Simplify applyAlertStatus using fromMaybe/do pattern
- Add warning log when widget alert query parsing fails
- Fix race condition by deleting monitors before dashboard update
- Normalize widgetId once at function entry in dashboardWidgetPutH
* Auto-format code with fourmolu
* Fix: format threshold values with thousand separators
* Refactor: extract formatWithCommas to Utils.hs
* Refactor: extract shared alert UI helpers to Pages/Monitors.hs
- Add thresholdInput_, recoveryInput_, directionSelect_, frequencySelect_, collapsibleSection_
- Update Dashboards.hs widgetAlertConfig_ to use shared helpers
- Update Log.hs alertConfigurationForm_ to use shared helpers
- Net reduction of ~37 lines through DRY refactoring
* Auto-format code with fourmolu
* Style: use surface-raised for alert form in Log.hs
* Add integration tests and doctests for alert monitoring
- Add doctests to monitorStatus for hysteresis logic validation
- Add widget alert cascade deletion test
- Add widget alert query sync test
- Add monitor with recovery thresholds integration test
- Export checkTriggeredQueryMonitors and monitorStatus from BackgroundJobs
- Fix missing AlertUpsertForm fields in MonitorsSpec
* Refactor: extract functions and fix hlint warnings
- Extract syncWidgetAlert, normalizeWidget, updateDashboardWidgets from dashboardWidgetPutH
- Extract evaluateQueryMonitor with error handling for SQL failures
- Fix hlint warnings: use toText, one, and function composition
* Auto-format code with fourmolu
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Update doctest expected outputs to include ::text casts for text operators
(Has, NotHas, Contains, NotContains, StartsWith, NotStartsWith, EndsWith, NotEndsWith)
- Fix ambiguous record update warning in Pages/Api.hs by using explicit record construction
- Add !important to hover/active styles to override library defaults
- Change dropdown position from 'text' to 'input' for proper alignment
- Consolidate hover and active selectors
- Fix span overflow bug by using actual time range (maxEndTime - minStartTime)
instead of max duration for rootVal calculation
- Add getContrastTextColor that returns tinted text colors instead of pure
white/black for softer, more visually pleasing appearance
- Add left padding (ml-1) to span text for better readability
- Simplify buildHierarchy function and fix serviceName type in WaterfallItem
When switching tabs via HTMX, the widget-order-trigger form retained
its original hx-patch URL pointing to the initially loaded tab. This
caused widget reorder operations on other tabs to incorrectly update
the original tab's widget order, making it appear empty.
Added OOB swap in dashboardTabContentGetH to update the form's hx-patch
URL to the current tab whenever a tab switch occurs.
When using contains/has/startswith/endswith operators on columns without
field keys (like `body contains "value"`), cast to ::text to support
JSONB columns. Previously this generated invalid SQL like
`body ILIKE '%value%'` which fails with "operator does not exist: jsonb ~~* text".
Now generates `body::text ILIKE '%value%'` which works for both text and
JSONB columns.
- Add legendSize field to Widget (xs, sm, md, lg) with sm as default
- Fix bug where empty widget reorder patch would wipe all widgets
- Set Log.hs widgets to use xs legend size
- Add support for compound legend positions (top-right, top-left, bottom-right, bottom-left)
- Make legend items smaller: 6x6 indicator, font size 10
- Reduce chart-to-legend gap from 18% to 14%
- Add negative padding to reduce icon-to-text gap
- Update Log Explorer charts to use top-right legend position
* Refactor enums to use deriving via WrappedEnumSC
- Fix WrappedEnumSC FromField to use toPascal/fromSnake instead of T.toTitle
(was breaking multi-word snake_case like "runtime_exception")
- Add FromHttpApiData instance to WrappedEnumSC
- Refactor AnomalyTypes, AnomalyActions, ConversationType, FieldCategoryEnum,
and Permissions to derive ToField/FromField/Display via WrappedEnumSC
- Remove manual instance boilerplate (~100 lines)
- Update RequestMessages.hs to use display instead of fieldCategoryEnumToText
* Auto-format code with fourmolu
* Derive more ToField/FromField instances
- Add WrappedEnumShow wrapper for Show/Read-based enums
- Derive SDKTypes and RequestTypes via WrappedEnumShow
- Derive PGTextArray via newtype deriving
- Derive ProjectS3Bucket via Aeson
* Auto-format code with fourmolu
* Replace Unsafe.read with readMaybe for safe enum parsing
FromField instances now return ConversionFailed error with details
instead of crashing on invalid database values.
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* system logs
* Auto-format code with fourmolu
* Simplify SystemLogs to use existing SeverityLevel type
Remove redundant SystemLogSeverity type and use SeverityLevel
(SLInfo, SLWarn, SLError) from Telemetry module directly.
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Make entire row clickable for "Check for recent data", "Load more", and
"Expand time range" actions instead of just the text link
- Fix row heights to match virtualizer's 28px item size to prevent overlaps
- Add CSS rule to prevent clicks on closed popovers
- Fix chevron-down SVG icon color in dark mode by using fill instead of stroke
- Replace tabindex-based dropdown with popover API to fix click-through bug
- Add cursor-pointer to dropdown trigger button
🤖 Generated with [Claude Code](https://claude.com/claude-code)
The parser uses sepBy which returns [] on parse failure instead of
an error. Now we detect when user provided a non-empty query but
got empty AST, treating this as a parse error and returning 0 results.
Updated the "invalid query syntax" test to expect 0 results instead
of all results, making it consistent with other error handling tests.
Previously, a malformed query like "status_code === \"200\"" would
show an error toast but still return all results (since empty AST
meant no filters). Now it returns 0 results, which is the expected
behavior for invalid queries.
- Download highlight.js v11.11.1 (CSS + core + 11 language packs) to
static/public/assets/deps/highlightjs/
- Update Onboarding.hs to use hashAssetFile for cache-busted local files
- Regenerate workbox service worker to precache highlight.js assets
- Update workbox-cli to v7.0.0
When a dashboard tab requires a variable that isn't set, a modal now
auto-appears prompting the user to select a value. Features:
- Searchable list of options with client-side filtering
- Preserves current tab when selecting (stays on Service Summary, etc.)
- Supports chaining: selecting first required var prompts for next
- Respects dependsOn: child variables only shown after parent is set
- Dismissible via X button, backdrop click, or Escape key
- Works for both tab.requires and variable.required=true
Also fixes template interpolation in Widget table row click handler.
* feat(dashboard): implement htmx lazy-loading for dashboard tabs
- Add path-based tab navigation (/p/{pid}/dashboards/{did}/tab/{slug})
- Only process widgets for the active tab (lazy loading)
- Use htmx to fetch tab content dynamically on tab switch
- Update URL in browser when switching tabs (linkable tab URLs)
- Re-initialize GridStack after htmx swaps new tab content
- Active tab indicator updates via hyperscript on htmx:afterOnLoad
- Redirect legacy query-param based URLs to path-based format
* Auto-format code with fourmolu
* feat(dashboard): add active tab name to page breadcrumbs
- Add pageTitleSuffix field to BWConfig for breadcrumb extensions
- Show current tab name in breadcrumbs (Dashboards > Dashboard Title > Tab Name)
- Use htmx out-of-band swap to update breadcrumb when switching tabs
- Breadcrumb updates automatically when navigating between tabs via htmx
* Auto-format code with fourmolu
* feat(dashboard): add ability to rename tabs via breadcrumb click
- Add pageTitleSuffixModalId to BWConfig for tab rename modals
- Make tab name in breadcrumb clickable to open rename modal
- Add TabRenameForm and TabRenameRes types for form handling
- Add dashboardTabRenamePatchH handler to update tab names
- Add tab rename modal to dashboard page
- Redirect to new tab slug URL after rename
- Add "Rename tab" option to context menu dropdown
* refactor(dashboard): extract common processing logic and address code review
- Extract processConstantsAndExtendParams helper to reduce duplication
- Extract mkWidgetProcessor helper for widget processing with dashboard ID
- Add activeTabSlugKey constant to replace magic string "activeTabSlug"
- Fix potential race condition: use requestAnimationFrame instead of setTimeout
- Mark unused dashVM as _dashVM in dashboardTabContentGetH
* Auto-format code with fourmolu
* refactor(dashboard): simplify code and remove redundancy
- Remove redundant tabNameToSlug wrapper, use slugify directly
- Simplify getDefaultTabSlug using fmap and viaNonEmpty
- Extract breadcrumbSuffixOob_ helper for OOB breadcrumb swaps
- Remove unnecessary main widgets processing in dashboardTabGetH
(tab-based dashboards don't need dash.widgets processed)
* fix(dashboard): remove redundant lambda in mkWidgetProcessor (hlint)
* refactor(dashboard): improve code succinctness per review
- Use point-free style in findTabBySlug
- Simplify processConstantsAndExtendParams with <&>
- Make mkWidgetProcessor point-free with fmap and lens
- Use lens ix for tab name update instead of zipWith
- Extract queryStringFrom helper for query string construction
* fix: resolve type errors and remove SET TRANSACTION warning
- Fix type signatures for processConstantsAndExtendParams and mkWidgetProcessor to use ATAuthCtx
- Fix ambiguous record update using lens syntax
- Make faSprite_ and breadcrumbSuffixOob_ polymorphic for ToHtml instance
- Remove unnecessary SET TRANSACTION READ ONLY that caused warnings
* parallel widget processing in dashboards. and adding consts to the query params
* Auto-format code with fourmolu
* fix(dashboard): add tab loading indicators and fix OOB breadcrumb on initial load
- Add per-tab loading spinner indicators
- Only include OOB breadcrumb swap for HTMX partial loads, not full page loads
* perf(dashboard): optimize tab switching performance
- Replace widget retry loops with widgetDepsReady flag pattern
- Remove duplicate tooltip initialization (afterSwap + afterSettle → afterSettle only)
- Add HTMX view transitions for smoother tab swaps
- Use htmx:afterSettle instead of afterSwap for GridStack init
* fix: address code review feedback
- Add READ ONLY transaction wrapper for user SQL queries (security)
- Fix tab rename to use OOB swap + history.replaceState instead of HX-Redirect
* revert: remove SQL transaction and tab rename changes
* fix: rename children to childWidgets to avoid shadowing lens import
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* Improve anomaly detail page layout and add AI chat
- Move action buttons (acknowledge, archive) to navbar as page actions
- Remove surface-raised from header, add more breathing room
- Update button styles to match navbar patterns (btn-sm)
- Add time stat boxes showing first/last seen with compact formatting
- Add AI chat functionality for investigating anomalies
- Add side-chevron-left icon (without box) for collapse toggle
- Use new icon for trace panel collapse button
* Auto-format code with fourmolu
* Refactor AI chat to use agentic tools and DB-persisted history
- Extend AgenticConfig with conversationId and conversationType fields
- Add runAgenticChatWithHistory for DB-backed conversations
- Refactor Anomalies.hs to use agentic query system (access to 8 tools)
- Add Slack/Discord thread persistence in DB instead of repeated API calls
- Add deterministic UUID5 helpers for thread conversation IDs
- One-time migration pattern: first thread access migrates from external API
- Log Explorer remains stateless for low latency
* Auto-format code with fourmolu
* Fix security issues and improve AI chat persistence
Security fixes:
- Use executeSecuredQuery for dashboard constants/variables SQL
- Add project_id filtering to prevent cross-project data access
AI chat improvements:
- Save user message before AI call for complete history
- Persist assistant response on error and parse failure paths
- Fix race condition in getOrCreateConversation (use listToMaybe)
Code style (CLAUDE.md):
- Remove unnecessary $ before lambdas in Anomalies.hs
- Clean up unused imports in Dashboards.hs
* Auto-format code with fourmolu
* Refactor time display and error handling for cleaner code
- Add abbreviateUnit helper with LambdaCase for cleaner word mapping
- Simplify compactTimeAgo using word-based abbreviation
- Refactor timeStatBox_ to use pattern matching on T.words
- Replace fromMaybe.error with explicit pattern match in getOrCreateConversation
* Auto-format code with fourmolu
* Fix hlint warnings in Anomalies and AI modules
- Replace T.unwords/T.words/T.unlines with Relude equivalents
- Simplify lambdas to operator sections
- Replace pure () with pass
* Optimize AI and anomaly code with better data structures
- Use Map for O(1) time unit abbreviation lookup in Anomalies.hs
- Extract dangerous SQL patterns to top-level constant with additional
security patterns (copy, execute, explain, set)
- Replace V.toList conversions with pure Vector operations using vIntercalate
- Use V.take instead of take after V.toList for efficiency
- Reorder migration files for correct dependency order
* Auto-format code with fourmolu
* Optimize AI and anomaly code with better data structures
- Use Map for O(1) time unit abbreviation lookup in Anomalies.hs
- Extract dangerous SQL patterns to top-level constant with additional
security patterns (copy, execute, explain, set)
- Replace V.toList conversions with pure Vector operations using vIntercalate
- Use V.take instead of take after V.toList for efficiency
- Reorder migration files for correct dependency order
* Auto-format code with fourmolu
* Use chunksOf and parseMaybe for cleaner code in Anomalies.hs
- Replace manual groupMessages recursion with chunksOf from extra
- Use parseMaybe from aeson instead of case on fromJSON result
* Fix security and code quality issues
- Fix SQL injection risk: wrap query in transaction with READ ONLY mode,
add comment markers (--,/*,*/) to dangerous patterns validation
- Replace unsafe error call with safe fromMaybe in getOrCreateConversation
- Extract textToConversationId helper for cleaner UUID V5 generation
- Remove Control.Lens import, use record update syntax instead
- Add manage_updated_at trigger migration for ai_conversations table
- Update parser test expectations to match current output format
* Auto-format code with fourmolu
* Fix bugs and improve code quality in anomaly/issues modules
- Fix race condition in getOrCreateConversation: fail explicitly if
RETURNING clause doesn't return a row instead of silent fallback
- Replace Map lookup with pattern matching for time unit abbreviation
- Fix V.take order: take before map for better performance
- Make chat history pairing more robust with explicit recursion
- Add doctests for abbreviateUnit function
- Remove obvious comments, keep only meaningful ones
- Add updated_at trigger for ai_conversations table
* Auto-format code with fourmolu
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* feat: Add agentic tool calling for AI query generation
Introduces an agentic LLM module that allows the AI to run exploratory
queries before generating the final KQL query. This improves accuracy
when users ask about specific data that the LLM may not know about.
Key changes:
- New Pkg.AI.Agentic module with tool definitions and execution
- Tools: GetFieldValues, GetServices, CountQuery, SampleLogs
- Context-aware mode selection (agentic vs fast mode)
- Web explorer uses agentic mode for complex queries
- Bot handlers continue using fast mode for lower latency
The system intelligently decides when to use tools vs direct response
based on query complexity and context (web UI vs bots).
* Auto-format code with fourmolu
* feat: Use FastMode by default with precomputed facet context
Changes:
- WebExplorer now uses FastMode by default for faster responses
- Pre-include facet context (services, status codes, etc.) in prompts
- Add GetFacets tool for agentic mode when deeper exploration needed
- Fetch precomputed facets in aiSearchH and pass to agentic config
- LLM now has real project data context without needing tool calls
This makes queries faster while still providing accurate results by
giving the LLM actual field values (services, methods, status codes)
from the precomputed facets stored in the database.
* Auto-format code with fourmolu
* refactor: Use KQL instead of raw SQL for agentic tool execution
- Update executeGetFieldValues to use KQL summarize query
- Update executeCountQuery to use selectLogTable for count queries
- Update executeGetServices to use KQL summarize by resource.service.name
- Update executeSampleLogs to use selectLogTable with KQL parsing
- Add helper functions for formatting query results
- Add Time and Log effects to tool execution constraints
- Remove unused SQL query building helpers
This change ensures all tool queries go through the KQL infrastructure,
enabling caching and optimizations built into the KQL workflow.
* Auto-format code with fourmolu
* refactor: Add RunQuery tool and apply code review improvements
Key changes:
- Add RunQuery tool that supports both KQL (preferred) and raw SQL
for complex queries that KQL cannot express
- Add security validation for SQL queries (SELECT only)
- Extract constants (maxFieldValues, maxSampleLogs, etc.)
- Add helper functions (successResult, errorResult, getTextArg, etc.)
to reduce code duplication in tool execution
- Apply LambdaCase extension for cleaner pattern matching
- Use mapMaybe instead of filter + map for better efficiency
- Extract keyFacetFields as a constant
- Reduce overall code complexity from ~289 lines to ~233 lines
The LLM can now choose between KQL and SQL based on query complexity,
with strong preference for KQL due to its caching and optimizations.
* Auto-format code with fourmolu
* security: Inject project_id filter into SQL queries
- Add injectProjectFilter to automatically add project_id filter
- Handles queries with and without existing WHERE clauses
- Wraps existing WHERE conditions in parentheses for correct precedence
- Updates tool description to inform LLM that project_id is auto-injected
This prevents any cross-project data access when using raw SQL queries.
* security: Use parameterized CTE for SQL query project filtering
Instead of string manipulation to inject project_id (which was vulnerable
to SQL injection), use a CTE wrapper with parameterized queries:
- Add executeSecuredQuery that wraps user queries in a CTE
- The CTE shadows otel_logs_and_spans with a pre-filtered version
- project_id is passed as a parameterized query value
- Remove unsafe injectProjectFilter and findInsertPoint functions
- Clean up unused imports
This approach is robust because:
1. project_id cannot be injected - it's a query parameter
2. Any query referencing otel_logs_and_spans gets the filtered version
3. No brittle string parsing of WHERE clauses
* Auto-format code with fourmolu
* fix: Add missing runKqlQuery helper and reduce tool duplication
- Add runKqlQuery helper function that was removed by linter
- Refactor executeCountQuery to use runKqlQuery helper
- Refactor executeGetServices to use runKqlQuery helper
- Refactor executeSampleLogs to use runKqlQuery helper
- Remove duplicate/leftover code from partial merge
- Net reduction of ~114 lines through DRY refactoring
* perf: Replace CTE with direct WHERE injection for project_id
CTEs can cause performance issues on large tables because PostgreSQL
may materialize the entire CTE before applying user predicates.
New approach:
- Inject "project_id = ?" directly into the WHERE clause
- project_id value is still parameterized (safe from SQL injection)
- limit is also parameterized
- Query structure manipulation is for WHERE placement only, not values
- PostgreSQL can now use indexes on project_id efficiently
* security: Block UNION/EXCEPT/INTERSECT to prevent project_id filter bypass
- Add UNION/EXCEPT/INTERSECT to blocked SQL patterns since they could
allow querying data from other projects in the second query
- Improve keyword detection with word boundary checking (prefix, space,
newline, tab) instead of just substring matching
- Provide specific error message for bypass attempts vs DDL/DML operations
* Auto-format code with fourmolu
* Refactor Agentic.hs for code succinctness and add unit tests
- Group magic constants into ToolLimits config record
- Simplify mode selection logic (~25 lines reduced)
- Simplify SQL validation and runSqlQuery (~25 lines reduced)
- Simplify format functions using ViewPatterns
- Use bimap/either for cleaner error handling
- Sanitize error messages (don't expose internal errors)
- Add unit tests for suggestAgenticMode, tool parsing, SQL validation
- Fix pre-existing build errors in RequestDumps.hs (try import, minimum)
Total: ~80 lines reduced (663 -> 581 LOC, 12% reduction)
* Auto-format code with fourmolu
* Fix case-sensitivity bug in executeSecuredQuery
The function was using lowerQuery for detection but applying
positions to userQuery. While this worked for ASCII (toLower
preserves length), it was fragile. Refactored to use a
breakOnCI helper that handles case-insensitivity consistently
and returns splits of the original text.
* Fix hlint warnings in Agentic.hs
* Refactor Agentic.hs to use langchain-hs native tool calling
- Use langchain-hs LLM.chat API with OpenAI tool definitions instead of
custom text-based tool protocol
- Define tools using OAITool.Tool format passed to the chat API
- Implement agentic loop that handles tool calls via LLM.toolCalls
- Maintain conversation history using langchain-hs ChatHistory
- Add GetSchema tool for field discovery
- Update cabal.project to latest langchain-hs commit
- Simplify tests to match updated exports
* Auto-format code with fourmolu
* Fix hlint warnings in Agentic.hs
* Improve code quality in Agentic.hs
- Remove fake SQL validation tests that tested non-existent functions
- Add maxDisplayRows and maxBodyPreview to ToolLimits (removes magic numbers)
- Add getTextArg helper to reduce duplicate pattern matching
- Use bool function for jsonToText Bool case
- Pass limits to formatSampleLogs and formatQueryResults
* Improve code quality and reduce boilerplate in Agentic.hs
- Add module-level Haddock documentation explaining the agentic system
- Remove unused AgenticResponse type from exports
- Add getLimitArg helper to consolidate repeated limit calculation pattern
- Add mkSimpleTool and mkProp helpers to reduce tool definition boilerplate
- Add toolError helper for consistent error messages with context
- Rename getIntFromArgs to getIntArg for consistency with getTextArg
* Auto-format code with fourmolu
* Fix UTF-8 bug in executeSecuredQuery's breakOnCIKeyword
The previous implementation calculated text length on the lowercase
version but applied the split to the original text. This would fail
for Unicode characters that change length when lowercased (e.g.,
German ẞ → ss, Turkish İ → i).
Fixed by using character-by-character search with proper position
tracking on the original string, avoiding the length mismatch issue.
* Unify AI system with always-on tool calling
- Merge Pkg.AI.Agentic into Pkg.AI for single AI module
- Remove FastMode/AgenticMode distinction - always use tool calling
- Remove suggestAgenticMode brittle keyword matching - LLM decides when to use tools
- Add facet context fetching to all bots (Discord, Slack, WhatsApp)
- All channels now have unified behavior with full project awareness
- Simplify AgenticConfig by removing mode field
- Increase maxIterations to 5 for better tool exploration
* Auto-format code with fourmolu
* Refactor bot modules to share AI query processing logic
Extract common functionality into Pages.Bots.Utils:
- AIQueryResult data type for unified response handling
- processAIQuery for shared facet fetching and AI execution
- formatThreadsPrompt for common thread context formatting
Reduces duplication across Discord, Slack, and WhatsApp modules.
* Auto-format code with fourmolu
* Use langchain-hs TokenBufferMemory for thread context
Replace manual formatThreadsPrompt with TokenBufferMemory to provide
token-aware thread context management. This prevents prompt overflow
by trimming messages to 3000 tokens while preserving system messages.
* Auto-format code with fourmolu
* Improve AI tool calling with SQL support and code cleanup
- Add run_sql_query tool for complex queries not expressible in KQL
- Make helper functions more succinct using LambdaCase
- Use V.mapMaybe instead of mapMaybe . V.toList for better performance
- Preserve diagnostic info in error handling (show parse/execution errors)
* Use TokenBufferMemory consistently for chat history management
- Add addMessagesToMemory helper in AI.hs using TokenBufferMemory
- Replace manual message concatenation in runAgenticLoop with memory-based approach
- Simplify roleToText in Utils.hs to use show
* Simplify code patterns and reduce redundancy
- Simplify executeSecuredQuery using subquery wrapper (34 -> 9 lines)
- Use Semigroup for NonEmpty concatenation in addMessagesToMemory
- Replace verbose case pattern with <&> in processAIQuery
- Use tuple destructuring for Maybe handling
* Fix hlint warning: use nonEmpty from Relude
🤖 Generated with [Claude Code](https://claude.com/claude-code)
* Remove redundant Data.List.NonEmpty import
🤖 Generated with [Claude Code](https://claude.com/claude-code)
* Remove unused TimeSpec import
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
The gridstack library update to v12.4.1 replaced the custom APItoolkit
resize icon (dotted grid pattern) with the default arrow icon. This adds
CSS overrides in tailwind.css to restore:
- Custom dotted grid SVG resize handle (#94A3B8 color)
- Smaller handle size (14x14 vs 20x20)
- Custom placeholder styling with "drop here" text
Co-authored-by: Claude <noreply@anthropic.com>
* Add KQL functions for _overview.yaml SQL migration
Implement P0 and P1 KQL features needed to migrate _overview.yaml dashboard
queries from SQL to KQL:
P0 Features:
- countif(condition): COUNT(*) FILTER (WHERE condition) for error rate calculations
- dcount(field): COUNT(DISTINCT field) for service/resource counts
P1 Features:
- coalesce(field, default): COALESCE for null handling
- strcat(field1, separator, field2): CONCAT for string concatenation
- iff(condition, then, else): CASE WHEN for simple conditionals
- case(cond1, val1, ..., default): Multi-branch CASE for categorization
SQL generation for each function:
- countif(status == "ERROR") → COUNT(*) FILTER (WHERE status = 'ERROR')
- dcount(service_name) → COUNT(DISTINCT service_name)
- coalesce(method, "unknown") → COALESCE(method, 'unknown')
- strcat(method, " ", path) → CONCAT(method, ' ', path)
- iff(x > 0, a, b) → CASE WHEN x > 0 THEN a ELSE b END
- case(x >= 500, e, x >= 400, w, s) → CASE WHEN x >= 500 THEN e WHEN x >= 400 THEN w ELSE s END
Example target KQL for _overview.yaml:
kind == "server" AND duration != null
| summarize
error_rate = countif(status_code == "ERROR") * 100.0 / count(),
p95_latency = percentile(duration / 1e6, 95)
by resource.service.name
* Auto-format code with fourmolu
* Refactor KQL parsers to use applicative style
- Add pSimpleAgg helper for common function(subject) pattern
- Convert pCountIf, pCoalesce, pStrcat, pIff to applicative style
- Simplify pCase with inline tuple construction
- Remove redundant pDCount (now handled by pSimpleAgg)
- Net reduction of ~50 lines while maintaining same functionality
* Auto-format code with fourmolu
* Align KQL functions with Microsoft KQL dialect
Update function signatures to match official Microsoft KQL syntax:
- dcount(expr [, accuracy]): Add optional accuracy parameter (0-4)
for HyperLogLog estimation precision
- coalesce(arg1, arg2, [arg3,...]): Now variadic (2-64 args)
Returns first non-null expression
- strcat(arg1, arg2, [arg3,...]): Now variadic (1-64 args)
Concatenates any scalar expressions
- iff(condition, then, else): Now accepts any Values for then/else
Not limited to just field references
- case(pred1, val1, [pred2, val2, ...] else): Values are now any scalar
Matches Microsoft KQL multi-branch conditional syntax
Added pScalarExpr helper to parse either literal values or field
references, enabling flexible value parsing in scalar functions.
Sources:
- https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/dcount-aggfunction
- https://learn.microsoft.com/en-us/kusto/query/coalesce-function
- https://learn.microsoft.com/en-us/kusto/query/strcat-function
- https://learn.microsoft.com/en-us/kusto/query/iff-function
- https://learn.microsoft.com/en-us/kusto/query/case-function
* Auto-format code with fourmolu
* Refactor KQL parsers for DRY and fix backtracking
Parser improvements:
- Extract `comma` helper for repeated `string "," *> space` pattern
- Add `pVariadicAgg` helper for variadic functions (coalesce, strcat)
- Convert pDCount to applicative style (7 lines -> 5 lines)
- Simplify pCoalesce and pStrcat to one-liners using pVariadicAgg
- Use `comma` consistently across all parsers
Bug fix:
- Add `try` to Count parser to prevent partial consumption breaking
backtracking to countif/dcount parsers
- Reorder dcount before count in parser choice list
Code reduction: ~30 lines eliminated while improving consistency
* Auto-format code with fourmolu
* Refactor KQL parser for succinctness and convert percentile queries to KQL
Parser improvements:
- Add withoutAlias helper to eliminate repetitive <*> pure Nothing
- Add commaSep/commaSep1 for cleaner comma-separated parsing
- Refactor pPercentilesMulti/pPercentileSingle to applicative style
- Fix pScalarExpr to use toQText instead of display (preserves KQL notation)
- Extract pCount parser with cleaner parentheses handling
- Use foldMap for CASE WHEN display, simplify Plain display
Dashboard improvements:
- Convert Request Latency Percentiles widget to KQL (was 25 lines SQL)
- Convert Service Latency Percentiles widget to KQL (was 25 lines SQL)
- Both now use: summarize percentiles(duration, 50, 75, 90, 95, 99) by bin_auto(timestamp)
* Auto-format code with fourmolu
* Improve tests with full SQL assertions and fix KQL dot notation
Tests:
- Update countif, dcount, coalesce, strcat, iff, case tests to assert
full generated SQL queries instead of partial string checks
- Add full SQL assertion for percentiles LATERAL unnest query
- Update where clause combination test with full SQL assertion
Dashboard KQL:
- Fix KQL queries to use "." separator (resource.service.name)
instead of "___" which is only for raw SQL
- Add order by timestamp desc for proper ordering
* Fix ambiguous 'many' import and apply HLint suggestion
- Hide 'many' from Relude to resolve ambiguity with Text.Megaparsec
- Replace case with maybe in extractPercentilesInfo per HLint
* Refactor KQL parser and add dcount accuracy validation
- Remove redundant commaSep/commaSep1 wrappers, use sepBy directly
- Use ViewPatterns for cleaner extractPercentilesInfo pattern matching
- Add validation for dcount accuracy bounds (0-4 per KQL spec)
- Add comment explaining dcount accuracy is ignored in PostgreSQL
- Add edge case tests for variadic functions, nested calls, field paths
* Fix UnexpectedNull error for hashes field in OtelLogsAndSpans
Use COALESCE(hashes, '{}') in SQL queries to handle NULL values
in the hashes column, preventing PGArray parsing failures.
* Fix UnexpectedNull error for start_time field in OtelLogsAndSpans
Use COALESCE(start_time, timestamp) in SQL queries to handle NULL values
in start_time column. Logs may not have start_time set, falling back to
the main timestamp field.
* Fix field references in coalesce/strcat SQL generation
- Add Field constructor to Values type for unquoted field references
- Update pScalarExpr to use Field for subject references
- Remove catch-all from pValues that was consuming commas
- Add coalesce validation requiring 2+ arguments
- Clean up Stats.hs exports
- Update test expectations for correct SQL output
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
* feat(dashboards): add query constants for reusable SQL/KQL results
Add dashboard constants feature that allows defining SQL/KQL queries
whose results are executed once and made available to all widgets as
{{const-<key>}} placeholders that expand to subqueries.
This improves:
- Performance: CTEs like top_resources are executed once per dashboard load
instead of duplicated in every widget query
- Maintainability: Query logic is defined once in the constants section
and referenced wherever needed
Changes:
- Add Constant type with key, sql, query, description, and result fields
- Add constants field to Dashboard type
- Add processConstant function to execute constant queries
- Add constantToCTE helper to convert results to VALUES subquery
- Update _overview.yaml with top_resources constant example
* refactor(dashboards): use simple list format for constants instead of CTEs
Change constantToCTE to constantToList to produce a simpler format:
- Before: (SELECT * FROM (VALUES ('a'), ('b')) AS t(name))
- After: ('a', 'b')
This is:
- Simpler to implement and understand
- Better performance (no subquery parsing)
- Works better with KQL queries
- Queries use IN clauses instead of JOINs
* feat(dashboards): add KQL query support for constants
Constants can now use either SQL or KQL queries:
- sql: Direct SQL query (takes precedence)
- query: KQL query (parsed and executed via Charts.queryMetrics)
Example YAML:
constants:
- key: top_services
query: summarize count() by resource.service.name | top 10 by count_
* Auto-format code with fourmolu
* refactor(dashboards): address code review feedback
- Replace fold with fromMaybe [] for clarity
- Fix unsafe head with pattern matching: [escapeValue v | (v : _) <- rows]
- Rename constantToList to constantToSQLList for clarity
* fix(dashboards): correct Constant type JSON deriving clause
Wrap DAE.CamelToSnake in DAE.FieldLabelModifier to match the pattern
used by other types like Variable.
* fix(dashboards): address code review feedback for constants
- Fix NULL issue in constantToSQLList: use empty subquery
(SELECT NULL::text WHERE FALSE) instead of (NULL) to avoid
SQL three-valued logic issues with IN clauses
- Add logging for failed constant SQL/KQL queries to aid debugging
* Auto-format code with fourmolu
* refactor(dashboards): improve constants code quality
- Add execution timing with Log.timeAction for constant queries
- Log debug message on successful query completion with duration
- Use LambdaCase for cleaner constantToSQLList pattern matching
- Use bimap composition for more idiomatic constantsToMap
- Document that empty inner lists are intentionally skipped
* refactor(dashboards): simplify constants implementation
- Extract runQuery helper to deduplicate SQL/KQL error handling
- Use where clause instead of let...in for constantToSQLList
- Eliminate intermediate constantsMap allocation by building
allParamsWithConstants directly from processedConstants
- Remove unused constantsToMap function
- Use traverse instead of forM for more idiomatic style
Reduces code by 18 lines while improving clarity.
* refactor(dashboards): extract common logic and use functional patterns
- Extract replaceVariableFields using HasField' constraint from
generic-lens to deduplicate replaceQueryVariables/replaceConstantVariables
- Simplify runQuery helper using either with $> operator
- Both changes improve code reuse and reduce duplication
* Auto-format code with fourmolu
* fix(dashboards): resolve ambiguous id reference
Use identity instead of id to avoid conflict with User.id field.
* fix(dashboards): use explicit functions to avoid overlapping instances
Revert generic HasField' approach - explicit functions are simpler
and avoid type class resolution issues.
* Auto-format code with fourmolu
* fix(dashboards): substitute constant placeholders in widget queries
Replace {{const-*}} placeholders in widget sql and query fields during
server-side processing. Previously, constants were processed but not
substituted into widget queries before rendering, causing the frontend
to send raw placeholders to /chart_data which resulted in SQL syntax
errors like "WHEN name IN THEN".
* fix(dashboards): rename shadowed variables to avoid -Werror
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Update @vitest/browser from 3.1.4 to 4.0.16 to resolve peer dependency
conflict with vitest 4.0.16. Both packages must be on the same major version.
Co-authored-by: Claude <noreply@anthropic.com>
* Add KQL percentiles() function for latency percentile queries
- Add Percentiles aggregation function to Stats.hs that generates
p50, p75, p90, p95, p99 percentiles with optional divisor for
unit conversion (e.g., percentiles(duration, 1e6) for ns to ms)
- Update Parser.hs to generate LATERAL unnest SQL pattern when
percentiles() is used in summarize queries
- Update Log.hs to use KQL query instead of raw SQL for latency
percentiles widget
- Update dashboard YAML files to use KQL percentile queries:
- opentelemetry-tracing.yaml
- endpoint-stats.yaml
- mysql.yaml
- postgresql.yaml
- apache.yaml
- cloudflare.yaml
This reduces SQL boilerplate significantly and makes percentile
queries much more readable and maintainable.
* Auto-format code with fourmolu
* Update KQL percentile/percentiles to match Microsoft KQL syntax
Update the implementation to match Microsoft KQL semantics:
- percentile(expr, percentile) - single percentile value
- percentiles(expr, p1, p2, p3, ...) - multiple custom percentile values
Key changes:
- Parser now accepts explicit percentile values (e.g., 50, 75, 90, 95)
- If last value > 100, it's treated as a divisor for unit conversion
- SQL generation dynamically builds ARRAY based on specified percentiles
- Dashboard queries now use explicit percentile values
Examples:
percentile(duration, 95) - single 95th percentile
percentiles(duration, 50, 75, 90, 95) - multiple percentiles
percentiles(duration, 50, 75, 90, 95, 1e6) - with divisor for ns->ms
* Match Microsoft KQL syntax for percentile/percentiles functions
Update the implementation to exactly match Microsoft KQL semantics:
- percentile(expr, percentile) - single percentile value
- percentiles(expr, p1, p2, ...) - multiple percentile values
Now supports expressions in the first argument for unit conversion:
percentiles(duration / 1e6, 50, 75, 90, 95)
Key changes:
- Add SubjectExpr type to support field / number and field * number
- Parser accepts expression as first arg, then percentile values
- No more magic divisor detection - use explicit expressions
- Dashboard queries updated to use proper expression syntax
Examples matching Microsoft KQL:
percentile(duration, 95)
percentiles(duration, 50, 75, 90, 95)
percentiles(duration / 1e6, 50, 75, 90, 95, 99)
* Auto-format code with fourmolu
* Fix chart widget query updates and combine where clauses
- Fix event dispatch in query-editor.ts: dispatch update-query to window
so chart widgets receive query change events
- Fix Parser.hs: combine multiple where clauses with AND instead of
replacing them, so filters like "service == X | duration != null"
correctly apply both conditions
- Add combineWhereClause helper function
- Add unit test for combined where clause behavior
- Refactor Stats.hs: extract setAlias helper for namedAggregation
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
The sessionAndProject function had a bug where ONBOARDING projects
were incorrectly redirected to /?missingProjectPermission even when
the user had valid access.
The issue was in the pattern matching logic:
1. Pattern `Just p | p.paymentPlan /= "ONBOARDING"` correctly skipped
ONBOARDING projects
2. But the fallthrough `_ | pid == nil || isSudo` only allowed nil
UUIDs or sudo users to fetch from DB
3. Regular users with ONBOARDING projects fell through to the redirect
This fix adds an explicit pattern for ONBOARDING projects (Just _)
that fetches fresh data from the database, matching the original
intent described in the comments.
Co-authored-by: Claude <noreply@anthropic.com>
Update 7 test cases to include `count(*) OVER() as _total_count`
in expected SQL output, matching the change from commit 7b9515c
that combined data and count queries into one.
Co-authored-by: Claude <noreply@anthropic.com>