1 package compbio.statistic;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
8 import java.util.Iterator;
12 import compbio.cassandra.DateBean;
13 import compbio.cassandra.ProteinBean;
14 import compbio.cassandra.CassandraNativeConnector;
15 import compbio.cassandra.CassandraReader;
16 import compbio.cassandra.Pair;
17 import compbio.cassandra.JobBean;
18 import compbio.cassandra.Total;
19 import compbio.cassandra.TotalByCounterBean;
20 import compbio.cassandra.TotalJobsStatisticBean;
21 import compbio.cassandra.UserBean;
23 public class CassandraRequester {
24 private CassandraReader db = new CassandraReader();
25 private static long currentDate = 0;
26 private static long earlestDate = 0;
27 private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
28 private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
31 * query: execution time for the period from date1 till date2
33 public List<DataBase> extractExecutionTime(String date1, String date2) {
40 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
41 System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
46 long dateStart = DateParsing(date1, formatYYMMDD);
47 long dateEnd = DateParsing(date2, formatYYMMDD);
48 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
50 if (dateStart < earlestDate)
51 dateStart = earlestDate;
52 if (dateEnd > currentDate)
53 dateStart = currentDate;
55 Calendar start = Calendar.getInstance();
56 start.setTime(new Date(dateStart));
57 Calendar end = Calendar.getInstance();
58 end.setTime(new Date(dateEnd));
59 query = new ArrayList<DataBase>();
60 List<Integer> totalTime = new ArrayList<Integer>();
61 for (int i = 0; i < nbins; i++)
63 List<Pair<String, String>> res = db.ReadProteinDataTable();
64 List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
66 for (Pair<String, String> entry : res) {
67 SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
69 Date jobstartdate = dateformatter.parse(entry.getElement0());
70 long date = jobstartdate.getTime();
71 if (dateStart <= date && date <= dateEnd) {
72 SimpleDateFormat datetimeformatter = new SimpleDateFormat("yyyy/MM/dd:H:m:s");
73 Date jobstarttime = datetimeformatter.parse(entry.getElement0());
74 Date jobendtime = datetimeformatter.parse(entry.getElement1());
75 long diff = (jobendtime.getTime() - jobstarttime.getTime()) / 1000;
76 Pair<Date, Long> pair = new Pair<Date, Long>(jobstartdate, Long.valueOf(diff));
79 } catch (ParseException e) {
84 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
85 List<Integer> timeResult = new ArrayList<Integer>();
86 for (int i = 0; i < nbins; i++)
88 for (Pair<Date, Long> p : numres) {
89 if (date.equals(p.getElement0())) {
90 long lenResult = p.getElement1().longValue();
92 timeResult.set(0, timeResult.get(0) + 1);
93 else if (lenResult > 30 && lenResult <= 60)
94 timeResult.set(1, timeResult.get(1) + 1);
95 else if (lenResult > 60 && lenResult <= 120)
96 timeResult.set(2, timeResult.get(2) + 1);
97 else if (lenResult > 120 && lenResult <= 600)
98 timeResult.set(3, timeResult.get(3) + 1);
100 timeResult.set(4, timeResult.get(4) + 1);
104 for (int i = 0; i < nbins; i++)
105 totalTime.set(i, totalTime.get(i) + timeResult.get(i));
106 DataBase db = new DataBase();
107 db.setTimeRez(timeResult);
108 db.setDate(DateFormat(date.getTime()));
112 DataBase db = new DataBase();
113 db.setTimeTotalExec(totalTime);
120 * query: total number of jobs for the period from date1 till date2
122 public TotalJobsStatisticBean countJobs(String date1, String date2) {
129 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
130 System.out.println("CassandraRequester.countJobs: wrong format for date1 " + date1 + "or date2 " + date2);
134 long dateStart = DateParsing(date1, formatYYMMDD);
135 long dateEnd = DateParsing(date2, formatYYMMDD);
136 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
138 if (dateStart < earlestDate)
139 dateStart = earlestDate;
140 if (dateEnd > currentDate)
141 dateStart = currentDate;
143 Calendar start = Calendar.getInstance();
144 start.setTime(new Date(dateStart));
145 Calendar end = Calendar.getInstance();
146 end.setTime(new Date(dateEnd));
147 TotalJobsStatisticBean query = new TotalJobsStatisticBean();
148 Total wholeTotal = new Total(0, 0, 0, 0, 0);
149 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
150 Total res = db.ReadDateTable(date.getTime());
153 query.setDateTotal(DateFormat(date.getTime()), res);
154 wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
155 wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
156 wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
157 wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
158 wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
160 query.setWholeTotal(wholeTotal);
165 * query: jobs and sequence at date
167 public DateBean readJobByDay(String date) {
168 if (!isThisDateValid(date, formatDDMMYY)) {
169 System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
173 long day = DateParsing(date, formatDDMMYY);
174 if (day < earlestDate || day > currentDate)
176 return db.ReadProteinData(day, date);
180 * query: protein sequence
182 public List<ProteinBean> readProteins(String protIn, String flag) {
183 List<ProteinBean> result;
184 if (flag.equals("whole"))
185 result = db.ReadWholeSequence(protIn);
187 result = db.ReadPartOfSequence(protIn);
191 if (flag.equals("part")) {
192 for (ProteinBean entry : result) {
193 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
200 * query protein sequences with number of jobs
202 public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
203 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
204 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
207 for (Map.Entry<String, Integer> entry : map.entrySet()) {
208 if (entry.getValue() > minimalcounter) {
209 TotalByCounterBean bean = new TotalByCounterBean();
210 bean.setTotaljobs(entry.getValue());
211 bean.setName(entry.getKey());
219 * query ip with number of jobs
221 public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
222 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
223 Map<String, Integer> map = db.ReadIpByCounter();
224 if (minimalcounter == null)
228 for (Map.Entry<String, Integer> entry : map.entrySet()) {
229 if (entry.getValue() > minimalcounter) {
230 TotalByCounterBean bean = new TotalByCounterBean();
231 bean.setTotaljobs(entry.getValue());
232 bean.setName(entry.getKey());
240 * query jobs log info
242 public JobBean readJobLog(String jobid) {
245 return db.ReadJobLog(jobid);
249 * query jobs by ipStructureJobLog
251 public UserBean readIp(String ip) {
254 Map<String, String[]> res = db.ReadIpWithJobs(ip);
257 UserBean query = new UserBean(ip);
258 query.setMainInfo(res);
263 * create list of parts of protein sequence;
265 private static List<String> CreateSubprot(String protein, String subprot) {
266 List<String> sub = new ArrayList<String>();
267 String subStr = protein;
268 while (subStr.length() > 0 && subStr.contains(subprot)) {
269 String first = subStr.substring(0, subStr.indexOf(subprot));
270 if (first.length() > 0)
273 subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
275 if (subStr.length() > 0)
281 * convert String date into long date (miliseconds since the epoch start)
283 private static long DateParsing(String datInput, SimpleDateFormat formatter) {
284 if (datInput == null) {
290 dateWorkSt = formatter.parse(datInput).getTime();
291 } catch (ParseException e) {
297 // convert long to date in string format
298 private static String DateFormat(long inDate) {
299 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
300 return datformat.format(new Date(inDate));
303 // convert long to date in string format
304 private static String DateFormatYYMMDD(long inDate) {
305 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
306 return datformat.format(new Date(inDate));
310 * set earlest date and current dates. earlestDate is static and should be
311 * set at the 1st call currentDate should be re-calculated every time
313 private static void SetDateRange() {
314 Calendar cal = Calendar.getInstance();
315 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
317 if (0 == earlestDate) {
318 CassandraRequester cr = new CassandraRequester();
319 earlestDate = cr.earliestDate();
323 public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
324 if (dateToValidate == null || dateToValidate.equals("")) {
328 // if not valid, this will throw ParseException
329 sdf.setLenient(false);
330 Date date = sdf.parse(dateToValidate);
331 } catch (ParseException e) {
339 * find the earliest date in the database
341 public long earliestDate() {
342 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
347 * find the earliest date in the database in String format
349 public String earliestDateString() {
350 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
351 return DateFormatYYMMDD(earlestDate);