generated from PeopleForBikes/bna-mechanics-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a table for storing the price per second associated to running Fargate with our configuration. Drive-by: - Removes unused fields in the BrokenspokePipeline table. Signed-off-by: Rémy Greinhofer <[email protected]> save Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
12 changed files
with
242 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14 | ||
use sea_orm::entity::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] | ||
#[sea_orm(table_name = "fargate_price")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub per_second: Decimal, | ||
pub created_at: TimeDateTimeWithTimeZone, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use dotenv::dotenv; | ||
use effortless::{ | ||
api::{entry_not_found, invalid_path_parameter, parse_query_string_parameter}, | ||
error::APIError, | ||
fragment::{get_apigw_request_id, BnaRequestExt}, | ||
}; | ||
use entity::prelude::*; | ||
use lambda_http::{run, service_fn, Body, Error, IntoResponse, Request, Response}; | ||
use lambdas::{api_database_connect, build_paginated_response, pagination_parameters}; | ||
use sea_orm::{EntityTrait, PaginatorTrait, QueryOrder, QuerySelect}; | ||
use serde_json::json; | ||
use tracing::{debug, info}; | ||
|
||
async fn function_handler(event: Request) -> Result<Response<Body>, Error> { | ||
dotenv().ok(); | ||
|
||
// Retrieve pagination parameters if any. | ||
let (page_size, page) = match pagination_parameters(&event) { | ||
Ok((page_size, page)) => (page_size, page), | ||
Err(e) => return Ok(e), | ||
}; | ||
|
||
// Retrieve the ID of the entry to update. | ||
let parameter = "id"; | ||
let id = event.path_parameter::<i32>(parameter); | ||
|
||
// Set the database connection. | ||
let db = match api_database_connect(&event).await { | ||
Ok(db) => db, | ||
Err(e) => return Ok(e), | ||
}; | ||
|
||
// Retrieve all entries or a specific one. | ||
debug!("Processing the requests..."); | ||
let res = match id { | ||
Some(id) => match id { | ||
Ok(id) => { | ||
let model = FargatePrice::find_by_id(id).one(&db).await?; | ||
let res: Response<Body> = match model { | ||
Some(model) => json!(model).into_response().await, | ||
None => entry_not_found(&event).into(), | ||
}; | ||
res | ||
} | ||
Err(e) => { | ||
return Ok( | ||
invalid_path_parameter(&event, parameter, e.to_string().as_str()).into(), | ||
); | ||
} | ||
}, | ||
None => { | ||
// Retrieve the filter if any. | ||
let latest: Option<bool> = match parse_query_string_parameter::<bool>(&event, "latest") | ||
{ | ||
Ok(latest) => latest, | ||
Err(e) => return Ok(e.into()), | ||
}; | ||
|
||
// Select the entity. | ||
let mut select = FargatePrice::find(); | ||
|
||
// Select latest only. | ||
if latest.is_some() { | ||
select = select | ||
.order_by_asc(entity::fargate_price::Column::CreatedAt) | ||
.limit(1); | ||
} | ||
|
||
// Select the results. | ||
let query = select | ||
.clone() | ||
.paginate(&db, page_size) | ||
.fetch_page(page - 1) | ||
.await; | ||
let res: Response<Body> = match query { | ||
Ok(models) => { | ||
let total_items = select.count(&db).await?; | ||
build_paginated_response(json!(models), total_items, page, page_size, &event)? | ||
} | ||
Err(e) => APIError::with_pointer( | ||
get_apigw_request_id(&event), | ||
event.uri().path().to_string().as_str(), | ||
e.to_string().as_str(), | ||
) | ||
.into(), | ||
}; | ||
res | ||
} | ||
}; | ||
|
||
Ok(res) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
// disable printing the name of the module in every log line. | ||
.with_target(false) | ||
// disabling time is handy because CloudWatch will add the ingestion time. | ||
.without_time() | ||
.init(); | ||
|
||
run(service_fn(function_handler)).await.map_err(|e| { | ||
info!("{e}"); | ||
e | ||
}) | ||
} |
Oops, something went wrong.