<!DOCTYPE html>
<html>

	<head>
	  <meta charset="utf-8">
	  <meta name="viewport" content="width=device-width, initial-scale=1.0">

	  <title>Hidden Service - AIL</title>
		<link rel="icon" href="{{ url_for('static', filename='image/ail-icon.png') }}">

	  <!-- Core CSS -->
	  <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
	  <link href="{{ url_for('static', filename='font-awesome/css/font-awesome.css') }}" rel="stylesheet">
	  <link href="{{ url_for('static', filename='css/sb-admin-2.css') }}" rel="stylesheet">
				<link href="{{ url_for('static', filename='css/daterangepicker.min.css') }}" rel="stylesheet" type="text/css" />
	  <!-- JS -->
	  <script language="javascript" src="{{ url_for('static', filename='js/jquery.js')}}"></script>
		<script language="javascript" src="{{ url_for('static', filename='js/moment.min.js') }}"></script>
		<script language="javascript" src="{{ url_for('static', filename='js/jquery.daterangepicker.min.js') }}"></script>
		<script language="javascript" src="{{ url_for('static', filename='js/d3.min.js') }}"></script>

		<style>
		.input-group .form-control {
			position: unset;
		}
		div.tooltip {
			position: absolute;
			text-align: center;
			padding: 2px;
			font: 12px sans-serif;
			background: #ebf4fb;
			border: 2px solid #b7ddf2;
			border-radius: 8px;
			pointer-events: none;
			color: #000000;
		}
		.line_graph {
		  fill: none;
		  stroke: steelblue;
		  stroke-width: 2px;
			stroke-linejoin: round;
			stroke-linecap: round;
			stroke-width: 1.5;
		}
		</style>

	</head>
	<body>

            {% include 'navbar.html' %}

            <div id="page-wrapper">

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

						<div class="panel panel-primary">
						  <div class="panel-heading">ONION</div>
						  <div class="panel-body">
								<table class="table table-striped">
							    <thead>
							      <tr>
							        <th>Domain</th>
							        <th>First Seen</th>
											<th>Last Check</th>
							        <th>Status</th>
							      </tr>
							    </thead>
							    <tbody id="tbody_last_crawled">
										{% for metadata_onion in last_onions %}
								      <tr>
								        <td><a target="_blank" href="{{ url_for('hiddenServices.onion_domain') }}?onion_domain={{ metadata_onion['domain'] }}">{{ metadata_onion['domain'] }}</a></td>
								        <td>{{'{}/{}/{}'.format(metadata_onion['first_seen'][0:4], metadata_onion['first_seen'][4:6], metadata_onion['first_seen'][6:8])}}</td>
												<td>{{'{}/{}/{}'.format(metadata_onion['last_check'][0:4], metadata_onion['last_check'][4:6], metadata_onion['last_check'][6:8])}}</td>
								        <td><div style="color:{{metadata_onion['status_color']}}; display:inline-block">
															<i class="fa {{metadata_onion['status_icon']}} fa-2x"></i>
															{{metadata_onion['status_text']}}
														</div>
												</td>
								      </tr>
										{% endfor %}
							    </tbody>
							  </table>

								<div id="graph_line">
								</div>

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

					<div class="col-md-6">
						<div class="panel panel-info" style="text-align:center;">
							<div class="panel-heading">
								Select domains by date range :
								<br>
								<br>
								<form action="{{ url_for('hiddenServices.get_onions_by_daterange') }}" id="hash_selector_form" method='post'>
									<div class="row">
										<div class="col-md-6">
											<div class="input-group">
												<span class="input-group-addon"><i class="fa fa-calendar  fa" aria-hidden="true"></i></span>
												<input class="form-control" id="date-range-from" placeholder="yyyy-mm-dd" value="{{ date_from }}" name="date_from">
											</div>
											<div class="input-group">
												<span class="input-group-addon"><i class="fa fa-calendar  fa" aria-hidden="true"></i></span>
												<input class="form-control" id="date-range-to" placeholder="yyyy-mm-dd" value="{{ date_to }}" name="date_to">
											</div>
											<div class="checkbox">
												<label>
													<input type="checkbox" name="domains_tags" value="True" checked>
													<div style="color:#286090; display:inline-block">
														Domains Tags <i class="fa fa-tags"></i>
													</div>
												</label>
											</div>
										</div>
										<div class="col-md-6">
											<div class="checkbox">
											  <label>
											    <input type="checkbox" name="domains_up" value="True" checked>
													<div style="color:Green; display:inline-block">
														Domains UP <i class="fa fa-check-circle"></i>
													</div>
											  </label>
											</div>
											<div class="checkbox">
											  <label>
											    <input type="checkbox" name="domains_down" value="True">
													<div style="color:Red; display:inline-block">
														Domains DOWN 	<i class="fa fa-times-circle"></i>
													</div>
											  </label>
											</div>

										</div>
									</div>
									<button class="btn btn-primary" style="text-align:center;">
										<i class="fa fa-eye-slash"></i> Show Onions
									</button>
								<form>
							</div>
						</div>

						<div class="panel panel-info">
      				<div class="panel-heading">
                <i class="fa fa-eye-slash"></i> Domains Crawled Today
      				</div>

              <table class="table table-hover table-striped">
                <tbody>
                  <tr>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_up=True">
												<div style="color:Green; display:inline-block">
													<i class="fa fa-check-circle fa-2x"></i>
													Domains UP
												</div>
											</a>
										</td>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_up=True">
												<div style="color:Green; display:inline-block">
													<div id="text_domain_up">{{ statDomains['domains_up'] }}</div>
												</div>
											</a>
										</td>
                  </tr>
                  <tr>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_down=True">
												<div style="color:Red; display:inline-block">
													<i class="fa fa-times-circle fa-2x"></i>
													Domains DOWN
												</div>
											</a>
										</td>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_down=True">
												<div style="color:Red; display:inline-block">
													<div id="text_domain_down">{{ statDomains['domains_down'] }}</div>
												</div>
											</a>
										</td>
                  </tr>
                  <tr>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_up=True&domains_down=True">
												Crawled Domains
											</a>
										</td>
                    <td>
											<a target="_blank" href="{{ url_for('hiddenServices.show_domains_by_daterange') }}?date_from={{ date_from }}&domains_up=True&domains_down=True">
												<div id="text_total_domains">{{ statDomains['total'] }}</div>
											</a>
										</td>
                  </tr>
									<tr>
                    <td>Domains in Queue</td>
                    <td><div id="text_domain_queue">{{ statDomains['domains_queue'] }}</div></td>
                  </tr>
                </tbody>
              </table>
            </div>

						<div class="panel panel-info" {%if not crawler_metadata%}hidden{%endif%} id="panel_crawler">
	      			<div class="panel-heading">
	               Crawlers Status
	      			</div>
	              <table class="table table-hover table-striped">
                <tbody id="tbody_crawler_info">
									{% for crawler in crawler_metadata %}
	                  <tr>
		                   <td>
												<i class="fa fa-{%if crawler['status']%}check{%else%}times{%endif%}-circle fa-2x" 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>
            <!-- /#page-wrapper -->

            <script>
								var all_graph = {};
								var to_refresh = false
                $(document).ready(function(){
                    activePage = "page-hiddenServices"
                    $("#"+activePage).addClass("active");

										$('#date-range-from').dateRangePicker({
											separator : ' to ',
											getValue: function()
											{
												if ($('#date-range-from').val() && $('#date-range-to').val() )
													return $('#date-range-from').val() + ' to ' + $('#date-range-to').val();
												else
													return '';
											},
											setValue: function(s,s1,s2)
											{
												$('#date-range-from').val(s1);
												$('#date-range-to').val(s2);
											}
						  			});
										$('#date-range-to').dateRangePicker({
											separator : ' to ',
											getValue: function()
											{
												if ($('#date-range-from').val() && $('#date-range-to').val() )
													return $('#date-range-from').val() + ' to ' + $('#date-range-to').val();
												else
													return '';
											},
											setValue: function(s,s1,s2)
											{
												$('#date-range-from').val(s1);
												$('#date-range-to').val(s2);
											}
						  			});

										all_graph.line_chart = create_line_chart('graph_line', "{{ url_for('hiddenServices.domain_crawled_7days_json') }}?type=onion");

										$( window ).focus(function() {
											to_refresh = true
											refresh_list_crawled();
										});
										$( window ).blur(function() {
											to_refresh = false
										});

										to_refresh = true
										refresh_list_crawled();
								});
									$(window).on("resize", function() {
										all_graph.onResize();
									});

            </script>

