Skip to content

Commit 0758a12

Browse files
committed
imp(): improved project resolver with dailyEvents
1 parent 81dcb5f commit 0758a12

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

src/models/eventsFactory.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ const mongo = require('../mongo');
77
const Event = require('../models/event');
88
const { ObjectID } = require('mongodb');
99

10-
/**
11-
* @typedef {Object} RecentEventSchema
12-
* @property {Event} event - event model
13-
* @property {Number} count - recent error occurred count
14-
* @property {String} data - error occurred date (string)
15-
*/
16-
1710
/**
1811
* @typedef {Object} EventRepetitionSchema
1912
* @property {String} _id — repetition's identifier
@@ -22,6 +15,13 @@ const { ObjectID } = require('mongodb');
2215
* @property {Number} timestamp - repetition's Unix timestamp
2316
*/
2417

18+
/**
19+
* @typedef {Object} DaylyEventsSchema
20+
* @property {Event} event - original event of the daily one
21+
* @property {EventRepetitionSchema} repetition - last repetition of the day
22+
* @property {String | null} nextCursor - pointer to the next dailyEvent for pagination
23+
*/
24+
2525
/**
2626
* @typedef {Object} EventsFilters
2727
* @property {boolean} [starred] - if true, events with 'starred' mark should be included to the output
@@ -148,16 +148,16 @@ class EventsFactory extends Factory {
148148
* Returns events that grouped by day
149149
*
150150
* @param {Number} limit - events count limitations
151-
* @param {Number} skip - certain number of documents to skip
151+
* @param {String} paginatoinCursor - pointer to the first daily event to be selected
152152
* @param {'BY_DATE' | 'BY_COUNT'} sort - events sort order
153153
* @param {EventsFilters} filters - marks by which events should be filtered
154154
* @param {String} search - Search query
155155
*
156156
* @return {RecentEventSchema[]}
157157
*/
158-
async findRecent(
158+
async findRecentDailyEventsWithEventAndRepetition(
159159
limit = 10,
160-
skip = 0,
160+
paginationCursor = '',
161161
sort = 'BY_DATE',
162162
filters = {},
163163
search = ''
@@ -194,6 +194,11 @@ class EventsFactory extends Factory {
194194

195195
const pipeline = [
196196
{
197+
$match: paginationCursor ? {
198+
_id: {
199+
$gte: new ObjectID(paginationCursor),
200+
}
201+
} : {},
197202
$sort: {
198203
groupingTimestamp: -1,
199204
[sort]: -1,
@@ -241,6 +246,9 @@ class EventsFactory extends Factory {
241246
: {};
242247

243248
pipeline.push(
249+
/**
250+
* Left outer join original event on groupHash field
251+
*/
244252
{
245253
$lookup: {
246254
from: 'events:' + this.projectId,
@@ -249,48 +257,49 @@ class EventsFactory extends Factory {
249257
as: 'event',
250258
},
251259
},
260+
{
261+
$lookup: {
262+
from: 'repetitions:' + this.projectId,
263+
localField: 'lastRepetitionId',
264+
foreignField: '_id',
265+
as: 'repetition',
266+
}
267+
},
268+
/**
269+
* Desctruct event and repetition arrays since there are only one document in both arrays
270+
*/
252271
{
253272
$unwind: '$event',
254273
},
274+
{
275+
$unwind: '$repetition',
276+
},
255277
{
256278
$match: {
257279
...matchFilter,
258280
...searchFilter,
259281
},
260282
},
261-
{ $skip: skip },
262-
{ $limit: limit },
283+
{ $limit: limit + 1 },
263284
{
264-
$group: {
265-
_id: null,
266-
dailyInfo: { $push: '$$ROOT' },
267-
events: { $push: '$event' },
268-
},
269-
},
270-
{
271-
$unset: 'dailyInfo.event',
285+
$unset: 'groupHash',
272286
}
273287
);
274288

275289
const cursor = this.getCollection(this.TYPES.DAILY_EVENTS).aggregate(pipeline);
276290

277-
const result = (await cursor.toArray()).shift();
291+
const result = await cursor.toArray();
278292

279-
/**
280-
* aggregation can return empty array so that
281-
* result can be undefined
282-
*
283-
* for that we check result existence
284-
*
285-
* extra field `projectId` needs to satisfy GraphQL query
286-
*/
287-
if (result && result.events) {
288-
result.events.forEach(event => {
289-
event.projectId = this.projectId;
290-
});
293+
let lastEvent;
294+
295+
if (result.length === limit + 1) {
296+
lastEvent = result.pop();
291297
}
292298

293-
return result;
299+
return {
300+
nextCursor: lastEvent ? lastEvent._id.toString() : null,
301+
...result,
302+
};
294303
}
295304

296305
/**

src/resolvers/project.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ module.exports = {
344344
*
345345
* @return {Promise<RecentEventSchema[]>}
346346
*/
347-
async recentEvents(project, { limit, skip, sort, filters, search }) {
347+
async dailyEventsPortion(project, { limit, skip, sort, filters, search }) {
348348
if (search) {
349349
if (search.length > MAX_SEARCH_QUERY_LENGTH) {
350350
search = search.slice(0, MAX_SEARCH_QUERY_LENGTH);
@@ -353,7 +353,22 @@ module.exports = {
353353

354354
const factory = new EventsFactory(project._id);
355355

356-
return factory.findRecent(limit, skip, sort, filters, search);
356+
// @todo - rename
357+
const res = factory.findRecentDailyEventsWithEventAndRepetition(limit, skip, sort, filters, search);
358+
359+
res.forEach((dailyEvent) => {
360+
const dailyEventLatestRepetition = dailyEvent.repetition;
361+
const dailyEventOriginalEvent = dailyEvent.event;
362+
363+
// wait for util implementation
364+
// const mergedRepetition = merge(dailyEventOriginalEvent, dailyEventLatestRepetition);
365+
delete dailyEvent.repetition;
366+
delete dailyEvent.event;
367+
368+
// dailyEvent.event = mergedRepetition;
369+
370+
return dailyEvent;
371+
})
357372
},
358373

359374
/**

0 commit comments

Comments
 (0)