|
@@ -1,22 +1,13 @@
|
|
import { GenericAggregate } from './shared'
|
|
import { GenericAggregate } from './shared'
|
|
-import {
|
|
|
|
- ChannelEvent,
|
|
|
|
- ChannelEventsBucketModel,
|
|
|
|
- ChannelEventType,
|
|
|
|
- UnsequencedChannelEvent,
|
|
|
|
-} from '../models/ChannelEvent'
|
|
|
|
|
|
+import { ChannelEvent, ChannelEventModel, ChannelEventType } from '../models/ChannelEvent'
|
|
|
|
|
|
import { ChannelFollowsInfo } from '../entities/ChannelFollowsInfo'
|
|
import { ChannelFollowsInfo } from '../entities/ChannelFollowsInfo'
|
|
import { mapPeriods } from '../helpers'
|
|
import { mapPeriods } from '../helpers'
|
|
import { differenceInCalendarDays } from 'date-fns'
|
|
import { differenceInCalendarDays } from 'date-fns'
|
|
|
|
|
|
-type ChannelEventsAggregationResult = {
|
|
|
|
- events?: ChannelEvent[]
|
|
|
|
-}[]
|
|
|
|
-
|
|
|
|
type TimePeriodEventsData = {
|
|
type TimePeriodEventsData = {
|
|
- sevenDays: Partial<UnsequencedChannelEvent>[]
|
|
|
|
- thirtyDays: Partial<UnsequencedChannelEvent>[]
|
|
|
|
|
|
+ sevenDays: Partial<ChannelEvent>[]
|
|
|
|
+ thirtyDays: Partial<ChannelEvent>[]
|
|
}
|
|
}
|
|
|
|
|
|
type TimePeriodFollows = {
|
|
type TimePeriodFollows = {
|
|
@@ -57,9 +48,9 @@ export class FollowsAggregate implements GenericAggregate<ChannelEvent> {
|
|
}
|
|
}
|
|
|
|
|
|
private addOrRemoveFollowEvent(
|
|
private addOrRemoveFollowEvent(
|
|
- array: Partial<UnsequencedChannelEvent>[],
|
|
|
|
|
|
+ array: Partial<ChannelEvent>[],
|
|
eventType: ChannelEventType,
|
|
eventType: ChannelEventType,
|
|
- { channelId, timestamp }: UnsequencedChannelEvent
|
|
|
|
|
|
+ { channelId, timestamp }: ChannelEvent
|
|
): void {
|
|
): void {
|
|
if (eventType === ChannelEventType.FollowChannel) {
|
|
if (eventType === ChannelEventType.FollowChannel) {
|
|
array.push({ channelId, timestamp })
|
|
array.push({ channelId, timestamp })
|
|
@@ -110,13 +101,7 @@ export class FollowsAggregate implements GenericAggregate<ChannelEvent> {
|
|
}
|
|
}
|
|
|
|
|
|
public static async Build(): Promise<FollowsAggregate> {
|
|
public static async Build(): Promise<FollowsAggregate> {
|
|
- const aggregation: ChannelEventsAggregationResult = await ChannelEventsBucketModel.aggregate([
|
|
|
|
- { $unwind: '$events' },
|
|
|
|
- { $group: { _id: null, allEvents: { $push: '$events' } } },
|
|
|
|
- { $project: { events: '$allEvents' } },
|
|
|
|
- ])
|
|
|
|
-
|
|
|
|
- const events = aggregation[0]?.events || []
|
|
|
|
|
|
+ const events = await ChannelEventModel.find({}).lean()
|
|
|
|
|
|
const aggregate = new FollowsAggregate()
|
|
const aggregate = new FollowsAggregate()
|
|
events.forEach((event) => {
|
|
events.forEach((event) => {
|
|
@@ -129,7 +114,7 @@ export class FollowsAggregate implements GenericAggregate<ChannelEvent> {
|
|
return aggregate
|
|
return aggregate
|
|
}
|
|
}
|
|
|
|
|
|
- public applyEvent(event: UnsequencedChannelEvent) {
|
|
|
|
|
|
+ public applyEvent(event: ChannelEvent) {
|
|
const { type, ...eventWithoutType } = event
|
|
const { type, ...eventWithoutType } = event
|
|
const { channelId } = eventWithoutType
|
|
const { channelId } = eventWithoutType
|
|
const currentChannelFollows = this.channelFollowsMap[channelId] || 0
|
|
const currentChannelFollows = this.channelFollowsMap[channelId] || 0
|