Skip to content

Commit 2306550

Browse files
Copilotgfauredev
andauthored
feat: Cachix caching, extract magic values into constants, remove bloat
Agent-Logs-Url: https://github.com/gfauredev/LogOut/sessions/aef32a65-7c1b-414f-a4df-2632d06ea734 Co-authored-by: gfauredev <19304085+gfauredev@users.noreply.github.com>
1 parent edcabe5 commit 2306550

8 files changed

Lines changed: 62 additions & 26 deletions

File tree

.github/workflows/cd.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ jobs:
3535
extra_nix_config: |
3636
extra-substituters = https://cache.garnix.io
3737
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
38+
- uses: cachix/cachix-action@v16
39+
with:
40+
name: gfauredev
41+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
3842
- uses: DeterminateSystems/magic-nix-cache-action@v13
3943
- run: sleep 5 # Sleep hoping Garnix evaluation will start after
4044
- uses: lewagon/wait-on-check-action@v1.5.0
@@ -111,6 +115,10 @@ jobs:
111115
extra_nix_config: |
112116
extra-substituters = https://cache.garnix.io
113117
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
118+
- uses: cachix/cachix-action@v16
119+
with:
120+
name: gfauredev
121+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
114122
- uses: DeterminateSystems/magic-nix-cache-action@v13
115123
- run: sleep 5 # Sleep hoping Garnix evaluation will start after
116124
- uses: lewagon/wait-on-check-action@v1.5.0
@@ -154,6 +162,10 @@ jobs:
154162
extra_nix_config: |
155163
extra-substituters = https://cache.garnix.io
156164
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
165+
- uses: cachix/cachix-action@v16
166+
with:
167+
name: gfauredev
168+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
157169
- uses: DeterminateSystems/magic-nix-cache-action@v13
158170
- run: sleep 5 # Sleep hoping Garnix evaluation will start after
159171
- uses: lewagon/wait-on-check-action@v1.5.0

.github/workflows/ci.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ jobs:
3333
extra_nix_config: |
3434
extra-substituters = https://cache.garnix.io
3535
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
36+
- uses: cachix/cachix-action@v16
37+
with:
38+
name: gfauredev
39+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
3640
- uses: DeterminateSystems/magic-nix-cache-action@v13
3741
- run: sleep 5 # Sleep hoping Garnix evaluation will start after
3842
- uses: lewagon/wait-on-check-action@v1.5.0
@@ -80,6 +84,10 @@ jobs:
8084
extra_nix_config: |
8185
extra-substituters = https://cache.garnix.io
8286
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
87+
- uses: cachix/cachix-action@v16
88+
with:
89+
name: gfauredev
90+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
8391
- uses: DeterminateSystems/magic-nix-cache-action@v13
8492
- name: Start web app server
8593
run: |
@@ -108,13 +116,16 @@ jobs:
108116
timeout-minutes: 40
109117
steps:
110118
- uses: actions/checkout@v6
111-
# - uses: wimpysworld/nothing-but-nix@v10
112119
- uses: cachix/install-nix-action@v31
113120
with:
114121
github_access_token: ${{ secrets.GITHUB_TOKEN }}
115122
extra_nix_config: |
116123
extra-substituters = https://cache.garnix.io
117124
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
125+
- uses: cachix/cachix-action@v16
126+
with:
127+
name: gfauredev
128+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
118129
- uses: DeterminateSystems/magic-nix-cache-action@v13
119130
- name: Remove system Chrome to prevent version mismatch with nixpkgs chromedriver
120131
run: sudo apt-get remove -y google-chrome-stable

