aboutsummaryrefslogtreecommitdiff
path: root/src_py/hatter/backend.py
diff options
context:
space:
mode:
authorbozo.kopic <bozo.kopic@gmail.com>2017-08-17 15:17:40 +0200
committerbozo.kopic <bozo.kopic@gmail.com>2017-08-17 15:17:40 +0200
commit1bc393cdc26bbaa9b2927cb4614784d185a7bb27 (patch)
treeea9ab04949012c46eb9d926d9764dd98022a3592 /src_py/hatter/backend.py
parent074b604076049fb809c22ec0afaf18f5b55cc3ca (diff)
backend
Diffstat (limited to 'src_py/hatter/backend.py')
-rw-r--r--src_py/hatter/backend.py83
1 files changed, 82 insertions, 1 deletions
diff --git a/src_py/hatter/backend.py b/src_py/hatter/backend.py
index afb61a4..155e888 100644
--- a/src_py/hatter/backend.py
+++ b/src_py/hatter/backend.py
@@ -1,3 +1,20 @@
+import sqlite3
+import datetime
+import threading
+import logging
+
+from hatter import util
+
+
+util.monkeypatch_sqlite3()
+
+
+LogEntry = util.namedtuple(
+ 'LogEntry',
+ ['timestamp', 'datetime.datetime: timestamp'],
+ ['repository', 'str: repository'],
+ ['commit', 'str: commit'],
+ ['msg', 'str: message'])
class Backend:
@@ -8,5 +25,69 @@ class Backend:
async def async_close(self):
pass
- def enqueue(self, url, commit):
+ def add_job(self, url, commit):
pass
+
+
+class LogHandler(logging.Handler):
+
+ def __init__(self, db, repository, commit):
+ super().__init__()
+ self._db = db
+ self._repository
+ self._commit = commit
+
+ def emit(self, record):
+ self._db.add(
+ timestamp=datetime.datetime.fromtimestamp(
+ record.created, datetime.timezone.utc),
+ repository=self._repository,
+ commit=self._commit,
+ msg=record.getMessage())
+
+
+class DB:
+
+ def __init__(self, db_path):
+ db_path.parent.mkdir(exist_ok=True)
+ self._db = sqlite3.connect('file:{}?nolock=1'.format(db_path),
+ uri=True,
+ isolation_level=None,
+ detect_types=sqlite3.PARSE_DECLTYPES)
+ self._db.executescript("CREATE TABLE IF NOT EXISTS log ("
+ "timestamp TIMESTAMP, "
+ "repository TEXT, "
+ "commit TEXT, "
+ "msg TEXT)")
+ self._db.commit()
+ self._lock = threading.Lock()
+
+ def close(self):
+ with self._lock:
+ self._db.close()
+
+ def add(self, timestamp, repository, commit, msg):
+ with self._lock:
+ self._db.execute(
+ "INSERT INTO log VALUES "
+ "(:timestamp, :repository, :commit, :msg)",
+ {'timestamp': timestamp,
+ 'repository': repository,
+ 'commit': commit,
+ 'msg': msg})
+
+ def query(self, offset, limit):
+ with self._lock:
+ c = self._db.execute(
+ "SELECT rowid, * FROM log ORDER BY rowid DESC "
+ "LIMIT :limit OFFSET :offset",
+ {'limit': limit, 'offset': offset})
+ try:
+ result = c.fetchall()
+ except Exception as e:
+ result = []
+ return [LogEntry(timestamp=i[1],
+ repository=i[2],
+ commit=i[3],
+ msg=i[4])
+ for i in result]