/
utils.py
262 lines (206 loc) · 8.22 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""Various utility functions."""
# TODO split this module into categories (graph DB, analyses etc.)
# Improve maintainability index
# TODO: https://github.com/fabric8-analytics/fabric8-analytics-server/issues/373
import datetime
import os
import hashlib
import logging
from selinon import run_flow
from flask import current_app
from flask.json import JSONEncoder
import semantic_version as sv
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from f8a_worker.models import (WorkerResult, StackAnalysisRequest)
from f8a_worker.utils import json_serial, MavenCoordinates
from f8a_worker.setup_celery import init_celery
from .default_config import STACK_ANALYSIS_REQUEST_TIMEOUT
from sqlalchemy.exc import SQLAlchemyError
from f8a_utils.ingestion_utils import trigger_workerflow
logger = logging.getLogger(__name__)
# TODO remove hardcoded gremlin_url when moving to Production This is just
# a stop-gap measure for demo
gremlin_url = "http://{host}:{port}".format(
host=os.environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_HOST", "localhost"),
port=os.environ.get("BAYESIAN_GREMLIN_HTTP_SERVICE_PORT", "8182"))
companion_reason_statement = "along with the provided input stack. " \
"Do you want to consider adding this Package?"
zero_version = sv.Version("0.0.0")
ENABLE_BOOK_KEEPING_FLOW = os.environ.get('ENABLE_BOOK_KEEPING_FLOW', 'false') == 'true'
def server_run_flow(flow_name, flow_args):
"""Run a flow.
:param flow_name: name of flow to be run as stated in YAML config file
:param flow_args: arguments for the flow
:return: dispatcher ID handling flow
"""
logger.debug('Running flow %s', flow_name)
start = datetime.datetime.now()
init_celery(result_backend=False)
dispacher_id = run_flow(flow_name, flow_args)
logger.debug('It took %f seconds to start %s flow.',
(datetime.datetime.now() - start).total_seconds(), flow_name)
return dispacher_id
def get_user_email(user_profile):
"""Get user e-mail address or the default address if user profile does not exist."""
# fallback address
default_email = 'bayesian@redhat.com'
if user_profile is not None:
return user_profile.get('email', default_email)
else:
return default_email
def create_component_bookkeeping(ecosystem, packages_list, request_args, headers):
"""Run the component analysis for given ecosystem+package+version."""
if ENABLE_BOOK_KEEPING_FLOW:
payload = {
"external_request_id": headers.get('X-Request-Id', None),
"flowname": "componentApiFlow",
"data": {
"api_name": "component_analyses_post",
"manifest_hash": request_args.get('utm_content', None),
"ecosystem": ecosystem,
"packages_list": packages_list,
"user_id": headers.get('uuid', None),
"user_agent": headers.get('User-Agent', None),
"source": request_args.get('utm_source', None),
"telemetry_id": headers.get('X-Telemetry-Id', None)
}
}
try:
trigger_workerflow(payload)
except Exception as e:
logger.error('Failed to trigger worker flow for payload %s with error %s',
payload, e)
def server_create_analysis(ecosystem, package, version, user_profile,
api_flow=True, force=False, force_graph_sync=False):
"""Create bayesianApiFlow handling analyses for specified EPV.
:param ecosystem: ecosystem for which the flow should be run
:param package: package for which should be flow run
:param version: package version
:param force: force run flow even specified EPV exists
:param force_graph_sync: force synchronization to graph
:return: dispatcher ID handling flow
"""
# Bookkeeping first
component = MavenCoordinates.normalize_str(package) if ecosystem == 'maven' else package
args = {
'ecosystem': ecosystem,
'name': component,
'version': version,
'force': force,
'force_graph_sync': force_graph_sync
}
if api_flow:
return server_run_flow('bayesianApiFlow', args)
else:
return server_run_flow('bayesianFlow', args)
def get_system_version():
"""Get the actual version of the server.
It's usually set up from the F8A_SYSTEM_VERSION environment variable.
"""
try:
with open(current_app.config['SYSTEM_VERSION']) as f:
lines = f.readlines()
except OSError:
raise
ret = {}
for line in lines:
couple = line.strip().split(sep='=', maxsplit=1)
if len(couple) > 1:
ret[couple[0].lower()] = couple[1]
return ret
class JSONEncoderWithExtraTypes(JSONEncoder):
"""JSON Encoder that supports additional types.
- date/time objects
- arbitrary non-mapping iterables
"""
# TODO I already seen similar implementation elsewhere, probably a candidate for a package?
def default(self, obj):
"""Implement the custom JSON encoder."""
try:
if isinstance(obj, datetime.datetime):
return json_serial(obj)
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
def retrieve_worker_result(rdb, external_request_id, worker):
"""Retrieve results for selected worker from RDB."""
start = datetime.datetime.now()
try:
query = rdb.session.query(WorkerResult) \
.filter(WorkerResult.external_request_id == external_request_id,
WorkerResult.worker == worker)
result = query.one()
except (NoResultFound, MultipleResultsFound):
return None
except SQLAlchemyError:
rdb.session.rollback()
raise
result_dict = result.to_dict()
# compute elapsed time
logger.debug('%s took %f seconds to retrieve %s worker results.',
external_request_id, (datetime.datetime.now() - start).total_seconds(),
worker)
return result_dict
def fetch_sa_request(rdb, external_request_id):
"""Query the stack analysis record for a given request id."""
try:
return rdb.session.query(StackAnalysisRequest)\
.filter(StackAnalysisRequest.id == external_request_id).first()
except SQLAlchemyError:
rdb.session.rollback()
raise
def request_timed_out(request):
"""Check if a stack analysis request has timed out."""
row = request.to_dict()
submit_time = row.get("submitTime", {})
current_time = datetime.datetime.now()
diff = (current_time - submit_time).seconds
if diff > int(STACK_ANALYSIS_REQUEST_TIMEOUT):
return True
return False
def is_valid(param):
"""Return true is the param is not a null value."""
return param is not None
def generate_content_hash(content):
"""Return the sha1 digest of a string."""
hash_object = hashlib.sha1(content.encode('utf-8'))
return hash_object.hexdigest()
# TODO: this is module constant -> use capital letters with underscores separating words.
accepted_file_names = {
"npmlist.json": "npm",
"golist.json": "golang",
"pylist.json": "pypi",
"dependencies.txt": "maven"
}
accepted_ecosystems = [
"npm",
"maven",
"pypi",
"golang",
]
def check_for_accepted_ecosystem(ecosystem):
"""Check if the ecosystem is supported or not."""
return ecosystem in accepted_ecosystems
def resolved_files_exist(manifests):
"""Check if the manifest files are already resolved."""
if type(manifests) is list:
for manifest in manifests:
if manifest['filename'] in accepted_file_names:
return True
else:
if manifests in accepted_file_names:
return True
return False
def get_ecosystem_from_manifest(manifests):
"""Check if the manifest files are already resolved."""
if type(manifests) is list:
for manifest in manifests:
if manifest['filename'] in accepted_file_names:
return accepted_file_names[manifest['filename']]
else:
if manifests in accepted_file_names:
return accepted_file_names[manifests]
return None