Skip to content

Commit

Permalink
feat: resilience malicious integration
Browse files Browse the repository at this point in the history
working resilience malicious
  • Loading branch information
tejas2292 committed Mar 31, 2023
1 parent dd383c4 commit 8c67df5
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 154 deletions.
158 changes: 103 additions & 55 deletions application/routes/resilience_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,59 @@

@resilience_routes.route('/resilience/malicious', endpoint='resilience_malicious', methods=['GET', 'POST'])
def resilience_analyisis_malicious():
layout = 'degree_centrality'
layout2 = 'equal_to'
tab_main = 'tab1'
attack = 'malicious'
filename2 = session['filename2']
networkGraphs = get_networkGraph(filename2)
tab_main = session.get('tab_main', 'tab1')
layout = session.get('layout_malicious', 'degree_centrality')
layout2 = session.get('layout2_malicious', 'equal_to')

if request.method == 'POST':
number_of_levels = request.form.get('number_of_levels', None)
number_of_levels = int(number_of_levels) if number_of_levels else None
number_of_threashold = request.form.get('number_of_threashold', None)
layout2 = request.form.get('layout2')
number_of_threashold = int(number_of_threashold) if number_of_threashold else None
layout = request.form.get('layout')
if layout2 == "greater_than":
operator = ">"
elif layout2 == "less_than":
operator = "<"
elif layout2 == "equal_to":
operator = "="
else:
number_of_levels = None
number_of_threashold = None
session['val1'] = None
session['val2'] = None
operator = None

filename2 = session['filename2']
networkGraphs = get_networkGraph(filename2)
number_of_nodes = session.get('number_of_nodes', None)
number_of_edges = session.get('number_of_edges', None)
number_of_threashold = session.get('number_of_threashold', None)

val1 = session.get('val1', None)
val2 = session.get('val2', None)
if val1 != number_of_levels:
session['val1'] = number_of_levels
val3 = session.get('val3', None)
val4 = session.get('val4', None)

# here we use number of nodes
global networkGraphs2
if val3 != layout:
session['val3'] = layout

if val1 != number_of_nodes:
session['val1'] = number_of_nodes
tab_main = 'tab1'
session['tab_main'] = tab_main
networkGraphs2, df2 = resilience(networkGraphs, attack=attack, metric=layout, number_of_nodes=int(number_of_nodes))

# here we use number of edges
if val2 != number_of_threashold:
session['val2'] = number_of_threashold
tab_main = 'tab2'
session['tab_main'] = tab_main
networkGraphs2, df2 = resilience(networkGraphs, attack=attack, metric=layout, threshold=int(number_of_threashold), operator=operator)

return render_template('resilience/resilience_analyisis_malicious.html', tab_main=tab_main,
layout=layout, layout2=layout2, number_of_levels=number_of_levels, number_of_threashold=number_of_threashold)
if val4 != layout2:
session['val4'] = layout2
tab_main = 'tab2'
session['tab_main'] = tab_main
if(number_of_threashold is not None):
networkGraphs2, df2 = resilience(networkGraphs, attack=attack, metric=layout, threshold=int(number_of_threashold), operator=operator)

return template_resilience_tabs('resilience/resilience_analyisis_malicious.html', number_of_nodes, number_of_edges, number_of_threashold, layout, layout2, tab_main, attack)

@resilience_routes.route('/resilience/cluster', endpoint='resilience_cluster', methods=['GET', 'POST'])
def resilience_analyisis_cluster():
Expand All @@ -71,15 +96,18 @@ def resilience_analyisis_cluster():

return render_template('resilience/resilience_analyisis_cluster.html', layout=layout, number_of_cluster_to_generate=number_of_cluster_to_generate, cluster_to_attack=cluster_to_attack)


