Skip to content

Commit

Permalink
Merge pull request #225 from psrenergy/gb/better-performance-time-con…
Browse files Browse the repository at this point in the history
…troller

Better performance time controller
  • Loading branch information
guilhermebodin authored Aug 12, 2024
2 parents 940a09c + 1f5ff93 commit e589790
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
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

2 comments on commit e589790

@guilhermebodin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112960

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.17.1 -m "<description of version>" e589790441a28206924dfb482431598298c316bb
git push origin v0.17.1

Please sign in to comment.