<!DOCTYPE html>

<html>
<head>
	<title>AIL-Framework</title>
	<link rel="icon" href="{{ url_for('static', filename='image/ail-icon.png')}}">
	<!-- Core CSS -->
	<link href="{{ url_for('static', filename='css/bootstrap4.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/font-awesome.min.css') }}" rel="stylesheet">

	<!-- JS -->
	<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
	<script src="{{ url_for('static', filename='js/bootstrap4.min.js')}}"></script>


</head>

<body>

	{% include 'nav_bar.html' %}

	<div class="container-fluid">
		<div class="row">

			{% include 'crawler/menu_sidebar.html' %}

			<div class="col-12 col-lg-10" id="core_content">

				{% include 'crawler/crawler_disabled.html' %}

				<div class="row">
					<div class="col-xl-6">

						<div class="card mt-1 mb-1">
						  <div class="card-header text-white bg-dark">
								<h5><a class="text-info" href="{{ url_for('hiddenServices.Crawler_Splash_last_by_type')}}?type=onion"><i class="fas fa-user-secret"></i> Onions Crawlers</a></h5>
								<div class="row">
									<div class="col-6">
										<span class="badge badge-success" id="stat_onion_domain_up">{{ statDomains_onion['domains_up'] }}</span> UP
										<span class="badge badge-danger ml-md-3" id="stat_onion_domain_down">{{ statDomains_onion['domains_down'] }}</span> DOWN
									</div>
									<div class="col-6">
										<span class="badge badge-success" id="stat_onion_total">{{ statDomains_onion['total'] }}</span> Crawled
										<span class="badge badge-warning ml-md-3" id="stat_onion_queue">{{ statDomains_onion['domains_queue'] }}</span> Queue
									</div>
								</div>
							</div>
						  <div class="card-body px-0 py-0 ">
								<table class="table">
									<tbody id="tbody_crawler_onion_info">
										{% for crawler in crawler_metadata_onion %}
											<tr>
						      			<td>
													<i class="fas fa-{%if crawler['status']%}check{%else%}times{%endif%}-circle" style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};"></i> {{crawler['crawler_info']}}
												</td>
												<td>
													{{crawler['crawling_domain']}}
												</td>
												<td style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};">
													{{crawler['status_info']}}
												</td>
											</tr>
										{% endfor %}
									</tbody>
								</table>
							</div>
						</div>

					</div>
					<div class="col-xl-6">
						<div class="card mt-1 mb-1">
							<div class="card-header text-white bg-dark">
								<h5><a class="text-info" href="{{ url_for('hiddenServices.Crawler_Splash_last_by_type')}}?type=regular"><i class="fab fa-html5"></i> Regular Crawlers</a></h5>
								<div class="row">
									<div class="col-6">
										<span class="badge badge-success" id="stat_regular_domain_up">{{ statDomains_regular['domains_up'] }}</span> UP
										<span class="badge badge-danger ml-md-3" id="stat_regular_domain_down">{{ statDomains_regular['domains_down'] }}</span> DOWN
									</div>
									<div class="col-6">
										<span class="badge badge-success" id="stat_regular_total">{{ statDomains_regular['total'] }}</span> Crawled
										<span class="badge badge-warning ml-md-3" id="stat_regular_queue">{{ statDomains_regular['domains_queue'] }}</span> Queue
									</div>
								</div>
							</div>
							<div class="card-body px-0 py-0 ">
								<table class="table">
									<tbody id="tbody_crawler_regular_info">
										{% for crawler in crawler_metadata_regular %}
											<tr>
												<td>
													<i class="fas fa-{%if crawler['status']%}check{%else%}times{%endif%}-circle" style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};"></i> {{crawler['crawler_info']}}
												</td>
												<td>
													{{crawler['crawling_domain']}}
												</td>
												<td style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};">
													{{crawler['status_info']}}
												</td>
											</tr>
										{% endfor %}
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>

			</div>
		</div>
	</div>


</body>

