2021-02-12 01:37:18 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-03-01 19:31:43 +05:30
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2021-02-12 01:37:18 +05:30
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-02-12 17:34:05 +05:30
|
|
|
import {
|
|
|
|
openDatabase,
|
|
|
|
txnAsPromise,
|
|
|
|
reqAsPromise,
|
|
|
|
iterateCursor,
|
|
|
|
fetchResults,
|
2021-09-18 03:21:20 +05:30
|
|
|
} from "../matrix/storage/idb/utils";
|
2021-02-12 17:34:05 +05:30
|
|
|
import {BaseLogger} from "./BaseLogger.js";
|
|
|
|
|
|
|
|
export class IDBLogger extends BaseLogger {
|
2021-02-12 20:38:07 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2021-02-16 22:15:47 +05:30
|
|
|
const {name, flushInterval = 60 * 1000, limit = 3000} = options;
|
2021-02-12 01:37:18 +05:30
|
|
|
this._name = name;
|
|
|
|
this._limit = limit;
|
|
|
|
this._queuedItems = this._loadQueuedItems();
|
2021-02-12 17:34:05 +05:30
|
|
|
// TODO: also listen for unload just in case sync keeps on running after pagehide is fired?
|
2021-02-12 01:37:18 +05:30
|
|
|
window.addEventListener("pagehide", this, false);
|
2021-02-12 22:35:39 +05:30
|
|
|
this._flushInterval = this._platform.clock.createInterval(() => this._tryFlush(), flushInterval);
|
2021-02-12 01:37:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
window.removeEventListener("pagehide", this, false);
|
|
|
|
this._flushInterval.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(evt) {
|
|
|
|
if (evt.type === "pagehide") {
|
|
|
|
this._finishAllAndFlush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _tryFlush() {
|
|
|
|
const db = await this._openDB();
|
|
|
|
try {
|
2021-02-12 22:35:39 +05:30
|
|
|
const txn = db.transaction(["logs"], "readwrite");
|
2021-02-12 01:37:18 +05:30
|
|
|
const logs = txn.objectStore("logs");
|
|
|
|
const amount = this._queuedItems.length;
|
|
|
|
for(const i of this._queuedItems) {
|
|
|
|
logs.add(i);
|
|
|
|
}
|
|
|
|
const itemCount = await reqAsPromise(logs.count());
|
|
|
|
if (itemCount > this._limit) {
|
2021-02-16 22:15:19 +05:30
|
|
|
// delete an extra 10% so we don't need to delete every time we flush
|
|
|
|
let deleteAmount = (itemCount - this._limit) + Math.round(0.1 * this._limit);
|
2021-02-12 01:37:18 +05:30
|
|
|
await iterateCursor(logs.openCursor(), (_, __, cursor) => {
|
|
|
|
cursor.delete();
|
2021-02-16 22:15:19 +05:30
|
|
|
deleteAmount -= 1;
|
|
|
|
return {done: deleteAmount === 0};
|
2021-02-12 01:37:18 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
await txnAsPromise(txn);
|
|
|
|
this._queuedItems.splice(0, amount);
|
2021-02-16 22:15:19 +05:30
|
|
|
} catch (err) {
|
2021-02-16 23:06:41 +05:30
|
|
|
console.error("Could not flush logs", err);
|
2021-02-12 01:37:18 +05:30
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
db.close();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_finishAllAndFlush() {
|
2021-02-12 18:35:51 +05:30
|
|
|
this._finishOpenItems();
|
2021-02-19 00:16:40 +05:30
|
|
|
this.log({l: "pagehide, closing logs", t: "navigation"});
|
2021-02-12 01:37:18 +05:30
|
|
|
this._persistQueuedItems(this._queuedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadQueuedItems() {
|
|
|
|
const key = `${this._name}_queuedItems`;
|
2021-02-12 17:34:05 +05:30
|
|
|
try {
|
|
|
|
const json = window.localStorage.getItem(key);
|
|
|
|
if (json) {
|
|
|
|
window.localStorage.removeItem(key);
|
|
|
|
return JSON.parse(json);
|
|
|
|
}
|
2021-02-17 00:27:45 +05:30
|
|
|
} catch (err) {
|
|
|
|
console.error("Could not load queued log items", err);
|
|
|
|
}
|
2021-02-12 01:37:18 +05:30
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_openDB() {
|
2021-02-22 15:16:40 +05:30
|
|
|
return openDatabase(this._name, db => db.createObjectStore("logs", {keyPath: "id", autoIncrement: true}), 1);
|
2021-02-12 01:37:18 +05:30
|
|
|
}
|
|
|
|
|
2021-03-01 19:31:43 +05:30
|
|
|
_persistItem(logItem, filter, forced) {
|
|
|
|
const serializedItem = logItem.serialize(filter, forced);
|
2021-02-12 01:37:18 +05:30
|
|
|
this._queuedItems.push({
|
2021-02-16 19:38:08 +05:30
|
|
|
json: JSON.stringify(serializedItem)
|
2021-02-12 01:37:18 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_persistQueuedItems(items) {
|
2021-02-12 17:34:05 +05:30
|
|
|
try {
|
|
|
|
window.localStorage.setItem(`${this._name}_queuedItems`, JSON.stringify(items));
|
|
|
|
} catch (e) {
|
2021-02-16 23:06:41 +05:30
|
|
|
console.error("Could not persist queued log items in localStorage, they will likely be lost", e);
|
2021-02-12 17:34:05 +05:30
|
|
|
}
|
2021-02-12 01:37:18 +05:30
|
|
|
}
|
|
|
|
|
2021-02-12 17:34:05 +05:30
|
|
|
async export() {
|
2021-02-12 22:35:39 +05:30
|
|
|
const db = await this._openDB();
|
2021-02-12 01:37:18 +05:30
|
|
|
try {
|
2021-02-12 22:35:39 +05:30
|
|
|
const txn = db.transaction(["logs"], "readonly");
|
2021-02-12 01:37:18 +05:30
|
|
|
const logs = txn.objectStore("logs");
|
2021-02-16 16:07:48 +05:30
|
|
|
const storedItems = await fetchResults(logs.openCursor(), () => false);
|
|
|
|
const allItems = storedItems.concat(this._queuedItems);
|
2021-02-22 15:16:40 +05:30
|
|
|
return new IDBLogExport(allItems, this, this._platform);
|
2021-02-12 01:37:18 +05:30
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
db.close();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 17:34:05 +05:30
|
|
|
|
|
|
|
async _removeItems(items) {
|
2021-02-12 22:35:39 +05:30
|
|
|
const db = await this._openDB();
|
2021-02-12 17:34:05 +05:30
|
|
|
try {
|
2021-02-12 22:35:39 +05:30
|
|
|
const txn = db.transaction(["logs"], "readwrite");
|
2021-02-12 17:34:05 +05:30
|
|
|
const logs = txn.objectStore("logs");
|
|
|
|
for (const item of items) {
|
|
|
|
const queuedIdx = this._queuedItems.findIndex(i => i.id === item.id);
|
|
|
|
if (queuedIdx === -1) {
|
|
|
|
logs.delete(item.id);
|
|
|
|
} else {
|
|
|
|
this._queuedItems.splice(queuedIdx, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await txnAsPromise(txn);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
db.close();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class IDBLogExport {
|
|
|
|
constructor(items, logger, platform) {
|
|
|
|
this._items = items;
|
|
|
|
this._logger = logger;
|
|
|
|
this._platform = platform;
|
|
|
|
}
|
2021-02-17 00:30:08 +05:30
|
|
|
|
2021-02-12 20:38:28 +05:30
|
|
|
get count() {
|
|
|
|
return this._items.length;
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:34:05 +05:30
|
|
|
/**
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
removeFromStore() {
|
|
|
|
return this._logger._removeItems(this._items);
|
|
|
|
}
|
|
|
|
|
|
|
|
asBlob() {
|
|
|
|
const log = {
|
2021-02-17 21:44:24 +05:30
|
|
|
formatVersion: 1,
|
|
|
|
appVersion: this._platform.updateService?.version,
|
2021-02-16 19:38:08 +05:30
|
|
|
items: this._items.map(i => JSON.parse(i.json))
|
2021-02-12 17:34:05 +05:30
|
|
|
};
|
|
|
|
const json = JSON.stringify(log);
|
2021-02-12 20:38:07 +05:30
|
|
|
const buffer = this._platform.encoding.utf8.encode(json);
|
2021-02-12 17:34:05 +05:30
|
|
|
const blob = this._platform.createBlob(buffer, "application/json");
|
|
|
|
return blob;
|
|
|
|
}
|
2021-02-12 01:37:18 +05:30
|
|
|
}
|