Skip to content

Commit

Permalink
feat: add ingestion time to source documents
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Dec 6, 2024
1 parent 1354501 commit 99bf536
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions entity/src/source_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Model {
pub sha384: String,
pub sha512: String,
pub size: i64,
pub ingestion_time: time::OffsetDateTime,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod m0000730_alter_importer_add_progress;
mod m0000740_ensure_get_purl_fns;
mod m0000750_alter_advisory_add_document_id;
mod m0000760_product_status_index;
mod m0000780_alter_source_document_time;

pub struct Migrator;

Expand Down Expand Up @@ -195,6 +196,7 @@ impl MigratorTrait for Migrator {
Box::new(m0000740_ensure_get_purl_fns::Migration),
Box::new(m0000750_alter_advisory_add_document_id::Migration),
Box::new(m0000760_product_status_index::Migration),
Box::new(m0000780_alter_source_document_time::Migration),
]
}
}
Expand Down
46 changes: 46 additions & 0 deletions migration/src/m0000780_alter_source_document_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::Now;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(SourceDocument::Table)
.add_column(
ColumnDef::new(SourceDocument::IngestionTime)
.timestamp_with_time_zone()
.default(Func::cust(Now))
.not_null()
.to_owned(),
)
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(SourceDocument::Table)
.drop_column(SourceDocument::IngestionTime)
.to_owned(),
)
.await?;

Ok(())
}
}

#[derive(DeriveIden)]
enum SourceDocument {
Table,
IngestionTime,
}
3 changes: 1 addition & 2 deletions modules/fundamental/src/advisory/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use sea_orm::{
QueryTrait, RelationTrait, Select, Statement, TransactionTrait,
};
use sea_query::{ColumnRef, ColumnType, Expr, Func, IntoColumnRef, IntoIden, JoinType, SimpleExpr};
use trustify_common::db::UpdateDeprecatedAdvisory;
use trustify_common::{
db::{
limiter::LimiterAsModelTrait,
multi_model::{FromQueryResultMultiModel, SelectIntoMultiModel},
query::{Columns, Filtering, Query},
Database,
Database, UpdateDeprecatedAdvisory,
},
id::{Id, TrySelectForId},
model::{Paginated, PaginatedResults},
Expand Down
5 changes: 5 additions & 0 deletions modules/fundamental/src/source_document/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Error;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use time::OffsetDateTime;
use trustify_common::id::{Id, IdError};
use trustify_entity::source_document;
use trustify_module_storage::service::StorageKey;
Expand All @@ -12,6 +13,9 @@ pub struct SourceDocument {
pub sha384: String,
pub sha512: String,
pub size: u64,
/// The timestamp the document was ingested
#[serde(with = "time::serde::rfc3339")]
pub ingestion_time: OffsetDateTime,
}

impl SourceDocument {
Expand All @@ -21,6 +25,7 @@ impl SourceDocument {
sha384: format!("sha384:{}", source_document.sha384),
sha512: format!("sha512:{}", source_document.sha512),
size: source_document.size as u64,
ingestion_time: source_document.ingestion_time,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/ingestor/src/graph/advisory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl Graph {
sha384: Set(digests.sha384.encode_hex()),
sha512: Set(digests.sha512.encode_hex()),
size: Set(digests.size as i64),
ingestion_time: sea_orm::Set(OffsetDateTime::now_utc()),
};

let doc = doc_model.insert(connection).await?;
Expand Down
1 change: 1 addition & 0 deletions modules/ingestor/src/graph/sbom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl Graph {
sha384: Set(digests.sha384.encode_hex()),
sha512: Set(digests.sha512.encode_hex()),
size: Set(digests.size as i64),
ingestion_time: Set(OffsetDateTime::now_utc()),
};

let doc = doc_model.insert(connection).await?;
Expand Down
5 changes: 5 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3580,7 +3580,12 @@ components:
- sha384
- sha512
- size
- ingestion_time
properties:
ingestion_time:
type: string
format: date-time
description: The timestamp the document was ingested
sha256:
type: string
sha384:
Expand Down

0 comments on commit 99bf536

Please sign in to comment.