Skip to content

Commit a33c08e

Browse files
committed
Limit unique user count for calculating keyboard statistics.
1 parent c168a22 commit a33c08e

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

functions/src/keyboards/create-keyboard-statistics-command.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type ICreateKeyboardStatisticsResult = {
1818
statistics?: IKeyboardStatistics;
1919
} & IResult;
2020

21+
const UNIQUE_USER_COUNT_THRESHOLD: number = 2;
22+
2123
export class CreateKeyboardStatisticsCommand extends AbstractCommand<ICreateKeyboardStatisticsResult> {
2224
@NeedAuthentication()
2325
@ValidateRequired(['keyboardDefinitionId'])
@@ -53,21 +55,29 @@ export class CreateKeyboardStatisticsCommand extends AbstractCommand<ICreateKeyb
5355
const flashingKeymapDateValueMap: { [key: string]: number } =
5456
CreateKeyboardStatisticsCommand.createDefaultDateValueMap();
5557

56-
querySnapshot.forEach((doc) => {
57-
const createdAt = doc.data().createdAt.toDate();
58-
const date = createdAt.toISOString().substring(0, 10);
59-
if (doc.data().operation === 'configure/open') {
60-
if (openingKeyboardDateValueMap[date] == undefined) {
61-
openingKeyboardDateValueMap[date] = 0;
62-
}
63-
openingKeyboardDateValueMap[date] += 1;
64-
} else if (doc.data().operation === 'configure/flash') {
65-
if (flashingKeymapDateValueMap[date] == undefined) {
66-
flashingKeymapDateValueMap[date] = 0;
58+
const uniqueUserIds: Set<string> = new Set();
59+
for (const doc of querySnapshot.docs) {
60+
uniqueUserIds.add(doc.data().uid);
61+
}
62+
63+
// If there is only one user, the statistics is not returned because of a privacy issue.
64+
if (UNIQUE_USER_COUNT_THRESHOLD <= uniqueUserIds.size) {
65+
for (const doc of querySnapshot.docs) {
66+
const createdAt = doc.data().createdAt.toDate();
67+
const date = createdAt.toISOString().substring(0, 10);
68+
if (doc.data().operation === 'configure/open') {
69+
if (openingKeyboardDateValueMap[date] == undefined) {
70+
openingKeyboardDateValueMap[date] = 0;
71+
}
72+
openingKeyboardDateValueMap[date] += 1;
73+
} else if (doc.data().operation === 'configure/flash') {
74+
if (flashingKeymapDateValueMap[date] == undefined) {
75+
flashingKeymapDateValueMap[date] = 0;
76+
}
77+
flashingKeymapDateValueMap[date] += 1;
6778
}
68-
flashingKeymapDateValueMap[date] += 1;
6979
}
70-
});
80+
}
7181

7282
const statistics: IKeyboardStatistics = {
7383
counts_of_opening_keyboard: {

0 commit comments

Comments
 (0)