@resilience_routes.route('/resilience/random', endpoint='resilience_random', methods=['GET', 'POST'])
def resilience_analyisis_random():
attack = 'random'
tab_main = session.get('tab_main', 'tab1')
filename2 = session['filename2']
networkGraphs = get_networkGraph(filename2)
number_of_nodes = session.get('number_of_nodes', None)
number_of_edges = session.get('number_of_edges', None)

number_of_threashold = session.get('number_of_threashold', None)
layout = session.get('layout_malicious', 'degree_centrality')
layout2 = session.get('layout2_malicious', 'equal_to')

val1 = session.get('val1', None)
val2 = session.get('val2', None)

Expand All @@ -89,21 +117,56 @@ def resilience_analyisis_random():
session['val1'] = number_of_nodes
tab_main = 'tab1'
session['tab_main'] = tab_main
networkGraphs2, df2 = resilience(networkGraphs, attack='random', number_of_nodes=int(number_of_nodes))
networkGraphs2, df2 = resilience(networkGraphs, attack=attack, number_of_nodes=int(number_of_nodes))

# here we use number of edges
if val2 != number_of_edges:
session['val2'] = number_of_edges
tab_main = 'tab2'
session['tab_main'] = tab_main
networkGraphs2, df2 = resilience(networkGraphs, attack='random', number_of_edges=int(number_of_edges))
networkGraphs2, df2 = resilience(networkGraphs, attack=attack, number_of_edges=int(number_of_edges))

print('number_of_nodes',number_of_nodes)

return template_resilience_tabs('resilience/resilience_analyisis_random.html', number_of_nodes, number_of_edges, tab_main)
#return render_template('resilience/resilience_analyisis_random.html', tab_main=tab_main, number_of_nodes=number_of_nodes, number_of_edges=number_of_edges)
return template_resilience_tabs('resilience/resilience_analyisis_random.html', number_of_nodes, number_of_edges, number_of_threashold, layout, layout2, tab_main, attack)

@resilience_routes.route('/resilience/malicious/upload', endpoint='resilience_malicious_upload', methods=['GET', 'POST'])
def resilience_malicious_upload():
number_of_nodes = request.form.get('number_of_nodes')
number_of_threashold = request.form.get('number_of_threashold')
layout2 = request.form.get('layout2')
layout = request.form.get('layout')

def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_edges_arg, tab_main_arg):
if number_of_nodes == '':
number_of_nodes = None
if number_of_threashold == '':
number_of_threashold = None
session['number_of_nodes'] = number_of_nodes
session['number_of_threashold'] = number_of_threashold
session['number_of_threashold'] = number_of_threashold
session['layout_malicious'] = layout
session['layout2_malicious'] = layout2
# do something with the form data

return redirect(url_for('resilience_routes.resilience_malicious'))

@resilience_routes.route('/resilience/random/upload', endpoint='resilience_random_upload', methods=['GET', 'POST'])
def resilience_random_upload():
number_of_nodes = request.form.get('number_of_nodes')
number_of_edges = request.form.get('number_of_edges')
if number_of_nodes == '':
number_of_nodes = None
if number_of_edges == '':
number_of_edges = None
session['number_of_nodes'] = number_of_nodes
session['number_of_edges'] = number_of_edges

# do something with the form data

return redirect(url_for('resilience_routes.resilience_random'))


def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_edges_arg, number_of_threashold_arg, layout_arg, layout2_arg, tab_main_arg, attack_arg):
filename2 = session['filename2']
networkGraphs = get_networkGraph(filename2)
multi_toggle = True
Expand All @@ -113,8 +176,14 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
visualisation_layout = 'map'
else:
visualisation_layout = 'sfdp'

attack = attack_arg
attack_name_from_nw = None

if networkGraphs2 is not None:
attack_name_from_nw = networkGraphs2.get_attack_vector()

