Skip to content

Commit

Permalink
feat: merge super likes stat with old likes
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jan 17, 2024
1 parent fc5769b commit 987112b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
38 changes: 36 additions & 2 deletions src/components/statistics/Statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import messages from 'src/messages'
import { useMyAddress } from '../auth/MyAccountsContext'
import { PageContent } from '../main/PageWrapper'
import { useResponsiveSize } from '../responsive/ResponsiveContext'
import { getSuperLikesStats } from '../utils/datahub/active-staking'
import { getSuperLikesStats, SuperLikesStat } from '../utils/datahub/active-staking'
import { Loading } from '../utils/index'
import Section from '../utils/Section'
import { Stats } from '../utils/Stats'
Expand Down Expand Up @@ -178,7 +178,8 @@ export function Statistics(props: FormProps) {
statisticsDataArrPromise,
superLikeDataPromise,
] as const)
console.log(statisticsDataArr, superLikeData)

combineOldLikesAndSuperLikes(statisticsDataArr, superLikeData)

if (isMounted) {
setData(statisticsDataArr)
Expand Down Expand Up @@ -232,6 +233,39 @@ export function Statistics(props: FormProps) {
)
}

function combineOldLikesAndSuperLikes(
stats: StatType[],
superLikesStats: SuperLikesStat[],
): StatType[] {
const likesStat = stats.find(
stat => stat.activityType === 'PostReactionCreated,CommentReactionCreated',
)
if (!likesStat) return stats

const totalSuperLikes = superLikesStats.reduce((acc, stat) => acc + stat.count, 0)
likesStat.totalCount += totalSuperLikes
likesStat.countByPeriod += totalSuperLikes
likesStat.todayCount = superLikesStats[superLikesStats.length - 1].count

const allLikesMap = new Map<string, StatsType>()
likesStat.statisticsData.forEach(stat => allLikesMap.set(stat.format_date, stat))

superLikesStats.forEach(stat => {
const dateFormat = dayjs(stat.dayUnixTimestamp).format('YYYY-MM-DD')
const existingStat = allLikesMap.get(dateFormat)
if (existingStat) {
existingStat.count += stat.count
} else {
likesStat.statisticsData.push({ count: stat.count, format_date: dateFormat })
}
})

likesStat.statisticsData.sort((a, b) => {
return dayjs(a.format_date).unix() - dayjs(b.format_date).unix()
})
return stats
}

const getDatesBetweenDates = (startDate: Date, endDate: Date) => {
let dates: string[] = []
const theDate = new Date(startDate)
Expand Down
5 changes: 2 additions & 3 deletions src/components/utils/datahub/active-staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ const GET_SUPER_LIKES_STATS = gql`
}
}
`
export async function getSuperLikesStats(
period: number,
): Promise<{ count: number; dayUnixTimestamp: number }[]> {
export type SuperLikesStat = { count: number; dayUnixTimestamp: number }
export async function getSuperLikesStats(period: number): Promise<SuperLikesStat[]> {
const { day: currentTimestamp } = getDayAndWeekTimestamp()
const currentMinusPeriod = dayjs().subtract(period, 'day').toDate()
const { day: startTimestamp } = getDayAndWeekTimestamp(currentMinusPeriod)
Expand Down

0 comments on commit 987112b

Please sign in to comment.