vector/api/schema/
health.rs

1use async_graphql::{Object, SimpleObject, Subscription};
2use chrono::{DateTime, Utc};
3use tokio::time::Duration;
4use tokio_stream::{wrappers::IntervalStream, Stream, StreamExt};
5
6#[derive(SimpleObject)]
7pub struct Heartbeat {
8    utc: DateTime<Utc>,
9}
10
11impl Heartbeat {
12    fn new() -> Self {
13        Heartbeat { utc: Utc::now() }
14    }
15}
16
17#[derive(Default)]
18pub(super) struct HealthQuery;
19
20#[Object]
21impl HealthQuery {
22    /// Returns `true` to denote the GraphQL server is reachable
23    async fn health(&self) -> bool {
24        true
25    }
26}
27
28#[derive(Default)]
29pub struct HealthSubscription;
30
31#[Subscription]
32impl HealthSubscription {
33    /// Heartbeat, containing the UTC timestamp of the last server-sent payload
34    async fn heartbeat(
35        &self,
36        #[graphql(default = 1000, validator(minimum = 10, maximum = 60_000))] interval: i32,
37    ) -> impl Stream<Item = Heartbeat> + use<> {
38        IntervalStream::new(tokio::time::interval(Duration::from_millis(
39            interval as u64,
40        )))
41        .map(|_| Heartbeat::new())
42    }
43}