mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 04:01:58 +00:00
metrics wip
This commit is contained in:
@@ -1,19 +1,28 @@
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
use chrono::Utc;
|
||||||
use futures::future::ready;
|
use futures::future::ready;
|
||||||
use rpc_toolkit::{Context, ParentHandler, Server};
|
use rpc_toolkit::{from_fn_async, Context, HandlerExt, ParentHandler, Server};
|
||||||
|
use sqlx::query;
|
||||||
|
use ts_rs::TS;
|
||||||
|
|
||||||
use crate::analytics::context::AnalyticsContext;
|
use crate::analytics::context::AnalyticsContext;
|
||||||
use crate::middleware::cors::Cors;
|
use crate::middleware::cors::Cors;
|
||||||
use crate::net::static_server::{bad_request, not_found, server_error};
|
use crate::net::static_server::{bad_request, not_found, server_error};
|
||||||
use crate::net::web_server::WebServer;
|
use crate::net::web_server::WebServer;
|
||||||
use crate::rpc_continuations::Guid;
|
use crate::rpc_continuations::Guid;
|
||||||
|
use crate::Error;
|
||||||
|
|
||||||
pub mod context;
|
pub mod context;
|
||||||
|
|
||||||
pub fn analytics_api<C: Context>() -> ParentHandler<C> {
|
pub fn analytics_api<C: Context>() -> ParentHandler<C> {
|
||||||
ParentHandler::new() // TODO: FullMetal
|
ParentHandler::new()
|
||||||
|
.subcommand("recordMetrics", from_fn_async(record_metrics).no_cli())
|
||||||
|
.subcommand(
|
||||||
|
"recordUserActivity",
|
||||||
|
from_fn_async(record_user_activity).no_cli(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analytics_server_router(ctx: AnalyticsContext) -> Router {
|
pub fn analytics_server_router(ctx: AnalyticsContext) -> Router {
|
||||||
@@ -76,3 +85,59 @@ impl WebServer {
|
|||||||
Self::new(bind, analytics_server_router(ctx))
|
Self::new(bind, analytics_server_router(ctx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, TS)]
|
||||||
|
#[ts(export)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct MetricsParams {
|
||||||
|
version: char,
|
||||||
|
pkg_id: char,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, TS)]
|
||||||
|
#[ts(export)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct ActivityParams {
|
||||||
|
server_id: String,
|
||||||
|
os_version: String,
|
||||||
|
arch: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn record_metrics(
|
||||||
|
ctx: AnalyticsContext,
|
||||||
|
MetricsParams {
|
||||||
|
version,
|
||||||
|
pkg_id,
|
||||||
|
}: MetricsParams,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let pool = ctx.db;
|
||||||
|
let created_at = Utc::now().to_rfc3339();
|
||||||
|
query!(
|
||||||
|
"INSERT INTO metric (created_at, version, pkg_id) VALUES ($1, $2, $3)",
|
||||||
|
created_at,
|
||||||
|
version,
|
||||||
|
pkg_id
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn record_user_activity(
|
||||||
|
analytics_ctx: AnalyticsContext,
|
||||||
|
ActivityParams { server_id, os_version, arch }: ActivityParams,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let pool = analytics_ctx.db;
|
||||||
|
let created_at = Utc::now().to_rfc3339();
|
||||||
|
|
||||||
|
query!("INSERT INTO user_activity (created_at, server_id, os_version, arch) VALUES ($1, $2, $3, $4)",
|
||||||
|
created_at,
|
||||||
|
server_id,
|
||||||
|
os_vers,
|
||||||
|
arch
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user