mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #2041 from gradido/statistics-resolver
feat: Statistics Resolver
This commit is contained in:
commit
e5f7bec965
15
admin/src/graphql/communityStatistics.js
Normal file
15
admin/src/graphql/communityStatistics.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
export const communityStatistics = gql`
|
||||||
|
query {
|
||||||
|
communityStatistics {
|
||||||
|
totalUsers
|
||||||
|
activeUsers
|
||||||
|
deletedUsers
|
||||||
|
totalGradidoCreated
|
||||||
|
totalGradidoDecayed
|
||||||
|
totalGradidoAvailable
|
||||||
|
totalGradidoUnbookedDecayed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
@ -31,6 +31,7 @@ export enum RIGHTS {
|
|||||||
LIST_ALL_CONTRIBUTIONS = 'LIST_ALL_CONTRIBUTIONS',
|
LIST_ALL_CONTRIBUTIONS = 'LIST_ALL_CONTRIBUTIONS',
|
||||||
UPDATE_CONTRIBUTION = 'UPDATE_CONTRIBUTION',
|
UPDATE_CONTRIBUTION = 'UPDATE_CONTRIBUTION',
|
||||||
LIST_CONTRIBUTION_LINKS = 'LIST_CONTRIBUTION_LINKS',
|
LIST_CONTRIBUTION_LINKS = 'LIST_CONTRIBUTION_LINKS',
|
||||||
|
COMMUNITY_STATISTICS = 'COMMUNITY_STATISTICS',
|
||||||
SEARCH_ADMIN_USERS = 'SEARCH_ADMIN_USERS',
|
SEARCH_ADMIN_USERS = 'SEARCH_ADMIN_USERS',
|
||||||
// Admin
|
// Admin
|
||||||
SEARCH_USERS = 'SEARCH_USERS',
|
SEARCH_USERS = 'SEARCH_USERS',
|
||||||
|
|||||||
@ -30,6 +30,7 @@ export const ROLE_USER = new Role('user', [
|
|||||||
RIGHTS.UPDATE_CONTRIBUTION,
|
RIGHTS.UPDATE_CONTRIBUTION,
|
||||||
RIGHTS.SEARCH_ADMIN_USERS,
|
RIGHTS.SEARCH_ADMIN_USERS,
|
||||||
RIGHTS.LIST_CONTRIBUTION_LINKS,
|
RIGHTS.LIST_CONTRIBUTION_LINKS,
|
||||||
|
RIGHTS.COMMUNITY_STATISTICS,
|
||||||
])
|
])
|
||||||
export const ROLE_ADMIN = new Role('admin', Object.values(RIGHTS)) // all rights
|
export const ROLE_ADMIN = new Role('admin', Object.values(RIGHTS)) // all rights
|
||||||
|
|
||||||
|
|||||||
26
backend/src/graphql/model/CommunityStatistics.ts
Normal file
26
backend/src/graphql/model/CommunityStatistics.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { ObjectType, Field } from 'type-graphql'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
|
@ObjectType()
|
||||||
|
export class CommunityStatistics {
|
||||||
|
@Field(() => Number)
|
||||||
|
totalUsers: number
|
||||||
|
|
||||||
|
@Field(() => Number)
|
||||||
|
activeUsers: number
|
||||||
|
|
||||||
|
@Field(() => Number)
|
||||||
|
deletedUsers: number
|
||||||
|
|
||||||
|
@Field(() => Decimal)
|
||||||
|
totalGradidoCreated: Decimal
|
||||||
|
|
||||||
|
@Field(() => Decimal)
|
||||||
|
totalGradidoDecayed: Decimal
|
||||||
|
|
||||||
|
@Field(() => Decimal)
|
||||||
|
totalGradidoAvailable: Decimal
|
||||||
|
|
||||||
|
@Field(() => Decimal)
|
||||||
|
totalGradidoUnbookedDecayed: Decimal
|
||||||
|
}
|
||||||
77
backend/src/graphql/resolver/StatisticsResolver.ts
Normal file
77
backend/src/graphql/resolver/StatisticsResolver.ts
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import { Resolver, Query, Authorized } from 'type-graphql'
|
||||||
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
|
import { CommunityStatistics } from '@model/CommunityStatistics'
|
||||||
|
import { User as DbUser } from '@entity/User'
|
||||||
|
import { Transaction as DbTransaction } from '@entity/Transaction'
|
||||||
|
import { getConnection } from '@dbTools/typeorm'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
import { calculateDecay } from '@/util/decay'
|
||||||
|
|
||||||
|
@Resolver()
|
||||||
|
export class StatisticsResolver {
|
||||||
|
@Authorized([RIGHTS.COMMUNITY_STATISTICS])
|
||||||
|
@Query(() => CommunityStatistics)
|
||||||
|
async communityStatistics(): Promise<CommunityStatistics> {
|
||||||
|
const allUsers = await DbUser.find({ withDeleted: true })
|
||||||
|
|
||||||
|
let totalUsers = 0
|
||||||
|
let activeUsers = 0
|
||||||
|
let deletedUsers = 0
|
||||||
|
|
||||||
|
let totalGradidoAvailable: Decimal = new Decimal(0)
|
||||||
|
let totalGradidoUnbookedDecayed: Decimal = new Decimal(0)
|
||||||
|
|
||||||
|
const receivedCallDate = new Date()
|
||||||
|
|
||||||
|
for (let i = 0; i < allUsers.length; i++) {
|
||||||
|
if (allUsers[i].deletedAt) {
|
||||||
|
deletedUsers++
|
||||||
|
} else {
|
||||||
|
totalUsers++
|
||||||
|
const lastTransaction = await DbTransaction.findOne({
|
||||||
|
where: { userId: allUsers[i].id },
|
||||||
|
order: { balanceDate: 'DESC' },
|
||||||
|
})
|
||||||
|
if (lastTransaction) {
|
||||||
|
activeUsers++
|
||||||
|
const decay = calculateDecay(
|
||||||
|
lastTransaction.balance,
|
||||||
|
lastTransaction.balanceDate,
|
||||||
|
receivedCallDate,
|
||||||
|
)
|
||||||
|
if (decay) {
|
||||||
|
totalGradidoAvailable = totalGradidoAvailable.plus(decay.balance.toString())
|
||||||
|
totalGradidoUnbookedDecayed = totalGradidoUnbookedDecayed.plus(decay.decay.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryRunner = getConnection().createQueryRunner()
|
||||||
|
await queryRunner.connect()
|
||||||
|
|
||||||
|
const { totalGradidoCreated } = await queryRunner.manager
|
||||||
|
.createQueryBuilder()
|
||||||
|
.select('SUM(transaction.amount) AS totalGradidoCreated')
|
||||||
|
.from(DbTransaction, 'transaction')
|
||||||
|
.where('transaction.typeId = 1')
|
||||||
|
.getRawOne()
|
||||||
|
|
||||||
|
const { totalGradidoDecayed } = await queryRunner.manager
|
||||||
|
.createQueryBuilder()
|
||||||
|
.select('SUM(transaction.decay) AS totalGradidoDecayed')
|
||||||
|
.from(DbTransaction, 'transaction')
|
||||||
|
.where('transaction.decay IS NOT NULL')
|
||||||
|
.getRawOne()
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalUsers,
|
||||||
|
activeUsers,
|
||||||
|
deletedUsers,
|
||||||
|
totalGradidoCreated,
|
||||||
|
totalGradidoDecayed,
|
||||||
|
totalGradidoAvailable,
|
||||||
|
totalGradidoUnbookedDecayed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user