mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Merge pull request #1493 from Human-Connection/implement_better_statistics_resolver
Implement better statistics resolver
This commit is contained in:
commit
9b4fa4721e
@ -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
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -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")
|
||||
|
||||
14
backend/src/schema/types/type/Statistics.gql
Normal file
14
backend/src/schema/types/type/Statistics.gql
Normal 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!
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user