ceres-server 0.7.0

REST API server for Ceres harvesting, embedding, and search workflows
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Response DTOs for API endpoints.

use chrono::{DateTime, Utc};
use serde::Serialize;
use utoipa::ToSchema;
use uuid::Uuid;

use ceres_core::{DatabaseStats, HarvestJob, SearchResult, SyncStats};

// =============================================================================
// Health & Stats
// =============================================================================

/// Process liveness response.
#[derive(Debug, Serialize, ToSchema)]
pub struct ProbeResponse {
    /// Probe status (`alive`).
    pub status: String,
    /// Server version.
    pub version: String,
}

/// Health check response.
#[derive(Debug, Serialize, ToSchema)]
pub struct HealthResponse {
    /// Health status ("healthy", "degraded", or "unhealthy")
    pub status: String,
    /// Server version
    pub version: String,
    /// Database connectivity status
    pub database: ServiceStatus,
}

/// Status of an individual service component.
#[derive(Debug, Serialize, ToSchema)]
pub struct ServiceStatus {
    /// Whether the service is reachable
    pub healthy: bool,
    /// Optional message (e.g., error details)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// Database statistics response.
#[derive(Debug, Serialize, ToSchema)]
pub struct StatsResponse {
    /// Total number of datasets in the database
    pub total_datasets: i64,
    /// Number of datasets with generated embeddings
    pub datasets_with_embeddings: i64,
    /// Number of unique indexed portals
    pub total_portals: i64,
    /// Timestamp of the last update
    pub last_update: Option<DateTime<Utc>>,
    /// Number of datasets marked as stale (removed from source portal)
    pub stale_datasets: i64,
}

impl From<DatabaseStats> for StatsResponse {
    fn from(s: DatabaseStats) -> Self {
        Self {
            total_datasets: s.total_datasets,
            datasets_with_embeddings: s.datasets_with_embeddings,
            total_portals: s.total_portals,
            last_update: s.last_update,
            stale_datasets: s.stale_datasets,
        }
    }
}

// =============================================================================
// Search
// =============================================================================

/// Semantic search response.
#[derive(Debug, Serialize, ToSchema)]
pub struct SearchResponse {
    /// The original search query
    pub query: String,
    /// Number of results returned
    pub count: usize,
    /// Search results ordered by similarity
    pub results: Vec<SearchResultDto>,
}

/// Individual search result with similarity score.
#[derive(Debug, Serialize, ToSchema)]
pub struct SearchResultDto {
    /// Dataset UUID
    pub id: Uuid,
    /// Dataset title
    pub title: String,
    /// Dataset description
    pub description: Option<String>,
    /// Dataset landing page URL
    pub url: String,
    /// Source portal URL
    pub source_portal: String,
    /// Catalog record kind.
    pub record_kind: String,
    /// Similarity score (0.0 to 1.0)
    pub similarity_score: f32,
}

impl From<SearchResult> for SearchResultDto {
    fn from(r: SearchResult) -> Self {
        Self {
            id: r.dataset.id,
            title: r.dataset.title,
            description: r.dataset.description,
            url: r.dataset.url,
            source_portal: r.dataset.source_portal,
            record_kind: r.dataset.record_kind.to_string(),
            similarity_score: r.similarity_score,
        }
    }
}

// =============================================================================
// Portals
// =============================================================================

/// Portal information with sync status.
#[derive(Debug, Serialize, ToSchema)]
pub struct PortalInfoResponse {
    /// Portal name
    pub name: String,
    /// Portal base URL
    pub url: String,
    /// Portal type (ckan, socrata, dcat, opendatasoft, arcgis)
    pub portal_type: String,
    /// DCAT profile (e.g., "sparql"), if applicable
    pub profile: Option<String>,
    /// Custom SPARQL endpoint URL, if applicable
    pub sparql_endpoint: Option<String>,
    /// Custom OGC CSW endpoint URL, if applicable.
    pub ogc_endpoint: Option<String>,
    /// Whether the portal is enabled for harvesting
    pub enabled: bool,
    /// Portal description
    pub description: Option<String>,
    /// Last successful sync timestamp
    pub last_sync: Option<DateTime<Utc>>,
    /// Number of datasets from this portal
    pub dataset_count: Option<i64>,
}

/// Portal statistics response.
#[derive(Debug, Serialize, ToSchema)]
pub struct PortalStatsResponse {
    /// Portal name
    pub name: String,
    /// Portal URL
    pub url: String,
    /// Number of datasets from this portal
    pub dataset_count: i64,
    /// Last successful sync timestamp
    pub last_sync: Option<DateTime<Utc>>,
    /// Last sync mode (full or incremental)
    pub last_sync_mode: Option<String>,
    /// Last sync status (completed or cancelled)
    pub last_sync_status: Option<String>,
    /// Datasets synced in last sync
    pub last_sync_datasets: Option<i32>,
}

// =============================================================================
// Harvest
// =============================================================================

/// Harvest job response.
#[derive(Debug, Serialize, ToSchema)]
pub struct HarvestJobResponse {
    /// Job UUID
    pub job_id: Uuid,
    /// Current job status
    pub status: String,
    /// Target portal URL
    pub portal_url: String,
    /// Portal name
    pub portal_name: Option<String>,
    /// Job creation timestamp
    pub created_at: DateTime<Utc>,
    /// Job start timestamp
    pub started_at: Option<DateTime<Utc>>,
    /// Job completion timestamp
    pub completed_at: Option<DateTime<Utc>>,
    /// Final sync statistics
    pub sync_stats: Option<SyncStatsDto>,
    /// Error message if failed
    pub error_message: Option<String>,
}

impl From<HarvestJob> for HarvestJobResponse {
    fn from(job: HarvestJob) -> Self {
        Self {
            job_id: job.id,
            status: job.status.as_str().to_string(),
            portal_url: job.portal_url,
            portal_name: job.portal_name,
            created_at: job.created_at,
            started_at: job.started_at,
            completed_at: job.completed_at,
            sync_stats: job.sync_stats.map(SyncStatsDto::from),
            error_message: job.error_message,
        }
    }
}

/// Sync statistics for harvest operations.
#[derive(Debug, Serialize, ToSchema)]
pub struct SyncStatsDto {
    /// Datasets unchanged (no update needed)
    pub unchanged: usize,
    /// Datasets updated
    pub updated: usize,
    /// New datasets created
    pub created: usize,
    /// Datasets that failed processing
    pub failed: usize,
    /// Datasets skipped (circuit breaker)
    pub skipped: usize,
    /// Total datasets processed
    pub total: usize,
}

impl From<SyncStats> for SyncStatsDto {
    fn from(s: SyncStats) -> Self {
        Self {
            unchanged: s.unchanged,
            updated: s.updated,
            created: s.created,
            failed: s.failed,
            skipped: s.skipped,
            total: s.total(),
        }
    }
}

/// Harvest status overview.
#[derive(Debug, Serialize, ToSchema)]
pub struct HarvestStatusResponse {
    /// Number of pending jobs
    pub pending_jobs: i64,
    /// Number of running jobs
    pub running_jobs: i64,
    /// Recent harvest jobs
    pub recent_jobs: Vec<HarvestJobResponse>,
}

// =============================================================================
// Datasets
// =============================================================================

/// Dataset details response.
#[derive(Debug, Serialize, ToSchema)]
pub struct DatasetResponse {
    /// Dataset UUID
    pub id: Uuid,
    /// Original ID from source portal
    pub original_id: String,
    /// Source portal URL
    pub source_portal: String,
    /// Dataset landing page URL
    pub url: String,
    /// Dataset title
    pub title: String,
    /// Dataset description
    pub description: Option<String>,
    /// Catalog record kind.
    pub record_kind: String,
    /// Source-specific raw metadata with configured sensitive keys removed
    pub metadata: serde_json::Value,
    /// First indexed timestamp
    pub first_seen_at: DateTime<Utc>,
    /// Last update timestamp
    pub last_updated_at: DateTime<Utc>,
}

impl From<ceres_core::Dataset> for DatasetResponse {
    fn from(d: ceres_core::Dataset) -> Self {
        Self {
            id: d.id,
            original_id: d.original_id,
            source_portal: d.source_portal,
            url: d.url,
            title: d.title,
            description: d.description,
            record_kind: d.record_kind.to_string(),
            metadata: d.metadata,
            first_seen_at: d.first_seen_at,
            last_updated_at: d.last_updated_at,
        }
    }
}

/// Stable, normalized resource schema for a dataset.
///
/// This is the supported public contract for consuming resource and
/// distribution metadata. It is derived on read from the dataset's harvested
/// raw `metadata`; see [`ceres_core::DatasetSchema`].
#[derive(Debug, Serialize, ToSchema)]
#[schema(example = json!({
    "id": "2f1c1b44-6957-4c61-8823-3d77e91b024a",
    "original_id": "air-quality-2024",
    "source_portal": "https://data.example.org",
    "resources": [{
        "name": "Air quality observations",
        "format": "CSV",
        "media_type": "text/csv",
        "url": "https://data.example.org/download/air-quality.csv",
        "description": "Hourly station observations",
        "fields": [{
            "name": "station_id",
            "type": "string",
            "description": "Monitoring station identifier"
        }]
    }]
}))]
pub struct DatasetSchemaResponse {
    /// Dataset UUID
    #[schema(example = "2f1c1b44-6957-4c61-8823-3d77e91b024a")]
    pub id: Uuid,
    /// Original ID from source portal
    #[schema(example = "air-quality-2024")]
    pub original_id: String,
    /// Source portal URL
    #[schema(example = "https://data.example.org")]
    pub source_portal: String,
    /// Resources/distributions that make up the dataset; always present and
    /// empty when the harvested metadata exposes no normalizable resources.
    pub resources: Vec<DatasetResourceDto>,
}

