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

Better performance time controller #225

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PSRClassesInterface"
uuid = "1eab49e5-27d8-4905-b9f6-327b6ea666c4"
version = "0.17.0"
version = "0.17.1"

[deps]
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
Expand Down
35 changes: 27 additions & 8 deletions src/PSRDatabaseSQLite/time_controller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const CollectionAttribute = Tuple{String, String}

# Some comments
# TODO we can further optimize the time controller with a few strategies
# 1 - We can try to ask for the data in the same query that we ask for the dates. I just don`t know how to write the good query for that
# 2 - We can use prepared statements for the queries
# 3 - Avoid querying the data for every id in the attribute. Currently we fill the cache of dates before making the query and use it to inform which date each id should query. This is quite inneficient
# The best way of optimizing it would be to solve 1 and 2.
# 1 - We can use prepared statements for the queries

mutable struct TimeControllerCache{T}
data::Vector{T}
Expand All @@ -20,8 +17,6 @@ mutable struct TimeControllerCache{T}
closest_next_date_with_data::Vector{DateTime}

# Private caches with the closest previous and next dates
# _closest_previous_date_with_data = maximum(closest_previous_date_with_data)
# _closest_next_date_with_data = minimum(closest_next_date_with_data)
_closest_global_previous_date_with_data::DateTime
_closest_global_next_date_with_data::DateTime

Expand All @@ -44,12 +39,36 @@ function _update_time_controller_cache!(
date_time::DateTime,
)
_update_time_controller_cache_dates!(cache, db, attribute, date_time)
_request_time_series_data_for_time_controller_cache(cache, db, attribute)

return nothing
end

function _request_time_series_data_for_time_controller_cache(
cache::TimeControllerCache,
db,
attribute::Attribute,
)
query = "SELECT id, $(attribute.id) FROM $(attribute.table_where_is_located) WHERE "
for (i, id) in enumerate(cache._collection_ids)
cache.data[i] =
_request_time_series_data_for_time_controller_cache(db, attribute, id, cache.closest_previous_date_with_data[i])
query *= "(id = $id AND DATETIME(date_time) = DATETIME('$(cache.closest_previous_date_with_data[i])'))"
if i < length(cache._collection_ids)
query *= " OR "
end
end
query *= " ORDER BY id;"

df = DBInterface.execute(db.sqlite_db, query) |> DataFrame

_psrdatabasesqlite_null_value(attribute.type)
for (i, id) in enumerate(cache._collection_ids)
index = searchsorted(df.id, id)
if isempty(index)
cache.data[i] = _psrdatabasesqlite_null_value(attribute.type)
else
cache.data[i] = df[index[1], 2]
end
end
return nothing
end

Expand Down
Loading