cognite/api/core/
events.rs

1use serde::Serialize;
2use std::collections::HashSet;
3
4use crate::api::resource::*;
5use crate::dto::core::event::*;
6use crate::error::Result;
7use crate::utils::lease::CleanResource;
8use crate::{IdentityList, ItemsVec, Patch};
9
10/// Event objects store complex information about multiple assets over a time period.
11/// Typical types of events might include Alarms, Process Data, and Logs.
12///
13/// For storage of low volume, manually generated, schedulable activities such as
14/// maintenance schedules, work orders, or other "appointment" type activities. The Data Modelling
15/// service is now recommended.
16///
17/// For storage of very high volume discrete events, consider using time series.
18pub type EventsResource = Resource<Event>;
19
20impl WithBasePath for EventsResource {
21    const BASE_PATH: &'static str = "events";
22}
23
24impl Create<AddEvent, Event> for EventsResource {}
25impl<R> DeleteWithIgnoreUnknownIds<IdentityList<R>> for EventsResource
26where
27    IdentityList<R>: Serialize,
28    R: Send + Sync,
29{
30}
31impl Update<Patch<PatchEvent>, Event> for EventsResource {}
32impl<R> RetrieveWithIgnoreUnknownIds<IdentityList<R>, Event> for EventsResource
33where
34    IdentityList<R>: Serialize,
35    R: Send + Sync,
36{
37}
38impl SearchItems<'_, EventFilter, EventSearch, Event> for EventsResource {}
39impl FilterWithRequest<EventFilterQuery, Event> for EventsResource {}
40
41impl EventsResource {
42    /// Compute aggregates over events, such as getting the count of all events in a project,
43    /// checking different names and descriptions of events in your project, etc.
44    ///
45    /// # Arguments
46    ///
47    /// * `aggregate` - Aggregate to compute
48    ///
49    /// The returned aggregates depend on which aggregates were requested.
50    pub async fn aggregate(
51        &self,
52        aggregate: EventAggregateRequest,
53    ) -> Result<Vec<EventAggregateResponse>> {
54        let resp: ItemsVec<EventAggregateResponse> =
55            self.api_client.post("events/aggregate", &aggregate).await?;
56        Ok(resp.items)
57    }
58}
59
60impl CleanResource<Event> for EventsResource {
61    async fn clean_resource(&self, resources: Vec<Event>) -> std::result::Result<(), crate::Error> {
62        let ids = resources.iter().map(|a| a.id).collect::<HashSet<i64>>();
63        self.delete(&ids.into_iter().collect::<Vec<_>>(), true)
64            .await
65    }
66}