-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1239 from input-output-hk/greg/1127/download_stat…
…istics add snapshot downloads statistics
- Loading branch information
Showing
14 changed files
with
269 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
.DS_Store | ||
.direnv/ | ||
.tmp/ | ||
.vscode/ | ||
target | ||
target-* | ||
test-results.xml | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,14 @@ | ||
# Statistics | ||
|
||
## Snapshot downloads per day | ||
|
||
```sh | ||
$> sqlite3 -table -batch \ | ||
$DATA_STORES_DIRECTORY/monitoring.sqlite3 \ | ||
< snapshot_downloads.sql | ||
``` | ||
|
||
The variable `$DATA_STORES_DIRECTORY` should point to the directory where the | ||
databases files are stored (see files in `mithril-aggregator/config` using the | ||
key `data_stores_directory` to know where they are). | ||
|
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,12 @@ | ||
select | ||
date(created_at) as downloaded_at, | ||
count(*) as downloads | ||
from event | ||
where | ||
source = 'HTTP::statistics' | ||
and action = 'snapshot_downloaded' | ||
group by 1 | ||
order by | ||
downloaded_at desc, | ||
downloads desc | ||
|
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ mod reply; | |
pub mod router; | ||
mod signatures_routes; | ||
mod signer_routes; | ||
mod statistics_routes; |
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
108 changes: 108 additions & 0 deletions
108
mithril-aggregator/src/http_server/routes/statistics_routes.rs
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 std::sync::Arc; | ||
use warp::Filter; | ||
|
||
use crate::http_server::routes::middlewares; | ||
use crate::DependencyContainer; | ||
|
||
pub fn routes( | ||
dependency_manager: Arc<DependencyContainer>, | ||
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { | ||
post_statistics(dependency_manager) | ||
} | ||
|
||
/// POST /statistics/snapshot | ||
fn post_statistics( | ||
dependency_manager: Arc<DependencyContainer>, | ||
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { | ||
warp::path!("statistics" / "snapshot") | ||
.and(warp::post()) | ||
.and(warp::body::json()) | ||
.and(middlewares::with_event_transmitter( | ||
dependency_manager.clone(), | ||
)) | ||
.and_then(handlers::post_snapshot_statistics) | ||
} | ||
|
||
mod handlers { | ||
use std::{convert::Infallible, sync::Arc}; | ||
|
||
use mithril_common::messages::SnapshotMessage; | ||
use reqwest::StatusCode; | ||
|
||
use crate::event_store::{EventMessage, TransmitterService}; | ||
use crate::http_server::routes::reply; | ||
|
||
pub async fn post_snapshot_statistics( | ||
snapshot_message: SnapshotMessage, | ||
event_transmitter: Arc<TransmitterService<EventMessage>>, | ||
) -> Result<impl warp::Reply, Infallible> { | ||
let headers: Vec<(&str, &str)> = Vec::new(); | ||
|
||
match event_transmitter.send_event_message( | ||
"HTTP::statistics", | ||
"snapshot_downloaded", | ||
&snapshot_message, | ||
headers, | ||
) { | ||
Err(e) => Ok(reply::internal_server_error(e)), | ||
Ok(_) => Ok(reply::empty(StatusCode::CREATED)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use mithril_common::messages::SnapshotMessage; | ||
use mithril_common::test_utils::apispec::APISpec; | ||
|
||
use warp::{http::Method, test::request}; | ||
|
||
use crate::{ | ||
dependency_injection::DependenciesBuilder, http_server::SERVER_BASE_PATH, Configuration, | ||
}; | ||
|
||
fn setup_router( | ||
dependency_manager: Arc<DependencyContainer>, | ||
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { | ||
let cors = warp::cors() | ||
.allow_any_origin() | ||
.allow_headers(vec!["content-type"]) | ||
.allow_methods(vec![Method::GET, Method::POST, Method::OPTIONS]); | ||
|
||
warp::any() | ||
.and(warp::path(SERVER_BASE_PATH)) | ||
.and(routes(dependency_manager).with(cors)) | ||
} | ||
|
||
#[tokio::test] | ||
async fn post_statistics_ok() { | ||
let config = Configuration::new_sample(); | ||
let mut builder = DependenciesBuilder::new(config); | ||
let mut rx = builder.get_event_transmitter_receiver().await.unwrap(); | ||
let dependency_manager = builder.build_dependency_container().await.unwrap(); | ||
let snapshot_message = SnapshotMessage::dummy(); | ||
|
||
let method = Method::POST.as_str(); | ||
let path = "/statistics/snapshot"; | ||
|
||
let response = request() | ||
.method(method) | ||
.json(&snapshot_message) | ||
.path(&format!("/{SERVER_BASE_PATH}{path}")) | ||
.reply(&setup_router(Arc::new(dependency_manager))) | ||
.await; | ||
|
||
APISpec::verify_conformity( | ||
APISpec::get_all_spec_files(), | ||
method, | ||
path, | ||
"application/json", | ||
&snapshot_message, | ||
&response, | ||
); | ||
|
||
let _ = rx.try_recv().unwrap(); | ||
} | ||
} |
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
Oops, something went wrong.