This commit is contained in:
luckysideburn
2024-02-29 20:22:30 +00:00
parent 7a93406bf1
commit 039110f8ff
7 changed files with 96 additions and 41 deletions
+18 -4
View File
@@ -1,3 +1,9 @@
function diffBetweenTwoDates(date1, date2) {
console.log("[SAVE-CHAOS-REPORT-CONF] Diff between two dates: " + date1 + " and " + date2);
var diff = (date2.getTime() - date1.getTime()) / 1000;
console.log("[SAVE-CHAOS-REPORT-CONF] Diff between two dates: " + diff + " seconds")
return diff;
}
function addPostUploadFile(selectedValue) {
if (selectedValue == "POST") {
@@ -74,6 +80,7 @@ function checkIfSomeItemIsEmpty(dict, except) {
}
function sendSavedChaosReport() {
startGameMode()
chaos_report_switch = true;
document.getElementById("httpStatsCanvasDiv").style.display = "block";
drawCanvasHTTPStatusCodeStats()
@@ -86,11 +93,15 @@ function sendSavedChaosReport() {
"chaosReportCheckSiteURLPayload": chaos_report_post_data
}
if (chaos_report_start_date == "") {
chaos_report_start_date = new Date();
}
chaosReportprojectName = presetBodyDict["chaosReportProject"];
$("#chaosReportAuthorDiv").html(presetBodyDict["chaosReportAuthor"]);
$("#chaosReportProjectDiv").html(presetBodyDict["chaosReportProject"]);
$("#chaosReportDateDiv").html(new Date().toLocaleString());
$("#chaosReportSessionTimeDiv").html("0");
$("#chaosReportDateDiv").html(chaos_report_start_date.toLocaleString());
$("#chaosReportSessionTimeDiv").html(diffBetweenTwoDates(chaos_report_start_date, new Date()) + " seconds");
$("#chaosReportCheckSiteURLDiv").html(presetBodyDict["chaosReportCheckSiteURL"]);
if (!isValidURL(presetBodyDict["chaosReportCheckSiteURL"])) {
@@ -114,7 +125,7 @@ function sendSavedChaosReport() {
}
var oReq = new XMLHttpRequest();
oReq.open("POST", k8s_url + "/chaos/report/save", true);
oReq.open("POST", k8s_url + "/chaos/report/save?project=" + $("#chaosReportProject").val(), true);
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
@@ -152,6 +163,9 @@ function saveChaosReport() {
}
function updateElapsedTimeArray(projectName) {
$("#chaosReportSessionTimeDiv").html(diffBetweenTwoDates(chaos_report_start_date, new Date()) + " seconds");
console.log("[SAVE-CHAOS-REPORT-CONF] Diff Between Dates: " + toString(diffBetweenTwoDates(chaos_report_start_date, new Date())));
console.log("[SAVE-CHAOS-REPORT-CONF] Updating elapsed time array for project: " + projectName);
var oReq = new XMLHttpRequest();
@@ -182,7 +196,7 @@ function updateChaosReportStartTime(projectName) {
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
$("#chaosReportDateDiv").html("Session Start Date: " + this.responseText);
$("#chaosReportDateDiv").html(this.responseText);
}
};;
+12 -12
View File
@@ -213,8 +213,8 @@
</div>
<div class="col text-center" style="margin-bottom: 0%;">
<input class="form-check-input" type="checkbox" value="" id="flexCheckDefault" onclick="showPrepareChaosReportModal(this)">
<label class="form-check-label" for="flexCheckDefault">
Enable Resilience Report
<label class="form-check-label text-kinv" for="flexCheckDefault">
Add HTTP check & Chaos Report
</label>
</div>
<div class="col text-center" style="margin-bottom: 2%;">
@@ -393,22 +393,22 @@ experiments:
</thead>
<tbody>
<tr>
<td>PROJECT</td>
<td class="td-grey">PROJECT</td>
<td><p id="chaosReportProjectDiv"></p></td>
</tr>
<tr>
<td>AUTHOR</td>
<td><p id="chaosReportAuthorDiv"></p></td>
<td class="td-grey">AUTHOR</td>
<td><p id="chaosReportAuthorDiv" class="text-kinv"></p></td>
</tr>
<td>Chaos session date</td>
<td><p id="chaosReportDateDiv"></p></td>
<td class="td-grey">Chaos session date</td>
<td><p id="chaosReportDateDiv" class="text-kinv"></p></td>
<tr>
<td>Chaos session time</td>
<td><p id="chaosReportSessionTimeDiv"></p></td>
<td class="td-grey">Chaos session time</td>
<td><p id="chaosReportSessionTimeDiv" class="text-kinv"></p></td>
</tr>
<tr>
<td>Checked URL</td>
<td><p id="chaosReportCheckSiteURLDiv"></p></td>
<td class="td-grey">Checked URL</td>
<td><p id="chaosReportCheckSiteURLDiv" class="text-kinv"></p></td>
</tr>
</tr>
</tbody>
@@ -451,8 +451,8 @@ experiments:
</div>
</div>
</div>
<script src="./programming_mode.js"></script>
<script src="./utils.js"></script>
<script src="./programming_mode.js"></script>
<script src="./modals.js"></script>
<script src="./mode_controls.js"></script>
<script src="./game_console.js"></script>
+1 -1
View File
@@ -101,7 +101,7 @@ var chaos_logs_pos = 0;
var chaos_report_switch = false;
var chaos_report_http_elapsed_time_array = [];
var chaosReportprojectName = "";
var chaos_report_start_date = "";
var chart_deleted_pods_total = 0;
var chart_chaos_jobs_total = 0;
var chart_current_chaos_job_pod = 0;
+9 -1
View File
@@ -1,4 +1,12 @@
/* Programming Mode Functions */
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/* TODO: this is very bad... change asap :D */
loadPresetsCodeJson = `{
@@ -185,7 +193,7 @@ function drawChaosProgramFlow() {
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
if (isJsonString(this.responseText)){
if (IsJsonString(this.responseText)){
var flow = JSON.parse(this.responseText);
var flow_html = "";
let i = 0;
+5 -1
View File
@@ -159,4 +159,8 @@ div {
-webkit-border-radius: 15px;
padding: 5px;
border-image-slice: 5;
}
}
.td-grey {
color: #7c737f;
}
+21 -1
View File
@@ -365,6 +365,19 @@ server {
location /chaos/report/save {
content_by_lua_block {
local function add_project(project_list, new_project)
local new_project_list = project_list .. "," .. new_project
return new_project_list
end
local function check_if_redis_key_exists(redis, key)
if redis:exists(key) == 1 then
return true
end
return false
end
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
@@ -375,7 +388,14 @@ server {
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
red:set("chaos_report_program", data)
red:set("chaos_report_project_" .. args["project"], data)
if check_if_redis_key_exists(red, "chaos_report_project_list") then
red:set("chaos_report_project_list", add_project(red:get("chaos_report_project_list"), args["project"]))
else
red:set("chaos_report_project_list", args["project"])
end
ngx.log(ngx.INFO, "[CHAOS-REPORT-SAVE] " .. data)
}
}
+30 -21
View File
@@ -17,6 +17,10 @@ import time
import datetime
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def join_string_to_array(project_list_string, separator):
return project_list_string.split(separator)
def do_http_request(url, method, headers, data):
try:
response = requests.request(method, url, headers=headers, data=data, verify=False, allow_redirects=True, timeout=10)
@@ -93,35 +97,40 @@ batch_api = client.BatchV1Api()
while True:
logging.info("[k-inv][metrics_loop] Metrics loop is active")
logging.info(f"[k-inv][metrics_loop] Metrics loop is active - {r.exists('chaos_report_project_list')}")
if r.exists('chaos_report_program'):
logging.info(f"[k-inv][metrics_loop][chaos_report] Found chaos_report_program key in Redis. Starting chaos_report_program")
if r.exists('chaos_report_project_list'):
logging.info("[k-inv][metrics_loop] Found chaos_report_project_list Redis Key")
for project in join_string_to_array(r.get('chaos_report_project_list').decode(), ','):
logging.info(f"[k-inv][metrics_loop] Computing Chaos Report Project: {project}")
chaos_program_key = f"chaos_report_project_{project}"
if r.exists(chaos_program_key):
logging.info(f"[k-inv][metrics_loop][chaos_report] Found chaos_report_project_{project} key in Redis. Starting {project} ")
if check_if_json_is_valid(r.get('chaos_report_program')):
chaos_report_program = json.loads(r.get('chaos_report_program'))
now = datetime.datetime.now()
if check_if_json_is_valid(r.get(chaos_program_key)):
chaos_report_program = json.loads(r.get(chaos_program_key))
now = datetime.datetime.now()
logging.info(f"[k-inv][metrics_loop][chaos_report] chaos_report_program is valid JSON: {chaos_report_program}")
response = do_http_request(chaos_report_program['chaosReportCheckSiteURL'], chaos_report_program['chaosReportCheckSiteURLMethod'], json.loads(chaos_report_program['chaosReportCheckSiteURLHeaders']), chaos_report_program['chaosReportCheckSiteURLPayload'])
logging.info(f"[k-inv][metrics_loop][chaos_report] chaos_report_program response: {response.status_code}")
logging.info(f"[k-inv][metrics_loop][chaos_report] chaos_report_program is valid JSON: {chaos_report_program}")
response = do_http_request(chaos_report_program['chaosReportCheckSiteURL'], chaos_report_program['chaosReportCheckSiteURLMethod'], json.loads(chaos_report_program['chaosReportCheckSiteURLHeaders']), chaos_report_program['chaosReportCheckSiteURLPayload'])
logging.info(f"[k-inv][metrics_loop][chaos_report] chaos_report_program response: {response.status_code}")
check_url_counter_key = f"{chaos_report_program['chaosReportProject']}_check_url_counter"
check_url_status_code_key = f"{chaos_report_program['chaosReportProject']}_check_url_status_code"
check_url_elapsed_time_key = f"{chaos_report_program['chaosReportProject']}_check_url_elapsed_time"
check_url_start_time = f"{chaos_report_program['chaosReportProject']}_check_url_start_time"
check_url_counter_key = f"{chaos_report_program['chaosReportProject']}_check_url_counter"
check_url_status_code_key = f"{chaos_report_program['chaosReportProject']}_check_url_status_code"
check_url_elapsed_time_key = f"{chaos_report_program['chaosReportProject']}_check_url_elapsed_time"
check_url_start_time = f"{chaos_report_program['chaosReportProject']}_check_url_start_time"
if r.get(check_url_counter_key) == None:
r.set(check_url_counter_key, 0)
else:
r.incr(check_url_counter_key)
if r.get(check_url_counter_key) == None:
r.set(check_url_counter_key, 0)
else:
r.incr(check_url_counter_key)
if r.get(check_url_start_time) == None:
r.set(check_url_start_time, now.strftime("%Y-%m-%d %H:%M:%S"))
if r.get(check_url_start_time) == None:
r.set(check_url_start_time, now.strftime("%Y-%m-%d %H:%M:%S"))
r.set(check_url_status_code_key, response.status_code)
r.set(check_url_elapsed_time_key, float(response.elapsed.total_seconds()))
r.set(check_url_status_code_key, response.status_code)
r.set(check_url_elapsed_time_key, float(response.elapsed.total_seconds()))
try:
label_selector="chaos-controller=kubeinvaders"