From ccbbc8011645e766f67b443d7b49a08e197eda3e Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Sun, 29 Oct 2023 13:37:40 +0000 Subject: [PATCH] Fix mapping import, show mapping count on list --- hamstertools/__init__.py | 9 +++++---- hamstertools/app.py | 31 ++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hamstertools/__init__.py b/hamstertools/__init__.py index 758d2e6..84eb479 100755 --- a/hamstertools/__init__.py +++ b/hamstertools/__init__.py @@ -95,7 +95,6 @@ def list_category_activities(ids): def tidy_categories(): """ Remove categories with no activities """ - # Create a subquery to calculate the count of activities per category subquery = ( HamsterCategory .select(HamsterCategory, fn.COUNT(HamsterActivity.id).alias('activities_count')) @@ -104,7 +103,6 @@ def tidy_categories(): .alias('subquery') ) - # Use the subquery to filter categories where activities_count is 0 categories = ( HamsterCategory .select() @@ -601,17 +599,20 @@ def reset(): @db_.command() @click.option('-g', '--global', 'global_', help='Does this file contain mappings to global activties', is_flag=True) @click.option('--mapping-path', help='Mapping file') -def import_csv(mapping_path=None, global_=False): +def mapping2db(mapping_path=None, global_=False): mapping_file = _get_kimai_mapping_file(mapping_path, None) next(mapping_file) mapping_reader = csv.reader(mapping_file) for row in mapping_reader: + hamster_category = HamsterCategory.get(name=row[0]) - hamster_activity = HamsterActivity.get(name=row[1]) + hamster_activity = HamsterActivity.get(name=row[1], + category_id=hamster_category.id) kimai_customer = KimaiCustomer.get(name=row[2]) kimai_project = KimaiProject.get(name=row[3], customer_id=kimai_customer.id) + try: kimai_activity = KimaiActivity.get( name=row[4], diff --git a/hamstertools/app.py b/hamstertools/app.py index d9153e7..3eed92a 100644 --- a/hamstertools/app.py +++ b/hamstertools/app.py @@ -15,6 +15,7 @@ from .db import ( KimaiProject, KimaiCustomer, KimaiActivity, + HamsterKimaiMapping, ) from .kimai import ( KimaiAPI, @@ -78,15 +79,34 @@ class ActivitiesScreen(ListScreen): def _refresh(self, filter_query=None): self.table.clear() + facts_count_query = ( + HamsterFact + .select(HamsterFact.activity_id, fn.COUNT(HamsterFact.id).alias('facts_count')) + .group_by(HamsterFact.activity_id) + .alias('facts_count_query') + ) + + mappings_count_query = ( + HamsterKimaiMapping + .select(HamsterKimaiMapping.hamster_activity_id, + fn.COUNT(HamsterKimaiMapping.id).alias('mappings_count')) + .group_by(HamsterKimaiMapping.hamster_activity_id) + .alias('mappings_count_query') + ) + activities = ( HamsterActivity.select( HamsterActivity, - HamsterCategory, - fn.Count(HamsterFact.id).alias("facts_count") + HamsterCategory.id, + fn.COALESCE(HamsterCategory.name, 'None').alias("category_name"), + fn.COALESCE(facts_count_query.c.facts_count, 0).alias('facts_count'), + fn.COALESCE(mappings_count_query.c.mappings_count, 0).alias('mappings_count') ) .join(HamsterCategory, JOIN.LEFT_OUTER) .switch(HamsterActivity) - .join(HamsterFact, JOIN.LEFT_OUTER) + .join(facts_count_query, JOIN.LEFT_OUTER, on=(HamsterActivity.id == facts_count_query.c.activity_id)) + .switch(HamsterActivity) + .join(mappings_count_query, JOIN.LEFT_OUTER, on=(HamsterActivity.id == mappings_count_query.c.hamster_activity_id)) .group_by(HamsterActivity) ) @@ -100,10 +120,11 @@ class ActivitiesScreen(ListScreen): [ [ activity.category_id, - (activity.category.name if (activity.category_id != -1) else ""), + activity.category_name, activity.id, activity.name, activity.facts_count, + activity.mappings_count, ] for activity in activities ] @@ -115,7 +136,7 @@ class ActivitiesScreen(ListScreen): self.table = self.query_one(DataTable) self.table.cursor_type = "row" self.columns = self.table.add_columns( - "category id", "category", "activity id", "activity", "entries" + "category id", "category", "activity id", "activity", "entries", "mappings" ) self.sort = (self.columns[1], self.columns[3]) self._refresh()