Skip to content

Commit 2a2dcb9

Browse files
committed
save history
1 parent 07cd954 commit 2a2dcb9

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

extension.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ var VitalsMenuButton = GObject.registerClass({
6666

6767
this._sensors = new Sensors.Sensors(this._settings, this._sensorIcons);
6868
this._values = new Values.Values(this._settings, this._sensorIcons);
69+
this._historyCachePath = GLib.get_user_cache_dir() + '/vitals/history.json';
70+
this._values.loadTimeSeries(this._historyCachePath);
6971
this._menuLayout = new St.BoxLayout({
7072
vertical: false,
7173
clip_to_allocation: true,
@@ -564,7 +566,7 @@ var VitalsMenuButton = GObject.registerClass({
564566

565567
_updateTimeSettingChanged() {
566568
this._destroyTimer();
567-
this._values.clearTimeSeries();
569+
this._values.clearTimeSeries(this._historyCachePath);
568570
this._initializeTimer();
569571
}
570572

@@ -835,6 +837,7 @@ var VitalsMenuButton = GObject.registerClass({
835837
destroy() {
836838
this._hideHistoryPopout();
837839
this._destroyTimer();
840+
this._values.saveTimeSeries(this._historyCachePath);
838841
this._sensors.destroy();
839842

840843
for (let signal of Object.values(this._settingChangedSignals))

values.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

27+
import GLib from 'gi://GLib';
28+
import Gio from 'gi://Gio';
2729
import GObject from 'gi://GObject';
2830

2931
const cbFun = (d, c) => {
@@ -79,9 +81,69 @@ export const Values = GObject.registerClass({
7981
while (buf.length > maxPoints) buf.shift();
8082
}
8183

82-
clearTimeSeries() {
84+
clearTimeSeries(cachePath) {
8385
this._timeSeries = {};
8486
this._timeSeriesFormat = {};
87+
if (cachePath) {
88+
try {
89+
const file = Gio.File.new_for_path(cachePath);
90+
if (file.query_exists(null))
91+
file.delete(null);
92+
} catch (e) {
93+
// ignore
94+
}
95+
}
96+
}
97+
98+
saveTimeSeries(path) {
99+
try {
100+
const obj = {
101+
version: 1,
102+
timeSeries: this._timeSeries,
103+
timeSeriesFormat: this._timeSeriesFormat
104+
};
105+
const json = JSON.stringify(obj);
106+
const dir = GLib.path_get_dirname(path);
107+
GLib.mkdir_with_parents(dir, 0o755);
108+
GLib.file_set_contents(path, json);
109+
} catch (e) {
110+
// ignore write failures
111+
}
112+
}
113+
114+
loadTimeSeries(path) {
115+
try {
116+
const file = Gio.File.new_for_path(path);
117+
if (!file.query_exists(null)) return;
118+
const [ok, contents] = GLib.file_get_contents(path);
119+
if (!ok) return;
120+
const decoder = new TextDecoder('utf-8');
121+
const json = decoder.decode(contents);
122+
const obj = JSON.parse(json);
123+
if (!obj || obj.version !== 1) return;
124+
if (obj.timeSeries && typeof obj.timeSeries === 'object')
125+
this._timeSeries = obj.timeSeries;
126+
if (obj.timeSeriesFormat && typeof obj.timeSeriesFormat === 'object')
127+
this._timeSeriesFormat = obj.timeSeriesFormat;
128+
const now = Date.now() / 1000;
129+
const maxAge = this._getHistoryDurationSeconds();
130+
const cutoff = now - maxAge;
131+
for (const key in this._timeSeries) {
132+
const buf = this._timeSeries[key];
133+
if (!Array.isArray(buf)) {
134+
delete this._timeSeries[key];
135+
continue;
136+
}
137+
while (buf.length > 0 && buf[0].t < cutoff)
138+
buf.shift();
139+
if (buf.length === 0) {
140+
delete this._timeSeries[key];
141+
delete this._timeSeriesFormat[key];
142+
}
143+
}
144+
} catch (e) {
145+
// ignore corrupt or missing file
146+
}
85147
}
86148

87149
getTimeSeries(key) {

0 commit comments

Comments
 (0)