cache last ntg result for off hours

master
bel 2022-01-29 18:59:16 -07:00
parent ff1f62def9
commit 0ded04f072
1 changed files with 16 additions and 2 deletions

View File

@ -239,7 +239,11 @@ func (ntg NTGVision) workingHours(now time.Time) bool {
func (ntg NTGVision) SearchStates(states []config.State) ([]Job, error) {
if !ntg.workingHours(time.Now()) {
return nil, nil
lastNtgB, _ := config.Get().DB().Get("ntg_last_search_states")
var jobs []Job
json.Unmarshal(lastNtgB, &jobs)
logtr.Verbosef("ntg.SearchStates: outside of working hours so returning ntg_last_search_states: %+v", jobs)
return jobs, nil
}
rc, err := ntg.searcher.searchStates(states)
@ -257,12 +261,22 @@ func (ntg NTGVision) SearchStates(states []config.State) ([]Job, error) {
var ntgjobs []ntgVisionJob
err = json.Unmarshal(b, &ntgjobs)
if err != nil {
return nil, err
}
jobs := make([]Job, len(ntgjobs))
for i := range jobs {
jobs[i] = ntgjobs[i].Job()
}
return jobs, err
jobsB, err := json.Marshal(jobs)
if err == nil {
config.Get().DB().Set("ntg_last_search_states", jobsB)
logtr.Verbosef("ntg.SearchStates: in working hours so setting ntg_last_search_states: %+v", jobs)
}
return jobs, nil
}
func getNTGTokenKey() string {