Skip to content

Commit

Permalink
fix(#7616): telemetry logs not recording every day (#8555)
Browse files Browse the repository at this point in the history
- Prevents increasing the syncedSeq when sync fails.
- Refactors telemetry to use a telemetry db per day to collect records, then it aggregates the records into a doc in the user's meta db and deletes previous days of telemetry db.
- Adds temporary code to delete old telemetry db and old localStorage properties.
  • Loading branch information
latin-panda authored Oct 23, 2023
1 parent 5555510 commit 0b907e1
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 291 deletions.
27 changes: 15 additions & 12 deletions tests/e2e/default/telemetry/telemetry.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ const setupUser = () => {
};
};

const setOldTelemetryDate = async (user, date) => {
const telemetryDateStorageKey = `medic-${user.username}-telemetry-date`;
await browser.execute((telemetryDateStorageKey, yesterday) => {
// eslint-disable-next-line no-undef
window.localStorage.setItem(telemetryDateStorageKey, yesterday);
}, telemetryDateStorageKey, date.valueOf());
};

describe('Telemetry', () => {
const DATE_FORMAT = 'YYYY-MM-DD';
const TELEMETRY_PREFIX = 'telemetry';
let user;
let docs;

Expand All @@ -49,16 +43,25 @@ describe('Telemetry', () => {
await utils.saveDocs(docs);
await utils.createUsers([user]);
await loginPage.login(user);
await commonPage.waitForPageLoaded();
});

it('should record telemetry', async () => {
const yesterday = moment().subtract(1, 'day');
const yesterdayDBName = `${TELEMETRY_PREFIX}-${yesterday.format(DATE_FORMAT)}-${user.username}`;
const telemetryRecord = {
key: 'a-telemetry-record',
value: 3,
date_recorded: yesterday.toDate(),
};
await browser.execute(async (dbName, record) => {
// eslint-disable-next-line no-undef
await window.PouchDB(dbName).post(record);
}, yesterdayDBName, telemetryRecord);

// Some user activities to generate telemetry records
await commonPage.goToReports();
await commonPage.goToPeople();
await setOldTelemetryDate(user, yesterday);

// generate telemetry aggregate
await commonPage.goToReports();
await commonPage.sync();

Expand All @@ -67,7 +70,7 @@ describe('Telemetry', () => {
const options = { auth: { username: user.username, password: user.password }, userName: user.username };
const metaDocs = await utils.requestOnTestMetaDb({ ...options, path: '/_all_docs?include_docs=true' });

const telemetryEntry = metaDocs.rows.find(row => row.id.startsWith('telemetry'));
const telemetryEntry = metaDocs.rows.find(row => row.id.startsWith(TELEMETRY_PREFIX));
expect(telemetryEntry.doc).to.deep.nested.include({
'metadata.year': yesterday.year(),
'metadata.month': yesterday.month() + 1,
Expand Down
6 changes: 2 additions & 4 deletions webapp/src/ts/services/db-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,11 @@ export class DBSyncService {
]))
.then(([ push, pull ]) => {
telemetryEntry.recordSuccess({ push, pull });
this.ngZone.runOutsideAngular(() => purger.writeMetaPurgeLog(local, { syncedSeq: currentSeq }));
})
.catch(err => {
telemetryEntry.recordFailure(err, this.knownOnlineState);
})
.then(() => this.ngZone.runOutsideAngular(() => {
purger.writeMetaPurgeLog(local, { syncedSeq: currentSeq });
}));
});
}

private sendUpdate(syncState: SyncState) {
Expand Down
Loading

0 comments on commit 0b907e1

Please sign in to comment.