{{template "header.tmpl" .}}
<style type="text/css">
#filterform{
	margin: 10px 0px;
}
#filterform .form-group{
	margin-right: 20px;
}
</style>
</head>
<body>
{{template "navbar.tmpl" .}}

<div class="container-fluid">
	<h4 class="text-center">Redis服务列表 <small>[<a href="#" id="add_btn"><i class="glyphicon glyphicon-plus"></i> 点击这里添加</a>]</small></h4>
	<hr />
	<table class="table table-bordered" style="margin-bottom:0px;">
		<thead>
			<tr>
				<th>ID</th>
				<th>服务地址</th>
				<th>备注名</th>
				<th>连接密码</th>
				<th>连接超时(秒)&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="连接超时(单位：秒)" data-content="检测Redis状态时如果连接操作超过该值将会被认为Redis状态异常。" onclick="return false;">?</a></th>
				<th>最大失败次数&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最大失败次数" data-content="如果Redis状态检测失败连续失败次数达到该值将会触发报警。" onclick="return false;">?</a></th>
				<th>最小剩余内存&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最小剩余内存" data-content="Redis最小可用的内存空间值，低于该值将会触发自动扩容操作。" onclick="return false;">?</a></th>
				<th>单次扩容大小&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="单次扩容大小" data-content="Redis剩余不足需要扩容时将会增加该数值指定大小的内存空间。" onclick="return false;">?</a></th>
				<th>最大内存使用&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最大内存使用" data-content="自动扩容的内存上限，Redis的内存使用达到该值将不再自动扩容。" onclick="return false;">?</a></th>
				<th>最大连接数&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最大连接数" data-content="Redis的最大并发连接数，达到该值将会触发报警。" onclick="return false;">?</a></th>
				<th>最大新增淘汰数&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最大新增淘汰数" data-content="上次检测之后增加的淘汰记录数上限，达到该值后将触发报警。" onclick="return false;">?</a></th>
				<th>最大QPS&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="最大QPS" data-content="Redis的最大QPS值，达到该值将会触发报警。" onclick="return false;">?</a></th>
				<th>报警邮箱列表&nbsp;<a href="#" data-toggle="popover" data-trigger="focus" title="报警邮箱列表" data-content="接收报警邮件的邮箱地址列表，与邮件组合并后作为接收报警邮箱的邮箱列表。" onclick="return false;">?</a></th>
				<th>状态</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="list"></tbody>
	</table>
</div>

<script type="text/javascript">
var $SESS = {{.Sess}};

