Skip to content

Commit

Permalink
Add appstore.timeout and refresh_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Sep 4, 2024
1 parent b83796e commit 06ad410
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 56 deletions.
2 changes: 2 additions & 0 deletions SettingsOperator.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
appstore:
refresh_interval_ms: 86400
timeout_ms: 500
keys:
- crypto wallet
apps:
Expand Down
32 changes: 19 additions & 13 deletions apps/operator/src/appstore_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use storage::{
pub struct AppstoreUpdater {
client: AppStoreClient,
database: DatabaseClient,
timeout_ms: u64,
}

impl AppstoreUpdater {
pub fn new(client: AppStoreClient, database: DatabaseClient) -> Self {
Self { client, database }
pub fn new(client: AppStoreClient, database: DatabaseClient, timeout_ms: u64) -> Self {
Self { client, database, timeout_ms }
}

pub async fn update_details(&mut self, apps: Vec<OperatorAppStoreApp>, languages: Vec<OperatorAppStoreLanguage>) {
Expand All @@ -31,12 +32,16 @@ impl AppstoreUpdater {
ratings: response.user_rating_count,
average_rating: response.average_user_rating,
};
values.push(information)
values.push(information);

println!("Update details. Found app: {}, language: {}", app.name, language.code);
}
Err(e) => {
eprintln!("Failed to look up app {}, language: {}, {:?}", app.name, language.code, e);
eprintln!("Update details. Failed to look up app {}, language: {}, {:?}", app.name, language.code, e);
}
}

tokio::time::sleep(tokio::time::Duration::from_millis(self.timeout_ms)).await;
}

match self.database.add_appstore_information(values.clone()) {
Expand All @@ -54,29 +59,30 @@ impl AppstoreUpdater {
for key in keys {
let mut positions: Vec<AppStorePosition> = Vec::new();

println!("Update positions key: {}", key);

for language in languages.clone() {
match self.client.search_apps(key.as_str(), &language.code, 200).await {
Ok(response) => {
println!("Found key: {}, language: {}, results: {}", key, language.code, response.results.len());

for (position, result) in response.results.iter().enumerate() {
if let Some(app) = apps.clone().into_iter().find(|a| a.id == result.track_id) {
let position = AppStorePosition {
let position = (position + 1) as i32;
positions.push(AppStorePosition {
store: "appstore".to_string(),
app: app.name.to_string(),
keyword: key.to_string(),
country: language.name.to_string(),
position: (position + 1) as i32,
};
position,
});

positions.push(position)
println!(
"Update positions. Found app: {}, language: {}, key: {}, position: {}",
app.name, language.code, key, position
);
}
tokio::time::sleep(tokio::time::Duration::from_millis(self.timeout_ms)).await;
}
}
Err(e) => {
eprintln!("Failed to fetch apps, keyword: {}, language: {}: {:?}", key, language.code, e);
eprintln!("Update positions. Failed to fetch apps, keyword: {}, language: {}: {:?}", key, language.code, e);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions apps/operator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use api_connector::AppStoreClient;
use storage::{DatabaseClient};
use storage::DatabaseClient;

pub mod appstore_updater;
pub use appstore_updater::AppstoreUpdater;
Expand All @@ -17,13 +17,13 @@ pub async fn main() {
let client = AppStoreClient::new();

let database = DatabaseClient::new(&settings.postgres.url.clone());
let mut appstore_updater = AppstoreUpdater::new(client, database);
let mut appstore_updater = AppstoreUpdater::new(client, database, settings_operator.appstore.timeout_ms);

loop {
appstore_updater.update_details(apps.clone(), languages.clone()).await;

appstore_updater.update_positions(keys.clone(), apps.clone(), languages.clone()).await;

tokio::time::sleep(tokio::time::Duration::from_secs(86400)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(settings_operator.appstore.refresh_interval_ms)).await;
}
}
43 changes: 3 additions & 40 deletions crates/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::env;
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;

pub mod operator;
pub use operator::{SettingsOperator, OperatorAppStoreApp, OperatorAppStoreLanguage};

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct Settings {
Expand Down Expand Up @@ -253,34 +256,6 @@ pub struct SwapProvider {
pub fee: SwapFee,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct SettingsOperator {
pub appstore: OperatorAppStore,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStore {
pub keys: Vec<String>,
pub apps: Vec<OperatorAppStoreApp>,
pub languages: Vec<OperatorAppStoreLanguage>,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStoreApp {
pub name: String,
pub id: u64,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStoreLanguage {
pub name: String,
pub code: String,
}

impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let current_dir = env::current_dir().unwrap();
Expand All @@ -292,15 +267,3 @@ impl Settings {
s.try_deserialize()
}
}

impl SettingsOperator {
pub fn new() -> Result<Self, ConfigError> {
let current_dir = env::current_dir().unwrap();
let setting_path = current_dir.join("SettingsOperator.yaml");
let s = Config::builder()
.add_source(File::from(setting_path))
.add_source(Environment::with_prefix("").prefix_separator("").separator("_"))
.build()?;
s.try_deserialize()
}
}
46 changes: 46 additions & 0 deletions crates/settings/src/operator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::env;

use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct SettingsOperator {
pub appstore: OperatorAppStore,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStore {
pub refresh_interval_ms: u64,
pub timeout_ms: u64,
pub keys: Vec<String>,
pub apps: Vec<OperatorAppStoreApp>,
pub languages: Vec<OperatorAppStoreLanguage>,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStoreApp {
pub name: String,
pub id: u64,
}

#[derive(Debug, Deserialize, Clone)]
#[allow(unused)]
pub struct OperatorAppStoreLanguage {
pub name: String,
pub code: String,
}

impl SettingsOperator {
pub fn new() -> Result<Self, ConfigError> {
let current_dir = env::current_dir().unwrap();
let setting_path = current_dir.join("SettingsOperator.yaml");
let s = Config::builder()
.add_source(File::from(setting_path))
.add_source(Environment::with_prefix("").prefix_separator("").separator("_"))
.build()?;
s.try_deserialize()
}
}

0 comments on commit 06ad410

Please sign in to comment.