Skip to content

Commit

Permalink
Update library usage increase history ingest speed
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDallas committed Aug 13, 2022
1 parent 2484782 commit 1f7431d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 50 deletions.
28 changes: 3 additions & 25 deletions internal/arr/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package arr

import (
"fmt"
"math"
"time"

"github.com/jackdallas/premiumizearr/pkg/premiumizeme"
log "github.com/sirupsen/logrus"
"golift.io/starr"
"golift.io/starr/radarr"
)

Expand All @@ -28,30 +26,10 @@ func (arr *RadarrArr) GetHistory() (radarr.History, error) {
arr.LastUpdateCountMutex.Lock()
defer arr.LastUpdateCountMutex.Unlock()

if time.Since(arr.LastUpdate) > 60*time.Second || arr.History == nil {
//Get first page of records
his, err := arr.Client.GetHistoryPage(&starr.Req{PageSize: 250, Page: 1})
if time.Since(arr.LastUpdate) > 30*time.Second || arr.History == nil {
his, err := arr.Client.GetHistory(0, 1000)
if err != nil {
return radarr.History{}, fmt.Errorf("failed to get history from radarr: %+v", err)
}

if his.TotalRecords == arr.LastUpdateCount && his.TotalRecords > 0 {
return *arr.History, nil
}

if his.TotalRecords > 250 {
cachedPages := int(math.Ceil(float64(arr.LastUpdateCount) / 250))
log.Tracef("Loaded %d cached pages of history\n", cachedPages)
remotePages := int(math.Ceil(float64(his.TotalRecords) / float64(250)))
log.Tracef("Found %d pages of history on the radarr server\n", cachedPages)
for i := 2; i <= remotePages-cachedPages; i++ {
log.Tracef("Radarr.GetHistory(): Getting History Page %d", i)
h, err := arr.Client.GetHistoryPage(&starr.Req{PageSize: 250, Page: i})
if err != nil {
return radarr.History{}, fmt.Errorf("failed to get history from radarr: %+v", err)
}
his.Records = append(his.Records, h.Records...)
}
return radarr.History{}, err
}

arr.History = his
Expand Down
28 changes: 3 additions & 25 deletions internal/arr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package arr

import (
"fmt"
"math"
"time"

"github.com/jackdallas/premiumizearr/pkg/premiumizeme"
log "github.com/sirupsen/logrus"
"golift.io/starr"
"golift.io/starr/sonarr"
)

Expand All @@ -28,30 +26,10 @@ func (arr *SonarrArr) GetHistory() (sonarr.History, error) {
arr.LastUpdateCountMutex.Lock()
defer arr.LastUpdateCountMutex.Unlock()

if time.Since(arr.LastUpdate) > 60*time.Second || arr.History == nil {
//Get first page of records
his, err := arr.Client.GetHistoryPage(&starr.Req{PageSize: 250, Page: 1})
if time.Since(arr.LastUpdate) > 30*time.Second || arr.History == nil {
his, err := arr.Client.GetHistory(0, 1000)
if err != nil {
return sonarr.History{}, fmt.Errorf("failed to get history from sonarr: %+v", err)
}

if his.TotalRecords == arr.LastUpdateCount && his.TotalRecords > 0 {
return *arr.History, nil
}

if his.TotalRecords > 250 {
cachedPages := int(math.Ceil(float64(arr.LastUpdateCount) / 250))
fmt.Printf("Loaded %d cached pages of history\n", cachedPages)
remotePages := int(math.Ceil(float64(his.TotalRecords) / float64(250)))
fmt.Printf("Found %d pages of history on the sonarr server\n", cachedPages)
for i := 2; i <= remotePages-cachedPages; i++ {
log.Tracef("Sonarr.GetHistory(): Getting History Page %d", i)
h, err := arr.Client.GetHistoryPage(&starr.Req{PageSize: 250, Page: i})
if err != nil {
return sonarr.History{}, fmt.Errorf("failed to get history from sonarr: %+v", err)
}
his.Records = append(his.Records, h.Records...)
}
return sonarr.History{}, err
}

arr.History = his
Expand Down

0 comments on commit 1f7431d

Please sign in to comment.