Skip to content

Commit 5949788

Browse files
committed
feat(affected-users): added chart data
1 parent c253875 commit 5949788

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

src/models/eventsFactory.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MAX_DB_READ_BATCH_SIZE = Number(process.env.MAX_DB_READ_BATCH_SIZE);
1818
const ChartType = {
1919
Accepted: 'accepted',
2020
RateLimited: 'rate-limited',
21+
AffectedUsers: 'affected-users',
2122
};
2223

2324
/**
@@ -523,15 +524,35 @@ class EventsFactory extends Factory {
523524
];
524525
}
525526

527+
/**
528+
* Get event daily chart data (affected users) from MongoDB only
529+
*
530+
* @param {string} groupHash - event's group hash
531+
* @param {number} days - how many days to fetch
532+
* @param {number} timezoneOffset - user's local timezone offset in minutes
533+
* @returns {Promise<Array>}
534+
*/
535+
async getEventDailyAffectedUsersChart(groupHash, days, timezoneOffset = 0) {
536+
const data = await this.findChartData(days, timezoneOffset, groupHash, 'affectedUsers');
537+
538+
return [
539+
{
540+
label: ChartType.AffectedUsers,
541+
data,
542+
},
543+
];
544+
}
545+
526546
/**
527547
* Fetch timestamps and total count of errors (or target error) for each day since
528548
*
529549
* @param {number} days - how many days we need to fetch for displaying in a chart
530550
* @param {number} timezoneOffset - user's local timezone offset in minutes
531551
* @param {string} groupHash - event's group hash for showing only target event
552+
* @param {'count'|'affectedUsers'} valueField - field to aggregate
532553
* @return {ProjectChartItem[]}
533554
*/
534-
async findChartData(days, timezoneOffset = 0, groupHash = '') {
555+
async findChartData(days, timezoneOffset = 0, groupHash = '', valueField = 'count') {
535556
const today = new Date();
536557
const since = today.setDate(today.getDate() - days) / 1000;
537558

@@ -554,13 +575,15 @@ class EventsFactory extends Factory {
554575
};
555576
}
556577

578+
const projection = {
579+
lastRepetitionTime: 1,
580+
groupingTimestamp: 1,
581+
[valueField]: 1,
582+
};
583+
557584
const dailyEventsCursor = await this.getCollection(this.TYPES.DAILY_EVENTS)
558585
.find(options, {
559-
projection: {
560-
lastRepetitionTime: 1,
561-
groupingTimestamp: 1,
562-
count: 1,
563-
},
586+
projection,
564587
})
565588
.batchSize(MAX_DB_READ_BATCH_SIZE);
566589

@@ -576,11 +599,13 @@ class EventsFactory extends Factory {
576599
const key = `groupingTimestamp:${groupingTimestamp}`;
577600
const current = groupedCounts[key] || 0;
578601

579-
if (item.count === undefined || item.count === null) {
580-
console.warn(`Missing 'count' field for daily event with key ${key}. Defaulting to 0.`);
602+
const value = item[valueField];
603+
604+
if (value === undefined || value === null) {
605+
console.warn(`Missing '${valueField}' field for daily event with key ${key}. Defaulting to 0.`);
581606
groupedCounts[key] = current;
582607
} else {
583-
groupedCounts[key] = current + item.count;
608+
groupedCounts[key] = current + value;
584609
}
585610
}
586611

src/resolvers/event.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ module.exports = {
9090
return factory.getEventDailyChart(groupHash, days, timezoneOffset);
9191
},
9292

93+
/**
94+
* Return chart data for affected users of target event occured in last few days
95+
*
96+
* @param {string} projectId - event's project
97+
* @param {string} groupHash - event's groupHash
98+
* @param {number} days - how many days we need to fetch for displaying in a charts
99+
* @param {number} timezoneOffset - user's local timezone offset in minutes
100+
* @returns {Promise<ProjectChartItem[]>}
101+
*/
102+
async affectedUsersChartData({ projectId, groupHash }, { days, timezoneOffset }, context) {
103+
const factory = getEventsFactory(context, projectId);
104+
105+
return factory.getEventDailyAffectedUsersChart(groupHash, days, timezoneOffset);
106+
},
107+
93108
/**
94109
* Return AI suggestion for the event
95110
*

src/sso/saml/controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export default class SamlController {
154154
res.redirect(redirectUrl.toString());
155155
} catch (error) {
156156
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
157+
157158
this.log(
158159
'error',
159160
'SSO initiation error for workspace:',
@@ -271,6 +272,7 @@ export default class SamlController {
271272

272273
if (!isValidRequest) {
273274
const requestIdShort = samlData.inResponseTo.slice(0, 8);
275+
274276
this.log(
275277
'error',
276278
'[ACS] InResponseTo validation failed for workspace:',
@@ -285,6 +287,7 @@ export default class SamlController {
285287
}
286288
} catch (error) {
287289
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
290+
288291
this.log(
289292
'error',
290293
'[ACS] SAML validation error for workspace:',
@@ -387,6 +390,7 @@ export default class SamlController {
387390
*/
388391
if (error instanceof Error && error.message.includes('SAML')) {
389392
const errorMessage = error.message;
393+
390394
this.log(
391395
'error',
392396
'[ACS] SAML processing error for workspace:',
@@ -400,6 +404,7 @@ export default class SamlController {
400404
}
401405

402406
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
407+
403408
this.log(
404409
'error',
405410
'[ACS] ACS callback error for workspace:',

src/typeDefs/event.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ type Event {
296296
"""
297297
timezoneOffset: Int! = 0
298298
): [ChartLine!]!
299+
300+
"""
301+
Return graph of affected users dynamics for the specified period
302+
"""
303+
affectedUsersChartData(
304+
"""
305+
How many days we need to fetch for displaying in a chart
306+
"""
307+
days: Int! = 0
308+
309+
"""
310+
User's local timezone offset in minutes
311+
"""
312+
timezoneOffset: Int! = 0
313+
): [ChartLine!]!
299314
}
300315
301316
"""

0 commit comments

Comments
 (0)