Merge pull request #1493 from Human-Connection/implement_better_statistics_resolver

Implement better statistics resolver
This commit is contained in:
Wolfgang Huß 2019-09-06 14:55:18 +02:00 committed by GitHub
commit 9b4fa4721e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 73 deletions

View File

@ -1,68 +1,37 @@
export const query = (cypher, session) => {
return new Promise((resolve, reject) => {
const data = []
session.run(cypher).subscribe({
onNext: function(record) {
const item = {}
record.keys.forEach(key => {
item[key] = record.get(key)
})
data.push(item)
},
onCompleted: function() {
session.close()
resolve(data)
},
onError: function(error) {
reject(error)
},
})
})
}
const queryOne = (cypher, session) => {
return new Promise((resolve, reject) => {
query(cypher, session)
.then(res => {
resolve(res.length ? res.pop() : {})
})
.catch(err => {
reject(err)
})
})
}
export default {
Query: {
statistics: async (parent, args, { driver, user }) => {
return new Promise(resolve => {
const session = driver.session()
const queries = {
countUsers:
'MATCH (r:User) WHERE r.deleted <> true OR NOT exists(r.deleted) RETURN COUNT(r) AS countUsers',
countPosts:
'MATCH (r:Post) WHERE r.deleted <> true OR NOT exists(r.deleted) RETURN COUNT(r) AS countPosts',
countComments:
'MATCH (r:Comment) WHERE r.deleted <> true OR NOT exists(r.deleted) RETURN COUNT(r) AS countComments',
countNotifications: 'MATCH ()-[r:NOTIFIED]->() RETURN COUNT(r) AS countNotifications',
countInvites: 'MATCH (r:InvitationCode) RETURN COUNT(r) AS countInvites',
countFollows: 'MATCH (:User)-[r:FOLLOWS]->(:User) RETURN COUNT(r) AS countFollows',
countShouts: 'MATCH (:User)-[r:SHOUTED]->(:Post) RETURN COUNT(r) AS countShouts',
const session = driver.session()
const response = {}
try {
const mapping = {
countUsers: 'User',
countPosts: 'Post',
countComments: 'Comment',
countNotifications: 'NOTIFIED',
countInvites: 'InvitationCode',
countFollows: 'FOLLOWS',
countShouts: 'SHOUTED',
}
const data = {
countUsers: queryOne(queries.countUsers, session).then(res => res.countUsers.low),
countPosts: queryOne(queries.countPosts, session).then(res => res.countPosts.low),
countComments: queryOne(queries.countComments, session).then(
res => res.countComments.low,
),
countNotifications: queryOne(queries.countNotifications, session).then(
res => res.countNotifications.low,
),
countInvites: queryOne(queries.countInvites, session).then(res => res.countInvites.low),
countFollows: queryOne(queries.countFollows, session).then(res => res.countFollows.low),
countShouts: queryOne(queries.countShouts, session).then(res => res.countShouts.low),
}
resolve(data)
})
const cypher = `
CALL apoc.meta.stats() YIELD labels, relTypesCount
RETURN labels, relTypesCount
`
const result = await session.run(cypher)
const [statistics] = await result.records.map(record => {
return {
...record.get('labels'),
...record.get('relTypesCount'),
}
})
Object.keys(mapping).forEach(key => {
const stat = statistics[mapping[key]]
response[key] = stat ? stat.toNumber() : 0
})
} finally {
session.close()
}
return response
},
},
}

View File

@ -2,8 +2,6 @@ type Query {
isLoggedIn: Boolean!
# Get the currently logged in User based on the given JWT Token
currentUser: User
# Get the latest Network Statistics
statistics: Statistics!
findPosts(query: String!, limit: Int = 10, filter: _PostFilter): [Post]!
@cypher(
statement: """
@ -39,16 +37,6 @@ type Mutation {
unfollow(id: ID!, type: FollowTypeEnum): Boolean!
}
type Statistics {
countUsers: Int!
countPosts: Int!
countComments: Int!
countNotifications: Int!
countInvites: Int!
countFollows: Int!
countShouts: Int!
}
type Report {
id: ID!
submitter: User @relation(name: "REPORTED", direction: "IN")

View File

@ -0,0 +1,14 @@
type Query {
statistics: Statistics!
}
type Statistics {
countUsers: Int!
countPosts: Int!
countComments: Int!
countNotifications: Int!
countInvites: Int!
countFollows: Int!
countShouts: Int!
}