• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 5765110266

04 Aug 2023 05:53PM UTC coverage: 61.195% (+0.02%) from 61.175%
5765110266

push

github

web-flow
Bump divviup-api to 0.0.17. (#367)

3124 of 5105 relevant lines covered (61.19%)

122.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.64
/src/entity/task.rs
1
use crate::{
2
    clients::aggregator_client::{api_types::TaskResponse, TaskMetrics},
3
    entity::{
4
        account, membership, AccountColumn, Accounts, Aggregator, AggregatorColumn, Aggregators,
5
    },
6
};
7
use sea_orm::{
8
    ActiveModelBehavior, ActiveModelTrait, ActiveValue, ConnectionTrait, DbErr, DeriveEntityModel,
9
    DerivePrimaryKey, DeriveRelation, EntityTrait, EnumIter, IntoActiveModel, PrimaryKeyTrait,
10
    Related, RelationDef, RelationTrait,
11
};
12
use serde::{Deserialize, Serialize};
13
use time::OffsetDateTime;
14
use uuid::Uuid;
15
use validator::{Validate, ValidationError};
16

17
pub mod vdaf;
18
use vdaf::Vdaf;
19
mod new_task;
20
pub use new_task::NewTask;
21
mod update_task;
22
pub use update_task::UpdateTask;
23
mod provisionable_task;
24
pub use provisionable_task::{ProvisionableTask, TaskProvisioningError};
25

26
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
42,895✔
27
#[sea_orm(table_name = "task")]
28
pub struct Model {
29
    #[sea_orm(primary_key, auto_increment = false)]
30
    pub id: String,
31
    pub account_id: Uuid,
32
    pub name: String,
33
    pub vdaf: Vdaf,
34
    pub min_batch_size: i64,
35
    pub max_batch_size: Option<i64>,
36
    #[serde(with = "time::serde::iso8601")]
37
    pub created_at: OffsetDateTime,
38
    #[serde(with = "time::serde::iso8601")]
39
    pub updated_at: OffsetDateTime,
40
    pub time_precision_seconds: i32,
41
    pub report_count: i32,
42
    pub aggregate_collection_count: i32,
43
    #[serde(default, with = "time::serde::iso8601::option")]
44
    pub expiration: Option<OffsetDateTime>,
45
    pub leader_aggregator_id: Uuid,
46
    pub helper_aggregator_id: Uuid,
47
}
48

49
impl Model {
50
    pub async fn update_metrics(
1✔
51
        self,
1✔
52
        metrics: TaskMetrics,
1✔
53
        db: impl ConnectionTrait,
1✔
54
    ) -> Result<Self, DbErr> {
1✔
55
        let mut task = self.into_active_model();
1✔
56
        task.report_count = ActiveValue::Set(metrics.reports.try_into().unwrap_or(i32::MAX));
1✔
57
        task.aggregate_collection_count =
1✔
58
            ActiveValue::Set(metrics.report_aggregations.try_into().unwrap_or(i32::MAX));
1✔
59
        task.updated_at = ActiveValue::Set(OffsetDateTime::now_utc());
1✔
60
        task.update(&db).await
1✔
61
    }
1✔
62

63
    pub async fn leader_aggregator(
7✔
64
        &self,
7✔
65
        db: &impl ConnectionTrait,
7✔
66
    ) -> Result<super::Aggregator, DbErr> {
7✔
67
        super::Aggregators::find_by_id(self.leader_aggregator_id)
7✔
68
            .one(db)
7✔
69
            .await
8✔
70
            .transpose()
7✔
71
            .ok_or(DbErr::Custom("expected leader aggregator".into()))?
7✔
72
    }
7✔
73

74
    pub async fn helper_aggregator(&self, db: &impl ConnectionTrait) -> Result<Aggregator, DbErr> {
2✔
75
        Aggregators::find_by_id(self.helper_aggregator_id)
2✔
76
            .one(db)
2✔
77
            .await
×
78
            .transpose()
2✔
79
            .ok_or(DbErr::Custom("expected helper aggregator".into()))?
2✔
80
    }
2✔
81

82
    pub async fn aggregators(&self, db: &impl ConnectionTrait) -> Result<[Aggregator; 2], DbErr> {
2✔
83
        futures_lite::future::try_zip(self.leader_aggregator(db), self.helper_aggregator(db))
2✔
84
            .await
×
85
            .map(|(leader, helper)| [leader, helper])
2✔
86
    }
2✔
87

88
    pub async fn first_party_aggregator(
2✔
89
        &self,
2✔
90
        db: &impl ConnectionTrait,
2✔
91
    ) -> Result<Option<Aggregator>, DbErr> {
2✔
92
        Ok(self
2✔
93
            .aggregators(db)
2✔
94
            .await?
×
95
            .into_iter()
2✔
96
            .find(|agg| agg.is_first_party))
4✔
97
    }
2✔
98
}
99

100
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
1,582✔
101
pub enum Relation {
102
    #[sea_orm(
103
        belongs_to = "Accounts",
104
        from = "Column::AccountId",
105
        to = "AccountColumn::Id"
106
    )]
107
    Account,
108

109
    #[sea_orm(
110
        belongs_to = "Aggregators",
111
        from = "Column::HelperAggregatorId",
112
        to = "AggregatorColumn::Id"
113
    )]
114
    HelperAggregator,
115

116
    #[sea_orm(
117
        belongs_to = "Aggregators",
118
        from = "Column::LeaderAggregatorId",
119
        to = "AggregatorColumn::Id"
120
    )]
121
    LeaderAggregator,
122
}
123

124
impl Related<account::Entity> for Entity {
125
    fn to() -> RelationDef {
231✔
126
        Relation::Account.def()
231✔
127
    }
231✔
128
}
129

130
impl Related<membership::Entity> for Entity {
131
    fn to() -> RelationDef {
×
132
        account::Relation::Memberships.def()
×
133
    }
×
134

135
    fn via() -> Option<RelationDef> {
×
136
        Some(account::Relation::Tasks.def().rev())
×
137
    }
×
138
}
139

140
impl ActiveModelBehavior for ActiveModel {}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc