{% extends "base.html" %}
{% block title %}{{ lang.menu_links.hapservers.h2 }} {{ service_desc.service }}{% endblock %}
{% block h2 %}{{ lang.menu_links.hapservers.h2 }} {{ service_desc.service }}{% endblock %}
{% block content %}
{% from 'include/input_macros.html' import input, checkbox, select, copy_to_clipboard %}
<script src="/inc/overview.js"></script>
<link href="/inc/css/chart.min.css" rel="stylesheet">
<script src="/inc/metrics.js"></script>
<script src="/inc/chart.min-4.3.0.js"></script>
{% if servers|length == 0 %} {% include 'include/getstarted.html' %} {% endif %}
<style>
.ui-checkboxradio-label {
	padding-bottom: 5px !important;
	padding-top: 5px !important;
}
</style>
<script>
	var ip = []
	var hostnamea = []
	{% for s in servers %}
		ip.push("{{s[2]}}")
		var host = "{{s[2]}}"
		host = host.replace(/\./g, '\\.');
		hostnamea.push(host)
		var ip_for_check = "{{s.2}}";
		ip_for_check = ip_for_check.replace(/\./g, '\\.');
		setInterval(check_service_status, 11000, {{s.0}}, '{{s.2}}', '{{service}}');
	{% endfor %}
	$( function() {
		$( ".sortable" ).sortable({
			revert: true,
			placeholder: "ui-state-highlight"
		});
		$( ".sortable" ).disableSelection();

		$( ".sortable" ).sortable({
			handle: ".portlet-header",
			stop: function(event, ui) {
				var itemOrder = $('.sortable').sortable("toArray");
				for (var i = 0; i < itemOrder.length; i++) {
					var pos = i;
					var id = itemOrder[i].split('-')[2]
					change_pos(pos, id);
				}
			}
		});
		{% for s in services %}
			{% if 'is not installed' in s.1 or s.1 == '' or user_status == 0 %}
				{% if s.0 == 'roxy-wi-checker' %}
					$(':regex(id, alert-)').checkboxradio('disable');
				{% elif s.0 == 'roxy-wi-keep_alive' or s.0 == 'roxy-wi-keep-alive' %}
					$(':regex(id, active-)').checkboxradio('disable');
				{% elif s.0 == 'roxy-wi-metrics' %}
					$(':regex(id, metrics-)').checkboxradio('disable');
				{% endif %}
			{% endif %}
		{% endfor %}
	});
