Skip to content

Commit 791e7fd

Browse files
Merge pull request #48 from Nasdaq/release-0.5.x
release-0.5.x
2 parents a7ffb34 + 79d4070 commit 791e7fd

7 files changed

Lines changed: 75 additions & 66 deletions

File tree

ncdssdk/src/main/python/ncdsclient/consumer/NasdaqKafkaAvroConsumer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def get_kafka_consumer(self, stream_name, timestamp=None):
9393
if auto_offset_cfg == "earliest" or auto_offset_cfg == "smallest" or auto_offset_cfg == "beginning":
9494
self.logger.debug(
9595
f"Auto offset reset config set to: {auto_offset_cfg}")
96-
return SeekToMidnight.seek_to_midnight_at_past_day(kafka_consumer, topic_partition, 0)
96+
return SeekToMidnight.seek_to_midnight_at_past_day(kafka_consumer, topic_partition, 0, self.kafka_props.get(self.kafka_config_loader.TIMEOUT))
9797

9898
else:
9999
return kafka_consumer
@@ -105,7 +105,7 @@ def get_kafka_consumer(self, stream_name, timestamp=None):
105105
self.logger.debug(
106106
"offset: " + str(topic_partition.offset) + ", timestamp: " + str(timestamp))
107107
offsets_for_times = kafka_consumer.offsets_for_times(
108-
[topic_partition], self.kafka_props.get(self.kafka_config_loader.TIMEOUT))
108+
[topic_partition], self.kafka_cfg.TIMEOUT)
109109
except Exception as e:
110110
self.logger.exception(e)
111111
sys.exit(0)
@@ -132,7 +132,7 @@ def get_consumer(self, avro_schema, stream_name):
132132
a :class:`.KafkaAvroConsumer` instance with a key and value deserializer set through the avro_schema parameter
133133
"""
134134
if 'group.id' not in self.kafka_props:
135-
self.kafka_props[self.kafka_config_loader.GROUP_ID_CONFIG] = f'{self.client_ID}_{stream_name}_{datetime.datetime.today().day}'
135+
self.kafka_props[self.kafka_config_loader.GROUP_ID_CONFIG] = f'{self.client_ID}'
136136
return KafkaAvroConsumer(self.kafka_props, avro_schema)
137137

138138
def get_schema_for_topic(self, topic):

ncdssdk/src/main/python/ncdsclient/internal/ReadSchemaTopic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def __init__(self):
3232

3333
def read_schema(self, topic):
3434
auth_config_loader = AuthenticationConfigLoader()
35-
schema_consumer = self.get_consumer(
36-
"Control-" + auth_config_loader.get_client_id(self.security_props))
35+
schema_consumer = self.get_consumer(auth_config_loader.get_client_id(self.security_props))
3736
latest_record = None
3837
num_messages = self.kafka_props[self.kafka_config_loader.NUM_MESSAGES]
3938
timeout = self.kafka_props[self.kafka_config_loader.TIMEOUT]
@@ -46,6 +45,7 @@ def read_schema(self, topic):
4645
try:
4746
msg_val = message.value()
4847

48+
latest_record = None
4949
if "name" in msg_val and msg_val["name"] == topic:
5050
latest_record = message
5151
if latest_record and 'schema' in msg_val:
@@ -105,7 +105,7 @@ def get_consumer(self, client_id):
105105
self.kafka_props = KafkaConfigLoader.load_test_config()
106106

107107
self.kafka_props[self.kafka_config_loader.AUTO_OFFSET_RESET_CONFIG] = 'earliest'
108-
self.kafka_props[self.kafka_config_loader.GROUP_ID_CONFIG] = f'{client_id}1'
108+
self.kafka_props[self.kafka_config_loader.GROUP_ID_CONFIG] = f'{client_id}'
109109

110110
kafka_avro_consumer = KafkaAvroConsumer(
111111
self.kafka_props, ctrl_msg_schema)
@@ -115,7 +115,7 @@ def get_consumer(self, client_id):
115115

116116
kafka_avro_consumer.assign([topic_partition])
117117

118-
return SeekToMidnight.seek_to_midnight_at_past_day(kafka_avro_consumer, topic_partition, 7)
118+
return SeekToMidnight.seek_to_midnight_at_past_day(kafka_avro_consumer, topic_partition, 6, self.kafka_props[self.kafka_config_loader.TIMEOUT])
119119

120120
def internal_schema(self, topic):
121121
try:

ncdssdk/src/main/python/ncdsclient/internal/utils/SeekToMidnight.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import logging
22
import sys
33
from confluent_kafka import OFFSET_BEGINNING, OFFSET_INVALID
4-
from datetime import datetime, date, timedelta, time
4+
from datetime import datetime, timedelta
5+
import pytz
56

67
logger = logging.getLogger(__name__)
78

89

9-
def seek_to_midnight_at_past_day(kafka_avro_consumer, topic_partition, num_days_ago=0):
10+
def seek_to_midnight_at_past_day(kafka_avro_consumer, topic_partition, num_days_ago=0, timeout=10):
1011
topic_partition.offset = get_timestamp_at_midnight(num_days_ago)
1112
logger.debug(
1213
f"Num days ago: {num_days_ago}. Setting partition offset to timestamp: {topic_partition.offset}")
1314
try:
1415
logger.debug(f"topic partition: {topic_partition}")
1516
offsets_for_times = kafka_avro_consumer.offsets_for_times(
16-
[topic_partition], timeout=5)
17+
[topic_partition], timeout=timeout)
1718
except Exception as e:
1819
logger.exception(e)
1920
sys.exit(0)
@@ -34,6 +35,5 @@ def seek_to_midnight_at_past_day(kafka_avro_consumer, topic_partition, num_days_
3435

3536

3637
def get_timestamp_at_midnight(num_days_ago=0):
37-
past_day = date.today()-timedelta(days=num_days_ago)
38-
midnight = datetime.combine(past_day, time.min)
39-
return int(midnight.timestamp() * 1000)
38+
midnight = datetime.now(pytz.timezone('America/New_York')).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=num_days_ago)
39+
return int(midnight.timestamp() * 1000)

ncdssdk_client/src/main/python/ncdsclient/NCDSSession.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def __init__(self, cmd):
3131
self.logger = logging.getLogger(__name__)
3232

3333
self.logger = logging.getLogger(__name__)
34-
self.ncds_client = None
3534

3635
def main(self):
3736
self.security_cfg = load_auth_properties(self.auth_props_file)
@@ -41,42 +40,47 @@ def main(self):
4140
cmd_to_validate = ValidateInput(self.cmd)
4241
cmd_to_validate.validate_user_input()
4342

43+
ncds_client = None
44+
4445
try:
45-
self.ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
4646
if self.test_option == "TOP":
4747
self.top_cmd()
4848

4949
elif self.test_option == "SCHEMA":
50+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
5051
# Dump the Schema for the self.topic
51-
schema = self.ncds_client.get_schema_for_topic(self.topic)
52+
schema = ncds_client.get_schema_for_topic(self.topic)
5253
print("Schema for the Topic:" + self.topic)
5354
if schema:
5455
print(schema)
5556
else:
5657
print(" Access to topic is not granted ")
5758

5859
elif self.test_option == "GETMSG":
60+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
5961
print("Finding the message")
6062
if "auto.offset.reset" in self.kafka_cfg and self.kafka_cfg["auto.offset.reset"] == "latest":
6163
print("Need to get run GETMSG with 'earliest' offset")
6264
sys.exit(0)
63-
msg = self.ncds_client.get_sample_messages(
65+
msg = ncds_client.get_sample_messages(
6466
self.topic, self.message_name, False)
6567
if msg is not None:
6668
print(msg)
6769
else:
6870
print("Message Not Found ...")
6971

7072
elif self.test_option == "GETALLMSGS":
73+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
7174
print("Finding the messages")
7275
if "auto.offset.reset" in self.kafka_cfg and self.kafka_cfg["auto.offset.reset"] == "latest":
7376
print("Need to run GETMSG with 'earliest' offset")
7477
sys.exit(0)
75-
self.ncds_client.get_sample_messages(
78+
ncds_client.get_sample_messages(
7679
self.topic, self.message_name, True)
7780

7881
elif self.test_option == "TOPICS":
79-
self.topics = self.ncds_client.list_topics_for_client()
82+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
83+
self.topics = ncds_client.list_topics_for_client()
8084
print("List of streams available on Nasdaq Cloud Data Service:")
8185
for self.topic in self.topics:
8286
print(self.topic)
@@ -90,9 +94,10 @@ def main(self):
9094
logging.exception(e)
9195

9296
def top_cmd(self):
97+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
9398
numOfRecords = max(10, min(int(self.num_top_messages), 999))
94-
records = self.ncds_client.top_messages(
95-
self.topic) if not self.timestamp else self.ncds_client.top_messages(self.topic, self.timestamp)
99+
records = ncds_client.top_messages(
100+
self.topic) if not self.timestamp else ncds_client.top_messages(self.topic, self.timestamp)
96101
print("Top " + str(numOfRecords) +
97102
" Records for the Topic: " + self.topic)
98103
if records:
@@ -108,12 +113,13 @@ def top_cmd(self):
108113
print("Access to topic is not granted")
109114

110115
def cont_stream_cmd(self):
111-
consumer = self.ncds_client.ncds_kafka_consumer(
112-
self.topic) if not self.timestamp else self.ncds_client.ncds_kafka_consumer(self.topic, self.timestamp)
116+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
117+
consumer = ncds_client.ncds_kafka_consumer(
118+
self.topic) if not self.timestamp else ncds_client.ncds_kafka_consumer(self.topic, self.timestamp)
113119

114120
try:
115121
while True:
116-
message = consumer.poll(self.kafka_cfg[self.kafka_config_loader.TIMEOUT])
122+
message = consumer.poll(self.kafka_cfg[self.kafka_config_loader.NUM_MESSAGES])
117123
if message is None:
118124
print(f"No Records Found for the Topic: {self.topic}")
119125
else:
@@ -134,8 +140,9 @@ def filter_stream_cmd(self):
134140
if self.msgnames is not None:
135141
msgname_set = set(self.msgnames.split(","))
136142

137-
consumer = self.ncds_client.ncds_kafka_consumer(
138-
self.topic) if not self.timestamp else self.ncds_client.ncds_kafka_consumer(self.topic, self.timestamp)
143+
ncds_client = NCDSClient(self.security_cfg, self.kafka_cfg)
144+
consumer = ncds_client.ncds_kafka_consumer(
145+
self.topic) if not self.timestamp else ncds_client.ncds_kafka_consumer(self.topic, self.timestamp)
139146

140147
try:
141148
while True:

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ oauthlib>=3.1.0,<4
44
requests>=2.25.1,<3
55
requests-oauthlib>=1.3.0,<2
66
avro>=1.10.2,<2
7+
pytz>=2023.3

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ urllib3==1.26.6
3434
# via requests
3535
zipp==3.4.1
3636
# via importlib-metadata
37+
pytz==2023.3

setup.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
from setuptools import setup, find_packages
2-
import pathlib
3-
4-
here = pathlib.Path(__file__).parent.resolve()
5-
6-
# Get long description from README
7-
long_description = (here / 'README.md').read_text(encoding='utf-8')
8-
9-
setup(
10-
name='ncdssdk',
11-
version='0.4.0',
12-
description='A Python SDK for developing applications to access the NCDS API',
13-
long_description=long_description,
14-
long_description_content_type='text/markdown',
15-
author='Nasdaq',
16-
classifiers=[
17-
# Indicate who your project is intended for
18-
'Intended Audience :: Developers',
19-
'Topic :: Software Development :: Build Tools',
20-
21-
# Pick your license as you wish
22-
'License :: OSI Approved :: Apache Software License',
23-
24-
# Specify the Python versions you support here. In particular, ensure
25-
# that you indicate you support Python 3. These classifiers are *not*
26-
# checked by 'pip install'. See instead 'python_requires' below.
27-
'Programming Language :: Python :: 3.9',
28-
],
29-
keywords='Nasdaq, NCDS, ncdssdk',
30-
packages=find_packages(),
31-
python_requires='>=3.9, <4',
32-
install_requires=open("requirements.in").readlines(),
33-
34-
include_package_data=True,
35-
# We could possibly use entry_points parameter with console_scripts here to specify the NCDSSession script
36-
37-
project_urls={ # Optional
38-
'Source': 'https://github.com/Nasdaq/NasdaqCloudDataService-SDK-Python',
39-
},
40-
)
1+
from setuptools import setup, find_packages
2+
import pathlib
3+
4+
here = pathlib.Path(__file__).parent.resolve()
5+
6+
# Get long description from README
7+
long_description = (here / 'README.md').read_text(encoding='utf-8')
8+
9+
setup(
10+
name='ncdssdk',
11+
version='0.5.0',
12+
description='A Python SDK for developing applications to access the NCDS API',
13+
long_description=long_description,
14+
long_description_content_type='text/markdown',
15+
author='Nasdaq',
16+
classifiers=[
17+
# Indicate who your project is intended for
18+
'Intended Audience :: Developers',
19+
'Topic :: Software Development :: Build Tools',
20+
21+
# Pick your license as you wish
22+
'License :: OSI Approved :: Apache Software License',
23+
24+
# Specify the Python versions you support here. In particular, ensure
25+
# that you indicate you support Python 3. These classifiers are *not*
26+
# checked by 'pip install'. See instead 'python_requires' below.
27+
'Programming Language :: Python :: 3.9',
28+
],
29+
keywords='Nasdaq, NCDS, ncdssdk',
30+
packages=find_packages(),
31+
python_requires='>=3.9, <4',
32+
install_requires=open("requirements.in").readlines(),
33+
34+
include_package_data=True,
35+
# We could possibly use entry_points parameter with console_scripts here to specify the NCDSSession script
36+
37+
project_urls={ # Optional
38+
'Source': 'https://github.com/Nasdaq/NasdaqCloudDataService-SDK-Python',
39+
},
40+
)

0 commit comments

Comments
 (0)