Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions database/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"slices"
"sort"
"time"

Expand Down Expand Up @@ -34,6 +35,7 @@ type Poll struct {

const POLL_TYPE_SIMPLE = "simple"
const POLL_TYPE_RANKED = "ranked"
const MATCH = "$match"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see where you're (SonarQube is?) going with this, but I'm not so sure this improves maintainability. I think it'd be better to keep this in the query because that's how mongo queries look like i.e.

{ $match: { salary: 9000 } }

I would like more input though, I could be totally crazy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can mark it in SonarQube as a false positive because of the query reasoning


func GetPoll(ctx context.Context, id string) (*Poll, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
Expand Down Expand Up @@ -143,7 +145,7 @@ func GetClosedVotedPolls(ctx context.Context, userId string) ([]*Poll, error) {

cursor, err := Client.Database(db).Collection("votes").Aggregate(ctx, mongo.Pipeline{
{{
Key: "$match", Value: bson.D{
Key: MATCH, Value: bson.D{
{Key: "userId", Value: userId},
},
}},
Expand All @@ -167,7 +169,7 @@ func GetClosedVotedPolls(ctx context.Context, userId string) ([]*Poll, error) {
},
}},
{{
Key: "$match", Value: bson.D{
Key: MATCH, Value: bson.D{
{Key: "open", Value: false},
},
}},
Expand Down Expand Up @@ -224,19 +226,15 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st
for _, picks := range votes {
// Go over picks until we find a non-eliminated candidate
for _, candidate := range picks {
if !containsValue(eliminated, candidate) {
if _, ok := tallied[candidate]; ok {
tallied[candidate]++
} else {
tallied[candidate] = 1
}
if !slices.Contains(eliminated, candidate) {
tallied[candidate]++
voteCount += 1
break
}
}
}
// Eliminate lowest vote getter
minVote := 1000000 //the smallest number of votes received thus far (to find who is in last)
minVote := int(^uint(0) >> 1) //the smallest number of votes received thus far (to find who is in last)
minPerson := make([]string, 0) //the person(s) with the least votes that need removed
for person, vote := range tallied {
if vote < minVote { // this should always be true round one, to set a true "who is in last"
Expand Down Expand Up @@ -293,7 +291,7 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) {
pollResult := make(map[string]int)
cursor, err := Client.Database(db).Collection("votes").Aggregate(ctx, mongo.Pipeline{
{{
Key: "$match", Value: bson.D{
Key: MATCH, Value: bson.D{
{Key: "pollId", Value: pollId},
},
}},
Expand Down Expand Up @@ -328,7 +326,7 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) {
// Get all votes
cursor, err := Client.Database(db).Collection("votes").Aggregate(ctx, mongo.Pipeline{
{{
Key: "$match", Value: bson.D{
Key: MATCH, Value: bson.D{
{Key: "pollId", Value: pollId},
},
}},
Expand All @@ -343,15 +341,6 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) {
return nil, nil
}

func containsValue(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}

// orderOptions takes a RankedVote's options, and returns an ordered list of
// their choices
//
Expand All @@ -366,11 +355,11 @@ func containsValue(slice []string, value string) bool {
func orderOptions(ctx context.Context, options map[string]int) []string {
// Figure out all the ranks they've listed
var ranks []int = make([]int, len(options))
reverse_map := make(map[int]string)
reverseMap := make(map[int]string)
i := 0
for option, rank := range options {
ranks[i] = rank
reverse_map[rank] = option
reverseMap[rank] = option
i += 1
}

Expand All @@ -379,7 +368,7 @@ func orderOptions(ctx context.Context, options map[string]int) []string {
// normalise the ranks for counts that don't start at 1
var choices []string = make([]string, len(ranks))
for idx, rank := range ranks {
choices[idx] = reverse_map[rank]
choices[idx] = reverseMap[rank]
}

return choices
Expand Down