Skip to content

Commit

Permalink
feat: support sorted model analysis chart
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jan 23, 2024
1 parent 71e26ad commit 651d529
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions admin/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func GetModelData(cache *redis.Client) ModelChartForm {
}
}

func GetSortedModelData(cache *redis.Client) ModelChartForm {
form := GetModelData(cache)
data := utils.Sort(form.Value, func(a ModelData, b ModelData) bool {
return utils.Sum(a.Data) > utils.Sum(b.Data)
})

form.Value = data

return form
}

func GetRequestData(cache *redis.Client) RequestChartForm {
dates := getDays(7)

Expand Down
2 changes: 1 addition & 1 deletion admin/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func InfoAPI(c *gin.Context) {

func ModelAnalysisAPI(c *gin.Context) {
cache := utils.GetCacheFromContext(c)
c.JSON(http.StatusOK, GetModelData(cache))
c.JSON(http.StatusOK, GetSortedModelData(cache))
}

func RequestAnalysisAPI(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/admin/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ export const modelColorMapper: Record<string, string> = {
};

export function getModelColor(model: string): string {
return modelColorMapper[model] || "#000000";
return modelColorMapper[model] || "#111";
}
37 changes: 37 additions & 0 deletions utils/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,43 @@ func InsertChannel[T any](ch chan T, value T, index int) {
}
}

func Sort[T any](arr []T, compare func(a, b T) bool) []T {
if len(arr) <= 1 {
return arr
}

var result []T
var temp []T
var hasFirst bool
var first T

for _, item := range arr {
if !hasFirst {
first = item
hasFirst = true
continue
}

if compare(item, first) {
temp = append(temp, item)
} else {
result = append(result, first)
result = append(result, Sort(temp, compare)...)
first = item
temp = []T{}
}
}

if len(temp) > 0 {
result = append(result, first)
result = append(result, Sort(temp, compare)...)
} else if hasFirst {
result = append(result, first)
}

return result
}

func Each[T any, U any](arr []T, f func(T) U) []U {
var res []U
for _, v := range arr {
Expand Down

0 comments on commit 651d529

Please sign in to comment.