Fix mapping import, show mapping count on list

This commit is contained in:
3wc 2023-10-29 13:37:40 +00:00
parent f8f83ce4d4
commit ccbbc80116
2 changed files with 31 additions and 9 deletions

View File

@ -95,7 +95,6 @@ def list_category_activities(ids):
def tidy_categories(): def tidy_categories():
""" Remove categories with no activities """ """ Remove categories with no activities """
# Create a subquery to calculate the count of activities per category
subquery = ( subquery = (
HamsterCategory HamsterCategory
.select(HamsterCategory, fn.COUNT(HamsterActivity.id).alias('activities_count')) .select(HamsterCategory, fn.COUNT(HamsterActivity.id).alias('activities_count'))
@ -104,7 +103,6 @@ def tidy_categories():
.alias('subquery') .alias('subquery')
) )
# Use the subquery to filter categories where activities_count is 0
categories = ( categories = (
HamsterCategory HamsterCategory
.select() .select()
@ -601,17 +599,20 @@ def reset():
@db_.command() @db_.command()
@click.option('-g', '--global', 'global_', help='Does this file contain mappings to global activties', is_flag=True) @click.option('-g', '--global', 'global_', help='Does this file contain mappings to global activties', is_flag=True)
@click.option('--mapping-path', help='Mapping file') @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) mapping_file = _get_kimai_mapping_file(mapping_path, None)
next(mapping_file) next(mapping_file)
mapping_reader = csv.reader(mapping_file) mapping_reader = csv.reader(mapping_file)
for row in mapping_reader: for row in mapping_reader:
hamster_category = HamsterCategory.get(name=row[0]) 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_customer = KimaiCustomer.get(name=row[2])
kimai_project = KimaiProject.get(name=row[3], kimai_project = KimaiProject.get(name=row[3],
customer_id=kimai_customer.id) customer_id=kimai_customer.id)
try: try:
kimai_activity = KimaiActivity.get( kimai_activity = KimaiActivity.get(
name=row[4], name=row[4],

View File

@ -15,6 +15,7 @@ from .db import (
KimaiProject, KimaiProject,
KimaiCustomer, KimaiCustomer,
KimaiActivity, KimaiActivity,
HamsterKimaiMapping,
) )
from .kimai import ( from .kimai import (
KimaiAPI, KimaiAPI,
@ -78,15 +79,34 @@ class ActivitiesScreen(ListScreen):
def _refresh(self, filter_query=None): def _refresh(self, filter_query=None):
self.table.clear() 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 = ( activities = (
HamsterActivity.select( HamsterActivity.select(
HamsterActivity, HamsterActivity,
HamsterCategory, HamsterCategory.id,
fn.Count(HamsterFact.id).alias("facts_count") 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) .join(HamsterCategory, JOIN.LEFT_OUTER)
.switch(HamsterActivity) .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) .group_by(HamsterActivity)
) )
@ -100,10 +120,11 @@ class ActivitiesScreen(ListScreen):
[ [
[ [
activity.category_id, activity.category_id,
(activity.category.name if (activity.category_id != -1) else ""), activity.category_name,
activity.id, activity.id,
activity.name, activity.name,
activity.facts_count, activity.facts_count,
activity.mappings_count,
] ]
for activity in activities for activity in activities
] ]
@ -115,7 +136,7 @@ class ActivitiesScreen(ListScreen):
self.table = self.query_one(DataTable) self.table = self.query_one(DataTable)
self.table.cursor_type = "row" self.table.cursor_type = "row"
self.columns = self.table.add_columns( 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.sort = (self.columns[1], self.columns[3])
self._refresh() self._refresh()