diff --git a/backend/src/schema/resolvers/statistics.js b/backend/src/schema/resolvers/statistics.js index db6e205d2..466c1ef70 100644 --- a/backend/src/schema/resolvers/statistics.js +++ b/backend/src/schema/resolvers/statistics.js @@ -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 }, }, } diff --git a/backend/src/schema/types/schema.gql b/backend/src/schema/types/schema.gql index 7aa04ea57..eb78cabfe 100644 --- a/backend/src/schema/types/schema.gql +++ b/backend/src/schema/types/schema.gql @@ -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") diff --git a/backend/src/schema/types/type/Statistics.gql b/backend/src/schema/types/type/Statistics.gql new file mode 100644 index 000000000..3963a3e50 --- /dev/null +++ b/backend/src/schema/types/type/Statistics.gql @@ -0,0 +1,14 @@ +type Query { + statistics: Statistics! +} + +type Statistics { + countUsers: Int! + countPosts: Int! + countComments: Int! + countNotifications: Int! + countInvites: Int! + countFollows: Int! + countShouts: Int! +} + diff --git a/webapp/components/ContributionForm/ContributionForm.vue b/webapp/components/ContributionForm/ContributionForm.vue index 43f9f3d8c..9ff43d236 100644 --- a/webapp/components/ContributionForm/ContributionForm.vue +++ b/webapp/components/ContributionForm/ContributionForm.vue @@ -1,14 +1,14 @@