-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathutils.py
159 lines (142 loc) · 5.01 KB
/
utils.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
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# Knowage, Open Source Business Intelligence suite
# Copyright (C) 2016 Engineering Ingegneria Informatica S.p.A.
#
# Knowage is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Knowage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pandas as pd
import requests
from app.utilities import constants, security, cuncurrency_manager as cm
from datetime import datetime, timedelta
import os
import xml.etree.ElementTree as ET
import json
import pkg_resources
def findFreePort():
import socket
import random
s = socket.socket()
count = 0
range = getPortsRange()
lower_bound = int(range[0])
upper_bound = int(range[1])
while count < upper_bound - lower_bound:
count += 1
p = random.randint(lower_bound, upper_bound)
try:
s.bind(('', p)) #bind to a free port provided by the host
return s.getsockname()[1] #return the port number assigned
except:
pass
return None
def retrieveScriptInfo(data):
script = data.get("script")
output_variable = data.get('output_variable')
return script, output_variable
def retrieveKnowageInfo(headers):
user_id = headers['Authorization']
return user_id
def retrieveDatasetInfo(data):
dataset_name = data.get('dataset')
datastore_request = data['datastore_request']
return dataset_name, datastore_request
def retrieveWidgetInfo(data):
document_id = data['document_id']
widget_id = data['widget_id']
return document_id, widget_id
def retrieveAnalyticalDriversInfo(data):
drivers = data.get("drivers")
return drivers
def getDatasetAsDataframe(widget):
address = "http://" + widget.knowage_address + "/knowage/restful-services/2.0/datasets/" + widget.dataset_name + "/data?offset=0&size=-1"
auth_token = security.buildAuthToken(widget.user_id)
headers = {'Authorization': auth_token}
#rest request for dataset
payload = widget.datastore_request
r = requests.post(address, headers=headers, data=payload)
#retrieve column names from metadata
names = r.json()["metaData"]["fields"]
column_names = []
column_types = {}
for x in names:
if type(x) is dict:
column_names.append(x['header'])
if x["type"] == "float":
column_types.update({x['name']: "float64"})
elif x["type"] == "float":
column_types.update({x['name']: "int64"})
#save data as dataframe
df = pd.DataFrame(r.json()["rows"])
#cast types
df = df.astype(column_types)
#drop first column (redundant)
df.drop(columns=['id'], inplace=True)
# assign column names
df.columns = column_names
return df
def serverExists(id):
with cm.lck:
if id in cm.active_servers.keys():
return True
return False
def destroyServer(id):
# retrieve server and thread to be stopped and stop them
with cm.lck:
bokeh_server = cm.active_servers[id]
del cm.active_servers[id]
th = cm.bokeh_resources[id].thread
del cm.bokeh_resources[id]
bokeh_server.io_loop.stop()
bokeh_server.stop()
th.join()
def bokehGarbageCollector():
to_destroy = []
now = datetime.now()
with cm.lck:
for widget_id, res in cm.bokeh_resources.items():
if now - res.timestamp > timedelta(hours=1):
# add widget to destroy list
to_destroy.append(widget_id)
# delete temp files
os.remove(constants.TMP_FOLDER + "bokeh_script_" + str(widget_id) + ".txt")
if res.dataset_name is not None:
dataset_file = constants.TMP_FOLDER + res.dataset_name + ".pckl"
os.remove(dataset_file)
for w in to_destroy:
destroyServer(w)
def getHMACKey():
tree = ET.parse(constants.CONFIG_FILE)
root = tree.getroot()
key = root[0][0].text
return key
def getKnowageAddress():
tree = ET.parse(constants.CONFIG_FILE)
root = tree.getroot()
addr = root[0][1].text
return addr
def getPythonAddress():
tree = ET.parse(constants.CONFIG_FILE)
root = tree.getroot()
addr = root[0][2].text
return addr
def getPortsRange():
tree = ET.parse(constants.CONFIG_FILE)
root = tree.getroot()
range = root[0][3].text
return range.split("-")
def getEnvironmentLibraries():
to_return = []
for d in pkg_resources.working_set:
lib = str(d).split(" ")
to_return.append({"name": lib[0], "version": lib[1]})
return json.dumps(to_return)