hawk-sdk 0.0.5__py3-none-any.whl → 0.0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of hawk-sdk might be problematic. Click here for more details.

@@ -4,12 +4,9 @@
4
4
  """
5
5
  from typing import List
6
6
 
7
- from google.cloud import bigquery
8
-
9
- from hawk_sdk.core.common.constants import PROJECT_ID
10
- from hawk_sdk.core.common.data_object import DataObject
11
7
  from hawk_sdk.api.futures.repository import FuturesRepository
12
8
  from hawk_sdk.api.futures.service import FuturesService
9
+ from hawk_sdk.core.common.data_object import DataObject
13
10
 
14
11
 
15
12
  class Futures:
@@ -17,8 +14,7 @@ class Futures:
17
14
 
18
15
  def __init__(self, environment="production") -> None:
19
16
  """Initializes the Futures datasource with required configurations."""
20
- self.connector = bigquery.Client(project=PROJECT_ID)
21
- self.repository = FuturesRepository(self.connector, environment=environment)
17
+ self.repository = FuturesRepository(environment=environment)
22
18
  self.service = FuturesService(self.repository)
23
19
 
24
20
  def get_ohlcvo(self, start_date: str, end_date: str, interval: str, hawk_ids: List[int]) -> DataObject:
@@ -34,3 +30,15 @@ class Futures:
34
30
  name="futures_ohlcvo",
35
31
  data=self.service.get_ohlcvo(start_date, end_date, interval, hawk_ids)
36
32
  )
33
+
34
+ def get_snapshot(self, timestamp: str, hawk_ids: List[int]) -> DataObject:
35
+ """Fetch snapshot data for the specified timestamp and hawk_ids.
36
+
37
+ :param timestamp: The timestamp for the data query (YYYY-MM-DD HH:MM:SS).
38
+ :param hawk_ids: A list of specific hawk_ids to filter by.
39
+ :return: A hawk DataObject containing the snapshot data.
40
+ """
41
+ return DataObject(
42
+ name="futures_snapshot",
43
+ data=self.service.get_snapshot(timestamp, hawk_ids)
44
+ )
@@ -6,24 +6,23 @@ import logging
6
6
  from typing import Iterator, List
7
7
 
8
8
  from google.cloud import bigquery
9
- from google.cloud.bigquery import Client
9
+
10
+ from hawk_sdk.core.common.utils import get_bigquery_client
10
11
 
11
12
 
12
13
  class FuturesRepository:
13
14
  """Repository for accessing Futures raw data."""
14
15
 
15
- def __init__(self, bq_client: Client, environment: str) -> None:
16
+ def __init__(self, environment: str) -> None:
16
17
  """Initializes the repository with a BigQuery client.
17
18
 
18
- :param bq_client: An instance of BigQuery Client.
19
19
  :param environment: The environment to fetch data from (e.g., 'production', 'development').
20
20
  """
21
- self.bq_client = bq_client
21
+ self.bq_client = get_bigquery_client()
22
22
  self.environment = environment
23
23
 
24
24
  def fetch_ohlcvo(self, start_date: str, end_date: str, interval: str, hawk_ids: List[int]) -> Iterator[dict]:
25
- """Fetches raw data from BigQuery for the given date range and hawk_ids using query parameters."""
26
-
25
+ """Fetches raw OHLCVO data from BigQuery for the given date range and hawk_ids using query parameters."""
27
26
  query = f"""
28
27
  WITH records_data AS (
29
28
  SELECT
@@ -85,3 +84,60 @@ class FuturesRepository:
85
84
  except Exception as e:
86
85
  logging.error(f"Failed to fetch OHLCVO data: {e}")
87
86
  raise
87
+
88
+ def fetch_snapshot(self, timestamp: str, hawk_ids: List[int]) -> Iterator[dict]:
89
+ """Fetches the most recent snapshot data from BigQuery for the given time and hawk_ids."""
90
+ query = f"""
91
+ WITH records_data AS (
92
+ SELECT
93
+ r.record_timestamp AS date,
94
+ hi.value AS ticker,
95
+ MAX(CASE WHEN f.field_name = 'close_snapshot' THEN r.double_value END) AS close_snapshot,
96
+ MAX(CASE WHEN f.field_name = 'high_snapshot' THEN r.double_value END) AS high_snapshot,
97
+ MAX(CASE WHEN f.field_name = 'low_snapshot' THEN r.double_value END) AS low_snapshot,
98
+ MAX(CASE WHEN f.field_name = 'cvol_snapshot' THEN r.int_value END) AS cvol_snapshot,
99
+ MAX(CASE WHEN f.field_name = 'bid_snapshot' THEN r.double_value END) AS bid_snapshot,
100
+ MAX(CASE WHEN f.field_name = 'ask_snapshot' THEN r.double_value END) AS ask_snapshot
101
+ FROM
102
+ `wsb-hc-qasap-ae2e.{self.environment}.records` AS r
103
+ JOIN
104
+ `wsb-hc-qasap-ae2e.{self.environment}.fields` AS f
105
+ ON r.field_id = f.field_id
106
+ JOIN
107
+ `wsb-hc-qasap-ae2e.{self.environment}.hawk_identifiers` AS hi
108
+ ON r.hawk_id = hi.hawk_id
109
+ WHERE
110
+ r.hawk_id IN UNNEST(@hawk_ids)
111
+ AND f.field_name IN ('close_snapshot', 'high_snapshot', 'low_snapshot', 'cvol_snapshot', 'bid_snapshot', 'ask_snapshot')
112
+ AND r.record_timestamp <= @timestamp
113
+ GROUP BY
114
+ date, ticker
115
+ )
116
+ SELECT DISTINCT
117
+ date,
118
+ ticker,
119
+ close_snapshot,
120
+ high_snapshot,
121
+ low_snapshot,
122
+ cvol_snapshot,
123
+ bid_snapshot,
124
+ ask_snapshot
125
+ FROM
126
+ records_data
127
+ ORDER BY
128
+ date;
129
+ """
130
+
131
+ query_params = [
132
+ bigquery.ArrayQueryParameter("hawk_ids", "INT64", hawk_ids),
133
+ bigquery.ScalarQueryParameter("timestamp", "DATETIME", timestamp)
134
+ ]
135
+
136
+ job_config = bigquery.QueryJobConfig(query_parameters=query_params)
137
+
138
+ try:
139
+ query_job = self.bq_client.query(query, job_config=job_config)
140
+ return query_job.result()
141
+ except Exception as e:
142
+ logging.error(f"Failed to fetch snapshot data: {e}")
143
+ raise
@@ -31,6 +31,16 @@ class FuturesService:
31
31
  raw_data = self.repository.fetch_ohlcvo(start_date, end_date, interval, hawk_ids)
32
32
  return self._normalize_data(raw_data)
33
33
 
34
+ def get_snapshot(self, timestamp: str, hawk_ids: List[int]) -> pd.DataFrame:
35
+ """Fetches snapshot data and normalizes it into a pandas DataFrame.
36
+
37
+ :param timestamp: The timestamp for the data query (YYYY-MM-DD HH:MM:SS).
38
+ :param hawk_ids: A list of specific hawk_ids to filter by.
39
+ :return: A pandas DataFrame containing the normalized snapshot data.
40
+ """
41
+ raw_data = self.repository.fetch_snapshot(timestamp, hawk_ids)
42
+ return self._normalize_data(raw_data)
43
+
34
44
  @staticmethod
35
45
  def _normalize_data(data: Iterator[dict]) -> pd.DataFrame:
36
46
  """Converts raw data into a normalized pandas DataFrame.
@@ -4,12 +4,9 @@
4
4
  """
5
5
  from typing import List
6
6
 
7
- from google.cloud import bigquery
8
-
9
- from hawk_sdk.core.common.constants import PROJECT_ID
10
- from hawk_sdk.core.common.data_object import DataObject
11
7
  from hawk_sdk.api.system.repository import SystemRepository
12
8
  from hawk_sdk.api.system.service import SystemService
9
+ from hawk_sdk.core.common.data_object import DataObject
13
10
 
14
11
 
15
12
  class System:
@@ -17,8 +14,7 @@ class System:
17
14
 
18
15
  def __init__(self, environment="production") -> None:
19
16
  """Initializes the System datasource with required configurations."""
20
- self.connector = bigquery.Client(project=PROJECT_ID)
21
- self.repository = SystemRepository(self.connector, environment=environment)
17
+ self.repository = SystemRepository(environment=environment)
22
18
  self.service = SystemService(self.repository)
23
19
 
24
20
  def get_hawk_ids(self, tickers: List[str]) -> DataObject:
@@ -6,19 +6,20 @@ import logging
6
6
  from typing import Iterator, List
7
7
 
8
8
  from google.cloud import bigquery
9
- from google.cloud.bigquery import Client
9
+
10
+ from hawk_sdk.core.common.utils import get_bigquery_client
10
11
 
11
12
 
12
13
  class SystemRepository:
13
14
  """Repository for accessing System data."""
14
15
 
15
- def __init__(self, bq_client: Client, environment: str) -> None:
16
+ def __init__(self, environment: str) -> None:
16
17
  """Initializes the repository with a BigQuery client.
17
18
 
18
19
  :param bq_client: An instance of BigQuery Client.
19
20
  :param environment: The environment to fetch data from (e.g., 'production', 'development').
20
21
  """
21
- self.bq_client = bq_client
22
+ self.bq_client = get_bigquery_client()
22
23
  self.environment = environment
23
24
 
24
25
  def fetch_hawk_ids(self, tickers: List[str]) -> Iterator[dict]:
@@ -0,0 +1,21 @@
1
+ import json
2
+ import os
3
+
4
+ from google.cloud import bigquery
5
+ from google.oauth2 import service_account
6
+
7
+ from hawk_sdk.core.common.constants import PROJECT_ID
8
+
9
+
10
+ def get_bigquery_client() -> bigquery.Client:
11
+ if 'GOOGLE_APPLICATION_CREDENTIALS' in os.environ:
12
+ # Initialize the client using the json file in the environment variable
13
+ return bigquery.Client(project=PROJECT_ID)
14
+
15
+ else:
16
+ # Load the service account credentials from the JSON string
17
+ service_account_json = os.environ.get('SERVICE_ACCOUNT_JSON')
18
+ credentials = service_account.Credentials.from_service_account_info(
19
+ json.loads(service_account_json)
20
+ )
21
+ return bigquery.Client(project=PROJECT_ID, credentials=credentials)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hawk-sdk
3
- Version: 0.0.5
3
+ Version: 0.0.7
4
4
  Requires-Dist: google-cloud-bigquery
5
5
  Requires-Dist: pandas
6
6
 
@@ -1,19 +1,20 @@
1
1
  hawk_sdk/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  hawk_sdk/api/__init__.py,sha256=BD8XgbQ2o4EIzECUu4h6g-9jzpg--BiT6brrnyO4Puc,90
3
3
  hawk_sdk/api/futures/__init__.py,sha256=9mL1L6wlZKXTvMrTuQV35Fl9kTy4qjf3RIJzYBXcpeM,46
4
- hawk_sdk/api/futures/main.py,sha256=jdt4IkdrZ1abcYfg-sih1eXb2uuVLspIst6yRNE1H1o,1557
5
- hawk_sdk/api/futures/repository.py,sha256=g8jSZCauCKwQYdjpZ3Oqe9NL8GjarDuSXDKpFl0J3vw,3588
6
- hawk_sdk/api/futures/service.py,sha256=kAzWLKeLXurD1qu8bCaFfjBwugZy2wtpdBmjibrdXmI,1564
4
+ hawk_sdk/api/futures/main.py,sha256=XNihiVb75wyPx78dOzEVWhQ6vmN-NLHZTk4L7zY-4WQ,1909
5
+ hawk_sdk/api/futures/repository.py,sha256=P6dsLJe8hQ2-hkDX_pa-VRoUUXaU-uBhA4S5mwK79_o,5983
6
+ hawk_sdk/api/futures/service.py,sha256=0zJn3xJNoHgEOeVVpIF2i0r5g8mvf4y1K_YnvHDF5mU,2078
7
7
  hawk_sdk/api/system/__init__.py,sha256=Oy8XUp5WHx4fczjZXzfbkkHluHW-t7BLN4IgrOG6Pk4,44
8
- hawk_sdk/api/system/main.py,sha256=FdpXaHp5yPFi17M1-1v4LU9vnwywkdYeOelps1hnKr0,1191
9
- hawk_sdk/api/system/repository.py,sha256=3CfFjNVC0X09TKLh9sfwd5HxeLcb49MRCbvb3Su2Wnk,1642
8
+ hawk_sdk/api/system/main.py,sha256=t2zWeEHSTYuWZAsqCMLt_CyRWoN8T8N6oEVshPgf6kk,1025
9
+ hawk_sdk/api/system/repository.py,sha256=IllwoWXZyi46INuCRQeibHJaMDR1TfbT6l2uAhii2Xc,1654
10
10
  hawk_sdk/api/system/service.py,sha256=LOZIfrEBKynJlj8V-4UM4abzVV9ogwUVxM7s2xFXlpo,1262
11
11
  hawk_sdk/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
12
  hawk_sdk/core/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  hawk_sdk/core/common/base_enum.py,sha256=ZgC8gZjTzsoJzzdgN2DjYk3dVL_uM2Stuaky5bo6GuQ,267
14
14
  hawk_sdk/core/common/constants.py,sha256=KmsNfRVCwGeXEBHfo3TzSaDYJDMtnL-YEQKsO1DnWV8,33
15
15
  hawk_sdk/core/common/data_object.py,sha256=f4YO415Zz-lm3QYJQ3sJ4ugH9ZX9Dc7T-JvO4IdeyOw,1073
16
- hawk_sdk-0.0.5.dist-info/METADATA,sha256=gi1GPSAdkCp-dAeCQ3c4ihvzs7w1BI7UlfJ59qwB1-g,112
17
- hawk_sdk-0.0.5.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
18
- hawk_sdk-0.0.5.dist-info/top_level.txt,sha256=aSjbudHcWSYsKXH9Wg0L1ltJfDOHXzjYFPO3v3cP-SE,9
19
- hawk_sdk-0.0.5.dist-info/RECORD,,
16
+ hawk_sdk/core/common/utils.py,sha256=bxF-iJzbqXGYtQKElH6k5-bSsdcvft0o51G9slZ1grI,745
17
+ hawk_sdk-0.0.7.dist-info/METADATA,sha256=TufZrqZ_Uezkfp5AKymLIUeuJKIWVWB4k3rdpLYmTlk,112
18
+ hawk_sdk-0.0.7.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
19
+ hawk_sdk-0.0.7.dist-info/top_level.txt,sha256=aSjbudHcWSYsKXH9Wg0L1ltJfDOHXzjYFPO3v3cP-SE,9
20
+ hawk_sdk-0.0.7.dist-info/RECORD,,