diff --git a/internal/parser/clangtidy/score.go b/internal/parser/clangtidy/score.go index 52bb4e5..3689edb 100644 --- a/internal/parser/clangtidy/score.go +++ b/internal/parser/clangtidy/score.go @@ -2,8 +2,9 @@ package clangtidy import ( "fmt" - "sort" "strings" + + "github.com/joint-online-judge/JOJ3/pkg/utils" ) func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) { @@ -33,21 +34,14 @@ func GetResult(jsonMessages []JsonMessage, conf Conf) (int, string) { } } } - type kv struct { - Key string - Value int - } - var ss []kv - 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 { + 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 score, comment diff --git a/internal/parser/cpplint/parser.go b/internal/parser/cpplint/parser.go index 9d83e42..7c448f5 100644 --- a/internal/parser/cpplint/parser.go +++ b/internal/parser/cpplint/parser.go @@ -5,8 +5,10 @@ import ( "log/slog" "regexp" "strconv" + "strings" "github.com/joint-online-judge/JOJ3/internal/stage" + "github.com/joint-online-judge/JOJ3/pkg/utils" ) type Conf struct { @@ -21,18 +23,19 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { re := regexp.MustCompile(pattern) matches := re.FindAllStringSubmatch(stderr, -1) score := 0 - comment := "" + comment := "### Test results summary\n\n" + categoryCount := map[string]int{} for _, match := range matches { - fileName := match[1] - lineNum, err := strconv.Atoi(match[2]) - if err != nil { - slog.Error("parse lineNum", "error", err) - return stage.ParserResult{ - Score: 0, - Comment: fmt.Sprintf("Unexpected parser error: %s.", err), - } - } - message := match[3] + // fileName := match[1] + // lineNum, err := strconv.Atoi(match[2]) + // if err != nil { + // slog.Error("parse lineNum", "error", err) + // return stage.ParserResult{ + // Score: 0, + // Comment: fmt.Sprintf("Unexpected parser error: %s.", err), + // } + // } + // message := match[3] category := match[4] confidence, err := strconv.Atoi(match[5]) if err != nil { @@ -43,9 +46,21 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { } } score -= confidence - // TODO: add more detailed comment, just re-assemble for now - comment += fmt.Sprintf("%s:%d: %s [%s] [%d]\n", - fileName, lineNum, message, category, confidence) + parts := strings.Split(category, "/") + if len(parts) > 0 { + 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{ Score: score, diff --git a/pkg/utils/map.go b/pkg/utils/map.go new file mode 100644 index 0000000..ba07f87 --- /dev/null +++ b/pkg/utils/map.go @@ -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 +}