.github/workflows/copilot-setup-steps.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,10 @@ jobs:
2222
extra_nix_config: |
2323
extra-substituters = https://cache.garnix.io
2424
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
25+
- uses: cachix/cachix-action@v16
26+
with:
27+
name: gfauredev
28+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
2529
- uses: DeterminateSystems/magic-nix-cache-action@v13
26-
# - uses: lewagon/wait-on-check-action@v1.5.0
27-
# with:
28-
# ref: ${{ github.event.pull_request.head.sha || github.sha }}
29-
# check-regexp: 'Evaluate flake.nix'
30-
# repo-token: ${{ secrets.GITHUB_TOKEN }}
31-
# fail-on-no-checks: false
32-
# allowed-conclusions: success,failure,cancelled,skipped,timed_out,neutral
33-
# - uses: lewagon/wait-on-check-action@v1.5.0
34-
# with:
35-
# ref: ${{ github.event.pull_request.head.sha || github.sha }}
36-
# check-regexp: 'devShell.*'
37-
# repo-token: ${{ secrets.GITHUB_TOKEN }}
38-
# fail-on-no-checks: false
39-
# allowed-conclusions: success,failure,cancelled,skipped,timed_out,neutral
4030
- run: nix develop --command cargo check # Install dependencies
4131
continue-on-error: true

.github/workflows/heavy.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ jobs:
1414
extra_nix_config: |
1515
extra-substituters = https://cache.garnix.io
1616
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
17+
- uses: cachix/cachix-action@v16
18+
with:
19+
name: gfauredev
20+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
1721
- uses: DeterminateSystems/magic-nix-cache-action@v13
1822
- run: nix develop -c cargo deny check
1923

