[sleeplog] Change caching for global getStats

master
storm64 2022-05-26 11:27:36 +02:00
parent d04efb6fe2
commit aef6f638cb
4 changed files with 76 additions and 20 deletions

View File

@ -7,3 +7,5 @@
0.10: Complete rework off this app! 0.10: Complete rework off this app!
- beta01: Add interface.html to view your saved log data - beta01: Add interface.html to view your saved log data
- beta02: Add "View log" function for debugging log + send data for gadgetbridge - beta02: Add "View log" function for debugging log + send data for gadgetbridge
- beta03: Change caching for global getStats

View File

@ -12,6 +12,7 @@ It is using the built in movement calculation to decide your sleeping state. Whi
-+- -+- -+- -+-
```` ````
--- ---
### Introduction ### 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 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 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: ---
``` ### Access statistics
"data": [ ---
{"name": "sleeplog.log", "storageFile": true}, * Last Asleep Time [Date]:
{"wildcard": "sleeplog_????.log"}, `Date(sleeplog.awakeSince)`
{"wildcard": "sleeplog_??????.csv"} * 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 ### Worth Mentioning
@ -94,3 +126,15 @@ The app icon is downloaded from [https://icons8.com](https://icons8.com).
#### License #### License
[MIT License](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"}
],
````

View File

@ -222,9 +222,6 @@ if (sleeplog.conf.enabled) {
// cache consecutive status to check for changes later on // cache consecutive status to check for changes later on
data.consecutive = this.consecutive; data.consecutive = this.consecutive;
// set disabled move log status
var moveLogStatus = false;
// check if changing to deep sleep from non sleepling // check if changing to deep sleep from non sleepling
if (data.status === 4 && this.status <= 2) { if (data.status === 4 && this.status <= 2) {
// set asleepSince if undefined // set asleepSince if undefined
@ -247,8 +244,6 @@ if (sleeplog.conf.enabled) {
data.consecutive = 2; data.consecutive = 2;
// reset awakeSince // reset awakeSince
this.info.awakeSince = 0; this.info.awakeSince = 0;
// enabled move log status
moveLogStatus = true;
} else if (data.status <= 2 && this.info.awakeSince && } else if (data.status <= 2 && this.info.awakeSince &&
this.info.awakeSince + this.conf.maxAwake <= data.timestamp) { this.info.awakeSince + this.conf.maxAwake <= data.timestamp) {
// set non consecutive sleep // 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 // check if the status has changed
if (data.status !== this.status || data.consecutive !== this.consecutive) { if (data.status !== this.status || data.consecutive !== this.consecutive) {
// append status // append status
@ -282,8 +280,19 @@ if (sleeplog.conf.enabled) {
// call debugging function if set // call debugging function if set
if (this.debug) require("sleeplog").debug(data); if (this.debug) require("sleeplog").debug(data);
// call move log function if set // check if changed into known consecutive state
if (moveLogStatus) require("sleeplog").moveLog(); 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 // 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 // define function to access stats of the last night
getStats: function() { getStats: function() {
// check if stats cache is not defined or older than 24h // check if stats cache is not defined or older than 12h
if (this.statsCache === undefined || this.statsCache.calculatedAt + 864E5 < Date.now()) { // 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 // read stats of the last night into cache and remove module from cache
this.statsCache = require("sleeplog").getStats(); this.statsCache = require("sleeplog").getStats();
Modules.removeCached("sleeplog"); Modules.removeCached("sleeplog");

View File

@ -2,7 +2,7 @@
"id":"sleeplog", "id":"sleeplog",
"name":"Sleep Log", "name":"Sleep Log",
"shortName": "SleepLog", "shortName": "SleepLog",
"version": "0.10beta02", "version": "0.10beta03",
"description": "Log and view your sleeping habits. This app is using the built in movement calculation.", "description": "Log and view your sleeping habits. This app is using the built in movement calculation.",
"icon": "app.png", "icon": "app.png",
"type": "app", "type": "app",