Skip to content

Commit

Permalink
Merge pull request #775 from bounswe/be-618/implement_rating_algorithm
Browse files Browse the repository at this point in the history
be-618: Implement rating algorithm
  • Loading branch information
BatuhanIlhan authored Dec 25, 2023
2 parents 5864523 + baa7036 commit 1f03526
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class PollService {
): Promise<void> {
const poll = await this.pollRepository.findOne({
where: { id },
relations: ['options', 'tags'],
relations: ['options', 'tags','likes','comments'],
});

if (!poll) {
Expand Down
26 changes: 22 additions & 4 deletions app/backend/src/ranking/ranking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Ranking } from './entities/ranking.entity';
import { Repository } from 'typeorm';
import { Vote } from '../vote/entities/vote.entity';
import { Tag } from '../tag/entities/tag.entity';
import { VoteService } from '../vote/vote.service';

@Injectable()
export class RankingService {
Expand All @@ -16,6 +17,7 @@ export class RankingService {
private readonly voteRepository: Repository<Vote>,
@InjectRepository(Tag)
private readonly tagRepository: Repository<Tag>,
private readonly voteService: VoteService,
){}


Expand Down Expand Up @@ -77,13 +79,17 @@ export class RankingService {
}
)
const userIds: string[] = votes.map((vote) => vote.user.id);
if(!userIds.length){
return
}
poll.tags.push(await this.tagRepository.findOne({where: {name: "general"}}));

poll.tags.forEach( async (tag) => {this.updateScoreByTag(tag.id,userIds)});
const addition_score = await this.generateScoring(poll.id, option.id, poll.likes.length, poll.comments.length);
poll.tags.forEach( async (tag) => {this.updateScoreByTag(tag.id, userIds, addition_score)});

}

async updateScoreByTag(tagID:string, userIds:string[]){
async updateScoreByTag(tagID:string, userIds:string[],addition_score:number){
userIds.forEach(async (userId) => {
const ranking: Ranking = await this.rankingRepository.findOne({
where:{
Expand All @@ -97,20 +103,32 @@ export class RankingService {
}
)
if(ranking){
ranking.score+=1;
ranking.score+=addition_score;
this.rankingRepository.save(ranking)
}else{
const newRanking= this.rankingRepository.create({
tag: { id: tagID },
user:{ id: userId },
score: 1,
score: addition_score,
});
this.rankingRepository.save(newRanking);
}
});

}

async generateScoring(pollID: string,optionID: string,likeCount: number,commentCount:number):Promise<number>{
const vote_distribution = await this.voteService.getVoteRate(pollID);
const vote_count= await this.voteService.getVoteCount(pollID);

let base = vote_count/vote_distribution.find(entity => entity.optionId === optionID).count;

base += base * (3 * commentCount + likeCount + vote_count) / 10

return Math.ceil(base)

}

}


0 comments on commit 1f03526

Please sign in to comment.