refactor: split getFullRelations into smaller helpers
This commit is contained in:
@@ -148,33 +148,28 @@ func (c *Client) currentOnlyRelation(ctx context.Context, id int) ([]RelationEnt
|
|||||||
}}, nil
|
}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFullRelations returns related anime based on watch order, with parallel fetching (3 concurrent).
|
func (c *Client) handleWatchOrderError(ctx context.Context, id int, err error) ([]RelationEntry, error) {
|
||||||
func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry, error) {
|
if errors.Is(err, watchorder.ErrWatchOrderNotFound) {
|
||||||
result, err := c.getWatchOrder(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, watchorder.ErrWatchOrderNotFound) {
|
|
||||||
return c.currentOnlyRelation(ctx, id)
|
|
||||||
}
|
|
||||||
observability.Warn(
|
|
||||||
"relations_watch_order_fallback_current_only",
|
|
||||||
"jikan",
|
|
||||||
"",
|
|
||||||
map[string]any{
|
|
||||||
"anime_id": id,
|
|
||||||
},
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
return c.currentOnlyRelation(ctx, id)
|
return c.currentOnlyRelation(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
type fetchResult struct {
|
observability.Warn(
|
||||||
index int
|
"relations_watch_order_fallback_current_only",
|
||||||
anime Anime
|
"jikan",
|
||||||
entry watchorder.WatchOrderEntry
|
"",
|
||||||
}
|
map[string]any{
|
||||||
|
"anime_id": id,
|
||||||
|
},
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
|
||||||
var allowedEntries []watchorder.WatchOrderEntry
|
return c.currentOnlyRelation(ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildAllowedWatchOrderEntries(result watchorder.WatchOrderResult) ([]watchorder.WatchOrderEntry, map[int]bool) {
|
||||||
|
allowedEntries := make([]watchorder.WatchOrderEntry, 0, len(result.WatchOrder))
|
||||||
seen := make(map[int]bool)
|
seen := make(map[int]bool)
|
||||||
|
|
||||||
for _, entry := range result.WatchOrder {
|
for _, entry := range result.WatchOrder {
|
||||||
if len(allowedEntries) >= maxWatchOrderEntries {
|
if len(allowedEntries) >= maxWatchOrderEntries {
|
||||||
break
|
break
|
||||||
@@ -182,16 +177,21 @@ func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry,
|
|||||||
if !isAllowedWatchOrderType(entry.Type) || seen[entry.ID] {
|
if !isAllowedWatchOrderType(entry.Type) || seen[entry.ID] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
seen[entry.ID] = true
|
seen[entry.ID] = true
|
||||||
allowedEntries = append(allowedEntries, entry)
|
allowedEntries = append(allowedEntries, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return allowedEntries, seen
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) fetchRelationResults(ctx context.Context, entries []watchorder.WatchOrderEntry) []fetchResult {
|
||||||
g, gCtx := errgroup.WithContext(ctx)
|
g, gCtx := errgroup.WithContext(ctx)
|
||||||
g.SetLimit(3)
|
g.SetLimit(3)
|
||||||
|
|
||||||
results := make(chan fetchResult, len(allowedEntries))
|
results := make(chan fetchResult, len(entries))
|
||||||
|
|
||||||
for i, entry := range allowedEntries {
|
for i, entry := range entries {
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
anime, err := c.GetAnimeByID(gCtx, entry.ID)
|
anime, err := c.GetAnimeByID(gCtx, entry.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -201,10 +201,12 @@ func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry,
|
|||||||
c.EnqueueAnimeFetchRetry(gCtx, entry.ID, err)
|
c.EnqueueAnimeFetchRetry(gCtx, entry.ID, err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case results <- fetchResult{index: i, anime: anime, entry: entry}:
|
case results <- fetchResult{index: i, anime: anime, entry: entry}:
|
||||||
case <-gCtx.Done():
|
case <-gCtx.Done():
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -214,18 +216,21 @@ func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry,
|
|||||||
close(results)
|
close(results)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fetched := make([]fetchResult, 0, len(allowedEntries))
|
fetched := make([]fetchResult, 0, len(entries))
|
||||||
for res := range results {
|
for res := range results {
|
||||||
fetched = append(fetched, res)
|
fetched = append(fetched, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-sort because they might have finished out of order
|
|
||||||
sort.Slice(fetched, func(i, j int) bool {
|
sort.Slice(fetched, func(i, j int) bool {
|
||||||
return fetched[i].index < fetched[j].index
|
return fetched[i].index < fetched[j].index
|
||||||
})
|
})
|
||||||
|
|
||||||
relations := make([]RelationEntry, 0, len(fetched)+1)
|
return fetched
|
||||||
for _, res := range fetched {
|
}
|
||||||
|
|
||||||
|
func buildRelationsFromResults(results []fetchResult, id int) []RelationEntry {
|
||||||
|
relations := make([]RelationEntry, 0, len(results)+1)
|
||||||
|
for _, res := range results {
|
||||||
relations = append(relations, RelationEntry{
|
relations = append(relations, RelationEntry{
|
||||||
Anime: res.anime,
|
Anime: res.anime,
|
||||||
Relation: watchOrderTypeLabel(res.entry.Type),
|
Relation: watchOrderTypeLabel(res.entry.Type),
|
||||||
@@ -234,18 +239,46 @@ func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if !seen[id] {
|
return relations
|
||||||
currentAnime, err := c.GetAnimeByID(ctx, id)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
relations = append([]RelationEntry{{
|
func (c *Client) ensureCurrentRelation(ctx context.Context, id int, seen map[int]bool, relations []RelationEntry) ([]RelationEntry, error) {
|
||||||
Anime: currentAnime,
|
if seen[id] {
|
||||||
Relation: "Current",
|
return relations, nil
|
||||||
IsCurrent: true,
|
}
|
||||||
IsExtra: false,
|
|
||||||
}}, relations...)
|
currentAnime, err := c.GetAnimeByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return append([]RelationEntry{{
|
||||||
|
Anime: currentAnime,
|
||||||
|
Relation: "Current",
|
||||||
|
IsCurrent: true,
|
||||||
|
IsExtra: false,
|
||||||
|
}}, relations...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type fetchResult struct {
|
||||||
|
index int
|
||||||
|
anime Anime
|
||||||
|
entry watchorder.WatchOrderEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFullRelations returns related anime based on watch order, with parallel fetching (3 concurrent).
|
||||||
|
func (c *Client) GetFullRelations(ctx context.Context, id int) ([]RelationEntry, error) {
|
||||||
|
result, err := c.getWatchOrder(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return c.handleWatchOrderError(ctx, id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedEntries, seen := buildAllowedWatchOrderEntries(result)
|
||||||
|
fetched := c.fetchRelationResults(ctx, allowedEntries)
|
||||||
|
relations := buildRelationsFromResults(fetched, id)
|
||||||
|
relations, err = c.ensureCurrentRelation(ctx, id, seen, relations)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relations) == 0 {
|
if len(relations) == 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user