package zeta.handler.statistic;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.servlet.ServletException;
import zeta.ZetaServlet;
import zeta.util.DatabaseUtils;
import zeta.util.Table;
public class CloseZerosHandler extends AbstractHandler {
public CloseZerosHandler(ZetaServlet servlet) {
super(servlet, 43200000, 43200000, 86400000); }
public String createPage(Connection con) throws SQLException, ServletException {
List closeZeros = new ArrayList(100);
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT c.work_unit_id,c.start,r.stop,u.name,f.found"
+ " FROM zeta.computation c,zeta.result r,zeta.user u,zeta.found f"
+ " WHERE c.task_id=1 AND c.task_id=r.task_id"
+ " AND c.task_id=f.task_id"
+ " AND c.work_unit_id=r.work_unit_id"
+ " AND c.work_unit_id=f.work_unit_id"
+ " AND c.user_id=u.id"
+ " AND c.server_id=u.server_id"
+ " AND f.type='close zeros'"
+ " AND f.approved_yn='Y'");
while (rs.next()) {
String found = rs.getString(5);
final int l = found.length();
int i = 0;
while (i < l && !Character.isDigit(found.charAt(i))) {
++i;
}
int j = found.indexOf(' ', i);
if (i > 0 && j > i) {
BigDecimal t1 = new BigDecimal(found.substring(i, j));
for (i = j; i < l && !Character.isDigit(found.charAt(i)); ++i);
j = found.indexOf(' ', i);
if (i > 0 && j > i) {
BigDecimal t2 = new BigDecimal(found.substring(i, j));
t2 = t2.subtract(t1);
t2 = t2.add(new BigDecimal(0.000000005));
i = found.indexOf("This happened at ", j);
if (i > 0) {
final Object[] obj = { t2, new Long(rs.getLong(1)), rs.getTimestamp(2), rs.getTimestamp(3), rs.getString(4), new Long(found.substring(i+17)) };
closeZeros.add(obj);
}
}
}
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
}
HtmlTableGenerator generator = new HtmlTableGenerator(servlet, servlet.getHandlerAddress(TopProducersHandler.class), "user");
Table table = new Table(6);
table.setColumnName(0, "place");
table.setType(0, Types.VARCHAR);
table.setAlignment(0, Table.LEFT);
table.setColumnName(1, "user");
table.setType(1, Types.VARCHAR);
table.setAlignment(1, Table.LEFT);
table.setColumnName(2, "gram point");
table.setType(2, Types.INTEGER);
table.setAlignment(2, Table.RIGHT);
table.setColumnName(3, "work unit requested");
table.setType(3, Types.TIMESTAMP);
table.setAlignment(3, Table.CENTER);
table.setFormat(3, new SimpleDateFormat("MM/dd/yyyy"));
table.setColumnName(4, "work unit delivered");
table.setType(4, Types.TIMESTAMP);
table.setAlignment(4, Table.CENTER);
table.setFormat(4, new SimpleDateFormat("MM/dd/yyyy"));
table.setColumnName(5, "distance less than");
table.setType(5, Types.DOUBLE);
table.setAlignment(5, Table.RIGHT);
table.setFormat(5, new DecimalFormat("0.00000000"));
Object[] closeZerosArray = closeZeros.toArray();
Arrays.sort(closeZerosArray, new Comparator() {
public int compare(Object o1, Object o2) {
int cmp = ((BigDecimal)((Object[])o1)[0]).compareTo(((Object[])o2)[0]);
return (cmp == 0)? ((Long)((Object[])o1)[1]).compareTo(((Object[])o2)[1]) : cmp;
}
public boolean equals(Object obj) {
return (((Object[])(Object)this)[0].equals(((Object[])obj)[0]) && ((Object[])(Object)this)[1].equals(((Object[])obj)[1]));
}
}
);
int row = 0;
for (int i = 0; i < closeZerosArray.length; ++i) {
Object[] obj = (Object[])closeZerosArray[i];
String user = ((String)obj[4]).toLowerCase();
table.addRow();
table.setValue(row, 0, String.valueOf(row+1)+'.');
table.setValue(row, 1, obj[4]);
table.setValue(row, 2, obj[5]);
table.setValue(row, 3, obj[2]);
table.setValue(row, 4, obj[3]);
table.setValue(row, 5, obj[0]);
++row;
}
StringBuffer buffer = new StringBuffer(70*1024);
buffer.append("<tr><td colspan=\"6\" height=\"30pt\" class=\"second-head-gray\"><center>Close zeros:</center></td></tr>");
buffer.append("<tr><td colspan=\"6\"><p>Only close zeros with a distance less than 0.0002 are stored in this list.");
buffer.append(" To learn more about close zeros, see <a href=\"http://mathworld.wolfram.com/LehmersPhenomenon.html\">Lehmer's Phenomenon</a>.");
buffer.append("<br>At moment this list is not completed because only a ZetaGrid library version 1.32 or above (released at Nov 9, 2002)");
buffer.append(" evaluates close zeros and stores them directly.");
buffer.append(" All other close zeros that have been found by previous ZetaGrid library versions are currently being analyzed.");
buffer.append(" It will take some months until all results will be reflected in this list (status currently: less than 300 billion zeros about 99.2%, above 300 billion zeros about 50%).");
buffer.append("<br>The close zero at 1,048,449,114 was found in previous computation by J. van de Lune, H. J. J. te Riele and D. T. Winter (1986).");
buffer.append("<br>The close zero at 3,570,918,900 was found in previous computation by J. van de Lune (Sep 24, 2000).");
buffer.append("<tr><td colspan=\"6\">");
buffer.append("<p><center>");
buffer.append(generator.generate(table));
buffer.append("</center></td></tr>");
return buffer.toString();
}
}