Skip to content

Commit

Permalink
Update end date in resample query to include interval for last window…
Browse files Browse the repository at this point in the history
… bucket (#853)

* Update end date in resample query

Signed-off-by: GBBBAS <[email protected]>

* Update to API Tests due to HTTPX update

Signed-off-by: GBBBAS <[email protected]>

---------

Signed-off-by: GBBBAS <[email protected]>
  • Loading branch information
GBBBAS authored Jan 9, 2025
1 parent a426994 commit 94ef54a
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ def _sample_query(parameters_dict: dict) -> tuple:
"`{{ business_unit|lower }}`.`sensors`.`{{ asset|lower }}_{{ data_security_level|lower }}_events_{{ data_type|lower }}` "
"{% endif %}"
"{% if case_insensitivity_tag_search is defined and case_insensitivity_tag_search == true %}"
"WHERE `{{ timestamp_column }}` BETWEEN to_timestamp(\"{{ start_date }}\") AND to_timestamp(\"{{ end_date }}\") AND UPPER(`{{ tagname_column }}`) IN ('{{ tag_names | join('\\', \\'') | upper }}') "
"WHERE `{{ timestamp_column }}` BETWEEN to_timestamp(\"{{ start_date }}\") AND timestampadd({{ time_interval_unit }}, {{ time_interval_rate }}, to_timestamp(\"{{ end_date }}\")) AND UPPER(`{{ tagname_column }}`) IN ('{{ tag_names | join('\\', \\'') | upper }}') "
"{% else %}"
"WHERE `{{ timestamp_column }}` BETWEEN to_timestamp(\"{{ start_date }}\") AND to_timestamp(\"{{ end_date }}\") AND `{{ tagname_column }}` IN ('{{ tag_names | join('\\', \\'') }}') "
"WHERE `{{ timestamp_column }}` BETWEEN to_timestamp(\"{{ start_date }}\") AND timestampadd({{ time_interval_unit }}, {{ time_interval_rate }}, to_timestamp(\"{{ end_date }}\")) AND `{{ tagname_column }}` IN ('{{ tag_names | join('\\', \\'') }}') "
"{% endif %}"
"{% if include_status is defined and include_status == true and include_bad_data is defined and include_bad_data == false %} AND `{{ status_column }}` <> 'Bad' {% endif %}) "
',date_array AS (SELECT explode(sequence(from_utc_timestamp(to_timestamp("{{ start_date }}"), "{{ time_zone }}"), from_utc_timestamp(to_timestamp("{{ end_date }}"), "{{ time_zone }}"), INTERVAL \'{{ time_interval_rate + \' \' + time_interval_unit }}\')) AS timestamp_array) '
",window_buckets AS (SELECT timestamp_array AS window_start, timestampadd({{time_interval_unit }}, {{ time_interval_rate }}, timestamp_array) AS window_end FROM date_array) "
",window_buckets AS (SELECT timestamp_array AS window_start, timestampadd({{ time_interval_unit }}, {{ time_interval_rate }}, timestamp_array) AS window_end FROM date_array) "
",resample AS (SELECT /*+ RANGE_JOIN(d, {{ range_join_seconds }} ) */ d.window_start, d.window_end, e.`{{ tagname_column }}`, {{ agg_method }}(e.`{{ value_column }}`) OVER (PARTITION BY e.`{{ tagname_column }}`, d.window_start ORDER BY e.`{{ timestamp_column }}` ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `{{ value_column }}` FROM window_buckets d INNER JOIN raw_events e ON d.window_start <= e.`{{ timestamp_column }}` AND d.window_end > e.`{{ timestamp_column }}`) "
",project AS (SELECT window_start AS `{{ timestamp_column }}`, `{{ tagname_column }}`, `{{ value_column }}` FROM resample GROUP BY window_start, `{{ tagname_column }}`, `{{ value_column }}` "
"{% if is_resample is defined and is_resample == true %}"
Expand Down
18 changes: 9 additions & 9 deletions tests/api/v1/test_api_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
RawResponse,
)
from pandas.io.json import build_table_schema
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport
from src.api.v1 import app
from src.api.v1.common import json_response_batch
from src.sdk.python.rtdip_sdk.queries.time_series import batch
Expand Down Expand Up @@ -87,7 +87,7 @@ async def test_api_batch_single_get_success(mocker: MockerFixture):
mock_lookup = "src.api.v1.batch.lookup_before_get"
mocked_lookup_before_get = mocker.patch(mock_lookup, return_value=None)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -175,7 +175,7 @@ async def test_api_batch_single_get_success_with_lookup(mocker: MockerFixture):
mock_lookup = "src.api.v1.batch.lookup_before_get"
mocked_lookup_before_get = mocker.patch(mock_lookup, return_value=test_data)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -255,7 +255,7 @@ async def test_api_batch_single_post_success(mocker: MockerFixture):
# Make a surveillance batch method reference to check if called and what args with
surveillance_batch = mocker.patch(mock_method, return_value=mock_method_return_data)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -300,7 +300,7 @@ async def test_api_batch_single_get_unsupported_route_error(mocker: MockerFixtur
os.environ, {"DATABRICKS_SERVING_ENDPOINT": MOCK_MAPPING_ENDPOINT_URL}
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -343,7 +343,7 @@ async def test_api_batch_single_post_missing_body_error(mocker: MockerFixture):
os.environ, {"DATABRICKS_SERVING_ENDPOINT": MOCK_MAPPING_ENDPOINT_URL}
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -406,7 +406,7 @@ async def test_api_batch_multiple_success(mocker: MockerFixture):
# Make a surveillance batch method reference to check if called and what args with
surveillance_batch = mocker.patch(mock_method, side_effect=mock_patch_side_effect)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -464,7 +464,7 @@ async def test_api_batch_one_success_one_fail(mocker: MockerFixture):
os.environ, {"DATABRICKS_SERVING_ENDPOINT": MOCK_MAPPING_ENDPOINT_URL}
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -515,7 +515,7 @@ async def test_api_batch_one_success_one_fail(mocker: MockerFixture):
os.environ, {"DATABRICKS_SERVING_ENDPOINT": MOCK_MAPPING_ENDPOINT_URL}
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down
20 changes: 10 additions & 10 deletions tests/api/v1/test_api_circular_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
MOCK_TAG_MAPPING_EMPTY,
MOCK_MAPPING_ENDPOINT_URL,
)
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport
from src.api.v1 import app

