Update random bits and bobs
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
106
src/config.rs
106
src/config.rs
@@ -15,27 +15,6 @@ use rust_bert::{
|
||||
use std::{env, num::NonZeroU32, path::PathBuf, sync::Arc};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ALPACA_MODE: Mode = env::var("ALPACA_MODE")
|
||||
.expect("ALPACA_MODE must be set.")
|
||||
.parse()
|
||||
.expect("ALPACA_MODE must be 'live' or 'paper'");
|
||||
static ref ALPACA_URL_SUBDOMAIN: String = match *ALPACA_MODE {
|
||||
Mode::Live => String::from("api"),
|
||||
Mode::Paper => String::from("paper-api"),
|
||||
};
|
||||
#[derive(Debug)]
|
||||
pub static ref ALPACA_API_URL: String = format!(
|
||||
"https://{subdomain}.alpaca.markets/v2",
|
||||
subdomain = *ALPACA_URL_SUBDOMAIN
|
||||
);
|
||||
#[derive(Debug)]
|
||||
pub static ref ALPACA_WEBSOCKET_URL: String = format!(
|
||||
"wss://{subdomain}.alpaca.markets/stream",
|
||||
subdomain = *ALPACA_URL_SUBDOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
pub const ALPACA_STOCK_DATA_API_URL: &str = "https://data.alpaca.markets/v2/stocks/bars";
|
||||
pub const ALPACA_CRYPTO_DATA_API_URL: &str = "https://data.alpaca.markets/v1beta3/crypto/us/bars";
|
||||
pub const ALPACA_NEWS_DATA_API_URL: &str = "https://data.alpaca.markets/v1beta1/news";
|
||||
@@ -45,69 +24,78 @@ pub const ALPACA_CRYPTO_DATA_WEBSOCKET_URL: &str =
|
||||
"wss://stream.data.alpaca.markets/v1beta3/crypto/us";
|
||||
pub const ALPACA_NEWS_DATA_WEBSOCKET_URL: &str = "wss://stream.data.alpaca.markets/v1beta1/news";
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ALPACA_MODE: Mode = env::var("ALPACA_MODE")
|
||||
.expect("ALPACA_MODE must be set.")
|
||||
.parse()
|
||||
.expect("ALPACA_MODE must be 'live' or 'paper'");
|
||||
pub static ref ALPACA_SOURCE: Source = env::var("ALPACA_SOURCE")
|
||||
.expect("ALPACA_SOURCE must be set.")
|
||||
.parse()
|
||||
.expect("ALPACA_SOURCE must be 'iex', 'sip', or 'otc'");
|
||||
pub static ref ALPACA_API_KEY: String = env::var("ALPACA_API_KEY").expect("ALPACA_API_KEY must be set.");
|
||||
pub static ref ALPACA_API_SECRET: String = env::var("ALPACA_API_SECRET").expect("ALPACA_API_SECRET must be set.");
|
||||
#[derive(Debug)]
|
||||
pub static ref ALPACA_API_URL: String = format!(
|
||||
"https://{}.alpaca.markets/v2",
|
||||
match *ALPACA_MODE {
|
||||
Mode::Live => String::from("api"),
|
||||
Mode::Paper => String::from("paper-api"),
|
||||
}
|
||||
);
|
||||
#[derive(Debug)]
|
||||
pub static ref ALPACA_WEBSOCKET_URL: String = format!(
|
||||
"wss://{}.alpaca.markets/stream",
|
||||
match *ALPACA_MODE {
|
||||
Mode::Live => String::from("api"),
|
||||
Mode::Paper => String::from("paper-api"),
|
||||
}
|
||||
);
|
||||
pub static ref MAX_BERT_INPUTS: usize = env::var("MAX_BERT_INPUTS")
|
||||
.expect("MAX_BERT_INPUTS must be set.")
|
||||
.parse()
|
||||
.expect("MAX_BERT_INPUTS must be a positive integer.");
|
||||
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub alpaca_api_key: String,
|
||||
pub alpaca_api_secret: String,
|
||||
pub alpaca_client: Client,
|
||||
pub alpaca_rate_limit: DefaultDirectRateLimiter,
|
||||
pub alpaca_source: Source,
|
||||
pub alpaca_rate_limiter: DefaultDirectRateLimiter,
|
||||
pub clickhouse_client: clickhouse::Client,
|
||||
pub max_bert_inputs: usize,
|
||||
pub sequence_classifier: Arc<Mutex<SequenceClassificationModel>>,
|
||||
pub sequence_classifier: Mutex<SequenceClassificationModel>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_env() -> Self {
|
||||
let alpaca_api_key = env::var("ALPACA_API_KEY").expect("ALPACA_API_KEY must be set.");
|
||||
let alpaca_api_secret =
|
||||
env::var("ALPACA_API_SECRET").expect("ALPACA_API_SECRET must be set.");
|
||||
let alpaca_source: Source = env::var("ALPACA_SOURCE")
|
||||
.expect("ALPACA_SOURCE must be set.")
|
||||
.parse()
|
||||
.expect("ALPACA_SOURCE must be 'iex', 'sip', or 'otc'.");
|
||||
|
||||
let clickhouse_url = env::var("CLICKHOUSE_URL").expect("CLICKHOUSE_URL must be set.");
|
||||
let clickhouse_user = env::var("CLICKHOUSE_USER").expect("CLICKHOUSE_USER must be set.");
|
||||
let clickhouse_password =
|
||||
env::var("CLICKHOUSE_PASSWORD").expect("CLICKHOUSE_PASSWORD must be set.");
|
||||
let clickhouse_db = env::var("CLICKHOUSE_DB").expect("CLICKHOUSE_DB must be set.");
|
||||
|
||||
let max_bert_inputs: usize = env::var("MAX_BERT_INPUTS")
|
||||
.expect("MAX_BERT_INPUTS must be set.")
|
||||
.parse()
|
||||
.expect("MAX_BERT_INPUTS must be a positive integer.");
|
||||
|
||||
Self {
|
||||
alpaca_client: Client::builder()
|
||||
.default_headers(HeaderMap::from_iter([
|
||||
(
|
||||
HeaderName::from_static("apca-api-key-id"),
|
||||
HeaderValue::from_str(&alpaca_api_key)
|
||||
HeaderValue::from_str(&ALPACA_API_KEY)
|
||||
.expect("Alpaca API key must not contain invalid characters."),
|
||||
),
|
||||
(
|
||||
HeaderName::from_static("apca-api-secret-key"),
|
||||
HeaderValue::from_str(&alpaca_api_secret)
|
||||
HeaderValue::from_str(&ALPACA_API_SECRET)
|
||||
.expect("Alpaca API secret must not contain invalid characters."),
|
||||
),
|
||||
]))
|
||||
.build()
|
||||
.unwrap(),
|
||||
alpaca_rate_limit: RateLimiter::direct(Quota::per_minute(match alpaca_source {
|
||||
alpaca_rate_limiter: RateLimiter::direct(Quota::per_minute(match *ALPACA_SOURCE {
|
||||
Source::Iex => unsafe { NonZeroU32::new_unchecked(200) },
|
||||
Source::Sip => unsafe { NonZeroU32::new_unchecked(10000) },
|
||||
Source::Otc => unimplemented!("OTC rate limit not implemented."),
|
||||
})),
|
||||
alpaca_source,
|
||||
clickhouse_client: clickhouse::Client::default()
|
||||
.with_url(clickhouse_url)
|
||||
.with_user(clickhouse_user)
|
||||
.with_password(clickhouse_password)
|
||||
.with_database(clickhouse_db),
|
||||
alpaca_api_key,
|
||||
alpaca_api_secret,
|
||||
max_bert_inputs,
|
||||
sequence_classifier: Arc::new(Mutex::new(
|
||||
.with_url(env::var("CLICKHOUSE_URL").expect("CLICKHOUSE_URL must be set."))
|
||||
.with_user(env::var("CLICKHOUSE_USER").expect("CLICKHOUSE_USER must be set."))
|
||||
.with_password(
|
||||
env::var("CLICKHOUSE_PASSWORD").expect("CLICKHOUSE_PASSWORD must be set."),
|
||||
)
|
||||
.with_database(env::var("CLICKHOUSE_DB").expect("CLICKHOUSE_DB must be set.")),
|
||||
sequence_classifier: Mutex::new(
|
||||
SequenceClassificationModel::new(SequenceClassificationConfig::new(
|
||||
ModelType::Bert,
|
||||
ModelResource::Torch(Box::new(LocalResource {
|
||||
@@ -125,7 +113,7 @@ impl Config {
|
||||
None,
|
||||
))
|
||||
.unwrap(),
|
||||
)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user