Compare commits

..

2 commits

Author SHA1 Message Date
2ec89ee1eb Cache store results 2024-02-02 21:04:10 -05:00
d714b98dee cargo fmt 2024-02-02 20:26:43 -05:00
13 changed files with 264 additions and 81 deletions

2
backend/Cargo.lock generated
View file

@ -304,6 +304,7 @@ dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-targets 0.48.5",
]
@ -1172,6 +1173,7 @@ name = "powertools"
version = "1.5.0-ng1"
dependencies = [
"async-trait",
"chrono",
"clap",
"community_settings_core",
"libc",

View file

@ -28,12 +28,20 @@ simplelog = "0.12"
# limits & driver functionality
limits_core = { version = "3", path = "./limits_core" }
community_settings_core = { version = "0.1", path = "./community_settings_core" }
regex = "1"
# steam deck libs
smokepatio = { version = "0.1", features = [ "std" ], path = "../../smokepatio" }
libc = "0.2"
# online settings
community_settings_core = { version = "0.1", path = "./community_settings_core" }
chrono = { version = "0.4", features = [ "serde" ] }
# hardware enablement
#libryzenadj = { version = "0.14", path = "../../libryzenadj-rs-14" }
libryzenadj = { version = "0.13" }
# ureq's tls feature does not like musl targets
ureq = { version = "2", features = ["json", "gzip", "brotli", "charset"], default-features = false, optional = true }
@ -53,13 +61,13 @@ debug = false
strip = true
lto = true
codegen-units = 1
opt-level = 3
[profile.docker]
inherits = "release"
debug = false
strip = true
lto = "thin"
codegen-units = 16
opt-level = 2
codegen-units = 8
debug-assertions = false
overflow-checks = false

View file

@ -111,14 +111,17 @@ pub fn load_variant(
) -> impl Fn(super::ApiParameterType) -> super::ApiParameterType {
let sender = Mutex::new(sender); // Sender is not Sync; this is required for safety
let setter = move |variant: u64, variant_name: Option<String>| {
log::debug!("load_variant(variant: {}, variant_name: {:?})", variant, variant_name);
log::debug!(
"load_variant(variant: {}, variant_name: {:?})",
variant,
variant_name
);
sender
.lock()
.unwrap()
.send(ApiMessage::LoadVariant(
variant,
variant_name
.unwrap_or_else(|| "".to_owned()),
variant_name.unwrap_or_else(|| "".to_owned()),
))
.expect("load_variant send failed")
};

View file

@ -3,17 +3,36 @@ use std::sync::{Arc, Mutex, RwLock};
use usdpl_back::core::serdes::Primitive;
use usdpl_back::AsyncCallable;
use chrono::{offset::Utc, DateTime};
use serde::{Deserialize, Serialize};
use super::handler::{ApiMessage, GeneralMessage};
const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us";
static BASE_URL: RwLock<Option<String>> = RwLock::new(None);
const MAX_CACHE_DURATION: std::time::Duration =
std::time::Duration::from_secs(60 * 60 * 24 * 7 /* 7 days */);
#[derive(Serialize, Deserialize, Clone, Debug)]
struct CachedData<T> {
data: T,
updated: DateTime<Utc>,
}
type StoreCache =
std::collections::HashMap<u32, CachedData<Vec<community_settings_core::v1::Metadata>>>;
pub fn set_base_url(base_url: String) {
*BASE_URL.write().expect("Failed to acquire write lock for store base url") = Some(base_url);
*BASE_URL
.write()
.expect("Failed to acquire write lock for store base url") = Some(base_url);
}
fn get_base_url() -> String {
BASE_URL.read().expect("Failed to acquire read lock for store base url")
BASE_URL
.read()
.expect("Failed to acquire read lock for store base url")
.clone()
.unwrap_or_else(|| BASE_URL_FALLBACK.to_owned())
}
@ -30,35 +49,124 @@ fn url_upload_config() -> String {
format!("{}/api/setting", get_base_url())
}
fn cache_path() -> std::path::PathBuf {
crate::utility::settings_dir().join(crate::consts::WEB_SETTINGS_CACHE)
}
fn load_cache() -> StoreCache {
let path = cache_path();
let file = match std::fs::File::open(&path) {
Ok(f) => f,
Err(e) => {
log::warn!("Failed to open store cache {}: {}", path.display(), e);
return StoreCache::default();
}
};
let mut file = std::io::BufReader::new(file);
match ron::de::from_reader(&mut file) {
Ok(cache) => cache,
Err(e) => {
log::error!("Failed to parse store cache {}: {}", path.display(), e);
return StoreCache::default();
}
}
}
fn save_cache(cache: &StoreCache) {
let path = cache_path();
let file = match std::fs::File::create(&path) {
Ok(f) => f,
Err(e) => {
log::warn!("Failed to create store cache {}: {}", path.display(), e);
return;
}
};
let mut file = std::io::BufWriter::new(file);
if let Err(e) =
ron::ser::to_writer_pretty(&mut file, cache, crate::utility::ron_pretty_config())
{
log::error!("Failed to parse store cache {}: {}", path.display(), e);
}
}
fn get_maybe_cached(steam_app_id: u32) -> Vec<community_settings_core::v1::Metadata> {
let mut cache = load_cache();
if let Some(cached_result) = cache.get(&steam_app_id) {
if cached_result.updated < (Utc::now() - MAX_CACHE_DURATION) {
// cache needs update
if let Ok(result) = search_by_app_id_online(steam_app_id) {
cache.insert(
steam_app_id,
CachedData {
data: result.clone(),
updated: Utc::now(),
},
);
save_cache(&cache);
result
} else {
// if all else fails, out of date results are better than no results
cached_result.data.to_owned()
}
} else {
// cache is ok, use it
cached_result.data.to_owned()
}
} else {
if let Ok(result) = search_by_app_id_online(steam_app_id) {
cache.insert(
steam_app_id,
CachedData {
data: result.clone(),
updated: Utc::now(),
},
);
save_cache(&cache);
result
} else {
Vec::with_capacity(0)
}
}
}
fn search_by_app_id_online(
steam_app_id: u32,
) -> std::io::Result<Vec<community_settings_core::v1::Metadata>> {
let req_url = url_search_by_app_id(steam_app_id);
match ureq::get(&req_url).call() {
Ok(response) => {
let json_res: std::io::Result<Vec<community_settings_core::v1::Metadata>> =
response.into_json();
match json_res {
Ok(search_results) => Ok(search_results),
Err(e) => {
log::error!("Cannot parse response from `{}`: {}", req_url, e);
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))
}
}
}
Err(e) => {
log::warn!("Cannot get search results from `{}`: {}", req_url, e);
Err(std::io::Error::new(
std::io::ErrorKind::ConnectionAborted,
e,
))
}
}
}
/// Get search results web method
pub fn search_by_app_id() -> impl AsyncCallable {
let getter = move || {
move |steam_app_id: u32| {
let req_url = url_search_by_app_id(steam_app_id);
match ureq::get(&req_url).call() {
Ok(response) => {
let json_res: std::io::Result<Vec<community_settings_core::v1::Metadata>> =
response.into_json();
match json_res {
Ok(search_results) => {
// search results may be quite large, so let's do the JSON string conversion in the background (blocking) thread
match serde_json::to_string(&search_results) {
Err(e) => log::error!(
"Cannot convert search results from `{}` to JSON: {}",
req_url,
e
),
Ok(s) => return s,
}
}
Err(e) => {
log::error!("Cannot parse response from `{}`: {}", req_url, e)
}
}
let search_results = get_maybe_cached(steam_app_id);
match serde_json::to_string(&search_results) {
Err(e) => {
log::error!("Cannot convert search results to JSON: {}", e);
"[]".to_owned()
}
Err(e) => log::warn!("Cannot get search results from `{}`: {}", req_url, e),
Ok(s) => s,
}
"[]".to_owned()
}
};
super::async_utils::AsyncIsh {

View file

@ -26,10 +26,7 @@ impl Args {
}
pub fn is_default(&self) -> bool {
self.port.is_none()
&& self.log.is_none()
&& !self.verbose
&& self.op.is_none()
self.port.is_none() && self.log.is_none() && !self.verbose && self.op.is_none()
}
}

View file

@ -12,7 +12,9 @@ pub fn clean_up() -> Result<(), ()> {
}
}
fn clean_up_io(directories: impl Iterator<Item=impl AsRef<std::path::Path>>) -> std::io::Result<()> {
fn clean_up_io(
directories: impl Iterator<Item = impl AsRef<std::path::Path>>,
) -> std::io::Result<()> {
let results = directories.map(|dir| std::fs::remove_dir_all(dir));
for res in results {
res?;

View file

@ -1,8 +1,8 @@
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread::{self, JoinHandle};
use std::sync::mpsc::{channel, Sender};
use std::io::Write;
use std::thread::{self, JoinHandle};
pub fn dump_sys_info() -> Result<(), ()> {
let (tx, rx) = channel();
@ -16,9 +16,7 @@ pub fn dump_sys_info() -> Result<(), ()> {
join_handles.push(read_file(file, tx.clone()));
}
let useful_commands = vec![
"dmidecode",
];
let useful_commands = vec!["dmidecode"];
for cmd in useful_commands.into_iter() {
join_handles.push(execute_command(cmd, tx.clone()));
}
@ -29,38 +27,48 @@ pub fn dump_sys_info() -> Result<(), ()> {
}
}
let mut dump_file = std::fs::File::create("powertools_sys_dump.txt").expect("Failed to create dump file");
let mut dump_file =
std::fs::File::create("powertools_sys_dump.txt").expect("Failed to create dump file");
for response in rx.into_iter() {
dump_file.write(
&format!("{} v{} ###### {} ######\n{}\n",
crate::consts::PACKAGE_NAME,
crate::consts::PACKAGE_VERSION,
response.0,
response.1.unwrap_or("[None]".to_owned())
).into_bytes()
).expect("Failed to write to dump file");
dump_file
.write(
&format!(
"{} v{} ###### {} ######\n{}\n",
crate::consts::PACKAGE_NAME,
crate::consts::PACKAGE_VERSION,
response.0,
response.1.unwrap_or("[None]".to_owned())
)
.into_bytes(),
)
.expect("Failed to write to dump file");
}
Ok(())
}
fn read_file(file: impl AsRef<Path> + Send + 'static, tx: Sender<(String, Option<String>)>) -> JoinHandle<()> {
fn read_file(
file: impl AsRef<Path> + Send + 'static,
tx: Sender<(String, Option<String>)>,
) -> JoinHandle<()> {
thread::spawn(move || {
let file = file.as_ref();
tx.send(
(file.display().to_string(),
std::fs::read_to_string(file).ok())
).expect("Failed to send file contents");
tx.send((
file.display().to_string(),
std::fs::read_to_string(file).ok(),
))
.expect("Failed to send file contents");
})
}
fn execute_command(command: &'static str, tx: Sender<(String, Option<String>)>) -> JoinHandle<()> {
thread::spawn(move || {
tx.send(
(command.to_owned(), Command::new(command)
tx.send((
command.to_owned(),
Command::new(command)
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).into_owned())
.ok()
)).expect("Failed to send command output");
}
)
.ok(),
))
.expect("Failed to send command output");
})
}

View file

@ -10,4 +10,6 @@ pub const DEFAULT_SETTINGS_VARIANT_NAME: &str = "Primary";
pub const LIMITS_FILE: &str = "limits_cache.ron";
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.ron";
pub const WEB_SETTINGS_CACHE: &str = "store_cache.ron";
pub const MESSAGE_SEEN_ID_FILE: &str = "seen_message.bin";

View file

@ -50,7 +50,11 @@ fn main() -> Result<(), ()> {
},
#[cfg(not(debug_assertions))]
{
if args.verbose { LevelFilter::Debug } else { LevelFilter::Info }
if args.verbose {
LevelFilter::Debug
} else {
LevelFilter::Info
}
},
Default::default(),
std::fs::File::create(&log_filepath).expect("Failed to create log file"),

View file

@ -71,7 +71,13 @@ impl FileJson {
if setting.name.is_empty() {
setting.name = format!("Variant {}", setting.variant);
}
log::debug!("Inserting setting variant `{}` ({}) for app `{}` ({})", setting.name, setting.variant, file.name, app_id);
log::debug!(
"Inserting setting variant `{}` ({}) for app `{}` ({})",
setting.name,
setting.variant,
file.name,
app_id
);
file.variants.insert(setting.variant, setting.clone());
(file, setting)
} else {
@ -83,15 +89,24 @@ impl FileJson {
if setting.name.is_empty() {
setting.name = format!("Variant {}", setting.variant);
}
log::debug!("Creating new setting variant `{}` ({}) for app `{}` ({})", setting.name, setting.variant, app_name, app_id);
log::debug!(
"Creating new setting variant `{}` ({}) for app `{}` ({})",
setting.name,
setting.variant,
app_name,
app_id
);
let mut setting_variants = HashMap::with_capacity(1);
setting_variants.insert(setting.variant, setting.clone());
(Self {
version: 0,
app_id: app_id,
name: app_name,
variants: setting_variants,
}, setting)
(
Self {
version: 0,
app_id: app_id,
name: app_name,
variants: setting_variants,
},
setting,
)
};
file.save(path)?;

View file

@ -135,7 +135,8 @@ impl TGeneral for General {
setting: SettingVariant::General,
})
.map(|file| {
file.0.variants
file.0
.variants
.into_iter()
.map(|(id, conf)| crate::api::VariantInfo {
id: id.to_string(),
@ -147,7 +148,11 @@ impl TGeneral for General {
}
fn get_variant_info(&self) -> crate::api::VariantInfo {
log::debug!("Current variant `{}` ({})", self.variant_name, self.variant_id);
log::debug!(
"Current variant `{}` ({})",
self.variant_name,
self.variant_id
);
crate::api::VariantInfo {
id: self.variant_id.to_string(),
name: self.variant_name.clone(),
@ -260,7 +265,10 @@ impl Settings {
let mut valid_ids: Vec<&u64> = settings_file.variants.keys().collect();
valid_ids.sort();
if let Some(id) = valid_ids.get(0) {
Ok(settings_file.variants.get(id).expect("variant id key magically disappeared"))
Ok(settings_file
.variants
.get(id)
.expect("variant id key magically disappeared"))
} else {
Err(SettingError {
msg: format!(
@ -293,7 +301,11 @@ impl Settings {
let json_path = crate::utility::settings_dir().join(&filename);
if json_path.exists() {
if variant == u64::MAX {
log::debug!("Creating new variant `{}` in existing settings file {}", variant_name, json_path.display());
log::debug!(
"Creating new variant `{}` in existing settings file {}",
variant_name,
json_path.display()
);
self.create_and_load_variant(&json_path, app_id, variant_name)?;
} else {
let file_json = FileJson::open(&json_path).map_err(|e| SettingError {
@ -336,7 +348,11 @@ impl Settings {
}
*self.general.persistent() = false;
if variant == u64::MAX {
log::debug!("Creating new variant `{}` in new settings file {}", variant_name, json_path.display());
log::debug!(
"Creating new variant `{}` in new settings file {}",
variant_name,
json_path.display()
);
self.create_and_load_variant(&json_path, app_id, variant_name)?;
}
}
@ -345,7 +361,12 @@ impl Settings {
Ok(*self.general.persistent())
}
fn create_and_load_variant(&mut self, json_path: &PathBuf, app_id: u64, variant_name: String) -> Result<(), SettingError> {
fn create_and_load_variant(
&mut self,
json_path: &PathBuf,
app_id: u64,
variant_name: String,
) -> Result<(), SettingError> {
*self.general.persistent() = true;
self.general.variant_id(u64::MAX);
self.general.variant_name(variant_name.clone());

View file

@ -196,9 +196,20 @@ impl Gpu {
let is_lcd = matches!(self.variant, super::Model::LCD);
let is_lock_feature_enabled = self.limits.extras.quirks.contains("pp_dpm_fclk-static");
if (is_oled && self.limits.extras.quirks.contains("pp_dpm_fclk-reversed-on-OLED"))
|| (is_lcd && self.limits.extras.quirks.contains("pp_dpm_fclk-reversed-on-LCD"))
|| self.limits.extras.quirks.contains("pp_dpm_fclk-reversed") {
if (is_oled
&& self
.limits
.extras
.quirks
.contains("pp_dpm_fclk-reversed-on-OLED"))
|| (is_lcd
&& self
.limits
.extras
.quirks
.contains("pp_dpm_fclk-reversed-on-LCD"))
|| self.limits.extras.quirks.contains("pp_dpm_fclk-reversed")
{
let options_count = self
.sysfs_card
.read_value(GPU_MEMORY_DOWNCLOCK_ATTRIBUTE.to_owned())
@ -208,12 +219,13 @@ impl Gpu {
if is_lock_feature_enabled {
format!("{}\n", modifier - max_val)
} else {
if max_val == 0 as u64 {
if max_val == 0 as u64 {
format!("{}\n", modifier)
} else {
use std::fmt::Write;
let mut payload = format!("{}", modifier - max_val);
for i in (0..max_val).rev(/* rev() isn't necessary but it creates a nicer (ascending) order */) {
for i in (0..max_val).rev(/* rev() isn't necessary but it creates a nicer (ascending) order */)
{
write!(payload, " {}", modifier - i)
.expect("Failed to write to memory payload (should be infallible!?)");
}

View file

@ -174,6 +174,7 @@ mod generate {
);
let savefile = crate::persist::FileJson {
version: 0,
app_id: 0,
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
variants: mini_variants,
};