Skip to content

Commit

Permalink
fix: improve log analysis insertion and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Apr 15, 2024
1 parent 3c14ac2 commit 192a295
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
50 changes: 25 additions & 25 deletions server/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"log"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"

"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/nedpals/bugbuddy/server/daemon"
"github.com/nedpals/bugbuddy/server/daemon/types"
"github.com/nedpals/bugbuddy/server/executor"
"github.com/nedpals/bugbuddy/server/helpers"
"github.com/nedpals/bugbuddy/server/logger"
log_analyzer "github.com/nedpals/bugbuddy/server/logger/analyzer"
errorquotient "github.com/nedpals/bugbuddy/server/logger/analyzer/error_quotient"
la_nearest "github.com/nedpals/bugbuddy/server/logger/analyzer/nearest"
red "github.com/nedpals/bugbuddy/server/logger/analyzer/repeated_error_density"
timetosolve "github.com/nedpals/bugbuddy/server/logger/analyzer/time_to_solve"
"github.com/nedpals/bugbuddy/server/lsp_server"
Expand Down Expand Up @@ -239,29 +241,20 @@ type analyzerResultEntry struct {
}

func (a *analyzerResultEntry) Write(name string, filePath string, value any) {
filePath = strings.TrimSpace(filePath)

// check if the filePath is already in the list
if _, ok := a.FilenamesIndices[filePath]; !ok {
// check if the filePath is already an alias
if alias, ok := a.FilenameAliases[filePath]; ok {
// do not mutate the original file path
filePath = alias
} else if found := fuzzy.RankFindFold(filePath, a.Filenames); len(found) != 0 {
// find the closest file name first before adding the value
foundPath := found[0].Target

// check if the found path is a prefix of the file path
if found[0].Distance <= 5 && len(filePath) > len(foundPath) && strings.HasPrefix(filePath, foundPath) {
// if it is, replace the found path with the file path
a.FilenameAliases[foundPath] = filePath
a.FilenamesIndices[filePath] = a.FilenamesIndices[foundPath]
delete(a.FilenamesIndices, foundPath)

// replace the found path with the file path
a.Filenames[a.FilenamesIndices[filePath]] = filePath
} else {
// if it is not, add the file path
a.FilenamesIndices[filePath] = len(a.Filenames)
a.Filenames = append(a.Filenames, filePath)
}
} else {
} else if nearest := la_nearest.FilenameNearest(filePath, a.FilenamesIndices, a.Filenames); nearest != filePath && strings.HasPrefix(filePath, nearest) {
// if it is, replace the found path with the file path
a.Filenames[a.FilenamesIndices[nearest]] = filePath
a.FilenameAliases[nearest] = filePath
a.FilenamesIndices[filePath] = a.FilenamesIndices[nearest]
delete(a.FilenamesIndices, nearest)
} else if _, ok := a.FilenamesIndices[filePath]; !ok {
// if it is not, add the file path
a.FilenamesIndices[filePath] = len(a.Filenames)
a.Filenames = append(a.Filenames, filePath)
Expand Down Expand Up @@ -418,16 +411,23 @@ var analyzeLogCmd = &cobra.Command{
sheet.SetColAutoWidth(aCellRow+2, adjustToTextWidth)
}

for fileIdx, filePath := range result.Filenames {
// sort filenames
sortedFilenames := slices.Clone(result.Filenames)
sort.Slice(sortedFilenames, func(i, j int) bool {
return sortedFilenames[i] < sortedFilenames[j]
})

for idx, filePath := range sortedFilenames {
if len(strings.TrimSpace(filePath)) == 0 {
continue
}

row, _ := sheet.Row(fileIdx + 1)
fileIdx := result.FilenamesIndices[filePath]
row, _ := sheet.Row(idx + 1)
row.AddCell().SetValue(filePath)

for _, analyzerName := range selectedAnalyzers {
cell, _ := sheet.Cell(fileIdx+1, analyzerCellLocations[analyzerName])
cell, _ := sheet.Cell(idx+1, analyzerCellLocations[analyzerName])

switch analyzerName {
case "eq":
Expand All @@ -437,7 +437,7 @@ var analyzeLogCmd = &cobra.Command{
case "tts":
cell.SetValue(result.TimeToSolve[fileIdx].Seconds())

hhMmSsCell, _ := sheet.Cell(fileIdx+1, analyzerCellLocations[analyzerName]+1)
hhMmSsCell, _ := sheet.Cell(idx+1, analyzerCellLocations[analyzerName]+1)
hhMmSsCell.SetValue(formatDuration(result.TimeToSolve[fileIdx]))
}
}
Expand Down
22 changes: 2 additions & 20 deletions server/logger/analyzer/internal/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package internal
import (
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/nedpals/bugbuddy/server/logger/analyzer/nearest"
)

type ResultStore[T any] struct {
Expand All @@ -14,25 +14,7 @@ type ResultStore[T any] struct {
}

func (r *ResultStore[T]) FilenameNearest(filePath string) string {
if _, ok := r.FilenamesIndices[filePath]; !ok {
// check if the filePath is already an alias
found := fuzzy.RankFindNormalizedFold(filePath, r.Filenames)

// if the file path is not found, return the file path
if len(found) == 0 {
return filePath
}

// find the closest file name first before adding the value
foundPath := found[0].Target
distance := found[0].Distance

if distance <= 6 && (strings.HasPrefix(foundPath, filePath) || strings.HasPrefix(filePath, foundPath)) {
return foundPath
}
}

return filePath
return nearest.FilenameNearest(filePath, r.FilenamesIndices, r.Filenames)
}

func (r *ResultStore[T]) checkAndUpdateFilename(filePath string) string {
Expand Down
31 changes: 31 additions & 0 deletions server/logger/analyzer/nearest/nearest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nearest

import (
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
)

const MAX_CLOSEST_FILE_DISTANCE = 6

func FilenameNearest(filePath string, indices map[string]int, filenames []string) string {
if _, ok := indices[filePath]; !ok {
// check if the filePath is already an alias
found := fuzzy.RankFindNormalizedFold(filePath, filenames)

// if the file path is not found, return the file path
if len(found) == 0 {
return filePath
}

// find the closest file name first before adding the value
foundPath := found[0].Target
distance := found[0].Distance

if distance <= MAX_CLOSEST_FILE_DISTANCE && (strings.HasPrefix(foundPath, filePath) || strings.HasPrefix(filePath, foundPath)) {
return foundPath
}
}

return filePath
}

0 comments on commit 192a295

Please sign in to comment.