@@ -28,6 +32,10 @@ jobs:
2832
extra_nix_config: |
2933
extra-substituters = https://cache.garnix.io
3034
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
35+
- uses: cachix/cachix-action@v16
36+
with:
37+
name: gfauredev
38+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
3139
- uses: DeterminateSystems/magic-nix-cache-action@v13
3240
- run: nix develop -c cargo mutants --no-shuffle \
3341
--caught --timeout 300 --exclude "src/components/*"
@@ -38,14 +46,17 @@ jobs:
3846
permissions:
3947
contents: read
4048
steps:
41-
# - uses: wimpysworld/nothing-but-nix@v10
4249
- uses: actions/checkout@v6
4350
- uses: cachix/install-nix-action@v31
4451
with:
4552
github_access_token: ${{ secrets.GITHUB_TOKEN }}
4653
extra_nix_config: |
4754
extra-substituters = https://cache.garnix.io
4855
extra-trusted-public-keys = cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=
56+
- uses: cachix/cachix-action@v16
57+
with:
58+
name: gfauredev
59+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
4960
- uses: DeterminateSystems/magic-nix-cache-action@v13
5061
- run: gh release download --pattern "*.apk" --dir . # From CD
5162
env:

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ where
328328
E: AsRef<models::Exercise>,
329329
{
330330
use models::{Category, Distance, ExerciseLog, Force, Weight, WorkoutSession};
331-
let base_time = models::get_current_timestamp().saturating_sub(3600);
331+
let base_time = models::get_current_timestamp().saturating_sub(utils::SECONDS_IN_HOUR);
332332
let mut session = WorkoutSession::new();
333333
session.start_time = base_time;
334334
for (i, entry) in entries.iter().enumerate() {

src/models/analytics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use crate::models::{ExerciseLog, HG_PER_KG, M_PER_KM};
2+
/// Minimum average duration (in minutes) below which values are displayed in seconds.
3+
const DURATION_MINS_SECS_THRESHOLD: f64 = 3.0;
4+
/// Minimum average duration (in minutes) below which values are displayed in minutes rather than hours.
5+
const DURATION_HOURS_MINS_THRESHOLD: f64 = 180.0;
26

37
#[derive(Clone, Copy, PartialEq, Debug, serde::Serialize, serde::Deserialize)]
48
pub enum Metric {
@@ -53,9 +57,9 @@ pub fn adapt_metric_unit(metric: Metric, values: &[f64]) -> (&'static str, f64)
5357
}
5458
}
5559
Metric::Duration => {
56-
if avg < 3.0 {
60+
if avg < DURATION_MINS_SECS_THRESHOLD {
5761
("s", 60.0)
58-
} else if avg < 180.0 {
62+
} else if avg < DURATION_HOURS_MINS_THRESHOLD {
5963
("min", 1.0)
6064
} else {
6165
("h", 1.0 / 60.0)

src/services/exercise_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub(crate) async fn download_exercises() -> Result<Option<Vec<Exercise>>, String
182182
.send()
183183
.await
184184
.map_err(|e| format!("HTTP error: {e}"))?;
185-
if response.status().as_u16() == 304 {
185+
if response.status() == reqwest::StatusCode::NOT_MODIFIED {
186186
log::info!("exercises.json is up to date (304 Not Modified)");
187187
return Ok(None);
188188
}
@@ -216,7 +216,7 @@ async fn download_exercise_lang(lang: &str) -> Result<Vec<ExerciseLangEntry>, St
216216
let response = reqwest::get(&url)
217217
.await
218218
.map_err(|e| format!("HTTP error fetching {lang} lang file: {e}"))?;
219-
if response.status().as_u16() == 404 {
219+
if response.status() == reqwest::StatusCode::NOT_FOUND {
220220
return Ok(Vec::new());
221221
}
222222
if !response.status().is_success() {

src/services/storage.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ pub(crate) mod native_storage {
736736
pub const STORE_SESSIONS: &str = "sessions";
737737
pub const STORE_CUSTOM_EXERCISES: &str = "custom_exercises";
738738
pub const STORE_EXERCISES: &str = "exercises";
739+
/// Name of the application data sub-directory under the OS data dir.
740+
const APP_DATA_DIR_NAME: &str = "log-out";
741+
/// File name of the `SQLite` database within the application data directory.
742+
pub const DB_FILENAME: &str = "log-out.db";
743+
/// `SQLite` `user_version` value written on a successful schema migration.
744+
/// Any database with a lower version is wiped and recreated from scratch.
745+
const SCHEMA_VERSION: u32 = 2;
739746
/// Structured error type for native (`SQLite`) storage operations.
740747
#[derive(Debug, thiserror::Error)]
741748
pub enum StorageError {
@@ -781,10 +788,10 @@ pub(crate) mod native_storage {
781788
}
782789
dirs::data_local_dir()
783790
.unwrap_or_else(|| PathBuf::from("."))
784-
.join("log-out")
791+
.join(APP_DATA_DIR_NAME)
785792
}
786793
fn db_path() -> PathBuf {
787-
data_dir().join("log-out.db")
794+
data_dir().join(DB_FILENAME)
788795
}
789796
/// Runs incremental schema migrations to bring the database up to the current version.
790797
///
@@ -796,8 +803,9 @@ pub(crate) mod native_storage {
796803
/// reset without needing to re-create the long-lived connection.
797804
fn apply_migration_if_needed(conn: &Connection) -> Result<(), StorageError> {
798805
let schema_version: u32 = conn.query_row("PRAGMA user_version", [], |r| r.get(0))?;
799-
if schema_version < 2 {
806+
if schema_version < SCHEMA_VERSION {
800807
// Fresh install or outdated schema: drop everything and start clean.
808+
// SCHEMA_VERSION must match the `PRAGMA user_version` value at the end.
801809
conn.execute_batch(
802810
"DROP TABLE IF EXISTS sessions;
803811
DROP TABLE IF EXISTS custom_exercises;
@@ -1500,7 +1508,7 @@ mod tests {
15001508
let _g = lock();
15011509
native_storage::get_all::<WorkoutSession>(native_storage::STORE_SESSIONS).ok();
15021510
{
1503-
let db_path = native_storage::data_dir().join("log-out.db");
1511+
let db_path = native_storage::data_dir().join(native_storage::DB_FILENAME);
15041512
if db_path.exists() {
15051513
let conn = rusqlite::Connection::open(&db_path).unwrap();
15061514
conn.execute_batch(
@@ -1544,7 +1552,7 @@ mod tests {
15441552
#[test]
15451553
fn get_all_skips_corrupt_rows() {
15461554
let _g = lock();
1547-
let db_path = native_storage::data_dir().join("log-out.db");
1555+
let db_path = native_storage::data_dir().join(native_storage::DB_FILENAME);
15481556
native_storage::get_all::<WorkoutSession>(native_storage::STORE_SESSIONS).unwrap();
15491557
{
15501558
let conn = rusqlite::Connection::open(&db_path).unwrap();

0 commit comments

Comments
 (0)