</script>
<div id="up-pannel" class="sortable">
	{%- for s in servers %}
		{%- if serv %}
			{% set config_id = 'id=config_link' %}
			{% set compare_id = 'id=compare_link' %}
			{% set map_id = 'id=map_link' %}
			{% set stats_id = 'id=stats_link' %}
			{% set logs_id = 'id=logs_link' %}
			{% set versions_id = 'id=versions_link' %}
			<script>
				var server_ip = '{{s.2}}'
				function showMetrics() {
					removeData();
					{%- if service == 'haproxy' %}
						{%- for s in servers %}
							{%- if s.8.0.9 %}
								getChartData(server_ip)
								getHttpChartData(server_ip)
								getWafChartData(server_ip)
							{% endif %}
						{% endfor %}
					{% endif %}
					showOverviewServer('{{s.1}}', server_ip, '{{s.0}}', '{{service}}');
					{%- if service == 'nginx' %}
						showNginxConnections(server_ip)
						{% if s.8.0.21 %}
							getNginxChartData(server_ip)
						{% endif %}
					{%- elif service == 'apache' %}
						showApachekBytes(server_ip)
						{% if s.8.0.27 %}
							getApacheChartData(server_ip)
						{% endif %}
					{%- elif service == 'haproxy' %}
						showBytes(server_ip)
					{%- elif service == 'keepalived' %}
						keepalivedBecameMaster(server_ip)
					{% endif %}
				}
				showMetrics();
				$( function() {
					$("#time-range").on('selectmenuchange', function () {
						removeData()
						let metrics = new Promise(
								(resolve, reject) => {
									{% for s in servers %}
										{% if service == 'haproxy' %}
											getChartData(server_ip)
											getHttpChartData(server_ip)
											getWafChartData(server_ip)
										{% elif service == 'nginx' %}
											getNginxChartData(server_ip)
										{% elif service == 'apache' %}
											getApacheChartData(server_ip)
										{% endif %}
									{% endfor %}
								});
						metrics.then();
					});
				});
				$(document).on('keydown', null, 'a', start_button);
				function start_button(event) {
					confirmAjaxAction('start', '{{service}}', '{{s.2}}');
				}
				$(document).on('keydown', null, 's', stop_button);
				function stop_button(event) {
					confirmAjaxAction('stop', '{{service}}', '{{s.2}}');
				}
				$(document).on('keydown', null, 'e', reload_button);
				function reload_button(event) {
					confirmAjaxAction('reload', '{{service}}', '{{s.2}}');
				}
				{% for set in restart_settings %}
					{% if set.server_id == s.0 and set.setting == 'restart' and set.value|int == 0 %}
					$(document).on('keydown', null, 'r', restart_button);
					function restart_button(event) {
						confirmAjaxAction('restart', '{{service}}', '{{s.2}}', '{{s.1}}');
					}
					{% endif %}
				{% endfor %}
				$(document).on('keydown', null, 'k', metrics_button);
				function metrics_button(event) {
					showMetrics();
				}
				$(document).bind('keydown', 'o', function (){
					window.location = document.getElementById('compare_link').href;
				});
				$(document).bind('keydown', 'c', function (){
					window.location = document.getElementById('config_link').href;
				});
				$(document).bind('keydown', 'm', function (){
					window.location = document.getElementById('map_link').href;
				});
				$(document).bind('keydown', 't', function (){
					window.location = document.getElementById('stats_link').href;
				});
				$(document).bind('keydown', 'l', function (){
					window.location = document.getElementById('logs_link').href;
				});
				$(document).bind('keydown', 'v', function (){
					window.location = document.getElementById('versions_link').href;
				});
			</script>
		{% endif %}
		{% set checker_desc = lang.services.hapservers_desc %}
		{% if service == 'nginx' %}
			{% set is_auto_start_enabled = s.8.0.17 %}
			{% set is_checker_enabled = s.8.0.19 %}
			{% set is_metrics_enabled = s.8.0.21 %}
			{% if s.5.0.1 == 'active' or s.5.0.1 == 'Up' %}
				{% set additional_status_class = 'div-server-head-up' %}
			{% else %}
				{% set additional_status_class = 'div-server-head-down' %}
			{% endif %}
		{% elif service == 'keepalived' %}
			{% set is_auto_start_enabled = s.8.0.22 %}
			{% set is_checker_enabled = s.8.0.23 %}
			{% if s.5.0.1 == 'active' %}
				{% set additional_status_class = 'div-server-head-up' %}
			{% else %}
				{% set additional_status_class = 'div-server-head-down' %}
			{% endif %}
		{% elif service == 'apache' %}
			{% set is_auto_start_enabled = s.8.0.25 %}
			{% set is_checker_enabled = s.8.0.26 %}
			{% set is_metrics_enabled = s.8.0.27 %}
			{% if 'Apache' in s.4.0 %}
				{% set additional_status_class = 'div-server-head-up' %}
			{% else %}
				{% set additional_status_class = 'div-server-head-down' %}
			{% endif %}
		{% elif service == 'haproxy' %}
			{% set is_auto_start_enabled = s.8.0.12 %}
			{% set is_checker_enabled = s.8.0.8 %}
			{% set is_metrics_enabled = s.8.0.9 %}
			{% if s.5 != False %}
				{% set additional_status_class = 'div-server-head-up' %}
			{% else %}
				{% set additional_status_class = 'div-server-head-down' %}
			{% endif %}
		{% endif %}
		<div id="div-server-{{s.0}}" class="div-server-hapwi {{additional_status_class}}">
			<div class="server-name">
				<input type="hidden" id="server-name-{{s.0}}" value="{{s.1}}" />
				<input type="hidden" id="service" value="{{service}}" />
				{% if not serv %}
					<a href="{{ url_for('service.services', service=service, serv=s.2) }}" title="{{lang.words.more|title()}} {{lang.words.about}} {{s.1}}" style="color: #5d9ceb">{{s.1}}</a>
				{% else %}
					{{s.1}}
				{% endif %}
				{% if s.6|int() >= 1 %}
					<span
						{% if keep_alive|int() >= 1 %}
						class="shield green" title="Autostart {{lang.words.is}} {{lang.words.enabled2}}">
						{% else %}
						class="shield red" title="Autostart {{lang.words.is}} {{lang.words.enabled2}}, Auto start {{lang.words.service}} {{lang.words.is}} {{lang.words.not}} {{lang.words.running}}">
						{% endif %}
					</span>
				{% endif %}
				{% if s.8.0.20 == 1 %}
					<span class="lock" title="{{lang.phrases.server_is_inaccessible_for_editing}}"></span>
				{% endif %}
				{% for set in docker_settings %}
					{% if set.server_id == s.0 and set.setting == 'dockerized' and set.value == '1' %}
					<span class="box" title="This server is dockerized"></span>
					{% endif %}
				{% endfor %}
				{% if role <= 3 %}
				<span class="server-action">
					<a id="start-{{ s.2 }}" class="start" title="{{lang.words.start|title()}} {{service}} {{lang.words.service}}">
						<span class="service-start" onclick="confirmAjaxAction('start', '{{service}}', '{{s.2}}')"></span>
					</a>
					{% if service != 'keepalived' %}
					<a id="reload-{{ s.2 }}" class="reload" title="{{lang.words.reload|title()}} {{service}} {{lang.words.service}}">
						<span class="service-reload" onclick="confirmAjaxAction('reload', '{{service}}', '{{s.2}}', '{{s.1}}')"></span>
					</a>
					{% endif %}
					{% for set in restart_settings %}
						{% if set.server_id == s.0 and set.setting == 'restart' and set.value|int == 0 %}
						<a id="restart-{{ s.2 }}" class="restart" title="{{lang.words.restart|title()}} {{service}} {{lang.words.service}}">
							<span class="service-reload service-restart" onclick="confirmAjaxAction('restart', '{{service}}', '{{s.2}}')"></span>
						</a>
						{% endif %}
					{% endfor %}
					{% if restart_settings|length == 0 %}
						<a id="restart-{{ s.2 }}" class="restart" title="{{lang.words.restart|title()}} {{service}} {{lang.words.service}}">
							<span class="service-reload service-restart" onclick="confirmAjaxAction('restart', '{{service}}', '{{s.2}}')"></span>
						</a>
					{% endif %}
					<a id="stop-{{ s.2 }}" class="stop" title="{{lang.words.stop|title()}} {{service}} {{lang.words.service}}">
						<span class="service-stop" onclick="confirmAjaxAction('stop', '{{service}}', '{{s.2}}')"></span>
					</a>
					<a href="{{ url_for('main.service_history', service=service, server_ip=s.2) }}" title="{{lang.words.view|title()}} {{lang.words.history3}} {{s.1}}" class="history" style="margin: 0 5px 0 10px;"></a>
					{% if service != 'keepalived' %}
					<span class="menu-bar" onclick="serverSettings('{{s.0}}', '{{s.1}}')" title="{{lang.words.edit|title()}} {{lang.words.settings}} {{s.1}}"></span>
					{% endif %}
					{%if not serv%}
					<span class="portlet-header" title="{{lang.words.change2|title()}} {{lang.words.position}} {{s.1}}">
						<i class="fas fa-grip-vertical"></i>
					</span>
					{%endif%}
				</span>
				{% endif %}
			</div>
			<div class="server-desc">
				{% if s.3 is none %}
				{{lang.words.no_desc|title()}}
				{% else %}
				{{s.3}}
				{% endif %}
				<br />
				{% if service == 'nginx' or service == 'keepalived' %}
					{{lang.words.version|title()}}: {{s.5.0.0}} {{lang.words.process_num|title()}}: {{s.5.0.3}}
					<br />
					{% if s.5.0.1 == 'active' or s.5.0.1 == 'Up' %}
						{{lang.words.started|title()}}:
					{% else %}
						{{lang.words.stopped|title()}}:
					{% endif %}
					{{s.5.0.2}}
				{% elif service == 'apache' %}
					{% if s.4.0 is defined %}
						{{s.4.0.split(' ')[0].split('Server')[1]}} {{s.4.0.split(' ')[1]}} {{s.4.2}}
						<br />
						{{s.4.1.split(' ')[0].split('Server')[1]}} {% for i in s.4.1.split(' ')[1:5] %} {{i}}{% endfor %}
					{% else %}
						{{lang.errors.cannot_get_info}} Apache
					{% endif %}
				{% else %}
					{% if s.5.0 is defined %}
						{{s.5.0.0}} {{s.5.0.1}}
						<br />
						{{s.5.0.2}}
					{% else %}
						{{lang.errors.cannot_get_info}} HAProxy
					{% endif %}
				{% endif %}
				<span title="Date of last configuration edit">
					{{lang.services.last_edit}}:
					<span id="edit_date_{{s.2}}"></span>
				</span>
				<div class="overflow_div">
				IP: {{ copy_to_clipboard(value=s.2, style='font-weight: bold') }}
				{% if s.7.0.0 != None %}
					Master for: <b>{{ copy_to_clipboard(value=s.7.0.1, style='font-weight: bold') }}</b>
				{% endif %}
				{% if s.9.0 == '1' %}
					VRRP: <b>
					{% if s.9.1 == 'MASTER' or s.9.1 == 'BACKUP' or s.9.1 == 'INIT' or s.9.1 == 'FAULT' %}
						{{s.9.1}}
					{% else %}
						<span title="{{lang.errors.cannot_get_info}} Keepalived" class="help_cursor">{{lang.words.error|title()}}</span>
					{% endif %}
					</b>
				{% endif %}
				</div>
			</div>
			<div class="server-act-links" id="server-{{s.8.0.0|string()}}-{{service}}">
				{% if role <= 2 %}
					{% set id = 'alert-' + s.8.0.0|string() %}
					{% if is_checker_enabled == 1 %}
						{{ checkbox(id, title=checker_desc, value='1', desc='Checker', checked='checked') }}
					{% else %}
						{{ checkbox(id, title=checker_desc, value='1', desc='Checker') }}
					{% endif %}
				{% endif %}
				{% if role <= 2 and service != 'keepalived' %}
					{% set id = 'metrics-' + s.8.0.0|string() %}
					{% if is_metrics_enabled == 1 %}
						{{ checkbox(id, title='Collecting metrics is enabled', value='1', desc='Metrics', checked='checked') }}
					{% else %}
						{{ checkbox(id, title='Enable collecting metrics', value='1', desc='Metrics') }}
					{% endif %}
				{% endif %}
				{% if role <= 2 %}
					{% set id = 'active-' + s.8.0.0|string() %}
					{% if is_auto_start_enabled == 1 %}
						{{ checkbox(id, title='Auto Start is enabled', value='1', desc='Auto Start', checked='checked') }}
					{% else %}
						{{ checkbox(id, title='Enable Auto Start', value='1', desc='Auto Start') }}
					{% endif %}
				{% endif %}
			</div>
			<div class="server-act-links">
				{% if service == 'nginx' or service == 'apache' %}
				<a href="/app/config/{{service}}/{{s.2}}/show-files" {{config_id}} class="ui-button ui-widget ui-corner-all" title="{{lang.words.open|title()}} {{lang.words.running}} {{lang.words.configs}}">{{lang.menu_links.config.link}}</a>
				{% else %}
				<a href="/app/config/{{service}}/{{s.2}}/show/undefined" {{config_id}} class="ui-button ui-widget ui-corner-all" title="{{lang.words.open|title()}} {{lang.words.running}} {{lang.words.config}}">{{lang.menu_links.config.link}}</a>
				{% endif %}
				<a href="/app/config/compare/{{service}}/{{s.2}}" {{compare_id}} class="ui-button ui-widget ui-corner-all" title="{{lang.words.compare|title()}} {{lang.words.configs}}">{{lang.words.compare|title()}}</a>
			{% if service == 'haproxy' %}
				<a href="/app/config/map/{{service}}/{{s.2}}" class="ui-button ui-widget ui-corner-all" {{map_id}} title="{{lang.words.show|title()}} {{lang.words.map}}">{{lang.words.map|title()}}</a>
			{% endif %}
			{% if service != 'keepalived' %}
			<a href="{{ url_for('main.stats', service=service, serv=s.2) }}" class="ui-button ui-widget ui-corner-all" {{stats_id}} title="{{lang.words.view|title()}} {{service}} {{lang.words.statistics}}">{{lang.menu_links.stats.link}}</a>
			{% endif %}
			<a href="{{ url_for('logs.logs', service=service) }}" {{logs_id}} class="ui-button ui-widget ui-corner-all" title="{{lang.words.view|title()}} {{service}} {{lang.words.logs}}">{{lang.menu_links.logs.link}}</a>
			{% if role <= 2 %}
				<a href="{{ url_for('config.versions', service=service, server_ip=s.2) }}" class="ui-button ui-widget ui-corner-all" {{versions_id}} title="{{lang.words.view|title()}}/{{lang.words.rollback|title()}} {{lang.words.to}} {{lang.words.previous}} {{lang.words.config}}">{{lang.menu_links.versions.link}}</a>
			{% endif %}
			</div>
		</div>
		{% if serv %}
			<div id="bin_bout"></div>
			<div id="ajax-server-{{s.0}}" class="ajax-server"></div>
			<div class="div-server div-backends" style="margin-bottom: 0px;">
				<div class="server-name backends">
					{% if service == 'haproxy' %}
						{{lang.words.backends|title()}}:
					{% elif service == 'keepalived' %}
						VRRP {{lang.words.addresses|title()}}:
					{% else %}
						{{lang.words.virtual|title()}} {{lang.words.hosts}}:
					{% endif %}
				</div>
				<div style="margin-top: 10px;" id="top-{{s.2}}"></div>
			</div>
		{% endif %}
	{% endfor %}
