Alexey Velikiy
3 years ago
7 changed files with 181 additions and 8 deletions
@ -0,0 +1,43 @@
|
||||
use hastic::services::user_service; |
||||
|
||||
use warp::filters::method::post; |
||||
use warp::http::HeaderValue; |
||||
use warp::hyper::{Body, StatusCode}; |
||||
use warp::{http::Response, Filter}; |
||||
use warp::{Rejection, Reply}; |
||||
|
||||
use serde::Serialize; |
||||
|
||||
use crate::api::{self, API}; |
||||
|
||||
use parking_lot::RwLock; |
||||
use std::sync::Arc; |
||||
|
||||
|
||||
async fn get_query( |
||||
p: HashMap<String, String>, |
||||
ms: Arc<RwLock<metric_service::MetricService>>, |
||||
) -> anyhow::Result<MetricResult> { |
||||
if !p.contains_key("from") { |
||||
return Err(anyhow::anyhow!("Missing attribute from")); |
||||
} |
||||
if !p.contains_key("to") { |
||||
return Err(anyhow::anyhow!("Missing attribute to")); |
||||
} |
||||
let from = p.get("from").unwrap().parse::<u64>()?; |
||||
let to = p.get("to").unwrap().parse::<u64>()?; |
||||
|
||||
let prom = ms.read().get_prom(); |
||||
drop(ms); |
||||
// Ok(prom.query(from, to, step).await?)
|
||||
} |
||||
|
||||
pub fn get_route( |
||||
user_service: Arc<RwLock<user_service::UserService>>, |
||||
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { |
||||
return warp::path!("api" / "segments") |
||||
.and(get()) |
||||
.and(warp::query::<HashMap<String, String>>()) |
||||
.and(warp::any().map(move || metric_service.clone())) |
||||
.and_then(query); |
||||
} |
@ -1,8 +1,13 @@
|
||||
use anyhow; |
||||
|
||||
mod api; |
||||
|
||||
#[tokio::main] |
||||
async fn main() { |
||||
async fn main() -> anyhow::Result<()> { |
||||
let config = hastic::config::Config::new(); |
||||
let api = api::API::new(&config); |
||||
let api = api::API::new(&config)?; |
||||
|
||||
api.serve().await; |
||||
|
||||
Ok(()) |
||||
} |
||||
|
@ -1 +1,61 @@
|
||||
struct DataService {} |
||||
use rusqlite::{ Connection, params }; |
||||
|
||||
|
||||
#[derive(Debug)] |
||||
pub struct Segment { |
||||
pub id: Option<u64>, |
||||
pub start: u64, |
||||
pub end: u64, |
||||
} |
||||
|
||||
|
||||
pub struct DataService { |
||||
connection: Connection |
||||
} |
||||
|
||||
|
||||
impl DataService { |
||||
pub fn new() -> anyhow::Result<DataService> { |
||||
let conn = Connection::open("./data.db")?; |
||||
conn.execute( |
||||
"CREATE TABLE IF NOT EXISTS segment ( |
||||
id INTEGER PRIMARY KEY, |
||||
start INTEGER NOT NULL, |
||||
end INTEGER NOT NULL |
||||
)", |
||||
[], |
||||
)?; |
||||
|
||||
|
||||
|
||||
Ok(DataService { connection: conn }) |
||||
} |
||||
|
||||
pub fn insert_segment(&self, segment: &Segment) -> anyhow::Result<u64> { |
||||
// TODO: merge with other segments
|
||||
self.connection.execute( |
||||
"INSERT INTO segment (start, end) VALUES (?1, ?2)", |
||||
params![segment.start, segment.end], |
||||
)?; |
||||
Ok(10) |
||||
} |
||||
|
||||
pub fn get_segments(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> { |
||||
let mut stmt = self.connection.prepare( |
||||
"SELECT id, start, end FROM person WHERE ?1 < start AND end < ?2" |
||||
)?; |
||||
|
||||
let res = stmt.query_map(params![from, to], |row| { |
||||
Ok(Segment{
|
||||
id: row.get(0)?,
|
||||
start: row.get(1)?,
|
||||
end: row.get(2)? |
||||
}) |
||||
})?.map(|e| e.unwrap()).collect(); |
||||
|
||||
|
||||
|
||||
Ok(res) |
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue