Skip to content

Commit

Permalink
Dataloader testing
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvigneshwars committed Apr 10, 2024
1 parent 295a3ea commit eed8ea4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charts/processed_data/charts/processed_data/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ type: application

version: 0.1.0

appVersion: 0.1.0-rc4
appVersion: 0.1.0-rc5
1 change: 1 addition & 0 deletions processed_data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-graphql = { version = "7.0.2", default-features = false, features = [
"chrono",
"graphiql",
"tracing",
"dataloader",
] }
async-graphql-axum = { version = "7.0.2" }
aws-credential-types = { version = "0.56.0" }
Expand Down
58 changes: 47 additions & 11 deletions processed_data/src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod entities;
use crate::S3Bucket;
use async_graphql::{
dataloader::{DataLoader, Loader},
ComplexObject, Context, EmptyMutation, EmptySubscription, Object, Schema, SchemaBuilder,
};
use aws_sdk_s3::presigning::PresigningConfig;
Expand All @@ -15,7 +16,9 @@ use models::{
processing_job_parameter,
};
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
use std::collections::HashMap;
use std::time::Duration;
use tracing::instrument;

Check warning on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `tracing::instrument`

Check warning on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / generate

unused import: `tracing::instrument`

Check failure on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `tracing::instrument`

Check warning on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `tracing::instrument`

Check warning on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / generate

unused import: `tracing::instrument`

Check failure on line 21 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `tracing::instrument`
use url::Url;

use self::entities::AutoProcProgram;
Expand All @@ -24,29 +27,62 @@ use self::entities::AutoProcProgram;
pub type RootSchema = Schema<Query, EmptyMutation, EmptySubscription>;

/// A schema builder for the service
pub fn root_schema_builder() -> SchemaBuilder<Query, EmptyMutation, EmptySubscription> {
Schema::build(Query, EmptyMutation, EmptySubscription).enable_federation()
pub fn root_schema_builder(
database: DatabaseConnection,
) -> SchemaBuilder<Query, EmptyMutation, EmptySubscription> {
Schema::build(Query, EmptyMutation, EmptySubscription)
.data(DataLoader::new(
DataCollectionLoader::new(database.clone()),
tokio::spawn,
))
.data(database)
.enable_federation()
}

/// The root query of the service
#[derive(Debug, Clone, Default)]
pub struct Query;

pub struct DataCollectionLoader(DatabaseConnection);

Check failure on line 46 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for a struct

Check failure on line 46 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for a struct

impl DataCollectionLoader {
fn new(database: DatabaseConnection) -> Self {

Check failure on line 49 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for an associated function

Check failure on line 49 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for an associated function
Self(database)
}
}

impl Loader<u32> for DataCollectionLoader {
type Value = DataProcessing;
type Error = async_graphql::Error;

async fn load(&self, keys: &[u32]) -> Result<HashMap<u32, Self::Value>, Self::Error> {
let mut results = HashMap::new();
let keys_vec: Vec<u32> = keys.iter().cloned().collect();

Check failure on line 60 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable

Check failure on line 60 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
let records = data_collection_file_attachment::Entity::find()
.filter(data_collection_file_attachment::Column::DataCollectionId.is_in(keys_vec))
.all(&self.0)
.await?;

for record in records {
let data_collection_id = record.data_collection_id;
let data = DataProcessing::from(record);

results.insert(data_collection_id, data);
}

Ok(results)
}
}

#[ComplexObject]
impl DataCollection {
/// Fetched all the processed data from data collection during a session
async fn processed_data(
&self,
ctx: &Context<'_>,
) -> Result<Vec<DataProcessing>, async_graphql::Error> {
let database = ctx.data::<DatabaseConnection>()?;
Ok(data_collection_file_attachment::Entity::find()
.filter(data_collection_file_attachment::Column::DataCollectionId.eq(self.id))
.all(database)
.await?
.into_iter()
.map(DataProcessing::from)
.collect())
) -> Result<Option<DataProcessing>, async_graphql::Error> {
let loader = ctx.data_unchecked::<DataLoader<DataCollectionLoader>>();
Ok(loader.load_one(self.id).await?)

Check failure on line 85 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

question mark operator is useless here

Check failure on line 85 in processed_data/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / lint

question mark operator is useless here
}

/// Fetched all the processing jobs
Expand Down
7 changes: 5 additions & 2 deletions processed_data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ struct SchemaArgs {
/// The path to write the schema to, if not set the schema will be printed to stdout
#[arg(short, long)]
path: Option<PathBuf>,
#[arg(long, env = "DATABASE_URL")]
database_url: Url,

Check failure on line 130 in processed_data/src/main.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for a struct field

Check failure on line 130 in processed_data/src/main.rs

View workflow job for this annotation

GitHub Actions / lint

missing documentation for a struct field
}

/// Creates a connection pool to access the database
Expand Down Expand Up @@ -239,12 +241,13 @@ async fn main() {
Cli::Serve(args) => {
setup_telemetry(args.log_level, args.otel_collector_url).unwrap();
let database = setup_database(args.database_url).await.unwrap();
let schema = root_schema_builder().data(database).finish();
let schema = root_schema_builder(database).finish();
let router = setup_router(schema);
serve(router, args.port).await.unwrap();
}
Cli::Schema(args) => {
let schema = root_schema_builder().finish();
let database = setup_database(args.database_url).await.unwrap();
let schema = root_schema_builder(database).finish();
let schema_string = schema.sdl_with_options(SDLExportOptions::new().federation());
if let Some(path) = args.path {
let mut file = File::create(path).unwrap();
Expand Down

0 comments on commit eed8ea4

Please sign in to comment.