Highly ambitious ATProtocol AppView service and sdks
1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5pub use crate::database::{SortField, WhereClause, WhereCondition};
6
7#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
8#[serde(rename_all = "camelCase")]
9pub struct Record {
10 pub uri: String,
11 pub cid: String,
12 pub did: String,
13 pub collection: String,
14 pub json: Value,
15 pub indexed_at: DateTime<Utc>,
16 pub slice_uri: Option<String>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct IndexedRecord {
22 pub uri: String,
23 pub cid: String,
24 pub did: String,
25 pub collection: String,
26 pub value: Value,
27 pub indexed_at: String,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct BulkSyncParams {
33 pub collections: Option<Vec<String>>,
34 pub external_collections: Option<Vec<String>>,
35 pub repos: Option<Vec<String>>,
36 pub limit_per_repo: Option<i32>,
37 pub skip_validation: Option<bool>,
38 pub max_repos: Option<i32>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
42#[serde(rename_all = "camelCase")]
43pub struct Actor {
44 pub did: String,
45 pub handle: Option<String>,
46 pub slice_uri: String,
47 pub indexed_at: String,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(rename_all = "camelCase")]
52pub struct CollectionStats {
53 pub collection: String,
54 pub record_count: i64,
55 pub unique_actors: i64,
56}
57
58#[derive(Debug, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase")]
60pub struct SliceRecordsParams {
61 pub slice: String,
62 pub limit: Option<i32>,
63 pub cursor: Option<String>,
64 #[serde(rename = "where")]
65 pub where_clause: Option<WhereClause>,
66 pub sort_by: Option<Vec<SortField>>,
67}
68
69#[derive(Debug, Serialize, Deserialize)]
70#[serde(rename_all = "camelCase")]
71pub struct SliceRecordsOutput {
72 pub records: Vec<IndexedRecord>,
73 pub cursor: Option<String>,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
77#[serde(rename_all = "camelCase")]
78pub struct OAuthClient {
79 pub id: i32,
80 pub slice_uri: String,
81 pub client_id: String,
82 pub registration_access_token: Option<String>,
83 pub created_at: DateTime<Utc>,
84 pub created_by_did: String,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89pub struct SparklinePoint {
90 pub timestamp: String,
91 pub count: i64,
92}
93
94/// Date interval for date truncation in aggregations
95#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(rename_all = "lowercase")]
97pub enum DateInterval {
98 Second,
99 Minute,
100 Hour,
101 Day,
102 Week,
103 Month,
104 Quarter,
105 Year,
106}
107
108impl DateInterval {
109 /// Convert to PostgreSQL date_trunc() interval string
110 pub fn to_pg_interval(&self) -> &'static str {
111 match self {
112 DateInterval::Second => "second",
113 DateInterval::Minute => "minute",
114 DateInterval::Hour => "hour",
115 DateInterval::Day => "day",
116 DateInterval::Week => "week",
117 DateInterval::Month => "month",
118 DateInterval::Quarter => "quarter",
119 DateInterval::Year => "year",
120 }
121 }
122}
123
124/// Group by field specification for aggregations
125#[derive(Debug, Clone)]
126pub enum GroupByField {
127 /// Simple field name
128 Simple(String),
129 /// Date-truncated field
130 Truncated {
131 field: String,
132 interval: DateInterval,
133 },
134}
135
136impl GroupByField {
137 /// Get the field name
138 pub fn field_name(&self) -> &str {
139 match self {
140 GroupByField::Simple(name) => name,
141 GroupByField::Truncated { field, .. } => field,
142 }
143 }
144}