Skip to content

Commit

Permalink
refactor(analytics): use hooks and resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-mitra committed Oct 10, 2023
1 parent 9d8f51c commit edd0b45
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 88 deletions.
89 changes: 4 additions & 85 deletions packages/server-core/src/analytics/analytics/analytics.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import { Paginated, Params } from '@feathersjs/feathers'
import type { KnexAdapterOptions } from '@feathersjs/knex'
import { KnexAdapter } from '@feathersjs/knex'
import { Params } from '@feathersjs/feathers'
import { KnexService } from '@feathersjs/knex'

import {
AnalyticsData,
Expand All @@ -34,94 +33,14 @@ import {
AnalyticsType
} from '@etherealengine/engine/src/schemas/analytics/analytics.schema'

import { instanceAttendancePath } from '@etherealengine/engine/src/schemas/networking/instance-attendance.schema'
import { userPath } from '@etherealengine/engine/src/schemas/user/user.schema'
import { Knex } from 'knex'
import { Application } from '../../../declarations'
import { RootParams } from '../../api/root-params'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AnalyticsParams extends RootParams<AnalyticsQuery> {}

/**
* A class for Analytics service
*/

export class AnalyticsService<T = AnalyticsType, ServiceParams extends Params = AnalyticsParams> extends KnexAdapter<
export class AnalyticsService<T = AnalyticsType, ServiceParams extends Params = AnalyticsParams> extends KnexService<
AnalyticsType,
AnalyticsData,
AnalyticsParams,
AnalyticsPatch
> {
app: Application

constructor(options: KnexAdapterOptions, app: Application) {
super(options)
this.app = app
}

async find(params?: AnalyticsParams) {
if (params?.query!.action === 'dailyUsers') {
const limit = params.query!.$limit || 30
const returned: Paginated<AnalyticsType> = {
total: limit,
skip: 0,
limit,
data: []
}
const currentDate = new Date()
for (let i = 0; i < limit; i++) {
const knexClient: Knex = this.app.get('knexClient')

const instanceAttendance = await knexClient
.countDistinct('userId AS count')
.table(instanceAttendancePath)
.where('createdAt', '>', new Date(new Date().setDate(currentDate.getDate() - (i + 1))).toISOString())
.andWhere('createdAt', '<=', new Date(new Date().setDate(currentDate.getDate() - i)).toISOString())
.first()

returned.data.push({
id: '',
count: instanceAttendance.count,
type: '',
createdAt: new Date(new Date().setDate(currentDate.getDate() - i)).toDateString(),
updatedAt: new Date(new Date().setDate(currentDate.getDate() - i)).toDateString()
})
}
return returned
} else if (params?.query!.action === 'dailyNewUsers') {
const limit = params.query!.$limit || 30
const returned: Paginated<AnalyticsType> = {
total: limit,
skip: 0,
limit,
data: []
}
const currentDate = new Date()
for (let i = 0; i < limit; i++) {
const knexClient: Knex = this.app.get('knexClient')
const newUsers = await knexClient
.count('id AS count')
.table(userPath)
.where('createdAt', '>', new Date(new Date().setDate(currentDate.getDate() - (i + 1))).toISOString())
.andWhere('createdAt', '<=', new Date(new Date().setDate(currentDate.getDate() - i)).toISOString())
.first()

returned.data.push({
id: '',
count: newUsers.count,
type: '',
createdAt: new Date(new Date().setDate(currentDate.getDate() - i)).toDateString(),
updatedAt: new Date(new Date().setDate(currentDate.getDate() - i)).toDateString()
})
}
return returned
} else {
return await super._find(params)
}
}

async create(data: AnalyticsData, params?: AnalyticsParams) {
return super._create(data, params)
}
}
> {}
79 changes: 77 additions & 2 deletions packages/server-core/src/analytics/analytics/analytics.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ Ethereal Engine. All Rights Reserved.
*/

import { hooks as schemaHooks } from '@feathersjs/schema'
import { iff, isProvider } from 'feathers-hooks-common'
import { discardQuery, iff, isProvider } from 'feathers-hooks-common'

import {
AnalyticsType,
analyticsDataValidator,
analyticsPatchValidator,
analyticsQueryValidator
} from '@etherealengine/engine/src/schemas/analytics/analytics.schema'
import { instanceAttendancePath } from '@etherealengine/engine/src/schemas/networking/instance-attendance.schema'
import { userPath } from '@etherealengine/engine/src/schemas/user/user.schema'
import { Paginated } from '@feathersjs/feathers'
import { Knex } from 'knex'
import { HookContext } from '../../../declarations'
import authenticate from '../../hooks/authenticate'
import verifyScope from '../../hooks/verify-scope'
import { AnalyticsService } from './analytics.class'
import {
analyticsDataResolver,
analyticsExternalResolver,
Expand All @@ -41,6 +48,70 @@ import {
analyticsResolver
} from './analytics.resolvers'

function checkQueryAction(action: 'dailyUsers' | 'dailyNewUsers') {
return (context: HookContext<AnalyticsService>) => context.params.query?.action === action
}

async function addDailyUsers(context: HookContext<AnalyticsService>) {
const limit = context.params.query?.$limit || 30
const result: Paginated<AnalyticsType> = {
total: limit,
skip: 0,
limit,
data: []
}

const currentDate = new Date()
for (let day = 0; day < limit; day++) {
const knexClient: Knex = context.app.get('knexClient')

const instanceAttendance = await knexClient
.countDistinct('userId AS count')
.table(instanceAttendancePath)
.where('createdAt', '>', new Date(new Date().setDate(currentDate.getDate() - (day + 1))).toISOString())
.andWhere('createdAt', '<=', new Date(new Date().setDate(currentDate.getDate() - day)).toISOString())
.first()

result.data.push({
id: '',
count: instanceAttendance.count,
type: '',
createdAt: new Date(new Date().setDate(currentDate.getDate() - day)).toDateString(),
updatedAt: new Date(new Date().setDate(currentDate.getDate() - day)).toDateString()
})
}
context.result = result
}

async function addDailyNewUsers(context: HookContext<AnalyticsService>) {
const limit = context.params.query?.$limit || 30
const result: Paginated<AnalyticsType> = {
total: limit,
skip: 0,
limit,
data: []
}
const currentDate = new Date()
for (let day = 0; day < limit; day++) {
const knexClient: Knex = this.app.get('knexClient')
const newUsers = await knexClient
.count('id AS count')
.table(userPath)
.where('createdAt', '>', new Date(new Date().setDate(currentDate.getDate() - (day + 1))).toISOString())
.andWhere('createdAt', '<=', new Date(new Date().setDate(currentDate.getDate() - day)).toISOString())
.first()

result.data.push({
id: '',
count: newUsers.count,
type: '',
createdAt: new Date(new Date().setDate(currentDate.getDate() - day)).toDateString(),
updatedAt: new Date(new Date().setDate(currentDate.getDate() - day)).toDateString()
})
}
context.result = result
}

export default {
around: {
all: [schemaHooks.resolveExternal(analyticsExternalResolver), schemaHooks.resolveResult(analyticsResolver)]
Expand All @@ -53,7 +124,11 @@ export default {
() => schemaHooks.validateQuery(analyticsQueryValidator),
schemaHooks.resolveQuery(analyticsQueryResolver)
],
find: [],
find: [
iff(checkQueryAction('dailyUsers'), addDailyUsers),
iff(checkQueryAction('dailyNewUsers'), addDailyNewUsers),
discardQuery('action')
],
get: [],
create: [() => schemaHooks.validateData(analyticsDataValidator), schemaHooks.resolveData(analyticsDataResolver)],
update: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/server-core/src/analytics/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (app: Application): void => {
multi: true
}

app.use(analyticsPath, new AnalyticsService(options, app), {
app.use(analyticsPath, new AnalyticsService(options), {
// A list of all methods this service exposes externally
methods: analyticsMethods,
// You can add additional custom events to be sent to clients here
Expand Down

0 comments on commit edd0b45

Please sign in to comment.