forked from galadriel-ai/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle.py
65 lines (51 loc) · 2.08 KB
/
oracle.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
import asyncio
import json
import settings
from src.repositories.ipfs_repository import IpfsRepository
from src.repositories.web3.chat_repository import Web3ChatRepository
from src.repositories.web3.function_repository import Web3FunctionRepository
from src.repositories.web3.knowledge_base_repository import Web3KnowledgeBaseRepository
from src.repositories.knowledge_base_repository import KnowledgeBaseRepository
from src.service import chat_service
from src.service import functions_service
from src.service import knowledge_base_indexing_service
from src.service import knowledge_base_query_service
web3_chat_repository = Web3ChatRepository()
web3_function_repository = Web3FunctionRepository()
web3_kb_repository = Web3KnowledgeBaseRepository()
ipfs_repository = IpfsRepository()
kb_repository = KnowledgeBaseRepository(max_size=settings.KNOWLEDGE_BASE_CACHE_MAX_SIZE)
repositories = [web3_chat_repository, web3_function_repository, web3_kb_repository]
async def collect_and_save_metrics():
while True:
metrics = {
"transactions_sent": 0,
"errors": 0,
}
for repo in repositories:
repo_metrics = repo.get_metrics().copy()
metrics["transactions_sent"] += repo_metrics.pop(
"transactions_sent", 0
)
metrics["errors"] += repo_metrics.pop("errors", 0)
metrics.update(repo_metrics)
with open("metrics.json", "w") as f:
json.dump(metrics, f)
print("Metrics saved to file.")
await asyncio.sleep(10)
async def main():
tasks = [
chat_service.execute(web3_chat_repository, ipfs_repository),
functions_service.execute(web3_function_repository),
knowledge_base_indexing_service.execute(
web3_kb_repository, ipfs_repository, kb_repository
),
knowledge_base_query_service.execute(
web3_kb_repository, ipfs_repository, kb_repository
),
collect_and_save_metrics(),
]
print("Oracle started!")
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())