Skip to content

Commit 0599da4

Browse files
committed
refactor: separate Event and Project chart data APIs
- Event.chartData: uses 'days' parameter, fetches data only from MongoDB - Project.chartData: uses 'startDate', 'endDate', 'groupBy' parameters, fetches from Redis with MongoDB fallback - Add getChartDataFromMongo method for Event-specific chart data - Fix redisHelper comment to reflect TS.INCRBY usage - Remove unused 'projectId' parameter from getChartDataFromMongo
1 parent 6147a7a commit 0599da4

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

src/models/eventsFactory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,17 @@ class EventsFactory extends Factory {
402402
};
403403
}
404404

405+
/**
406+
* Get chart data for projects (uses Redis with fallback to MongoDB)
407+
*
408+
* @param {string} startDate - start date (ISO string or Unix timestamp)
409+
* @param {string} endDate - end date (ISO string or Unix timestamp)
410+
* @param {number} groupBy - grouping interval in minutes
411+
* @param {number} timezoneOffset - user's local timezone offset in minutes
412+
* @param {string} projectId - project ID
413+
* @param {string} groupHash - event's group hash (empty for project-level data)
414+
* @returns {Promise<Array>}
415+
*/
405416
async getChartData(startDate, endDate, groupBy = 60, timezoneOffset = 0, projectId = '', groupHash = '') {
406417
try {
407418
const redisData = await this.redis.getChartDataFromRedis(
@@ -431,6 +442,18 @@ class EventsFactory extends Factory {
431442
}
432443
}
433444

445+
/**
446+
* Get chart data from MongoDB only (for events)
447+
*
448+
* @param {number} days - how many days to fetch
449+
* @param {number} timezoneOffset - user's local timezone offset in minutes
450+
* @param {string} groupHash - event's group hash
451+
* @returns {Promise<Array>}
452+
*/
453+
async getChartDataFromMongo(days, timezoneOffset = 0, groupHash = '') {
454+
return this.findChartData(days, timezoneOffset, groupHash);
455+
}
456+
434457
/**
435458
* Fetch timestamps and total count of errors (or target error) for each day since
436459
*

src/redisHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export default class RedisHelper {
101101

102102
let result: [string, string][] = [];
103103
try {
104-
// Use aggregation to sum events within each bucket
105-
// Since we now use TS.ADD (not TS.INCRBY), each sample is 1, so SUM gives us count
104+
// Use aggregation to sum values within each bucket
105+
// TS.INCRBY creates one point per time period with accumulated count
106106
result = (await this.redisClient.sendCommand([
107107
'TS.RANGE',
108108
key,

src/resolvers/event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ module.exports = {
8383
* @param {number} timezoneOffset - user's local timezone offset in minutes
8484
* @returns {Promise<ProjectChartItem[]>}
8585
*/
86-
async chartData({ projectId, groupHash }, { startDate, endDate, groupBy, timezoneOffset }, context) {
86+
async chartData({ projectId, groupHash }, { days, timezoneOffset }, context) {
8787
const factory = getEventsFactory(context, projectId);
8888

89-
return factory.getChartData(startDate, endDate, groupBy, timezoneOffset, projectId, groupHash);
89+
return factory.getChartDataFromMongo(days, timezoneOffset, groupHash);
9090
},
9191

9292
/**

src/typeDefs/event.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,9 @@ type Event {
282282
"""
283283
chartData(
284284
"""
285-
Start date (ISO string or Unix timestamp in seconds)
285+
How many days we need to fetch for displaying in a chart
286286
"""
287-
startDate: String!
288-
289-
"""
290-
End date (ISO string or Unix timestamp in seconds)
291-
"""
292-
endDate: String!
293-
294-
"""
295-
Grouping interval in minutes (1=minute, 60=hour, 1440=day)
296-
"""
297-
groupBy: Int! = 60
287+
days: Int! = 0
298288
299289
"""
300290
User's local timezone offset in minutes

0 commit comments

Comments
 (0)