Skip to content

Commit

Permalink
Add resolver to get most interacted entities for given type
Browse files Browse the repository at this point in the history
  • Loading branch information
ikprk committed Jul 4, 2024
1 parent 5be4304 commit c353aaa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
43 changes: 29 additions & 14 deletions src/server-extension/resolvers/StateResolver/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Resolver, Root, Subscription, Query, ObjectType, Field } from 'type-graphql'
import { Resolver, Root, Subscription, Query, ObjectType, Field, Args } from 'type-graphql'
import type { EntityManager } from 'typeorm'
import { ProcessorState } from './types'
import {
EarningStatsOutput,
ProcessorState,
TopInteractedEntity,
TopInteractedEntityArgs,
} from './types'
import _, { isObject } from 'lodash'
import { globalEm } from '../../../utils/globalEm'
import { has } from '../../../utils/misc'
Expand Down Expand Up @@ -74,6 +79,28 @@ export class StateResolver {
return state
}

@Query(() => [TopInteractedEntity])
async getTopInteractedEnities(
@Args() args: TopInteractedEntityArgs
): Promise<TopInteractedEntity[]> {
const em = await this.tx()

const result: { entity_id: string; entrycount: number }[] = await em.query(`
SELECT entity_id, SUM(count) as entryCount
FROM admin.user_interaction_count
WHERE type = '${args.type}'
AND day_timestamp >= NOW() - INTERVAL '${args.period} DAYS'
GROUP BY entity_id
ORDER BY entryCount DESC
LIMIT 10;
`)

return result.map((res) => ({
interactionCount: res.entrycount,
entityId: res.entity_id,
}))
}

@Query(() => EarningStatsOutput)
async totalJoystreamEarnings(): Promise<EarningStatsOutput> {
const em = await this.tx()
Expand Down Expand Up @@ -121,15 +148,3 @@ export class StateResolver {
}
}
}

@ObjectType()
export class EarningStatsOutput {
@Field({ nullable: false })
crtSaleVolume: string

@Field({ nullable: false })
totalRewardsPaid: string

@Field({ nullable: false })
nftSaleVolume: string
}
32 changes: 31 additions & 1 deletion src/server-extension/resolvers/StateResolver/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import { Field, Int, ObjectType } from 'type-graphql'
import { ArgsType, Field, Int, ObjectType } from 'type-graphql'

@ObjectType()
export class ProcessorState {
@Field(() => Int, { nullable: false })
lastProcessedBlock!: number
}

@ObjectType()
export class TopInteractedEntity {
@Field({ nullable: false })
entityId: string

@Field({ nullable: false })
interactionCount: number
}

@ArgsType()
export class TopInteractedEntityArgs {
@Field(() => String, { nullable: false })
type: string

@Field(() => Int, { nullable: false })
period: number
}

@ObjectType()
export class EarningStatsOutput {
@Field({ nullable: false })
crtSaleVolume: string

@Field({ nullable: false })
totalRewardsPaid: string

@Field({ nullable: false })
nftSaleVolume: string
}

0 comments on commit c353aaa

Please sign in to comment.