-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.py
151 lines (101 loc) · 4.43 KB
/
producer.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
142
143
144
145
146
147
from os.path import exists
import os
import requests
import json
import datetime
import time
from Logger import Logger
log = Logger('Producer')
from kafka import KafkaProducer
with open('config.json', 'r') as f:
config = json.load(f)
from time import sleep
# # One more example
# producer.send(topic1, key=b'event#2', value=b'This is a Kafka-Python basic tutorial')
# producer.flush()
#
class Prodcuer:
def __init__(self, arangoDBUrl,topicName,brokers):
self.arangoDBUrl = arangoDBUrl
self.topicName=topicName
self.brokers=brokers
self.producer = KafkaProducer(bootstrap_servers=brokers)
def getExectionPlanIDs(self,createdDate):
# createdDate='1676322519258'
query = "FOR u IN executionPlan FILTER u._created > xy and u._created>1676322519258 RETURN {_key:u._key}".replace(
'xy', createdDate)
# #
# query = "FOR u IN executionPlan FILTER u._created > xy RETURN {_key:u._key}".replace(
# 'xy', createdDate)
# query = "FOR u IN executionPlan FILTER u._created > xy and u._id in ['executionPlan/a2220c6f-cf0e-5fba-8f05-b04559a6a28c'] RETURN {_key:u._key}".replace(
# 'xy', createdDate)
# query = "FOR u IN executionPlan FILTER u._created > xy and u._id in ['executionPlan/14a6582e-669f-5977-b7c7-6d1fd487168f'] RETURN {_key:u._key}".replace(
# 'xy', createdDate)
#query="FOR u IN executionPlan FILTER u._created > xy and u._id=='executionPlan/b156e991-a538-5052-b513-1d25648906d8' RETURN {_key:u._key}".replace('xy',createdDate)
#query = "FOR u IN executionPlan FILTER u._id =='executionPlan/fdd38b4a-493f-5b9e-b462-28c07ac7fc07' RETURN u".replace('xy', createdDate)
#print(query)
log.info(query)
payload = json.dumps({
"query": query
})
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
response = requests.request("POST", self.arangoDBUrl, headers=headers, data=payload)
listKeys=json.loads(response.text)['result']
#print(listKeys)
log.info(listKeys)
return json.dumps(listKeys)
#return listKeys
def sendMessageToKafka(self,message):
if message:
try:
self.producer.send(self.topicName, value=message.encode('utf-8'))
self.producer.flush()
except Exception as e:
#print(e)
log.error(e)
# Topics/Brokers
class ProducerExecution:
def __init__(self):
self.topic1 = config['topic']
self.brokers = [config['kafka-broker']]
# self.url = "http://34.242.14.60:8529/_db/spline/_api/cursor"
self.url = config['arangoDBUrl']
self.p1 = Prodcuer(self.url,self.topic1,self.brokers)
def run(self):
path_to_file= "lastRun.json"
while True:
time.sleep(5)
file_exists = exists(path_to_file)
if (not file_exists or os.stat(path_to_file).st_size == 0):
unixtime ='0'
json.dumps({"lastRun":unixtime })
excutionplanIDS = self.p1.getExectionPlanIDs(unixtime)
with open('lastRun.json', 'w') as fcc_file:
fcc_file.write(json.dumps({"lastRun":unixtime }))
if json.loads(excutionplanIDS):
self.p1.sendMessageToKafka(excutionplanIDS)
unixtime = self.getCurrentLinuxTime()
with open('lastRun.json', 'w') as fcc_file:
dicty={'lastRun':unixtime}
fcc_file.write(json.dumps(dicty))
else:
with open('lastRun.json', 'r') as fcc_file:
txt=fcc_file.read()
fcc_data = json.loads(txt)
lastrun=fcc_data["lastRun"]
excutionplanIDS = self.p1.getExectionPlanIDs(lastrun)
if json.loads(excutionplanIDS):
self.p1.sendMessageToKafka(excutionplanIDS)
unixtime = self.getCurrentLinuxTime()
with open('lastRun.json', 'w') as fcc_file:
dicty={'lastRun':unixtime}
fcc_file.write(json.dumps(dicty))
def getCurrentLinuxTime(self):
d = datetime.datetime.now()
unixtime = str(int(datetime.datetime.timestamp(d) * 1000))
return str(int(unixtime))
producerEx=ProducerExecution()
producerEx.run()