fedisearch/application/src/graphql/server/schema/queries/listStats.ts

96 wiersze
2.8 KiB
TypeScript
Czysty Zwykły widok Historia

2022-09-14 19:16:00 +00:00
import { arg, extendType, nonNull } from 'nexus'
import { StatsList, StatsQueryInput } from '../types'
import { Context } from '../../context'
import { ListStatsVariables } from '../../../common/queries/listStats'
import nodeIndex from '../../../../lib/storage/Definitions/nodeIndex'
import { StatsQueryInputType } from '../../../common/types/StatsQueryInput'
2022-11-03 18:38:01 +00:00
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
2022-09-14 19:16:00 +00:00
const getSort = (query: StatsQueryInputType) => {
switch (query.sortBy) {
case 'nodeCount':
return { _count: { order: query.sortWay } }
case 'accountFeedCount':
return { accountFeedCount: { order: query.sortWay } }
case 'channelFeedCount':
return { channelFeedCount: { order: query.sortWay } }
case 'softwareName':
default:
return { _key: { order: query.sortWay } }
}
}
export const listStats = extendType({
type: 'Query',
definition (t) {
t.field('listStats', {
type: StatsList,
args: {
query: arg({
type: nonNull(StatsQueryInput),
default: { sortBy: 'nodeCount', sortWay: 'desc' }
})
},
2022-11-03 18:38:01 +00:00
resolve: async (event, { query }: ListStatsVariables, { elasticClient }: Context) => {
2022-09-14 19:16:00 +00:00
console.info('Searching stats', { query })
const results = await elasticClient.search({
index: nodeIndex,
query: {
match_all: {}
},
aggs: {
software: {
terms: {
field: 'softwareName',
size: 1000,
min_doc_count: 2
},
aggs: {
accountFeedCount: {
sum: {
field: 'accountFeedCount'
}
},
channelFeedCount: {
sum: {
field: 'channelFeedCount'
}
},
sort: {
bucket_sort: {
sort: [
2022-11-03 18:38:01 +00:00
// @ts-expect-error
2022-09-14 19:16:00 +00:00
getSort(query)
]
}
}
}
}
}
})
2022-11-03 18:38:01 +00:00
interface Aggregation {
buckets: Array<{
key: string
2022-09-14 19:16:00 +00:00
// eslint-disable-next-line camelcase
2022-11-03 18:38:01 +00:00
doc_count: number
accountFeedCount: { value: number }
channelFeedCount: { value: number }
}>
2022-09-14 19:16:00 +00:00
}
2022-11-03 18:38:01 +00:00
const software = results?.aggregations?.software as Aggregation
2022-09-14 19:16:00 +00:00
return {
items: software.buckets.map(bucket => {
return {
softwareName: bucket.key,
nodeCount: bucket.doc_count,
accountFeedCount: bucket.accountFeedCount.value,
channelFeedCount: bucket.channelFeedCount.value
}
})
}
}
})
}
})