</div>
	{% if serv %}
	{% for s in servers %}
	{% if service in ('haproxy', 'nginx', 'apache') %}
		{% if (service == 'haproxy' and s.8.0.9)
			or (service == 'nginx' and s.8.0.21)
			or (service == 'apache' and s.8.0.27)  %}
			<div class="metrics-time-range">
				<b>{{lang.words.time_range|title()}}:</b>
				<select title="Choose time range" id="time-range">
					<option value="30">30 {{lang.words.minutes}}</option>
					<option value="60">1 {{lang.words.hour}}</option>
					<option value="180">3 {{lang.words.hours}}</option>
					<option value="360">6 {{lang.words.hours2}}</option>
					<option value="720">12 {{lang.words.hours2}}</option>
				</select>
			</div>
			<div style="clear: both;"></div>
			<div id="refresh" class="metrics-refresh" style="margin-right: 140px;" title="{{lang.words.refresh|title()}}" onclick="showMetrics()">
				<span class="refresh"></span>
			</div>
		{% endif %}
	{% endif %}
		 {% if service == 'haproxy' and s.8.0.9 %}
		<div id="server_metrics_div" class="chart-container_overview">
			<canvas id="{{s.2}}" role="img"></canvas>
		</div>
		<div class="chart-container_overview http_metrics_div">
			<canvas id="http_{{s.2}}" role="img"></canvas>
		</div>
		{% if waf_server == s.2 %}
		<div id="waf_metrics_div" class="chart-container_overview">
			<canvas id="waf_{{s.2}}" role="img"></canvas>
		</div>
		{% endif %}
		{% elif service == 'nginx' and s.8.0.21 %}
		<div id="nginx_metrics_div" class="chart-container" style="display: block; width: 90%; height: 300px;">
			<canvas id="nginx_{{s.2}}" role="img"></canvas>
		</div>
		{% elif service == 'apache' and s.8.0.27 %}
		<div id="apache_metrics_div" class="chart-container" style="display: block; width: 90%; height: 300px;">
			<canvas id="apache_{{s.2}}" role="img"></canvas>
		</div>
		{% endif %}
	{% endfor %}
{% endif %}
<div id="dialog-confirm" style="display: none;">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:3px 12px 20px 0;"></span>{{lang.phrases.are_you_sure}}</p>
</div>
<div id="dialog-settings-service" style="display: none;"></div>
<script>
	{% if serv %}
		overviewHapserverBackends(ip, hostnamea, '{{service}}');
	{% endif %}
	showHapservers(ip, hostnamea, '{{service}}');
</script>
{% endblock %}
