| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "reasonix/internal/event" |
| 6 | "reasonix/internal/provider" |
| 7 | "slices" |
| 8 | ) |
| 9 | |
| 10 | func maintenanceHistoryRow(op *event.SessionOperationInfo) HistoryMessage { |
| 11 | return HistoryMessage{Role: "compaction", Pending: op.Status == "running" || op.Status == "cancelling" || op.Status == "finalizing", Trigger: "manual", |
| 12 | OperationID: op.OperationID, OperationRevision: op.OperationRevision, RuntimeEpoch: op.RuntimeEpoch, |
| 13 | OperationKind: op.Kind, OperationStatus: op.Status, OperationActivity: op.Activity, |
| 14 | ErrorCode: op.ErrorCode, Detail: op.Detail, Applied: op.Applied, |
| 15 | InputTokens: op.InputTokens, ResultTokens: op.ResultTokens, |
| 16 | Messages: op.Messages, Summary: op.Summary, Archive: op.Archive} |
| 17 | } |
| 18 | |
| 19 | func maintenanceHistoryMessage(m provider.Message) []HistoryMessage { |
| 20 | var op event.SessionOperationInfo |
| 21 | if json.Unmarshal([]byte(m.Content), &op) != nil || op.OperationID == "" { |
| 22 | return nil |
| 23 | } |
| 24 | row := maintenanceHistoryRow(&op) |
| 25 | row.RecordID = m.ID |
| 26 | return []HistoryMessage{row} |
| 27 | } |
| 28 | |
| 29 | func upsertMaintenancePreview(rows []HistoryMessage, op *event.SessionOperationInfo) []HistoryMessage { |
| 30 | if op == nil || op.OperationID == "" { |
| 31 | return rows |
| 32 | } |
| 33 | row := maintenanceHistoryRow(op) |
| 34 | for i, prior := range slices.Backward(rows) { |
| 35 | if prior.OperationID == op.OperationID { |
| 36 | rows[i] = row |
| 37 | return rows |
| 38 | } |
| 39 | } |
| 40 | return append(rows, row) |
| 41 | } |
| 42 |