Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get config-versions api #248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/context_aware_config/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod audit_log;
pub mod config;
pub mod config_versions;
pub mod context;
pub mod default_config;
pub mod dimension;
Expand Down
2 changes: 2 additions & 0 deletions crates/context_aware_config/src/api/config_versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod handlers;
pub use handlers::endpoints;
50 changes: 50 additions & 0 deletions crates/context_aware_config/src/api/config_versions/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{
db::{models::ConfigVersion, schema::config_versions},
types::{PaginatedResponse, QueryFilters},
};

extern crate base64;
use service_utils::service::types::DbConnection;

use superposition_types::result as superposition;

use actix_web::{
get,
web::{Json, Query},
Scope,
};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};

pub fn endpoints() -> Scope {
Scope::new("").service(get)
}

#[get("")]
async fn get(
db_conn: DbConnection,
filters: Query<QueryFilters>,
) -> superposition::Result<Json<PaginatedResponse<ConfigVersion>>> {
let DbConnection(mut conn) = db_conn;

let n_version: i64 = config_versions::dsl::config_versions
.count()
.get_result(&mut conn)?;
let mut builder = config_versions::dsl::config_versions
.into_boxed()
.order(config_versions::dsl::created_at.desc());
if let Some(limit) = filters.count {
builder = builder.limit(limit);
}
if let Some(page) = filters.page {
let offset = (page - 1) * filters.count.unwrap_or(10);
builder = builder.offset(offset);
}
let limit = filters.count.unwrap_or(10);
let config_versions: Vec<ConfigVersion> = builder.load(&mut conn)?;
let total_pages = (n_version as f64 / limit as f64).ceil() as i64;
Ok(Json(PaginatedResponse {
total_pages,
total_items: n_version,
data: config_versions,
}))
}
1 change: 1 addition & 0 deletions crates/context_aware_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod api;
pub mod db;
pub mod helpers;
pub mod middlewares;
pub mod types;
pub mod validation_functions;
14 changes: 14 additions & 0 deletions crates/context_aware_config/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize)]
pub struct QueryFilters {
pub count: Option<i64>,
pub page: Option<i64>,
}

#[derive(Serialize, Debug, Clone, Deserialize)]
pub struct PaginatedResponse<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move this to superposition_types in some new file for such common utility types as it might be useful for experimentation as well for list experiments

pub total_pages: i64,
pub total_items: i64,
pub data: Vec<T>,
}
5 changes: 5 additions & 0 deletions crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ async fn main() -> Result<()> {
.wrap(AppExecutionScopeMiddlewareFactory::new(AppScope::CAC))
.service(type_templates::endpoints()),
)
.service(
scope("/config-versions")
.wrap(AppExecutionScopeMiddlewareFactory::new(AppScope::CAC))
.service(config_versions::endpoints()),
)
.service(
experiments::endpoints(scope("/experiments")).wrap(
AppExecutionScopeMiddlewareFactory::new(AppScope::EXPERIMENTATION),
Expand Down
Loading