From 474494f9637e54f47a36a0b45c25b5214d8b53a4 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 21 Dec 2020 14:06:03 -0600 Subject: [PATCH] add systemd service --- .gitignore | 1 + Makefile | 7 +++++++ goalctl | 21 +++------------------ grapher | 5 +++++ server.py => localwaka | 24 +++++++++++++----------- localwaka.service | 10 ++++++++++ utils.py | 22 ++++++++++++++++++++++ 7 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 Makefile create mode 100755 grapher rename server.py => localwaka (90%) mode change 100644 => 100755 create mode 100644 localwaka.service diff --git a/.gitignore b/.gitignore index 1b1f638..9a051d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ *.pyc /data.db +.mypy_cache diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7d84815 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +PREFIX = /usr + +install: + install -D localwaka $(PREFIX)/bin/localwaka + +uninstall: + rm -f $(PREFIX)/bin/localwaka diff --git a/goalctl b/goalctl index 6d3cd82..c20d7c8 100755 --- a/goalctl +++ b/goalctl @@ -11,7 +11,7 @@ import sys import json import sqlite3 from config import timeout -from utils import window +import utils conn = sqlite3.connect("data.db") @@ -54,23 +54,9 @@ if goal_type == "overall": today = date.today() start_time = datetime.combine(today, time(hour=0, minute=0, second=0)) end_time = datetime.now() - print(f"from {start_time} to {end_time}") - start_stamp = datetime.timestamp(start_time) - end_stamp = datetime.timestamp(end_time) - # get all the entries - c = conn.cursor() - curs = c.execute(""" - select time from heartbeats where time > ? and time < ? order by time; - """, [start_stamp, end_stamp]) - - coded_secs = 0 - for (first, second) in window(curs, 2): - print(datetime.fromtimestamp(first[0])) - diff = second[0] - first[0] - if diff < timeout: - coded_secs += diff - print(coded_secs) + coded_secs = utils.get_seconds_coded(conn, start_time, end_time, timeout) + print("minutes coded:", (coded_secs/60)) elif data["interval"] == "week": # start of the week @@ -79,4 +65,3 @@ if goal_type == "overall": print(last_monday) # vim: set ft=python: - diff --git a/grapher b/grapher new file mode 100755 index 0000000..106676e --- /dev/null +++ b/grapher @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +import matplotlib + +# vim: set ft=python: diff --git a/server.py b/localwaka old mode 100644 new mode 100755 similarity index 90% rename from server.py rename to localwaka index b8aea0d..36d4e35 --- a/server.py +++ b/localwaka @@ -1,15 +1,16 @@ -from http.server import BaseHTTPRequestHandler +#!/usr/bin/env python3 + import socketserver import sqlite3 -from collections import namedtuple import json - +import configparser +from http.server import BaseHTTPRequestHandler +from collections import namedtuple PORT = 5800 conn = sqlite3.connect("data.db") c = conn.cursor() -# c.execute("""drop table if exists "heartbeats" """) c.execute(""" create table if not exists "heartbeats" ( id int primary key, @@ -38,7 +39,7 @@ class Handler(BaseHTTPRequestHandler): def do_POST(self): if self.path == "/api/v1/heartbeats.bulk": content_len = int(self.headers.get("Content-Length")) - # todo: cap this or potential vuln? + # TODO: cap this or potential vuln? body = self.rfile.read(content_len) def object_hook(d): @@ -87,10 +88,11 @@ class Server(socketserver.TCPServer): super().__init__(("", PORT), Handler) -server = Server() -try: - server.serve_forever() -except KeyboardInterrupt: - print("shutting down") - server.shutdown() +if __name__ == "__main__": + server = Server() + try: + server.serve_forever() + except KeyboardInterrupt: + print("shutting down") + server.shutdown() conn.close() diff --git a/localwaka.service b/localwaka.service new file mode 100644 index 0000000..80f513f --- /dev/null +++ b/localwaka.service @@ -0,0 +1,10 @@ +[Unit] +Description=Local WakaTime Server + +[Service] +ExecStart=/usr/bin/python /home/michael/Projects/localwaka/server.py +Restart=always +WorkingDirectory=/home/michael/Projects/localwaka + +[Install] +WantedBy=default.target diff --git a/utils.py b/utils.py index bdba4ce..67841df 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,7 @@ +from datetime import datetime from itertools import islice +import utils + def window(seq, n=2): "Returns a sliding window (of width n) over data from the iterable" @@ -10,3 +13,22 @@ def window(seq, n=2): for elem in it: result = result[1:] + (elem,) yield result + + +def get_seconds_coded(conn, start_time, end_time, timeout): + start_stamp = datetime.timestamp(start_time) + end_stamp = datetime.timestamp(end_time) + + # get all the entries + c = conn.cursor() + curs = c.execute(""" + select time from heartbeats where time > ? and time < ? order by time; + """, [start_stamp, end_stamp]) + + coded_secs = 0 + for (first, second) in utils.window(curs, 2): + diff = second[0] - first[0] + if diff < timeout: + coded_secs += diff + + return coded_secs