Skip to content

Commit

Permalink
Update model_indicator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
minshao authored and msk committed Feb 21, 2024
1 parent 400c6e0 commit 334b394
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased-review-database-0.25]

### Changed

- Updated the `ModelIndicator` GraphQL type. Added `name` field as the name of
the model indicator.
- Changed the return type of `indicatorList` GraphQL query to `[ModelIndicator!]!`.

### Removed

- Removed the obsoleted `ModelIndicatorOutput` GraphQL type. This type was
previously used as return type of `indicatorList` GraphQL query. With
advancements and improvements in our system, this type is no longer necessary
and has been removed to streamline the codebase and enhance overall maintainability.

## [Unreleased]

### Added
Expand Down Expand Up @@ -387,6 +402,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).

- An initial version.

[Unreleased-review-database-0.25]: https://github.com/aicers/review-web/compare/main...compat-review-database-0.25
[Unreleased]: https://github.com/aicers/review-web/compare/0.17.0...main
[0.17.0]: https://github.com/aicers/review-web/compare/0.16.0...0.17.0
[0.16.0]: https://github.com/aicers/review-web/compare/0.15.0...0.16.0
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.1" }
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls-native-roots",
] }
review-database = { git = "https://github.com/petabi/review-database.git", rev = "41ebf117" }
review-database = { git = "https://github.com/petabi/review-database.git", rev = "e13263aa" }
roxy = { git = "https://github.com/aicers/roxy.git", tag = "0.2.1" }
rustls = "0.21"
rustls-native-certs = "0.6"
Expand Down
51 changes: 25 additions & 26 deletions src/graphql/indicator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::graphql::{Role, RoleGuard};
use async_graphql::{Context, Object, Result, SimpleObject};
use async_graphql::{Context, Object, Result};
use chrono::{DateTime, Utc};
use review_database::{self as database};

Expand All @@ -12,8 +12,9 @@ impl IndicatorQuery {
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn indicator(&self, ctx: &Context<'_>, name: String) -> Result<Option<ModelIndicator>> {
let store = super::get_store(ctx).await?;
database::ModelIndicator::get(&store, &name)
let store = crate::graphql::get_store(ctx).await?;
let map = store.model_indicator_map();
map.get(&name)
.map(|indicator| indicator.map(Into::into))
.map_err(Into::into)
}
Expand All @@ -23,18 +24,14 @@ impl IndicatorQuery {
.or(RoleGuard::new(Role::SecurityAdministrator))
.or(RoleGuard::new(Role::SecurityManager))
.or(RoleGuard::new(Role::SecurityMonitor))")]
async fn indicator_list(&self, ctx: &Context<'_>) -> Result<Vec<ModelIndicatorOutput>> {
async fn indicator_list(&self, ctx: &Context<'_>) -> Result<Vec<ModelIndicator>> {
use database::Iterable;

let store = super::get_store(ctx).await?;
database::ModelIndicator::get_list(&store)
.map(|list| {
list.into_iter()
.map(|(name, indicator)| ModelIndicatorOutput {
name,
indicator: indicator.into(),
})
.collect()
})
.map_err(Into::into)
let map = store.model_indicator_map();
map.iter(database::Direction::Forward, None)
.map(|res| res.map(Into::into).map_err(Into::into))
.collect()
}
}

Expand All @@ -53,9 +50,10 @@ impl IndicatorMutation {
name: String,
dbfile: String,
) -> Result<String> {
let indicator = database::ModelIndicator::new(&dbfile)?;
let indicator = database::ModelIndicator::new(&name, &dbfile)?;
let store = super::get_store(ctx).await?;
indicator.insert(&store, &name).map_err(Into::into)
let map = store.model_indicator_map();
map.insert(indicator).map_err(Into::into).map(|()| name)
}

/// Removes Indicator, returning the db's name and version that no longer exist.
Expand All @@ -69,7 +67,9 @@ impl IndicatorMutation {
#[graphql(validator(min_items = 1))] names: Vec<String>,
) -> Result<Vec<String>> {
let store = super::get_store(ctx).await?;
database::ModelIndicator::remove(&store, &names).map_err(Into::into)
let map = store.model_indicator_map();
map.remove(names.iter().map(String::as_str))
.map_err(Into::into)
}

/// Updates the given indicator, returning the indicator name that was updated.
Expand All @@ -84,9 +84,10 @@ impl IndicatorMutation {
name: String,
new: String,
) -> Result<String> {
let indicator = database::ModelIndicator::new(&new)?;
let indicator = database::ModelIndicator::new(&name, &new)?;
let store = super::get_store(ctx).await?;
indicator.update(&store, &name).map_err(Into::into)
let map = store.model_indicator_map();
map.update(indicator).map_err(Into::into).map(|()| name)
}
}

Expand All @@ -96,6 +97,11 @@ struct ModelIndicator {

#[Object]
impl ModelIndicator {
/// The name of the model indicator.
async fn name(&self) -> &str {
&self.inner.name
}

/// The description of the model indicator.
async fn description(&self) -> &str {
&self.inner.description
Expand All @@ -122,10 +128,3 @@ impl From<database::ModelIndicator> for ModelIndicator {
Self { inner }
}
}

#[derive(SimpleObject)]
#[allow(clippy::module_name_repetitions)]
pub struct ModelIndicatorOutput {
name: String,
indicator: ModelIndicator,
}

0 comments on commit 334b394

Please sign in to comment.