Migrate ingest and API paths to string IDs

This commit is contained in:
2026-02-14 20:10:23 +03:00
parent 6df92b7a69
commit 3ffdf9b0e1
42 changed files with 1958 additions and 437 deletions

View File

@@ -192,13 +192,13 @@ type lotMetricsRow struct {
func (r *Repository) loadLotExposures(ctx context.Context, start, end time.Time) (map[string]*LotMetrics, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT l.id, l.code,
COUNT(DISTINCT i.component_id) AS component_count,
COUNT(DISTINCT i.part_id) AS component_count,
SUM(GREATEST(0, TIMESTAMPDIFF(SECOND,
GREATEST(i.installed_at, ?),
LEAST(COALESCE(i.removed_at, ?), ?)
))) AS exposure_seconds
FROM installations i
JOIN components c ON c.id = i.component_id
JOIN parts c ON c.id = i.part_id
LEFT JOIN lots l ON l.id = c.lot_id
WHERE i.installed_at <= ?
AND (i.removed_at IS NULL OR i.removed_at >= ?)
@@ -242,7 +242,7 @@ func (r *Repository) loadLotFailures(ctx context.Context, start, end time.Time)
rows, err := r.db.QueryContext(ctx, `
SELECT l.id, l.code, COUNT(*) AS failures
FROM failure_events f
JOIN components c ON c.id = f.component_id
JOIN parts c ON c.id = f.part_id
LEFT JOIN lots l ON l.id = c.lot_id
WHERE f.failure_time >= ? AND f.failure_time <= ?
GROUP BY l.id, l.code
@@ -283,11 +283,11 @@ func (r *Repository) loadFirmwareFailures(ctx context.Context, start, end time.T
SELECT o.firmware_version
FROM failure_events f
LEFT JOIN observations o
ON o.component_id = f.component_id
ON o.part_id = f.part_id
AND o.observed_at = (
SELECT MAX(o2.observed_at)
FROM observations o2
WHERE o2.component_id = f.component_id
WHERE o2.part_id = f.part_id
AND o2.observed_at <= f.failure_time
)
WHERE f.failure_time >= ? AND f.failure_time <= ?`,
@@ -315,15 +315,15 @@ func (r *Repository) loadFirmwareFailures(ctx context.Context, start, end time.T
func (r *Repository) loadFirmwareComponents(ctx context.Context, start, end time.Time) (map[string]int64, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT o.component_id, o.firmware_version
SELECT o.part_id, o.firmware_version
FROM observations o
JOIN (
SELECT component_id, MAX(observed_at) AS observed_at
SELECT part_id, MAX(observed_at) AS observed_at
FROM observations
WHERE observed_at >= ? AND observed_at <= ?
GROUP BY component_id
GROUP BY part_id
) latest
ON latest.component_id = o.component_id
ON latest.part_id = o.part_id
AND latest.observed_at = o.observed_at`,
start, end,
)
@@ -334,9 +334,9 @@ func (r *Repository) loadFirmwareComponents(ctx context.Context, start, end time
components := map[string]int64{}
for rows.Next() {
var componentID sql.NullInt64
var partID sql.NullString
var firmware sql.NullString
if err := rows.Scan(&componentID, &firmware); err != nil {
if err := rows.Scan(&partID, &firmware); err != nil {
return nil, err
}
key := firmwareValue(firmware)