<script>

all_graph.onResize = function () {
	var aspect = 1000 / 500, all_graph = $("#graph_div");
	var targetWidth = all_graph.parent().width();
	all_graph.attr("width", targetWidth);
	all_graph.attr("height", targetWidth / aspect);
}
window.all_graph = all_graph;

function create_line_chart(id, url){
	var width = 650;
	var height = Math.round(width / 6);
	var margin = {top: 20, right: 55, bottom: 50, left: 40};
	var x = d3.scaleTime().range([0, width]);
	var y = d3.scaleLinear().rangeRound([height, 0]);
	var xAxis = d3.axisBottom(x);
	var yAxis = d3.axisLeft(y);
	var parseTime = d3.timeParse("%Y-%m-%d");
	var line = d3.line()
							.x(function(d) {
          			return x(d.date);
        			})
							.y(function(d) {
          			return y(d.value);
        			});
	var svg_line = d3.select('#'+id).append('svg')
				.attr("id", "graph_div")
				.attr("width", width + margin.left + margin.right)
				.attr("height", height + margin.top + margin.bottom)
				.append('g')
						.attr('transform', "translate("+ margin.left +","+ margin.top +")");
	var div = d3.select('body').append('div')
				.attr('class', 'tooltip')
				.style('opacity', 0);
	//add div tooltip
d3.json(url)
	.then(function(data){
		data.forEach(function(d) {
			d.date_label = d.date;
			d.date = parseTime(d.date);
			d.value = +d.value;
		});
		// fit the data
		x.domain(d3.extent(data, function(d) { return d.date; }));
		//x.domain(data.map(function (d) { return d.date; })); //E
		y.domain([0, d3.max(data, function(d){ return d.value ; })]);
		//line
		svg_line.append("path")
				.data([data])
				.attr("class", "line_graph")
				.attr("d", line);
		// add X axis
		svg_line.append("g")
		    .attr("transform", "translate(0," + height + ")")
		    .call(d3.axisBottom(x))
				.selectAll("text")
					.style("text-anchor", "end")
					.attr("transform", "rotate(-45)" );
		// Add the Y Axis
		svg_line.append("g")
		    .call(d3.axisLeft(y));
		//add a dot circle
    svg_line.selectAll('dot')
				.data(data).enter()
				.append('circle')
						.attr('r', 2)
							.attr('cx', function(d) { return x(d.date); })
							.attr('cy', function(d) { return y(d.value); })
            	.on('mouseover', function(d) {
	            	div.transition().style('opacity', .9);
	            	div.html('' + d.date_label+ '<br/>' + d.value).style('left', (d3.event.pageX) + 'px')
								.style("left", (d3.event.pageX) + "px")
								.style("top", (d3.event.pageY - 28) + "px");
          		})
            	.on('mouseout', function(d)
          			{
            			div.transition().style('opacity', 0);
          			});
	});
}
</script>

