Skip to content

Commit

Permalink
Merge pull request #84 from yorks/master
Browse files Browse the repository at this point in the history
improve db query performance, using between instead of strftime
  • Loading branch information
skx authored Aug 1, 2021
2 parents 8e4f7ca + 93a9234 commit 7f81ccd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ func getHistory(environment string) ([]PuppetHistory, error) {
//
// Now we have all the unique dates in `dates`.
//
loc,_ := time.LoadLocation("Local")
for _, known := range dates {

//
Expand All @@ -721,8 +722,11 @@ func getHistory(environment string) ([]PuppetHistory, error) {
x.Unchanged = "0"
x.Failed = "0"
x.Date = known
formatTime,_:=time.ParseInLocation("02/01/2006 15:04:05", known + " 00:00:00", loc)
ts1 := formatTime.Unix()
ts2 := ts1 + 3600*24 - 1

query := "SELECT distinct state, COUNT(state) AS CountOf FROM reports WHERE strftime('%d/%m/%Y', DATE(executed_at, 'unixepoch'))=? "
query := "SELECT distinct state, COUNT(state) AS CountOf FROM reports WHERE executed_at between ? and ?"
if len(environment) > 0 {
query += " AND environment = '" + environment + "' "
}
Expand All @@ -732,7 +736,7 @@ func getHistory(environment string) ([]PuppetHistory, error) {
return nil, err
}

rows, err = stmt.Query(known)
rows, err = stmt.Query(ts1, ts2)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7f81ccd

Please sign in to comment.