add systemd service

This commit is contained in:
Michael Zhang 2020-12-21 14:06:03 -06:00
parent 2ba0ad501e
commit 474494f963
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
7 changed files with 61 additions and 29 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
__pycache__
*.pyc
/data.db
.mypy_cache

7
Makefile Normal file
View file

@ -0,0 +1,7 @@
PREFIX = /usr
install:
install -D localwaka $(PREFIX)/bin/localwaka
uninstall:
rm -f $(PREFIX)/bin/localwaka

21
goalctl
View file

@ -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:

5
grapher Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env python3
import matplotlib
# vim: set ft=python:

24
server.py → localwaka Normal file → Executable file
View file

@ -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()

10
localwaka.service Normal file
View file

@ -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

View file

@ -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