The only tool you need to know what is happening and how to fix it.
Go alertingexception-trackerissue-trackinglogsmetricsmonitoringobservability-platformopen-telemetryotelsession-replaytelemetrytraces
A lot of sqlite work and fixing otel trace ids
ddux82e89efc687c
diff --git a/CLAUDE.md b/CLAUDE.mdindex 90a28deb..31e7e107 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -665,6 +665,50 @@ func (c *ReportController) Report(ctx *gin.Context) {- **ClickHouse**: High-volume append-only telemetry with time-series aggregations, batch inserts only (transactions, exceptions, metric points, spans, tasks, sessions)- Rule of thumb: "Will this data be updated after creation?" → PostgreSQL. "Is this immutable, time-stamped, high-volume data queried with aggregations?" → ClickHouse.+#### SQLite Dual-Database Architecture (self-hosted mode)++In SQLite mode (`DB_TYPE=sqlite`), the backend uses **two separate SQLite databases** mirroring the PostgreSQL/ClickHouse split:++| Database | Variable | File | Purpose | Transactions |+|----------|----------|------|---------|-------------|+| **Main DB** | `db.DB` | `traceway.db` | PostgreSQL replacement — relational/config data | Yes (`middleware.Transactional`, `db.ExecuteTransaction`) |+| **Telemetry DB** | `db.TelemetryDB` | `traceway_telemetry.db` | ClickHouse replacement — append-only telemetry | No — direct inserts without transactions |++**Main DB tables** (`db.DB` — transactional, uses lit with `*sql.Tx`):+- `users`, `organizations`, `organization_users`, `projects`, `invitations`+- `source_maps`, `metric_registry`, `widget_groups`, `widget_group_widgets`+- `notification_channels`, `notification_rules`, `notification_history`++**Telemetry DB tables** (`db.TelemetryDB` — non-transactional, uses lit with `db.TelemetryDB` directly):+- `endpoints`, `tasks`, `exception_stack_traces`, `spans`, `metric_points`+- `session_recordings`, `archived_exceptions`, `slow_endpoints`, `fired_notifications`++**How to access each database in repository code:**++```go+// Main DB (PostgreSQL replacement) — use transactions via middleware or ExecuteTransaction+// Repositories receive *sql.Tx from middleware.GetTx(ctx) or db.ExecuteTransaction+user, err := lit.SelectSingleNamed[models.User](tx, "SELECT ... FROM users WHERE ...", lit.P{...})++// Telemetry DB (ClickHouse replacement) — use db.TelemetryDB directly, no transactions+results, err := lit.SelectNamed[endpointRow](db.TelemetryDB, "SELECT ... FROM endpoints WHERE ...", lit.P{...})++// Telemetry inserts — no transaction wrapping+for _, item := range items {+ row := modelToRow(item)+ lit.InsertExistingUuid(db.TelemetryDB, &row)+}+```++**Migrations** are split into two directories:+- `backend/app/migrations/sqlite/` — runs on `db.DB` (main)+- `backend/app/migrations/sqlite_telemetry/` — runs on `db.TelemetryDB` (telemetry)++**SQLite-specific type helpers** (`backend/app/repositories/sqlite_types.go`):+- `SQLiteTime` — implements `sql.Scanner`/`driver.Valuer` for `time.Time` ↔ SQLite TEXT+- `SQLiteJSONMap` — implements `sql.Scanner`/`driver.Valuer` for `map[string]string` ↔ SQLite JSON TEXT+- Row types (e.g., `endpointRow`, `taskRow`) wrap domain models with these types for lit compatibility+#### Organization Roles| Role | Description ||------|-------------|diff --git a/backend/app/controllers/distributed_trace.controller.go b/backend/app/controllers/distributed_trace.controller.goindex 7d32e54d..472b09bc 100644--- a/backend/app/controllers/distributed_trace.controller.go+++ b/backend/app/controllers/distributed_trace.controller.go@@ -98,6 +98,14 @@ func (d distributedTraceController) GetDistributedTrace(c *gin.Context) {}}+ matchedIds := make(map[uuid.UUID]bool)+ for _, ep := range endpoints {+ matchedIds[ep.Id] = true+ }+ for _, t := range tasks {+ matchedIds[t.Id] = true+ }+var nodes []DistributedTraceNodefor _, ep := range endpoints {@@ -124,6 +132,23 @@ func (d distributedTraceController) GetDistributedTrace(c *gin.Context) {nodes = append(nodes, node)}+ for _, exc := range exceptions {+ if exc.TraceId != nil && matchedIds[*exc.TraceId] {+ continue+ }+ nodes = append(nodes, DistributedTraceNode{+ ProjectId: exc.ProjectId,+ ProjectName: projectNameMap[exc.ProjectId],+ TraceType: "exception",+ Spans: []models.Span{},+ Exception: &EndpointExceptionInfo{+ ExceptionHash: exc.ExceptionHash,+ StackTrace: exc.StackTrace,+ RecordedAt: exc.RecordedAt.Format("2006-01-02T15:04:05Z07:00"),+ },+ })+ }+if nodes == nil {nodes = []DistributedTraceNode{}}diff --git a/backend/app/controllers/endpoint_detail.controller.go b/backend/app/controllers/endpoint_detail.controller.goindex 06e1b307..249e6041 100644--- a/backend/app/controllers/endpoint_detail.controller.go+++ b/backend/app/controllers/endpoint_detail.controller.go@@ -54,6 +54,10 @@ func (t endpointDetailController) GetEndpointDetail(c *gin.Context) {endpoint, err := repositories.EndpointRepository.FindById(c, projectId, endpointId)span.End()if err != nil {+ c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("error loading endpoint: %w", err))+ return+ }+ if endpoint == nil {c.JSON(http.StatusNotFound, gin.H{"error": "Endpoint not found"})return}diff --git a/backend/app/controllers/otelcontrollers/trace_converter.go b/backend/app/controllers/otelcontrollers/trace_converter.goindex 79ce5226..0011e0d8 100644--- a/backend/app/controllers/otelcontrollers/trace_converter.go+++ b/backend/app/controllers/otelcontrollers/trace_converter.go@@ -41,15 +41,13 @@ func convertTraces(projectId uuid.UUID, req *coltracepb.ExportTraceServiceRequesisRoot := len(span.ParentSpanId) == 0spanIdStr := string(span.SpanId)- traceId := otelTraceIDToUUID(span.TraceId)+ var traceId uuid.UUID- if !isRoot {- if foundTraceId, ok := spanToTraceId[string(span.ParentSpanId)]; ok {- traceId = foundTraceId- }- }-- if traceId == uuid.Nil {+ if isRoot {+ traceId = uuid.New()+ } else if foundTraceId, ok := spanToTraceId[string(span.ParentSpanId)]; ok {+ traceId = foundTraceId+ } else {traceId = uuid.New()}diff --git a/backend/app/db/db.go b/backend/app/db/db.goindex ab0de089..d333bdb7 100644--- a/backend/app/db/db.go+++ b/backend/app/db/db.go@@ -1,6 +1,7 @@package dbimport (+ "context""database/sql""fmt"@@ -8,24 +9,16 @@ import (_ "github.com/lib/pq""github.com/tracewayapp/lit/v2"- _ "modernc.org/sqlite")-var DB *sql.DB+var DB *sql.DB // PostgreSQL-replacement: relational/config data (transactional)+var TelemetryDB *sql.DB // ClickHouse-replacement: append-only telemetry data (non-transactional)var Driver lit.Driver = lit.PostgreSQLfunc IsSQLite() bool {return Driver == lit.SQLite}-func Init() error {- cfg := config.Config- if cfg.DBType == "sqlite" {- return initSQLite()- }- return initPostgres()-}-func initPostgres() error {cfg := config.Config@@ -66,38 +59,6 @@ func initPostgres() error {return nil}-func initSQLite() error {- path := config.Config.SQLitePath- if path == "" {- path = "./traceway.db"- }-- db, err := sql.Open("sqlite", path)- if err != nil {- return fmt.Errorf("failed to open sqlite connection: %w", err)- }-- if err := db.Ping(); err != nil {- return fmt.Errorf("failed to ping sqlite: %w", err)- }-- if _, err := db.Exec("PRAGMA foreign_keys = ON"); err != nil {- return fmt.Errorf("failed to enable foreign keys: %w", err)- }- if _, err := db.Exec("PRAGMA journal_mode = WAL"); err != nil {- return fmt.Errorf("failed to set WAL mode: %w", err)- }-- db.SetMaxOpenConns(1)-- DB = db- Driver = lit.SQLite-- config.Logf("SQLite database opened at %s", path)-- return nil-}-func GetDB() *sql.DB {return DB}diff --git a/backend/app/db/db_pgch.go b/backend/app/db/db_pgch.gonew file mode 100644index 00000000..25938f14--- /dev/null+++ b/backend/app/db/db_pgch.go@@ -0,0 +1,7 @@+//go:build pgch++package db++func Init() error {+ return initPostgres()+}diff --git a/backend/app/db/db_sqlite.go b/backend/app/db/db_sqlite.gonew file mode 100644index 00000000..5f99dbef--- /dev/null+++ b/backend/app/db/db_sqlite.go@@ -0,0 +1,69 @@+//go:build !pgch++package db++import (+ "database/sql"+ "fmt"+ "strings"++ "github.com/tracewayapp/traceway/backend/app/config"+ "github.com/tracewayapp/lit/v2"+ _ "modernc.org/sqlite"+)++func Init() error {+ cfg := config.Config+ if cfg.DBType == "sqlite" {+ return initSQLite()+ }+ return initPostgres()+}++func initSQLite() error {+ path := config.Config.SQLitePath+ if path == "" {+ path = "./traceway.db"+ }++ mainDB, err := openSQLite(path, true)+ if err != nil {+ return err+ }+ DB = mainDB+ Driver = lit.SQLite+ config.Logf("SQLite database opened at %s", path)++ telemetryPath := strings.TrimSuffix(path, ".db") + "_telemetry.db"+ if path == ":memory:" {+ telemetryPath = ":memory:"+ }+ telDB, err := openSQLite(telemetryPath, false)+ if err != nil {+ return err+ }+ TelemetryDB = telDB+ config.Logf("SQLite telemetry database opened at %s", telemetryPath)++ return nil+}++func openSQLite(path string, foreignKeys bool) (*sql.DB, error) {+ d, err := sql.Open("sqlite", path)+ if err != nil {+ return nil, fmt.Errorf("failed to open sqlite at %s: %w", path, err)+ }+ if err := d.Ping(); err != nil {+ return nil, fmt.Errorf("failed to ping sqlite at %s: %w", path, err)+ }+ if foreignKeys {+ if _, err := d.Exec("PRAGMA foreign_keys = ON"); err != nil {+ return nil, fmt.Errorf("failed to enable foreign keys: %w", err)+ }+ }+ if _, err := d.Exec("PRAGMA journal_mode = WAL"); err != nil {+ return nil, fmt.Errorf("failed to set WAL mode: %w", err)+ }+ d.SetMaxOpenConns(1)+ return d, nil+}diff --git a/backend/app/migrations/migrations_sqlite.go b/backend/app/migrations/migrations_sqlite.goindex b0a9a687..6496c87d 100644--- a/backend/app/migrations/migrations_sqlite.go+++ b/backend/app/migrations/migrations_sqlite.go@@ -3,6 +3,7 @@package migrationsimport (+ "database/sql""embed""fmt""sort"@@ -22,20 +23,33 @@ var ExtensionPostgresMigrations []ExtensionMigration//go:embed sqlite/*.sqlvar migrationsSqliteFS embed.FS+//go:embed sqlite_telemetry/*.sql+var migrationsSqliteTelemetryFS embed.FS+func Run(dbType string) error {- sqliteDB := db.DB+ if err := runMigrationsOn(db.DB, migrationsSqliteFS, "sqlite", "schema_migrations"); err != nil {+ return fmt.Errorf("main db migrations: %w", err)+ }++ if err := runMigrationsOn(db.TelemetryDB, migrationsSqliteTelemetryFS, "sqlite_telemetry", "schema_migrations"); err != nil {+ return fmt.Errorf("telemetry db migrations: %w", err)+ }++ return nil+}- _, err := sqliteDB.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (+func runMigrationsOn(target *sql.DB, fsys embed.FS, dir string, trackingTable string) error {+ _, err := target.Exec(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (version TEXT PRIMARY KEY,applied_at DATETIME DEFAULT (datetime('now'))- )`)+ )`, trackingTable))if err != nil {- return fmt.Errorf("failed to create schema_migrations table: %w", err)+ return fmt.Errorf("failed to create %s table: %w", trackingTable, err)}- entries, err := migrationsSqliteFS.ReadDir("sqlite")+ entries, err := fsys.ReadDir(dir)if err != nil {- return fmt.Errorf("failed to read sqlite migrations dir: %w", err)+ return fmt.Errorf("failed to read migrations dir %s: %w", dir, err)}var files []string@@ -50,7 +64,7 @@ func Run(dbType string) error {version := strings.TrimSuffix(file, ".up.sql")var count int- err := sqliteDB.QueryRow("SELECT COUNT(*) FROM schema_migrations WHERE version = ?", version).Scan(&count)+ err := target.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE version = ?", trackingTable), version).Scan(&count)if err != nil {return fmt.Errorf("failed to check migration version %s: %w", version, err)}@@ -58,7 +72,7 @@ func Run(dbType string) error {continue}- content, err := migrationsSqliteFS.ReadFile("sqlite/" + file)+ content, err := fsys.ReadFile(dir + "/" + file)if err != nil {return fmt.Errorf("failed to read migration file %s: %w", file, err)}@@ -69,12 +83,12 @@ func Run(dbType string) error {if stmt == "" {continue}- if _, err := sqliteDB.Exec(stmt); err != nil {+ if _, err := target.Exec(stmt); err != nil {return fmt.Errorf("failed to execute migration %s: %w", file, err)}}- if _, err := sqliteDB.Exec("INSERT INTO schema_migrations (version) VALUES (?)", version); err != nil {+ if _, err := target.Exec(fmt.Sprintf("INSERT INTO %s (version) VALUES (?)", trackingTable), version); err != nil {return fmt.Errorf("failed to record migration version %s: %w", version, err)}}diff --git a/backend/app/migrations/sqlite_telemetry/0001_telemetry_tables.up.sql b/backend/app/migrations/sqlite_telemetry/0001_telemetry_tables.up.sqlnew file mode 100644index 00000000..28120b1b--- /dev/null+++ b/backend/app/migrations/sqlite_telemetry/0001_telemetry_tables.up.sql@@ -0,0 +1,107 @@+CREATE TABLE IF NOT EXISTS endpoints (+ id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ endpoint TEXT NOT NULL DEFAULT '',+ duration INTEGER NOT NULL DEFAULT 0,+ recorded_at DATETIME NOT NULL,+ status_code INTEGER NOT NULL DEFAULT 0,+ body_size INTEGER NOT NULL DEFAULT 0,+ client_ip TEXT NOT NULL DEFAULT '',+ attributes TEXT NOT NULL DEFAULT '{}',+ app_version TEXT NOT NULL DEFAULT '',+ server_name TEXT NOT NULL DEFAULT '',+ distributed_trace_id TEXT DEFAULT NULL+);+CREATE INDEX IF NOT EXISTS idx_endpoints_project_recorded ON endpoints(project_id, recorded_at);+CREATE INDEX IF NOT EXISTS idx_endpoints_project_endpoint ON endpoints(project_id, endpoint);++CREATE TABLE IF NOT EXISTS tasks (+ id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ task_name TEXT NOT NULL DEFAULT '',+ duration INTEGER NOT NULL DEFAULT 0,+ recorded_at DATETIME NOT NULL,+ client_ip TEXT NOT NULL DEFAULT '',+ attributes TEXT NOT NULL DEFAULT '{}',+ app_version TEXT NOT NULL DEFAULT '',+ server_name TEXT NOT NULL DEFAULT '',+ distributed_trace_id TEXT DEFAULT NULL+);+CREATE INDEX IF NOT EXISTS idx_tasks_project_recorded ON tasks(project_id, recorded_at);++CREATE TABLE IF NOT EXISTS exception_stack_traces (+ id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ trace_id TEXT,+ trace_type TEXT NOT NULL DEFAULT 'endpoint',+ exception_hash TEXT NOT NULL DEFAULT '',+ stack_trace TEXT NOT NULL DEFAULT '',+ recorded_at DATETIME NOT NULL,+ attributes TEXT NOT NULL DEFAULT '{}',+ app_version TEXT NOT NULL DEFAULT '',+ server_name TEXT NOT NULL DEFAULT '',+ is_message INTEGER NOT NULL DEFAULT 0,+ distributed_trace_id TEXT DEFAULT NULL+);+CREATE INDEX IF NOT EXISTS idx_exceptions_project_recorded ON exception_stack_traces(project_id, recorded_at);+CREATE INDEX IF NOT EXISTS idx_exceptions_project_hash ON exception_stack_traces(project_id, exception_hash);++CREATE TABLE IF NOT EXISTS spans (+ id TEXT NOT NULL,+ trace_id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ name TEXT NOT NULL DEFAULT '',+ start_time DATETIME NOT NULL,+ duration INTEGER NOT NULL DEFAULT 0,+ recorded_at DATETIME NOT NULL+);+CREATE INDEX IF NOT EXISTS idx_spans_project_trace ON spans(project_id, trace_id);++CREATE TABLE IF NOT EXISTS metric_points (+ project_id TEXT NOT NULL,+ name TEXT NOT NULL DEFAULT '',+ value REAL NOT NULL DEFAULT 0,+ tags TEXT NOT NULL DEFAULT '{}',+ recorded_at DATETIME NOT NULL+);+CREATE INDEX IF NOT EXISTS idx_metric_points_project_name ON metric_points(project_id, name, recorded_at);++CREATE TABLE IF NOT EXISTS session_recordings (+ id TEXT NOT NULL,+ project_id TEXT NOT NULL,+ exception_id TEXT NOT NULL,+ file_path TEXT NOT NULL DEFAULT '',+ recorded_at DATETIME NOT NULL+);+CREATE INDEX IF NOT EXISTS idx_session_recordings_project_exception ON session_recordings(project_id, exception_id);++CREATE TABLE IF NOT EXISTS archived_exceptions (+ project_id TEXT NOT NULL,+ exception_hash TEXT NOT NULL,+ archived_at DATETIME NOT NULL DEFAULT (datetime('now')),+ UNIQUE(project_id, exception_hash)+);++CREATE TABLE IF NOT EXISTS slow_endpoints (+ project_id TEXT NOT NULL,+ endpoint TEXT NOT NULL,+ offset_ms INTEGER NOT NULL DEFAULT 0,+ reason TEXT NOT NULL DEFAULT '',+ UNIQUE(project_id, endpoint)+);++CREATE TABLE IF NOT EXISTS fired_notifications (+ project_id TEXT NOT NULL,+ rule_id INTEGER NOT NULL DEFAULT 0,+ rule_type TEXT NOT NULL DEFAULT '',+ rule_name TEXT NOT NULL DEFAULT '',+ channel_type TEXT NOT NULL DEFAULT '',+ channel_name TEXT NOT NULL DEFAULT '',+ severity TEXT NOT NULL DEFAULT '',+ subject TEXT NOT NULL DEFAULT '',+ body TEXT NOT NULL DEFAULT '',+ status TEXT NOT NULL DEFAULT '',+ error_message TEXT NOT NULL DEFAULT '',+ endpoint TEXT NOT NULL DEFAULT '',+ fired_at DATETIME NOT NULL+);diff --git a/backend/app/repositories/endpoint.repository_sqlite.go b/backend/app/repositories/endpoint.repository_sqlite.goindex f54f2f37..f0b2efc1 100644--- a/backend/app/repositories/endpoint.repository_sqlite.go+++ b/backend/app/repositories/endpoint.repository_sqlite.go@@ -4,9 +4,6 @@ package repositoriesimport ("context"- "database/sql"- "encoding/json"- "errors""fmt""math""sort"@@ -14,10 +11,113 @@ import ("time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")+type endpoint struct {+ Id uuid.UUID `lit:"id"`+ ProjectId uuid.UUID `lit:"project_id"`+ Endpoint string `lit:"endpoint"`+ Duration int64 `lit:"duration"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+ StatusCode int16 `lit:"status_code"`+ BodySize int32 `lit:"body_size"`+ ClientIP string `lit:"client_ip"`+ Attributes SQLiteJSONMap `lit:"attributes"`+ AppVersion string `lit:"app_version"`+ ServerName string `lit:"server_name"`+ DistributedTraceId *uuid.UUID `lit:"distributed_trace_id"`+}++type groupedEndpointRow struct {+ Endpoint string `lit:"endpoint"`+ TotalCount uint64 `lit:"total_count"`+ AvgDuration float64 `lit:"avg_duration"`+ LastSeen string `lit:"last_seen"`+ OffsetMs uint32 `lit:"offset_ms"`+ ServerErrorCount uint64 `lit:"server_error_count"`+ ClientErrorCount uint64 `lit:"client_error_count"`+ SatisfiedCount uint64 `lit:"satisfied_count"`+ ToleratingCount uint64 `lit:"tolerating_count"`+ BadCount uint64 `lit:"bad_count"`+}++type endpointDurationRow struct {+ Duration float64 `lit:"duration"`+}++type slowEndpointRow struct {+ OffsetMs uint32 `lit:"offset_ms"`+ Reason string `lit:"reason"`+}++type endpointMetricRow struct {+ Endpoint string `lit:"endpoint"`+ MetricValue float64 `lit:"metric_value"`+}++type distinctEndpointRow struct {+ Endpoint string `lit:"endpoint"`+}++type endpointDetailStatsRow struct {+ Count int64 `lit:"count"`+ AvgDurationMs float64 `lit:"avg_duration_ms"`+ ErrorRate float64 `lit:"error_rate"`+ SatisfiedTolerating float64 `lit:"satisfied_tolerating"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[endpoint](driver)+ lit.RegisterModel[groupedEndpointRow](driver)+ lit.RegisterModel[endpointDurationRow](driver)+ lit.RegisterModel[slowEndpointRow](driver)+ lit.RegisterModel[endpointMetricRow](driver)+ lit.RegisterModel[distinctEndpointRow](driver)+ lit.RegisterModel[endpointDetailStatsRow](driver)+ })+}++func endpointToRow(e models.Endpoint) endpoint {+ return endpoint{+ Id: e.Id,+ ProjectId: e.ProjectId,+ Endpoint: e.Endpoint,+ Duration: int64(e.Duration),+ RecordedAt: NewSQLiteTime(e.RecordedAt),+ StatusCode: e.StatusCode,+ BodySize: e.BodySize,+ ClientIP: e.ClientIP,+ Attributes: NewSQLiteJSONMap(e.Attributes),+ AppVersion: e.AppVersion,+ ServerName: e.ServerName,+ DistributedTraceId: e.DistributedTraceId,+ }+}++func (r *endpoint) toModel() models.Endpoint {+ e := models.Endpoint{+ Id: r.Id,+ ProjectId: r.ProjectId,+ Endpoint: r.Endpoint,+ Duration: time.Duration(r.Duration),+ RecordedAt: r.RecordedAt.Time,+ StatusCode: r.StatusCode,+ BodySize: r.BodySize,+ ClientIP: r.ClientIP,+ AppVersion: r.AppVersion,+ ServerName: r.ServerName,+ DistributedTraceId: r.DistributedTraceId,+ }+ if r.Attributes != nil {+ e.Attributes = map[string]string(r.Attributes)+ }+ return e+}+type endpointRepository struct{}func (e *endpointRepository) InsertAsync(ctx context.Context, lines []models.Endpoint) error {@@ -25,111 +125,85 @@ func (e *endpointRepository) InsertAsync(ctx context.Context, lines []models.Endreturn nil}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- stmt, err := tx.PrepareContext(ctx, `INSERT INTO endpoints (id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)- if err != nil {- return err- }- defer stmt.Close()-- for _, t := range lines {- attributesJSON := "{}"- if len(t.Attributes) != 0 {- if b, err := json.Marshal(t.Attributes); err == nil {- attributesJSON = string(b)- }- }- var distributedTraceIdStr *string- if t.DistributedTraceId != nil {- s := t.DistributedTraceId.String()- distributedTraceIdStr = &s- }- if _, err := stmt.ExecContext(ctx,- t.Id.String(), t.ProjectId.String(), t.Endpoint,- int64(t.Duration), t.RecordedAt.UTC().Format(time.RFC3339Nano),- t.StatusCode, t.BodySize, t.ClientIP, attributesJSON,- t.AppVersion, t.ServerName, distributedTraceIdStr,- ); err != nil {+ for _, ep := range lines {+ row := endpointToRow(ep)+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {return err}}- return tx.Commit()+ return nil}func (e *endpointRepository) CountBetween(ctx context.Context, projectId uuid.UUID, start, end time.Time) (int64, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM endpoints WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)).Scan(&count)- return count, err+ result, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return 0, err+ }+ if result == nil {+ return 0, nil+ }+ return int64(result.Count), nil}func (e *endpointRepository) FindAll(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy string) ([]models.Endpoint, int64, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM endpoints WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), fromDate.UTC().Format(time.RFC3339Nano), toDate.UTC().Format(time.RFC3339Nano)).Scan(&count)+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}++ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ params)if err != nil {return nil, 0, err}+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }offset := (page - 1) * pageSize- allowedOrderBy := map[string]bool{- "recorded_at": true,- "duration": true,- "status_code": true,- "body_size": true,- }+ allowedOrderBy := map[string]bool{"recorded_at": true, "duration": true, "status_code": true, "body_size": true}if !allowedOrderBy[orderBy] {orderBy = "recorded_at"}- query := `SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- ORDER BY ` + orderBy + ` DESC LIMIT ? OFFSET ?`-- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), fromDate.UTC().Format(time.RFC3339Nano), toDate.UTC().Format(time.RFC3339Nano),- pageSize, offset)+ rows, err := lit.SelectNamed[endpoint](db.TelemetryDB,+ fmt.Sprintf(`SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY %s DESC LIMIT :limit OFFSET :offset`, orderBy),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "limit": pageSize, "offset": offset})if err != nil {return nil, 0, err}- defer rows.Close()- endpoints, err := scanEndpoints(rows)- if err != nil {- return nil, 0, err+ endpoints := make([]models.Endpoint, 0, len(rows))+ for _, row := range rows {+ endpoints = append(endpoints, row.toModel())}return endpoints, count, nil}func (e *endpointRepository) FindGroupedByEndpoint(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy string, sortDirection string, search string) ([]models.EndpointStats, int64, error) {- fromStr := fromDate.UTC().Format(time.RFC3339Nano)- toStr := toDate.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}- whereClause := "e.project_id = ? AND e.recorded_at >= ? AND e.recorded_at <= ?"- args := []interface{}{pidStr, fromStr, toStr}+ whereClause := "e.project_id = :project_id AND e.recorded_at >= :from AND e.recorded_at <= :to"if search != "" {- whereClause += " AND INSTR(LOWER(e.endpoint), LOWER(?)) > 0"- args = append(args, search)+ whereClause += " AND INSTR(LOWER(e.endpoint), LOWER(:search)) > 0"+ params["search"] = search}- var totalEndpoints int64- countQuery := "SELECT COUNT(DISTINCT e.endpoint) FROM endpoints e WHERE " + whereClause- if err := db.DB.QueryRowContext(ctx, countQuery, args...).Scan(&totalEndpoints); err != nil {+ countQuery := "SELECT COUNT(DISTINCT e.endpoint) AS count FROM endpoints e WHERE " + whereClause+ totalResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB, countQuery, params)+ if err != nil {return nil, 0, err}+ totalEndpoints := int64(0)+ if totalResult != nil {+ totalEndpoints = int64(totalResult.Count)+ }groupQuery := `SELECTe.endpoint,@@ -147,31 +221,23 @@ func (e *endpointRepository) FindGroupedByEndpoint(ctx context.Context, projectIWHERE ` + whereClause + `GROUP BY e.endpoint`- rows, err := db.DB.QueryContext(ctx, groupQuery, args...)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, groupQuery, params)if err != nil {return nil, 0, err}- defer rows.Close()-- type groupRow struct {- endpoint string- totalCount uint64- avgDuration float64- lastSeen string- offsetMs uint32- serverErrorCount uint64- clientErrorCount uint64- satisfiedCount uint64- toleratingCount uint64- badCount uint64- }-- var groups []groupRow- for rows.Next() {- var g groupRow- if err := rows.Scan(&g.endpoint, &g.totalCount, &g.avgDuration, &g.lastSeen,- &g.offsetMs, &g.serverErrorCount, &g.clientErrorCount,- &g.satisfiedCount, &g.toleratingCount, &g.badCount); err != nil {++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)+ if err != nil {+ return nil, 0, err+ }+ defer sqlRows.Close()++ var groups []groupedEndpointRow+ for sqlRows.Next() {+ var g groupedEndpointRow+ if err := sqlRows.Scan(&g.Endpoint, &g.TotalCount, &g.AvgDuration, &g.LastSeen,+ &g.OffsetMs, &g.ServerErrorCount, &g.ClientErrorCount,+ &g.SatisfiedCount, &g.ToleratingCount, &g.BadCount); err != nil {return nil, 0, err}groups = append(groups, g)@@ -179,7 +245,7 @@ func (e *endpointRepository) FindGroupedByEndpoint(ctx context.Context, projectIvar stats []models.EndpointStatsfor _, g := range groups {- durations, err := fetchSortedDurations(ctx, pidStr, g.endpoint, fromStr, toStr)+ durations, err := fetchSortedDurations(ctx, projectId, g.Endpoint, fromDate, toDate)if err != nil {return nil, 0, err}@@ -188,23 +254,22 @@ func (e *endpointRepository) FindGroupedByEndpoint(ctx context.Context, projectIp95 := computePercentile(durations, 0.95)p99 := computePercentile(durations, 0.99)- impact := computeImpactScore(g.endpoint, g.totalCount, g.satisfiedCount, g.toleratingCount, g.badCount, g.clientErrorCount, p99, g.offsetMs)+ impact := computeImpactScore(g.Endpoint, g.TotalCount, g.SatisfiedCount, g.ToleratingCount, g.BadCount, g.ClientErrorCount, p99, g.OffsetMs)- lastSeen, _ := time.Parse(time.RFC3339Nano, g.lastSeen)+ lastSeen, _ := time.Parse(time.RFC3339Nano, g.LastSeen)- s := models.EndpointStats{- Endpoint: g.endpoint,- Count: g.totalCount,+ stats = append(stats, models.EndpointStats{+ Endpoint: g.Endpoint,+ Count: g.TotalCount,P50Duration: time.Duration(p50),P95Duration: time.Duration(p95),P99Duration: time.Duration(p99),- AvgDuration: time.Duration(g.avgDuration),+ AvgDuration: time.Duration(g.AvgDuration),LastSeen: lastSeen,Impact: impact,- ImpactReason: ComputeImpactReason(g.endpoint, g.totalCount, g.satisfiedCount, g.toleratingCount,- g.badCount, g.clientErrorCount, p99, g.offsetMs),- }- stats = append(stats, s)+ ImpactReason: ComputeImpactReason(g.Endpoint, g.TotalCount, g.SatisfiedCount, g.ToleratingCount,+ g.BadCount, g.ClientErrorCount, p99, g.OffsetMs),+ })}sortEndpointStats(stats, orderBy, sortDirection)@@ -217,32 +282,27 @@ func (e *endpointRepository) FindGroupedByEndpoint(ctx context.Context, projectIif endIdx > len(stats) {endIdx = len(stats)}- page_stats := stats[start:endIdx]- return page_stats, totalEndpoints, nil+ return stats[start:endIdx], totalEndpoints, nil}-func (e *endpointRepository) FindByEndpoint(ctx context.Context, projectId uuid.UUID, endpoint string, fromDate, toDate time.Time, page, pageSize int, orderBy string, sortDirection string) ([]models.Endpoint, int64, error) {- pidStr := projectId.String()- fromStr := fromDate.UTC().Format(time.RFC3339Nano)- toStr := toDate.UTC().Format(time.RFC3339Nano)+func (e *endpointRepository) FindByEndpoint(ctx context.Context, projectId uuid.UUID, endpointName string, fromDate, toDate time.Time, page, pageSize int, orderBy string, sortDirection string) ([]models.Endpoint, int64, error) {+ params := lit.P{"project_id": projectId, "endpoint": endpointName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM endpoints WHERE project_id = ? AND endpoint = ? AND recorded_at >= ? AND recorded_at <= ?",- pidStr, endpoint, fromStr, toStr).Scan(&count)+ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM endpoints WHERE project_id = :project_id AND endpoint = :endpoint AND recorded_at >= :from AND recorded_at <= :to",+ params)if err != nil {return nil, 0, err}+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }offset := (page - 1) * pageSize- allowedOrderBy := map[string]bool{- "recorded_at": true,- "duration": true,- "status_code": true,- "body_size": true,- }+ allowedOrderBy := map[string]bool{"recorded_at": true, "duration": true, "status_code": true, "body_size": true}if !allowedOrderBy[orderBy] {orderBy = "recorded_at"}@@ -252,141 +312,117 @@ func (e *endpointRepository) FindByEndpoint(ctx context.Context, projectId uuid.sortDir = "ASC"}- query := `SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM endpoints- WHERE project_id = ? AND endpoint = ? AND recorded_at >= ? AND recorded_at <= ?- ORDER BY ` + orderBy + ` ` + sortDir + ` LIMIT ? OFFSET ?`-- rows, err := db.DB.QueryContext(ctx, query, pidStr, endpoint, fromStr, toStr, pageSize, offset)+ rows, err := lit.SelectNamed[endpoint](db.TelemetryDB,+ fmt.Sprintf(`SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id+ FROM endpoints WHERE project_id = :project_id AND endpoint = :endpoint AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY %s %s LIMIT :limit OFFSET :offset`, orderBy, sortDir),+ lit.P{"project_id": projectId, "endpoint": endpointName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "limit": pageSize, "offset": offset})if err != nil {return nil, 0, err}- defer rows.Close()- endpoints, err := scanEndpoints(rows)- if err != nil {- return nil, 0, err+ endpoints := make([]models.Endpoint, 0, len(rows))+ for _, row := range rows {+ endpoints = append(endpoints, row.toModel())}return endpoints, count, nil}func (e *endpointRepository) FindById(ctx context.Context, projectId, endpointId uuid.UUID) (*models.Endpoint, error) {- query := `SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM endpoints- WHERE project_id = ? AND id = ?- LIMIT 1`-- var t models.Endpoint- var idStr, projectIdStr, recordedAtStr, attributesJSON string- var distributedTraceIdStr sql.NullString- err := db.DB.QueryRowContext(ctx, query, projectId.String(), endpointId.String()).Scan(- &idStr, &projectIdStr, &t.Endpoint, &t.Duration, &recordedAtStr,- &t.StatusCode, &t.BodySize, &t.ClientIP, &attributesJSON, &t.AppVersion, &t.ServerName, &distributedTraceIdStr)+ row, err := lit.SelectSingleNamed[endpoint](db.TelemetryDB,+ `SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id+ FROM endpoints WHERE project_id = :project_id AND id = :id LIMIT 1`,+ lit.P{"project_id": projectId, "id": endpointId})if err != nil {- if errors.Is(err, sql.ErrNoRows) {- return nil, nil- }return nil, err}-- t.Id, _ = uuid.Parse(idStr)- t.ProjectId, _ = uuid.Parse(projectIdStr)- t.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- t.DistributedTraceId = &parsed- }- }- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {- t.Attributes = nil- }+ if row == nil {+ return nil, nil}-- return &t, nil+ ep := row.toModel()+ return &ep, nil}func (e *endpointRepository) CountByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- CAST(COUNT(*) AS REAL) as count- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) AvgDurationByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- AVG(duration) / 1000000.0 as avg_duration_ms- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket, AVG(duration) / 1000000.0 as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) ErrorRateByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as error_rate- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket,+ SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) CountByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {secs := intervalMinutes * 60- query := fmt.Sprintf(`SELECT- datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket,- CAST(COUNT(*) AS REAL) as count- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`, secs, secs)-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, secs, secs),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) AvgDurationByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {secs := intervalMinutes * 60- query := fmt.Sprintf(`SELECT- datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket,- AVG(duration) / 1000000.0 as avg_duration_ms- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`, secs, secs)-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket, AVG(duration) / 1000000.0 as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, secs, secs),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) ErrorRateByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {secs := intervalMinutes * 60- query := fmt.Sprintf(`SELECT- datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket,- SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as error_rate- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`, secs, secs)-- return queryTimeSeries(ctx, query, projectId.String(), start, end)+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket,+ SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as agg_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, secs, secs),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return nil, err+ }+ return timeSeriesResultsToPoints(results), nil}func (e *endpointRepository) FindWorstEndpoints(ctx context.Context, projectId uuid.UUID, start, end time.Time, limit int) ([]models.EndpointStats, error) {- fromStr := start.UTC().Format(time.RFC3339Nano)- toStr := end.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}groupQuery := `SELECTe.endpoint,@@ -401,34 +437,26 @@ func (e *endpointRepository) FindWorstEndpoints(ctx context.Context, projectId uSUM(CASE WHEN e.duration > (1500000000 + COALESCE(s.offset_ms, 0) * 1000000) OR e.status_code >= 500 THEN 1 ELSE 0 END) as bad_countFROM endpoints eLEFT JOIN slow_endpoints s ON e.endpoint = s.endpoint AND e.project_id = s.project_id- WHERE e.project_id = ? AND e.recorded_at >= ? AND e.recorded_at <= ?+ WHERE e.project_id = :project_id AND e.recorded_at >= :from AND e.recorded_at <= :toGROUP BY e.endpoint`- rows, err := db.DB.QueryContext(ctx, groupQuery, pidStr, fromStr, toStr)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, groupQuery, params)if err != nil {return nil, err}- defer rows.Close()-- type groupRow struct {- endpoint string- totalCount uint64- avgDuration float64- lastSeen string- offsetMs uint32- serverErrorCount uint64- clientErrorCount uint64- satisfiedCount uint64- toleratingCount uint64- badCount uint64- }-- var groups []groupRow- for rows.Next() {- var g groupRow- if err := rows.Scan(&g.endpoint, &g.totalCount, &g.avgDuration, &g.lastSeen,- &g.offsetMs, &g.serverErrorCount, &g.clientErrorCount,- &g.satisfiedCount, &g.toleratingCount, &g.badCount); err != nil {++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)+ if err != nil {+ return nil, err+ }+ defer sqlRows.Close()++ var groups []groupedEndpointRow+ for sqlRows.Next() {+ var g groupedEndpointRow+ if err := sqlRows.Scan(&g.Endpoint, &g.TotalCount, &g.AvgDuration, &g.LastSeen,+ &g.OffsetMs, &g.ServerErrorCount, &g.ClientErrorCount,+ &g.SatisfiedCount, &g.ToleratingCount, &g.BadCount); err != nil {return nil, err}groups = append(groups, g)@@ -436,7 +464,7 @@ func (e *endpointRepository) FindWorstEndpoints(ctx context.Context, projectId uvar stats []models.EndpointStatsfor _, g := range groups {- durations, err := fetchSortedDurations(ctx, pidStr, g.endpoint, fromStr, toStr)+ durations, err := fetchSortedDurations(ctx, projectId, g.Endpoint, start, end)if err != nil {return nil, err}@@ -445,23 +473,22 @@ func (e *endpointRepository) FindWorstEndpoints(ctx context.Context, projectId up95 := computePercentile(durations, 0.95)p99 := computePercentile(durations, 0.99)- impact := computeImpactScore(g.endpoint, g.totalCount, g.satisfiedCount, g.toleratingCount, g.badCount, g.clientErrorCount, p99, g.offsetMs)+ impact := computeImpactScore(g.Endpoint, g.TotalCount, g.SatisfiedCount, g.ToleratingCount, g.BadCount, g.ClientErrorCount, p99, g.OffsetMs)- lastSeen, _ := time.Parse(time.RFC3339Nano, g.lastSeen)+ lastSeen, _ := time.Parse(time.RFC3339Nano, g.LastSeen)- s := models.EndpointStats{- Endpoint: g.endpoint,- Count: g.totalCount,+ stats = append(stats, models.EndpointStats{+ Endpoint: g.Endpoint,+ Count: g.TotalCount,P50Duration: time.Duration(p50),P95Duration: time.Duration(p95),P99Duration: time.Duration(p99),- AvgDuration: time.Duration(g.avgDuration),+ AvgDuration: time.Duration(g.AvgDuration),LastSeen: lastSeen,Impact: impact,- ImpactReason: ComputeImpactReason(g.endpoint, g.totalCount, g.satisfiedCount, g.toleratingCount,- g.badCount, g.clientErrorCount, p99, g.offsetMs),- }- stats = append(stats, s)+ ImpactReason: ComputeImpactReason(g.Endpoint, g.TotalCount, g.SatisfiedCount, g.ToleratingCount,+ g.BadCount, g.ClientErrorCount, p99, g.OffsetMs),+ })}sort.Slice(stats, func(i, j int) bool {@@ -476,41 +503,39 @@ func (e *endpointRepository) FindWorstEndpoints(ctx context.Context, projectId u}func (e *endpointRepository) GetEndpointStats(ctx context.Context, projectId uuid.UUID, endpoint string, start, end time.Time) (*models.EndpointDetailStats, error) {- pidStr := projectId.String()- fromStr := start.UTC().Format(time.RFC3339Nano)- toStr := end.UTC().Format(time.RFC3339Nano)+ params := lit.P{"project_id": projectId, "endpoint": endpoint, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}durationMinutes := end.Sub(start).Minutes()if durationMinutes < 1 {durationMinutes = 1}- query := `SELECT- COUNT(*) as count,- CASE WHEN COUNT(*) > 0 THEN AVG(duration) / 1000000.0 ELSE 0 END as avg_duration_ms,- CASE WHEN COUNT(*) > 0 THEN SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) ELSE 0 END as error_rate,- SUM(CASE WHEN duration <= 500000000 AND status_code < 500 THEN 1 ELSE 0 END) +- SUM(CASE WHEN duration > 500000000 AND duration <= 2000000000 AND status_code < 500 THEN 1 ELSE 0 END) * 0.5 as satisfied_tolerating- FROM endpoints- WHERE project_id = ? AND endpoint = ? AND recorded_at >= ? AND recorded_at <= ?`-- var stats models.EndpointDetailStats- var count int64- var satisfiedTolerating float64-- err := db.DB.QueryRowContext(ctx, query, pidStr, endpoint, fromStr, toStr).Scan(- &count, &stats.AvgDuration, &stats.ErrorRate, &satisfiedTolerating)+ row, err := lit.SelectSingleNamed[endpointDetailStatsRow](db.TelemetryDB,+ `SELECT+ COUNT(*) as count,+ CASE WHEN COUNT(*) > 0 THEN AVG(duration) / 1000000.0 ELSE 0 END as avg_duration_ms,+ CASE WHEN COUNT(*) > 0 THEN SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) ELSE 0 END as error_rate,+ SUM(CASE WHEN duration <= 500000000 AND status_code < 500 THEN 1 ELSE 0 END) ++ SUM(CASE WHEN duration > 500000000 AND duration <= 2000000000 AND status_code < 500 THEN 1 ELSE 0 END) * 0.5 as satisfied_tolerating+ FROM endpoints WHERE project_id = :project_id AND endpoint = :endpoint AND recorded_at >= :from AND recorded_at <= :to`,+ params)if err != nil {return nil, err}+ if row == nil {+ return &models.EndpointDetailStats{}, nil+ }- stats.Count = count- if count > 0 {- stats.Apdex = satisfiedTolerating / float64(count)+ var stats models.EndpointDetailStats+ stats.Count = row.Count+ stats.AvgDuration = row.AvgDurationMs+ stats.ErrorRate = row.ErrorRate+ if row.Count > 0 {+ stats.Apdex = row.SatisfiedTolerating / float64(row.Count)}- stats.Throughput = float64(count) / durationMinutes+ stats.Throughput = float64(row.Count) / durationMinutes- durations, err := fetchSortedDurations(ctx, pidStr, endpoint, fromStr, toStr)+ durations, err := fetchSortedDurations(ctx, projectId, endpoint, start, end)if err != nil {return nil, err}@@ -525,12 +550,9 @@ func (e *endpointRepository) GetEndpointStats(ctx context.Context, projectId uui}func (e *endpointRepository) GetEndpointStackedChart(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int, metricType string) (*models.EndpointStackedChartResponse, error) {- pidStr := projectId.String()- fromStr := start.UTC().Format(time.RFC3339Nano)- toStr := end.UTC().Format(time.RFC3339Nano)+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}- // Step 1: Get top 5 endpoints by metric- topEndpoints, err := getTopEndpointsByMetric(ctx, pidStr, fromStr, toStr, metricType)+ topEndpoints, err := getTopEndpointsByMetric(ctx, projectId, start, end, metricType)if err != nil {return nil, err}@@ -542,12 +564,17 @@ func (e *endpointRepository) GetEndpointStackedChart(ctx context.Context, projec}, nil}- // Step 2: Build CASE expression for categorization+ secs := intervalMinutes * 60++ if metricType == "p50" || metricType == "p95" || metricType == "p99" || metricType == "" {+ return e.getStackedChartWithPercentiles(ctx, projectId, start, end, secs, topEndpoints, metricType)+ }+caseExpr := "CASE "- caseArgs := make([]interface{}, 0, len(topEndpoints))- for _, ep := range topEndpoints {- caseExpr += "WHEN endpoint = ? THEN ? "- caseArgs = append(caseArgs, ep, ep)+ for i, ep := range topEndpoints {+ key := fmt.Sprintf("ep_%d", i)+ caseExpr += fmt.Sprintf("WHEN endpoint = :%s THEN :%s ", key, key)+ params[key] = ep}caseExpr += "ELSE 'Other' END"@@ -555,44 +582,35 @@ func (e *endpointRepository) GetEndpointStackedChart(ctx context.Context, projecswitch metricType {case "total_time":metricExpr = "COUNT(*) * AVG(duration) / 1000000.0"- case "p95":- metricExpr = "AVG(duration) / 1000000.0"- case "p99":- metricExpr = "AVG(duration) / 1000000.0"default:metricExpr = "AVG(duration) / 1000000.0"}- secs := intervalMinutes * 60timeSeriesQuery := fmt.Sprintf(`SELECTdatetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket,%s as endpoint_category,%s as metric_valueFROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?+ WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :toGROUP BY bucket, endpoint_categoryORDER BY bucket ASC, endpoint_category ASC`, secs, secs, caseExpr, metricExpr)- args := make([]interface{}, 0, len(caseArgs)+3)- args = append(args, caseArgs...)- args = append(args, pidStr, fromStr, toStr)-- // For p50/p95/p99 we need per-bucket per-category percentile computation- if metricType == "p50" || metricType == "p95" || metricType == "p99" || metricType == "" {- return e.getStackedChartWithPercentiles(ctx, pidStr, fromStr, toStr, secs, topEndpoints, metricType)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, timeSeriesQuery, params)+ if err != nil {+ return nil, err}- rows, err := db.DB.QueryContext(ctx, timeSeriesQuery, args...)+ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}- defer rows.Close()+ defer sqlRows.Close()var series []models.EndpointTimeSeriesPoint- for rows.Next() {+ for sqlRows.Next() {var p models.EndpointTimeSeriesPointvar tsStr string- if err := rows.Scan(&tsStr, &p.Endpoint, &p.Value); err != nil {+ if err := sqlRows.Scan(&tsStr, &p.Endpoint, &p.Value); err != nil {return nil, err}p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)@@ -616,7 +634,7 @@ func (e *endpointRepository) GetEndpointStackedChart(ctx context.Context, projec}, nil}-func (e *endpointRepository) getStackedChartWithPercentiles(ctx context.Context, pidStr, fromStr, toStr string, bucketSecs int, topEndpoints []string, metricType string) (*models.EndpointStackedChartResponse, error) {+func (e *endpointRepository) getStackedChartWithPercentiles(ctx context.Context, projectId uuid.UUID, start, end time.Time, bucketSecs int, topEndpoints []string, metricType string) (*models.EndpointStackedChartResponse, error) {percentile := 0.5switch metricType {case "p95":@@ -635,14 +653,20 @@ func (e *endpointRepository) getStackedChartWithPercentiles(ctx context.Context,endpoint,durationFROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?+ WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :toORDER BY bucket ASC, endpoint ASC, duration ASC`, bucketSecs, bucketSecs)- rows, err := db.DB.QueryContext(ctx, query, pidStr, fromStr, toStr)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)+ if err != nil {+ return nil, err+ }+ defer sqlRows.Close()type bucketKey struct {bucket string@@ -650,16 +674,16 @@ func (e *endpointRepository) getStackedChartWithPercentiles(ctx context.Context,}durationMap := make(map[bucketKey][]float64)- for rows.Next() {- var bucketStr, endpoint string+ for sqlRows.Next() {+ var bucketStr, epName stringvar duration float64- if err := rows.Scan(&bucketStr, &endpoint, &duration); err != nil {+ if err := sqlRows.Scan(&bucketStr, &epName, &duration); err != nil {return nil, err}category := "Other"- if topSet[endpoint] {- category = endpoint+ if topSet[epName] {+ category = epName}key := bucketKey{bucket: bucketStr, category: category}@@ -704,68 +728,41 @@ func (e *endpointRepository) getStackedChartWithPercentiles(ctx context.Context,}func (e *endpointRepository) GetSlowEndpoint(ctx context.Context, projectId uuid.UUID, endpoint string) (uint32, string, error) {- var offsetMs uint32- var reason string- err := db.DB.QueryRowContext(ctx,- "SELECT offset_ms, reason FROM slow_endpoints WHERE project_id = ? AND endpoint = ?",- projectId.String(), endpoint).Scan(&offsetMs, &reason)- return offsetMs, reason, err+ result, err := lit.SelectSingleNamed[slowEndpointRow](db.TelemetryDB,+ "SELECT offset_ms, reason FROM slow_endpoints WHERE project_id = :project_id AND endpoint = :endpoint",+ lit.P{"project_id": projectId, "endpoint": endpoint})+ if err != nil {+ return 0, "", err+ }+ if result == nil {+ return 0, "", fmt.Errorf("not found")+ }+ return result.OffsetMs, result.Reason, nil}func (e *endpointRepository) UpsertSlowEndpoint(ctx context.Context, projectId uuid.UUID, endpoint string, offsetMs uint32, reason string) error {- _, err := db.DB.ExecContext(ctx,- "INSERT OR REPLACE INTO slow_endpoints (project_id, endpoint, offset_ms, reason) VALUES (?, ?, ?, ?)",- projectId.String(), endpoint, offsetMs, reason)+ query, args, err := lit.ParseNamedQuery(db.Driver,+ "INSERT OR REPLACE INTO slow_endpoints (project_id, endpoint, offset_ms, reason) VALUES (:project_id, :endpoint, :offset_ms, :reason)",+ lit.P{"project_id": projectId, "endpoint": endpoint, "offset_ms": offsetMs, "reason": reason})+ if err != nil {+ return err+ }+ _, err = db.TelemetryDB.ExecContext(ctx, query, args...)return err}// --- helpers ----func scanEndpoints(rows *sql.Rows) ([]models.Endpoint, error) {- var endpoints []models.Endpoint- for rows.Next() {- var t models.Endpoint- var idStr, projectIdStr, recordedAtStr, attributesJSON string- var distributedTraceIdStr sql.NullString- if err := rows.Scan(&idStr, &projectIdStr, &t.Endpoint, &t.Duration, &recordedAtStr,- &t.StatusCode, &t.BodySize, &t.ClientIP, &attributesJSON, &t.AppVersion, &t.ServerName, &distributedTraceIdStr); err != nil {- return nil, err- }- t.Id, _ = uuid.Parse(idStr)- t.ProjectId, _ = uuid.Parse(projectIdStr)- t.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- t.DistributedTraceId = &parsed- }- }- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {- t.Attributes = nil- }- }- endpoints = append(endpoints, t)- }- return endpoints, nil-}--func fetchSortedDurations(ctx context.Context, pidStr, endpoint, fromStr, toStr string) ([]float64, error) {- rows, err := db.DB.QueryContext(ctx,- "SELECT duration FROM endpoints WHERE project_id = ? AND endpoint = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY duration ASC",- pidStr, endpoint, fromStr, toStr)+func fetchSortedDurations(ctx context.Context, projectId uuid.UUID, endpoint string, from, to time.Time) ([]float64, error) {+ results, err := lit.SelectNamed[endpointDurationRow](db.TelemetryDB,+ "SELECT duration FROM endpoints WHERE project_id = :project_id AND endpoint = :endpoint AND recorded_at >= :from AND recorded_at <= :to ORDER BY duration ASC",+ lit.P{"project_id": projectId, "endpoint": endpoint, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)})if err != nil {return nil, err}- defer rows.Close()-- var durations []float64- for rows.Next() {- var d float64- if err := rows.Scan(&d); err != nil {- return nil, err- }- durations = append(durations, d)+ durations := make([]float64, 0, len(results))+ for _, r := range results {+ durations = append(durations, r.Duration)}return durations, nil}@@ -866,55 +863,26 @@ func sortEndpointStats(stats []models.EndpointStats, orderBy string, sortDirecti})}-func queryTimeSeries(ctx context.Context, query, pidStr string, start, end time.Time) ([]models.TimeSeriesPoint, error) {- rows, err := db.DB.QueryContext(ctx, query, pidStr, start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano))- if err != nil {- return nil, err- }- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var p models.TimeSeriesPoint- var tsStr string- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil-}+func getTopEndpointsByMetric(ctx context.Context, projectId uuid.UUID, from, to time.Time, metricType string) ([]string, error) {+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)}-func getTopEndpointsByMetric(ctx context.Context, pidStr, fromStr, toStr, metricType string) ([]string, error) {if metricType == "total_time" {- query := `SELECT endpoint, COUNT(*) * AVG(duration) / 1000000.0 as metric_value- FROM endpoints- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY endpoint- ORDER BY metric_value DESC- LIMIT 5`-- rows, err := db.DB.QueryContext(ctx, query, pidStr, fromStr, toStr)+ results, err := lit.SelectNamed[endpointMetricRow](db.TelemetryDB,+ `SELECT endpoint, COUNT(*) * AVG(duration) / 1000000.0 as metric_value+ FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY endpoint ORDER BY metric_value DESC LIMIT 5`,+ params)if err != nil {return nil, err}- defer rows.Close()-- var endpoints []string- for rows.Next() {- var ep string- var val float64- if err := rows.Scan(&ep, &val); err != nil {- return nil, err- }- endpoints = append(endpoints, ep)++ endpoints := make([]string, 0, len(results))+ for _, r := range results {+ endpoints = append(endpoints, r.Endpoint)}return endpoints, nil}- // For p50/p95/p99: need to fetch durations per endpoint, compute percentile, sortpercentile := 0.5switch metricType {case "p95":@@ -923,21 +891,12 @@ func getTopEndpointsByMetric(ctx context.Context, pidStr, fromStr, toStr, metricpercentile = 0.99}- epQuery := `SELECT DISTINCT endpoint FROM endpoints WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?`- rows, err := db.DB.QueryContext(ctx, epQuery, pidStr, fromStr, toStr)+ epRows, err := lit.SelectNamed[distinctEndpointRow](db.TelemetryDB,+ `SELECT DISTINCT endpoint FROM endpoints WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to`,+ params)if err != nil {return nil, err}- defer rows.Close()-- var allEndpoints []string- for rows.Next() {- var ep string- if err := rows.Scan(&ep); err != nil {- return nil, err- }- allEndpoints = append(allEndpoints, ep)- }type epMetric struct {endpoint string@@ -945,13 +904,13 @@ func getTopEndpointsByMetric(ctx context.Context, pidStr, fromStr, toStr, metric}var metrics []epMetric- for _, ep := range allEndpoints {- durations, err := fetchSortedDurations(ctx, pidStr, ep, fromStr, toStr)+ for _, ep := range epRows {+ durations, err := fetchSortedDurations(ctx, projectId, ep.Endpoint, from, to)if err != nil {return nil, err}val := computePercentile(durations, percentile) / 1000000.0- metrics = append(metrics, epMetric{endpoint: ep, value: val})+ metrics = append(metrics, epMetric{endpoint: ep.Endpoint, value: val})}sort.Slice(metrics, func(i, j int) bool {@@ -975,21 +934,38 @@ func (e *endpointRepository) FindByDistributedTraceId(ctx context.Context, distrif len(projectIds) == 0 {return nil, nil}- placeholders := strings.TrimRight(strings.Repeat("?,", len(projectIds)), ",")+ params := lit.P{"trace_id": distributedTraceId}+ placeholders := make([]string, len(projectIds))+ for i, pid := range projectIds {+ key := fmt.Sprintf("pid_%d", i)+ placeholders[i] = ":" + key+ params[key] = pid+ }query := `SELECT id, project_id, endpoint, duration, recorded_at, status_code, body_size, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM endpoints- WHERE distributed_trace_id = ? AND project_id IN (` + placeholders + `)+ FROM endpoints WHERE distributed_trace_id = :trace_id AND project_id IN (` + strings.Join(placeholders, ",") + `)ORDER BY recorded_at ASC`- args := []interface{}{distributedTraceId.String()}- for _, pid := range projectIds {- args = append(args, pid.String())++ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return nil, err}- rows, err := db.QueryerFromContext(ctx).QueryContext(ctx, query, args...)++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}- defer rows.Close()- return scanEndpoints(rows)+ defer sqlRows.Close()++ var endpoints []models.Endpoint+ for sqlRows.Next() {+ var row endpoint+ if err := sqlRows.Scan(&row.Id, &row.ProjectId, &row.Endpoint, &row.Duration, &row.RecordedAt,+ &row.StatusCode, &row.BodySize, &row.ClientIP, &row.Attributes, &row.AppVersion, &row.ServerName, &row.DistributedTraceId); err != nil {+ return nil, err+ }+ endpoints = append(endpoints, row.toModel())+ }+ return endpoints, nil}var EndpointRepository = endpointRepository{}diff --git a/backend/app/repositories/exception_stack_trace.repository_sqlite.go b/backend/app/repositories/exception_stack_trace.repository_sqlite.goindex 13d8b895..a176a0de 100644--- a/backend/app/repositories/exception_stack_trace.repository_sqlite.go+++ b/backend/app/repositories/exception_stack_trace.repository_sqlite.go@@ -5,94 +5,130 @@ package repositoriesimport ("context""database/sql"- "encoding/json""errors""fmt""strings""time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")-type exceptionStackTraceRepository struct{}+type exceptionRow struct {+ Id uuid.UUID `lit:"id"`+ ProjectId uuid.UUID `lit:"project_id"`+ TraceId *uuid.UUID `lit:"trace_id"`+ TraceType string `lit:"trace_type"`+ ExceptionHash string `lit:"exception_hash"`+ StackTrace string `lit:"stack_trace"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+ Attributes SQLiteJSONMap `lit:"attributes"`+ AppVersion string `lit:"app_version"`+ ServerName string `lit:"server_name"`+ IsMessage bool `lit:"is_message"`+ DistributedTraceId *uuid.UUID `lit:"distributed_trace_id"`+}-func (e *exceptionStackTraceRepository) InsertAsync(ctx context.Context, lines []models.ExceptionStackTrace) error {- if len(lines) == 0 {- return nil- }+type exceptionRowNaming struct{ lit.DefaultDbNamingStrategy }- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()+func (exceptionRowNaming) GetTableNameFromStructName(string) string {+ return "exception_stack_traces"+}- stmt, err := tx.PrepareContext(ctx,- `INSERT INTO exception_stack_traces (id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)- if err != nil {- return err- }- defer stmt.Close()+type exceptionGroupRow struct {+ ExceptionHash string `lit:"exception_hash"`+ StackTrace string `lit:"stack_trace"`+ LastSeen string `lit:"last_seen"`+ FirstSeen string `lit:"first_seen"`+ Count uint64 `lit:"count"`+ MaxArchivedAt *string `lit:"max_archived_at"`+}- for _, est := range lines {- attributesJSON := "{}"- if len(est.Attributes) != 0 {- if attributesBytes, err := json.Marshal(est.Attributes); err == nil {- attributesJSON = string(attributesBytes)- }- }+type exceptionTrendRow struct {+ Hash string `lit:"exception_hash"`+ Hour string `lit:"hour"`+ Count uint64 `lit:"count"`+}- isMessage := 0- if est.IsMessage {- isMessage = 1- }+func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModelWithNaming[exceptionRow](driver, exceptionRowNaming{})+ lit.RegisterModel[exceptionGroupRow](driver)+ lit.RegisterModel[exceptionTrendRow](driver)+ })+}- traceType := est.TraceType- if traceType == "" {- traceType = "endpoint"- }+func exceptionToRow(est models.ExceptionStackTrace) exceptionRow {+ traceType := est.TraceType+ if traceType == "" {+ traceType = "endpoint"+ }+ return exceptionRow{+ Id: est.Id,+ ProjectId: est.ProjectId,+ TraceId: est.TraceId,+ TraceType: traceType,+ ExceptionHash: est.ExceptionHash,+ StackTrace: est.StackTrace,+ RecordedAt: NewSQLiteTime(est.RecordedAt),+ Attributes: NewSQLiteJSONMap(est.Attributes),+ AppVersion: est.AppVersion,+ ServerName: est.ServerName,+ IsMessage: est.IsMessage,+ DistributedTraceId: est.DistributedTraceId,+ }+}- traceIdStr := ""- if est.TraceId != nil {- traceIdStr = est.TraceId.String()- }+func (r *exceptionRow) toModel() models.ExceptionStackTrace {+ est := models.ExceptionStackTrace{+ Id: r.Id,+ ProjectId: r.ProjectId,+ TraceId: r.TraceId,+ TraceType: r.TraceType,+ ExceptionHash: r.ExceptionHash,+ StackTrace: r.StackTrace,+ RecordedAt: r.RecordedAt.Time,+ AppVersion: r.AppVersion,+ ServerName: r.ServerName,+ IsMessage: r.IsMessage,+ DistributedTraceId: r.DistributedTraceId,+ }+ if r.Attributes != nil {+ est.Attributes = map[string]string(r.Attributes)+ }+ return est+}- var distributedTraceIdStr *string- if est.DistributedTraceId != nil {- s := est.DistributedTraceId.String()- distributedTraceIdStr = &s- }+type exceptionStackTraceRepository struct{}++func (e *exceptionStackTraceRepository) InsertAsync(ctx context.Context, lines []models.ExceptionStackTrace) error {+ if len(lines) == 0 {+ return nil+ }- if _, err := stmt.ExecContext(ctx,- est.Id.String(),- est.ProjectId.String(),- traceIdStr,- traceType,- est.ExceptionHash,- est.StackTrace,- est.RecordedAt.UTC().Format(time.RFC3339Nano),- attributesJSON,- est.AppVersion,- est.ServerName,- isMessage,- distributedTraceIdStr,- ); err != nil {+ for _, est := range lines {+ row := exceptionToRow(est)+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {return err}}- return tx.Commit()+ return nil}func (e *exceptionStackTraceRepository) CountBetween(ctx context.Context, projectId uuid.UUID, start, end time.Time) (int64, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM exception_stack_traces WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)).Scan(&count)- return count, err+ result, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM exception_stack_traces WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return 0, err+ }+ if result == nil {+ return 0, nil+ }+ return int64(result.Count), nil}func (e *exceptionStackTraceRepository) FindGrouped(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy string, search string, searchType string, includeArchived bool) ([]models.ExceptionGroup, int64, error) {@@ -104,23 +140,18 @@ func (e *exceptionStackTraceRepository) FindGrouped(ctx context.Context, projectsortDirection = "ASC"}- allowedOrderBy := map[string]bool{- "last_seen": true,- "first_seen": true,- "count": true,- }+ allowedOrderBy := map[string]bool{"last_seen": true, "first_seen": true, "count": true}if !allowedOrderBy[orderBy] {orderBy = "count"}- whereClause := "e.project_id = ? AND e.recorded_at >= ? AND e.recorded_at <= ?"- args := []interface{}{projectId.String(), fromDate.UTC().Format(time.RFC3339Nano), toDate.UTC().Format(time.RFC3339Nano)}+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}+ whereClause := "e.project_id = :project_id AND e.recorded_at >= :from AND e.recorded_at <= :to"if search != "" {- whereClause += " AND INSTR(LOWER(e.stack_trace), LOWER(?)) > 0"- args = append(args, search)+ whereClause += " AND INSTR(LOWER(e.stack_trace), LOWER(:search)) > 0"+ params["search"] = search}-if searchType == "issues" {whereClause += " AND e.is_message = 0"} else if searchType == "messages" {@@ -134,52 +165,50 @@ func (e *exceptionStackTraceRepository) FindGrouped(ctx context.Context, projectarchiveSubquery := `LEFT JOIN (SELECT exception_hash, MAX(archived_at) as archived_at- FROM archived_exceptions- WHERE project_id = ?+ FROM archived_exceptions WHERE project_id = :archive_project_idGROUP BY exception_hash) a ON e.exception_hash = a.exception_hash`+ params["archive_project_id"] = projectId- countQuery := `SELECT COUNT(*) FROM (+ countQuery := `SELECT COUNT(*) AS count FROM (SELECT e.exception_hash, MAX(a.archived_at) as max_archived_at- FROM exception_stack_traces e- ` + archiveSubquery + `+ FROM exception_stack_traces e ` + archiveSubquery + `WHERE ` + whereClause + `- GROUP BY e.exception_hash` + havingClause + `- )`+ GROUP BY e.exception_hash` + havingClause + `)`- countArgs := append([]interface{}{projectId.String()}, args...)- var count int64- err := db.DB.QueryRowContext(ctx, countQuery, countArgs...).Scan(&count)+ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB, countQuery, params)if err != nil {return nil, 0, err}+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }fullQuery := `SELECT e.exception_hash, e.stack_trace, MAX(e.recorded_at) as last_seen, MIN(e.recorded_at) as first_seen, COUNT(*) as count, MAX(a.archived_at) as max_archived_at- FROM exception_stack_traces e- ` + archiveSubquery + `+ FROM exception_stack_traces e ` + archiveSubquery + `WHERE ` + whereClause + `GROUP BY e.exception_hash` + havingClause + `- ORDER BY ` + orderBy + ` ` + sortDirection + ` LIMIT ? OFFSET ?`+ ORDER BY ` + orderBy + ` ` + sortDirection + ` LIMIT :limit OFFSET :offset`+ params["limit"] = pageSize+ params["offset"] = offset- queryArgs := append([]interface{}{projectId.String()}, args...)- queryArgs = append(queryArgs, pageSize, offset)- rows, err := db.DB.QueryContext(ctx, fullQuery, queryArgs...)+ groupRows, err := lit.SelectNamed[exceptionGroupRow](db.TelemetryDB, fullQuery, params)if err != nil {return nil, 0, err}- defer rows.Close()- var groups []models.ExceptionGroup- for rows.Next() {- var g models.ExceptionGroup- var lastSeenStr, firstSeenStr string- var maxArchivedAt sql.NullString- if err := rows.Scan(&g.ExceptionHash, &g.StackTrace, &lastSeenStr, &firstSeenStr, &g.Count, &maxArchivedAt); err != nil {- return nil, 0, err- }- g.LastSeen, _ = time.Parse(time.RFC3339Nano, lastSeenStr)- g.FirstSeen, _ = time.Parse(time.RFC3339Nano, firstSeenStr)- groups = append(groups, g)+ groups := make([]models.ExceptionGroup, 0, len(groupRows))+ for _, g := range groupRows {+ lastSeen, _ := time.Parse(time.RFC3339Nano, g.LastSeen)+ firstSeen, _ := time.Parse(time.RFC3339Nano, g.FirstSeen)+ groups = append(groups, models.ExceptionGroup{+ ExceptionHash: g.ExceptionHash,+ StackTrace: g.StackTrace,+ LastSeen: lastSeen,+ FirstSeen: firstSeen,+ Count: g.Count,+ })}return groups, count, nil@@ -187,105 +216,73 @@ func (e *exceptionStackTraceRepository) FindGrouped(ctx context.Context, projectfunc (e *exceptionStackTraceRepository) FindByHash(ctx context.Context, projectId uuid.UUID, exceptionHash string, page, pageSize int) (*models.ExceptionGroup, []models.ExceptionStackTrace, int64, error) {offset := (page - 1) * pageSize+ params := lit.P{"project_id": projectId, "exception_hash": exceptionHash}- var group models.ExceptionGroup- var lastSeenStr, firstSeenStr string- err := db.DB.QueryRowContext(ctx,- `SELECT exception_hash, stack_trace, MAX(recorded_at) as last_seen, MIN(recorded_at) as first_seen, COUNT(*) as count- FROM exception_stack_traces- WHERE project_id = ? AND exception_hash = ?+ groupRow, err := lit.SelectSingleNamed[exceptionGroupRow](db.TelemetryDB,+ `SELECT exception_hash, stack_trace, MAX(recorded_at) as last_seen, MIN(recorded_at) as first_seen, COUNT(*) as count, NULL as max_archived_at+ FROM exception_stack_traces WHERE project_id = :project_id AND exception_hash = :exception_hashGROUP BY exception_hash`,- projectId.String(), exceptionHash).Scan(&group.ExceptionHash, &group.StackTrace, &lastSeenStr, &firstSeenStr, &group.Count)+ params)if err != nil {if errors.Is(err, sql.ErrNoRows) {return nil, nil, 0, nil}return nil, nil, 0, err}- group.LastSeen, _ = time.Parse(time.RFC3339Nano, lastSeenStr)- group.FirstSeen, _ = time.Parse(time.RFC3339Nano, firstSeenStr)+ if groupRow == nil {+ return nil, nil, 0, nil+ }- rows, err := db.DB.QueryContext(ctx,+ lastSeen, _ := time.Parse(time.RFC3339Nano, groupRow.LastSeen)+ firstSeen, _ := time.Parse(time.RFC3339Nano, groupRow.FirstSeen)+ group := &models.ExceptionGroup{+ ExceptionHash: groupRow.ExceptionHash,+ StackTrace: groupRow.StackTrace,+ LastSeen: lastSeen,+ FirstSeen: firstSeen,+ Count: groupRow.Count,+ }++ rows, err := lit.SelectNamed[exceptionRow](db.TelemetryDB,`SELECT id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id- FROM exception_stack_traces- WHERE project_id = ? AND exception_hash = ?- ORDER BY recorded_at DESC LIMIT ? OFFSET ?`,- projectId.String(), exceptionHash, pageSize, offset)+ FROM exception_stack_traces WHERE project_id = :project_id AND exception_hash = :exception_hash+ ORDER BY recorded_at DESC LIMIT :limit OFFSET :offset`,+ lit.P{"project_id": projectId, "exception_hash": exceptionHash, "limit": pageSize, "offset": offset})if err != nil {return nil, nil, 0, err}- defer rows.Close()- var occurrences []models.ExceptionStackTrace- for rows.Next() {- o := scanExceptionStackTrace(rows)- if o == nil {- return nil, nil, 0, fmt.Errorf("failed to scan exception stack trace row")- }- occurrences = append(occurrences, *o)+ occurrences := make([]models.ExceptionStackTrace, 0, len(rows))+ for _, row := range rows {+ occurrences = append(occurrences, row.toModel())}- return &group, occurrences, int64(group.Count), nil+ return group, occurrences, int64(group.Count), nil}func (e *exceptionStackTraceRepository) CountByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- CAST(COUNT(*) AS REAL) as count- FROM exception_stack_traces- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- rows, err := db.DB.QueryContext(ctx, query, projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano))+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM exception_stack_traces WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var p models.TimeSeriesPoint- var tsStr string- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *exceptionStackTraceRepository) CountByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {intervalSeconds := intervalMinutes * 60-- query := `SELECT- datetime((strftime('%s', recorded_at) / ?) * ?, 'unixepoch') as bucket,- CAST(COUNT(*) AS REAL) as count- FROM exception_stack_traces- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`-- rows, err := db.DB.QueryContext(ctx, query, intervalSeconds, intervalSeconds, projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano))+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM exception_stack_traces WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, intervalSeconds, intervalSeconds),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var p models.TimeSeriesPoint- var tsStr string- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *exceptionStackTraceRepository) GetHourlyTrendForHashes(ctx context.Context, projectId uuid.UUID, hashes []string, start, end time.Time) (map[string][]models.ExceptionTrendPoint, error) {@@ -293,38 +290,32 @@ func (e *exceptionStackTraceRepository) GetHourlyTrendForHashes(ctx context.Contreturn make(map[string][]models.ExceptionTrendPoint), nil}- placeholders := strings.TrimRight(strings.Repeat("?,", len(hashes)), ",")-- query := `SELECT- exception_hash,- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- COUNT(*) as count- FROM exception_stack_traces- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ? AND exception_hash IN (` + placeholders + `)- GROUP BY exception_hash, hour- ORDER BY exception_hash, hour ASC`-- args := []interface{}{projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)}- for _, h := range hashes {- args = append(args, h)+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}+ placeholders := make([]string, len(hashes))+ for i, h := range hashes {+ key := fmt.Sprintf("hash_%d", i)+ placeholders[i] = ":" + key+ params[key] = h}- rows, err := db.DB.QueryContext(ctx, query, args...)+ query := `SELECT exception_hash, strftime('%Y-%m-%d %H:00:00', recorded_at) as hour, COUNT(*) as count+ FROM exception_stack_traces+ WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ AND exception_hash IN (` + strings.Join(placeholders, ",") + `)+ GROUP BY exception_hash, hour ORDER BY exception_hash, hour ASC`++ trendRows, err := lit.SelectNamed[exceptionTrendRow](db.TelemetryDB, query, params)if err != nil {return nil, err}- defer rows.Close()result := make(map[string][]models.ExceptionTrendPoint)- for rows.Next() {- var hash string- var point models.ExceptionTrendPoint- var tsStr string- if err := rows.Scan(&hash, &tsStr, &point.Count); err != nil {- return nil, err- }- point.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- result[hash] = append(result[hash], point)+ for _, row := range trendRows {+ ts, _ := time.Parse("2006-01-02 15:04:05", row.Hour)+ result[row.Hash] = append(result[row.Hash], models.ExceptionTrendPoint{+ Timestamp: ts,+ Count: row.Count,+ })}return result, nil@@ -335,27 +326,20 @@ func (e *exceptionStackTraceRepository) ArchiveByHashes(ctx context.Context, proreturn nil}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- stmt, err := tx.PrepareContext(ctx,- "INSERT OR REPLACE INTO archived_exceptions (project_id, exception_hash, archived_at) VALUES (?, ?, ?)")- if err != nil {- return err- }- defer stmt.Close()-- now := time.Now().UTC().Format(time.RFC3339Nano)+ now := NewSQLiteTime(time.Now().UTC())for _, hash := range hashes {- if _, err := stmt.ExecContext(ctx, projectId.String(), hash, now); err != nil {+ query, args, err := lit.ParseNamedQuery(db.Driver,+ "INSERT OR REPLACE INTO archived_exceptions (project_id, exception_hash, archived_at) VALUES (:project_id, :exception_hash, :archived_at)",+ lit.P{"project_id": projectId, "exception_hash": hash, "archived_at": now})+ if err != nil {+ return err+ }+ if _, err := db.TelemetryDB.ExecContext(ctx, query, args...); err != nil {return err}}- return tx.Commit()+ return nil}func (e *exceptionStackTraceRepository) UnarchiveByHashes(ctx context.Context, projectId uuid.UUID, hashes []string) error {@@ -363,183 +347,118 @@ func (e *exceptionStackTraceRepository) UnarchiveByHashes(ctx context.Context, preturn nil}- placeholders := strings.TrimRight(strings.Repeat("?,", len(hashes)), ",")- query := "DELETE FROM archived_exceptions WHERE project_id = ? AND exception_hash IN (" + placeholders + ")"+ params := lit.P{"project_id": projectId}+ placeholders := make([]string, len(hashes))+ for i, h := range hashes {+ key := fmt.Sprintf("hash_%d", i)+ placeholders[i] = ":" + key+ params[key] = h+ }- args := []interface{}{projectId.String()}- for _, h := range hashes {- args = append(args, h)+ query := "DELETE FROM archived_exceptions WHERE project_id = :project_id AND exception_hash IN (" + strings.Join(placeholders, ",") + ")"+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return err}- _, err := db.DB.ExecContext(ctx, query, args...)+ _, err = db.TelemetryDB.ExecContext(ctx, parsedQuery, args...)return err}func (e *exceptionStackTraceRepository) IsArchived(ctx context.Context, projectId uuid.UUID, hash string) (bool, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM archived_exceptions WHERE project_id = ? AND exception_hash = ?",- projectId.String(), hash).Scan(&count)+ result, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM archived_exceptions WHERE project_id = :project_id AND exception_hash = :exception_hash",+ lit.P{"project_id": projectId, "exception_hash": hash})if err != nil {return false, err}- return count > 0, nil+ if result == nil {+ return false, nil+ }+ return result.Count > 0, nil}func (e *exceptionStackTraceRepository) FindExceptionByTraceId(ctx context.Context, projectId uuid.UUID, traceId uuid.UUID) (*models.ExceptionStackTrace, error) {- row := db.DB.QueryRowContext(ctx,+ row, err := lit.SelectSingleNamed[exceptionRow](db.TelemetryDB,`SELECT id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id- FROM exception_stack_traces- WHERE project_id = ? AND trace_id = ? AND is_message = 0- LIMIT 1`,- projectId.String(), traceId.String())-- est := scanExceptionStackTraceRow(row)- return est, nil+ FROM exception_stack_traces WHERE project_id = :project_id AND trace_id = :trace_id AND is_message = 0 LIMIT 1`,+ lit.P{"project_id": projectId, "trace_id": traceId})+ if err != nil {+ return nil, err+ }+ if row == nil {+ return nil, nil+ }+ est := row.toModel()+ return &est, nil}func (e *exceptionStackTraceRepository) FindAllByTraceId(ctx context.Context, projectId uuid.UUID, traceId uuid.UUID) ([]models.ExceptionStackTrace, error) {- rows, err := db.DB.QueryContext(ctx,+ rows, err := lit.SelectNamed[exceptionRow](db.TelemetryDB,`SELECT id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id- FROM exception_stack_traces- WHERE project_id = ? AND trace_id = ?- ORDER BY recorded_at ASC`,- projectId.String(), traceId.String())+ FROM exception_stack_traces WHERE project_id = :project_id AND trace_id = :trace_id ORDER BY recorded_at ASC`,+ lit.P{"project_id": projectId, "trace_id": traceId})if err != nil {return nil, err}- defer rows.Close()- var results []models.ExceptionStackTrace- for rows.Next() {- o := scanExceptionStackTrace(rows)- if o == nil {- return nil, fmt.Errorf("failed to scan exception stack trace row")- }- results = append(results, *o)+ results := make([]models.ExceptionStackTrace, 0, len(rows))+ for _, row := range rows {+ results = append(results, row.toModel())}-return results, nil}func (e *exceptionStackTraceRepository) FindById(ctx context.Context, projectId uuid.UUID, id uuid.UUID) (*models.ExceptionStackTrace, error) {- row := db.DB.QueryRowContext(ctx,+ row, err := lit.SelectSingleNamed[exceptionRow](db.TelemetryDB,`SELECT id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id- FROM exception_stack_traces- WHERE project_id = ? AND id = ?- LIMIT 1`,- projectId.String(), id.String())-- est := scanExceptionStackTraceRow(row)- if est == nil {- return nil, nil- }- return est, nil-}--func scanExceptionStackTraceRow(row *sql.Row) *models.ExceptionStackTrace {- var est models.ExceptionStackTrace- var idStr, projectIdStr, traceIdStr string- var recordedAtStr, attributesJSON string- var isMessage int- var distributedTraceIdStr sql.NullString-- err := row.Scan(&idStr, &projectIdStr, &traceIdStr, &est.TraceType, &est.ExceptionHash, &est.StackTrace,- &recordedAtStr, &attributesJSON, &est.AppVersion, &est.ServerName, &isMessage, &distributedTraceIdStr)+ FROM exception_stack_traces WHERE project_id = :project_id AND id = :id LIMIT 1`,+ lit.P{"project_id": projectId, "id": id})if err != nil {- return nil- }-- est.Id, _ = uuid.Parse(idStr)- est.ProjectId, _ = uuid.Parse(projectIdStr)- if traceIdStr != "" {- parsed, err := uuid.Parse(traceIdStr)- if err == nil {- est.TraceId = &parsed- }- }- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- est.DistributedTraceId = &parsed- }- }- est.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- est.IsMessage = isMessage == 1-- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &est.Attributes); err != nil {- est.Attributes = nil- }- }-- return &est-}--func scanExceptionStackTrace(rows *sql.Rows) *models.ExceptionStackTrace {- var est models.ExceptionStackTrace- var idStr, projectIdStr, traceIdStr string- var recordedAtStr, attributesJSON string- var isMessage int- var distributedTraceIdStr sql.NullString-- err := rows.Scan(&idStr, &projectIdStr, &traceIdStr, &est.TraceType, &est.ExceptionHash, &est.StackTrace,- &recordedAtStr, &attributesJSON, &est.AppVersion, &est.ServerName, &isMessage, &distributedTraceIdStr)- if err != nil {- return nil- }-- est.Id, _ = uuid.Parse(idStr)- est.ProjectId, _ = uuid.Parse(projectIdStr)- if traceIdStr != "" {- parsed, err := uuid.Parse(traceIdStr)- if err == nil {- est.TraceId = &parsed- }- }- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- est.DistributedTraceId = &parsed- }+ return nil, err}- est.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- est.IsMessage = isMessage == 1-- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &est.Attributes); err != nil {- est.Attributes = nil- }+ if row == nil {+ return nil, nil}-- return &est+ est := row.toModel()+ return &est, nil}func (e *exceptionStackTraceRepository) FindByDistributedTraceId(ctx context.Context, distributedTraceId uuid.UUID, projectIds []uuid.UUID) ([]models.ExceptionStackTrace, error) {if len(projectIds) == 0 {return nil, nil}- placeholders := strings.TrimRight(strings.Repeat("?,", len(projectIds)), ",")+ params := lit.P{"trace_id": distributedTraceId}+ placeholders := make([]string, len(projectIds))+ for i, pid := range projectIds {+ key := fmt.Sprintf("pid_%d", i)+ placeholders[i] = ":" + key+ params[key] = pid+ }+query := `SELECT id, project_id, trace_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, distributed_trace_id- FROM exception_stack_traces- WHERE distributed_trace_id = ? AND project_id IN (` + placeholders + `) AND is_message = 0+ FROM exception_stack_traces WHERE distributed_trace_id = :trace_id AND project_id IN (` + strings.Join(placeholders, ",") + `) AND is_message = 0ORDER BY recorded_at ASC`- args := []interface{}{distributedTraceId.String()}- for _, pid := range projectIds {- args = append(args, pid.String())++ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return nil, err}- rows, err := db.QueryerFromContext(ctx).QueryContext(ctx, query, args...)++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}- defer rows.Close()+ defer sqlRows.Close()+var results []models.ExceptionStackTrace- for rows.Next() {- o := scanExceptionStackTrace(rows)- if o == nil {- return nil, fmt.Errorf("failed to scan exception stack trace row")+ for sqlRows.Next() {+ var row exceptionRow+ if err := sqlRows.Scan(&row.Id, &row.ProjectId, &row.TraceId, &row.TraceType, &row.ExceptionHash, &row.StackTrace,+ &row.RecordedAt, &row.Attributes, &row.AppVersion, &row.ServerName, &row.IsMessage, &row.DistributedTraceId); err != nil {+ return nil, err}- results = append(results, *o)+ results = append(results, row.toModel())}return results, nil}diff --git a/backend/app/repositories/fired_notification.repository_sqlite.go b/backend/app/repositories/fired_notification.repository_sqlite.goindex 0b8031af..b19b7af9 100644--- a/backend/app/repositories/fired_notification.repository_sqlite.go+++ b/backend/app/repositories/fired_notification.repository_sqlite.go@@ -7,7 +7,9 @@ import ("time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db"+ "github.com/tracewayapp/traceway/backend/app/models")type FiredNotification struct {@@ -26,16 +28,69 @@ type FiredNotification struct {FiredAt time.Time}+type firedNotificationRow struct {+ ProjectId uuid.UUID `lit:"project_id"`+ RuleId int `lit:"rule_id"`+ RuleType string `lit:"rule_type"`+ RuleName string `lit:"rule_name"`+ ChannelType string `lit:"channel_type"`+ ChannelName string `lit:"channel_name"`+ Severity string `lit:"severity"`+ Subject string `lit:"subject"`+ Body string `lit:"body"`+ Status string `lit:"status"`+ ErrorMsg string `lit:"error_message"`+ Endpoint string `lit:"endpoint"`+ FiredAt SQLiteTime `lit:"fired_at"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[firedNotificationRow](driver)+ })+}+type firedNotificationRepository struct{}func (r *firedNotificationRepository) Insert(ctx context.Context, n FiredNotification) error {- _, err := db.DB.ExecContext(ctx,+ row := firedNotificationRow{+ ProjectId: n.ProjectId,+ RuleId: n.RuleId,+ RuleType: n.RuleType,+ RuleName: n.RuleName,+ ChannelType: n.ChannelType,+ ChannelName: n.ChannelName,+ Severity: n.Severity,+ Subject: n.Subject,+ Body: n.Body,+ Status: n.Status,+ ErrorMsg: n.ErrorMsg,+ Endpoint: n.Endpoint,+ FiredAt: NewSQLiteTime(n.FiredAt),+ }++ query, args, err := lit.ParseNamedQuery(db.Driver,`INSERT INTO fired_notifications (project_id, rule_id, rule_type, rule_name, channel_type, channel_name, severity, subject, body, status, error_message, endpoint, fired_at)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,- n.ProjectId.String(), n.RuleId, n.RuleType, n.RuleName,- n.ChannelType, n.ChannelName, n.Severity, n.Subject, n.Body,- n.Status, n.ErrorMsg, n.Endpoint, n.FiredAt.UTC().Format(time.RFC3339Nano),- )+ VALUES (:project_id, :rule_id, :rule_type, :rule_name, :channel_type, :channel_name, :severity, :subject, :body, :status, :error_message, :endpoint, :fired_at)`,+ lit.P{+ "project_id": row.ProjectId,+ "rule_id": row.RuleId,+ "rule_type": row.RuleType,+ "rule_name": row.RuleName,+ "channel_type": row.ChannelType,+ "channel_name": row.ChannelName,+ "severity": row.Severity,+ "subject": row.Subject,+ "body": row.Body,+ "status": row.Status,+ "error_message": row.ErrorMsg,+ "endpoint": row.Endpoint,+ "fired_at": row.FiredAt,+ })+ if err != nil {+ return err+ }+ _, err = db.TelemetryDB.ExecContext(ctx, query, args...)return err}diff --git a/backend/app/repositories/metric_point.repository_sqlite.go b/backend/app/repositories/metric_point.repository_sqlite.goindex ce8ccf2e..bd2a62c0 100644--- a/backend/app/repositories/metric_point.repository_sqlite.go+++ b/backend/app/repositories/metric_point.repository_sqlite.go@@ -4,89 +4,103 @@ package repositoriesimport ("context"- "encoding/json""fmt""strings""time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")type metricPointRepository struct{}+type avgResult struct {+ Value float64 `lit:"agg_value"`+}++type distinctServerResult struct {+ ServerName string `lit:"sn"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[avgResult](driver)+ lit.RegisterModel[distinctServerResult](driver)+ })+}+func (r *metricPointRepository) InsertAsync(ctx context.Context, points []models.MetricPoint) error {if len(points) == 0 {return nil}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- stmt, err := tx.PrepareContext(ctx, "INSERT INTO metric_points (project_id, name, value, tags, recorded_at) VALUES (?, ?, ?, ?, ?)")- if err != nil {- return err- }- defer stmt.Close()-for _, p := range points {- tags := p.Tags- if tags == nil {- tags = map[string]string{}- }- tagsJSON, err := json.Marshal(tags)+ tags := NewSQLiteJSONMap(p.Tags)+ tagsVal, _ := tags.Value()+ query, args, err := lit.ParseNamedQuery(db.Driver,+ "INSERT INTO metric_points (project_id, name, value, tags, recorded_at) VALUES (:project_id, :name, :value, :tags, :recorded_at)",+ lit.P{+ "project_id": p.ProjectId,+ "name": p.Name,+ "value": p.Value,+ "tags": tagsVal,+ "recorded_at": NewSQLiteTime(p.RecordedAt),+ })if err != nil {return err}-- if _, err := stmt.ExecContext(ctx,- p.ProjectId.String(),- p.Name,- p.Value,- string(tagsJSON),- p.RecordedAt.UTC().Format(time.RFC3339Nano),- ); err != nil {+ if _, err := db.TelemetryDB.ExecContext(ctx, query, args...); err != nil {return err}}- return tx.Commit()+ return nil}func (r *metricPointRepository) QueryTimeSeries(ctx context.Context, projectId uuid.UUID, name string, from, to time.Time, intervalMinutes int, aggregation string, tagFilters map[string]string, groupBy string) (map[string][]models.TimeSeriesPoint, error) {secs := intervalMinutes * 60-aggFunc := sqliteAggregationFunc(aggregation)-- query := fmt.Sprintf("SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') AS bucket", secs, secs)-- args := []interface{}{}-hasGroupBy := groupBy != ""++ selectClause := fmt.Sprintf("SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') AS bucket", secs, secs)if hasGroupBy {- query += ", json_extract(tags, '$.' || ?) AS group_key"- args = append(args, groupBy)+ selectClause += ", json_extract(tags, '$.' || :group_by) AS group_key"}+ selectClause += ", " + aggFunc + " AS agg_value FROM metric_points WHERE project_id = :project_id AND name = :name AND recorded_at >= :from AND recorded_at <= :to"- query += ", " + aggFunc + " AS agg_value FROM metric_points WHERE project_id = ? AND name = ? AND recorded_at >= ? AND recorded_at <= ?"- args = append(args, projectId.String(), name, from.UTC().Format(time.RFC3339Nano), to.UTC().Format(time.RFC3339Nano))+ params := lit.P{+ "project_id": projectId,+ "name": name,+ "from": NewSQLiteTime(from),+ "to": NewSQLiteTime(to),+ }+ if hasGroupBy {+ params["group_by"] = groupBy+ }- for k, v := range tagFilters {- query += " AND json_extract(tags, '$.' || ?) = ?"- args = append(args, k, v)+ filterClauses := ""+ for i, k := range sortedKeys(tagFilters) {+ fk := fmt.Sprintf("fk_%d", i)+ fv := fmt.Sprintf("fv_%d", i)+ filterClauses += fmt.Sprintf(" AND json_extract(tags, '$.' || :%s) = :%s", fk, fv)+ params[fk] = k+ params[fv] = tagFilters[k]}- query += " GROUP BY bucket"+ query := selectClause + filterClauses + " GROUP BY bucket"if hasGroupBy {query += ", group_key"}query += " ORDER BY bucket ASC"- rows, err := db.DB.QueryContext(ctx, query, args...)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return nil, err+ }++ rows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}@@ -129,48 +143,42 @@ func (r *metricPointRepository) QueryTimeSeries(ctx context.Context, projectId u}func (r *metricPointRepository) DiscoverMetrics(ctx context.Context, projectId uuid.UUID, from, to time.Time) ([]models.DiscoveredMetric, error) {- nameRows, err := db.DB.QueryContext(ctx,- "SELECT DISTINCT name FROM metric_points WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY name ASC",- projectId.String(), from.UTC().Format(time.RFC3339Nano), to.UTC().Format(time.RFC3339Nano))+ type nameRow struct {+ Name string `lit:"name"`+ }+ lit.RegisterModel[nameRow](db.Driver)++ names, err := lit.SelectNamed[nameRow](db.TelemetryDB,+ "SELECT DISTINCT name FROM metric_points WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to ORDER BY name ASC",+ lit.P{"project_id": projectId, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)})if err != nil {return nil, err}- defer nameRows.Close()- var names []string- for nameRows.Next() {- var name string- if err := nameRows.Scan(&name); err != nil {- return nil, err+ var metrics []models.DiscoveredMetric+ for _, n := range names {+ type keyRow struct {+ Key string `lit:"key"`}- names = append(names, name)- }+ lit.RegisterModel[keyRow](db.Driver)- var metrics []models.DiscoveredMetric- for _, name := range names {- tagRows, err := db.DB.QueryContext(ctx,+ tagKeys, err := lit.SelectNamed[keyRow](db.TelemetryDB,`SELECT DISTINCT j.key FROM metric_points, json_each(tags) j- WHERE project_id = ? AND name = ? AND recorded_at >= ? AND recorded_at <= ?+ WHERE project_id = :project_id AND name = :name AND recorded_at >= :from AND recorded_at <= :toORDER BY j.key ASC`,- projectId.String(), name, from.UTC().Format(time.RFC3339Nano), to.UTC().Format(time.RFC3339Nano))+ lit.P{"project_id": projectId, "name": n.Name, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)})if err != nil {return nil, err}- var tagKeys []string- for tagRows.Next() {- var key string- if err := tagRows.Scan(&key); err != nil {- tagRows.Close()- return nil, err- }- tagKeys = append(tagKeys, key)+ keys := make([]string, 0, len(tagKeys))+ for _, k := range tagKeys {+ keys = append(keys, k.Key)}- tagRows.Close()metrics = append(metrics, models.DiscoveredMetric{- Name: name,- TagKeys: tagKeys,+ Name: n.Name,+ TagKeys: keys,})}@@ -178,59 +186,59 @@ func (r *metricPointRepository) DiscoverMetrics(ctx context.Context, projectId u}func (r *metricPointRepository) DiscoverTagValues(ctx context.Context, projectId uuid.UUID, metricName, tagKey string, from, to time.Time) ([]string, error) {- rows, err := db.DB.QueryContext(ctx,- `SELECT DISTINCT json_extract(tags, '$.' || ?) AS tag_value+ type tagValueRow struct {+ TagValue string `lit:"tag_value"`+ }+ lit.RegisterModel[tagValueRow](db.Driver)++ results, err := lit.SelectNamed[tagValueRow](db.TelemetryDB,+ `SELECT DISTINCT json_extract(tags, '$.' || :tag_key) AS tag_valueFROM metric_points- WHERE project_id = ? AND name = ? AND recorded_at >= ? AND recorded_at <= ?- AND json_extract(tags, '$.' || ?) IS NOT NULL- AND json_extract(tags, '$.' || ?) != ''+ WHERE project_id = :project_id AND name = :name AND recorded_at >= :from AND recorded_at <= :to+ AND json_extract(tags, '$.' || :tag_key) IS NOT NULL+ AND json_extract(tags, '$.' || :tag_key) != ''ORDER BY tag_value ASC`,- tagKey, projectId.String(), metricName, from.UTC().Format(time.RFC3339Nano), to.UTC().Format(time.RFC3339Nano), tagKey, tagKey)+ lit.P{"project_id": projectId, "name": metricName, "tag_key": tagKey, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)})if err != nil {return nil, err}- defer rows.Close()- var values []string- for rows.Next() {- var v string- if err := rows.Scan(&v); err != nil {- return nil, err- }- values = append(values, v)+ values := make([]string, 0, len(results))+ for _, r := range results {+ values = append(values, r.TagValue)}return values, nil}func (r *metricPointRepository) GetAverageBetween(ctx context.Context, projectId uuid.UUID, name string, start, end time.Time) (float64, error) {- var avg float64- err := db.DB.QueryRowContext(ctx,- "SELECT COALESCE(avg(value), 0) FROM metric_points WHERE project_id = ? AND name = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), name, start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)).Scan(&avg)- return avg, err+ result, err := lit.SelectSingleNamed[avgResult](db.TelemetryDB,+ "SELECT COALESCE(avg(value), 0) AS agg_value FROM metric_points WHERE project_id = :project_id AND name = :name AND recorded_at >= :from AND recorded_at <= :to",+ lit.P{"project_id": projectId, "name": name, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return 0, err+ }+ if result == nil {+ return 0, nil+ }+ return result.Value, nil}func (r *metricPointRepository) GetDistinctServers(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]string, error) {- rows, err := db.DB.QueryContext(ctx,+ results, err := lit.SelectNamed[distinctServerResult](db.TelemetryDB,`SELECT DISTINCT json_extract(tags, '$.server_name') AS snFROM metric_points- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?+ WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :toAND json_extract(tags, '$.server_name') IS NOT NULLAND json_extract(tags, '$.server_name') != ''ORDER BY sn ASC`,- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano))+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()- var servers []string- for rows.Next() {- var s string- if err := rows.Scan(&s); err != nil {- return nil, err- }- servers = append(servers, s)+ servers := make([]string, 0, len(results))+ for _, r := range results {+ servers = append(servers, r.ServerName)}return servers, nil}@@ -238,20 +246,26 @@ func (r *metricPointRepository) GetDistinctServers(ctx context.Context, projectIfunc (r *metricPointRepository) GetAverageByIntervalPerServer(ctx context.Context, projectId uuid.UUID, name string, start, end time.Time, intervalMinutes int, servers []string) (map[string][]models.TimeSeriesPoint, error) {secs := intervalMinutes * 60+ params := lit.P{+ "project_id": projectId,+ "name": name,+ "from": NewSQLiteTime(start),+ "to": NewSQLiteTime(end),+ }+query := fmt.Sprintf(`SELECTdatetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') AS bucket,json_extract(tags, '$.server_name') AS sn,avg(value) AS avg_valueFROM metric_points- WHERE project_id = ? AND name = ? AND recorded_at >= ? AND recorded_at <= ?`, secs, secs)-- args := []interface{}{projectId.String(), name, start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)}+ WHERE project_id = :project_id AND name = :name AND recorded_at >= :from AND recorded_at <= :to`, secs, secs)if len(servers) > 0 {placeholders := make([]string, len(servers))for i, s := range servers {- placeholders[i] = "?"- args = append(args, s)+ key := fmt.Sprintf("srv_%d", i)+ placeholders[i] = ":" + key+ params[key] = s}query += " AND json_extract(tags, '$.server_name') IN (" + strings.Join(placeholders, ", ") + ")"} else {@@ -260,7 +274,12 @@ func (r *metricPointRepository) GetAverageByIntervalPerServer(ctx context.Contexquery += " GROUP BY bucket, sn ORDER BY bucket ASC, sn ASC"- rows, err := db.DB.QueryContext(ctx, query, args...)+ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return nil, err+ }++ rows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}@@ -303,4 +322,12 @@ func sqliteAggregationFunc(agg string) string {}}+func sortedKeys(m map[string]string) []string {+ keys := make([]string, 0, len(m))+ for k := range m {+ keys = append(keys, k)+ }+ return keys+}+var MetricPointRepository = metricPointRepository{}diff --git a/backend/app/repositories/session_recording.repository_sqlite.go b/backend/app/repositories/session_recording.repository_sqlite.goindex 25460d69..4c5c7ab9 100644--- a/backend/app/repositories/session_recording.repository_sqlite.go+++ b/backend/app/repositories/session_recording.repository_sqlite.go@@ -6,13 +6,27 @@ import ("context""database/sql""errors"- "time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")+type sessionRecording struct {+ Id uuid.UUID `lit:"id"`+ ProjectId uuid.UUID `lit:"project_id"`+ ExceptionId uuid.UUID `lit:"exception_id"`+ FilePath string `lit:"file_path"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[sessionRecording](driver)+ })+}+type sessionRecordingRepository struct{}func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordings []models.SessionRecording) error {@@ -20,42 +34,36 @@ func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordingsreturn nil}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- stmt, err := tx.PrepareContext(ctx, "INSERT INTO session_recordings (id, project_id, exception_id, file_path, recorded_at) VALUES (?, ?, ?, ?, ?)")- if err != nil {- return err- }- defer stmt.Close()-for _, rec := range recordings {- if _, err := stmt.ExecContext(ctx,- rec.Id.String(), rec.ProjectId.String(), rec.ExceptionId.String(),- rec.FilePath, rec.RecordedAt.UTC().Format(time.RFC3339Nano),- ); err != nil {+ row := sessionRecording{+ Id: rec.Id,+ ProjectId: rec.ProjectId,+ ExceptionId: rec.ExceptionId,+ FilePath: rec.FilePath,+ RecordedAt: NewSQLiteTime(rec.RecordedAt),+ }+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {return err}}- return tx.Commit()+ return nil}func (r *sessionRecordingRepository) FindByExceptionId(ctx context.Context, projectId uuid.UUID, exceptionId uuid.UUID) (string, error) {- var filePath string- err := db.DB.QueryRowContext(ctx,- "SELECT file_path FROM session_recordings WHERE project_id = ? AND exception_id = ? ORDER BY recorded_at DESC LIMIT 1",- projectId.String(), exceptionId.String()).Scan(&filePath)+ result, err := lit.SelectSingleNamed[filePathResult](db.TelemetryDB,+ "SELECT file_path FROM session_recordings WHERE project_id = :project_id AND exception_id = :exception_id ORDER BY recorded_at DESC LIMIT 1",+ lit.P{"project_id": projectId, "exception_id": exceptionId})if err != nil {- if errors.Is(err, sql.ErrNoRows) {- return "", sql.ErrNoRows- }return "", err}- return filePath, nil+ if result == nil {+ return "", sql.ErrNoRows+ }+ return result.FilePath, nil}+// Preserve original error contract: returns sql.ErrNoRows when not found+var _ = errors.Is+var SessionRecordingRepository = sessionRecordingRepository{}diff --git a/backend/app/repositories/span.repository_sqlite.go b/backend/app/repositories/span.repository_sqlite.goindex b378e388..d671529b 100644--- a/backend/app/repositories/span.repository_sqlite.go+++ b/backend/app/repositories/span.repository_sqlite.go@@ -7,70 +7,83 @@ import ("time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")-type spanRepository struct{}+type span struct {+ Id uuid.UUID `lit:"id"`+ TraceId uuid.UUID `lit:"trace_id"`+ ProjectId uuid.UUID `lit:"project_id"`+ Name string `lit:"name"`+ StartTime SQLiteTime `lit:"start_time"`+ Duration int64 `lit:"duration"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+}-func (r *spanRepository) InsertAsync(ctx context.Context, spans []models.Span) error {- if len(spans) == 0 {- return nil+func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[span](driver)+ })+}++func spanToRow(s models.Span) span {+ return span{+ Id: s.Id,+ TraceId: s.TraceId,+ ProjectId: s.ProjectId,+ Name: s.Name,+ StartTime: NewSQLiteTime(s.StartTime),+ Duration: int64(s.Duration),+ RecordedAt: NewSQLiteTime(s.RecordedAt),}+}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err+func (r *span) toModel() models.Span {+ return models.Span{+ Id: r.Id,+ TraceId: r.TraceId,+ ProjectId: r.ProjectId,+ Name: r.Name,+ StartTime: r.StartTime.Time,+ Duration: time.Duration(r.Duration),+ RecordedAt: r.RecordedAt.Time,}- defer tx.Rollback()+}- stmt, err := tx.PrepareContext(ctx, "INSERT INTO spans (id, trace_id, project_id, name, start_time, duration, recorded_at) VALUES (?, ?, ?, ?, ?, ?, ?)")- if err != nil {- return err+type spanRepository struct{}++func (r *spanRepository) InsertAsync(ctx context.Context, spans []models.Span) error {+ if len(spans) == 0 {+ return nil}- defer stmt.Close()for _, s := range spans {- if _, err := stmt.ExecContext(ctx,- s.Id.String(), s.TraceId.String(), s.ProjectId.String(),- s.Name, s.StartTime.UTC().Format(time.RFC3339Nano),- int64(s.Duration), s.RecordedAt.UTC().Format(time.RFC3339Nano),- ); err != nil {+ row := spanToRow(s)+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {return err}}- return tx.Commit()+ return nil}func (r *spanRepository) FindByTraceId(ctx context.Context, projectId, traceId uuid.UUID) ([]models.Span, error) {- rows, err := db.DB.QueryContext(ctx,+ rows, err := lit.SelectNamed[span](db.TelemetryDB,`SELECT id, trace_id, project_id, name, start_time, duration, recorded_atFROM spans- WHERE project_id = ? AND trace_id = ?+ WHERE project_id = :project_id AND trace_id = :trace_idORDER BY start_time ASC`,- projectId.String(), traceId.String())+ lit.P{"project_id": projectId, "trace_id": traceId})if err != nil {return nil, err}- defer rows.Close()- var spans []models.Span- for rows.Next() {- var s models.Span- var idStr, traceIdStr, projectIdStr string- var startTimeStr, recordedAtStr string- if err := rows.Scan(&idStr, &traceIdStr, &projectIdStr, &s.Name, &startTimeStr, &s.Duration, &recordedAtStr); err != nil {- return nil, err- }- s.Id, _ = uuid.Parse(idStr)- s.TraceId, _ = uuid.Parse(traceIdStr)- s.ProjectId, _ = uuid.Parse(projectIdStr)- s.StartTime, _ = time.Parse(time.RFC3339Nano, startTimeStr)- s.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- spans = append(spans, s)+ spans := make([]models.Span, 0, len(rows))+ for _, row := range rows {+ spans = append(spans, row.toModel())}-return spans, nil}diff --git a/backend/app/repositories/sqlite_types.go b/backend/app/repositories/sqlite_types.gonew file mode 100644index 00000000..8b91359f--- /dev/null+++ b/backend/app/repositories/sqlite_types.go@@ -0,0 +1,136 @@+//go:build !pgch++package repositories++import (+ "database/sql/driver"+ "encoding/json"+ "fmt"+ "time"++ "github.com/tracewayapp/lit/v2"+ "github.com/tracewayapp/traceway/backend/app/models"+)++// SQLiteTime handles scanning time.Time from SQLite DATETIME text columns+// and serializing time.Time for SQLite inserts.+type SQLiteTime struct {+ time.Time+}++func NewSQLiteTime(t time.Time) SQLiteTime {+ return SQLiteTime{Time: t}+}++func (t *SQLiteTime) Scan(src interface{}) error {+ switch v := src.(type) {+ case nil:+ t.Time = time.Time{}+ return nil+ case string:+ return t.parseString(v)+ case []byte:+ return t.parseString(string(v))+ case time.Time:+ t.Time = v+ return nil+ }+ return fmt.Errorf("SQLiteTime.Scan: unsupported type %T", src)+}++func (t *SQLiteTime) parseString(s string) error {+ for _, layout := range []string{time.RFC3339Nano, time.RFC3339, "2006-01-02 15:04:05", "2006-01-02T15:04:05Z"} {+ if parsed, err := time.Parse(layout, s); err == nil {+ t.Time = parsed+ return nil+ }+ }+ return fmt.Errorf("SQLiteTime: cannot parse %q", s)+}++func (t SQLiteTime) Value() (driver.Value, error) {+ return t.Time.UTC().Format(time.RFC3339Nano), nil+}++// SQLiteJSONMap handles scanning map[string]string from SQLite TEXT JSON columns+// and serializing map[string]string for SQLite inserts.+type SQLiteJSONMap map[string]string++func NewSQLiteJSONMap(m map[string]string) SQLiteJSONMap {+ if m == nil {+ return nil+ }+ return SQLiteJSONMap(m)+}++func (m *SQLiteJSONMap) Scan(src interface{}) error {+ if src == nil {+ *m = nil+ return nil+ }+ var data []byte+ switch v := src.(type) {+ case string:+ if v == "" || v == "{}" {+ *m = nil+ return nil+ }+ data = []byte(v)+ case []byte:+ if len(v) == 0 {+ *m = nil+ return nil+ }+ data = v+ default:+ return fmt.Errorf("SQLiteJSONMap.Scan: unsupported type %T", src)+ }+ return json.Unmarshal(data, m)+}++func (m SQLiteJSONMap) Value() (driver.Value, error) {+ if m == nil {+ return "{}", nil+ }+ b, err := json.Marshal(m)+ if err != nil {+ return "{}", err+ }+ return string(b), nil+}++// timeSeriesResult is a generic result type for all time-bucketed aggregation queries.+// Queries should alias the bucket as "bucket" and the value as "agg_value".+type timeSeriesResult struct {+ Bucket string `lit:"bucket"`+ Value float64 `lit:"agg_value"`+}++func timeSeriesResultsToPoints(results []*timeSeriesResult) []models.TimeSeriesPoint {+ points := make([]models.TimeSeriesPoint, 0, len(results))+ for _, r := range results {+ t, _ := time.Parse("2006-01-02 15:04:05", r.Bucket)+ points = append(points, models.TimeSeriesPoint{Timestamp: t, Value: r.Value})+ }+ return points+}++// groupedTimeSeriesResult is for time-bucketed queries with a group-by key.+type groupedTimeSeriesResult struct {+ Bucket string `lit:"bucket"`+ GroupKey *string `lit:"group_key"`+ Value float64 `lit:"agg_value"`+}++// filePathResult is for single-column file_path queries.+type filePathResult struct {+ FilePath string `lit:"file_path"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[timeSeriesResult](driver)+ lit.RegisterModel[groupedTimeSeriesResult](driver)+ lit.RegisterModel[filePathResult](driver)+ })+}diff --git a/backend/app/repositories/task.repository_sqlite.go b/backend/app/repositories/task.repository_sqlite.goindex 357deb7b..032ef9eb 100644--- a/backend/app/repositories/task.repository_sqlite.go+++ b/backend/app/repositories/task.repository_sqlite.go@@ -4,18 +4,88 @@ package repositoriesimport ("context"- "database/sql"- "encoding/json""fmt""sort""strings""time""github.com/google/uuid"+ "github.com/tracewayapp/lit/v2""github.com/tracewayapp/traceway/backend/app/db""github.com/tracewayapp/traceway/backend/app/models")+type task struct {+ Id uuid.UUID `lit:"id"`+ ProjectId uuid.UUID `lit:"project_id"`+ TaskName string `lit:"task_name"`+ Duration int64 `lit:"duration"`+ RecordedAt SQLiteTime `lit:"recorded_at"`+ ClientIP string `lit:"client_ip"`+ Attributes SQLiteJSONMap `lit:"attributes"`+ AppVersion string `lit:"app_version"`+ ServerName string `lit:"server_name"`+ DistributedTraceId *uuid.UUID `lit:"distributed_trace_id"`+}++type taskGroupRow struct {+ TaskName string `lit:"task_name"`+ Count uint64 `lit:"count"`+ AvgDuration float64 `lit:"avg_duration"`+ LastSeen string `lit:"last_seen"`+}++type taskCountStatsRow struct {+ Count int64 `lit:"count"`+ AvgDurMs float64 `lit:"avg_dur_ms"`+}++type durationValueRow struct {+ Duration float64 `lit:"duration"`+}++func init() {+ models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {+ lit.RegisterModel[task](driver)+ lit.RegisterModel[taskGroupRow](driver)+ lit.RegisterModel[taskCountStatsRow](driver)+ lit.RegisterModel[durationValueRow](driver)+ })+}++func taskToRow(t models.Task) task {+ return task{+ Id: t.Id,+ ProjectId: t.ProjectId,+ TaskName: t.TaskName,+ Duration: int64(t.Duration),+ RecordedAt: NewSQLiteTime(t.RecordedAt),+ ClientIP: t.ClientIP,+ Attributes: NewSQLiteJSONMap(t.Attributes),+ AppVersion: t.AppVersion,+ ServerName: t.ServerName,+ DistributedTraceId: t.DistributedTraceId,+ }+}++func (r *task) toModel() models.Task {+ t := models.Task{+ Id: r.Id,+ ProjectId: r.ProjectId,+ TaskName: r.TaskName,+ Duration: time.Duration(r.Duration),+ RecordedAt: r.RecordedAt.Time,+ ClientIP: r.ClientIP,+ AppVersion: r.AppVersion,+ ServerName: r.ServerName,+ DistributedTraceId: r.DistributedTraceId,+ }+ if r.Attributes != nil {+ t.Attributes = map[string]string(r.Attributes)+ }+ return t+}+type taskRepository struct{}func (e *taskRepository) InsertAsync(ctx context.Context, lines []models.Task) error {@@ -23,106 +93,78 @@ func (e *taskRepository) InsertAsync(ctx context.Context, lines []models.Task) ereturn nil}- tx, err := db.DB.BeginTx(ctx, nil)- if err != nil {- return err- }- defer tx.Rollback()-- stmt, err := tx.PrepareContext(ctx, "INSERT INTO tasks (id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")- if err != nil {- return err- }- defer stmt.Close()-for _, t := range lines {- attributesJSON := "{}"- if len(t.Attributes) != 0 {- if attributesBytes, err := json.Marshal(t.Attributes); err == nil {- attributesJSON = string(attributesBytes)- }- }- var distributedTraceIdStr *string- if t.DistributedTraceId != nil {- s := t.DistributedTraceId.String()- distributedTraceIdStr = &s- }- if _, err := stmt.ExecContext(ctx,- t.Id.String(), t.ProjectId.String(), t.TaskName,- int64(t.Duration), t.RecordedAt.UTC().Format(time.RFC3339Nano),- t.ClientIP, attributesJSON, t.AppVersion, t.ServerName, distributedTraceIdStr,- ); err != nil {+ row := taskToRow(t)+ if err := lit.InsertExistingUuid(db.TelemetryDB, &row); err != nil {return err}}- return tx.Commit()+ return nil}func (e *taskRepository) CountBetween(ctx context.Context, projectId uuid.UUID, start, end time.Time) (int64, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM tasks WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano),- ).Scan(&count)- return count, err+ result, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})+ if err != nil {+ return 0, err+ }+ if result == nil {+ return 0, nil+ }+ return int64(result.Count), nil}func (e *taskRepository) FindAll(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy string) ([]models.Task, int64, error) {- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM tasks WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- projectId.String(), fromDate.UTC().Format(time.RFC3339Nano), toDate.UTC().Format(time.RFC3339Nano),- ).Scan(&count)+ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)})if err != nil {return nil, 0, err}+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }offset := (page - 1) * pageSize- allowedOrderBy := map[string]bool{- "recorded_at": true,- "duration": true,- }+ allowedOrderBy := map[string]bool{"recorded_at": true, "duration": true}if !allowedOrderBy[orderBy] {orderBy = "recorded_at"}- query := "SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id FROM tasks WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY " + orderBy + " DESC LIMIT ? OFFSET ?"- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), fromDate.UTC().Format(time.RFC3339Nano), toDate.UTC().Format(time.RFC3339Nano),- pageSize, offset,- )+ rows, err := lit.SelectNamed[task](db.TelemetryDB,+ fmt.Sprintf(`SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY %s DESC LIMIT :limit OFFSET :offset`, orderBy),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "limit": pageSize, "offset": offset})if err != nil {return nil, 0, err}- defer rows.Close()- var tasks []models.Task- for rows.Next() {- t, err := scanTask(rows)- if err != nil {- return nil, 0, err- }- tasks = append(tasks, t)+ tasks := make([]models.Task, 0, len(rows))+ for _, row := range rows {+ tasks = append(tasks, row.toModel())}return tasks, count, nil}func (e *taskRepository) FindGroupedByTaskName(ctx context.Context, projectId uuid.UUID, fromDate, toDate time.Time, page, pageSize int, orderBy string, sortDirection string) ([]models.TaskStats, int64, error) {- fromStr := fromDate.UTC().Format(time.RFC3339Nano)- toStr := toDate.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()-- var totalCount int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(DISTINCT task_name) FROM tasks WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?",- pidStr, fromStr, toStr,- ).Scan(&totalCount)+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}++ totalResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(DISTINCT task_name) AS count FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to",+ params)if err != nil {return nil, 0, err}+ totalCount := int64(0)+ if totalResult != nil {+ totalCount = int64(totalResult.Count)+ }needsGoSort := orderBy == "p50_duration" || orderBy == "p95_duration" || orderBy == "impact"@@ -134,82 +176,44 @@ func (e *taskRepository) FindGroupedByTaskName(ctx context.Context, projectId uuoffset := (page - 1) * pageSizevar baseQuery string- var baseArgs []interface{}+ groupParams := lit.P{"project_id": projectId, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}if needsGoSort {baseQuery = `SELECT task_name, COUNT(*) as count, AVG(duration) as avg_duration, MAX(recorded_at) as last_seen- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :toGROUP BY task_name`- baseArgs = []interface{}{pidStr, fromStr, toStr}} else {- orderExpr := map[string]string{- "count": "count",- "last_seen": "last_seen",- }+ orderExpr := map[string]string{"count": "count", "last_seen": "last_seen"}expr, ok := orderExpr[orderBy]if !ok {expr = "count"}- baseQuery = `SELECT task_name, COUNT(*) as count, AVG(duration) as avg_duration, MAX(recorded_at) as last_seen- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY task_name- ORDER BY ` + expr + ` ` + sortDir + `- LIMIT ? OFFSET ?`- baseArgs = []interface{}{pidStr, fromStr, toStr, pageSize, offset}+ baseQuery = fmt.Sprintf(`SELECT task_name, COUNT(*) as count, AVG(duration) as avg_duration, MAX(recorded_at) as last_seen+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY task_name ORDER BY %s %s LIMIT :limit OFFSET :offset`, expr, sortDir)+ groupParams["limit"] = pageSize+ groupParams["offset"] = offset}- rows, err := db.DB.QueryContext(ctx, baseQuery, baseArgs...)+ groups, err := lit.SelectNamed[taskGroupRow](db.TelemetryDB, baseQuery, groupParams)if err != nil {return nil, 0, err}- defer rows.Close()-- type intermediate struct {- taskName string- count uint64- avgDuration float64- lastSeen string- }-- var intermediates []intermediate- for rows.Next() {- var it intermediate- if err := rows.Scan(&it.taskName, &it.count, &it.avgDuration, &it.lastSeen); err != nil {- return nil, 0, err- }- intermediates = append(intermediates, it)- }var stats []models.TaskStats- for _, it := range intermediates {- durRows, err := db.DB.QueryContext(ctx,- "SELECT duration FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY duration ASC",- pidStr, it.taskName, fromStr, toStr,- )+ for _, g := range groups {+ durations, err := fetchSortedTaskDurations(ctx, projectId, g.TaskName, fromDate, toDate)if err != nil {return nil, 0, err}- var sorted []float64- for durRows.Next() {- var d float64- if err := durRows.Scan(&d); err != nil {- durRows.Close()- return nil, 0, err- }- sorted = append(sorted, d)- }- durRows.Close()-- ls, _ := time.Parse(time.RFC3339Nano, it.lastSeen)+ ls, _ := time.Parse(time.RFC3339Nano, g.LastSeen)stats = append(stats, models.TaskStats{- TaskName: it.taskName,- Count: it.count,- P50Duration: time.Duration(computePercentile(sorted, 0.5)),- P95Duration: time.Duration(computePercentile(sorted, 0.95)),- AvgDuration: time.Duration(it.avgDuration),+ TaskName: g.TaskName,+ Count: g.Count,+ P50Duration: time.Duration(computePercentile(durations, 0.5)),+ P95Duration: time.Duration(computePercentile(durations, 0.95)),+ AvgDuration: time.Duration(g.AvgDuration),LastSeen: ls,})}@@ -256,25 +260,22 @@ func (e *taskRepository) FindGroupedByTaskName(ctx context.Context, projectId uu}func (e *taskRepository) FindByTaskName(ctx context.Context, projectId uuid.UUID, taskName string, fromDate, toDate time.Time, page, pageSize int, orderBy string, sortDirection string) ([]models.Task, int64, error) {- fromStr := fromDate.UTC().Format(time.RFC3339Nano)- toStr := toDate.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()-- var count int64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*) FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ?",- pidStr, taskName, fromStr, toStr,- ).Scan(&count)+ params := lit.P{"project_id": projectId, "task_name": taskName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate)}++ countResult, err := lit.SelectSingleNamed[models.CountResult](db.TelemetryDB,+ "SELECT COUNT(*) AS count FROM tasks WHERE project_id = :project_id AND task_name = :task_name AND recorded_at >= :from AND recorded_at <= :to",+ params)if err != nil {return nil, 0, err}+ count := int64(0)+ if countResult != nil {+ count = int64(countResult.Count)+ }offset := (page - 1) * pageSize- allowedOrderBy := map[string]bool{- "recorded_at": true,- "duration": true,- }+ allowedOrderBy := map[string]bool{"recorded_at": true, "duration": true}if !allowedOrderBy[orderBy] {orderBy = "recorded_at"}@@ -284,254 +285,114 @@ func (e *taskRepository) FindByTaskName(ctx context.Context, projectId uuid.UUIDsortDir = "ASC"}- query := "SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY " + orderBy + " " + sortDir + " LIMIT ? OFFSET ?"- rows, err := db.DB.QueryContext(ctx, query, pidStr, taskName, fromStr, toStr, pageSize, offset)+ rows, err := lit.SelectNamed[task](db.TelemetryDB,+ fmt.Sprintf(`SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id+ FROM tasks WHERE project_id = :project_id AND task_name = :task_name AND recorded_at >= :from AND recorded_at <= :to+ ORDER BY %s %s LIMIT :limit OFFSET :offset`, orderBy, sortDir),+ lit.P{"project_id": projectId, "task_name": taskName, "from": NewSQLiteTime(fromDate), "to": NewSQLiteTime(toDate), "limit": pageSize, "offset": offset})if err != nil {return nil, 0, err}- defer rows.Close()- var tasks []models.Task- for rows.Next() {- t, err := scanTask(rows)- if err != nil {- return nil, 0, err- }- tasks = append(tasks, t)+ tasks := make([]models.Task, 0, len(rows))+ for _, row := range rows {+ tasks = append(tasks, row.toModel())}return tasks, count, nil}func (e *taskRepository) FindById(ctx context.Context, projectId, taskId uuid.UUID) (*models.Task, error) {- row := db.DB.QueryRowContext(ctx,+ row, err := lit.SelectSingleNamed[task](db.TelemetryDB,`SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM tasks- WHERE project_id = ? AND id = ?- LIMIT 1`,- projectId.String(), taskId.String(),- )-- var t models.Task- var idStr, projectIdStr string- var recordedAtStr string- var dur int64- var attributesJSON string- var distributedTraceIdStr sql.NullString-- err := row.Scan(&idStr, &projectIdStr, &t.TaskName, &dur, &recordedAtStr, &t.ClientIP, &attributesJSON, &t.AppVersion, &t.ServerName, &distributedTraceIdStr)+ FROM tasks WHERE project_id = :project_id AND id = :id LIMIT 1`,+ lit.P{"project_id": projectId, "id": taskId})if err != nil {- if err == sql.ErrNoRows {- return nil, nil- }return nil, err}-- t.Id, _ = uuid.Parse(idStr)- t.ProjectId, _ = uuid.Parse(projectIdStr)- t.Duration = time.Duration(dur)- t.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- t.DistributedTraceId = &parsed- }- }-- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {- t.Attributes = nil- }+ if row == nil {+ return nil, nil}-+ t := row.toModel()return &t, nil}func (e *taskRepository) CountByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- CAST(COUNT(*) AS REAL) as count- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano),- )+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var tsStr string- var p models.TimeSeriesPoint- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *taskRepository) AvgDurationByHour(ctx context.Context, projectId uuid.UUID, start, end time.Time) ([]models.TimeSeriesPoint, error) {- query := `SELECT- strftime('%Y-%m-%d %H:00:00', recorded_at) as hour,- AVG(duration) / 1000000.0 as avg_duration_ms- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY hour- ORDER BY hour ASC`-- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano),- )+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ `SELECT strftime('%Y-%m-%d %H:00:00', recorded_at) as bucket, AVG(duration) / 1000000.0 as agg_value+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`,+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var tsStr string- var p models.TimeSeriesPoint- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *taskRepository) CountByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {- query := fmt.Sprintf(`SELECT- datetime((strftime('%%s', recorded_at) / (%d * 60)) * (%d * 60), 'unixepoch') as bucket,- CAST(COUNT(*) AS REAL) as count- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`, intervalMinutes, intervalMinutes)-- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano),- )+ secs := intervalMinutes * 60+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket, CAST(COUNT(*) AS REAL) as agg_value+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, secs, secs),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var tsStr string- var p models.TimeSeriesPoint- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *taskRepository) AvgDurationByInterval(ctx context.Context, projectId uuid.UUID, start, end time.Time, intervalMinutes int) ([]models.TimeSeriesPoint, error) {- query := fmt.Sprintf(`SELECT- datetime((strftime('%%s', recorded_at) / (%d * 60)) * (%d * 60), 'unixepoch') as bucket,- AVG(duration) / 1000000.0 as avg_duration_ms- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?- GROUP BY bucket- ORDER BY bucket ASC`, intervalMinutes, intervalMinutes)-- rows, err := db.DB.QueryContext(ctx, query,- projectId.String(), start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano),- )+ secs := intervalMinutes * 60+ results, err := lit.SelectNamed[timeSeriesResult](db.TelemetryDB,+ fmt.Sprintf(`SELECT datetime((strftime('%%s', recorded_at) / %d) * %d, 'unixepoch') as bucket, AVG(duration) / 1000000.0 as agg_value+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :to+ GROUP BY bucket ORDER BY bucket ASC`, secs, secs),+ lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)})if err != nil {return nil, err}- defer rows.Close()-- var points []models.TimeSeriesPoint- for rows.Next() {- var tsStr string- var p models.TimeSeriesPoint- if err := rows.Scan(&tsStr, &p.Value); err != nil {- return nil, err- }- p.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)- points = append(points, p)- }-- return points, nil+ return timeSeriesResultsToPoints(results), nil}func (e *taskRepository) FindWorstTasks(ctx context.Context, projectId uuid.UUID, start, end time.Time, limit int) ([]models.TaskStats, error) {- fromStr := start.UTC().Format(time.RFC3339Nano)- toStr := end.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()+ params := lit.P{"project_id": projectId, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}- rows, err := db.DB.QueryContext(ctx,+ groups, err := lit.SelectNamed[taskGroupRow](db.TelemetryDB,`SELECT task_name, COUNT(*) as count, AVG(duration) as avg_duration, MAX(recorded_at) as last_seen- FROM tasks- WHERE project_id = ? AND recorded_at >= ? AND recorded_at <= ?+ FROM tasks WHERE project_id = :project_id AND recorded_at >= :from AND recorded_at <= :toGROUP BY task_name`,- pidStr, fromStr, toStr,- )+ params)if err != nil {return nil, err}- defer rows.Close()-- type intermediate struct {- taskName string- count uint64- avgDuration float64- lastSeen string- }-- var intermediates []intermediate- for rows.Next() {- var it intermediate- if err := rows.Scan(&it.taskName, &it.count, &it.avgDuration, &it.lastSeen); err != nil {- return nil, err- }- intermediates = append(intermediates, it)- }var stats []models.TaskStats- for _, it := range intermediates {- durRows, err := db.DB.QueryContext(ctx,- "SELECT duration FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY duration ASC",- pidStr, it.taskName, fromStr, toStr,- )+ for _, g := range groups {+ durations, err := fetchSortedTaskDurations(ctx, projectId, g.TaskName, start, end)if err != nil {return nil, err}- var sorted []float64- for durRows.Next() {- var d float64- if err := durRows.Scan(&d); err != nil {- durRows.Close()- return nil, err- }- sorted = append(sorted, d)- }- durRows.Close()-- ls, _ := time.Parse(time.RFC3339Nano, it.lastSeen)+ ls, _ := time.Parse(time.RFC3339Nano, g.LastSeen)stats = append(stats, models.TaskStats{- TaskName: it.taskName,- Count: it.count,- P50Duration: time.Duration(computePercentile(sorted, 0.5)),- P95Duration: time.Duration(computePercentile(sorted, 0.95)),- AvgDuration: time.Duration(it.avgDuration),+ TaskName: g.TaskName,+ Count: g.Count,+ P50Duration: time.Duration(computePercentile(durations, 0.5)),+ P95Duration: time.Duration(computePercentile(durations, 0.95)),+ AvgDuration: time.Duration(g.AvgDuration),LastSeen: ls,})}@@ -549,114 +410,89 @@ func (e *taskRepository) FindWorstTasks(ctx context.Context, projectId uuid.UUID}func (e *taskRepository) GetTaskStats(ctx context.Context, projectId uuid.UUID, taskName string, start, end time.Time) (*models.TaskDetailStats, error) {- fromStr := start.UTC().Format(time.RFC3339Nano)- toStr := end.UTC().Format(time.RFC3339Nano)- pidStr := projectId.String()+ params := lit.P{"project_id": projectId, "task_name": taskName, "from": NewSQLiteTime(start), "to": NewSQLiteTime(end)}durationMinutes := end.Sub(start).Minutes()if durationMinutes < 1 {durationMinutes = 1}- var count int64- var avgDur float64- err := db.DB.QueryRowContext(ctx,- "SELECT COUNT(*), AVG(duration) / 1000000.0 FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ?",- pidStr, taskName, fromStr, toStr,- ).Scan(&count, &avgDur)+ statsRow, err := lit.SelectSingleNamed[taskCountStatsRow](db.TelemetryDB,+ "SELECT COUNT(*) AS count, AVG(duration) / 1000000.0 AS avg_dur_ms FROM tasks WHERE project_id = :project_id AND task_name = :task_name AND recorded_at >= :from AND recorded_at <= :to",+ params)if err != nil {return nil, err}+ if statsRow == nil {+ return &models.TaskDetailStats{}, nil+ }- durRows, err := db.DB.QueryContext(ctx,- "SELECT duration FROM tasks WHERE project_id = ? AND task_name = ? AND recorded_at >= ? AND recorded_at <= ? ORDER BY duration ASC",- pidStr, taskName, fromStr, toStr,- )+ durations, err := fetchSortedTaskDurations(ctx, projectId, taskName, start, end)if err != nil {return nil, err}- defer durRows.Close()-- var sorted []float64- for durRows.Next() {- var d float64- if err := durRows.Scan(&d); err != nil {- return nil, err- }- sorted = append(sorted, d)- }nsToMs := 1000000.0return &models.TaskDetailStats{- Count: count,- AvgDuration: avgDur,- MedianDuration: computePercentile(sorted, 0.5) / nsToMs,- P95Duration: computePercentile(sorted, 0.95) / nsToMs,- P99Duration: computePercentile(sorted, 0.99) / nsToMs,- Throughput: float64(count) / durationMinutes,+ Count: statsRow.Count,+ AvgDuration: statsRow.AvgDurMs,+ MedianDuration: computePercentile(durations, 0.5) / nsToMs,+ P95Duration: computePercentile(durations, 0.95) / nsToMs,+ P99Duration: computePercentile(durations, 0.99) / nsToMs,+ Throughput: float64(statsRow.Count) / durationMinutes,}, nil}-func scanTask(rows *sql.Rows) (models.Task, error) {- var t models.Task- var idStr, projectIdStr string- var recordedAtStr string- var dur int64- var attributesJSON string- var distributedTraceIdStr sql.NullString-- if err := rows.Scan(&idStr, &projectIdStr, &t.TaskName, &dur, &recordedAtStr, &t.ClientIP, &attributesJSON, &t.AppVersion, &t.ServerName, &distributedTraceIdStr); err != nil {- return t, err- }-- t.Id, _ = uuid.Parse(idStr)- t.ProjectId, _ = uuid.Parse(projectIdStr)- t.Duration = time.Duration(dur)- t.RecordedAt, _ = time.Parse(time.RFC3339Nano, recordedAtStr)- if distributedTraceIdStr.Valid && distributedTraceIdStr.String != "" {- parsed, err := uuid.Parse(distributedTraceIdStr.String)- if err == nil {- t.DistributedTraceId = &parsed- }- }-- if attributesJSON != "" && attributesJSON != "{}" {- if err := json.Unmarshal([]byte(attributesJSON), &t.Attributes); err != nil {- t.Attributes = nil- }- }-- return t, nil-}-func (e *taskRepository) FindByDistributedTraceId(ctx context.Context, distributedTraceId uuid.UUID, projectIds []uuid.UUID) ([]models.Task, error) {if len(projectIds) == 0 {return nil, nil}- placeholders := strings.TrimRight(strings.Repeat("?,", len(projectIds)), ",")+ params := lit.P{"trace_id": distributedTraceId}+ placeholders := make([]string, len(projectIds))+ for i, pid := range projectIds {+ key := fmt.Sprintf("pid_%d", i)+ placeholders[i] = ":" + key+ params[key] = pid+ }query := `SELECT id, project_id, task_name, duration, recorded_at, client_ip, attributes, app_version, server_name, distributed_trace_id- FROM tasks- WHERE distributed_trace_id = ? AND project_id IN (` + placeholders + `)+ FROM tasks WHERE distributed_trace_id = :trace_id AND project_id IN (` + strings.Join(placeholders, ",") + `)ORDER BY recorded_at ASC`- args := []interface{}{distributedTraceId.String()}- for _, pid := range projectIds {- args = append(args, pid.String())++ parsedQuery, args, err := lit.ParseNamedQuery(db.Driver, query, params)+ if err != nil {+ return nil, err}- rows, err := db.QueryerFromContext(ctx).QueryContext(ctx, query, args...)++ sqlRows, err := db.TelemetryDB.QueryContext(ctx, parsedQuery, args...)if err != nil {return nil, err}- defer rows.Close()+ defer sqlRows.Close()+var tasks []models.Task- for rows.Next() {- t, err := scanTask(rows)- if err != nil {+ for sqlRows.Next() {+ var row task+ if err := sqlRows.Scan(&row.Id, &row.ProjectId, &row.TaskName, &row.Duration, &row.RecordedAt, &row.ClientIP, &row.Attributes, &row.AppVersion, &row.ServerName, &row.DistributedTraceId); err != nil {return nil, err}- tasks = append(tasks, t)+ tasks = append(tasks, row.toModel())}return tasks, nil}+func fetchSortedTaskDurations(ctx context.Context, projectId uuid.UUID, taskName string, from, to time.Time) ([]float64, error) {+ results, err := lit.SelectNamed[durationValueRow](db.TelemetryDB,+ "SELECT duration FROM tasks WHERE project_id = :project_id AND task_name = :task_name AND recorded_at >= :from AND recorded_at <= :to ORDER BY duration ASC",+ lit.P{"project_id": projectId, "task_name": taskName, "from": NewSQLiteTime(from), "to": NewSQLiteTime(to)})+ if err != nil {+ return nil, err+ }+ durations := make([]float64, 0, len(results))+ for _, r := range results {+ durations = append(durations, r.Duration)+ }+ return durations, nil+}+var TaskRepository = taskRepository{}diff --git a/backend/app/repositories/testhelper_sqlite_test.go b/backend/app/repositories/testhelper_sqlite_test.goindex ce7c865d..e13ba115 100644--- a/backend/app/repositories/testhelper_sqlite_test.go+++ b/backend/app/repositories/testhelper_sqlite_test.go@@ -15,36 +15,45 @@ import (func setupTestDB(t *testing.T) {t.Helper()- sqliteDB, err := sql.Open("sqlite", ":memory:")+ mainDB, err := sql.Open("sqlite", ":memory:")if err != nil {- t.Fatalf("failed to open in-memory sqlite: %v", err)+ t.Fatalf("failed to open in-memory sqlite (main): %v", err)}-- if _, err := sqliteDB.Exec("PRAGMA foreign_keys = ON"); err != nil {+ mainDB.SetMaxOpenConns(1)+ if _, err := mainDB.Exec("PRAGMA foreign_keys = ON"); err != nil {t.Fatalf("failed to enable foreign keys: %v", err)}- if _, err := sqliteDB.Exec("PRAGMA journal_mode = WAL"); err != nil {+ if _, err := mainDB.Exec("PRAGMA journal_mode = WAL"); err != nil {t.Fatalf("failed to set WAL mode: %v", err)}- sqliteDB.SetMaxOpenConns(1)+ telemetryDB, err := sql.Open("sqlite", ":memory:")+ if err != nil {+ t.Fatalf("failed to open in-memory sqlite (telemetry): %v", err)+ }+ telemetryDB.SetMaxOpenConns(1)+ if _, err := telemetryDB.Exec("PRAGMA journal_mode = WAL"); err != nil {+ t.Fatalf("failed to set WAL mode (telemetry): %v", err)+ }- db.DB = sqliteDB+ db.DB = mainDB+ db.TelemetryDB = telemetryDBdb.Driver = lit.SQLitemodels.Init(db.Driver)- if err := runTestMigrations(sqliteDB); err != nil {- t.Fatalf("failed to run migrations: %v", err)+ if err := runTelemetryMigrations(telemetryDB); err != nil {+ t.Fatalf("failed to run telemetry migrations: %v", err)}t.Cleanup(func() {- sqliteDB.Close()+ mainDB.Close()+ telemetryDB.Close()})}-func runTestMigrations(sqliteDB *sql.DB) error {- telemetryDDL := `+func runTelemetryMigrations(telemetryDB *sql.DB) error {+ ddl := `CREATE TABLE IF NOT EXISTS endpoints (id TEXT NOT NULL,project_id TEXT NOT NULL,@@ -56,7 +65,8 @@ CREATE TABLE IF NOT EXISTS endpoints (client_ip TEXT NOT NULL DEFAULT '',attributes TEXT NOT NULL DEFAULT '{}',app_version TEXT NOT NULL DEFAULT '',- server_name TEXT NOT NULL DEFAULT ''+ server_name TEXT NOT NULL DEFAULT '',+ distributed_trace_id TEXT DEFAULT NULL);CREATE INDEX IF NOT EXISTS idx_endpoints_project_recorded ON endpoints(project_id, recorded_at);CREATE INDEX IF NOT EXISTS idx_endpoints_project_endpoint ON endpoints(project_id, endpoint);@@ -70,7 +80,8 @@ CREATE TABLE IF NOT EXISTS tasks (client_ip TEXT NOT NULL DEFAULT '',attributes TEXT NOT NULL DEFAULT '{}',app_version TEXT NOT NULL DEFAULT '',- server_name TEXT NOT NULL DEFAULT ''+ server_name TEXT NOT NULL DEFAULT '',+ distributed_trace_id TEXT DEFAULT NULL);CREATE INDEX IF NOT EXISTS idx_tasks_project_recorded ON tasks(project_id, recorded_at);@@ -85,7 +96,8 @@ CREATE TABLE IF NOT EXISTS exception_stack_traces (attributes TEXT NOT NULL DEFAULT '{}',app_version TEXT NOT NULL DEFAULT '',server_name TEXT NOT NULL DEFAULT '',- is_message INTEGER NOT NULL DEFAULT 0+ is_message INTEGER NOT NULL DEFAULT 0,+ distributed_trace_id TEXT DEFAULT NULL);CREATE INDEX IF NOT EXISTS idx_exceptions_project_recorded ON exception_stack_traces(project_id, recorded_at);CREATE INDEX IF NOT EXISTS idx_exceptions_project_hash ON exception_stack_traces(project_id, exception_hash);@@ -151,9 +163,9 @@ CREATE TABLE IF NOT EXISTS fired_notifications ();`- statements := splitStatements(telemetryDDL)+ statements := splitStatements(ddl)for _, stmt := range statements {- if _, err := sqliteDB.Exec(stmt); err != nil {+ if _, err := telemetryDB.Exec(stmt); err != nil {return err}}diff --git a/frontend/src/lib/components/distributed-trace/distributed-trace-card.svelte b/frontend/src/lib/components/distributed-trace/distributed-trace-card.svelteindex db06fb0b..1178ae19 100644--- a/frontend/src/lib/components/distributed-trace/distributed-trace-card.svelte+++ b/frontend/src/lib/components/distributed-trace/distributed-trace-card.svelte@@ -40,11 +40,13 @@}function navigateToNode(node: DistributedTraceNode) {- const projectId = node.projectId;+ projectsState.selectProject(node.projectId);if (node.traceType === 'task' && node.task) {- goto(`/tasks/${encodeURIComponent(node.task.taskName)}/${node.task.id}?preset=24h&projectId=${projectId}`);+ goto(`/tasks/${encodeURIComponent(node.task.taskName)}/${node.task.id}?preset=24h`);+ } else if (node.traceType === 'exception' && node.exception) {+ goto(`/issues/${node.exception.exceptionHash}?preset=24h`);} else if (node.endpoint) {- goto(`/endpoints/${encodeURIComponent(node.endpoint.endpoint)}/${node.endpoint.id}?preset=24h&projectId=${projectId}`);+ goto(`/endpoints/${encodeURIComponent(node.endpoint.endpoint)}/${node.endpoint.id}?preset=24h`);}}@@ -53,40 +55,59 @@});</script>-<Card.Root>- <Card.Header>- <div class="flex items-center gap-2">- <GitBranch class="h-5 w-5 text-muted-foreground" />- <Card.Title>Distributed Trace</Card.Title>- </div>- <Card.Description>- This trace spans across multiple services- </Card.Description>- </Card.Header>- <Card.Content>- {#if loading}+{#if loading}+ <Card.Root>+ <Card.Header>+ <div class="flex items-center gap-2">+ <GitBranch class="h-5 w-5 text-muted-foreground" />+ <Card.Title>Distributed Trace</Card.Title>+ </div>+ <Card.Description>+ This trace spans across multiple services+ </Card.Description>+ </Card.Header>+ <Card.Content><div class="flex items-center justify-center py-6"><LoadingCircle size="md" /></div>- {:else if error}- <p class="text-sm text-muted-foreground">{error}</p>- {:else if response && response.nodes.length > 0}+ </Card.Content>+ </Card.Root>+{:else if response && response.nodes.length > 0}+ <Card.Root>+ <Card.Header>+ <div class="flex items-center gap-2">+ <GitBranch class="h-5 w-5 text-muted-foreground" />+ <Card.Title>Distributed Trace</Card.Title>+ </div>+ <Card.Description>+ This trace spans across multiple services+ </Card.Description>+ </Card.Header>+ <Card.Content><div class="space-y-3">{#each response.nodes as node, i}<div class="flex items-center gap-3 rounded-md border p-3 {i > 0 ? 'ml-6' : ''}"><div class="flex min-w-0 flex-1 items-center gap-3"><Badge variant="outline" class="shrink-0">{node.projectName}</Badge><span class="truncate font-mono text-sm">- {node.traceType === 'task' ? node.task?.taskName : node.endpoint?.endpoint}+ {#if node.traceType === 'task'}+ {node.task?.taskName}+ {:else if node.traceType === 'exception'}+ {node.exception?.stackTrace.split('\n')[0]}+ {:else}+ {node.endpoint?.endpoint}+ {/if}</span>{#if node.traceType === 'endpoint' && node.endpoint}<span class="shrink-0 font-mono text-sm {getStatusColor(node.endpoint.statusCode)}">{node.endpoint.statusCode}</span>{/if}- <span class="shrink-0 font-mono text-sm text-muted-foreground">- {formatDuration(node.traceType === 'task' ? node.task?.duration ?? 0 : node.endpoint?.duration ?? 0)}- </span>+ {#if node.traceType !== 'exception'}+ <span class="shrink-0 font-mono text-sm text-muted-foreground">+ {formatDuration(node.traceType === 'task' ? node.task?.duration ?? 0 : node.endpoint?.duration ?? 0)}+ </span>+ {/if}{#if node.exception}<Badge variant="destructive" class="shrink-0">Exception</Badge>{/if}@@ -96,15 +117,8 @@<ArrowRight class="ml-1 h-3 w-3" /></Button></div>- {#if i < response.nodes.length - 1}- <div class="ml-3 flex items-center gap-1 text-muted-foreground {i > 0 ? 'ml-9' : 'ml-3'}">- <div class="h-4 w-px bg-border"></div>- </div>- {/if}- {/each}+ {/each}</div>- {:else}- <p class="text-sm text-muted-foreground">No distributed trace data found.</p>- {/if}- </Card.Content>-</Card.Root>+ </Card.Content>+ </Card.Root>+{/if}diff --git a/frontend/src/lib/types/distributed-trace.ts b/frontend/src/lib/types/distributed-trace.tsindex beede1ab..41e8054d 100644--- a/frontend/src/lib/types/distributed-trace.ts+++ b/frontend/src/lib/types/distributed-trace.ts@@ -1,7 +1,7 @@export type DistributedTraceNode = {projectId: string;projectName: string;- traceType: 'endpoint' | 'task';+ traceType: 'endpoint' | 'task' | 'exception';endpoint?: {id: string;endpoint: string;