From 521772d301413627aebe27ad75aedbbe2b1e83d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Tue, 28 Aug 2018 21:21:52 +0200 Subject: [PATCH 1/2] added support for specifying filename for PeeweeStorage --- aw_datastore/storages/peewee.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/aw_datastore/storages/peewee.py b/aw_datastore/storages/peewee.py index 15523dd..2951e7c 100644 --- a/aw_datastore/storages/peewee.py +++ b/aw_datastore/storages/peewee.py @@ -1,4 +1,4 @@ -from typing import Optional, List +from typing import Optional, List, Dict, Any from datetime import datetime, timezone import json import os @@ -94,8 +94,10 @@ def detect_db_version(data_dir: str, max_version: Optional[int] = None) -> Optio class PeeweeStorage(AbstractStorage): sid = "peewee" - def __init__(self, testing): + def __init__(self, testing: bool = True, filepath: str = None) -> None: data_dir = get_data_dir("aw-server") + + # TODO: Won't work with custom filepath current_db_version = detect_db_version(data_dir, max_version=LATEST_VERSION) if current_db_version is not None and current_db_version < LATEST_VERSION: @@ -104,26 +106,27 @@ def __init__(self, testing): logger.info("Creating database file for new version {}".format(LATEST_VERSION)) logger.warning("ActivityWatch does not currently support database migrations, new database file will be empty") - filename = 'peewee-sqlite' + ('-testing' if testing else '') + ".v{}".format(LATEST_VERSION) + '.db' - filepath = os.path.join(data_dir, filename) + if not filepath: + filename = 'peewee-sqlite' + ('-testing' if testing else '') + ".v{}".format(LATEST_VERSION) + '.db' + filepath = os.path.join(data_dir, filename) self.db = _db self.db.init(filepath) logger.info("Using database file: {}".format(filepath)) # db.connect() - self.bucket_keys = {} + self.bucket_keys = {} # type: Dict[str, int] if not BucketModel.table_exists(): BucketModel.create_table() if not EventModel.table_exists(): EventModel.create_table() self.update_bucket_keys() - def update_bucket_keys(self): + def update_bucket_keys(self) -> None: buckets = BucketModel.select() self.bucket_keys = {bucket.id: bucket.key for bucket in buckets} - def buckets(self): + def buckets(self) -> Dict[str, Dict[str, Any]]: buckets = {bucket.id: bucket.json() for bucket in BucketModel.select()} return buckets @@ -214,7 +217,7 @@ def get_events(self, bucket_id: str, limit: int, return [Event(**e) for e in list(map(EventModel.json, q.execute()))] def get_eventcount(self, bucket_id: str, - starttime: Optional[datetime] = None, endtime: Optional[datetime] = None): + starttime: Optional[datetime] = None, endtime: Optional[datetime] = None): q = EventModel.select() \ .where(EventModel.bucket == self.bucket_keys[bucket_id]) if starttime: From 7ab63a3f5db2525c2f37160324b466f736331ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 29 Aug 2018 16:20:32 +0200 Subject: [PATCH 2/2] added bucket and export schemas --- aw_core/__init__.py | 4 ++++ aw_core/schemas/bucket.json | 45 +++++++++++++++++++++++++++++++++++++ aw_core/schemas/export.json | 16 +++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 aw_core/schemas/bucket.json create mode 100644 aw_core/schemas/export.json diff --git a/aw_core/__init__.py b/aw_core/__init__.py index dce5ff5..ace11d2 100644 --- a/aw_core/__init__.py +++ b/aw_core/__init__.py @@ -1,3 +1,5 @@ +# ignore: F401 + import logging logger = logging.getLogger(__name__) @@ -15,4 +17,6 @@ from . import log from . import models +from .models import Event + from . import schema diff --git a/aw_core/schemas/bucket.json b/aw_core/schemas/bucket.json new file mode 100644 index 0000000..b46e2c8 --- /dev/null +++ b/aw_core/schemas/bucket.json @@ -0,0 +1,45 @@ +{ + "$id": "https://activitywatch.net/schemas/bucket.v0.json", + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Bucket", + "description": "The Bucket model that is used in ActivityWatch", + "type": "object", + "required": ["id", "type", "client", "hostname"], + "properties": { + "id": { + "description": "The unique id for the bucket", + "type": "string" + }, + "name": { + "description": "The readable and renameable name for the bucket", + "type": "string" + }, + "type": { + "description": "The event type", + "type": "string" + }, + "client": { + "description": "The name of the client that is reporting to the bucket", + "type": "string" + }, + "hostname": { + "description": "The hostname of the machine on which the client is running", + "type": "string" + }, + "created": { + "description": "The creation datetime of the bucket", + "type": "string", + "format": "date-time" + }, + "data": { + "description": "", + "type": "object" + }, + "events": { + "type": "array", + "items": { + "$ref": "#/definitions/Event" + } + } + } +} diff --git a/aw_core/schemas/export.json b/aw_core/schemas/export.json new file mode 100644 index 0000000..4073934 --- /dev/null +++ b/aw_core/schemas/export.json @@ -0,0 +1,16 @@ +{ + "$id": "https://activitywatch.net/schemas/export.v0.json", + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Export", + "description": "The Export model that is used by ActivityWatch", + "type": "object", + "required": [], + "properties": { + "buckets": { + "type": "array", + "items": { + "$ref": "#/definitions/Bucket" + } + } + } +}