Skip to content

Commit

Permalink
limit positions to set elevation for to last 10 days
Browse files Browse the repository at this point in the history
  • Loading branch information
swiffer committed Sep 28, 2024
1 parent 6871ef7 commit a7bc48d
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/teslamate/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,30 @@ defmodule TeslaMate.Log do

def get_positions_without_elevation(min_id \\ 0, opts \\ []) do
limit = Keyword.get(opts, :limit, 100)
now = DateTime.utc_now()

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: %{
drive_id: p.drive_id
},
join: d in assoc(p, :drive),
where:
p.date > ^DateTime.add(now, -10, :day) and
d.start_date > ^DateTime.add(now, -10, :day),
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
p.date > ^DateTime.add(now, -10, :day)
)
|> order_by(asc: :id)
|> limit(^limit)
|> Repo.all()
Expand Down

0 comments on commit a7bc48d

Please sign in to comment.