Skip to content

Commit

Permalink
Identify case number before processing
Browse files Browse the repository at this point in the history
Always attempt to identify before processing
to avoid wasting cycles.

Resolves: #43
  • Loading branch information
dosaboy committed Jun 26, 2023
1 parent b7fcf2a commit 4ad99a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pkg/common/salesforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func GetCaseNumberByFilename(filename string) (string, error) {
}

for _, candidate := range regex.FindAll([]byte(filename), 1) {
if len(candidate) <= 8 {
if len(candidate) <= 8 && len(candidate) > 0 {
return string(candidate), nil
}
}

return "", fmt.Errorf("Not found case number on: %s", filename)
return "", fmt.Errorf("Could not identify case number from filename '%s'", filename)
}
17 changes: 9 additions & 8 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func RunReport(report *ReportToExecute) (map[string][]byte, error) {

const DefaultReportOutputFormat = "%s.athena-%s.%s"

func (runner *ReportRunner) UploadAndSaveReport(report *ReportToExecute, scriptOutputs map[string][]byte) error {
func (runner *ReportRunner) UploadAndSaveReport(report *ReportToExecute, caseNumber string, scriptOutputs map[string][]byte) error {
var file db.File
var uploadPath string
filePath := report.File.Path
Expand All @@ -124,11 +124,6 @@ func (runner *ReportRunner) UploadAndSaveReport(report *ReportToExecute, scriptO
uploadPath = path.Join(runner.Config.Processor.ReportsUploadPath, filepath.Base(filePath))
}

caseNumber, err := common.GetCaseNumberByFilename(filePath)
if err != nil || caseNumber == "" {
return fmt.Errorf("not found case number on filename: %s", filePath)
}

logrus.Infof("Getting case from salesforce number: %s", caseNumber)
sfCase, err := runner.SalesforceClient.GetCaseByNumber(caseNumber)
if err != nil {
Expand Down Expand Up @@ -178,6 +173,12 @@ func (runner *ReportRunner) Run(reportFn func(report *ReportToExecute) (map[stri
for _, report := range runner.Reports {
var err error

caseNumber, err := common.GetCaseNumberByFilename(report.File.Path)
if err != nil || caseNumber == "" {
logrus.Error(err)
continue
}

logrus.Debugf("Running report: %s on file: %s", report.Name, report.File.Path)
scriptOutputs, err := reportFn(&report)
if err != nil {
Expand All @@ -186,8 +187,8 @@ func (runner *ReportRunner) Run(reportFn func(report *ReportToExecute) (map[stri
}

logrus.Debugf("Uploading and saving report:%s script outputs: %d - for file: %s", report.Name, len(scriptOutputs), report.File.Path)
if err := runner.UploadAndSaveReport(&report, scriptOutputs); err != nil {
logrus.Errorf("cannot upload and save report: %s - error: %s", report.Name, err)
if err := runner.UploadAndSaveReport(&report, caseNumber, scriptOutputs); err != nil {
logrus.Errorf("Failed to upload and save report: %s - error: %s", report.Name, err)
continue
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ func (s *ProcessorTestSuite) TestRunProcessor() {

b, _ := json.Marshal(db.File{Path: "/uploads/sosreport-123.tar.xz"})
b1, _ := json.Marshal(db.File{Path: "/uploads/sosreport-321.tar.xz"})
b2, _ := json.Marshal(db.File{Path: "/uploads/sosreport-abc.tar.xz"})

_ = provider.Publish(context.Background(), "sosreports", &pubsub.Msg{Data: b})
_ = provider.Publish(context.Background(), "sosreports", &pubsub.Msg{Data: b1})
_ = provider.Publish(context.Background(), "sosreports", &pubsub.Msg{Data: b2})

var called = 0

Expand All @@ -79,8 +81,8 @@ func (s *ProcessorTestSuite) TestRunProcessor() {
return &subscriber
})

assert.Equal(s.T(), called, 2)
assert.Equal(s.T(), len(provider.Msgs["sosreports"]), 2)
assert.Equal(s.T(), called, 3)
assert.Equal(s.T(), len(provider.Msgs["sosreports"]), 3)
}

func TestNewProcessor(t *testing.T) {
Expand Down

0 comments on commit 4ad99a8

Please sign in to comment.