From aef6f638cb15f8269edd45bafd732aa9db822b63 Mon Sep 17 00:00:00 2001 From: storm64 Date: Thu, 26 May 2022 11:27:36 +0200 Subject: [PATCH] [sleeplog] Change caching for global getStats --- apps/sleeplog/ChangeLog | 4 ++- apps/sleeplog/README.md | 62 +++++++++++++++++++++++++++++++------ apps/sleeplog/boot.js | 28 +++++++++++------ apps/sleeplog/metadata.json | 2 +- 4 files changed, 76 insertions(+), 20 deletions(-) diff --git a/apps/sleeplog/ChangeLog b/apps/sleeplog/ChangeLog index 1adc4a4dc..e0743315b 100644 --- a/apps/sleeplog/ChangeLog +++ b/apps/sleeplog/ChangeLog @@ -6,4 +6,6 @@ 0.06: Reduced log size further to 750 entries 0.10: Complete rework off this app! - beta01: Add interface.html to view your saved log data - - beta02: Add "View log" function for debugging log + send data for gadgetbridge \ No newline at end of file + - beta02: Add "View log" function for debugging log + send data for gadgetbridge + - beta03: Change caching for global getStats + \ No newline at end of file diff --git a/apps/sleeplog/README.md b/apps/sleeplog/README.md index 13f95070c..e7adc6a30 100644 --- a/apps/sleeplog/README.md +++ b/apps/sleeplog/README.md @@ -12,6 +12,7 @@ It is using the built in movement calculation to decide your sleeping state. Whi -+- -+- ```` + --- ### Introduction --- @@ -60,16 +61,47 @@ But here are some explanations how to use the app and settings: if you'd like to view your logged data in the IDE, you can access it with `require("sleeplog").printLog(since, until)` or `require("sleeplog").readLog(since, until)` to view the raw data since & until in Bangle timestamp, e.g. `require("sleeplog").printLog(Date()-24*60*60*1000, Date())` for the last 24h ---- -Temporarily removed logfiles from metadata.json to prevent removal on un-/reinstall: -``` -"data": [ - {"name": "sleeplog.log", "storageFile": true}, - {"wildcard": "sleeplog_????.log"}, - {"wildcard": "sleeplog_??????.csv"} -], -```` +--- +### Access statistics +--- +* Last Asleep Time [Date]: + `Date(sleeplog.awakeSince)` +* Last Awake Duration [ms]: + `Date() - sleeplog.awakeSince` +* Last Statistics [object]: + ``` + // get stats of the last night (period as displayed inside the app) + // as this might be the mostly used function the data is cached inside the global object + sleeplog.getStats(); + + // get stats of the last 24h + require("sleeplog").getStats(0, 24*60*60*1000); + // same as + require("sleeplog").getStats(Date.now(), 24*60*60*1000); + // output as object, timestamps as UNIX timestamp, durations in minutes + ={ calculatedAt: 1653123553810, deepSleep: 250, lightSleep: 150, awakeSleep: 10, + consecSleep: 320, awakeTime: 1030, notWornTime: 0, unknownTime: 0, logDuration: 1440, + firstDate: 1653036600000, lastDate: 1653111600000 } + + // to get the start of a period defined by "Break TOD" of any date + var startOfBreak = require("sleeplog").getLastBreak(); + // same as + var startOfBreak = require("sleeplog").getLastBreak(Date.now()); + // output as date + =Date: Sat May 21 2022 12:00:00 GMT+0200 + + // get stats of this period as displayed inside the app + require("sleeplog").getStats(require("sleeplog").getLastBreak(), 24*60*60*1000); + // or any other day + require("sleeplog").getStats(require("sleeplog").getLastBreak(Date(2022,4,10)), 24*60*60*1000); + ``` +* Total Statistics [object]: + ``` + // use with caution, may take a long time ! + require("sleeplog").getStats(0, 0, require("sleeplog").readLog()); + ``` + --- ### Worth Mentioning @@ -94,3 +126,15 @@ The app icon is downloaded from [https://icons8.com](https://icons8.com). #### License [MIT License](LICENSE) + +--- + + +Temporarily removed logfiles from metadata.json to prevent removal on un-/reinstall: +``` +"data": [ + {"name": "sleeplog.log", "storageFile": true}, + {"wildcard": "sleeplog_????.log"}, + {"wildcard": "sleeplog_??????.csv"} +], +```` \ No newline at end of file diff --git a/apps/sleeplog/boot.js b/apps/sleeplog/boot.js index 1b303f282..e2b7f45d9 100644 --- a/apps/sleeplog/boot.js +++ b/apps/sleeplog/boot.js @@ -222,9 +222,6 @@ if (sleeplog.conf.enabled) { // cache consecutive status to check for changes later on data.consecutive = this.consecutive; - // set disabled move log status - var moveLogStatus = false; - // check if changing to deep sleep from non sleepling if (data.status === 4 && this.status <= 2) { // set asleepSince if undefined @@ -247,8 +244,6 @@ if (sleeplog.conf.enabled) { data.consecutive = 2; // reset awakeSince this.info.awakeSince = 0; - // enabled move log status - moveLogStatus = true; } else if (data.status <= 2 && this.info.awakeSince && this.info.awakeSince + this.conf.maxAwake <= data.timestamp) { // set non consecutive sleep @@ -258,6 +253,9 @@ if (sleeplog.conf.enabled) { } } + // cache change into a known consecutive state + var changeIntoConsec = data.consecutive; + // check if the status has changed if (data.status !== this.status || data.consecutive !== this.consecutive) { // append status @@ -282,8 +280,19 @@ if (sleeplog.conf.enabled) { // call debugging function if set if (this.debug) require("sleeplog").debug(data); - // call move log function if set - if (moveLogStatus) require("sleeplog").moveLog(); + // check if changed into known consecutive state + if (changeIntoConsec) { + // check if change is to consecutive sleep or not + if (changeIntoConsec === 2) { + // call move log function + require("sleeplog").moveLog(); + } else { + // update stats cache if available + if (this.statsCache) this.statsCache = require("sleeplog").getStats(); + } + // remove module from cache if not on debugging + if (!this.debug) Modules.removeCached("sleeplog"); + } }, // define function to append the status to the StorageFile log @@ -300,8 +309,9 @@ if (sleeplog.conf.enabled) { // define function to access stats of the last night getStats: function() { - // check if stats cache is not defined or older than 24h - if (this.statsCache === undefined || this.statsCache.calculatedAt + 864E5 < Date.now()) { + // check if stats cache is not defined or older than 12h + // if stats cache is set it will be updated on every change to non consecutive sleep + if (this.statsCache === undefined || this.statsCache.calculatedAt + 432E5 < Date.now()) { // read stats of the last night into cache and remove module from cache this.statsCache = require("sleeplog").getStats(); Modules.removeCached("sleeplog"); diff --git a/apps/sleeplog/metadata.json b/apps/sleeplog/metadata.json index 8eb9c5e5c..157f8920c 100644 --- a/apps/sleeplog/metadata.json +++ b/apps/sleeplog/metadata.json @@ -2,7 +2,7 @@ "id":"sleeplog", "name":"Sleep Log", "shortName": "SleepLog", - "version": "0.10beta02", + "version": "0.10beta03", "description": "Log and view your sleeping habits. This app is using the built in movement calculation.", "icon": "app.png", "type": "app",