<%@ page import="org.b1.core.SqlQueryExecutor" %>
<%@ page import="org.b1.entities.CustomSqlQuery" %>
<%@ page import="org.b1.filters.AdminFilter" %>
<%@ page import="org.b1.presentation.WebTools" %>
<%@ page import="org.b1.common.utils.ApplicationRegistry" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
    String title = "";
    String output = "";
    if (WebTools.isNoStatIp(request) || AdminFilter.adminLoggedIn(request)) {
        String key = request.getParameter("secretKey");
        CustomSqlQuery query = key == null ? null : ApplicationRegistry.getInstance().getCustomSqlStorage().getQueryForSecretKey(key);
        if (query != null) {
            title = query.getName();
            output += "<div><b>" + query.getName() + "</b></div>";
            output += SqlQueryExecutor.INSTANCE.executeQuery(query.getSqlText(), query.getTarget());
        } else {
            output += "Illegal secret key: " + key;
        }
    } else {
        output += "Illegal IP: " + request.getRemoteAddr();
    }
%>
<head>
    <title>SQL - <%=title%></title>
    <link rel="stylesheet" href="/css/adminstats.css?v=4" type="text/css" media="all"/>
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"></script>
</head>
<body>
    <%=output%>
<script src="/js/graphics.js"></script>
<div id="chartContainer">
    <div id="chartNode"></div>
    <div id="legend"></div>
    <div id="chartNode2"></div>
    <div id="legend2"></div>
</div>

</body>
</html>
