Merge pull request #2922 from specklesystems/fabians/stats-ioc-1

chore(server): stats IoC 1 - getTotalStreamCountFactory
This commit is contained in:
Alessandro Magionami
2024-09-11 09:51:16 +02:00
committed by GitHub
4 changed files with 17 additions and 10 deletions
@@ -1,3 +1,4 @@
import { db } from '@/db/knex'
import { Resolvers } from '@/modules/core/graph/generated/graphql'
import { mapServerRoleToValue } from '@/modules/core/helpers/graphTypes'
import { toProjectIdWhitelist } from '@/modules/core/helpers/token'
@@ -6,7 +7,10 @@ import {
adminProjectList,
adminUserList
} from '@/modules/core/services/admin'
import { getTotalStreamCount, getTotalUserCount } from '@/modules/stats/services'
import {
getTotalStreamCountFactory,
getTotalUserCount
} from '@/modules/stats/repositories'
export = {
Query: {
@@ -38,7 +42,7 @@ export = {
},
ServerStatistics: {
async totalProjectCount() {
return await getTotalStreamCount()
return await getTotalStreamCountFactory({ db })()
},
async totalUserCount() {
@@ -6,13 +6,14 @@ import {
getCommitHistory,
getObjectHistory,
getUserHistory,
getTotalStreamCount,
getTotalStreamCountFactory,
getTotalCommitCount,
getTotalObjectCount,
getTotalUserCount
} from '@/modules/stats/services/index'
} from '@/modules/stats/repositories/index'
import { Roles, Scopes } from '@speckle/shared'
import { throwForNotHavingServerRole } from '@/modules/shared/authz'
import { db } from '@/db/knex'
export = {
Query: {
@@ -28,7 +29,7 @@ export = {
ServerStats: {
async totalStreamCount() {
return await getTotalStreamCount()
return await getTotalStreamCountFactory({ db })()
},
async totalCommitCount() {
@@ -1,8 +1,9 @@
import knex from '@/db/knex'
import { Knex } from 'knex'
export async function getTotalStreamCount() {
export const getTotalStreamCountFactory = (deps: { db: Knex }) => async () => {
const query = 'SELECT COUNT(*) FROM streams'
const result = await knex.raw(query)
const result = await deps.db.raw(query)
return parseInt(result.rows[0].count)
}
@@ -14,14 +14,15 @@ import {
getCommitHistory,
getObjectHistory,
getUserHistory,
getTotalStreamCount,
getTotalStreamCountFactory,
getTotalCommitCount,
getTotalObjectCount,
getTotalUserCount
} from '@/modules/stats/services/index'
} from '@/modules/stats/repositories/index'
import { Scopes } from '@speckle/shared'
import { Server } from 'node:http'
import { Express } from 'express'
import { db } from '@/db/knex'
const params = { numUsers: 25, numStreams: 30, numObjects: 100, numCommits: 100 }
@@ -38,7 +39,7 @@ describe('Server stats services @stats-services', function () {
})
it('should return the total number of streams on this server', async () => {
const res = await getTotalStreamCount()
const res = await getTotalStreamCountFactory({ db })()
expect(res).to.equal(params.numStreams)
})