$(function(){
	//加载列表
	function redis_list() {
		var list = $('#list');
		list.empty();
		$.post('/syscfg/redis_list', {}, function(resp){
			if (resp && resp.errno == 0 && resp.data) {
				var html = '';
				for (var i=0; i<resp.data.length; i++) {
					var item = resp.data[i];
					html += '<tr>';
					html += '<td>' + item.Id + '</td>';
					html += '<td>' + item.Address + '</td>';
					html += '<td>' + (item.Remark == '' ? '<i class="text-muted">无</i>' : item.Remark) + '</td>';
					html += '<td>' + (item.Password == '' ? '<i class="text-muted">无</i>' : item.Password) + '</td>';
					html += '<td>' + item.MaxConnectWait + '</td>';
					html += '<td>' + item.MaxStatusFailed + '</td>';
					html += '<td>' + size2str(item.MinMemoryFree) + '</td>';
					html += '<td>' + size2str(item.StepMemoryIncrease) + '</td>';
					html += '<td>' + size2str(item.MaxMemoryUsage) + '</td>';
					html += '<td>' + numberFormat(item.MaxConnection) + '</td>';
					html += '<td>' + item.MaxEviIncreased + '</td>';
					html += '<td>' + numberFormat(item.MaxQPS) + '</td>';
					html += '<td>' + (item.MailList == '' ? '<i class="text-muted">无</i>' : item.MailList) + '</td>';
					html += '<td' + (item.Disabled ? ' class="text-danger"' : '') + '>' + (item.Disabled ? '禁用' : '正常') + '</td>';
					html += '<td>';
					html += '	<button class="btn btn-xs btn-primary" type="button" data-value="' + item.Id + '">编辑</button>&nbsp;';
					html += '	<button class="btn btn-xs btn-danger" type="button" data-value="' + item.Id + '">删除</button>';
					html += '</td>';
					html += '</tr>';
				}
				list.html(html);
				//修改
				$('.btn-primary', list).click(function(){
					var id = $(this).attr('data-value');
					$.get('/syscfg/redis_get', {'id': id}, function(resp){
						if (resp && resp.errno == 0) {
							redis_set(resp.data);
						}
					}, 'json');
					return false;
				});
				//删除
				$('.btn-danger', list).click(function(){
					var id = $(this).attr('data-value');
					$.alert({
						'content': '确定要删除该记录吗？',
						'btncancel': true,
						'callback': function() {
							$.post('/syscfg/redis_del', {'id': id}, function(resp){
								if (resp && resp.errno == 0) {
									redis_list();
								} else {
									$.alert({'content': resp.error ? resp.error : '操作失败！'});
								}
							}, 'json');
						}
					});
				});
			}
		}, 'json');
		return false;
	}

	//添加或修改记录
	function redis_set(record){
		record = $.extend({
			'Id': 0,
			'Address': '',
			'Remark': '',
			'Password': '',
			'MaxConnectWait': 5,
			'MaxStatusFailed': 1,
			'MinMemoryFree': 10*1024*1024,
			'StepMemoryIncrease': 100*1024*1024,
			'MaxMemoryUsage': 500*1024*1024,
			'MaxConnection': 1024,
			'MaxEviIncreased': 1,
			'MaxQPS': 30000,
			'MailList': '',
			'Disabled': false
		}, record);
		var html = `<form class="form-horizontal">
			<div class="form-group id-group" style="display:none;">
				<label class="col-md-4 control-label">ID</label>
				<div class="col-md-8">
					<input type="text" class="form-control" name="Id" maxlength="20" value="${record.Id}" readonly="readonly" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">服务地址(IP:端口)</label>
				<div class="col-md-8">
					<input type="text" class="form-control" name="Address" maxlength="20" value="${record.Address}" placeholder="IP或域名:端口" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">备注名称</label>
				<div class="col-md-8">
					<input type="text" class="form-control" name="Remark" maxlength="20" value="${record.Remark}" placeholder="用于后台展示" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">连接密码</label>
				<div class="col-md-8">
					<input type="text" class="form-control" name="Password" maxlength="20" value="${record.Password}" placeholder="未设置密码时留空" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">连接超时时长(秒)</label>
				<div class="col-md-8">
					<div class="input-group" style="width:100px;">
						<input type="text" class="form-control" name="MaxConnectWait" maxlength="2" value="${record.MaxConnectWait}" />
						<div class="input-group-addon">秒</div>
					</div>
					<p class="help-block">连接等待时长超过该值将当作失败处理</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最大状态检测失败次数</label>
				<div class="col-md-8">
					<div class="input-group" style="width:100px;">
						<input type="text" class="form-control" name="MaxStatusFailed" maxlength="2" value="${record.MaxStatusFailed}" />
						<div class="input-group-addon">次</div>
					</div>
					<p class="help-block">连续失败次数达到该值将会触发报警</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最小可用内存空间</label>
				<div class="col-md-8">
					<div class="input-group">
						<input type="text" class="form-control" name="MinMemoryFree" maxlength="10" value="${record.MinMemoryFree}" />
						<div class="input-group-btn" data-toggle="buttons">
							<label class="btn btn-default active" data-value="1"><input type="radio" autocomplete="off" /> Byte</label>
							<label class="btn btn-default" data-value="1024"><input type="radio" autocomplete="off" /> KB</label>
							<label class="btn btn-default" data-value="1048576"><input type="radio" autocomplete="off" /> MB</label>
							<label class="btn btn-default" data-value="1073741824"><input type="radio" autocomplete="off" /> GB</label>
						</div>
					</div>
					<p class="help-block">正整数，低于该值将自动扩容,设置0跳过该检查项</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">单次扩容大小</label>
				<div class="col-md-8">
					<div class="input-group">
						<input type="text" class="form-control" name="StepMemoryIncrease" maxlength="10" value="${record.StepMemoryIncrease}" />
						<div class="input-group-btn" data-toggle="buttons">
							<label class="btn btn-default active" data-value="1"><input type="radio" autocomplete="off" /> Byte</label>
							<label class="btn btn-default" data-value="1024"><input type="radio" autocomplete="off" /> KB</label>
							<label class="btn btn-default" data-value="1048576"><input type="radio" autocomplete="off" /> MB</label>
							<label class="btn btn-default" data-value="1073741824"><input type="radio" autocomplete="off" /> GB</label>
						</div>
					</div>
					<p class="help-block">正整数，剩余空间不足需要扩容时将增加该值指定大小的内存</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最大使用内存上限</label>
				<div class="col-md-8">
					<div class="input-group">
						<input type="text" class="form-control" name="MaxMemoryUsage" maxlength="10" value="${record.MaxMemoryUsage}" />
						<div class="input-group-btn" data-toggle="buttons">
							<label class="btn btn-default active" data-value="1"><input type="radio" autocomplete="off" /> Byte</label>
							<label class="btn btn-default" data-value="1024"><input type="radio" autocomplete="off" /> KB</label>
							<label class="btn btn-default" data-value="1048576"><input type="radio" autocomplete="off" /> MB</label>
							<label class="btn btn-default" data-value="1073741824"><input type="radio" autocomplete="off" /> GB</label>
						</div>
					</div>
					<p class="help-block">正整数，已用内存达到该值将不再自动扩容</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最大客户端并发连接数</label>
				<div class="col-md-8">
					<div class="input-group" style="width:150px;">
						<input type="text" class="form-control" name="MaxConnection" maxlength="10" value="${record.MaxConnection}" />
						<div class="input-group-addon">个</div>
					</div>
					<p class="help-block">超过该值将会触发报警</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最大新增淘汰记录数</label>
				<div class="col-md-8">
					<div class="input-group" style="width:100px;">
						<input type="text" class="form-control" name="MaxEviIncreased" maxlength="5" value="${record.MaxEviIncreased}" />
						<div class="input-group-addon">个</div>
					</div>
					<p class="help-block">超过该值将会触发报警</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">最大QPS上限值</label>
				<div class="col-md-8">
					<div class="input-group" style="width:150px;">
						<input type="text" class="form-control" name="MaxQPS" maxlength="10" value="${record.MaxQPS}" />
						<div class="input-group-addon">次/s</div>
					</div>
					<p class="help-block">超过该值将会触发报警</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">报警邮件接收邮箱列表<br />(多个之间使用“;”间隔)</label>
				<div class="col-md-8">
					<textarea class="form-control" name="MailList" rows="4" maxlength="1000">${record.MailList}</textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">状态</label>
				<div class="col-md-8">
					<select class="form-control" name="Disabled" style="width:100px;">
						<option value="0">正常</option>
						<option value="1">禁用</option>
					</select>
				</div>
			</div>
		</form>`;
		$.alert({
			'title': record.Id == 0 ? '添加新配置' : '修改配置',
			'content': html,
			'init': function(dlg) {
				var form = $('form', dlg).get(0);
				form.Disabled.value = record.Disabled ? '1' : '0';
				$(form.MinMemoryFree).change(function(){
					this.value = numberFormat(this.value);
				});
				$(form.MaxMemoryUsage).change(function(){
					this.value = numberFormat(this.value);
				});
				$(form.StepMemoryIncrease).change(function(){
					this.value = numberFormat(this.value);
				});
				$(form.MinMemoryFree).change();
				$(form.MaxMemoryUsage).change();
				$(form.StepMemoryIncrease).change();
				if (record.Id == 0) {
					$('.id-group', dlg).hide();
				} else {
					$('.id-group', dlg).show();
				}
			},
			'callback': function(dlg){
				var form = $('form', dlg).get(0);
				var MinMemoryFreeTimes = parseInt($('label.active', $(form.MinMemoryFree).parent()).attr('data-value'));
				var MaxMemoryUsageTimes = parseInt($('label.active', $(form.MaxMemoryUsage).parent()).attr('data-value'));
				var StepMemoryIncreaseTimes = parseInt($('label.active', $(form.StepMemoryIncrease).parent()).attr('data-value'));
				console.debug(MinMemoryFreeTimes, MaxMemoryUsageTimes, StepMemoryIncreaseTimes);
				form.MinMemoryFree.value = parseInt(form.MinMemoryFree.value.replace(/,/g, '')) * MinMemoryFreeTimes;
				form.MaxMemoryUsage.value = parseInt(form.MaxMemoryUsage.value.replace(/,/g, '')) * MaxMemoryUsageTimes;
				form.StepMemoryIncrease.value = parseInt(form.StepMemoryIncrease.value.replace(/,/g, '')) * StepMemoryIncreaseTimes;
				$('.input-group-btn label', form).removeClass('active');
				$('.input-group-btn :first-child', form).addClass('active');
				if (form.Address.value == '') {
					$.alert({'content': '请填写Redis的服务地址！'});
				} else if (!/[0-9A-Za-z.\-]+:\d{3,5}$/.test(form.Address.value)) {
					$.alert({'content': 'Redis服务地址格式不正确！'});
				} else if (form.Remark.value == '') {
					$.alert({'content': '请填写备注名称！'});
				} else if (/\D/.test(form.MaxConnectWait.value)) {
					$.alert({'content': '请填写正确的连接超时时长！'});
				} else if (/\D/.test(form.MaxStatusFailed.value)) {
					$.alert({'content': '请填写正确的最大状态检测失败次数！'});
				} else if (/\D/.test(form.MinMemoryFree.value)) {
					$.alert({'content': '请填写正确的最小可用内存空间(字节)！'});
				} else if (/\D/.test(form.StepMemoryIncrease.value)) {
					$.alert({'content': '请填写正确的单次扩容大小！'});
				} else if (/\D/.test(form.MaxMemoryUsage.value)) {
					$.alert({'content': '请填写正确的最大使用内存上限！'});
				} else if (/\D/.test(form.MaxConnection.value)) {
					$.alert({'content': '请填写正确的最大客户端并发连接数！'});
				} else if (/\D/.test(form.MaxEviIncreased.value)) {
					$.alert({'content': '请填写正确的最大新增淘汰记录数！'});
				} else if (/\D/.test(form.MaxQPS.value)) {
					$.alert({'content': '请填写正确的最大QPS上限值！'});
				} else if (form.MailList.value == '') {
					$.alert({'content': '请填写报警邮件接收邮箱列表！'});
				} else {
					$.post('/syscfg/redis_set', $(form).serialize(), function(resp){
						if (resp && resp.errno == 0) {
							redis_list();
						} else {
							$.alert({'content': resp.error ? resp.error : '操作失败！'});
						}
					}, 'json');
					return true;
				}
				return false;
			},
			'btncancel': true
		});
		return false;
	}

	redis_list(); //加载页面
	$('#add_btn').click(redis_set); //添加按钮
	$('[data-toggle="popover"]').popover(); //tips
});
</script>
</body>
</html>