59 lines
2.2 KiB
Go
59 lines
2.2 KiB
Go
package api
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/ponzischeme89/memby/server/internal/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
// What the *arr webhooks have been doing, for the Imports page.
|
||
|
|
//
|
||
|
|
// It exists because a webhook is the one part of this gateway that fails *silently*: a
|
||
|
|
// token typed wrongly into Sonarr, a URL the container cannot be reached on, or a
|
||
|
|
// notification never enabled all look exactly like a household in which nothing has been
|
||
|
|
// imported lately. Without this an operator's only recourse is reading container logs.
|
||
|
|
|
||
|
|
// ingestEventLimit is how much of the log the page carries. Enough to cover an evening's
|
||
|
|
// imports and a season pack, which is what somebody is looking at when they open it.
|
||
|
|
const ingestEventLimit = 50
|
||
|
|
|
||
|
|
type adminIngestResponse struct {
|
||
|
|
// Configured says whether each hook would answer at all. Both unset is the honest
|
||
|
|
// explanation of an empty table, and the page says so rather than leaving an operator
|
||
|
|
// to conclude the feature is broken.
|
||
|
|
SonarrConfigured bool `json:"sonarrConfigured"`
|
||
|
|
RadarrConfigured bool `json:"radarrConfigured"`
|
||
|
|
SettleSeconds int `json:"settleSeconds"`
|
||
|
|
SyncMinutes int `json:"syncMinutes"`
|
||
|
|
Counts store.IngestCounts `json:"counts"`
|
||
|
|
Recent []store.IngestJob `json:"recent"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) handleAdminIngest(w http.ResponseWriter, r *http.Request) {
|
||
|
|
ctx := r.Context()
|
||
|
|
response := adminIngestResponse{
|
||
|
|
SonarrConfigured: s.cfg.SonarrWebhookToken != "",
|
||
|
|
RadarrConfigured: s.cfg.RadarrWebhookToken != "",
|
||
|
|
SettleSeconds: int(s.ingestSettle().Seconds()),
|
||
|
|
SyncMinutes: int(s.LibrarySyncInterval().Minutes()),
|
||
|
|
Recent: []store.IngestJob{},
|
||
|
|
}
|
||
|
|
|
||
|
|
counts, err := s.store.IngestStateCounts(ctx)
|
||
|
|
if err != nil {
|
||
|
|
s.loggerFor(ctx).Error("ingest counts failed", "error", err)
|
||
|
|
writeError(w, http.StatusInternalServerError, "could not read the import queue")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
response.Counts = counts
|
||
|
|
|
||
|
|
recent, err := s.store.RecentIngests(ctx, ingestEventLimit)
|
||
|
|
if err != nil {
|
||
|
|
s.loggerFor(ctx).Error("ingest history failed", "error", err)
|
||
|
|
writeError(w, http.StatusInternalServerError, "could not read the import queue")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
response.Recent = recent
|
||
|
|
writeJSON(w, http.StatusOK, response)
|
||
|
|
}
|