Commit 13d36a00 authored by soheib's avatar soheib
Browse files

add get result list api

parent 63d01258
...@@ -98,7 +98,7 @@ export class AppService implements OnApplicationBootstrap { ...@@ -98,7 +98,7 @@ export class AppService implements OnApplicationBootstrap {
}) })
followersCount += 1 followersCount += 1
} }
else{ else {
return "already updated" return "already updated"
} }
} }
...@@ -165,7 +165,7 @@ export class AppService implements OnApplicationBootstrap { ...@@ -165,7 +165,7 @@ export class AppService implements OnApplicationBootstrap {
}) })
commentCount += 1 commentCount += 1
} }
else{ else {
return "already updated" return "already updated"
} }
} }
...@@ -380,11 +380,11 @@ export class AppService implements OnApplicationBootstrap { ...@@ -380,11 +380,11 @@ export class AppService implements OnApplicationBootstrap {
async getResults() { async getResults() {
let foundUsernames = await this.commentModel.distinct('owner_username') let foundUsernames = await this.commentModel.distinct('owner_username')
console.log(foundUsernames); console.log("total users for ranking:", foundUsernames.length);
let totalMentions = 0
for await (const username of foundUsernames) { for await (const username of foundUsernames) {
let mentions = await this.calculateUserScore(username) let mentions = await this.calculateUserScore(username)
let valid_mentions = 0 let valid_mentions = 0
let invalid_mentions = 0 let invalid_mentions = 0
...@@ -394,24 +394,23 @@ export class AppService implements OnApplicationBootstrap { ...@@ -394,24 +394,23 @@ export class AppService implements OnApplicationBootstrap {
let pending_users = new Array<string>() let pending_users = new Array<string>()
mentions.mentions.forEach(mention => { mentions.mentions.forEach(mention => {
if (mention.comment_status.includes(CommentStatus.isValid)){ if (mention.comment_status.includes(CommentStatus.isValid)) {
valid_mentions++ valid_mentions++
valid_users.push(mention.mentioned_username) valid_users.push(mention.mentioned_username)
} }
else if (mention.comment_status.includes(CommentStatus.isMentionedBefore) else if (mention.comment_status.includes(CommentStatus.isMentionedBefore)
|| mention.comment_status.includes(CommentStatus.isAFollowerBefore)){ || mention.comment_status.includes(CommentStatus.isAFollowerBefore)) {
invalid_mentions++ invalid_mentions++
inValid_users.push(mention.mentioned_username) inValid_users.push(mention.mentioned_username)
} }
else if (mention.comment_status.includes(CommentStatus.notFollower)) else if (mention.comment_status.includes(CommentStatus.notFollower)) {
pending_mentions++ pending_mentions++
pending_users.push(mention.mentioned_username) pending_users.push(mention.mentioned_username)
}
}) })
totalMentions += valid_mentions + invalid_mentions + pending_mentions
console.log("eachMention : ", valid_mentions + invalid_mentions + pending_mentions);
await this.delay(_.random(500,1000)) await this.delay(_.random(500, 1000))
let foundUser = await this.requestModel.findOne({ username: username }) let foundUser = await this.resultModel.findOne({ username: username })
if (!foundUser) { if (!foundUser) {
await this.resultModel.create({ await this.resultModel.create({
username: username, username: username,
...@@ -419,25 +418,23 @@ export class AppService implements OnApplicationBootstrap { ...@@ -419,25 +418,23 @@ export class AppService implements OnApplicationBootstrap {
invalid_mentions, invalid_mentions,
pending_mentions, pending_mentions,
score: valid_mentions + 1, score: valid_mentions + 1,
valid_users:valid_users valid_users: valid_users
,inValid_users:inValid_users, , inValid_users: inValid_users,
pending_users: pending_users pending_users: pending_users
}) })
} else { } else {
await this.resultModel.updateOne(foundUser._id, { await this.resultModel.updateOne({ _id: foundUser._id }, {
username: username, username: username,
valid_mentions, valid_mentions,
invalid_mentions, invalid_mentions,
pending_mentions, pending_mentions,
score: valid_mentions + 1, score: valid_mentions + 1,
valid_users:valid_users valid_users: valid_users
,inValid_users:inValid_users, , inValid_users: inValid_users,
pending_users: pending_users pending_users: pending_users
}) })
} }
} }
console.log("totalMentions : ", totalMentions);
return "records updated successfully" return "records updated successfully"
} }
...@@ -483,9 +480,60 @@ export class AppService implements OnApplicationBootstrap { ...@@ -483,9 +480,60 @@ export class AppService implements OnApplicationBootstrap {
return follower_objectResult return follower_objectResult
} }
async getFinalResults(){ async getFinalResults() {
return await this.resultModel.find()
let results = await this.resultModel.find().sort({ score: -1 })
let last_update = await this.resultModel.find().sort({ updatedAt: -1 })
let last_create = await this.resultModel.find().sort({ createdAt: -1 })
let date: number
console.log(last_update[0]['updatedAt'])
console.log(last_create[0]['createdAt'])
if (last_update[0]['updatedAt'] >= last_create[0]['createdAt']) {
date = last_update[0]['updatedAt']
}
else {
date = last_create[0]['createdAt']
}
let finalResult = new Array<ResultResponse>()
for await (const userRes of results) {
let response: ResultResponse = new ResultResponse()
response.users = new Array<any>()
userRes.pending_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.notFollower })
})
userRes.inValid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.inValid })
})
userRes.valid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.isValid })
})
response.username = userRes.username,
response.valid_mentions = userRes.valid_mentions,
response.invalid_mentions = userRes.invalid_mentions,
response.pending_mentions = userRes.pending_mentions,
response.score = userRes.score
finalResult.push(response)
}
return {
finalResult,
last_update: date
}
} }
} }
export class ResultResponse {
username: string
valid_mentions: number
invalid_mentions: number
pending_mentions: number
score: number
users?: Array<any>
}
...@@ -9,5 +9,7 @@ export enum CommentStatus { ...@@ -9,5 +9,7 @@ export enum CommentStatus {
isMentionedBefore ="isMentionedBefore", //"this username was mentioned already before your comment", isMentionedBefore ="isMentionedBefore", //"this username was mentioned already before your comment",
isAFollowerBefore = "isAFollowerBefore", //"this username was followed page before your comment", isAFollowerBefore = "isAFollowerBefore", //"this username was followed page before your comment",
notFollower = "notFollower", //"this username didnt follow page yet", notFollower = "notFollower", //"this username didnt follow page yet",
isValid = "isValid" //"your comment is valid" isValid = "isValid", //"your comment is valid"
inValid ="inValid (mentioned before or followed before)"
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment