From 59964dcab329ae0de6d6224a889d9f41d1385a79 Mon Sep 17 00:00:00 2001 From: Mariano Ramon Date: Sun, 17 May 2020 10:14:29 -0300 Subject: [PATCH] added week calculations, refactor mocks --- dmapp/config.py | 6 ++++ dmapp/dmcore/__init__.py | 14 ++++++++ dmapp/dmdb/__init__.py | 6 ++++ dmapp/dmweb/dm.py | 10 +++++- dmapp/dmweb/dmcal.py | 71 ++++++++++++++++++++++++++++++++++++++++ dmmain.py | 7 ++-- 6 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 dmapp/config.py create mode 100644 dmapp/dmcore/__init__.py create mode 100644 dmapp/dmdb/__init__.py diff --git a/dmapp/config.py b/dmapp/config.py new file mode 100644 index 0000000..996d122 --- /dev/null +++ b/dmapp/config.py @@ -0,0 +1,6 @@ +DESKTOPS = ("Work", + "Browse", + "Write", + "Learn", + "Idle", + "Self") \ No newline at end of file diff --git a/dmapp/dmcore/__init__.py b/dmapp/dmcore/__init__.py new file mode 100644 index 0000000..826dded --- /dev/null +++ b/dmapp/dmcore/__init__.py @@ -0,0 +1,14 @@ +class ActiveDesktop: + + def __init__(self, desktops, unlabeled = "Other"): + self.desktops = desktops + + + def active_workspace(): + + workspaces = subprocess.check_output(["wmctrl", "-d"]) \ + .decode("utf-8").strip("\n").split("\n") + + for workspace in workspaces: + if workspace[3] == "*": + return int(workspace[0]) \ No newline at end of file diff --git a/dmapp/dmdb/__init__.py b/dmapp/dmdb/__init__.py new file mode 100644 index 0000000..d6b55fb --- /dev/null +++ b/dmapp/dmdb/__init__.py @@ -0,0 +1,6 @@ +from pymongo import MongoClient + +client = MongoClient() + +db = client.deskmeter +switches = db.switch \ No newline at end of file diff --git a/dmapp/dmweb/dm.py b/dmapp/dmweb/dm.py index d5413a9..8ed7af0 100644 --- a/dmapp/dmweb/dm.py +++ b/dmapp/dmweb/dm.py @@ -146,7 +146,7 @@ def get_period_totals(start, end): #TODO _1 remove #day += total rows.append( {"ws": ws, - "total" : datetime.timedelta(seconds=total)}) + "total" : convert_timedelta(datetime.timedelta(seconds=total)) }) #TODO _1 remove #rows.append({"ws":"sum","total": day}) @@ -172,3 +172,11 @@ def local_date(date, convert=False): if convert: return pytz.utc.localize(date, is_dst=None).astimezone(bsas) return date.replace(tzinfo=bsas) + + +def convert_timedelta(duration): + days, seconds = duration.days, duration.seconds + hours = days * 24 + seconds // 3600 + minutes = (seconds % 3600) // 60 + seconds = (seconds % 60) + return "{}:{:02d}:{:02d}".format(hours, minutes, seconds) diff --git a/dmapp/dmweb/dmcal.py b/dmapp/dmweb/dmcal.py index 5649296..0de9875 100644 --- a/dmapp/dmweb/dmcal.py +++ b/dmapp/dmweb/dmcal.py @@ -42,6 +42,77 @@ class DMHTMLCalendar(calendar.HTMLCalendar): return returnstr + def oneweek(self,month,week): + + start_day = None + end_day = None + + for (d, wd) in week: + if d == 0: + continue + else: + start_day = d + break + + + for (d, wd) in reversed(week): + if d == 0: + continue + else: + end_day = d + break + + + start = datetime.datetime(2020, month, start_day).replace(hour=0, + minute=0, + second=0) + + end = datetime.datetime(2020, month, end_day).replace (hour=23, + minute=59, + second=59) + + rows = get_period_totals( local_date(start), + local_date(end)) + + returnstr = "" + for row in rows: + returnstr += "".format(row["ws"],row["total"]) + + returnstr += "
{}{}
" + return returnstr + + + # def formatmonthname(self, theyear, themonth, withyear=True): + # """ + # Return a month name as a table row. + # """ + # if withyear: + # s = '%s %s' % (month_name[themonth], theyear) + # else: + # s = '%s' % month_name[themonth] + # return '%s' % ( + # self.cssclass_month_head, s) + + + def formatweekheader(self): + """ + Return a header for a week as a table row. + """ + s = ''.join(self.formatweekday(i) for i in self.iterweekdays()) + s += "Week Totals" + return '%s' % s + + + + def formatweek(self, theweek): + """ + Return a complete week as a table row. + """ + s = ''.join(self.formatday(d, wd) for (d, wd) in theweek) + s += "{}".format(self.oneweek(self.dmmonth, theweek)) + return '%s' % s + + def formatday(self, day, weekday): """ Return a day as a table cell. diff --git a/dmmain.py b/dmmain.py index fc0dcb2..954ec51 100644 --- a/dmmain.py +++ b/dmmain.py @@ -17,12 +17,13 @@ dailies = db.daily desktops = ("Work", "Browse", "Write", + "Create", "Learn", - "Idle", - "Self") + "Other", + "Sleep") -unlabeled = "Other" +unlabeled = "Idle" def active_workspace():