Skip to content

Commit

Permalink
chore: optimization by improving filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 23, 2018
1 parent be61ee9 commit 4ebc498
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions cmd_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func newDBDumpCommand() *cobra.Command {

func dbDump(opts *dbOptions) error {
var issues IssueSlice
if err := db.Find(&issues).Error; err != nil {
if err := db.Set("gorm:auto_preload", false).Find(&issues).Error; err != nil {
return errors.Wrap(err, "failed to load issues")
}
out, err := json.MarshalIndent(issues, "", " ")
Expand All @@ -62,11 +62,25 @@ func dbDump(opts *dbOptions) error {
return nil
}

func loadIssues(db *gorm.DB) (Issues, error) {
func canonicalTargets(input []string) []string {
output := []string{}
base := Issue{RepoURL: "https://github.com/moul/depviz"}
for _, target := range input {
output = append(output, base.GetRelativeIssueURL(target))
}
return output
}

func loadIssues(db *gorm.DB, targets []string) (Issues, error) {
query := db
if len(targets) > 0 {
query = query.Where("repo_url IN (?)", canonicalTargets(targets))
}
var issues []*Issue
if err := db.Find(&issues).Error; err != nil {
if err := query.Find(&issues).Error; err != nil {
return nil, err
}
fmt.Println(targets, issues)
slice := IssueSlice(issues)
return slice.ToMap(), nil
}
2 changes: 1 addition & 1 deletion cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func run(opts *runOptions) error {
}
}

issues, err := loadIssues(db)
issues, err := loadIssues(db, opts.Targets)
if err != nil {
return errors.Wrap(err, "failed to load issues")
}
Expand Down

0 comments on commit 4ebc498

Please sign in to comment.