feat(parser/cpplint): better summary
Some checks failed
build / trigger-build-image (push) Blocked by required conditions
build / build (push) Has been cancelled
submodules sync / sync (push) Has been cancelled

This commit is contained in:
张泊明518370910136 2024-11-01 21:21:15 -04:00
parent bbde1136ad
commit 4b848bf1f1
GPG Key ID: D47306D7062CDA9D
3 changed files with 58 additions and 30 deletions

View File

@ -2,8 +2,9 @@ package clangtidy
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/joint-online-judge/JOJ3/pkg/utils"
) )
func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) { func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) {
@ -33,21 +34,14 @@ func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) {
} }
} }
} }
type kv struct { sortedMap := utils.SortMap(categoryCount,
Key string func(i, j utils.Pair[string, int]) bool {
Value int if i.Value == j.Value {
return i.Key < j.Key
} }
var ss []kv return i.Value > j.Value
for k, v := range categoryCount {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
if ss[i].Value == ss[j].Value {
return ss[i].Key < ss[j].Key
}
return ss[i].Value > ss[j].Value
}) })
for i, kv := range ss { for i, kv := range sortedMap {
comment += fmt.Sprintf("%d. %s: %d\n", i+1, kv.Key, kv.Value) comment += fmt.Sprintf("%d. %s: %d\n", i+1, kv.Key, kv.Value)
} }
return score, comment return score, comment

View File

@ -5,8 +5,10 @@ import (
"log/slog" "log/slog"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"github.com/joint-online-judge/JOJ3/internal/stage" "github.com/joint-online-judge/JOJ3/internal/stage"
"github.com/joint-online-judge/JOJ3/pkg/utils"
) )
type Conf struct { type Conf struct {
@ -21,18 +23,19 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
re := regexp.MustCompile(pattern) re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(stderr, -1) matches := re.FindAllStringSubmatch(stderr, -1)
score := 0 score := 0
comment := "" comment := "### Test results summary\n\n"
categoryCount := map[string]int{}
for _, match := range matches { for _, match := range matches {
fileName := match[1] // fileName := match[1]
lineNum, err := strconv.Atoi(match[2]) // lineNum, err := strconv.Atoi(match[2])
if err != nil { // if err != nil {
slog.Error("parse lineNum", "error", err) // slog.Error("parse lineNum", "error", err)
return stage.ParserResult{ // return stage.ParserResult{
Score: 0, // Score: 0,
Comment: fmt.Sprintf("Unexpected parser error: %s.", err), // Comment: fmt.Sprintf("Unexpected parser error: %s.", err),
} // }
} // }
message := match[3] // message := match[3]
category := match[4] category := match[4]
confidence, err := strconv.Atoi(match[5]) confidence, err := strconv.Atoi(match[5])
if err != nil { if err != nil {
@ -43,9 +46,21 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
} }
} }
score -= confidence score -= confidence
// TODO: add more detailed comment, just re-assemble for now parts := strings.Split(category, "/")
comment += fmt.Sprintf("%s:%d: %s [%s] [%d]\n", if len(parts) > 0 {
fileName, lineNum, message, category, confidence) category := parts[0]
categoryCount[category] += 1
}
}
sortedMap := utils.SortMap(categoryCount,
func(i, j utils.Pair[string, int]) bool {
if i.Value == j.Value {
return i.Key < j.Key
}
return i.Value > j.Value
})
for i, kv := range sortedMap {
comment += fmt.Sprintf("%d. %s: %d\n", i+1, kv.Key, kv.Value)
} }
return stage.ParserResult{ return stage.ParserResult{
Score: score, Score: score,

19
pkg/utils/map.go Normal file
View File

@ -0,0 +1,19 @@
package utils
import "sort"
type Pair[K comparable, V any] struct {
Key K
Value V
}
func SortMap[K comparable, V any](m map[K]V, less func(i, j Pair[K, V]) bool) []Pair[K, V] {
pairs := make([]Pair[K, V], 0, len(m))
for k, v := range m {
pairs = append(pairs, Pair[K, V]{k, v})
}
sort.Slice(pairs, func(i, j int) bool {
return less(pairs[i], pairs[j])
})
return pairs
}