/// A normalized resource or distribution within a dataset.
///
/// Nullable properties are always present in the JSON response and contain
/// `null` when the source portal did not provide a value.
#[derive(Debug, Serialize, ToSchema)]
pub struct DatasetResourceDto {
    /// Resource name/title, or `null` when unavailable.
    #[schema(required = true, nullable, example = "Air quality observations")]
    pub name: Option<String>,
    /// File format (e.g. "CSV", "JSON"), or `null` when unavailable.
    #[schema(required = true, nullable, example = "CSV")]
    pub format: Option<String>,
    /// MIME / media type (e.g. "text/csv"), or `null` when unavailable.
    #[schema(required = true, nullable, example = "text/csv")]
    pub media_type: Option<String>,
    /// Direct access URL for the resource, or `null` when unavailable.
    #[schema(
        required = true,
        nullable,
        example = "https://data.example.org/download/air-quality.csv"
    )]
    pub url: Option<String>,
    /// Resource description, or `null` when unavailable.
    #[schema(required = true, nullable, example = "Hourly station observations")]
    pub description: Option<String>,
    /// Column-level schema; always present and empty when unavailable.
    pub fields: Vec<ResourceFieldDto>,
}

/// A single field (column) within a resource's schema.
///
/// Nullable properties are always present in the JSON response and contain
/// `null` when the source portal did not provide a value.
#[derive(Debug, Serialize, ToSchema)]
pub struct ResourceFieldDto {
    /// Field/column name
    #[schema(example = "station_id")]
    pub name: String,
    /// Declared data type, or `null` when unavailable.
    #[schema(required = true, nullable, example = "string")]
    pub r#type: Option<String>,
    /// Field description, or `null` when unavailable.
    #[schema(required = true, nullable, example = "Monitoring station identifier")]
    pub description: Option<String>,
}

impl From<ceres_core::Dataset> for DatasetSchemaResponse {
    fn from(d: ceres_core::Dataset) -> Self {
        let schema = ceres_core::DatasetSchema::from_metadata(&d.metadata);
        Self {
            id: d.id,
            original_id: d.original_id,
            source_portal: d.source_portal,
            resources: schema
                .resources
                .into_iter()
                .map(DatasetResourceDto::from)
                .collect(),
        }
    }
}

impl From<ceres_core::DatasetResource> for DatasetResourceDto {
    fn from(r: ceres_core::DatasetResource) -> Self {
        Self {
            name: r.name,
            format: r.format,
            media_type: r.media_type,
            url: r.url,
            description: r.description,
            fields: r.fields.into_iter().map(ResourceFieldDto::from).collect(),
        }
    }
}

impl From<ceres_core::ResourceField> for ResourceFieldDto {
    fn from(f: ceres_core::ResourceField) -> Self {
        Self {
            name: f.name,
            r#type: f.r#type,
            description: f.description,
        }
    }
}