<script>

function refresh_list_crawled(){

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

			var tableRef = document.getElementById('tbody_last_crawled');
			$("#tbody_last_crawled").empty()

			for (var i = 0; i < data.last_onions.length; i++) {
					var data_domain = data.last_onions[i]
					var newRow   = tableRef.insertRow(tableRef.rows.length);

			    var newCell  = newRow.insertCell(0);
					newCell.innerHTML = "<td><a target=\"_blank\" href=\"{{ url_for('hiddenServices.onion_domain') }}?onion_domain="+data_domain['domain']+"\">"+data_domain['domain']+"</a></td>";

					newCell  = newRow.insertCell(1);
					newCell.innerHTML = "<td>"+data_domain['first_seen'].substr(0, 4)+"/"+data_domain['first_seen'].substr(4, 2)+"/"+data_domain['first_seen'].substr(6, 2)+"</td>"

					newCell  = newRow.insertCell(2);
					newCell.innerHTML = "<td>"+data_domain['last_check'].substr(0, 4)+"/"+data_domain['last_check'].substr(4, 2)+"/"+data_domain['last_check'].substr(6, 2)+"</td>"

					newCell  = newRow.insertCell(3);
					newCell.innerHTML = "<td><div style=\"color:"+data_domain['status_color']+"; display:inline-block\"><i class=\"fa "+data_domain['status_icon']+" fa-2x\"></i>"+data_domain['status_text']+"</div></td>"

			}
			var statDomains = data.statDomains
			document.getElementById('text_domain_up').innerHTML = statDomains['domains_up']
			document.getElementById('text_domain_down').innerHTML = statDomains['domains_down']
			document.getElementById('text_domain_queue').innerHTML = statDomains['domains_queue']
			document.getElementById('text_total_domains').innerHTML = statDomains['total']

			if(data.crawler_metadata.length!=0){
				$("#tbody_crawler_info").empty();
				var tableRef = document.getElementById('tbody_crawler_info');
				for (var i = 0; i < data.crawler_metadata.length; i++) {
						var crawler = data.crawler_metadata[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=\"fa fa-"+icon+"-circle fa-2x\" style=\"color:"+text_color+";\"></i>"+crawler['crawler_info']+"</td>";

						newCell  = newRow.insertCell(1);
						newCell.innerHTML = "<td><a target=\"_blank\" href=\"{{ url_for('hiddenServices.onion_domain') }}?onion_domain="+crawler['crawling_domain']+"\">"+crawler['crawling_domain']+"</a></td>";

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

						$("#panel_crawler").show();
				}
			} else {
				$("#panel_crawler").hide();
			}
		}
	);

	if (to_refresh) {
    setTimeout("refresh_list_crawled()", 10000);
	}
}


</script>
        </body>

</html>
