-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from oslokommune/collect-measurements
Initial function for importing KPIs
- Loading branch information
Showing
10 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import asyncio | ||
import logging | ||
from pathlib import Path | ||
|
||
from aiohttp import ClientSession, ClientResponseError | ||
from aws_xray_sdk.core import patch_all, xray_recorder | ||
from okdata.aws import ssm | ||
from okdata.aws.logging import logging_wrapper | ||
|
||
from common import dataplatform, util | ||
|
||
patch_all() | ||
logging.basicConfig() | ||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
MEASUREMENTS = { | ||
"fdeYObXg1OpTh1XxXPJ4": "dataspeilet-antall-malinger-i-origo", | ||
"iRznHuvBqx2ketpA8keT": "dataspeilet-etterlevelse-av-oppdateringshyppighet", | ||
} | ||
|
||
|
||
async def collect_measurements(measurements): | ||
# Fetch values for all measurements | ||
logger.info(f"Fetching data for {len(measurements)} measurements") | ||
api_key = ssm.get_secret("/dataplatform/okr-tracker/api-key") | ||
|
||
async with ClientSession( | ||
headers={"x-api-key": api_key}, | ||
base_url=util.getenv("OKR_TRACKER_API_BASE_URL"), | ||
raise_for_status=True, | ||
) as session: | ||
kpis = await asyncio.gather( | ||
*[fetch_measurement(session, kpi_id) for kpi_id in measurements.keys()] | ||
) | ||
|
||
measurements_data = {measurement_id: values for measurement_id, values in kpis} | ||
|
||
# Upload data to dataset | ||
for measurement_id, dataset_id in measurements.items(): | ||
measurement_values = measurements_data[measurement_id] | ||
|
||
if measurement_values is None: | ||
logger.warning( | ||
f"No data for measurement '{measurement_id}'; skipping import!" | ||
) | ||
continue | ||
|
||
logger.info( | ||
f"Uploading {len(measurement_values)} measurement values for to dataset '{dataset_id}'" | ||
) | ||
|
||
csv_file = util.write_dict_to_csv( | ||
filename=Path("/") / "tmp" / f"{dataset_id}_values.csv", | ||
data=measurement_values, | ||
fieldnames=["date", "value", "comment"], | ||
extrasaction="ignore", | ||
) | ||
|
||
dataplatform.upload_dataset(dataset_id, csv_file.name) | ||
|
||
|
||
async def fetch_measurement(session, measurement_id): | ||
try: | ||
async with session.get(f"/kpi/{measurement_id}/values") as response: | ||
response_data = await response.json() | ||
return ( | ||
measurement_id, | ||
[ | ||
{ | ||
"date": v["date"], | ||
"value": v["value"], | ||
"comment": (v["comment"] or "").replace("\n", " "), | ||
} | ||
for v in response_data | ||
], | ||
) | ||
except ClientResponseError as e: | ||
logger.error( | ||
"Error while fetching measurement `{}`: {}".format( | ||
measurement_id, | ||
str(e), | ||
) | ||
) | ||
return (measurement_id, None) | ||
|
||
|
||
@logging_wrapper | ||
@xray_recorder.capture("collect_monitors") | ||
def import_data(event, context): | ||
asyncio.run(collect_measurements(MEASUREMENTS)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import re | ||
|
||
import pytest | ||
from unittest.mock import patch | ||
|
||
from okdata.aws import ssm | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mocked_ssm_get_secret(): | ||
with patch.object(ssm, "get_secret", return_value="foo-token"): | ||
yield | ||
|
||
|
||
class AsyncMock: | ||
def __init__(self, status_code, data): | ||
self.status = status_code | ||
self._response_data = data | ||
|
||
async def __aenter__(self): | ||
return self | ||
|
||
async def __aexit__(self, *error_info): | ||
return self | ||
|
||
async def json(self): | ||
return self._response_data | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_client(monkeypatch, response_data): | ||
def mock_client_get(self, url): | ||
# /kpi/{measurement_id}/values | ||
if match := re.search(r"\/kpi\/(?P<measurement_id>[a-zA-Z0-9]+)\/values$", url): | ||
measurement_values = response_data.get(match.group("measurement_id")) | ||
if measurement_values is not None: | ||
return AsyncMock(200, measurement_values) | ||
return AsyncMock(404, None) | ||
|
||
monkeypatch.setattr("aiohttp.ClientSession.get", mock_client_get) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
import pytest | ||
from aws_xray_sdk.core import xray_recorder | ||
from unittest.mock import patch | ||
|
||
import measurements.handler as handler | ||
from common import dataplatform | ||
|
||
xray_recorder.begin_segment("Test") | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"response_data", | ||
[ | ||
{ | ||
"foo456": [ | ||
{ | ||
"value": 0.14, | ||
"date": "2024-02-18", | ||
"comment": "foooo", | ||
"created": "2024-02-18T20:03:12.871Z", | ||
}, | ||
{ | ||
"value": 0.7, | ||
"date": "2024-01-10", | ||
"comment": None, | ||
"created": "2024-01-10T20:03:12.871Z", | ||
}, | ||
], | ||
"bar456": [], | ||
} | ||
], | ||
) | ||
@patch.object(dataplatform, "upload_dataset") | ||
async def test_collect_measurements( | ||
mock_dataset_upload, | ||
mock_client, | ||
response_data, | ||
mocker, | ||
): | ||
measurements = { | ||
"foo456": "foo-dataset", | ||
"bar456": "bar-dataset", | ||
"baz789": "baz-dataset", | ||
} | ||
|
||
await handler.collect_measurements(measurements) | ||
|
||
for measurement_id, dataset_id in measurements.items(): | ||
csv_file_path = Path("/") / "tmp" / f"{dataset_id}_values.csv" | ||
|
||
response_values = response_data.get(measurement_id) | ||
|
||
if response_values is None: | ||
assert not csv_file_path.exists() | ||
continue | ||
|
||
with open(csv_file_path, "r") as csv_file: | ||
reader = csv.DictReader(csv_file) | ||
records = list(reader) | ||
|
||
assert reader.dialect == "excel" | ||
assert reader.fieldnames == ["date", "value", "comment"] | ||
|
||
assert len(records) == len(response_values) | ||
|
||
for i, record in enumerate(records): | ||
assert record == { | ||
field: str(response_values[i][field] or "") | ||
for field in reader.fieldnames | ||
} | ||
|
||
assert ( | ||
mocker.call(dataset_id, str(csv_file_path)) | ||
in mock_dataset_upload.call_args_list | ||
) | ||
|
||
assert mock_dataset_upload.call_count == len(response_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters