Skip to content

Commit

Permalink
feat: scrape summary
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 2, 2024
1 parent f121cd5 commit 0f17df2
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 64 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ scraped/
cover.out
ginkgo.report

config.properties
config.properties

*.gob
87 changes: 87 additions & 0 deletions api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,93 @@ func (result *AnalysisResult) Message(msg string) *AnalysisResult {
// +kubebuilder:object:generate=false
type AnalysisResults []AnalysisResult

type ScrapeSummary map[string]ConfigTypeScrapeSummary

func (t *ScrapeSummary) AddChangeSummary(configType string, cs ChangeSummary) {
v := (*t)[configType]
v.Change = &ChangeSummary{
Ignored: cs.Ignored,
Orphaned: cs.Orphaned,
}
(*t)[configType] = v
}

func (t *ScrapeSummary) AddInserted(configType string) {
v := (*t)[configType]
v.Added++
(*t)[configType] = v
}

func (t *ScrapeSummary) AddUpdated(configType string) {
v := (*t)[configType]
v.Updated++
(*t)[configType] = v
}

func (t *ScrapeSummary) AddUnchanged(configType string) {
v := (*t)[configType]
v.Unchanged++
(*t)[configType] = v
}

type ChangeSummary struct {
Orphaned map[string]int `json:"orphaned,omitempty"`
Ignored map[string]int `json:"ignored,omitempty"`
}

func (t ChangeSummary) IsEmpty() bool {
return len(t.Orphaned) == 0 && len(t.Ignored) == 0
}

func (t *ChangeSummary) AddOrphaned(typ string) {
if t.Orphaned == nil {
t.Orphaned = make(map[string]int)
}
t.Orphaned[typ] += 1
}

func (t *ChangeSummary) AddIgnored(typ string) {
if t.Ignored == nil {
t.Ignored = make(map[string]int)
}
t.Ignored[typ] += 1
}

func (t *ChangeSummary) Merge(b ChangeSummary) {
if b.Orphaned != nil {
if t.Orphaned == nil {
t.Orphaned = make(map[string]int)
}
for k, v := range b.Orphaned {
t.Orphaned[k] += v
}
}

if b.Ignored != nil {
if t.Ignored == nil {
t.Ignored = make(map[string]int)
}
for k, v := range b.Ignored {
t.Ignored[k] += v
}
}
}

type ChangeSummaryByType map[string]ChangeSummary

func (t *ChangeSummaryByType) Merge(typ string, b ChangeSummary) {
v := (*t)[typ]
v.Merge(b)
(*t)[typ] = v
}

type ConfigTypeScrapeSummary struct {
Added int `json:"added,omitempty"`
Updated int `json:"updated,omitempty"`
Unchanged int `json:"unchanged,omitempty"`
Change *ChangeSummary `json:"change,omitempty"`
}

// +kubebuilder:object:generate=false
type ScrapeResults []ScrapeResult

Expand Down
122 changes: 122 additions & 0 deletions api/v1/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package v1

import (
"testing"
)

func TestChangeSummary_Merge(t *testing.T) {
tests := []struct {
name string
summary1 ChangeSummary
summary2 ChangeSummary
expected ChangeSummary
}{
{
name: "merge empty summaries",
summary1: ChangeSummary{},
summary2: ChangeSummary{},
expected: ChangeSummary{},
},
{
name: "merge summaries with orphaned changes",
summary1: ChangeSummary{
Orphaned: map[string]int{
"foo": 1,
"bar": 2,
},
},
summary2: ChangeSummary{
Orphaned: map[string]int{
"foo": 3,
"baz": 4,
},
},
expected: ChangeSummary{
Orphaned: map[string]int{
"foo": 4,
"bar": 2,
"baz": 4,
},
},
},
{
name: "merge summaries with ignored changes",
summary1: ChangeSummary{
Ignored: map[string]int{
"foo": 1,
"bar": 2,
},
},
summary2: ChangeSummary{
Ignored: map[string]int{
"foo": 3,
"baz": 4,
},
},
expected: ChangeSummary{
Ignored: map[string]int{
"foo": 4,
"bar": 2,
"baz": 4,
},
},
},
{
name: "merge summaries with both orphaned and ignored changes",
summary1: ChangeSummary{
Orphaned: map[string]int{
"foo": 1,
"bar": 2,
},
Ignored: map[string]int{
"baz": 3,
"qux": 4,
},
},
summary2: ChangeSummary{
Orphaned: map[string]int{
"foo": 5,
"quux": 6,
},
Ignored: map[string]int{
"baz": 7,
"corge": 8,
},
},
expected: ChangeSummary{
Orphaned: map[string]int{
"foo": 6,
"bar": 2,
"quux": 6,
},
Ignored: map[string]int{
"baz": 10,
"qux": 4,
"corge": 8,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.summary1.Merge(tt.summary2)
if len(tt.summary1.Orphaned) != len(tt.expected.Orphaned) {
t.Errorf("Expected %d orphaned changes, got %d", len(tt.expected.Orphaned), len(tt.summary1.Orphaned))
}
for k, v := range tt.expected.Orphaned {
if tt.summary1.Orphaned[k] != v {
t.Errorf("Expected %d orphaned changes for %s, got %d", v, k, tt.summary1.Orphaned[k])
}
}
if len(tt.summary1.Ignored) != len(tt.expected.Ignored) {
t.Errorf("Expected %d ignored changes, got %d", len(tt.expected.Ignored), len(tt.summary1.Ignored))
}
for k, v := range tt.expected.Ignored {
if tt.summary1.Ignored[k] != v {
t.Errorf("Expected %d ignored changes for %s, got %d", v, k, tt.summary1.Ignored[k])
}
}
})
}
}
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func scrapeAndStore(ctx api.ScrapeContext) error {
}
if db.ConnectionString != "" {
logger.Infof("Exporting %d resources to DB", len(results))
return db.SaveResults(ctx, results)
_, err := db.SaveResults(ctx, results)
return err
}

if outputDir != "" {
Expand Down
Loading

0 comments on commit 0f17df2

Please sign in to comment.