generated from nhcarrigan/template
feat: massive overhaul to manage costs (#103)
### Explanation _No response_ ### Issue Closes #102 ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #103 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #103.
This commit is contained in:
@@ -556,6 +556,97 @@ pub async fn rename_path(old_path: String, new_path: String) -> Result<(), Strin
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ==================== Cost Tracking Commands ====================
|
||||
|
||||
const COST_HISTORY_STORE_KEY: &str = "cost_history";
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_cost_summary(app: AppHandle, days: u32) -> Result<crate::cost_tracking::CostSummary, String> {
|
||||
let history = load_cost_history(&app).await;
|
||||
Ok(history.get_summary(days))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_cost_alerts(app: AppHandle) -> Result<Vec<crate::cost_tracking::CostAlert>, String> {
|
||||
let mut history = load_cost_history(&app).await;
|
||||
let alerts = history.check_alerts();
|
||||
|
||||
// Save updated alert state
|
||||
save_cost_history(&app, &history).await?;
|
||||
|
||||
Ok(alerts)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_cost_alert_thresholds(
|
||||
app: AppHandle,
|
||||
daily: Option<f64>,
|
||||
weekly: Option<f64>,
|
||||
monthly: Option<f64>,
|
||||
) -> Result<(), String> {
|
||||
let mut history = load_cost_history(&app).await;
|
||||
history.set_alert_thresholds(daily, weekly, monthly);
|
||||
save_cost_history(&app, &history).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn export_cost_csv(app: AppHandle, days: u32) -> Result<String, String> {
|
||||
let history = load_cost_history(&app).await;
|
||||
Ok(history.export_csv(days))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_today_cost(app: AppHandle) -> Result<f64, String> {
|
||||
let history = load_cost_history(&app).await;
|
||||
Ok(history.get_today_cost())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_week_cost(app: AppHandle) -> Result<f64, String> {
|
||||
let history = load_cost_history(&app).await;
|
||||
Ok(history.get_week_cost())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_month_cost(app: AppHandle) -> Result<f64, String> {
|
||||
let history = load_cost_history(&app).await;
|
||||
Ok(history.get_month_cost())
|
||||
}
|
||||
|
||||
/// Add cost to history (called internally when stats are updated)
|
||||
pub async fn record_cost(app: &AppHandle, input_tokens: u64, output_tokens: u64, cost_usd: f64) {
|
||||
let mut history = load_cost_history(app).await;
|
||||
history.add_cost(input_tokens, output_tokens, cost_usd);
|
||||
let _ = save_cost_history(app, &history).await;
|
||||
}
|
||||
|
||||
/// Record a new session
|
||||
pub async fn record_session(app: &AppHandle) {
|
||||
let mut history = load_cost_history(app).await;
|
||||
history.increment_sessions();
|
||||
let _ = save_cost_history(app, &history).await;
|
||||
}
|
||||
|
||||
async fn load_cost_history(app: &AppHandle) -> crate::cost_tracking::CostHistory {
|
||||
let store = match app.store("hikari-cost-history.json") {
|
||||
Ok(s) => s,
|
||||
Err(_) => return crate::cost_tracking::CostHistory::new(),
|
||||
};
|
||||
|
||||
match store.get(COST_HISTORY_STORE_KEY) {
|
||||
Some(value) => serde_json::from_value(value.clone()).unwrap_or_default(),
|
||||
None => crate::cost_tracking::CostHistory::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_cost_history(app: &AppHandle, history: &crate::cost_tracking::CostHistory) -> Result<(), String> {
|
||||
let store = app.store("hikari-cost-history.json").map_err(|e| e.to_string())?;
|
||||
let value = serde_json::to_value(history).map_err(|e| e.to_string())?;
|
||||
store.set(COST_HISTORY_STORE_KEY, value);
|
||||
store.save().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user