Skip to main content

systemprompt_analytics/feedback/
types.rs

1//! Worker fencing, durable operation state and normalized projection records.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use systemprompt_identifiers::{AnalyticsChangeId, AnalyticsWorkerId, TaskId};
9use systemprompt_models::feedback::analytics::{AnalyticsFactKey, NormalizedAnalyticsFact};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ChangeReceipt {
13    pub change_id: AnalyticsChangeId,
14    pub state: FactChangeState,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct FactLease {
19    pub change_id: AnalyticsChangeId,
20    pub worker_id: AnalyticsWorkerId,
21    pub epoch: i64,
22    pub expires_at: DateTime<Utc>,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct ApplyOutcome {
27    pub change_id: AnalyticsChangeId,
28    pub generation: i64,
29    pub replaced: bool,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct StoredFact {
34    pub key: AnalyticsFactKey,
35    pub revision: i64,
36    pub occurred_at: DateTime<Utc>,
37    pub fact: Option<NormalizedAnalyticsFact>,
38    pub generation: i64,
39}
40
41#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
42pub struct FactsHealth {
43    pub generation: i64,
44    pub pending: i64,
45    pub leased: i64,
46    pub retries: i64,
47    pub oldest_pending_at: Option<DateTime<Utc>>,
48    pub last_applied_at: Option<DateTime<Utc>>,
49    pub last_recorded_at: Option<DateTime<Utc>>,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct BackfillProgress {
54    pub job_id: TaskId,
55    pub source: String,
56    pub cursor: String,
57    pub generation: i64,
58    pub pages: i64,
59    pub facts: i64,
60    pub complete: bool,
61}
62
63#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64pub struct FactsTotals {
65    pub invocations: i64,
66    pub verified_invocations: i64,
67    pub requests: i64,
68    pub failed_requests: i64,
69    pub priced_requests: i64,
70    pub latency_measured_requests: i64,
71    pub token_measured_requests: i64,
72    pub assessed_conversations: i64,
73    pub assessment_conversations: i64,
74    pub failed_assessments: i64,
75    pub spend_by_currency: std::collections::BTreeMap<String, i128>,
76    pub related_spend_non_additive: bool,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(deny_unknown_fields)]
81pub struct BackfillPage {
82    pub expected_generation: i64,
83    pub next_cursor: String,
84    pub complete: bool,
85    pub changes: Vec<systemprompt_models::feedback::analytics::AnalyticsChange>,
86}
87
88/// How much delta work one consumer worker claims and for how long.
89#[derive(Debug, Clone, Copy)]
90pub struct DeltaClaim {
91    pub limit: u32,
92    pub lease_seconds: u32,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct DeltaLease {
97    pub consumer: String,
98    pub worker_id: AnalyticsWorkerId,
99    pub epoch: i64,
100    pub after_generation: i64,
101    pub through_generation: i64,
102    pub expires_at: DateTime<Utc>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct FactDelta {
107    pub generation: i64,
108    pub key: AnalyticsFactKey,
109    pub before: Option<NormalizedAnalyticsFact>,
110    pub after: Option<NormalizedAnalyticsFact>,
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
114#[serde(rename_all = "snake_case")]
115pub enum FactChangeState {
116    Pending,
117    Leased,
118    Applied,
119    Superseded,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct ChangeDiagnostic {
124    pub change_id: AnalyticsChangeId,
125    pub state: FactChangeState,
126    pub attempts: i32,
127    pub lease_until: Option<DateTime<Utc>>,
128    pub last_error: Option<String>,
129}