fix status rate query

This commit is contained in:
Tao Bror Bojlén 2019-08-31 17:51:05 +01:00
parent 1f62f0efd5
commit 5a9508f3c9
No known key found for this signature in database
GPG key ID: C6EC7AAB905F9E6F
2 changed files with 11 additions and 9 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed error when viewing some uncrawlable instances - Fixed error when viewing some uncrawlable instances
- Fix navbar z-index - Fix navbar z-index
- Optimize query used for generating status rate
### Security ### Security

View file

@ -96,23 +96,24 @@ defmodule Backend.Scheduler do
}) })
instances = instances =
Crawl Instance
|> join(:inner, [c], c2 in subquery(earliest_crawl_subquery), |> join(:inner, [i], c in Crawl, on: i.domain == c.instance_domain)
on: c.instance_domain == c2.instance_domain |> join(:inner, [i], c2 in subquery(earliest_crawl_subquery),
on: i.domain == c2.instance_domain
) )
|> where( |> where(
[c, c2], [i, c, c2],
c.inserted_at > c2.earliest_crawl and not is_nil(c.statuses_seen) c.inserted_at > c2.earliest_crawl and c.statuses_seen > 0
) )
|> select([c], %{ |> select([i, c], %{
instance_domain: c.instance_domain, domain: i.domain,
status_count: sum(c.statuses_seen), status_count: sum(c.statuses_seen),
second_earliest_crawl: min(c.inserted_at) second_earliest_crawl: min(c.inserted_at)
}) })
|> group_by([c], c.instance_domain) |> group_by([i], i.domain)
|> Repo.all(timeout: :infinity) |> Repo.all(timeout: :infinity)
|> Enum.map(fn %{ |> Enum.map(fn %{
instance_domain: domain, domain: domain,
status_count: status_count, status_count: status_count,
second_earliest_crawl: oldest_timestamp second_earliest_crawl: oldest_timestamp
} -> } ->