if attack == attack_name_from_nw:
#if networkGraphs2 is not None:
# check POST condition
if request.method == 'POST':
visualisation_layout = request.form.get('visualisation_layout')
Expand Down Expand Up @@ -216,8 +285,8 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
df_label_propagation_after, graph_label_propagation_name_2 = plot_cluster(networkGraphs2, 'label_propagation', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_asyn_lpa_before, graph_asyn_lpa_name_1 = plot_cluster(networkGraphs, 'asyn_lpa', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_asyn_lpa_after, graph_asyn_lpa_name_2 = plot_cluster(networkGraphs2, 'asyn_lpa', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_k_clique_before, graph_k_clique_name_1 = plot_cluster(networkGraphs, 'k_clique', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_k_clique_after, graph_k_clique_name_2 = plot_cluster(networkGraphs2, 'k_clique', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_k_clique_before, graph_k_clique_name_1 = plot_cluster(networkGraphs, 'spectral', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_k_clique_after, graph_k_clique_name_2 = plot_cluster(networkGraphs2, 'spectral', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_spectral_before, graph_spectral_name_1 = plot_cluster(networkGraphs, 'spectral', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_spectral_after, graph_spectral_name_2 = plot_cluster(networkGraphs2, 'spectral', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
df_kmeans_before, graph_kmeans_name_1 = plot_cluster(networkGraphs, 'kmeans', noOfClusters=number_of_clusters,dynamic=False, layout=visualisation_layout)
Expand All @@ -235,8 +304,8 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
df_label_propagation_after, graph_label_propagation_name_2 = plot_cluster(networkGraphs2, 'label_propagation', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_asyn_lpa_before, graph_asyn_lpa_name_1 = plot_cluster(networkGraphs, 'asyn_lpa', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_asyn_lpa_after, graph_asyn_lpa_name_2 = plot_cluster(networkGraphs2, 'asyn_lpa', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_k_clique_before, graph_k_clique_name_1 = plot_cluster(networkGraphs, 'k_clique', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_k_clique_after, graph_k_clique_name_2 = plot_cluster(networkGraphs2, 'k_clique', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_k_clique_before, graph_k_clique_name_1 = plot_cluster(networkGraphs, 'spectral', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_k_clique_after, graph_k_clique_name_2 = plot_cluster(networkGraphs2, 'spectral', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_spectral_before, graph_spectral_name_1 = plot_cluster(networkGraphs, 'spectral', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_spectral_after, graph_spectral_name_2 = plot_cluster(networkGraphs2, 'spectral', noOfClusters=0,dynamic=False, layout=visualisation_layout)
df_kmeans_before, graph_kmeans_name_1 = plot_cluster(networkGraphs, 'kmeans', noOfClusters=0,dynamic=False, layout=visualisation_layout)
Expand Down Expand Up @@ -726,7 +795,6 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
graph_dbscan_path_2 = '../static/' + graph_dbscan_name_2
else:
graph_dbscan_path_2 = '../'+ networkGraphs2.session_folder + '/' + graph_dbscan_name_2

else:
graph_path1 = '../static/no_graph.html'
graph_path2 = '../static/no_graph.html'
Expand Down Expand Up @@ -875,8 +943,9 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
df_dbscan_after = pd.DataFrame(0, index=range(5), columns=range(5))


return render_template(template_name_arg, tab_main=tab_main_arg, visualisation_layout=visualisation_layout,
number_of_nodes=number_of_nodes_arg, number_of_edges=number_of_edges_arg, graph1=graph_path1, graph2=graph_path2,

return render_template(template_name_arg, tab_main=tab_main_arg, visualisation_layout=visualisation_layout, number_of_threashold=number_of_threashold_arg,
number_of_nodes=number_of_nodes_arg, number_of_edges=number_of_edges_arg, graph1=graph_path1, graph2=graph_path2, layout=layout_arg, layout2=layout2_arg,
multi_toggle=multi_toggle,directed_toggle=directed_toggle, number_of_clusters=number_of_clusters,
df_degree_centrality_before=df_degree_centrality_before, df_degree_centrality_after=df_degree_centrality_after,
graph_degree_centrality_layout_1=graph_degree_centrality_layout_path_1, graph_degree_centrality_layout_2=graph_degree_centrality_layout_path_2,
Expand Down Expand Up @@ -957,25 +1026,4 @@ def template_resilience_tabs(template_name_arg, number_of_nodes_arg, number_of_e
graph_agglomerative_1=graph_agglomerative_path_1, graph_agglomerative_2=graph_agglomerative_path_2,

df_dbscan_before=df_dbscan_before, df_dbscan_after=df_dbscan_after,
graph_dbscan_1=graph_dbscan_path_1, graph_dbscan_2=graph_dbscan_path_2)







@resilience_routes.route('/resilience/random/upload', endpoint='resilience_random_upload', methods=['GET', 'POST'])
def resilience_random_upload():
number_of_nodes = request.form.get('number_of_nodes')
number_of_edges = request.form.get('number_of_edges')
if number_of_nodes == '':
number_of_nodes = None
if number_of_edges == '':
number_of_edges = None
session['number_of_nodes'] = number_of_nodes
session['number_of_edges'] = number_of_edges

# do something with the form data

return redirect(url_for('resilience_routes.resilience_random'))
graph_dbscan_1=graph_dbscan_path_1, graph_dbscan_2=graph_dbscan_path_2)
30 changes: 30 additions & 0 deletions application/static/js/table_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3126,4 +3126,34 @@ $(document).ready(function () {
}
}]
});
});

//example-metrics

$(document).ready(function () {
$('#example-metrics').DataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"pageLength": 10, // Show 10 rows per page
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"columnDefs": [{
"targets": "_all",
"render": function (data, type, row) {
if (typeof data === 'number') {
return parseFloat(data.toFixed(6)); // Round to 6 decimal places for numbers
}
if (typeof data === 'string') {
if (data.length > 15 && data.startsWith('0x')) {
return data.slice(0, 6) + '...' + data.slice(-6);
} else if (!data.startsWith('0x')) {
return data.substring(0, 6); // Limit the length to 6 if the string doesn't start with '0x'
}
return data.substring(0, 12);
}
return data;
}
}]
});
});
2 changes: 1 addition & 1 deletion application/templates/clustering_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h3 class="styledh3">{{method_name}} Clustering</h3>
<hr>

<div class="card card-body">
<table id="example" class="display" style="width:100%">
<table id="example-metrics" class="display" style="width:100%">
<thead>
<tr>
{% for cell in example.columns %}
Expand Down
2 changes: 1 addition & 1 deletion application/templates/global_metrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h3 class="styledh3">Global Metrics</h3>
<div class="row">
<div class="col-10">
<div class="card card-body">
<table id="example" class="display" style="width:100%">
<table id="example-metrics" class="display" style="width:100%">
<thead>
<tr>
{% for cell in example.columns %}
Expand Down
2 changes: 1 addition & 1 deletion application/templates/hotspot_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="styledh3">{{method_name}} Hotspot</h3>
<hr>

<div class="card card-body">
<table id="example" class="display" style="width:100%">
<table id="example-metrics" class="display" style="width:100%">
<thead>
<tr>
{% for cell in example.columns %}
Expand Down
2 changes: 1 addition & 1 deletion application/templates/metrics_all_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ <h3 class="styledh3">{{method_name | safe}}</h3>
<hr>

<div class="card card-body">
<table id="example" class="display" style="width:100%">
<table id="example-metrics" class="display" style="width:100%">
<thead>
<tr>
{% for cell in example.columns %}
Expand Down
2 changes: 1 addition & 1 deletion application/templates/metrics_single_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ <h3 class="styledh3">{{method_name}}</h3>
</div>
<hr>
<div class="card card-body">
<table id="example" class="display" style="width:100%">
<table id="example-metrics" class="display" style="width:100%">
<thead>
<tr>
{% for cell in example.columns %}
Expand Down
Loading

0 comments on commit 8c67df5

Please sign in to comment.