1
0

Step counts
All checks were successful
Test Python / Test (push) Successful in 34s

This commit is contained in:
Jon Michael Aanes 2024-10-08 21:22:18 +02:00
parent 6b606475df
commit c74920e478
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 40 additions and 8 deletions

View File

@ -6,6 +6,7 @@ Sub-module for importing time-based data into Obsidian.
import datetime
from logging import getLogger
from pathlib import Path
from typing import Any
from personal_data.util import load_csv_file
@ -13,13 +14,7 @@ from .obsidian import ObsidianVault
logger = getLogger(__name__)
def import_data(obsidian_path: Path, dry_run=True):
vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None)
data_path = Path('/home/jmaa/Notes/workout.csv')
rows = load_csv_file(data_path)
logger.info('Loaded CSV with %d lines', len(rows))
def import_workout_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int:
num_updated = 0
for row in rows:
date = row['Date']
@ -43,5 +38,41 @@ def import_data(obsidian_path: Path, dry_run=True):
if was_updated:
num_updated += 1
del row, date
return num_updated
def import_step_counts_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int:
MINIMUM = 300
num_updated = 0
rows_per_day = {}
for row in rows:
date = row['Start'].date()
rows_per_day.setdefault(date, [])
rows_per_day[date].append(row)
del date, row
steps_per_day = { date: sum(row['Steps'] for row in rows) for date, rows in rows_per_day.items()}
for date, steps in steps_per_day.items():
if steps < MINIMUM:
continue
was_updated = vault.add_statistic(date, 'Steps', steps)
if was_updated:
num_updated += 1
del date, steps, was_updated
return num_updated
def import_data(obsidian_path: Path, dry_run=True):
vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None)
#data_path = Path('/home/jmaa/Notes/workout.csv')
data_path = Path('/home/jmaa/personal-archive/misc-data/step_counts_2023-07-26_to_2024-09-21.csv')
rows = load_csv_file(data_path)
logger.info('Loaded CSV with %d lines', len(rows))
#num_updated = import_workout_csv(vault, rows)
num_updated = import_step_counts_csv(vault, rows)
logger.info('Updated %d files', num_updated)

View File

@ -41,9 +41,10 @@ class ObsidianVault:
) -> bool:
if self.read_only == 'silent':
logger.info(
'Real only ObsidianVault ignoring add_statistic(%s, "%s", ?)',
'Real only ObsidianVault ignoring add_statistic(%s, "%s", %s)',
date,
statistic_key,
amount,
)
return False