Skip to content
Draft
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
2 changes: 1 addition & 1 deletion aw_server/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
bundlepath = projectpath.parent # the ActivityWatch bundle repo, in some circumstances

# This line set by script when run (metaprogramming)
__version__ = "v0.11.0b1.dev+98fd120"
__version__ = "v0.11.0.dev+dbcd092"


def get_rev():
Expand Down
38 changes: 33 additions & 5 deletions aw_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import iso8601

from aw_core.models import Event
from aw_core.models import Event, Setting
from aw_core.log import get_log_file_path
from aw_core.dirs import get_data_dir

Expand Down Expand Up @@ -117,7 +117,8 @@ def import_bucket(self, bucket_data: Any):
)
self.create_events(
bucket_id,
[Event(**e) if isinstance(e, dict) else e for e in bucket_data["events"]],
[Event(**e) if isinstance(e, dict)
else e for e in bucket_data["events"]],
)

def import_all(self, buckets: Dict[str, Any]):
Expand Down Expand Up @@ -165,7 +166,8 @@ def get_events(
end: datetime = None,
) -> List[Event]:
"""Get events from a bucket"""
logger.debug("Received get request for events in bucket '{}'".format(bucket_id))
logger.debug(
"Received get request for events in bucket '{}'".format(bucket_id))
if limit is None: # Let limit = None also mean "no limit"
limit = -1
events = [
Expand All @@ -186,7 +188,8 @@ def get_eventcount(
) -> int:
"""Get eventcount from a bucket"""
logger.debug(
"Received get request for eventcount in bucket '{}'".format(bucket_id)
"Received get request for eventcount in bucket '{}'".format(
bucket_id)
)
return self.db[bucket_id].get_eventcount(start, end)

Expand Down Expand Up @@ -290,10 +293,35 @@ def query2(self, name, query, timeperiods, cache):
starttime = iso8601.parse_date(period[0])
endtime = iso8601.parse_date(period[1])
query = str().join(query)
result.append(query2.query(name, query, starttime, endtime, self.db))
result.append(query2.query(
name, query, starttime, endtime, self.db))
return result

# SETTINGS

def get_settings(self) -> List[Setting]:
"""Get all the settings"""
# TODO: auto populate data if this array is empty
# data = {
# 'durationDefault': '86400',
# 'initialTimestamp': 'Thu Sep 23 2021 15:22:08 GMT+0530',
# 'newReleaseCheckData': '{"isEnabled":true,"nextCheckTime":"2021-09-29T09:51:36.782Z","howOftenToCheck":86400,"timesChecked":0}',
# 'startOfDay': '04:00',
# 'syncSettingsToServer': 'true',
# 'userSatisfactionPollData': '{"isEnabled":true,"nextPollTime":"2021-09-30T09:52:08.703Z","timesPollIsShown":0}'
# }
data = self.db.get_settings()
return data

def update_setting(self, data) -> bool:
"""Update a single setting"""
data = self.db.update_setting(data["key"], data["value"])

logger.info("setting updated {0}".format(data))
return data

# TODO: Right now the log format on disk has to be JSON, this is hard to read by humans...

def get_log(self):
"""Get the server log in json format"""
payload = []
Expand Down
20 changes: 18 additions & 2 deletions aw_server/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ def get(self, bucket_id):
start = iso8601.parse_date(args["start"]) if "start" in args else None
end = iso8601.parse_date(args["end"]) if "end" in args else None

events = current_app.api.get_eventcount(bucket_id, start=start, end=end)
events = current_app.api.get_eventcount(
bucket_id, start=start, end=end)
return events, 200


Expand Down Expand Up @@ -257,7 +258,8 @@ def post(self, bucket_id):
if "pulsetime" in request.args:
pulsetime = float(request.args["pulsetime"])
else:
raise BadRequest("MissingParameter", "Missing required parameter pulsetime")
raise BadRequest("MissingParameter",
"Missing required parameter pulsetime")

event = current_app.api.heartbeat(bucket_id, heartbeat, pulsetime)
return event.to_json_dict(), 200
Expand Down Expand Up @@ -339,6 +341,20 @@ def post(self):
return None, 200


# SETTINGS


@api.route("/0/settings")
class SettingsResource(Resource):
def get(self):
data = current_app.api.get_settings()
return jsonify(data)

def post(self):
data = current_app.api.update_setting(request.json)
return data


# LOGGING


Expand Down