Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: limit positions to set elevation for to last 10 days #4228

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions lib/teslamate/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,35 @@ defmodule TeslaMate.Log do
def get_positions_without_elevation(min_id \\ 0, opts \\ []) do
limit = Keyword.get(opts, :limit, 100)

date_earliest =
cond do
min_id == 0 ->
DateTime.add(DateTime.utc_now(), -10, :day)

true ->
DateTime.from_iso8601("2003-07-01T00:00:00Z")
end

naive_date_earliest = DateTime.to_naive(date_earliest)

non_streamed_drives =
Repo.all(
from d in subquery(
from p in Position,
select: %{
drive_id: p.drive_id,
streamed_count:
count()
|> filter(not is_nil(p.odometer) and is_nil(p.ideal_battery_range_km))
},
where: not is_nil(p.drive_id),
group_by: p.drive_id
),
select: d.drive_id,
where: d.streamed_count == 0
from p in Position,
select: p.drive_id,
inner_join: d in assoc(p, :drive),
where: d.start_date > ^naive_date_earliest,
having:
count()
|> filter(not is_nil(p.odometer) and is_nil(p.ideal_battery_range_km)) == 0,
group_by: p.drive_id
)

Position
|> where([p], p.id > ^min_id and is_nil(p.elevation) and p.drive_id in ^non_streamed_drives)
|> where(
[p],
p.id > ^min_id and is_nil(p.elevation) and p.drive_id in ^non_streamed_drives and
brianmay marked this conversation as resolved.
Show resolved Hide resolved
p.date > ^naive_date_earliest
)
|> order_by(asc: :id)
|> limit(^limit)
|> Repo.all()
Expand Down
Loading