1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Batch export and deletion of technical history with signal deduplication protection.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::{FromRow, PgPool};
use uuid::Uuid;
/// Engine-owned technical history eligible for archive operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FluxproArchiveSource {
/// Structured process execution events.
ExecutionLog,
/// Admitted signal payloads used for history and deduplication.
SignalHistory,
}
/// One archive row containing its identity, event timestamp, and raw JSON data.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, FromRow)]
pub struct FluxproArchiveRecord {
/// Database row identity.
pub uuid: Uuid,
/// Original UTC timestamp of the archived event.
pub event_at: DateTime<Utc>,
/// Complete source row represented as JSON for external archival.
pub data: Value,
}
/// Batch access to technical logs for host-managed external archival.
#[derive(Clone)]
pub struct FluxproArchiveService {
pool: PgPool,
}
impl FluxproArchiveService {
/// Uses an existing pool for technical-history archive operations.
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
/// Returns an ordered batch from engine-owned technical log tables.
/// Host applications must use this API instead of querying FluxPro tables directly.
pub async fn fetch_batch(
&self,
source: FluxproArchiveSource,
cutoff_at: DateTime<Utc>,
limit: i64,
) -> anyhow::Result<Vec<FluxproArchiveRecord>> {
let query = match source {
FluxproArchiveSource::ExecutionLog => {
r#"select uuid, created_at as event_at, to_jsonb(log) as data
from fluxpro.process_instance_log log
where created_at < $1
order by created_at, uuid
limit $2"#
}
FluxproArchiveSource::SignalHistory => {
// `get_last_signal` uses the newest row for duplicate protection. Keep that
// row while its process is active; all older rows and every row of a final
// process are technical history and can be archived.
r#"select signal.uuid, signal.created_at as event_at,
to_jsonb(signal) as data
from fluxpro.signal_history signal
left join fluxpro.process_instance instance
on instance.token = signal.process_id
left join fluxpro.process_stage stage
on stage.uuid = instance.current_stage
where signal.created_at < $1
and (
instance.uuid is null
or coalesce(stage.is_final, false)
or exists (
select 1
from fluxpro.signal_history newer
where newer.process_id = signal.process_id
and (
newer.created_at > signal.created_at
or (newer.created_at = signal.created_at and newer.uuid > signal.uuid)
)
)
)
order by signal.created_at, signal.uuid
limit $2"#
}
};
Ok(sqlx::query_as(query)
.bind(cutoff_at)
.bind(limit.clamp(1, 1_000))
.fetch_all(&self.pool)
.await?)
}
/// Deletes only the records whose exact identifiers were already persisted in a verified archive.
pub async fn delete_archived(
&self,
source: FluxproArchiveSource,
record_ids: &[Uuid],
cutoff_at: DateTime<Utc>,
) -> anyhow::Result<u64> {
if record_ids.is_empty() {
return Ok(0);
}
let query = match source {
FluxproArchiveSource::ExecutionLog => {
"delete from fluxpro.process_instance_log where uuid = any($1) and created_at < $2"
}
FluxproArchiveSource::SignalHistory => {
r#"delete from fluxpro.signal_history signal
where signal.uuid = any($1)
and signal.created_at < $2
and (
not exists (
select 1
from fluxpro.process_instance instance
where instance.token = signal.process_id
)
or exists (
select 1
from fluxpro.process_instance instance
left join fluxpro.process_stage stage
on stage.uuid = instance.current_stage
where instance.token = signal.process_id
and (
coalesce(stage.is_final, false)
or exists (
select 1
from fluxpro.signal_history newer
where newer.process_id = signal.process_id
and (
newer.created_at > signal.created_at
or (newer.created_at = signal.created_at and newer.uuid > signal.uuid)
)
)
)
)
)"#
}
};
Ok(sqlx::query(query)
.bind(record_ids)
.bind(cutoff_at)
.execute(&self.pool)
.await?
.rows_affected())
}
}