1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Save the session activity in the database

This commit is contained in:
Quentin Gliech
2023-09-19 19:02:59 +02:00
parent 407c78a7be
commit b85655b944
14 changed files with 352 additions and 5 deletions

View File

@ -15,7 +15,7 @@
use std::{collections::HashMap, net::IpAddr};
use chrono::{DateTime, Utc};
use mas_storage::Repository;
use mas_storage::{user::BrowserSessionRepository, Repository, RepositoryAccess};
use opentelemetry::{
metrics::{Counter, Histogram},
Key,
@ -38,6 +38,8 @@ const RESULT: Key = Key::from_static_str("result");
#[derive(Clone, Copy, Debug)]
struct ActivityRecord {
// XXX: We don't actually use the start time for now
#[allow(dead_code)]
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
ip: Option<IpAddr>,
@ -195,18 +197,47 @@ impl Worker {
}
/// Fallible part of [`Self::flush`].
#[tracing::instrument(name = "activity_tracker.flush", skip(self))]
async fn try_flush(&mut self) -> Result<(), anyhow::Error> {
let pending_records = &self.pending_records;
let repo = mas_storage_pg::PgRepository::from_pool(&self.pool)
let mut repo = mas_storage_pg::PgRepository::from_pool(&self.pool)
.await?
.boxed();
let mut browser_sessions = Vec::new();
let mut oauth2_sessions = Vec::new();
let mut compat_sessions = Vec::new();
for ((kind, id), record) in pending_records {
match kind {
SessionKind::Browser => {
browser_sessions.push((*id, record.end_time, record.ip));
}
SessionKind::OAuth2 => {
oauth2_sessions.push((*id, record.end_time, record.ip));
}
SessionKind::Compat => {
compat_sessions.push((*id, record.end_time, record.ip));
}
}
}
tracing::info!(
"Flushing {} activity records to the database",
pending_records.len()
);
// TODO: actually save the records
repo.browser_session()
.record_batch_activity(browser_sessions)
.await?;
repo.oauth2_session()
.record_batch_activity(oauth2_sessions)
.await?;
repo.compat_session()
.record_batch_activity(compat_sessions)
.await?;
repo.save().await?;
self.pending_records.clear();