-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.py
128 lines (83 loc) · 3.49 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
from os.path import exists
import os
import requests
import json
import datetime
import time
from kafka import KafkaProducer
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):
query="FOR u IN executionPlan FILTER u._created > xy and u._id=='executionPlan/fc6211bc-7eea-50c3-9d7d-f88303054203' RETURN u".replace('xy',createdDate)
#query = "FOR u IN executionPlan FILTER u._id =='executionPlan/fdd38b4a-493f-5b9e-b462-28c07ac7fc07' RETURN u".replace('xy', createdDate)
print(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)
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)
# Topics/Brokers
class ProducerExecution:
def __init__(self):
self.topic1 = 'spline-topic'
self.brokers = ['192.168.100.11:9092']
self.url = "http://192.168.100.11:8529/_db/spline/_api/cursor"
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()