From 5569703dfb73a3b4b9d32f543c1da3077e5d79bd Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sun, 3 Nov 2024 20:12:13 -0500 Subject: [PATCH] feat(parser/cpplint): show occurrences summary --- internal/parser/cpplint/parser.go | 64 +++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/internal/parser/cpplint/parser.go b/internal/parser/cpplint/parser.go index d3a0c3f..a6beac9 100644 --- a/internal/parser/cpplint/parser.go +++ b/internal/parser/cpplint/parser.go @@ -4,6 +4,7 @@ import ( "fmt" "log/slog" "regexp" + "sort" "strconv" "strings" @@ -30,13 +31,15 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { stderr := executorResult.Files[conf.Stderr] pattern := `(.+):(\d+): (.+) \[(.+)\] \[(\d)]\n` re := regexp.MustCompile(pattern) - matches := re.FindAllStringSubmatch(stderr, -1) + regexMatches := re.FindAllStringSubmatch(stderr, -1) score := conf.Score comment := "### Test results summary\n\n" - categoryCount := map[string]int{} - for _, match := range matches { - // fileName := match[1] - // lineNum, err := strconv.Atoi(match[2]) + categoryCount := make(map[string]int) + matchCount := make(map[string]int) + scoreChange := make(map[string]int) + for _, regexMatch := range regexMatches { + // fileName := regexMatch[1] + // lineNum, err := strconv.Atoi(regexMatch[2]) // if err != nil { // slog.Error("parse lineNum", "error", err) // return stage.ParserResult{ @@ -44,11 +47,11 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { // Comment: fmt.Sprintf("Unexpected parser error: %s.", err), // } // } - // message := match[3] - category := match[4] + // message := regexMatch[3] + category := regexMatch[4] // TODO: remove me if len(conf.Matches) == 0 { - confidence, err := strconv.Atoi(match[5]) + confidence, err := strconv.Atoi(regexMatch[5]) if err != nil { slog.Error("parse confidence", "error", err) return stage.ParserResult{ @@ -58,19 +61,23 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { } score -= confidence } - for _, match := range conf.Matches { - for _, keyword := range match.Keywords { - if strings.Contains(category, keyword) { - score -= match.Score - } - } - } parts := strings.Split(category, "/") if len(parts) > 0 { category := parts[0] categoryCount[category] += 1 } + // TODO: remove me ends + for _, match := range conf.Matches { + for _, keyword := range match.Keywords { + if strings.Contains(category, keyword) { + matchCount[keyword] += 1 + scoreChange[keyword] += -match.Score + score += -match.Score + } + } + } } + // TODO: remove me sortedMap := utils.SortMap(categoryCount, func(i, j utils.Pair[string, int]) bool { if i.Value == j.Value { @@ -81,6 +88,33 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { for i, kv := range sortedMap { comment += fmt.Sprintf("%d. %s: %d\n", i+1, kv.Key, kv.Value) } + // TODO: remove me ends + type Result struct { + Keyword string + Count int + ScoreChange int + } + var results []Result + for keyword, count := range matchCount { + results = append(results, Result{ + Keyword: keyword, + Count: count, + ScoreChange: scoreChange[keyword], + }) + } + sort.Slice(results, func(i, j int) bool { + if results[i].ScoreChange != results[j].ScoreChange { + return results[i].ScoreChange < results[j].ScoreChange + } + if results[i].Count != results[j].Count { + return results[i].Count > results[j].Count + } + return results[i].Keyword < results[j].Keyword + }) + for i, result := range results { + comment += fmt.Sprintf("%d. `%s`: %d occurrence(s), %d point(s)\n", + i+1, result.Keyword, result.Count, result.ScoreChange) + } return stage.ParserResult{ Score: score, Comment: comment,