MOCK_METHOD = "src.sdk.python.rtdip_sdk.queries.time_series.circular_average.get"
Expand All @@ -42,7 +42,7 @@
async def test_api_circular_average_get_success(mocker: MockerFixture, api_test_data):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -59,7 +59,7 @@ async def test_api_circular_average_get_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -82,7 +82,7 @@ async def test_api_circular_average_get_error(mocker: MockerFixture, api_test_da
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -97,7 +97,7 @@ async def test_api_circular_average_get_error(mocker: MockerFixture, api_test_da
async def test_api_circular_average_post_success(mocker: MockerFixture, api_test_data):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -115,7 +115,7 @@ async def test_api_circular_average_post_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -139,7 +139,7 @@ async def test_api_circular_average_post_error(mocker: MockerFixture, api_test_d
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -182,7 +182,7 @@ async def test_api_circular_average_get_lookup_success(mocker: MockerFixture):
modified_param_dict = CIRCULAR_AVERAGE_MOCKED_PARAMETER_DICT.copy()
del modified_param_dict["business_unit"]

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.get(
MOCK_API_NAME, headers=TEST_HEADERS, params=modified_param_dict
)
Expand Down Expand Up @@ -228,7 +228,7 @@ async def test_api_circular_average_post_lookup_success(mocker: MockerFixture):
modified_param_dict = CIRCULAR_AVERAGE_MOCKED_PARAMETER_DICT.copy()
del modified_param_dict["business_unit"]

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down Expand Up @@ -278,7 +278,7 @@ async def test_api_circular_average_get_lookup_no_tag_map_error(mocker: MockerFi
modified_param_dict["tagname"] = ["NonExistentTag"]
del modified_param_dict["business_unit"]

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
actual = await ac.get(
MOCK_API_NAME, headers=TEST_HEADERS, params=modified_param_dict
)
Expand Down
14 changes: 7 additions & 7 deletions tests/api/v1/test_api_circular_standard_deviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
TEST_HEADERS,
BASE_URL,
)
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport
from src.api.v1 import app

MOCK_METHOD = (
Expand All @@ -41,7 +41,7 @@ async def test_api_circular_standard_deviation_get_success(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -58,7 +58,7 @@ async def test_api_circular_standard_deviation_get_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -83,7 +83,7 @@ async def test_api_circular_standard_deviation_get_error(
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -100,7 +100,7 @@ async def test_api_circular_standard_deviation_post_success(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -118,7 +118,7 @@ async def test_api_circular_standard_deviation_post_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -144,7 +144,7 @@ async def test_api_circular_standard_deviation_post_error(
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down
14 changes: 7 additions & 7 deletions tests/api/v1/test_api_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
TEST_HEADERS,
BASE_URL,
)
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport
from src.api.v1 import app

MOCK_METHOD = "src.sdk.python.rtdip_sdk.queries.time_series.interpolate.get"
Expand All @@ -37,7 +37,7 @@
async def test_api_interpolate_get_success(mocker: MockerFixture, api_test_data):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -54,7 +54,7 @@ async def test_api_interpolate_get_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -77,7 +77,7 @@ async def test_api_interpolate_get_error(mocker: MockerFixture, api_test_data):
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -92,7 +92,7 @@ async def test_api_interpolate_get_error(mocker: MockerFixture, api_test_data):
async def test_api_interpolate_post_success(mocker: MockerFixture, api_test_data):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -110,7 +110,7 @@ async def test_api_interpolate_post_validation_error(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -134,7 +134,7 @@ async def test_api_interpolate_post_error(mocker: MockerFixture, api_test_data):
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down
14 changes: 7 additions & 7 deletions tests/api/v1/test_api_interpolation_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
TEST_HEADERS,
BASE_URL,
)
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport
from src.api.v1 import app

MOCK_METHOD = "src.sdk.python.rtdip_sdk.queries.time_series.interpolation_at_time.get"
Expand All @@ -38,7 +38,7 @@ async def test_api_interpolation_at_time_get_success(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -57,7 +57,7 @@ async def test_api_interpolation_at_time_get_success(
# )
# mocker = mocker_setup(mocker, MOCK_METHOD, test_data)

# async with AsyncClient(app=app, base_url=BASE_URL) as ac:
# async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
# response = await ac.get(
# MOCK_API_NAME,
# headers=TEST_HEADERS,
Expand All @@ -82,7 +82,7 @@ async def test_api_interpolation_at_time_get_error(
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.get(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -99,7 +99,7 @@ async def test_api_interpolation_at_time_post_success(
):
mocker = mocker_setup(mocker, MOCK_METHOD, api_test_data["mock_data_agg"])

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand All @@ -119,7 +119,7 @@ async def test_api_interpolation_at_time_post_success(
# )
# mocker = mocker_setup(mocker, MOCK_METHOD, test_data)

# async with AsyncClient(app=app, base_url=BASE_URL) as ac:
# async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
# response = await ac.post(
# MOCK_API_NAME,
# headers=TEST_HEADERS,
Expand All @@ -145,7 +145,7 @@ async def test_api_interpolation_at_time_post_error(
Exception("Error Connecting to Database"),
)

async with AsyncClient(app=app, base_url=BASE_URL) as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url=BASE_URL) as ac:
response = await ac.post(
MOCK_API_NAME,
headers=TEST_HEADERS,
Expand Down
Loading

0 comments on commit 94ef54a

Please sign in to comment.