Commit 1cccbb42 by Rafael Monnerat

slapos/grid/promise: Include stats history from slapos.toolbox

1 parent 1147d579
......@@ -39,6 +39,7 @@ import traceback
import psutil
import inspect
import hashlib
from datetime import datetime
from multiprocessing import Process, Queue as MQueue
from six.moves import queue, reload_module
from slapos.util import str2bytes, mkdir_p, chownDirectory
......@@ -389,15 +390,11 @@ class PromiseLauncher(object):
os.rename(promise_tmp_file, promise_output_file)
def _savePromiseHistoryResult(self, result):
#promise_name, state_dict, previous_state_list,
#history_folder):
state_dict = result.serialize()
promise_status_file = os.path.join(self.partition_folder,
'.slapgrid/promise/global.status')
previous_state_dict = {}
promise_status_file = os.path.join(PROMISE_STATE_FOLDER_NAME,
'promise_status.json')
if os.path.exists(promise_status_file):
with open(promise_status_file) as f:
try:
......@@ -441,8 +438,32 @@ class PromiseLauncher(object):
f.seek(0,2)
f.seek(f.tell() -2)
f.write('%s}' % ',{}]'.format(json.dumps(state_dict)))
def _saveStatisticsData(self, stat_file_path, date, success, error):
# csv-like document for success/error statictics
if not os.path.exists(stat_file_path) or os.stat(stat_file_path).st_size == 0:
with open(stat_file_path, 'w') as fstat:
data_dict = {
"date": time.time(),
"data": ["Date, Success, Error, Warning"]
}
fstat.write(json.dumps(data_dict))
current_state = '%s, %s, %s, %s' % (
date,
success,
error,
'')
# append to file
# XXX this is bad, it is getting everywhere.
if current_state:
with open (stat_file_path, mode="r+") as fstat:
fstat.seek(0,2)
position = fstat.tell() -2
fstat.seek(position)
fstat.write('%s}' % ',"{}"]'.format(current_state))
def _loadPromiseResult(self, promise_title):
promise_output_file = os.path.join(
self.promise_output_dir,
......@@ -633,8 +654,18 @@ class PromiseLauncher(object):
failed_promise_output = ""
previous_state_dict = {}
new_state_dict = {}
report_date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S+0000')
promise_status_file = os.path.join(self.partition_folder,
'.slapgrid/promise/global.status')
PROMISE_STATE_FOLDER_NAME,
'promise_status.json')
promise_result_file = os.path.join(self.partition_folder,
PROMISE_STATE_FOLDER_NAME,
'promise_result.json')
promise_stats_file = os.path.join(self.partition_folder,
PROMISE_STATE_FOLDER_NAME,
'promise_stats.json')
if os.path.exists(promise_status_file):
with open(promise_status_file) as f:
......@@ -657,7 +688,8 @@ class PromiseLauncher(object):
'queue': self.queue_result,
'slapgrid-version': version,
}
error = 0
success = 0
if os.path.exists(self.promise_folder) and os.path.isdir(self.promise_folder):
for promise_name in os.listdir(self.promise_folder):
if promise_name.startswith('__init__') or \
......@@ -678,7 +710,12 @@ class PromiseLauncher(object):
promise_result = self._launchPromise(promise_name, promise_path, config)
if promise_result:
change_date = promise_result.date.strftime('%Y-%m-%dT%H:%M:%S+0000')
promise_status = 'FAILED' if promise_result.hasFailed() else "OK"
if promise_result.hasFailed():
promise_status = 'FAILED'
error += 1
else:
promise_status = "OK"
success += 1
if promise_name in previous_state_dict:
status, previous_change_date, _ = previous_state_dict[promise_name]
if promise_status == status:
......@@ -716,7 +753,12 @@ class PromiseLauncher(object):
wrap_process=True)
if promise_result:
change_date = promise_result.date.strftime('%Y-%m-%dT%H:%M:%S+0000')
promise_status = 'FAILED' if promise_result.hasFailed() else "OK"
if promise_result.hasFailed():
promise_status = 'FAILED'
error += 1
else:
promise_status = "OK"
success += 1
if promise_name in previous_state_dict:
status, previous_change_date, _ = previous_state_dict[promise_name]
if promise_status == status:
......@@ -741,6 +783,10 @@ class PromiseLauncher(object):
failed_promise_name,
failed_promise_output))
# Save Global State
with open(promise_status_file, "w") as f:
json.dump(new_state_dict, f)
self._saveStatisticsData(promise_stats_file,
report_date, success, error)
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!