[sleeplog] Change caching for global getStats
parent
d04efb6fe2
commit
aef6f638cb
|
|
@ -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
|
||||
- beta02: Add "View log" function for debugging log + send data for gadgetbridge
|
||||
- beta03: Change caching for global getStats
|
||||
|
||||
|
|
@ -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"}
|
||||
],
|
||||
````
|
||||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in New Issue