-
Notifications
You must be signed in to change notification settings - Fork 17
Return costs #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Return costs #155
Changes from 3 commits
29aa093
0fe3370
01f111b
a36863d
5bf2654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
file: | ||
- type: $.Config.InstanceType | ||
id: $.Config.InstanceId | ||
path: | ||
paths: | ||
- config*.json | ||
- test*.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ func Run(ctx *v1.ScrapeContext, configs ...v1.ConfigScraper) ([]v1.ScrapeResult, | |
if err := db.PersistJobHistory(&jobHistory); err != nil { | ||
logger.Errorf("Error persisting job history: %v", err) | ||
} | ||
|
||
for _, result := range scraper.Scrape(ctx, config) { | ||
if result.AnalysisResult != nil { | ||
if rule, ok := analysis.Rules[result.AnalysisResult.Analyzer]; ok { | ||
|
@@ -36,6 +37,49 @@ func Run(ctx *v1.ScrapeContext, configs ...v1.ConfigScraper) ([]v1.ScrapeResult, | |
} | ||
} | ||
|
||
if result.Costs != nil { | ||
gormDB := db.DefaultDB() | ||
var accountTotal1h, accountTotal1d, accountTotal7d, accountTotal30d float64 | ||
for _, item := range result.Costs.LineItems { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is missing external_type, Did we also not extract this into a common clause so that we con't have to keep repeating it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @moshloop external type wasn't being used in |
||
tx := gormDB.Exec(`UPDATE config_items SET cost_per_minute = ?, cost_total_1d = ?, cost_total_7d = ?, cost_total_30d = ? WHERE ? = ANY(external_id)`, | ||
item.CostPerMin, | ||
item.Cost1d, | ||
item.Cost7d, | ||
item.Cost30d, | ||
item.ExternalID, | ||
) | ||
|
||
if tx.Error != nil { | ||
logger.Errorf("error updating costs for config_item (externalID=%s): %v", item.ExternalID, tx.Error) | ||
continue | ||
} | ||
|
||
if tx.RowsAffected == 0 { | ||
accountTotal1h += item.CostPerMin | ||
accountTotal1d += item.Cost1d | ||
accountTotal7d += item.Cost7d | ||
accountTotal30d += item.Cost30d | ||
continue | ||
} | ||
|
||
logger.Infof("updated cost (externalID=%s)", item.ExternalID) | ||
} | ||
|
||
err := gormDB.Exec(`UPDATE config_items SET cost_per_minute = ?, cost_total_1d = ?, cost_total_7d = ?, cost_total_30d = ? WHERE external_type = ? AND ? = ANY(external_id)`, | ||
accountTotal1h, | ||
accountTotal1d, | ||
accountTotal7d, | ||
accountTotal30d, | ||
result.Costs.ExternalType, | ||
result.Costs.ExternalID, | ||
).Error | ||
if err != nil { | ||
logger.Errorf("error updating costs (type=%s) (externalID=%s): %v", result.Costs.ExternalType, result.Costs.ExternalID, err) | ||
} | ||
|
||
logger.Infof("updated total cost (externalID=%s)", result.Costs.ExternalID) | ||
} | ||
|
||
result.Changes = changes.ProcessRules(result) | ||
|
||
if result.Config == nil && (result.AnalysisResult != nil || len(result.Changes) > 0) { | ||
|
@@ -57,17 +101,20 @@ func Run(ctx *v1.ScrapeContext, configs ...v1.ConfigScraper) ([]v1.ScrapeResult, | |
|
||
results = append(results, scraped...) | ||
} | ||
|
||
if result.Error != nil { | ||
jobHistory.AddError(result.Error.Error()) | ||
} else { | ||
jobHistory.IncrSuccess() | ||
} | ||
} | ||
|
||
jobHistory.End() | ||
if err := db.PersistJobHistory(&jobHistory); err != nil { | ||
logger.Errorf("Error persisting job history: %v", err) | ||
} | ||
} | ||
} | ||
|
||
return results, nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.