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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistiques d'instance interrogeables par API #716

Merged
merged 1 commit into from
Jan 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions graphql/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Article, Query: ArticleQuery, Mutation: ArticleMutation } = require('./a
const { Tag, Query: TagQuery, Mutation: TagMutation } = require('./tagResolver')
const { Version, Query: VersionQuery, Mutation: VersionMutation } = require('./versionResolver')
const { Mutation: AuthMutation } = require('./authResolver')
const { InstanceUsageStats, Query: StatsQuery } = require('./statsResolver')
const { EmailAddressResolver, JWTResolver, HexColorCodeResolver, DateTimeResolver } = require('graphql-scalars')

module.exports = {
Expand All @@ -17,12 +18,14 @@ module.exports = {
Article,
Tag,
Version,
InstanceUsageStats,
// Root queries & mutations
Query: {
...UserQuery,
...ArticleQuery,
...TagQuery,
...VersionQuery,
...StatsQuery,
},
Mutation: {
...UserMutation,
Expand Down
58 changes: 58 additions & 0 deletions graphql/resolvers/statsResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

const Article = require('../models/article.js')
const User = require('../models/user.js')

// incorrect, because we also inject it at runtime, based on Git tag
const { version } = require('../package.json')

module.exports = {
Query: {
async stats () {
return {
version
}
}
},

InstanceUsageStats: {
async articles () {
const [total, years] = await Promise.all([
Article.estimatedDocumentCount(),
Article.aggregate([
{ $project: { createdYear: {$year: "$createdAt"} } },
{ $group: { _id: '$createdYear', count: {$sum: 1} } }
])
])

return {
total,
years: years.map(({ _id: year, count }) => ({ year, count }))
}
},

async users () {
const [total, [{count: local}], [{count: openid}], years] = await Promise.all([
User.estimatedDocumentCount(),
User.aggregate([
{ $match:{ authType: "local"} },
{ $count: 'count'}
]),
User.aggregate([
{ $match:{ authType: { $ne : "local" }} },
{ $count: 'count'}
]),
User.aggregate([
{ $project: { createdYear: {$year: "$createdAt"} } },
{ $group: { _id: '$createdYear', count: {$sum: 1} } }
])
])

return {
total,
local,
openid,
years: years.map(({ _id: year, count }) => ({ year, count }))
}
}
}
}
35 changes: 35 additions & 0 deletions graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ type ArticleContributor {
roles: [String]
}

type InstanceUsageStats {
version: String
users: InstanceUserStats
articles: InstanceArticleStats
}

interface InstanceObjectUsageStats {
total: Int!
# currentYear: Int!
years: [InstanceObjectUsageYearlyStats]
}

type InstanceObjectUsageYearlyStats {
year: Int
count: Int
# variation: Int
}

type InstanceUserStats implements InstanceObjectUsageStats {
total: Int!
# currentYear: Int!
local: Int
openid: Int
years: [InstanceObjectUsageYearlyStats]
}

type InstanceArticleStats implements InstanceObjectUsageStats {
total: Int!
# currentYear: Int!
years: [InstanceObjectUsageYearlyStats]
}

input VersionInput {
article: ID!
major: Boolean
Expand Down Expand Up @@ -150,6 +182,9 @@ type Query {

"Fetch version info"
version(version: ID!): Version

"Fetch instance stats"
stats: InstanceUsageStats
}

type Mutation {
Expand Down