From 26f558859a120406ef3f3cdc0e0c494b645469f0 Mon Sep 17 00:00:00 2001 From: Ludvig Lundgren Date: Thu, 3 Feb 2022 21:58:41 +0100 Subject: [PATCH] fix: blank page loading filters (#107) * fix(filters): load indexers separate * feat: add ctx to filter related db methods --- internal/database/action.go | 2 +- internal/database/filter.go | 12 ++- internal/database/indexer.go | 16 ++-- internal/domain/action.go | 2 +- internal/domain/filter.go | 4 +- internal/domain/indexer.go | 2 +- internal/filter/service.go | 20 ++--- internal/http/filter.go | 24 +----- internal/indexer/service.go | 6 +- web/src/screens/filters/details.tsx | 117 +++++++++++++--------------- 10 files changed, 90 insertions(+), 115 deletions(-) diff --git a/internal/database/action.go b/internal/database/action.go index e81d23c..eb7deab 100644 --- a/internal/database/action.go +++ b/internal/database/action.go @@ -16,7 +16,7 @@ func NewActionRepo(db *SqliteDB) domain.ActionRepo { return &ActionRepo{db: db} } -func (r *ActionRepo) FindByFilterID(filterID int) ([]domain.Action, error) { +func (r *ActionRepo) FindByFilterID(ctx context.Context, filterID int) ([]domain.Action, error) { //r.db.lock.RLock() //defer r.db.lock.RUnlock() diff --git a/internal/database/filter.go b/internal/database/filter.go index c243187..9bfdeb8 100644 --- a/internal/database/filter.go +++ b/internal/database/filter.go @@ -19,11 +19,11 @@ func NewFilterRepo(db *SqliteDB) domain.FilterRepo { return &FilterRepo{db: db} } -func (r *FilterRepo) ListFilters() ([]domain.Filter, error) { +func (r *FilterRepo) ListFilters(ctx context.Context) ([]domain.Filter, error) { //r.db.lock.RLock() //defer r.db.lock.RUnlock() - rows, err := r.db.handler.Query("SELECT id, enabled, name, match_releases, except_releases, created_at, updated_at FROM filter ORDER BY name ASC") + rows, err := r.db.handler.QueryContext(ctx, "SELECT id, enabled, name, match_releases, except_releases, created_at, updated_at FROM filter ORDER BY name ASC") if err != nil { log.Error().Stack().Err(err).Msg("filters_list: error query data") return nil, err @@ -54,18 +54,16 @@ func (r *FilterRepo) ListFilters() ([]domain.Filter, error) { return filters, nil } -func (r *FilterRepo) FindByID(filterID int) (*domain.Filter, error) { +func (r *FilterRepo) FindByID(ctx context.Context, filterID int) (*domain.Filter, error) { //r.db.lock.RLock() //defer r.db.lock.RUnlock() - row := r.db.handler.QueryRow("SELECT id, enabled, name, min_size, max_size, delay, match_releases, except_releases, use_regex, match_release_groups, except_release_groups, scene, freeleech, freeleech_percent, shows, seasons, episodes, resolutions, codecs, sources, containers, match_hdr, except_hdr, years, artists, albums, release_types_match, formats, quality, media, log_score, has_log, has_cue, perfect_flac, match_categories, except_categories, match_uploaders, except_uploaders, tags, except_tags, created_at, updated_at FROM filter WHERE id = ?", filterID) - - var f domain.Filter - + row := r.db.handler.QueryRowContext(ctx, "SELECT id, enabled, name, min_size, max_size, delay, match_releases, except_releases, use_regex, match_release_groups, except_release_groups, scene, freeleech, freeleech_percent, shows, seasons, episodes, resolutions, codecs, sources, containers, match_hdr, except_hdr, years, artists, albums, release_types_match, formats, quality, media, log_score, has_log, has_cue, perfect_flac, match_categories, except_categories, match_uploaders, except_uploaders, tags, except_tags, created_at, updated_at FROM filter WHERE id = ?", filterID) if err := row.Err(); err != nil { return nil, err } + var f domain.Filter var minSize, maxSize, matchReleases, exceptReleases, matchReleaseGroups, exceptReleaseGroups, freeleechPercent, shows, seasons, episodes, years, artists, albums, matchCategories, exceptCategories, matchUploaders, exceptUploaders, tags, exceptTags sql.NullString var useRegex, scene, freeleech, hasLog, hasCue, perfectFlac sql.NullBool var delay, logScore sql.NullInt32 diff --git a/internal/database/indexer.go b/internal/database/indexer.go index 9e556d3..49c6b6d 100644 --- a/internal/database/indexer.go +++ b/internal/database/indexer.go @@ -64,7 +64,8 @@ func (r *IndexerRepo) List() ([]domain.Indexer, error) { rows, err := r.db.handler.Query("SELECT id, enabled, name, identifier, settings FROM indexer ORDER BY name ASC") if err != nil { - log.Fatal().Err(err) + log.Error().Stack().Err(err).Msg("indexer.list: error query indexer") + return nil, err } defer rows.Close() @@ -78,8 +79,6 @@ func (r *IndexerRepo) List() ([]domain.Indexer, error) { if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &f.Identifier, &settings); err != nil { log.Error().Stack().Err(err).Msg("indexer.list: error scanning data to struct") - } - if err != nil { return nil, err } @@ -100,17 +99,18 @@ func (r *IndexerRepo) List() ([]domain.Indexer, error) { return indexers, nil } -func (r *IndexerRepo) FindByFilterID(id int) ([]domain.Indexer, error) { +func (r *IndexerRepo) FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) { //r.db.lock.RLock() //defer r.db.lock.RUnlock() - rows, err := r.db.handler.Query(` + rows, err := r.db.handler.QueryContext(ctx, ` SELECT i.id, i.enabled, i.name, i.identifier FROM indexer i JOIN filter_indexer fi on i.id = fi.indexer_id WHERE fi.filter_id = ?`, id) if err != nil { - log.Fatal().Err(err) + log.Error().Stack().Err(err).Msg("indexer.find_by_filter_id: error query indexer") + return nil, err } defer rows.Close() @@ -123,9 +123,7 @@ func (r *IndexerRepo) FindByFilterID(id int) ([]domain.Indexer, error) { //var settingsMap map[string]string if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &f.Identifier); err != nil { - log.Error().Stack().Err(err).Msg("indexer.list: error scanning data to struct") - } - if err != nil { + log.Error().Stack().Err(err).Msg("indexer.find_by_filter_id: error scanning data to struct") return nil, err } diff --git a/internal/domain/action.go b/internal/domain/action.go index c42f89a..68aebe4 100644 --- a/internal/domain/action.go +++ b/internal/domain/action.go @@ -6,7 +6,7 @@ type ActionRepo interface { Store(ctx context.Context, action Action) (*Action, error) StoreFilterActions(ctx context.Context, actions []Action, filterID int64) ([]Action, error) DeleteByFilterID(ctx context.Context, filterID int) error - FindByFilterID(filterID int) ([]Action, error) + FindByFilterID(ctx context.Context, filterID int) ([]Action, error) List() ([]Action, error) Delete(actionID int) error ToggleEnabled(actionID int) error diff --git a/internal/domain/filter.go b/internal/domain/filter.go index 0ee3b23..b066a04 100644 --- a/internal/domain/filter.go +++ b/internal/domain/filter.go @@ -11,9 +11,9 @@ https://autodl-community.github.io/autodl-irssi/configuration/filter/ */ type FilterRepo interface { - FindByID(filterID int) (*Filter, error) + FindByID(ctx context.Context, filterID int) (*Filter, error) FindByIndexerIdentifier(indexer string) ([]Filter, error) - ListFilters() ([]Filter, error) + ListFilters(ctx context.Context) ([]Filter, error) Store(filter Filter) (*Filter, error) Update(ctx context.Context, filter Filter) (*Filter, error) ToggleEnabled(ctx context.Context, filterID int, enabled bool) error diff --git a/internal/domain/indexer.go b/internal/domain/indexer.go index bd7ce9a..70f4b28 100644 --- a/internal/domain/indexer.go +++ b/internal/domain/indexer.go @@ -11,7 +11,7 @@ type IndexerRepo interface { Update(indexer Indexer) (*Indexer, error) List() ([]Indexer, error) Delete(ctx context.Context, id int) error - FindByFilterID(id int) ([]Indexer, error) + FindByFilterID(ctx context.Context, id int) ([]Indexer, error) } type Indexer struct { diff --git a/internal/filter/service.go b/internal/filter/service.go index 1154693..c49deeb 100644 --- a/internal/filter/service.go +++ b/internal/filter/service.go @@ -13,10 +13,10 @@ import ( ) type Service interface { - FindByID(filterID int) (*domain.Filter, error) + FindByID(ctx context.Context, filterID int) (*domain.Filter, error) FindByIndexerIdentifier(indexer string) ([]domain.Filter, error) FindAndCheckFilters(release *domain.Release) (bool, *domain.Filter, error) - ListFilters() ([]domain.Filter, error) + ListFilters(ctx context.Context) ([]domain.Filter, error) Store(filter domain.Filter) (*domain.Filter, error) Update(ctx context.Context, filter domain.Filter) (*domain.Filter, error) ToggleEnabled(ctx context.Context, filterID int, enabled bool) error @@ -39,9 +39,9 @@ func NewService(repo domain.FilterRepo, actionRepo domain.ActionRepo, apiService } } -func (s *service) ListFilters() ([]domain.Filter, error) { +func (s *service) ListFilters(ctx context.Context) ([]domain.Filter, error) { // get filters - filters, err := s.repo.ListFilters() + filters, err := s.repo.ListFilters(ctx) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func (s *service) ListFilters() ([]domain.Filter, error) { var ret []domain.Filter for _, filter := range filters { - indexers, err := s.indexerSvc.FindByFilterID(filter.ID) + indexers, err := s.indexerSvc.FindByFilterID(ctx, filter.ID) if err != nil { return nil, err } @@ -61,22 +61,22 @@ func (s *service) ListFilters() ([]domain.Filter, error) { return ret, nil } -func (s *service) FindByID(filterID int) (*domain.Filter, error) { +func (s *service) FindByID(ctx context.Context, filterID int) (*domain.Filter, error) { // find filter - filter, err := s.repo.FindByID(filterID) + filter, err := s.repo.FindByID(ctx, filterID) if err != nil { return nil, err } // find actions and attach - actions, err := s.actionRepo.FindByFilterID(filter.ID) + actions, err := s.actionRepo.FindByFilterID(ctx, filter.ID) if err != nil { log.Error().Msgf("could not find filter actions: %+v", &filter.ID) } filter.Actions = actions // find indexers and attach - indexers, err := s.indexerSvc.FindByFilterID(filter.ID) + indexers, err := s.indexerSvc.FindByFilterID(ctx, filter.ID) if err != nil { log.Error().Err(err).Msgf("could not find indexers for filter: %+v", &filter.Name) return nil, err @@ -276,7 +276,7 @@ func (s *service) FindAndCheckFilters(release *domain.Release) (bool, *domain.Fi } // found matching filter, lets find the filter actions and attach - actions, err := s.actionRepo.FindByFilterID(f.ID) + actions, err := s.actionRepo.FindByFilterID(context.TODO(), f.ID) if err != nil { log.Error().Err(err).Msgf("could not find actions for filter: %+v", f.Name) } diff --git a/internal/http/filter.go b/internal/http/filter.go index 837b005..26654eb 100644 --- a/internal/http/filter.go +++ b/internal/http/filter.go @@ -12,8 +12,8 @@ import ( ) type filterService interface { - ListFilters() ([]domain.Filter, error) - FindByID(filterID int) (*domain.Filter, error) + ListFilters(ctx context.Context) ([]domain.Filter, error) + FindByID(ctx context.Context, filterID int) (*domain.Filter, error) Store(filter domain.Filter) (*domain.Filter, error) Delete(ctx context.Context, filterID int) error Update(ctx context.Context, filter domain.Filter) (*domain.Filter, error) @@ -44,7 +44,7 @@ func (h filterHandler) Routes(r chi.Router) { func (h filterHandler) getFilters(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - trackers, err := h.service.ListFilters() + trackers, err := h.service.ListFilters(ctx) if err != nil { // } @@ -60,7 +60,7 @@ func (h filterHandler) getByID(w http.ResponseWriter, r *http.Request) { id, _ := strconv.Atoi(filterID) - filter, err := h.service.FindByID(id) + filter, err := h.service.FindByID(ctx, id) if err != nil { h.encoder.StatusNotFound(ctx, w) return @@ -69,22 +69,6 @@ func (h filterHandler) getByID(w http.ResponseWriter, r *http.Request) { h.encoder.StatusResponse(ctx, w, filter, http.StatusOK) } -func (h filterHandler) storeFilterAction(w http.ResponseWriter, r *http.Request) { - var ( - ctx = r.Context() - filterID = chi.URLParam(r, "filterID") - ) - - id, _ := strconv.Atoi(filterID) - - filter, err := h.service.FindByID(id) - if err != nil { - // - } - - h.encoder.StatusResponse(ctx, w, filter, http.StatusCreated) -} - func (h filterHandler) store(w http.ResponseWriter, r *http.Request) { var ( ctx = r.Context() diff --git a/internal/indexer/service.go b/internal/indexer/service.go index 6fe389c..60aeb4c 100644 --- a/internal/indexer/service.go +++ b/internal/indexer/service.go @@ -16,7 +16,7 @@ type Service interface { Store(indexer domain.Indexer) (*domain.Indexer, error) Update(indexer domain.Indexer) (*domain.Indexer, error) Delete(ctx context.Context, id int) error - FindByFilterID(id int) ([]domain.Indexer, error) + FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) List() ([]domain.Indexer, error) GetAll() ([]*domain.IndexerDefinition, error) GetTemplates() ([]domain.IndexerDefinition, error) @@ -92,8 +92,8 @@ func (s *service) Delete(ctx context.Context, id int) error { return nil } -func (s *service) FindByFilterID(id int) ([]domain.Indexer, error) { - filters, err := s.repo.FindByFilterID(id) +func (s *service) FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) { + filters, err := s.repo.FindByFilterID(ctx, id) if err != nil { return nil, err } diff --git a/web/src/screens/filters/details.tsx b/web/src/screens/filters/details.tsx index 463497d..2254ce6 100644 --- a/web/src/screens/filters/details.tsx +++ b/web/src/screens/filters/details.tsx @@ -115,7 +115,7 @@ export default function FilterDetails() { let history = useHistory(); let { filterId }: any = useParams(); - const { isLoading, data } = useQuery(['filter', parseInt(filterId)], () => APIClient.filters.getByID(parseInt(filterId)), + const { isLoading, data: filter } = useQuery(['filter', parseInt(filterId)], () => APIClient.filters.getByID(parseInt(filterId)), { retry: false, refetchOnWindowFocus: false, @@ -125,12 +125,6 @@ export default function FilterDetails() { }, ) - const { data: indexers } = useQuery(["filter", "indexer_list"], APIClient.indexers.getOptions, - { - refetchOnWindowFocus: false - } - ) - const updateMutation = useMutation((filter: Filter) => APIClient.filters.update(filter), { onSuccess: (filter) => { // queryClient.setQueryData(['filter', filter.id], data) @@ -142,7 +136,7 @@ export default function FilterDetails() { const deleteMutation = useMutation((id: number) => APIClient.filters.delete(id), { onSuccess: () => { - toast.custom((t) => ) + toast.custom((t) => ) // redirect history.push("/filters") @@ -153,7 +147,7 @@ export default function FilterDetails() { return null } - if (!data) { + if (!filter) { return null } @@ -162,7 +156,7 @@ export default function FilterDetails() { } const deleteAction = () => { - deleteMutation.mutate(data.id) + deleteMutation.mutate(filter.id) } const handleMobileNav = (e: any, href: string) => { @@ -183,7 +177,7 @@ export default function FilterDetails() {