Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(analytics): use hooks and resolvers #9028

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { KnexAdapterParams } from '@feathersjs/knex'
import { Knex } from 'knex'
import { Application } from '../../../declarations'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AnalyticsParams extends KnexAdapterParams<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)
}
}
> {}
76 changes: 74 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,14 +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 isAction from '../../hooks/is-action'
import verifyScope from '../../hooks/verify-scope'
import { AnalyticsService } from './analytics.class'
import {
analyticsDataResolver,
analyticsExternalResolver,
Expand All @@ -40,6 +48,66 @@ import {
analyticsResolver
} from './analytics.resolvers'

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 @@ -51,7 +119,11 @@ export default {
() => schemaHooks.validateQuery(analyticsQueryValidator),
schemaHooks.resolveQuery(analyticsQueryResolver)
],
find: [],
find: [
iff(isAction('dailyUsers'), addDailyUsers),
iff(isAction('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