-
Notifications
You must be signed in to change notification settings - Fork 2
Add filtering by date range #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
8a7d63a
1bd29cd
cfc7e12
2672e6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ uploads | |
| globalConfig.json | ||
| coverage | ||
| tls | ||
| .history | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -23,9 +23,11 @@ const { ObjectID } = require('mongodb'); | |||
|
|
||||
| /** | ||||
| * @typedef {Object} EventsFilters | ||||
| * @property {boolean} [starred] - if true, events with 'starred' mark should be included to the output | ||||
| * @property {boolean} [resolved] - if true, events with 'resolved' should be included to the output | ||||
| * @property {boolean} [ignored] - if true, events with 'ignored' mark should be included to the output | ||||
| * @property {boolean} [starred] | ||||
| * @property {boolean} [resolved] | ||||
| * @property {boolean} [ignored] | ||||
| * @property {string|number} [dateFrom] | ||||
| * @property {string|number} [dateTo] | ||||
| */ | ||||
|
|
||||
| /** | ||||
|
|
@@ -149,8 +151,8 @@ class EventsFactory extends Factory { | |||
| * @param {Number} limit - events count limitations | ||||
| * @param {Number} skip - certain number of documents to skip | ||||
| * @param {'BY_DATE' | 'BY_COUNT'} sort - events sort order | ||||
| * @param {EventsFilters} filters - marks by which events should be filtered | ||||
| * @param {String} search - Search query | ||||
| * @param {EventsFilters} filters - filter object | ||||
| * @param {String} search - search query | ||||
| * | ||||
| * @return {RecentEventSchema[]} | ||||
| */ | ||||
|
|
@@ -165,9 +167,6 @@ class EventsFactory extends Factory { | |||
| throw new Error('Search parameter must be a string'); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Check if pattern is safe RegExp | ||||
| */ | ||||
|
Comment on lines
-225
to
-227
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unexpected jsdoc removal |
||||
| if (!safe(search)) { | ||||
| throw new Error('Invalid regular expression pattern'); | ||||
| } | ||||
|
|
@@ -231,13 +230,41 @@ class EventsFactory extends Factory { | |||
| } | ||||
| : {}; | ||||
|
|
||||
| const matchFilter = filters | ||||
| ? Object.fromEntries( | ||||
| Object | ||||
| .entries(filters) | ||||
| .map(([mark, exists]) => [`event.marks.${mark}`, { $exists: exists } ]) | ||||
| ) | ||||
| : {}; | ||||
| const matchFilter = { | ||||
| ...searchFilter, | ||||
| }; | ||||
|
|
||||
| // Filter by marks (event.marks.{key}) | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| ['starred', 'resolved', 'ignored'].forEach((mark) => { | ||||
| if (typeof filters[mark] === 'boolean') { | ||||
| matchFilter[`event.marks.${mark}`] = { $exists: filters[mark] }; | ||||
| } | ||||
| }); | ||||
|
|
||||
| // Filter by date (groupingTimestamp) | ||||
| if (filters.dateFrom || filters.dateTo) { | ||||
| matchFilter.groupingTimestamp = {}; | ||||
|
|
||||
| if (filters.dateFrom) { | ||||
| const from = typeof filters.dateFrom === 'string' | ||||
| ? Math.floor(new Date(filters.dateFrom).getTime() / 1000) | ||||
| : filters.dateFrom; | ||||
|
|
||||
| matchFilter.groupingTimestamp.$gte = from; | ||||
| } | ||||
|
|
||||
| if (filters.dateTo) { | ||||
| const to = typeof filters.dateTo === 'string' | ||||
| ? Math.floor(new Date(filters.dateTo).getTime() / 1000) | ||||
| : filters.dateTo; | ||||
|
|
||||
| matchFilter.groupingTimestamp.$lte = to; | ||||
| } | ||||
|
|
||||
| if (Object.keys(matchFilter.groupingTimestamp).length === 0) { | ||||
| delete matchFilter.groupingTimestamp; | ||||
| } | ||||
| } | ||||
|
|
||||
| pipeline.push( | ||||
| { | ||||
|
|
@@ -252,10 +279,7 @@ class EventsFactory extends Factory { | |||
| $unwind: '$event', | ||||
| }, | ||||
| { | ||||
| $match: { | ||||
| ...matchFilter, | ||||
| ...searchFilter, | ||||
| }, | ||||
| $match: matchFilter, | ||||
| }, | ||||
| { $skip: skip }, | ||||
| { $limit: limit }, | ||||
|
|
@@ -272,17 +296,8 @@ class EventsFactory extends Factory { | |||
| ); | ||||
|
|
||||
| const cursor = this.getCollection(this.TYPES.DAILY_EVENTS).aggregate(pipeline); | ||||
|
|
||||
| const result = (await cursor.toArray()).shift(); | ||||
|
|
||||
| /** | ||||
| * aggregation can return empty array so that | ||||
| * result can be undefined | ||||
| * | ||||
| * for that we check result existence | ||||
| * | ||||
| * extra field `projectId` needs to satisfy GraphQL query | ||||
| */ | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leave jsdoc please |
||||
| if (result && result.events) { | ||||
| result.events.forEach(event => { | ||||
| event.projectId = this.projectId; | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant jsdoc removal