From bd49ca1a637c5eb6d5f5db420dcd8b22cbb52f59 Mon Sep 17 00:00:00 2001 From: Sasha Sherstnev Date: Tue, 29 Oct 2013 22:31:27 +0000 Subject: [PATCH] Fix problem with dates --- server/compbio/statistic/StatisticsProt.java | 49 ++++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) mode change 100644 => 100755 server/compbio/statistic/StatisticsProt.java diff --git a/server/compbio/statistic/StatisticsProt.java b/server/compbio/statistic/StatisticsProt.java old mode 100644 new mode 100755 index df6fec5..4ee2917 --- a/server/compbio/statistic/StatisticsProt.java +++ b/server/compbio/statistic/StatisticsProt.java @@ -23,7 +23,7 @@ import compbio.cassandra.CassandraCreate; import compbio.cassandra.DataBase; public class StatisticsProt { - private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24; +// private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24; private CassandraCreate cc = new CassandraCreate(); private ArrayList query; private static long currentDate = 0; @@ -31,45 +31,47 @@ public class StatisticsProt { /* query: the period from date1 till date2 */ public List readDetails(String date1, String date2) { + if (!isThisDateValid(date1) || !isThisDateValid(date2)) { System.out.println("Wrong date: point 1"); return null; } SetDateRange(); - long dateStart = DateParsing(date1); long dateEnd = DateParsing(date2); + if ((dateStart < earlestDate && dateEnd < earlestDate) || (dateStart > currentDate && dateEnd > currentDate) + || dateStart > dateEnd) + return null; if (dateStart < earlestDate) dateStart = earlestDate; - if (dateStart > currentDate) - dateStart = currentDate - MILLISECONDS_PER_DAY; - if (dateEnd < earlestDate) - dateStart = earlestDate + MILLISECONDS_PER_DAY; if (dateEnd > currentDate) dateStart = currentDate; System.out.println("StatisticsProt.readDetails: earlestDate = " + earlestDate + ", currentDate = " + currentDate); System.out.println("StatisticsProt.readDetails: Start date " + date1 + ": int representation = " + dateStart); System.out.println("StatisticsProt.readDetails: End date " + date2 + ": int representation = " + dateEnd); - + Calendar start = Calendar.getInstance(); + start.setTime(new Date(dateStart)); + Calendar end = Calendar.getInstance(); + end.setTime(new Date(dateEnd)); query = new ArrayList(); int day = 0; - while (dateStart <= dateEnd) { + for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) { SliceQuery result = HFactory.createSliceQuery(cc.GetKeyspace(), LongSerializer.get(), StringSerializer.get(), StringSerializer.get()); result.setColumnFamily("ProteinData"); - result.setKey(dateStart); + result.setKey(date.getTime()); result.setRange(null, null, false, Integer.MAX_VALUE); QueryResult> columnSlice = result.execute(); ++day; - System.out.print("Day " + day + ": dataStart = " + dateStart + ": "); + System.out.print("Day " + day + ": dataStart = " + date + ": "); if (!columnSlice.get().getColumns().isEmpty()) { - DataBase db = new DataBase(DateFormat(dateStart), columnSlice.get().getColumns().size()); + DataBase db = new DataBase(DateFormat(date.getTime()), columnSlice.get().getColumns().size()); query.add(db); System.out.println("data exist"); } else { System.out.println("no data"); } - dateStart += MILLISECONDS_PER_DAY; + // dateStart += MILLISECONDS_PER_DAY; } System.out.println("StatisticsProt.readLength: total number of dates = " + query.size()); return query; @@ -84,31 +86,32 @@ public class StatisticsProt { return null; } SetDateRange(); - long dateStart = DateParsing(date1); long dateEnd = DateParsing(date2); + if ((dateStart < earlestDate && dateEnd < earlestDate) || (dateStart > currentDate && dateEnd > currentDate) + || dateStart > dateEnd) + return null; if (dateStart < earlestDate) dateStart = earlestDate; - if (dateStart > currentDate) - dateStart = currentDate - MILLISECONDS_PER_DAY; - if (dateEnd < earlestDate) - dateStart = earlestDate + MILLISECONDS_PER_DAY; if (dateEnd > currentDate) dateStart = currentDate; System.out.println("StatisticsProt.readLength: earlestDate = " + earlestDate + ", currentDate = " + currentDate); System.out.println("StatisticsProt.readLength: Start date is " + date1 + ": int representation = " + dateStart); System.out.println("StatisticsProt.readLength: End date is " + date2 + ": int representation = " + dateEnd); - + Calendar start = Calendar.getInstance(); + start.setTime(new Date(dateStart)); + Calendar end = Calendar.getInstance(); + end.setTime(new Date(dateEnd)); query = new ArrayList(); List totalTime = new ArrayList(); for (int i = 0; i < 4; i++) totalTime.add(i, 0); - while (dateStart <= dateEnd) { + for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) { List timeResult = new ArrayList(); SliceQuery result = HFactory.createSliceQuery(cc.GetKeyspace(), LongSerializer.get(), StringSerializer.get(), StringSerializer.get()); result.setColumnFamily("ProteinData"); - result.setKey(dateStart); + result.setKey(date.getTime()); result.setRange(null, null, false, Integer.MAX_VALUE); QueryResult> columnSlice = result.execute(); List> col = columnSlice.get().getColumns(); @@ -133,10 +136,10 @@ public class StatisticsProt { totalTime.set(i, totalTime.get(i) + timeResult.get(i)); DataBase db = new DataBase(); db.setTimeRez(timeResult); - db.setDate(DateFormat(dateStart)); + db.setDate(DateFormat(date.getTime())); query.add(db); } - dateStart += MILLISECONDS_PER_DAY; + // dateStart += MILLISECONDS_PER_DAY; } DataBase db = new DataBase(); db.setTimeTotalExec(totalTime); @@ -195,7 +198,7 @@ public class StatisticsProt { ++npred; } } - if (npred >= counter) { + if (npred > counter) { DataBase db = new DataBase(); db.setProt(last_key); db.setTotalId(npred); -- 1.7.10.2