/**
* License Agreement for OpenSearchServer
*
* Copyright (C) 2008-2012 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* This file is part of OpenSearchServer.
*
* OpenSearchServer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSearchServer.
* If not, see <http://www.gnu.org/licenses/>.
**/
package com.jaeksoft.searchlib.web;
import java.io.PrintWriter;
import com.jaeksoft.searchlib.Client;
import com.jaeksoft.searchlib.ClientCatalog;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.config.Mailer;
import com.jaeksoft.searchlib.statistics.Aggregate;
import com.jaeksoft.searchlib.statistics.StatisticPeriodEnum;
import com.jaeksoft.searchlib.statistics.StatisticTypeEnum;
import com.jaeksoft.searchlib.statistics.StatisticsAbstract;
public class ReportServlet extends AbstractServlet {
/**
*
*/
private static final long serialVersionUID = 7243867246220284137L;
/**
*
*/
private void doStatistics(Client client, String stat, String period,
PrintWriter pw) throws SearchLibException {
if (stat == null || period == null)
return;
StatisticTypeEnum statType = StatisticTypeEnum.valueOf(stat
.toUpperCase());
StatisticPeriodEnum statPeriod = StatisticPeriodEnum.valueOf(period
.toUpperCase());
StatisticsAbstract statistics = client.getStatisticsList().getStat(
statType, statPeriod);
if (statistics == null)
return;
pw.println("<html>");
pw.println("<p>" + statType + " - " + statPeriod.getName() + "</p>");
pw.println("<table cellpadding=\"1\" cellspacing=\"0\" border=\"1\">");
pw.println("<tr><th>Period start time</th><th>Count</th><th>Average</th><th>Min</th><th>Max</th><th>Error</th></tr>");
for (Aggregate aggr : statistics.getArray()) {
pw.println("<tr>");
pw.println("<td>" + aggr.getStartTime() + "</td>");
pw.println("<td>" + aggr.getCount() + "</td>");
pw.println("<td>" + aggr.getAverage() + "</td>");
pw.println("<td>" + aggr.getMin() + "</td>");
pw.println("<td>" + aggr.getMax() + "</td>");
pw.println("<td>" + aggr.getError() + "</td>");
pw.println("</tr>");
}
pw.println("</table>");
pw.println("</html>");
}
@Override
protected void doRequest(ServletTransaction transaction)
throws ServletException {
Mailer email = null;
try {
Client client = ClientCatalog.getClient(transaction
.getParameterString("use"));
String report = transaction.getParameterString("report");
String emails = transaction.getParameterString("emails");
if (emails == null)
return;
email = new Mailer(true, emails, null);
if ("statistics".equals(report)) {
email.setSubject("OpenSearchServer statistics report");
doStatistics(client, transaction.getParameterString("stat"),
transaction.getParameterString("period"),
email.getHtmlPrintWriter());
}
if (email.isEmpty()) {
transaction.addXmlResponse("Result", "Nothing to send");
} else {
email.send();
transaction.addXmlResponse("MailSent", emails);
}
} catch (Exception e) {
throw new ServletException(e);
} finally {
if (email != null)
email.close();
}
}
}