add systemd service
This commit is contained in:
parent
2ba0ad501e
commit
474494f963
7 changed files with 61 additions and 29 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
__pycache__
|
__pycache__
|
||||||
*.pyc
|
*.pyc
|
||||||
/data.db
|
/data.db
|
||||||
|
.mypy_cache
|
||||||
|
|
7
Makefile
Normal file
7
Makefile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
PREFIX = /usr
|
||||||
|
|
||||||
|
install:
|
||||||
|
install -D localwaka $(PREFIX)/bin/localwaka
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm -f $(PREFIX)/bin/localwaka
|
21
goalctl
21
goalctl
|
@ -11,7 +11,7 @@ import sys
|
||||||
import json
|
import json
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from config import timeout
|
from config import timeout
|
||||||
from utils import window
|
import utils
|
||||||
|
|
||||||
conn = sqlite3.connect("data.db")
|
conn = sqlite3.connect("data.db")
|
||||||
|
|
||||||
|
@ -54,23 +54,9 @@ if goal_type == "overall":
|
||||||
today = date.today()
|
today = date.today()
|
||||||
start_time = datetime.combine(today, time(hour=0, minute=0, second=0))
|
start_time = datetime.combine(today, time(hour=0, minute=0, second=0))
|
||||||
end_time = datetime.now()
|
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
|
coded_secs = utils.get_seconds_coded(conn, start_time, end_time, timeout)
|
||||||
c = conn.cursor()
|
print("minutes coded:", (coded_secs/60))
|
||||||
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)
|
|
||||||
|
|
||||||
elif data["interval"] == "week":
|
elif data["interval"] == "week":
|
||||||
# start of the week
|
# start of the week
|
||||||
|
@ -79,4 +65,3 @@ if goal_type == "overall":
|
||||||
print(last_monday)
|
print(last_monday)
|
||||||
|
|
||||||
# vim: set ft=python:
|
# vim: set ft=python:
|
||||||
|
|
||||||
|
|
5
grapher
Executable file
5
grapher
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import matplotlib
|
||||||
|
|
||||||
|
# vim: set ft=python:
|
24
server.py → localwaka
Normal file → Executable file
24
server.py → localwaka
Normal file → Executable file
|
@ -1,15 +1,16 @@
|
||||||
from http.server import BaseHTTPRequestHandler
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import socketserver
|
import socketserver
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from collections import namedtuple
|
|
||||||
import json
|
import json
|
||||||
|
import configparser
|
||||||
|
from http.server import BaseHTTPRequestHandler
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
PORT = 5800
|
PORT = 5800
|
||||||
conn = sqlite3.connect("data.db")
|
conn = sqlite3.connect("data.db")
|
||||||
|
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
# c.execute("""drop table if exists "heartbeats" """)
|
|
||||||
c.execute("""
|
c.execute("""
|
||||||
create table if not exists "heartbeats" (
|
create table if not exists "heartbeats" (
|
||||||
id int primary key,
|
id int primary key,
|
||||||
|
@ -38,7 +39,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
if self.path == "/api/v1/heartbeats.bulk":
|
if self.path == "/api/v1/heartbeats.bulk":
|
||||||
content_len = int(self.headers.get("Content-Length"))
|
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)
|
body = self.rfile.read(content_len)
|
||||||
|
|
||||||
def object_hook(d):
|
def object_hook(d):
|
||||||
|
@ -87,10 +88,11 @@ class Server(socketserver.TCPServer):
|
||||||
super().__init__(("", PORT), Handler)
|
super().__init__(("", PORT), Handler)
|
||||||
|
|
||||||
|
|
||||||
server = Server()
|
if __name__ == "__main__":
|
||||||
try:
|
server = Server()
|
||||||
server.serve_forever()
|
try:
|
||||||
except KeyboardInterrupt:
|
server.serve_forever()
|
||||||
print("shutting down")
|
except KeyboardInterrupt:
|
||||||
server.shutdown()
|
print("shutting down")
|
||||||
|
server.shutdown()
|
||||||
conn.close()
|
conn.close()
|
10
localwaka.service
Normal file
10
localwaka.service
Normal 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
|
22
utils.py
22
utils.py
|
@ -1,4 +1,7 @@
|
||||||
|
from datetime import datetime
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
|
import utils
|
||||||
|
|
||||||
|
|
||||||
def window(seq, n=2):
|
def window(seq, n=2):
|
||||||
"Returns a sliding window (of width n) over data from the iterable"
|
"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:
|
for elem in it:
|
||||||
result = result[1:] + (elem,)
|
result = result[1:] + (elem,)
|
||||||
yield result
|
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
|
||||||
|
|
Loading…
Reference in a new issue