show all event type counts in device msg logging

This commit is contained in:
Bruno Windels 2021-02-24 20:56:20 +01:00
parent b43754da6a
commit 636208a321
2 changed files with 28 additions and 5 deletions

View File

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import {OLM_ALGORITHM, MEGOLM_ALGORITHM} from "./e2ee/common.js"; import {OLM_ALGORITHM, MEGOLM_ALGORITHM} from "./e2ee/common.js";
import {countBy} from "../utils/groupBy.js";
// key to store in session store // key to store in session store
const PENDING_ENCRYPTED_EVENTS = "pendingEncryptedDeviceEvents"; const PENDING_ENCRYPTED_EVENTS = "pendingEncryptedDeviceEvents";
@ -36,10 +37,7 @@ export class DeviceMessageHandler {
*/ */
async writeSync(toDeviceEvents, txn, log) { async writeSync(toDeviceEvents, txn, log) {
const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted"); const encryptedEvents = toDeviceEvents.filter(e => e.type === "m.room.encrypted");
log.set("encryptedCount", encryptedEvents.length); log.set("eventsCount", countBy(toDeviceEvents, e => e.type));
const keyRequestCount = toDeviceEvents.reduce((sum, e) => sum + e.type === "m.room_key_request" ? 1 : 0, 0);
log.set("keyRequestCount", keyRequestCount);
log.set("otherCount", toDeviceEvents.length - encryptedEvents.length - keyRequestCount);
if (!encryptedEvents.length) { if (!encryptedEvents.length) {
return false; return false;
} }
@ -62,10 +60,12 @@ export class DeviceMessageHandler {
return r.event?.type === "m.room_key" && r.event.content?.algorithm === MEGOLM_ALGORITHM; return r.event?.type === "m.room_key" && r.event.content?.algorithm === MEGOLM_ALGORITHM;
}); });
let roomKeys; let roomKeys;
log.set("roomKeyCount", megOlmRoomKeysResults.length); log.set("eventsCount", countBy(olmResults, r => r.event.type));
log.set("roomKeys", megOlmRoomKeysResults.length);
if (megOlmRoomKeysResults.length) { if (megOlmRoomKeysResults.length) {
roomKeys = await this._megolmDecryption.addRoomKeys(megOlmRoomKeysResults, txn, log); roomKeys = await this._megolmDecryption.addRoomKeys(megOlmRoomKeysResults, txn, log);
} }
log.set("newRoomKeys", roomKeys.length);
return {roomKeys}; return {roomKeys};
} }

View File

@ -33,3 +33,26 @@ export function groupByWithCreator(array, groupFn, createCollectionFn, addCollec
return map; return map;
}, new Map()); }, new Map());
} }
export function countBy(events, mapper) {
return events.reduce((counts, event) => {
const mappedValue = mapper(event);
if (!counts[mappedValue]) {
counts[mappedValue] = 1;
} else {
counts[mappedValue] += 1;
}
return counts;
}, {});
}
export function tests() {
return {
countBy: assert => {
const counts = countBy([{type: "foo"}, {type: "bar"}, {type: "foo"}], o => o.type);
assert.equal(Object.keys(counts).length, 2);
assert.equal(counts.foo, 2);
assert.equal(counts.bar, 1);
}
}
}