Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aw_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ignore: F401

import logging

logger = logging.getLogger(__name__)
Expand All @@ -15,4 +17,6 @@
from . import log

from . import models
from .models import Event

from . import schema
45 changes: 45 additions & 0 deletions aw_core/schemas/bucket.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
16 changes: 16 additions & 0 deletions aw_core/schemas/export.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
19 changes: 11 additions & 8 deletions aw_datastore/storages/peewee.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down