Repository navigation
feat: bigquery_fetcher entrypoint #67
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ dependencies = [ | |
| 'sentry-arroyo>=2.22.0' | ||
| ] | ||
|
|
||
| [project.optional-dependencies] | ||
| # Only needed for the `bigquery_fetcher` entrypoint | ||
| bigquery = ['google-cloud-bigquery>=3.0.0'] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to update the |
||
|
|
||
| [tool.setuptools.package-data] | ||
| usageaccountant = ["py.typed"] | ||
|
|
||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| sentry-arroyo>=2.22.0 | ||
| pytest>=7.2.1 | ||
| mypy>=1.4.1 | ||
| google-cloud-bigquery>=3.0.0 | ||
| black>=24.10.0 | ||
| flake8>=6 |
This file contains hidden or 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,212 @@ | ||
| import unittest | ||
| from datetime import date, datetime, timezone | ||
| from io import StringIO | ||
| from json import loads | ||
| from typing import Any, Iterable, List, Mapping, cast | ||
| from unittest.mock import Mock | ||
|
|
||
| from arroyo.backends.kafka.consumer import KafkaPayload | ||
| from arroyo.backends.local.backend import LocalBroker | ||
| from arroyo.backends.local.storages.memory import MemoryMessageStorage | ||
| from arroyo.types import Partition, Topic | ||
| from arroyo.utils.clock import MockedClock | ||
| from google.cloud import bigquery | ||
|
|
||
| from usageaccountant import accumulator | ||
| from usageaccountant import bigquery_fetcher as bqf | ||
| from usageaccountant.accumulator import UsageUnit | ||
| from usageaccountant.fetcher_utils import UsageAccumulatorRecord | ||
|
|
||
| # A day-aligned epoch (2026-06-11 00:00:00 GMT). | ||
| INVENTORY_TS = 1781136000 | ||
|
|
||
|
|
||
| class MockBQ: | ||
| def __init__(self, rows: Iterable[Mapping[str, Any]]): | ||
| self.rows = rows | ||
|
|
||
| def query(self, query: str) -> Mock: | ||
| result_fn = Mock(return_value=self.rows) | ||
| return Mock(result=result_fn) | ||
|
|
||
|
|
||
| class TestBigQueryFetcher(unittest.TestCase): | ||
| query_str = ( | ||
| '[{"query": "SELECT app_feature, amount FROM t", ' | ||
| '"unit": "bytes", "shared_resource_id": "gcs_objectstore"}]' | ||
| ) | ||
|
|
||
| def setUp(self) -> None: | ||
| storage: MemoryMessageStorage[KafkaPayload] = MemoryMessageStorage() | ||
| self.broker = LocalBroker(storage, MockedClock()) | ||
| self.topic = Topic("test_bq_fetcher") | ||
| self.broker.create_topic(self.topic, 1) | ||
|
|
||
| producer = self.broker.get_producer() | ||
| # granularity of 1s so an overridden timestamp is preserved verbatim. | ||
| self.usage_accumulator = accumulator.UsageAccumulator( | ||
| 1, topic_name="test_bq_fetcher", producer=producer | ||
| ) | ||
|
|
||
| def test_parse_and_assert_query_file(self) -> None: | ||
| assert bqf.parse_and_assert_query_file(StringIO(self.query_str)) | ||
|
|
||
| def test_parse_and_assert_query_file_empty(self) -> None: | ||
| with self.assertRaises(AssertionError): | ||
| bqf.parse_and_assert_query_file(StringIO("[]")) | ||
|
|
||
| def test_parse_and_assert_query_file_not_list(self) -> None: | ||
| with self.assertRaises(AssertionError): | ||
| bqf.parse_and_assert_query_file(StringIO('{"query": "x"}')) | ||
|
|
||
| def test_parse_and_assert_query_file_missing_query(self) -> None: | ||
| with self.assertRaises(AssertionError): | ||
| bqf.parse_and_assert_query_file( | ||
| StringIO('[{"unit": "bytes", "shared_resource_id": "x"}]') | ||
| ) | ||
|
|
||
| def test_parse_and_assert_query_file_missing_resource(self) -> None: | ||
| with self.assertRaises(AssertionError): | ||
| bqf.parse_and_assert_query_file( | ||
| StringIO('[{"query": "x", "unit": "bytes"}]') | ||
| ) | ||
|
|
||
| def test_parse_and_assert_query_file_missing_unit(self) -> None: | ||
| with self.assertRaises(AssertionError): | ||
| bqf.parse_and_assert_query_file( | ||
| StringIO('[{"query": "x", "shared_resource_id": "y"}]') | ||
| ) | ||
|
|
||
| def test_normalize_timestamp_none(self) -> None: | ||
| assert bqf.normalize_timestamp(None) is None | ||
|
|
||
| def test_normalize_timestamp_int(self) -> None: | ||
| assert bqf.normalize_timestamp(INVENTORY_TS) == INVENTORY_TS | ||
|
|
||
| def test_normalize_timestamp_float(self) -> None: | ||
| assert bqf.normalize_timestamp(INVENTORY_TS + 0.9) == INVENTORY_TS | ||
|
|
||
| def test_normalize_timestamp_datetime(self) -> None: | ||
| dt = datetime(2026, 6, 11, tzinfo=timezone.utc) | ||
| assert bqf.normalize_timestamp(dt) == INVENTORY_TS | ||
|
|
||
| def test_normalize_timestamp_date(self) -> None: | ||
| assert bqf.normalize_timestamp(date(2026, 6, 11)) == INVENTORY_TS | ||
|
|
||
| def test_normalize_timestamp_bool_rejected(self) -> None: | ||
| with self.assertRaises(TypeError): | ||
| bqf.normalize_timestamp(True) | ||
|
|
||
| def test_normalize_timestamp_unsupported_type(self) -> None: | ||
| with self.assertRaises(TypeError): | ||
| bqf.normalize_timestamp("2026-06-11") | ||
|
|
||
| def test_process_rows_with_timestamp(self) -> None: | ||
| rows: List[Mapping[str, Any]] = [ | ||
| { | ||
| "app_feature": "attachments", | ||
| "amount": 100, | ||
| "timestamp": INVENTORY_TS, | ||
| }, | ||
| { | ||
| "app_feature": "preprod", | ||
| "amount": 200, | ||
| "timestamp": INVENTORY_TS, | ||
| }, | ||
| ] | ||
| records = bqf.process_rows(rows, UsageUnit.BYTES, "gcs_objectstore") | ||
| assert records == [ | ||
| UsageAccumulatorRecord( | ||
| "gcs_objectstore", | ||
| "attachments", | ||
| 100, | ||
| UsageUnit.BYTES, | ||
| INVENTORY_TS, | ||
| ), | ||
| UsageAccumulatorRecord( | ||
| "gcs_objectstore", | ||
| "preprod", | ||
| 200, | ||
| UsageUnit.BYTES, | ||
| INVENTORY_TS, | ||
| ), | ||
| ] | ||
|
|
||
| def test_process_rows_without_timestamp(self) -> None: | ||
| rows: List[Mapping[str, Any]] = [ | ||
| {"app_feature": "attachments", "amount": 100} | ||
| ] | ||
| records = bqf.process_rows( | ||
| rows, UsageUnit.BYTES, "bigtable_objectstore" | ||
| ) | ||
| assert records == [ | ||
| UsageAccumulatorRecord( | ||
| "bigtable_objectstore", | ||
| "attachments", | ||
| 100, | ||
| UsageUnit.BYTES, | ||
| None, | ||
| ) | ||
| ] | ||
|
|
||
| def test_process_rows_skips_nulls(self) -> None: | ||
| rows: List[Mapping[str, Any]] = [ | ||
| {"app_feature": None, "amount": 100}, | ||
| {"app_feature": "attachments", "amount": None}, | ||
| {"app_feature": "preprod", "amount": 50}, | ||
| ] | ||
| records = bqf.process_rows(rows, UsageUnit.BYTES, "gcs_objectstore") | ||
| assert records == [ | ||
| UsageAccumulatorRecord( | ||
| "gcs_objectstore", "preprod", 50, UsageUnit.BYTES, None | ||
| ) | ||
| ] | ||
|
|
||
| def test_main_produces_records(self) -> None: | ||
| client = MockBQ( | ||
| [ | ||
| { | ||
| "app_feature": "attachments", | ||
| "amount": "123", | ||
| "timestamp": INVENTORY_TS, | ||
| } | ||
| ] | ||
| ) | ||
|
|
||
| bqf.main( | ||
| query_file=StringIO(self.query_str), | ||
| usage_accumulator=self.usage_accumulator, | ||
| bq_client=cast(bigquery.Client, client), | ||
| dry_run=False, | ||
| ) | ||
|
|
||
| msg = self.broker.consume(Partition(self.topic, 0), 0) | ||
| assert msg is not None | ||
| payload = loads(msg.payload.value.decode("utf-8")) | ||
| assert payload == { | ||
| "timestamp": INVENTORY_TS, | ||
| "shared_resource_id": "gcs_objectstore", | ||
| "app_feature": "attachments", | ||
| "usage_unit": "bytes", | ||
| "amount": 123, | ||
| } | ||
| assert self.broker.consume(Partition(self.topic, 0), 1) is None | ||
|
|
||
| def test_main_dry_run(self) -> None: | ||
| client = MockBQ( | ||
| [ | ||
| { | ||
| "app_feature": "attachments", | ||
| "amount": "123", | ||
| "timestamp": INVENTORY_TS, | ||
| } | ||
| ] | ||
| ) | ||
| bqf.main( | ||
| query_file=StringIO(self.query_str), | ||
| usage_accumulator=self.usage_accumulator, | ||
| bq_client=cast(bigquery.Client, client), | ||
| dry_run=True, | ||
| ) | ||
| # Nothing is produced in dry-run mode. | ||
| assert self.broker.consume(Partition(self.topic, 0), 0) is None |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.