Skip to content

Commit 59f0dc9

Browse files
committed
Update analyze.go
1 parent 103873d commit 59f0dc9

1 file changed

Lines changed: 34 additions & 15 deletions

File tree

cli/cmd/analyze.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ type tableData struct {
2626
const (
2727
analyzeShort = "Analyze CloudQuery log files to detect stalled tables and calculate execution times"
2828
analyzeExample = `# Analyze a CloudQuery log file to find stalled tables
29-
cloudquery analyze --file path/to/cloudquery.log`
29+
cloudquery analyze --file path/to/cloudquery.log
30+
31+
# Analyze data for a specific invocation ID only
32+
cloudquery analyze --file path/to/cloudquery.log --invocation-id abc123`
3033
)
3134

3235
func NewCmdAnalyze() *cobra.Command {
@@ -38,6 +41,7 @@ func NewCmdAnalyze() *cobra.Command {
3841
RunE: analyze,
3942
}
4043
cmd.Flags().StringP("file", "f", "", "Path to the CloudQuery log file")
44+
cmd.Flags().StringP("invocation-id", "i", "", "Only analyze data for a specific invocation ID")
4145
_ = cmd.MarkFlagRequired("file")
4246

4347
return cmd
@@ -48,7 +52,13 @@ func analyze(cmd *cobra.Command, args []string) error {
4852
if err != nil {
4953
return err
5054
}
51-
return analyzeLogFile(logFile)
55+
56+
invocationID, err := cmd.Flags().GetString("invocation-id")
57+
if err != nil {
58+
return err
59+
}
60+
61+
return analyzeLogFile(logFile, invocationID)
5262
}
5363

5464
func extractInvocationID(line string) string {
@@ -59,9 +69,9 @@ func extractInvocationID(line string) string {
5969
return ""
6070
}
6171

62-
func analyzeLogFile(filePath string) error {
72+
func analyzeLogFile(filePath string, filterInvocationID string) error {
6373
// Define the regular expression patterns
64-
patternEnd := regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)\s+(INF|ERR|WRN)\s+(.*)\s+client=(.*)\s(.*)+errors=(\d+)?\s+module=([a-zA-Z-]+)?\s+resources=(\d+)?\s+table=(\w+)?`)
74+
patternEnd := regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)\s+(INF|ERR|WRN)\s+(.*)\s+client=(.*)\s+(.*)\s+errors=(\d+)?\s+(.*)+\s+module=([a-zA-Z-]+)?\s+resources=(\d+)?\s+table=(\w+)?`)
6575
patternStart := regexp.MustCompile(`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)\s+(INF|ERR|WRN)\s+(.*)\s+client=(.*)\s+(.*)\s+module=([a-zA-Z-]+)?\s+table=(\w+)?`)
6676

6777
fmt.Printf("Analyzing log file: %s\n", filePath)
@@ -79,11 +89,19 @@ func analyzeLogFile(filePath string) error {
7989
for scanner.Scan() {
8090
line := scanner.Text()
8191

92+
// Extract invocation ID for filtering
93+
extractedInvocationID := extractInvocationID(line)
94+
95+
// Skip if we're filtering by invocation ID and this line doesn't match
96+
if filterInvocationID != "" && extractedInvocationID != filterInvocationID {
97+
continue
98+
}
99+
82100
// Try to match end pattern
83101
if matches := patternEnd.FindStringSubmatch(line); matches != nil {
84102
allData = append(allData, logEntry{
85-
invocationID: extractInvocationID(line),
86-
key: matches[4] + matches[9],
103+
invocationID: extractedInvocationID,
104+
key: matches[4] + matches[10],
87105
ts: matches[1],
88106
typ: "end",
89107
})
@@ -93,7 +111,7 @@ func analyzeLogFile(filePath string) error {
93111
// Try to match start pattern
94112
if matches := patternStart.FindStringSubmatch(line); matches != nil {
95113
allData = append(allData, logEntry{
96-
invocationID: extractInvocationID(line),
114+
invocationID: extractedInvocationID,
97115
key: matches[4] + matches[7],
98116
ts: matches[1],
99117
typ: "start",
@@ -155,15 +173,15 @@ func analyzeLogFile(filePath string) error {
155173
if len(data.times) > 1 {
156174
// Calculate time difference in minutes
157175
timeDiff := int(data.times[len(data.times)-1].Sub(data.times[0]).Seconds() / 60)
158-
if timeDiff > 0 {
159-
if _, exists := timeDiffKeyPairs[invocationID]; !exists {
160-
timeDiffKeyPairs[invocationID] = []timeDiffKeyPair{}
161-
}
162-
timeDiffKeyPairs[invocationID] = append(timeDiffKeyPairs[invocationID], timeDiffKeyPair{
163-
timeDiff: timeDiff,
164-
key: key,
165-
})
176+
177+
if _, exists := timeDiffKeyPairs[invocationID]; !exists {
178+
timeDiffKeyPairs[invocationID] = []timeDiffKeyPair{}
166179
}
180+
timeDiffKeyPairs[invocationID] = append(timeDiffKeyPairs[invocationID], timeDiffKeyPair{
181+
timeDiff: timeDiff,
182+
key: key,
183+
})
184+
167185
} else if len(data.types) == 1 && data.types[0] == "start" {
168186
fmt.Printf("Table never completed: %s for invocation %s\n", key, invocationID)
169187
}
@@ -177,6 +195,7 @@ func analyzeLogFile(filePath string) error {
177195
})
178196
}
179197

198+
// Display results for all invocation IDs
180199
for invocationID := range timeDiffKeyPairs {
181200
fmt.Printf("Invocation ID: %s\n", invocationID)
182201
// Print time differences

0 commit comments

Comments
 (0)