-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartup.py
141 lines (98 loc) · 4.35 KB
/
startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# System libraries
import os
import sys
sys.path.append(os.path.dirname(__file__))
# Third party libraries
from dotenv import load_dotenv
load_dotenv()
from telegram import Bot
# Local libraries
from data_access.metadata.metadata_client import MongoDBClient as MetadataClient
from data_access.file.file_client import AzureFileClient, LocalFileClient
from data_access.vector.pinecone_client import PineconeClient
from data_access.cache.cache_client import MongoDBClient as CacheClient
from MyLogger import logger
from simple_profiler import SimpleProfiler
metadata_client = None
file_client = None
vector_client = None
cache_client = None
telegram_bot = None
telegram_chat_id = None
profiler = SimpleProfiler()
def get_clients() -> tuple[MetadataClient, AzureFileClient|LocalFileClient, PineconeClient]:
# TODO remove this function in favor of individual client getters
global metadata_client, file_client, vector_client
if metadata_client is not None and file_client is not None and vector_client is not None:
return metadata_client, file_client, vector_client
profiler.start("[get_clients] get environment")
ENVIRONMENT:str = get_environment()
logger.info(f"ENVIRONMENT : {ENVIRONMENT}")
profiler.start("[get_clients] initialize file client")
if ENVIRONMENT == "LOCAL":
file_client = LocalFileClient(os.getenv("LOCAL_FILE_ROOT"))
elif ENVIRONMENT == "AZURE":
file_client = AzureFileClient(os.getenv("AZURE_STORAGE_BLOB_TDPS_CONNECTION_STRING"))
profiler.start("[get_clients] initialize metadata client")
metadata_client = MetadataClient(os.getenv("MONGODB_CONNECTION_STRING"))
profiler.start("[get_clients] initialize vector client")
vector_client = PineconeClient(os.getenv("PINECONE_API_KEY"))
profiler.stop()
logger.info(profiler.print_statistics())
logger.info("Clients initialized successfully")
return metadata_client, file_client, vector_client
def get_metadata_client() -> MetadataClient:
global metadata_client
if metadata_client is not None:
return metadata_client
ENVIRONMENT:str = get_environment()
metadata_client = MetadataClient(os.getenv("MONGODB_CONNECTION_STRING"))
logger.info(f"Metadata client for environment {ENVIRONMENT} initialized successfully")
return metadata_client
def get_file_client() -> AzureFileClient|LocalFileClient:
global file_client
if file_client is not None:
return file_client
ENVIRONMENT:str = get_environment()
if ENVIRONMENT == "LOCAL":
file_client = LocalFileClient(os.getenv("LOCAL_FILE_ROOT"))
elif ENVIRONMENT == "AZURE":
file_client = AzureFileClient(os.getenv("AZURE_STORAGE_BLOB_TDPS_CONNECTION_STRING"))
logger.info(f"File client for environment {ENVIRONMENT} initialized successfully")
return file_client
def get_vector_client() -> PineconeClient:
global vector_client
if vector_client is not None:
return vector_client
ENVIRONMENT:str = get_environment()
vector_client = PineconeClient(os.getenv("PINECONE_API_KEY"))
logger.info(f"Vector client for environment {ENVIRONMENT} initialized successfully")
return vector_client
def get_cache_client() -> CacheClient:
global cache_client
if cache_client is not None:
return cache_client
ENVIRONMENT:str = get_environment()
cache_client = CacheClient(os.getenv("MONGODB_CONNECTION_STRING"))
logger.info(f"Cache client for environment {ENVIRONMENT} initialized successfully")
return cache_client
def get_telegram_bot() -> Bot:
global telegram_bot
global telegram_chat_id
if telegram_bot is not None:
return telegram_bot, telegram_chat_id
if os.getenv('TELEGRAM_TOKEN') is not None and os.getenv('TELEGRAM_CHAT_ID') is not None:
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
telegram_bot = Bot(token=TELEGRAM_TOKEN)
telegram_chat_id = TELEGRAM_CHAT_ID
logger.info("Created telegram bot")
return telegram_bot, telegram_chat_id
def get_environment() -> str:
ENVIRONMENT = os.getenv("ENVIRONMENT")
if ENVIRONMENT is None: ENVIRONMENT = "LOCAL"
ENVIRONMENT = ENVIRONMENT.upper()
if ENVIRONMENT not in ["LOCAL", "AZURE"]:
raise ValueError("Invalid environment")
return ENVIRONMENT
logger.info(f"ENVIRONMENT : {get_environment()}")