| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use sqlx::PgPool; |
| 6 |
|
| 7 |
use crate::db::enums::SyncPlatform; |
| 8 |
use crate::db::models::DbSyncDevice; |
| 9 |
use crate::db::{SyncAppId, SyncDeviceId, UserId}; |
| 10 |
use crate::error::Result; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#[tracing::instrument(skip_all)] |
| 25 |
pub async fn upsert_sync_device( |
| 26 |
pool: &PgPool, |
| 27 |
app_id: SyncAppId, |
| 28 |
user_id: UserId, |
| 29 |
device_name: &str, |
| 30 |
platform: SyncPlatform, |
| 31 |
client_version: Option<&str>, |
| 32 |
) -> Result<DbSyncDevice> { |
| 33 |
let device = sqlx::query_as::<_, DbSyncDevice>( |
| 34 |
r" |
| 35 |
INSERT INTO sync_devices (app_id, user_id, device_name, platform, client_version) |
| 36 |
VALUES ($1, $2, $3, $4, $5) |
| 37 |
ON CONFLICT (app_id, user_id, device_name) |
| 38 |
DO UPDATE SET |
| 39 |
platform = EXCLUDED.platform, |
| 40 |
last_seen_at = NOW(), |
| 41 |
client_version = COALESCE(EXCLUDED.client_version, sync_devices.client_version) |
| 42 |
RETURNING * |
| 43 |
", |
| 44 |
) |
| 45 |
.bind(app_id) |
| 46 |
.bind(user_id) |
| 47 |
.bind(device_name) |
| 48 |
.bind(platform) |
| 49 |
.bind(client_version) |
| 50 |
.fetch_one(pool) |
| 51 |
.await?; |
| 52 |
|
| 53 |
Ok(device) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#[tracing::instrument(skip_all)] |
| 58 |
pub async fn get_sync_devices( |
| 59 |
pool: &PgPool, |
| 60 |
app_id: SyncAppId, |
| 61 |
user_id: UserId, |
| 62 |
) -> Result<Vec<DbSyncDevice>> { |
| 63 |
let devices = sqlx::query_as::<_, DbSyncDevice>( |
| 64 |
"SELECT * FROM sync_devices WHERE app_id = $1 AND user_id = $2 ORDER BY last_seen_at DESC LIMIT 100", |
| 65 |
) |
| 66 |
.bind(app_id) |
| 67 |
.bind(user_id) |
| 68 |
.fetch_all(pool) |
| 69 |
.await?; |
| 70 |
|
| 71 |
Ok(devices) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#[tracing::instrument(skip_all)] |
| 79 |
pub async fn sync_device_belongs( |
| 80 |
pool: &PgPool, |
| 81 |
device_id: SyncDeviceId, |
| 82 |
app_id: SyncAppId, |
| 83 |
user_id: UserId, |
| 84 |
) -> Result<bool> { |
| 85 |
let exists: bool = sqlx::query_scalar( |
| 86 |
"SELECT EXISTS(SELECT 1 FROM sync_devices WHERE id = $1 AND app_id = $2 AND user_id = $3)", |
| 87 |
) |
| 88 |
.bind(device_id) |
| 89 |
.bind(app_id) |
| 90 |
.bind(user_id) |
| 91 |
.fetch_one(pool) |
| 92 |
.await?; |
| 93 |
Ok(exists) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#[tracing::instrument(skip_all)] |
| 105 |
pub async fn touch_and_advance_cursor( |
| 106 |
pool: &PgPool, |
| 107 |
device_id: SyncDeviceId, |
| 108 |
new_cursor: i64, |
| 109 |
client_version: Option<&str>, |
| 110 |
) -> Result<()> { |
| 111 |
sqlx::query( |
| 112 |
"UPDATE sync_devices |
| 113 |
SET last_seen_at = NOW(), |
| 114 |
last_pulled_seq = GREATEST(last_pulled_seq, $2), |
| 115 |
client_version = COALESCE($3, client_version) |
| 116 |
WHERE id = $1", |
| 117 |
) |
| 118 |
.bind(device_id) |
| 119 |
.bind(new_cursor) |
| 120 |
.bind(client_version) |
| 121 |
.execute(pool) |
| 122 |
.await?; |
| 123 |
Ok(()) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
#[tracing::instrument(skip_all)] |
| 128 |
pub async fn delete_sync_device( |
| 129 |
pool: &PgPool, |
| 130 |
device_id: SyncDeviceId, |
| 131 |
app_id: SyncAppId, |
| 132 |
user_id: UserId, |
| 133 |
) -> Result<bool> { |
| 134 |
let result = |
| 135 |
sqlx::query("DELETE FROM sync_devices WHERE id = $1 AND app_id = $2 AND user_id = $3") |
| 136 |
.bind(device_id) |
| 137 |
.bind(app_id) |
| 138 |
.bind(user_id) |
| 139 |
.execute(pool) |
| 140 |
.await?; |
| 141 |
|
| 142 |
Ok(result.rows_affected() > 0) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[derive(Debug, Clone, sqlx::FromRow, serde::Serialize)] |
| 148 |
pub struct ClientVersionCount { |
| 149 |
|
| 150 |
pub client_version: Option<String>, |
| 151 |
|
| 152 |
pub devices: i64, |
| 153 |
|
| 154 |
pub last_seen_at: chrono::DateTime<chrono::Utc>, |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#[tracing::instrument(skip_all)] |
| 165 |
pub async fn client_version_distribution( |
| 166 |
pool: &PgPool, |
| 167 |
window_days: i32, |
| 168 |
) -> Result<Vec<ClientVersionCount>> { |
| 169 |
let rows = sqlx::query_as::<_, ClientVersionCount>( |
| 170 |
"SELECT client_version, COUNT(*) AS devices, MAX(last_seen_at) AS last_seen_at |
| 171 |
FROM sync_devices |
| 172 |
WHERE last_seen_at > NOW() - make_interval(days => $1) |
| 173 |
GROUP BY client_version |
| 174 |
ORDER BY devices DESC, client_version DESC NULLS LAST", |
| 175 |
) |
| 176 |
.bind(window_days) |
| 177 |
.fetch_all(pool) |
| 178 |
.await?; |
| 179 |
|
| 180 |
Ok(rows) |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
#[tracing::instrument(skip_all)] |
| 185 |
pub async fn touch_sync_device(pool: &PgPool, device_id: SyncDeviceId) -> Result<()> { |
| 186 |
sqlx::query("UPDATE sync_devices SET last_seen_at = NOW() WHERE id = $1") |
| 187 |
.bind(device_id) |
| 188 |
.execute(pool) |
| 189 |
.await?; |
| 190 |
|
| 191 |
Ok(()) |
| 192 |
} |
| 193 |
|