From 0d2af686f95450a49d42c8dcd57fd2a7bcdfaade Mon Sep 17 00:00:00 2001 From: Iain Sproat <68657+iainsproat@users.noreply.github.com> Date: Thu, 19 Dec 2024 10:32:47 +0000 Subject: [PATCH] chore(server/stats): disable deprecated ServerStats (#3714) --- .../modules/stats/graph/resolvers/stats.ts | 42 +++++----- .../modules/stats/repositories/index.ts | 81 ------------------- .../server/modules/stats/tests/stats.spec.ts | 60 +------------- 3 files changed, 27 insertions(+), 156 deletions(-) diff --git a/packages/server/modules/stats/graph/resolvers/stats.ts b/packages/server/modules/stats/graph/resolvers/stats.ts index 9904aa54a..30dba86f9 100644 --- a/packages/server/modules/stats/graph/resolvers/stats.ts +++ b/packages/server/modules/stats/graph/resolvers/stats.ts @@ -1,19 +1,23 @@ import { Resolvers } from '@/modules/core/graph/generated/graphql' import { validateScopes } from '@/modules/shared' -import { - getStreamHistoryFactory, - getCommitHistoryFactory, - getObjectHistoryFactory, - getUserHistoryFactory, - getTotalStreamCountFactory, - getTotalCommitCountFactory, - getTotalObjectCountFactory, - getTotalUserCountFactory -} from '@/modules/stats/repositories/index' import { Roles, Scopes } from '@speckle/shared' import { throwForNotHavingServerRole } from '@/modules/shared/authz' -import { db } from '@/db/knex' + +const dummyHistory = [ + { '0': 0 }, + { '1': 0 }, + { '2': 0 }, + { '3': 0 }, + { '4': 0 }, + { '5': 0 }, + { '6': 0 }, + { '7': 0 }, + { '8': 0 }, + { '9': 0 }, + { '10': 0 }, + { '11': 0 } +] export = { Query: { @@ -29,35 +33,35 @@ export = { ServerStats: { async totalStreamCount() { - return await getTotalStreamCountFactory({ db })() + return 0 //deprecated, returning static value }, async totalCommitCount() { - return await getTotalCommitCountFactory({ db })() + return 0 //deprecated, returning static value }, async totalObjectCount() { - return await getTotalObjectCountFactory({ db })() + return 0 //deprecated, returning static value }, async totalUserCount() { - return await getTotalUserCountFactory({ db })() + return 0 //deprecated, returning static value }, async streamHistory() { - return await getStreamHistoryFactory({ db })() + return dummyHistory //deprecated, returning static value }, async commitHistory() { - return await getCommitHistoryFactory({ db })() + return dummyHistory //deprecated, returning static value }, async objectHistory() { - return await getObjectHistoryFactory({ db })() + return dummyHistory //deprecated, returning static value }, async userHistory() { - return await getUserHistoryFactory({ db })() + return dummyHistory //deprecated, returning static value } } } as Resolvers diff --git a/packages/server/modules/stats/repositories/index.ts b/packages/server/modules/stats/repositories/index.ts index 3471047ea..39c22aa4b 100644 --- a/packages/server/modules/stats/repositories/index.ts +++ b/packages/server/modules/stats/repositories/index.ts @@ -6,18 +6,6 @@ export const getTotalStreamCountFactory = (deps: { db: Knex }) => async () => { return parseInt(result.rows[0].count) } -export const getTotalCommitCountFactory = (deps: { db: Knex }) => async () => { - const query = 'SELECT COUNT(*) FROM commits' - const result = await deps.db.raw(query) - return parseInt(result.rows[0].count) -} - -export const getTotalObjectCountFactory = (deps: { db: Knex }) => async () => { - const query = 'SELECT COUNT(*) FROM objects' - const result = await deps.db.raw(query) - return parseInt(result.rows[0].count) -} - export const getTotalUserCountFactory = (deps: { db: Knex }) => async () => { // returns -1 for small tables, no good // const fastQuery = "SELECT reltuples::bigint AS estimate FROM pg_catalog.pg_class WHERE relname = 'users'" @@ -25,72 +13,3 @@ export const getTotalUserCountFactory = (deps: { db: Knex }) => async () => { const result = await deps.db.raw(query) return parseInt(result.rows[0].count) } - -export const getStreamHistoryFactory = (deps: { db: Knex }) => async () => { - const query = ` - SELECT - DATE_TRUNC('month', streams. "createdAt") AS created_month, - COUNT(id) AS count - FROM - streams - GROUP BY - DATE_TRUNC('month', streams. "createdAt") - ` - - const result = (await deps.db.raw(query)) as { - rows: Array<{ created_month: Date; count: string | number }> - } - result.rows.forEach((row) => (row.count = parseInt(row.count + ''))) - return result.rows -} - -export const getCommitHistoryFactory = (deps: { db: Knex }) => async () => { - const query = ` - SELECT - DATE_TRUNC('month', commits. "createdAt") AS created_month, - COUNT(id) AS count - FROM - commits - GROUP BY - DATE_TRUNC('month', commits. "createdAt") - ` - const result = (await deps.db.raw(query)) as { - rows: Array<{ created_month: Date; count: string | number }> - } - result.rows.forEach((row) => (row.count = parseInt(row.count + ''))) - return result.rows -} - -export const getObjectHistoryFactory = (deps: { db: Knex }) => async () => { - const query = ` - SELECT - DATE_TRUNC('month', objects. "createdAt") AS created_month, - COUNT(id) AS count - FROM - objects - GROUP BY - DATE_TRUNC('month', objects. "createdAt") - ` - const result = (await deps.db.raw(query)) as { - rows: Array<{ created_month: Date; count: string | number }> - } - result.rows.forEach((row) => (row.count = parseInt(row.count + ''))) - return result.rows -} - -export const getUserHistoryFactory = (deps: { db: Knex }) => async () => { - const query = ` - SELECT - DATE_TRUNC('month', users. "createdAt") AS created_month, - COUNT(id) AS count - FROM - users - GROUP BY - DATE_TRUNC('month', users. "createdAt") - ` - const result = (await deps.db.raw(query)) as { - rows: Array<{ created_month: Date; count: string | number }> - } - result.rows.forEach((row) => (row.count = parseInt(row.count + ''))) - return result.rows -} diff --git a/packages/server/modules/stats/tests/stats.spec.ts b/packages/server/modules/stats/tests/stats.spec.ts index 07ea82ebf..d74eeb13f 100644 --- a/packages/server/modules/stats/tests/stats.spec.ts +++ b/packages/server/modules/stats/tests/stats.spec.ts @@ -4,13 +4,7 @@ import { beforeEachContext, initializeTestServer } from '@/test/hooks' import { createManyObjects } from '@/test/helpers' import { - getStreamHistoryFactory, - getCommitHistoryFactory, - getObjectHistoryFactory, - getUserHistoryFactory, getTotalStreamCountFactory, - getTotalCommitCountFactory, - getTotalObjectCountFactory, getTotalUserCountFactory } from '@/modules/stats/repositories/index' import { Scopes } from '@speckle/shared' @@ -202,52 +196,6 @@ describe('Server stats services @stats-services', function () { const res = await getTotalStreamCountFactory({ db })() expect(res).to.equal(params.numStreams) }) - - it('should return the total number of commits on this server', async () => { - const res = await getTotalCommitCountFactory({ db })() - expect(res).to.equal(params.numCommits) - }) - - it('should return the total number of objects on this server', async () => { - const res = await getTotalObjectCountFactory({ db })() - expect(res).to.equal(params.numObjects) - }) - - it('should return the stream creation history by month', async () => { - const res = await getStreamHistoryFactory({ db })() - expect(res).to.be.an('array') - expect(res[0]).to.have.property('count') - expect(res[0]).to.have.property('created_month') - expect(res[0].count).to.be.a('number') - expect(res[0].count).to.equal(params.numStreams) - }) - - it('should return the commit creation history by month', async () => { - const res = await getCommitHistoryFactory({ db })() - expect(res).to.be.an('array') - expect(res[0]).to.have.property('count') - expect(res[0]).to.have.property('created_month') - expect(res[0].count).to.be.a('number') - expect(res[0].count).to.equal(params.numCommits) - }) - - it('should return the object creation history by month', async () => { - const res = await getObjectHistoryFactory({ db })() - expect(res).to.be.an('array') - expect(res[0]).to.have.property('count') - expect(res[0]).to.have.property('created_month') - expect(res[0].count).to.be.a('number') - expect(res[0].count).to.equal(params.numObjects) - }) - - it('should return the user creation history by month', async () => { - const res = await getUserHistoryFactory({ db })() - expect(res).to.be.an('array') - expect(res[0]).to.have.property('count') - expect(res[0]).to.have.property('created_month') - expect(res[0].count).to.be.a('number') - expect(res[0].count).to.equal(params.numUsers) - }) }) describe('Server stats api @stats-api', function () { @@ -357,10 +305,10 @@ describe('Server stats api @stats-api', function () { expect(res.body.data.serverStats).to.have.property('commitHistory') expect(res.body.data.serverStats).to.have.property('userHistory') - expect(res.body.data.serverStats.totalStreamCount).to.equal(params.numStreams) - expect(res.body.data.serverStats.totalCommitCount).to.equal(params.numCommits) - expect(res.body.data.serverStats.totalObjectCount).to.equal(params.numObjects) - expect(res.body.data.serverStats.totalUserCount).to.equal(params.numUsers + 2) // we're registering two extra users in the before hook + expect(res.body.data.serverStats.totalStreamCount).to.equal(0) // the endpoint is deprecated and we're now returning 0 + expect(res.body.data.serverStats.totalCommitCount).to.equal(0) // the endpoint is deprecated and we're now returning 0 + expect(res.body.data.serverStats.totalObjectCount).to.equal(0) // the endpoint is deprecated and we're now returning 0 + expect(res.body.data.serverStats.totalUserCount).to.equal(0) // the endpoint is deprecated and we're now returning 0 expect(res.body.data.serverStats.streamHistory).to.be.an('array') expect(res.body.data.serverStats.commitHistory).to.be.an('array')