Skip to content

Commit

Permalink
feat(sherlock): Implemented the Model pulling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
isala404 committed Mar 11, 2022
1 parent b2f07b5 commit b0d1c92
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sherlock/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

models/*
!models/.gitkeep
# End of https://www.toptal.com/developers/gitignore/api/rust
6 changes: 4 additions & 2 deletions sherlock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
prometheus = "0.13.0"
lazy_static = "1.4.0"
hyper = { version = "0.14", features = ["full"] }
hyper = { version = "0.14.17", features = ["full"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
flate2 = "1.0.22"
tar = "0.4.38"
2 changes: 1 addition & 1 deletion sherlock/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ENV SERVICE_NAME=sample_service
ENV NAMESPACE=default
ENV END_POINT=http://localhost:8501/v1/models/sherlock:predict
ENV POOL_DURATION=60

ENV MODEL_URL=https://static.isala.me/lazy-koala/sherlock/models/sample_service.tar.gz
# Define port
EXPOSE 9898

Expand Down
Empty file added sherlock/models/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions sherlock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ use std::{env::var, thread, time::Duration};
use lazy_static::lazy_static;
use prometheus::{labels, opts, register_gauge};
use std::collections::HashMap;
use flate2::read::GzDecoder;
use tar::Archive;

lazy_static! {
static ref SERVICE_NAME: String = var("SERVICE_NAME").unwrap();
static ref NAMESPACE: String = var("NAMESPACE").unwrap();
static ref END_POINT: String = var("END_POINT").unwrap();
static ref POOL_DURATION: String = var("POOL_DURATION").unwrap();
static ref MODEL_URL: String = var("MODEL_URL").unwrap();
static ref ANOMLAY_GAUGE: Gauge = register_gauge!(opts!(
"anomaly_score",
"Reconstruction loss of the autoencoder",
Expand All @@ -22,6 +25,15 @@ lazy_static! {
.unwrap();
}

fn download_model()->Result<(), Box<dyn std::error::Error>>{
let resp = reqwest::blocking::get(MODEL_URL.as_str())?;
let tarfile = GzDecoder::new(resp);
let mut archive = Archive::new(tarfile);
archive.unpack("models/")?;

Ok(())
}

async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let encoder = TextEncoder::new();

Expand Down Expand Up @@ -57,9 +69,20 @@ fn poll_anomaly_scores(delay: u64) {
}
}

#[allow(unused_must_use)]
#[tokio::main]
async fn main() {

// Forgive me father for i have sinned 🙏
// I couldn't figure out way to use reqwest's
// async response with GzDecoder 😭
thread::spawn(|| {
if let Err(err) = download_model() {
eprintln!("failed to download the model: {}", err);
std::process::exit(1);
}
}).join();

thread::spawn(|| poll_anomaly_scores(POOL_DURATION.as_str().parse::<u64>().unwrap()));

ANOMLAY_GAUGE.set(0.0);
Expand Down

0 comments on commit b0d1c92

Please sign in to comment.