Skip to content

Commit 8cb9d32

Browse files
authored
Merge pull request #69 from ActivityWatch/dev/syncprep
Prepare for sync MVP
2 parents c29d28a + 7ab63a3 commit 8cb9d32

4 files changed

Lines changed: 76 additions & 8 deletions

File tree

aw_core/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# ignore: F401
2+
13
import logging
24

35
logger = logging.getLogger(__name__)
@@ -15,4 +17,6 @@
1517
from . import log
1618

1719
from . import models
20+
from .models import Event
21+
1822
from . import schema

aw_core/schemas/bucket.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"$id": "https://activitywatch.net/schemas/bucket.v0.json",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"title": "Bucket",
5+
"description": "The Bucket model that is used in ActivityWatch",
6+
"type": "object",
7+
"required": ["id", "type", "client", "hostname"],
8+
"properties": {
9+
"id": {
10+
"description": "The unique id for the bucket",
11+
"type": "string"
12+
},
13+
"name": {
14+
"description": "The readable and renameable name for the bucket",
15+
"type": "string"
16+
},
17+
"type": {
18+
"description": "The event type",
19+
"type": "string"
20+
},
21+
"client": {
22+
"description": "The name of the client that is reporting to the bucket",
23+
"type": "string"
24+
},
25+
"hostname": {
26+
"description": "The hostname of the machine on which the client is running",
27+
"type": "string"
28+
},
29+
"created": {
30+
"description": "The creation datetime of the bucket",
31+
"type": "string",
32+
"format": "date-time"
33+
},
34+
"data": {
35+
"description": "",
36+
"type": "object"
37+
},
38+
"events": {
39+
"type": "array",
40+
"items": {
41+
"$ref": "#/definitions/Event"
42+
}
43+
}
44+
}
45+
}

aw_core/schemas/export.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$id": "https://activitywatch.net/schemas/export.v0.json",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"title": "Export",
5+
"description": "The Export model that is used by ActivityWatch",
6+
"type": "object",
7+
"required": [],
8+
"properties": {
9+
"buckets": {
10+
"type": "array",
11+
"items": {
12+
"$ref": "#/definitions/Bucket"
13+
}
14+
}
15+
}
16+
}

aw_datastore/storages/peewee.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, List
1+
from typing import Optional, List, Dict, Any
22
from datetime import datetime, timezone
33
import json
44
import os
@@ -94,8 +94,10 @@ def detect_db_version(data_dir: str, max_version: Optional[int] = None) -> Optio
9494
class PeeweeStorage(AbstractStorage):
9595
sid = "peewee"
9696

97-
def __init__(self, testing):
97+
def __init__(self, testing: bool = True, filepath: str = None) -> None:
9898
data_dir = get_data_dir("aw-server")
99+
100+
# TODO: Won't work with custom filepath
99101
current_db_version = detect_db_version(data_dir, max_version=LATEST_VERSION)
100102

101103
if current_db_version is not None and current_db_version < LATEST_VERSION:
@@ -104,26 +106,27 @@ def __init__(self, testing):
104106
logger.info("Creating database file for new version {}".format(LATEST_VERSION))
105107
logger.warning("ActivityWatch does not currently support database migrations, new database file will be empty")
106108

107-
filename = 'peewee-sqlite' + ('-testing' if testing else '') + ".v{}".format(LATEST_VERSION) + '.db'
108-
filepath = os.path.join(data_dir, filename)
109+
if not filepath:
110+
filename = 'peewee-sqlite' + ('-testing' if testing else '') + ".v{}".format(LATEST_VERSION) + '.db'
111+
filepath = os.path.join(data_dir, filename)
109112
self.db = _db
110113
self.db.init(filepath)
111114
logger.info("Using database file: {}".format(filepath))
112115

113116
# db.connect()
114117

115-
self.bucket_keys = {}
118+
self.bucket_keys = {} # type: Dict[str, int]
116119
if not BucketModel.table_exists():
117120
BucketModel.create_table()
118121
if not EventModel.table_exists():
119122
EventModel.create_table()
120123
self.update_bucket_keys()
121124

122-
def update_bucket_keys(self):
125+
def update_bucket_keys(self) -> None:
123126
buckets = BucketModel.select()
124127
self.bucket_keys = {bucket.id: bucket.key for bucket in buckets}
125128

126-
def buckets(self):
129+
def buckets(self) -> Dict[str, Dict[str, Any]]:
127130
buckets = {bucket.id: bucket.json() for bucket in BucketModel.select()}
128131
return buckets
129132

@@ -214,7 +217,7 @@ def get_events(self, bucket_id: str, limit: int,
214217
return [Event(**e) for e in list(map(EventModel.json, q.execute()))]
215218

216219
def get_eventcount(self, bucket_id: str,
217-
starttime: Optional[datetime] = None, endtime: Optional[datetime] = None):
220+
starttime: Optional[datetime] = None, endtime: Optional[datetime] = None):
218221
q = EventModel.select() \
219222
.where(EventModel.bucket == self.bucket_keys[bucket_id])
220223
if starttime:

0 commit comments

Comments
 (0)