Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add total results #270

Merged
merged 6 commits into from
Nov 26, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
We've fackin done it
euanwm committed Nov 23, 2023

Verified

This commit was signed with the committer’s verified signature.
billyjacoby Billy Jacoby
commit 3fb0f02969f99974b6181a8a0d415e2205da652a
17 changes: 15 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import (

var processedLeaderboard structs.LeaderboardData

var lifterData = lifter.Build()
// var lifterData = lifter.Build()

var QueryCache dbtools.QueryCache

@@ -139,7 +139,7 @@ func setupCORS(r *gin.Engine) {

func buildServer() *gin.Engine {
log.Println("Starting server...")
go dbtools.BuildDatabase(&processedLeaderboard)
dbtools.BuildDatabase(&processedLeaderboard)
r := gin.Default()
setupCORS(r)
r.GET("test", getTest)
@@ -151,8 +151,21 @@ func buildServer() *gin.Engine {
return r
}

// CacheMeOutsideHowBoutDat - Precaches data on startup on a separate thread due to container timeout constraints.
func CacheMeOutsideHowBoutDat() {
log.Println("Precaching data...")
for n, query := range dbtools.PreCacheQuery {
log.Println("Caching query: ", n)
_, _ = QueryCache.CheckQuery(query)
liftdata := processedLeaderboard.Select(query.SortBy)
dbtools.PreCacheFilter(*liftdata, query, dbtools.WeightClassList[query.WeightClass], &QueryCache)
}
log.Println("Caching complete")
}

func main() {
apiServer := buildServer()
go CacheMeOutsideHowBoutDat()
err := apiServer.Run()
if err != nil {
log.Fatal("Failed to run server")
4 changes: 3 additions & 1 deletion backend/dbtools/cache_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dbtools

import "backend/structs"
import (
"backend/structs"
)

type QueryCache struct {
Store []Query
38 changes: 38 additions & 0 deletions backend/dbtools/precache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dbtools

import (
"backend/enum"
"backend/structs"
)

// PreCacheQuery - Queries to pre-cache on startup.
var PreCacheQuery = []structs.LeaderboardPayload{
{
SortBy: "total",
Federation: "allfeds",
WeightClass: "MALL",
StartDate: enum.ZeroDate,
EndDate: enum.MaxDate,
},
{
SortBy: "total",
Federation: "allfeds",
WeightClass: "FALL",
StartDate: enum.ZeroDate,
EndDate: enum.MaxDate,
},
{
SortBy: "sinclair",
Federation: "allfeds",
WeightClass: "MALL",
StartDate: enum.ZeroDate,
EndDate: enum.MaxDate,
},
{
SortBy: "sinclair",
Federation: "allfeds",
WeightClass: "FALL",
StartDate: enum.ZeroDate,
EndDate: enum.MaxDate,
},
}
65 changes: 44 additions & 21 deletions backend/dbtools/sortby.go
Original file line number Diff line number Diff line change
@@ -4,14 +4,14 @@ import (
"backend/enum"
"backend/structs"
"backend/utilities"
"log"
"sort"
"time"
)

func removeFollowingLifts(bigData []structs.Entry) (filteredData []structs.Entry) {
func removeFollowingLifts(bigData []structs.Entry, pos *[]int) (filteredData []structs.Entry) {
var names []string
var position []int
// todo: refactor this so it's faster / more efficient
for i, d := range bigData {
if !utilities.Contains(names, d.Name) {
position = append(position, i)
@@ -26,50 +26,73 @@ func removeFollowingLifts(bigData []structs.Entry) (filteredData []structs.Entry

// Filter - Returns a slice of structs relating to the selected filter selection
func Filter(bigData []structs.Entry, filterQuery structs.LeaderboardPayload, weightCat structs.WeightClass, cache *QueryCache) (filteredData structs.LeaderboardResponse) {
exists, liftPositions := cache.CheckQuery(filterQuery)
exists, positions := cache.CheckQuery(filterQuery)

if exists {
log.Println("Cache hit")
filteredData.Data, filteredData.Size = fetchLifts(&bigData, liftPositions, filterQuery.Start, filterQuery.Stop)
filteredData.Data, filteredData.Size = fetchLifts(&bigData, positions, &filterQuery)
return
}

var liftPostions []int
var names []string
var liftPtr *structs.Entry
var liftPositions []int
for idx, lift := range bigData {
liftptr := &bigData[idx]
if getGender(liftptr) == weightCat.Gender {
liftPtr = &bigData[idx]
if getGender(liftPtr) == weightCat.Gender && !utilities.Contains(names, lift.Name) {
if lift.SelectedFederation(filterQuery.Federation) && lift.WithinWeightClass(WeightClassList[filterQuery.WeightClass].Gender, weightCat) && lift.WithinDates(filterQuery.StartDate, filterQuery.EndDate) {
liftPostions = append(liftPostions, idx)
liftPositions = append(liftPositions, idx)
names = append(names, lift.Name)
filteredData.Data = append(filteredData.Data, lift)
}
}
}
cache.AddQuery(filterQuery, liftPositions)

if filterQuery.Stop > len(liftPositions) {
filterQuery.Stop = len(liftPositions)
}

cache.AddQuery(filterQuery, liftPostions)
if filterQuery.Start > len(liftPositions) {
filterQuery.Start = len(liftPositions)
}

filteredData.Data, filteredData.Size = fetchLifts(&bigData, liftPostions, filterQuery.Start, filterQuery.Stop)
filteredData.Size = len(liftPositions)
filteredData.Data = filteredData.Data[filterQuery.Start:filterQuery.Stop]
return
}

func PreCacheFilter(bigData []structs.Entry, filterQuery structs.LeaderboardPayload, weightCat structs.WeightClass, cache *QueryCache) {
var names []string
var liftPtr *structs.Entry
var liftPositions []int
for idx, lift := range bigData {
liftPtr = &bigData[idx]
if getGender(liftPtr) == weightCat.Gender && !utilities.Contains(names, lift.Name) {
if lift.SelectedFederation(filterQuery.Federation) && lift.WithinWeightClass(WeightClassList[filterQuery.WeightClass].Gender, weightCat) && lift.WithinDates(filterQuery.StartDate, filterQuery.EndDate) {
liftPositions = append(liftPositions, idx)
names = append(names, lift.Name)
}
}
}
cache.AddQuery(filterQuery, liftPositions)
}

// fetchLifts - Returns a slice of structs relating to the selected filter selection, it will also remove any duplicate entries.
func fetchLifts(bigData *[]structs.Entry, pos []int, start int, stop int) (lifts []structs.Entry, size int) {
log.Println("Fetching...")
func fetchLifts(bigData *[]structs.Entry, pos []int, query *structs.LeaderboardPayload) (lifts []structs.Entry, size int) {
for _, p := range pos {
lifts = append(lifts, (*bigData)[p])
}
log.Println("Removing dupies")
lifts = removeFollowingLifts(lifts)

if stop > len(lifts) {
stop = len(lifts)
if query.Stop > len(lifts) {
query.Stop = len(lifts)
}

if start > len(lifts) {
start = len(lifts)
if query.Start > len(lifts) {
query.Start = len(lifts)
}

size = len(lifts)
lifts = lifts[start:stop]
log.Println("Fetched")
lifts = lifts[query.Start:query.Stop]
return
}