Skip to content

Commit

Permalink
Generate s3 download link for file attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvigneshwars committed Apr 19, 2024
1 parent d062de0 commit 28c8b45
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
volumes:
- ..:/workspace:cached,z
command: sleep infinity
ports:
- "8000:80"
environment:
DATABASE_URL: mysql://root:rootpassword@ispyb/ispyb_build
LOG_LEVEL: DEBUG
Expand Down
44 changes: 25 additions & 19 deletions processed_data/src/graphql/entities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_graphql::{Enum, SimpleObject};
use models::{
auto_proc_scaling, auto_proc_scaling_statistics, auto_proc_program_attachment,
sea_orm_active_enums::{ScalingStatisticsType, FileType},
auto_proc_program_attachment, auto_proc_scaling, auto_proc_scaling_statistics,
sea_orm_active_enums::{FileType, ScalingStatisticsType},
};
use sea_orm::QueryResult;

Expand Down Expand Up @@ -76,12 +76,14 @@ impl From<QueryResult> for AutoProcessing {
}
}

/// Type of file attachment for auto processing
#[derive(Enum, Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[allow(clippy::missing_docs_in_private_items)]
pub enum AttachmentFileType {
Log,
Result,
Graph,
Debug,
Log,
Result,
Graph,
Debug,
Input,
}

Expand Down Expand Up @@ -112,7 +114,7 @@ impl From<Option<FileType>> for AttachmentFileType {

/// Represents processed image file stored in s3 bucket
#[derive(Clone, Debug, PartialEq, SimpleObject)]
#[graphql(name = "AutoProcFileAttachment", unresolvable)]
#[graphql(name = "AutoProcFileAttachment", unresolvable, complex)]
pub struct AutoProcFileAttachment {
/// An opaque unique identifier for the autoproc file attachment
pub id: u32,
Expand All @@ -122,27 +124,25 @@ pub struct AutoProcFileAttachment {
/// Type of file attachment
pub file_type: Option<AttachmentFileType>,
/// Full name of the file
#[graphql(skip)]
// #[graphql(skip)]
pub file_name: Option<String>,
/// Path of the file stored in the file system
#[graphql(skip)]
// #[graphql(skip)]
pub file_path: Option<String>,

}

impl From<auto_proc_program_attachment::Model> for AutoProcFileAttachment {
fn from(value: auto_proc_program_attachment::Model) -> Self {
Self {
id: value.auto_proc_program_attachment_id,
auto_proc_program_id: value.auto_proc_program_id,
auto_proc_program_id: value.auto_proc_program_id,
file_type: Some(AttachmentFileType::from(value.file_type)),
file_name: value.file_name,
file_name: value.file_name,
file_path: value.file_path,
}
}
}


/// Represents a processing job
#[derive(Clone, Debug, PartialEq, SimpleObject)]
#[graphql(name = "ProcessingJobs", unresolvable)]
Expand Down Expand Up @@ -272,12 +272,18 @@ impl From<auto_proc_scaling_statistics::Model> for AutoProcScalingStatics {
}
}

// impl DataProcessing {
// /// S3 bucket object key
// pub fn object_key(&self) -> String {
// self.file_full_path.to_string()
// }
// }
impl AutoProcFileAttachment {
/// S3 bucket object key
pub fn object_key(&self) -> String {
let mut key = std::path::PathBuf::from(
<Option<String> as Clone>::clone(&self.file_path)
.unwrap()
.to_string(),
);
key.push(<Option<String> as Clone>::clone(&self.file_name).unwrap());
key.to_string_lossy().to_string()
}
}

/// Datasets subgraph extension
#[derive(SimpleObject)]
Expand Down
58 changes: 24 additions & 34 deletions processed_data/src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use async_graphql::{
};
use aws_sdk_s3::presigning::PresigningConfig;
use entities::{
AutoProcScalingStatics, AutoProcessing, DataCollection, ProcessingJob,
StatisticsType, AutoProcFileAttachment
AutoProcFileAttachment, AutoProcScalingStatics, AutoProcessing, DataCollection, ProcessingJob,
StatisticsType,
};
use models::{
auto_proc, auto_proc_integration, auto_proc_program, auto_proc_scaling,
auto_proc_scaling_statistics, processing_job, auto_proc_program_attachment,
processing_job_parameter,
auto_proc, auto_proc_integration, auto_proc_program, auto_proc_program_attachment,
auto_proc_scaling, auto_proc_scaling_statistics, processing_job, processing_job_parameter,
};
use sea_orm::{
ColumnTrait, ConnectionTrait, DatabaseConnection, EntityTrait, QueryFilter, Statement,
Expand Down Expand Up @@ -359,24 +358,24 @@ impl DataCollection {
}
}

// #[ComplexObject]
// impl AutoProcFileAttachment {
// /// Gives downloadable link for the processed image in the s3 bucket
// async fn file_url(&self, ctx: &Context<'_>) -> async_graphql::Result<String> {
// let s3_client = ctx.data::<aws_sdk_s3::Client>()?;
// let bucket = ctx.data::<S3Bucket>()?;
// let object_uri = s3_client
// .get_object()
// .bucket(bucket.clone())
// .key(self.object_key())
// .presigned(PresigningConfig::expires_in(Duration::from_secs(10 * 60))?)
// .await?
// .uri()
// .clone();
// let object_url = Url::parse(&object_uri.to_string())?;
// Ok(object_url.to_string())
// }
// }
#[ComplexObject]
impl AutoProcFileAttachment {
/// Gives downloadable link for the processed image in the s3 bucket
async fn file_url(&self, ctx: &Context<'_>) -> async_graphql::Result<String> {
let s3_client = ctx.data::<aws_sdk_s3::Client>()?;
let bucket = ctx.data::<S3Bucket>()?;
let object_uri = s3_client
.get_object()
.bucket(bucket.clone())
.key(self.object_key())
.presigned(PresigningConfig::expires_in(Duration::from_secs(10 * 60))?)
.await?
.uri()
.clone();
let object_url = Url::parse(&object_uri.to_string())?;
Ok(object_url.to_string())
}
}

#[ComplexObject]
impl AutoProcessing {
Expand Down Expand Up @@ -416,9 +415,9 @@ impl AutoProcessing {
}
}

/// Fetches all the file attachments
/// Fetches all the file attachments
async fn file_attachments(
&self,
&self,
ctx: &Context<'_>,
) -> Result<Option<Vec<AutoProcFileAttachment>>, async_graphql::Error> {
let loader = ctx.data_unchecked::<DataLoader<FileAttachmentDataLoader>>();
Expand All @@ -436,13 +435,4 @@ impl Query {
async fn router_data_collection(&self, id: u32) -> DataCollection {
DataCollection { id }
}

async fn processed_data(
&self,
ctx: &Context<'_>,
id: u32,
) -> Result<Option<Vec<AutoProcFileAttachment>>, async_graphql::Error> {
let loader = ctx.data_unchecked::<DataLoader<FileAttachmentDataLoader>>();
loader.load_one(id).await
}
}
6 changes: 5 additions & 1 deletion processed_data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ 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().finish();
let s3_client = aws_sdk_s3::Client::from_s3_client_args(args.s3_client);
let schema = root_schema_builder()
.data(s3_client)
.data(args.s3_bucket)
.finish();
let router = setup_router(schema, database);
serve(router, args.port).await.unwrap();
}
Expand Down

0 comments on commit 28c8b45

Please sign in to comment.