From b866ab25b673665d44719e053ca0e6b298604282 Mon Sep 17 00:00:00 2001 From: Lasnik Date: Mon, 10 Jul 2023 15:42:11 +0200 Subject: [PATCH 1/3] create module --- bumblebee_status/modules/contrib/usage.py | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 bumblebee_status/modules/contrib/usage.py diff --git a/bumblebee_status/modules/contrib/usage.py b/bumblebee_status/modules/contrib/usage.py new file mode 100644 index 0000000..5f21198 --- /dev/null +++ b/bumblebee_status/modules/contrib/usage.py @@ -0,0 +1,53 @@ +# pylint: disable=C0111,R0903 + +""" +Module for ActivityWatch (https://activitywatch.net/) +Displays the amount of time the system was used actively +""" +import sqlite3 +import os + +import core.module +import core.widget + + +class Module(core.module.Module): + def __init__(self, config, theme): + super().__init__(config, theme, core.widget.Widget(self.output)) + self.__uptime = "" + + def output(self, _): + return "{}".format(self.__uptime) + + def update(self): + database_loc = self.parameter('database', '~/.local/share/activitywatch/aw-server/peewee-sqlite.v2.db') + home = os.path.expanduser('~') + + database = sqlite3.connect(database_loc.replace('~', home)) + cursor = database.cursor() + + cursor.execute('SELECT key, id FROM bucketmodel') + + bucket_id = 1 + + for tuple in cursor.fetchall(): + if 'aw-watcher-afk' in tuple[1]: + bucket_id = tuple[0] + + cursor.execute(f'SELECT duration, datastr FROM eventmodel WHERE bucket_id = {bucket_id} ' + + 'AND strftime(\'%Y,%m,%d\', timestamp) = strftime(\'%Y,%m,%d\', \'now\')') + + duration = 0 + + for tuple in cursor.fetchall(): + if '{"status": "not-afk"}' in tuple[1]: + duration += tuple[0] + + hours = '%.0f' % (duration // 3600) + minutes = '%.0f' % ((duration % 3600) // 60) + seconds = '%.0f' % (duration % 60) + + formatting = self.parameter('format', 'HHh, MMmin') + self.__uptime = formatting.replace('HH', hours).replace('MM', minutes).replace('SS', seconds) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 305f9cf491583cf4d9359880d71e3383ba10d4ac Mon Sep 17 00:00:00 2001 From: Lasnik Date: Mon, 10 Jul 2023 15:51:00 +0200 Subject: [PATCH 2/3] formatting guidelines --- bumblebee_status/modules/contrib/usage.py | 44 ++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/bumblebee_status/modules/contrib/usage.py b/bumblebee_status/modules/contrib/usage.py index 5f21198..b05ace2 100644 --- a/bumblebee_status/modules/contrib/usage.py +++ b/bumblebee_status/modules/contrib/usage.py @@ -4,6 +4,7 @@ Module for ActivityWatch (https://activitywatch.net/) Displays the amount of time the system was used actively """ + import sqlite3 import os @@ -14,40 +15,49 @@ import core.widget class Module(core.module.Module): def __init__(self, config, theme): super().__init__(config, theme, core.widget.Widget(self.output)) - self.__uptime = "" + self.__usage = "" def output(self, _): - return "{}".format(self.__uptime) + return "{}".format(self.__usage) def update(self): - database_loc = self.parameter('database', '~/.local/share/activitywatch/aw-server/peewee-sqlite.v2.db') - home = os.path.expanduser('~') + database_loc = self.parameter( + "database", "~/.local/share/activitywatch/aw-server/peewee-sqlite.v2.db" + ) + home = os.path.expanduser("~") - database = sqlite3.connect(database_loc.replace('~', home)) + database = sqlite3.connect(database_loc.replace("~", home)) cursor = database.cursor() - - cursor.execute('SELECT key, id FROM bucketmodel') + + cursor.execute("SELECT key, id FROM bucketmodel") bucket_id = 1 for tuple in cursor.fetchall(): - if 'aw-watcher-afk' in tuple[1]: + if "aw-watcher-afk" in tuple[1]: bucket_id = tuple[0] - cursor.execute(f'SELECT duration, datastr FROM eventmodel WHERE bucket_id = {bucket_id} ' + - 'AND strftime(\'%Y,%m,%d\', timestamp) = strftime(\'%Y,%m,%d\', \'now\')') - + cursor.execute( + f"SELECT duration, datastr FROM eventmodel WHERE bucket_id = {bucket_id} " + + 'AND strftime("%Y,%m,%d", timestamp) = strftime("%Y,%m,%d", "now")' + ) + duration = 0 for tuple in cursor.fetchall(): if '{"status": "not-afk"}' in tuple[1]: duration += tuple[0] - hours = '%.0f' % (duration // 3600) - minutes = '%.0f' % ((duration % 3600) // 60) - seconds = '%.0f' % (duration % 60) - - formatting = self.parameter('format', 'HHh, MMmin') - self.__uptime = formatting.replace('HH', hours).replace('MM', minutes).replace('SS', seconds) + hours = "%.0f" % (duration // 3600) + minutes = "%.0f" % ((duration % 3600) // 60) + seconds = "%.0f" % (duration % 60) + + formatting = self.parameter("format", "HHh, MMmin") + self.__usage = ( + formatting.replace("HH", hours) + .replace("MM", minutes) + .replace("SS", seconds) + ) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 8178919e2c53ccf2652dae3d546e9c5a010cb77f Mon Sep 17 00:00:00 2001 From: Lasnik Date: Mon, 10 Jul 2023 16:52:22 +0200 Subject: [PATCH 3/3] add description --- bumblebee_status/modules/contrib/usage.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bumblebee_status/modules/contrib/usage.py b/bumblebee_status/modules/contrib/usage.py index b05ace2..d64e3e1 100644 --- a/bumblebee_status/modules/contrib/usage.py +++ b/bumblebee_status/modules/contrib/usage.py @@ -2,7 +2,22 @@ """ Module for ActivityWatch (https://activitywatch.net/) -Displays the amount of time the system was used actively +Displays the amount of time the system was used actively. + +Requirements: + * sqlite3 module for python + * ActivityWatch + +Errors: + * when you get 'error: unable to open database file', modify the parameter 'database' to your ActivityWatch database file + -> often found by running 'locate aw-server/peewee-sqlite.v2.db' + +Parameters: + * usage.database: path to your database file + * usage.format: Specify what gets printed to the bar + -> use 'HH', 'MM' or 'SS', they will get replaced by the number of hours, minutes and seconds, respectively + +contributed by lasnikr (https://github.com/lasnikr) """ import sqlite3