From 54d63ae514a0422a906cfc4928c5b07ccb88bba2 Mon Sep 17 00:00:00 2001 From: shaeespring Date: Thu, 25 Dec 2025 17:49:01 -0500 Subject: [PATCH 1/3] min number and cognitive complexity --- database/poll.go | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/database/poll.go b/database/poll.go index 5c26640..9d4769f 100644 --- a/database/poll.go +++ b/database/poll.go @@ -2,6 +2,7 @@ package database import ( "context" + "slices" "sort" "time" @@ -34,6 +35,7 @@ type Poll struct { const POLL_TYPE_SIMPLE = "simple" const POLL_TYPE_RANKED = "ranked" +const MATCH = "$match" func GetPoll(ctx context.Context, id string) (*Poll, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) @@ -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}, }, }}, @@ -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}, }, }}, @@ -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 { + if !slices.Contains(eliminated, candidate) { tallied[candidate]++ - } else { - tallied[candidate] = 1 - } 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" @@ -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}, }, }}, @@ -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}, }, }}, @@ -343,14 +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 From bc9b1b8e12fc43da923d5f44ea34ba85ff926c3d Mon Sep 17 00:00:00 2001 From: shaeespring Date: Thu, 25 Dec 2025 18:49:07 -0500 Subject: [PATCH 2/3] variable name --- database/poll.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/database/poll.go b/database/poll.go index 9d4769f..e7b8a92 100644 --- a/database/poll.go +++ b/database/poll.go @@ -227,14 +227,14 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st // Go over picks until we find a non-eliminated candidate for _, candidate := range picks { if !slices.Contains(eliminated, candidate) { - tallied[candidate]++ + tallied[candidate]++ voteCount += 1 break } } } // Eliminate lowest vote getter - minVote := int(^uint(0)>>1) //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" @@ -341,7 +341,6 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) { return nil, nil } - // orderOptions takes a RankedVote's options, and returns an ordered list of // their choices // @@ -356,11 +355,11 @@ func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) { 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 } @@ -369,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 From b176c2c49d1b6e83944f47301586d92763a6ae69 Mon Sep 17 00:00:00 2001 From: shaeespring Date: Tue, 30 Dec 2025 15:21:15 -0500 Subject: [PATCH 3/3] revert match constant and import math --- database/poll.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/database/poll.go b/database/poll.go index e7b8a92..485d648 100644 --- a/database/poll.go +++ b/database/poll.go @@ -2,6 +2,7 @@ package database import ( "context" + "math" "slices" "sort" "time" @@ -35,7 +36,6 @@ type Poll struct { const POLL_TYPE_SIMPLE = "simple" const POLL_TYPE_RANKED = "ranked" -const MATCH = "$match" func GetPoll(ctx context.Context, id string) (*Poll, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) @@ -145,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}, }, }}, @@ -169,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}, }, }}, @@ -234,7 +234,7 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st } } // Eliminate lowest vote getter - minVote := int(^uint(0) >> 1) //the smallest number of votes received thus far (to find who is in last) + minVote := math.MaxInt //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" @@ -291,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}, }, }}, @@ -326,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}, }, }},