bumblebee-status/bumblebee_status/modules/contrib/usage.py

79 lines
2.2 KiB
Python
Raw Normal View History

2023-07-10 15:42:11 +02:00
# pylint: disable=C0111,R0903
"""
Module for ActivityWatch (https://activitywatch.net/)
2023-07-10 16:52:22 +02:00
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)
2023-07-10 15:42:11 +02:00
"""
2023-07-10 15:51:00 +02:00
2023-07-10 15:42:11 +02:00
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))
2023-07-10 15:51:00 +02:00
self.__usage = ""
2023-07-10 15:42:11 +02:00
def output(self, _):
2023-07-10 15:51:00 +02:00
return "{}".format(self.__usage)
2023-07-10 15:42:11 +02:00
def update(self):
2023-07-10 15:51:00 +02:00
database_loc = self.parameter(
"database", "~/.local/share/activitywatch/aw-server/peewee-sqlite.v2.db"
)
home = os.path.expanduser("~")
2023-07-10 15:42:11 +02:00
2023-07-10 15:51:00 +02:00
database = sqlite3.connect(database_loc.replace("~", home))
2023-07-10 15:42:11 +02:00
cursor = database.cursor()
2023-07-10 15:51:00 +02:00
cursor.execute("SELECT key, id FROM bucketmodel")
2023-07-10 15:42:11 +02:00
bucket_id = 1
for tuple in cursor.fetchall():
2023-07-10 15:51:00 +02:00
if "aw-watcher-afk" in tuple[1]:
2023-07-10 15:42:11 +02:00
bucket_id = tuple[0]
2023-07-10 15:51:00 +02:00
cursor.execute(
f"SELECT duration, datastr FROM eventmodel WHERE bucket_id = {bucket_id} "
+ 'AND strftime("%Y,%m,%d", timestamp) = strftime("%Y,%m,%d", "now")'
)
2023-07-10 15:42:11 +02:00
duration = 0
for tuple in cursor.fetchall():
if '{"status": "not-afk"}' in tuple[1]:
duration += tuple[0]
2023-07-10 15:51:00 +02:00
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)
)
2023-07-10 15:42:11 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4