Rafal Pawlow 3 years ago
parent
commit
b209f01668
2 changed files with 19 additions and 15 deletions
  1. 9 10
      src/aggregates/views.ts
  2. 10 5
      src/resolvers/viewsInfo.ts

+ 9 - 10
src/aggregates/views.ts

@@ -47,20 +47,19 @@ export class ViewsAggregate {
   }
 
   public applyEvent(event: UnsequencedVideoEvent) {
-    const { videoId, channelId, categoryId, timestamp } = event
-    const currentVideoViews = this.videoViewsMap[event.videoId] || 0
-    const currentChannelViews = this.channelViewsMap[event.channelId] || 0
-    const currentCategoryViews =
-      event.categoryId && this.categoryViewsMap[event.categoryId] ? this.categoryViewsMap[event.categoryId] : 0
-    switch (event.type) {
+    const { videoId, channelId, categoryId, timestamp, type } = event
+    const currentVideoViews = this.videoViewsMap[videoId] || 0
+    const currentChannelViews = this.channelViewsMap[channelId] || 0
+    const currentCategoryViews = categoryId ? this.categoryViewsMap[categoryId] || 0 : 0
+    switch (type) {
       case VideoEventType.AddView:
-        this.videoViewsMap[event.videoId] = currentVideoViews + 1
-        this.channelViewsMap[event.channelId] = currentChannelViews + 1
-        if (event.categoryId) this.categoryViewsMap[event.categoryId] = currentCategoryViews + 1
+        this.videoViewsMap[videoId] = currentVideoViews + 1
+        this.channelViewsMap[channelId] = currentChannelViews + 1
+        if (categoryId) this.categoryViewsMap[categoryId] = currentCategoryViews + 1
         this.allViewsEvents = [...this.allViewsEvents, { videoId, channelId, categoryId, timestamp }]
         break
       default:
-        console.error(`Parsing unknown video event: ${event.type}`)
+        console.error(`Parsing unknown video event: ${type}`)
     }
   }
 }

+ 10 - 5
src/resolvers/viewsInfo.ts

@@ -152,12 +152,17 @@ const mapMostViewedArray = (views: Record<string, number>, limit?: number) =>
     : []
 
 const filterAllViewsByPeriod = (ctx: OrionContext, period?: number): Partial<UnsequencedVideoEvent>[] => {
-  const views = ctx.viewsAggregate.getAllViewsEvents()
+  const views = [...ctx.viewsAggregate.getAllViewsEvents()].reverse()
+  const filteredViews = []
   if (!period) return views
-  return views.filter(({ timestamp }) => {
-    if (!period) return true
-    return timestamp && differenceInCalendarDays(new Date(), timestamp) <= period
-  })
+
+  for (const view of views) {
+    const { timestamp } = view
+    if (timestamp && differenceInCalendarDays(new Date(), timestamp) > period) break
+    filteredViews.push(view)
+  }
+
+  return filteredViews
 }
 
 const buildMostViewedVideosArray = (ctx: OrionContext, period?: number) =>