API route for returning tracked analytics keys (#10019)

* feat: track metrics saved by NodeBB (and assoc. plugins), #9949

* feat: route to retrieve analytics keys, closes #9949
This commit is contained in:
Julian Lam
2021-11-19 15:12:13 -05:00
committed by GitHub
parent 449366ca83
commit 5b42b6b369
5 changed files with 51 additions and 8 deletions

View File

@@ -53,6 +53,8 @@ Analytics.increment = function (keys, callback) {
}
};
Analytics.getKeys = async () => db.getSortedSetRange('analyticsKeys', 0, -1);
Analytics.pageView = async function (payload) {
pageViews += 1;
@@ -90,6 +92,17 @@ Analytics.writeData = async function () {
const month = new Date();
const dbQueue = [];
// Build list of metrics that were updated
let metrics = [
'pageviews',
'pageviews:month',
];
metrics.forEach((metric) => {
const toAdd = ['registered', 'guest', 'bot'].map(type => `${metric}:${type}`);
metrics = [...metrics, ...toAdd];
});
metrics.push('uniquevisitors');
today.setHours(today.getHours(), 0, 0, 0);
month.setMonth(month.getMonth(), 1);
month.setHours(0, 0, 0, 0);
@@ -130,8 +143,13 @@ Analytics.writeData = async function () {
for (const [key, value] of Object.entries(counters)) {
dbQueue.push(db.sortedSetIncrBy(`analytics:${key}`, value, today.getTime()));
metrics.push(key);
delete counters[key];
}
// Update list of tracked metrics
dbQueue.push(db.sortedSetAdd('analyticsKeys', metrics.map(() => +Date.now()), metrics));
try {
await Promise.all(dbQueue);
} catch (err) {