-
Notifications
You must be signed in to change notification settings - Fork 4
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 #30 from 10-academy-w-9/frontend
Frontend init
- Loading branch information
Showing
36 changed files
with
20,687 additions
and
1,237 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class Config: | ||
SQLALCHEMY_DATABASE_URI = 'postgresql://test_user:password@localhost/test_db' | ||
SQLALCHEMY_DATABASE_URI = '' | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
JWT_SECRET_KEY = 'your_secret_key' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,69 @@ | ||
from confluent_kafka import Producer, Consumer, KafkaException | ||
import logging | ||
from confluent_kafka.admin import AdminClient, NewTopic | ||
from confluent_kafka import Producer, Consumer, KafkaException, KafkaError | ||
import json | ||
from decimal import Decimal | ||
|
||
class KafkaService: | ||
def __init__(self, brokers): | ||
self.brokers = brokers | ||
self.producer = Producer({'bootstrap.servers': brokers}) | ||
self.consumer = Consumer({ | ||
'bootstrap.servers': brokers, | ||
'group.id': 'backtest_group', | ||
'auto.offset.reset': 'earliest' | ||
}) | ||
self.admin_client = AdminClient({'bootstrap.servers': brokers}) | ||
|
||
def create_topic(self, topic): | ||
topic_metadata = self.admin_client.list_topics(timeout=10) | ||
if topic not in topic_metadata.topics: | ||
logging.info(f"Creating topic {topic}") | ||
new_topic = NewTopic(topic, num_partitions=1, replication_factor=1) | ||
fs = self.admin_client.create_topics([new_topic]) | ||
for topic, f in fs.items(): | ||
try: | ||
f.result() # The result itself is None | ||
logging.info(f"Topic {topic} created successfully") | ||
except Exception as e: | ||
logging.error(f"Failed to create topic {topic}: {str(e)}") | ||
else: | ||
logging.info(f"Topic {topic} already exists") | ||
|
||
def json_serializer(self, obj): | ||
if isinstance(obj, Decimal): | ||
return float(obj) | ||
raise TypeError("Type not serializable") | ||
|
||
def produce(self, topic, message): | ||
self.producer.produce(topic, key=None, value=json.dumps(message)) | ||
logging.info(f"Producing message to topic {topic}: {message}") | ||
serialized_message = json.dumps(message, default=self.json_serializer) | ||
self.producer.produce(topic, key=None, value=serialized_message) | ||
self.producer.flush() | ||
logging.info("Message produced successfully") | ||
|
||
def consume(self, topic, callback): | ||
self.create_topic(topic) | ||
self.consumer.subscribe([topic]) | ||
while True: | ||
msg = self.consumer.poll(timeout=1.0) | ||
if msg is None: | ||
continue | ||
if msg.error(): | ||
if msg.error().code() == KafkaError._PARTITION_EOF: | ||
logging.info(f"Subscribed to topic {topic}") | ||
try: | ||
while True: | ||
msg = self.consumer.poll(timeout=1.0) | ||
if msg is None: | ||
logging.debug("No message received") | ||
continue | ||
else: | ||
raise KafkaException(msg.error()) | ||
callback(json.loads(msg.value())) | ||
if msg.error(): | ||
if msg.error().code() == KafkaError._PARTITION_EOF: | ||
logging.info("End of partition reached") | ||
continue | ||
else: | ||
logging.error(f"Consumer error: {msg.error()}") | ||
raise KafkaException(msg.error()) | ||
logging.info(f"Received message: {msg.value()}") | ||
callback(json.loads(msg.value())) | ||
except Exception as e: | ||
logging.error(f"Error in Kafka consumer: {str(e)}") | ||
finally: | ||
self.consumer.close() | ||
|
||
kafka_service = KafkaService(brokers='localhost:9092') |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.