1 package compbio.statistic;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
11 import compbio.cassandra.DateBean;
12 import compbio.cassandra.ProteinBean;
13 import compbio.cassandra.CassandraNativeConnector;
14 import compbio.cassandra.CassandraReader;
15 import compbio.cassandra.DataBase;
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;
22 import compbio.engine.JobStatus;
24 public class CassandraRequester {
25 private CassandraReader db = new CassandraReader();
26 private ArrayList<DataBase> query;
27 private static long currentDate = 0;
28 private static long earlestDate = 0;
29 private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
30 private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
33 * query: execution time for the period from date1 till date2
35 public List<DataBase> extractExecutionTime(String date1, String date2) {
42 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
43 System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
48 long dateStart = DateParsing(date1, formatYYMMDD);
49 long dateEnd = DateParsing(date2, formatYYMMDD);
50 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
52 if (dateStart < earlestDate)
53 dateStart = earlestDate;
54 if (dateEnd > currentDate)
55 dateStart = currentDate;
57 Calendar start = Calendar.getInstance();
58 start.setTime(new Date(dateStart));
59 Calendar end = Calendar.getInstance();
60 end.setTime(new Date(dateEnd));
61 query = new ArrayList<DataBase>();
62 List<Integer> totalTime = new ArrayList<Integer>();
63 for (int i = 0; i < nbins; i++)
65 List<Pair<String, String>> res = db.ReadProteinDataTable();
66 List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
68 for (Pair<String, String> entry : res) {
69 SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
71 Date jobstartdate = dateformatter.parse(entry.getElement0());
72 long date = jobstartdate.getTime();
73 if (dateStart <= date && date <= dateEnd) {
74 SimpleDateFormat datetimeformatter = new SimpleDateFormat("yyyy/MM/dd:H:m:s");
75 Date jobstarttime = datetimeformatter.parse(entry.getElement0());
76 Date jobendtime = datetimeformatter.parse(entry.getElement1());
77 long diff = (jobendtime.getTime() - jobstarttime.getTime()) / 1000;
78 Pair<Date, Long> pair = new Pair<Date, Long>(jobstartdate, Long.valueOf(diff));
81 } catch (ParseException e) {
86 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
87 List<Integer> timeResult = new ArrayList<Integer>();
88 for (int i = 0; i < nbins; i++)
90 for (Pair<Date, Long> p : numres) {
91 if (date.equals(p.getElement0())) {
92 long lenResult = p.getElement1().longValue();
94 timeResult.set(0, timeResult.get(0) + 1);
95 else if (lenResult > 30 && lenResult <= 60)
96 timeResult.set(1, timeResult.get(1) + 1);
97 else if (lenResult > 60 && lenResult <= 120)
98 timeResult.set(2, timeResult.get(2) + 1);
99 else if (lenResult > 120 && lenResult <= 600)
100 timeResult.set(3, timeResult.get(3) + 1);
102 timeResult.set(4, timeResult.get(4) + 1);
106 for (int i = 0; i < nbins; i++)
107 totalTime.set(i, totalTime.get(i) + timeResult.get(i));
108 DataBase db = new DataBase();
109 db.setTimeRez(timeResult);
110 db.setDate(DateFormat(date.getTime()));
114 DataBase db = new DataBase();
115 db.setTimeTotalExec(totalTime);
121 * query: total number of jobs for the period from date1 till date2
123 public TotalJobsStatisticBean countJobs(String date1, String date2) {
130 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
131 System.out.println("CassandraRequester.countJobs: wrong format for date1 " + date1 + "or date2 " + date2);
135 long dateStart = DateParsing(date1, formatYYMMDD);
136 long dateEnd = DateParsing(date2, formatYYMMDD);
137 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
139 if (dateStart < earlestDate)
140 dateStart = earlestDate;
141 if (dateEnd > currentDate)
142 dateStart = currentDate;
144 Calendar start = Calendar.getInstance();
145 start.setTime(new Date(dateStart));
146 Calendar end = Calendar.getInstance();
147 end.setTime(new Date(dateEnd));
148 TotalJobsStatisticBean query = new TotalJobsStatisticBean();
149 Total wholeTotal = new Total(0, 0, 0, 0, 0);
150 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
151 Total res = db.ReadDateTable(date.getTime());
154 query.setDateTotal(DateFormat(date.getTime()), res);
155 wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
156 wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
157 wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
158 wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
159 wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
161 query.setWholeTotal(wholeTotal);
166 * query: jobs and sequence at date
168 public DateBean readJobByDay(String date, JobStatus status) {
169 if (!isThisDateValid(date, formatDDMMYY)) {
170 System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
174 long day = DateParsing(date, formatDDMMYY);
175 if (day < earlestDate || day > currentDate)
178 if (status == JobStatus.OK) {
179 return db.ReadProteinData(day, date);
181 return db.ReadFailedJobs(day, date, status);
185 * query: protein sequence
187 public List<ProteinBean> readProteins(String protIn, String searchtype) {
188 List<ProteinBean> result;
189 if (searchtype.equals("whole"))
190 result = db.ReadWholeSequence(protIn);
192 result = db.ReadPartOfSequence(protIn);
196 if (searchtype.equals("partial")) {
197 for (ProteinBean entry : result) {
198 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
205 * query protein sequences with number of jobs
207 public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
208 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
209 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
212 for (Map.Entry<String, Integer> entry : map.entrySet()) {
213 if (entry.getValue() > minimalcounter) {
214 TotalByCounterBean bean = new TotalByCounterBean();
215 bean.setTotaljobs(entry.getValue());
216 bean.setName(entry.getKey());
224 * query ip with number of jobs
226 public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
227 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
228 Map<String, Integer> map = db.ReadIpByCounter();
229 if (minimalcounter == null)
233 for (Map.Entry<String, Integer> entry : map.entrySet()) {
234 if (entry.getValue() > minimalcounter) {
235 TotalByCounterBean bean = new TotalByCounterBean();
236 bean.setTotaljobs(entry.getValue());
237 bean.setName(entry.getKey());
245 * query jobs log info
247 public JobBean readJobLog(String jobid) {
250 return db.ReadJobLog(jobid);
254 * query jobs by ipStructureJobLog
256 public UserBean readIp(String ip) {
259 Map<String, String[]> res = db.ReadIpWithJobs(ip);
262 UserBean query = new UserBean(ip);
263 query.setMainInfo(res);
268 * create list of parts of protein sequence;
270 private static List<String> CreateSubprot(String protein, String subprot) {
271 List<String> sub = new ArrayList<String>();
272 String subStr = protein;
273 while (subStr.length() > 0 && subStr.contains(subprot)) {
274 String first = subStr.substring(0, subStr.indexOf(subprot));
275 if (first.length() > 0)
278 subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
280 if (subStr.length() > 0)
286 * convert String date into long date (miliseconds since the epoch start)
288 private static long DateParsing(String datInput, SimpleDateFormat formatter) {
289 if (datInput == null) {
295 dateWorkSt = formatter.parse(datInput).getTime();
296 } catch (ParseException e) {
302 // convert long to date in string format
303 private static String DateFormat(long inDate) {
304 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
305 return datformat.format(new Date(inDate));
309 * set earlest date and current dates. earlestDate is static and should be
310 * set at the 1st call currentDate should be re-calculated every time
312 private static void SetDateRange() {
313 Calendar cal = Calendar.getInstance();
314 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
316 if (0 == earlestDate) {
317 CassandraRequester cr = new CassandraRequester();
318 earlestDate = cr.earliestDate();
322 public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
323 if (dateToValidate == null || dateToValidate.equals("")) {
327 // if not valid, this will throw ParseException
328 sdf.setLenient(false);
329 Date date = sdf.parse(dateToValidate);
330 } catch (ParseException e) {
338 * find the earliest date in the database
340 public long earliestDate() {
341 earlestDate = CassandraNativeConnector.getEarliestDateInDB();