<script>
var to_refresh = false
$(document).ready(function(){
	$("#page-Crawler").addClass("active");
	$("#nav_dashboard").addClass("active");
	$( window ).focus(function() {
			to_refresh = true
			refresh_crawler_status();
	});
	$( window ).blur(function() {
			to_refresh = false
	});

	to_refresh = true
	refresh_crawler_status();
});

function toggle_sidebar(){
	if($('#nav_menu').is(':visible')){
		$('#nav_menu').hide();
		$('#side_menu').removeClass('border-right')
		$('#side_menu').removeClass('col-lg-2')
		$('#core_content').removeClass('col-lg-10')
	}else{
		$('#nav_menu').show();
		$('#side_menu').addClass('border-right')
		$('#side_menu').addClass('col-lg-2')
		$('#core_content').addClass('col-lg-10')
	}
}

function refresh_crawler_status(){

	$.getJSON("{{ url_for('hiddenServices.crawler_dashboard_json') }}",
		function(data) {

			$('#stat_onion_domain_up').text(data.statDomains_onion['domains_up']);
			$('#stat_onion_domain_down').text(data.statDomains_onion['domains_down']);
			$('#stat_onion_total').text(data.statDomains_onion['total']);
			$('#stat_onion_queue').text(data.statDomains_onion['domains_queue']);

			$('#stat_regular_domain_up').text(data.statDomains_regular['domains_up']);
			$('#stat_regular_domain_down').text(data.statDomains_regular['domains_down']);
			$('#stat_regular_total').text(data.statDomains_regular['total']);
			$('#stat_regular_queue').text(data.statDomains_regular['domains_queue']);

			if(data.crawler_metadata_onion.length!=0){
				$("#tbody_crawler_onion_info").empty();
				var tableRef = document.getElementById('tbody_crawler_onion_info');
				for (var i = 0; i < data.crawler_metadata_onion.length; i++) {
						var crawler = data.crawler_metadata_onion[i];
						var newRow = tableRef.insertRow(tableRef.rows.length);
						var text_color;
						var icon;
						if(crawler['status']){
							text_color = 'Green';
							icon = 'check';
						} else {
							text_color = 'Red';
							icon = 'times';
						}

				    var newCell  = newRow.insertCell(0);
						newCell.innerHTML = "<td><i class=\"fas fa-"+icon+"-circle\" style=\"color:"+text_color+";\"></i> "+crawler['crawler_info']+"</td>";

						newCell  = newRow.insertCell(1);
						newCell.innerHTML = "<td>"+crawler['crawling_domain']+"</td>";

						newCell  = newRow.insertCell(2);
						newCell.innerHTML = "<td><div style=\"color:"+text_color+";\">"+crawler['status_info']+"</div></td>";

						//$("#panel_crawler").show();
				}
			}
			if(data.crawler_metadata_regular.length!=0){
				$("#tbody_crawler_regular_info").empty();
				var tableRef = document.getElementById('tbody_crawler_regular_info');
				for (var i = 0; i < data.crawler_metadata_regular.length; i++) {
						var crawler = data.crawler_metadata_regular[i];
						var newRow = tableRef.insertRow(tableRef.rows.length);
						var text_color;
						var icon;
						if(crawler['status']){
							text_color = 'Green';
							icon = 'check';
						} else {
							text_color = 'Red';
							icon = 'times';
						}

				    var newCell  = newRow.insertCell(0);
						newCell.innerHTML = "<td><i class=\"fas fa-"+icon+"-circle\" style=\"color:"+text_color+";\"></i> "+crawler['crawler_info']+"</td>";

						newCell  = newRow.insertCell(1);
						newCell.innerHTML = "<td>"+crawler['crawling_domain']+"</td>";

						newCell  = newRow.insertCell(2);
						newCell.innerHTML = "<td><div style=\"color:"+text_color+";\">"+crawler['status_info']+"</div></td>";

						//$("#panel_crawler").show();
				}
			}
		}
	);

	if (to_refresh) {
    setTimeout("refresh_crawler_status()", 10000);
	}
}
</script>
