Skip to content

Commit

Permalink
Refactor scoring charts to cleanup code and data label display
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc M. Adkins committed Aug 13, 2024
1 parent 7e1695b commit 0aee987
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 546 deletions.
504 changes: 0 additions & 504 deletions cmd/server/chart-score.go

This file was deleted.

49 changes: 28 additions & 21 deletions cmd/server/chart-bar.go → cmd/server/chart/bar.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package main
package chart

import (
"log/slog"
"net/http"
"strings"
"sync"

"github.com/gin-gonic/gin"
"github.com/vicanso/go-charts/v2"

"github.com/madkins23/go-slog/internal/data"
)

var (
chartCache = make(map[string][]byte)
chartCacheMutex sync.Mutex
)

// chartFunction generates an SVG chart for the current object tags.
func chartFunction(c *gin.Context) {
// Bar generates an SVG chart for the current object tags.
func Bar(c *gin.Context, bench *data.Benchmarks) {
itemArg := strings.TrimSuffix(c.Param("item"), ".svg")
item, err := data.BenchItemsString(itemArg)
if err != nil {
Expand All @@ -28,16 +22,16 @@ func chartFunction(c *gin.Context) {
}
tag := c.Param("tag")
cacheKey := tag + ":" + item.String()
chartCacheMutex.Lock()
ch, found := chartCache[cacheKey]
chartCacheMutex.Unlock()
CacheMutex.Lock()
ch, found := Cache[cacheKey]
CacheMutex.Unlock()
if !found {
var labels []string
var values []float64
if records := bench.HandlerRecords(data.TestTag(tag)); records != nil {
labels, values = chartTest(records, item)
} else if records := bench.TestRecords(data.HandlerTag(tag)); records != nil {
labels, values = chartHandler(records, item)
if records := bench.HandlerRecordsFor(data.TestTag(tag)); records != nil {
labels, values = chartTest(bench, records, item)
} else if records := bench.TestRecordsFor(data.HandlerTag(tag)); records != nil {
labels, values = chartHandler(bench, records, item)
} else {
slog.Error("Neither handler nor benchmark records found", "fn", "chartFunction")
c.HTML(http.StatusBadRequest, "pageFunction", gin.H{
Expand Down Expand Up @@ -72,15 +66,15 @@ func chartFunction(c *gin.Context) {
if err != nil {
panic(err)
}
chartCacheMutex.Lock()
chartCache[cacheKey] = ch
chartCacheMutex.Unlock()
CacheMutex.Lock()
Cache[cacheKey] = ch
CacheMutex.Unlock()
}
c.Data(http.StatusOK, "image/svg+xml", ch)
}

// charTest returns labels and values for a Test chart.
func chartTest(records data.HandlerRecords, item data.BenchItems) (labels []string, values []float64) {
func chartTest(bench *data.Benchmarks, records data.HandlerRecords, item data.BenchItems) (labels []string, values []float64) {
labels = make([]string, 0, len(records))
values = make([]float64, 0, len(records))
for _, tag := range bench.HandlerTags() {
Expand All @@ -95,7 +89,7 @@ func chartTest(records data.HandlerRecords, item data.BenchItems) (labels []stri
}

// chartHandler returns labels and values for a Handler chart.
func chartHandler(records data.TestRecords, item data.BenchItems) (labels []string, values []float64) {
func chartHandler(bench *data.Benchmarks, records data.TestRecords, item data.BenchItems) (labels []string, values []float64) {
labels = make([]string, 0, len(records))
values = make([]float64, 0, len(records))
for _, tag := range bench.TestTags() {
Expand All @@ -108,3 +102,16 @@ func chartHandler(records data.TestRecords, item data.BenchItems) (labels []stri
reverse(values)
return
}

// -----------------------------------------------------------------------------

// reverse an array.
func reverse[T any](array []T) {
i := 0
j := len(array) - 1
for i < j {
array[i], array[j] = array[j], array[i]
i++
j--
}
}
8 changes: 8 additions & 0 deletions cmd/server/chart/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package chart

import "sync"

var (
Cache = make(map[string][]byte)
CacheMutex sync.Mutex
)
Loading

0 comments on commit 0aee987

Please sign in to comment.