Skip to content

Commit 38ca559

Browse files
AutoProc Scaling and statics DataLoaders
1 parent e826208 commit 38ca559

File tree

1 file changed

+80
-15
lines changed
  • processed_data/src/graphql

1 file changed

+80
-15
lines changed

processed_data/src/graphql/mod.rs

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ pub fn root_schema_builder(
5555
AutoProcDataLoader::new(database.clone()),
5656
tokio::spawn,
5757
))
58+
.data(DataLoader::new(
59+
AutoProcScalingDataLoader::new(database.clone()),
60+
tokio::spawn,
61+
))
62+
.data(DataLoader::new(
63+
AutoProcScalingStaticsDL::new(database.clone()),
64+
tokio::spawn,
65+
))
5866
.data(database)
5967
.enable_federation()
6068
}
@@ -69,6 +77,8 @@ pub struct ProcessingJobParameterDataLoader(DatabaseConnection);
6977
pub struct AutoProcIntegrationDataLoader(DatabaseConnection);
7078
pub struct AutoProcProgramDataLoader(DatabaseConnection);
7179
pub struct AutoProcDataLoader(DatabaseConnection);
80+
pub struct AutoProcScalingDataLoader(DatabaseConnection);
81+
pub struct AutoProcScalingStaticsDL(DatabaseConnection);
7282

7383
impl ProcessingJobDataLoader {
7484
fn new(database: DatabaseConnection) -> Self {
@@ -106,6 +116,18 @@ impl AutoProcDataLoader {
106116
}
107117
}
108118

119+
impl AutoProcScalingDataLoader {
120+
fn new(database: DatabaseConnection) -> Self {
121+
Self(database)
122+
}
123+
}
124+
125+
impl AutoProcScalingStaticsDL {
126+
fn new(database: DatabaseConnection) -> Self {
127+
Self(database)
128+
}
129+
}
130+
109131
impl Loader<u32> for ProcessedDataLoader {
110132
type Value = DataProcessing;
111133
type Error = async_graphql::Error;
@@ -254,6 +276,60 @@ impl Loader<u32> for AutoProcDataLoader {
254276
}
255277
}
256278

279+
impl Loader<u32> for AutoProcScalingDataLoader {
280+
type Value = AutoProcScaling;
281+
type Error = async_graphql::Error;
282+
283+
#[instrument(name = "load_auto_proc_scaling", skip(self))]
284+
async fn load(&self, keys: &[u32]) -> Result<HashMap<u32, Self::Value>, Self::Error> {
285+
let mut results = HashMap::new();
286+
let keys_vec: Vec<u32> = keys.iter().cloned().collect();
287+
let records = auto_proc_scaling::Entity::find()
288+
.filter(auto_proc_scaling::Column::AutoProcId.is_in(keys_vec))
289+
.all(&self.0)
290+
.await?;
291+
292+
for record in records {
293+
let auto_proc_id = record.auto_proc_id.unwrap();
294+
let data = AutoProcScaling::from(record);
295+
results.insert(auto_proc_id, data);
296+
}
297+
298+
Ok(results)
299+
}
300+
}
301+
302+
// .filter(
303+
// auto_proc_scaling_statistics::Column::AutoProcScalingId
304+
// .eq(self.auto_proc_scaling_id),
305+
// )
306+
// .one(database)
307+
// .await?
308+
// .map(AutoProcScalingStatics::from))
309+
310+
impl Loader<u32> for AutoProcScalingStaticsDL {
311+
type Value = AutoProcScalingStatics;
312+
type Error = async_graphql::Error;
313+
314+
#[instrument(name = "load_auto_proc_scaling_statics", skip(self))]
315+
async fn load(&self, keys: &[u32]) -> Result<HashMap<u32, Self::Value>, Self::Error> {
316+
let mut results = HashMap::new();
317+
let keys_vec: Vec<u32> = keys.iter().cloned().collect();
318+
let records = auto_proc_scaling_statistics::Entity::find()
319+
.filter(auto_proc_scaling_statistics::Column::AutoProcScalingId.is_in(keys_vec))
320+
.all(&self.0)
321+
.await?;
322+
323+
for record in records {
324+
let auto_proc_scaling_id = record.auto_proc_scaling_id.unwrap();
325+
let data = AutoProcScalingStatics::from(record);
326+
results.insert(auto_proc_scaling_id, data);
327+
}
328+
329+
Ok(results)
330+
}
331+
}
332+
257333
#[ComplexObject]
258334
impl DataCollection {
259335
/// Fetched all the processed data from data collection during a session
@@ -340,12 +416,8 @@ impl AutoProcProgram {
340416
impl AutoProc {
341417
/// Fetches the scaling for automatic process
342418
async fn scaling(&self, ctx: &Context<'_>) -> async_graphql::Result<Option<AutoProcScaling>> {
343-
let database = ctx.data::<DatabaseConnection>()?;
344-
Ok(auto_proc_scaling::Entity::find()
345-
.filter(auto_proc_scaling::Column::AutoProcId.eq(self.auto_proc_id))
346-
.one(database)
347-
.await?
348-
.map(AutoProcScaling::from))
419+
let loader = ctx.data_unchecked::<DataLoader<AutoProcScalingDataLoader>>();
420+
Ok(loader.load_one(self.auto_proc_id).await?)
349421
}
350422
}
351423

@@ -356,15 +428,8 @@ impl AutoProcScaling {
356428
&self,
357429
ctx: &Context<'_>,
358430
) -> async_graphql::Result<Option<AutoProcScalingStatics>> {
359-
let database = ctx.data::<DatabaseConnection>()?;
360-
Ok(auto_proc_scaling_statistics::Entity::find()
361-
.filter(
362-
auto_proc_scaling_statistics::Column::AutoProcScalingId
363-
.eq(self.auto_proc_scaling_id),
364-
)
365-
.one(database)
366-
.await?
367-
.map(AutoProcScalingStatics::from))
431+
let loader = ctx.data_unchecked::<DataLoader<AutoProcScalingStaticsDL>>();
432+
Ok(loader.load_one(self.auto_proc_scaling_id).await?)
368433
}
369434
}
370435

0 commit comments

Comments
 (0)