This commit is contained in:
@@ -1055,13 +1055,31 @@ func appErrorPage(c *gin.Context, errorCode int, errorMessage string) {
|
||||
})
|
||||
}
|
||||
|
||||
func arrangeUserStatistics(userStatistics []database.GetUserStatisticsRow) gin.H {
|
||||
type UserStatistics struct {
|
||||
WPM UserTimeStatistics
|
||||
Duration UserTimeStatistics
|
||||
Words UserTimeStatistics
|
||||
}
|
||||
|
||||
type UserTimeStatistics struct {
|
||||
All []UserStatisticEntry
|
||||
Year []UserStatisticEntry
|
||||
Month []UserStatisticEntry
|
||||
Week []UserStatisticEntry
|
||||
}
|
||||
|
||||
type UserStatisticEntry struct {
|
||||
UserID string
|
||||
Value string
|
||||
}
|
||||
|
||||
func arrangeUserStatistics(userStatistics []database.GetUserStatisticsRow) UserStatistics {
|
||||
// Item Sorter
|
||||
sortItem := func(userStatistics []database.GetUserStatisticsRow, key string, less func(i int, j int) bool) []map[string]any {
|
||||
sortItem := func(userStatistics []database.GetUserStatisticsRow, key string, less func(i int, j int) bool) []UserStatisticEntry {
|
||||
sortedData := append([]database.GetUserStatisticsRow(nil), userStatistics...)
|
||||
sort.SliceStable(sortedData, less)
|
||||
|
||||
newData := make([]map[string]any, 0)
|
||||
newData := make([]UserStatisticEntry, 0)
|
||||
for _, item := range sortedData {
|
||||
v := reflect.Indirect(reflect.ValueOf(item))
|
||||
|
||||
@@ -1077,55 +1095,55 @@ func arrangeUserStatistics(userStatistics []database.GetUserStatisticsRow) gin.H
|
||||
value = niceNumbers(rawVal)
|
||||
}
|
||||
|
||||
newData = append(newData, map[string]any{
|
||||
"UserID": item.UserID,
|
||||
"Value": value,
|
||||
newData = append(newData, UserStatisticEntry{
|
||||
UserID: item.UserID,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
return newData
|
||||
}
|
||||
|
||||
return gin.H{
|
||||
"WPM": gin.H{
|
||||
"All": sortItem(userStatistics, "TotalWpm", func(i, j int) bool {
|
||||
return UserStatistics{
|
||||
WPM: UserTimeStatistics{
|
||||
All: sortItem(userStatistics, "TotalWpm", func(i, j int) bool {
|
||||
return userStatistics[i].TotalWpm > userStatistics[j].TotalWpm
|
||||
}),
|
||||
"Year": sortItem(userStatistics, "YearlyWpm", func(i, j int) bool {
|
||||
Year: sortItem(userStatistics, "YearlyWpm", func(i, j int) bool {
|
||||
return userStatistics[i].YearlyWpm > userStatistics[j].YearlyWpm
|
||||
}),
|
||||
"Month": sortItem(userStatistics, "MonthlyWpm", func(i, j int) bool {
|
||||
Month: sortItem(userStatistics, "MonthlyWpm", func(i, j int) bool {
|
||||
return userStatistics[i].MonthlyWpm > userStatistics[j].MonthlyWpm
|
||||
}),
|
||||
"Week": sortItem(userStatistics, "WeeklyWpm", func(i, j int) bool {
|
||||
Week: sortItem(userStatistics, "WeeklyWpm", func(i, j int) bool {
|
||||
return userStatistics[i].WeeklyWpm > userStatistics[j].WeeklyWpm
|
||||
}),
|
||||
},
|
||||
"Duration": gin.H{
|
||||
"All": sortItem(userStatistics, "TotalSeconds", func(i, j int) bool {
|
||||
Duration: UserTimeStatistics{
|
||||
All: sortItem(userStatistics, "TotalSeconds", func(i, j int) bool {
|
||||
return userStatistics[i].TotalSeconds > userStatistics[j].TotalSeconds
|
||||
}),
|
||||
"Year": sortItem(userStatistics, "YearlySeconds", func(i, j int) bool {
|
||||
Year: sortItem(userStatistics, "YearlySeconds", func(i, j int) bool {
|
||||
return userStatistics[i].YearlySeconds > userStatistics[j].YearlySeconds
|
||||
}),
|
||||
"Month": sortItem(userStatistics, "MonthlySeconds", func(i, j int) bool {
|
||||
Month: sortItem(userStatistics, "MonthlySeconds", func(i, j int) bool {
|
||||
return userStatistics[i].MonthlySeconds > userStatistics[j].MonthlySeconds
|
||||
}),
|
||||
"Week": sortItem(userStatistics, "WeeklySeconds", func(i, j int) bool {
|
||||
Week: sortItem(userStatistics, "WeeklySeconds", func(i, j int) bool {
|
||||
return userStatistics[i].WeeklySeconds > userStatistics[j].WeeklySeconds
|
||||
}),
|
||||
},
|
||||
"Words": gin.H{
|
||||
"All": sortItem(userStatistics, "TotalWordsRead", func(i, j int) bool {
|
||||
Words: UserTimeStatistics{
|
||||
All: sortItem(userStatistics, "TotalWordsRead", func(i, j int) bool {
|
||||
return userStatistics[i].TotalWordsRead > userStatistics[j].TotalWordsRead
|
||||
}),
|
||||
"Year": sortItem(userStatistics, "YearlyWordsRead", func(i, j int) bool {
|
||||
Year: sortItem(userStatistics, "YearlyWordsRead", func(i, j int) bool {
|
||||
return userStatistics[i].YearlyWordsRead > userStatistics[j].YearlyWordsRead
|
||||
}),
|
||||
"Month": sortItem(userStatistics, "MonthlyWordsRead", func(i, j int) bool {
|
||||
Month: sortItem(userStatistics, "MonthlyWordsRead", func(i, j int) bool {
|
||||
return userStatistics[i].MonthlyWordsRead > userStatistics[j].MonthlyWordsRead
|
||||
}),
|
||||
"Week": sortItem(userStatistics, "WeeklyWordsRead", func(i, j int) bool {
|
||||
Week: sortItem(userStatistics, "WeeklyWordsRead", func(i, j int) bool {
|
||||
return userStatistics[i].WeeklyWordsRead > userStatistics[j].WeeklyWordsRead
|
||||
}),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user