Skip to content

Commit 2352ba9

Browse files
feat: enhance analysis command with statistics option
Refactored the analysis command to support an enhanced statistics option, enabling users to opt-in for detailed performance metrics of the analysis process. This change introduces a more flexible approach to handling statistics, allowing for a clearer separation between the analysis output and performance metrics, thereby improving the usability and insights provided to the user. Signed-off-by: Matthis Holleville <[email protected]>
1 parent bc4c53b commit 2352ba9

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

cmd/analyze/analyze.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040
interactiveMode bool
4141
customAnalysis bool
4242
customHeaders []string
43-
stats bool
43+
withStats bool
4444
)
4545

4646
// AnalyzeCmd represents the problems command
@@ -64,6 +64,7 @@ var AnalyzeCmd = &cobra.Command{
6464
withDoc,
6565
interactiveMode,
6666
customHeaders,
67+
withStats,
6768
)
6869

6970
if err != nil {
@@ -77,12 +78,6 @@ var AnalyzeCmd = &cobra.Command{
7778
}
7879
config.RunAnalysis()
7980

80-
if stats {
81-
statsData := config.PrintStats()
82-
fmt.Println(string(statsData))
83-
os.Exit(0)
84-
}
85-
8681
if explain {
8782
if err := config.GetAIResults(output, anonymize); err != nil {
8883
color.Red("Error: %v", err)
@@ -95,6 +90,12 @@ var AnalyzeCmd = &cobra.Command{
9590
color.Red("Error: %v", err)
9691
os.Exit(1)
9792
}
93+
94+
if withStats {
95+
statsData := config.PrintStats()
96+
fmt.Println(string(statsData))
97+
}
98+
9899
fmt.Println(string(output_data))
99100

100101
if interactiveMode && explain {
@@ -154,5 +155,5 @@ func init() {
154155
// label selector flag
155156
AnalyzeCmd.Flags().StringVarP(&labelSelector, "selector", "L", "", "Label selector (label query) to filter on, supports '=', '==', and '!='. (e.g. -L key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.")
156157
// print stats
157-
AnalyzeCmd.Flags().BoolVarP(&stats, "stats", "s", false, "Print analysis stats. This option disables errors display.")
158+
AnalyzeCmd.Flags().BoolVarP(&withStats, "with-stat", "s", false, "Print analysis stats. This option disables errors display.")
158159
}

pkg/analysis/analysis.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Analysis struct {
5050
MaxConcurrency int
5151
AnalysisAIProvider string // The name of the AI Provider used for this analysis
5252
WithDoc bool
53+
WithStats bool
5354
Stats []common.AnalysisStats
5455
}
5556

@@ -83,6 +84,7 @@ func NewAnalysis(
8384
withDoc bool,
8485
interactiveMode bool,
8586
httpHeaders []string,
87+
withStats bool,
8688
) (*Analysis, error) {
8789
// Get kubernetes client from viper.
8890
kubecontext := viper.GetString("kubecontext")
@@ -113,6 +115,7 @@ func NewAnalysis(
113115
Explain: explain,
114116
MaxConcurrency: maxConcurrency,
115117
WithDoc: withDoc,
118+
WithStats: withStats,
116119
}
117120
if !explain {
118121
// Return early if AI use was not requested.
@@ -282,14 +285,21 @@ func (a *Analysis) RunAnalysis() {
282285
func (a *Analysis) executeAnalyzer(analyzer common.IAnalyzer, filter string, analyzerConfig common.Analyzer, semaphore chan struct{}, wg *sync.WaitGroup, mutex *sync.Mutex) {
283286
defer wg.Done()
284287

288+
var startTime time.Time
289+
var elapsedTime time.Duration
290+
285291
// Start the timer
286-
startTime := time.Now()
292+
if a.WithStats {
293+
startTime = time.Now()
294+
}
287295

288296
// Run the analyzer
289297
results, err := analyzer.Analyze(analyzerConfig)
290298

291299
// Measure the time taken
292-
elapsedTime := time.Since(startTime)
300+
if a.WithStats {
301+
elapsedTime = time.Since(startTime)
302+
}
293303
stat := common.AnalysisStats{
294304
Analyzer: filter,
295305
DurationTime: elapsedTime,
@@ -299,10 +309,14 @@ func (a *Analysis) executeAnalyzer(analyzer common.IAnalyzer, filter string, ana
299309
defer mutex.Unlock()
300310

301311
if err != nil {
302-
a.Stats = append(a.Stats, stat)
312+
if a.WithStats {
313+
a.Stats = append(a.Stats, stat)
314+
}
303315
a.Errors = append(a.Errors, fmt.Sprintf("[%s] %s", filter, err))
304316
} else {
305-
a.Stats = append(a.Stats, stat)
317+
if a.WithStats {
318+
a.Stats = append(a.Stats, stat)
319+
}
306320
a.Results = append(a.Results, results...)
307321
}
308322
<-semaphore

pkg/server/analyze/analyze.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package analyze
22

33
import (
4-
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
54
"context"
65
json "encoding/json"
6+
7+
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
78
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
89
)
910

@@ -31,6 +32,7 @@ func (h *Handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) (
3132
false, // Kubernetes Doc disabled in server mode
3233
false, // Interactive mode disabled in server mode
3334
[]string{}, //TODO: add custom http headers in server mode
35+
false, // with stats disable
3436
)
3537
config.Context = ctx // Replace context for correct timeouts.
3638
if err != nil {

0 commit comments

Comments
 (0)