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.DataBase;
17 import compbio.cassandra.Pair;
18 import compbio.cassandra.JobBean;
19 import compbio.cassandra.Total;
20 import compbio.cassandra.TotalByCounterBean;
21 import compbio.cassandra.TotalJobsStatisticBean;
22 import compbio.cassandra.UserBean;
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) {
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)
177 return db.ReadProteinData(day, date);
182 * query: protein sequence
184 public List<ProteinBean> readProteins(String protIn, String flag) {
185 List<ProteinBean> result;
186 if (flag.equals("whole"))
187 result = db.ReadWholeSequence(protIn);
189 result = db.ReadPartOfSequence(protIn);
193 if (flag.equals("part")) {
194 for (ProteinBean entry : result) {
195 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
203 * query protein sequences with number of jobs
205 public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
206 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
207 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
210 for (Map.Entry<String, Integer> entry : map.entrySet()) {
211 if (entry.getValue() > minimalcounter) {
212 TotalByCounterBean bean = new TotalByCounterBean();
213 bean.setTotaljobs(entry.getValue());
214 bean.setName(entry.getKey());
222 * query ip with number of jobs
224 public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
225 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
226 Map<String, Integer> map = db.ReadIpByCounter();
227 if (minimalcounter == null)
231 for (Map.Entry<String, Integer> entry : map.entrySet()) {
232 if (entry.getValue() > minimalcounter) {
233 TotalByCounterBean bean = new TotalByCounterBean();
234 bean.setTotaljobs(entry.getValue());
235 bean.setName(entry.getKey());
242 * query jobs log info
244 public JobBean readJobLog(String jobid) {
247 return db.ReadJobLog(jobid);
252 * query jobs by ipStructureJobLog
254 public UserBean readIp(String ip) {
257 Map<String, String[]> res = db.ReadIpWithJobs(ip);
260 UserBean query = new UserBean(ip);
261 query.setMainInfo(res);
266 * create list of parts of protein sequence;
268 private static List<String> CreateSubprot (String protein, String subprot) {
269 List<String> sub = new ArrayList<String>();
270 String subStr = protein;
271 while (subStr.length() > 0 && subStr.contains(subprot)) {
272 String first = subStr.substring(0, subStr.indexOf(subprot));
273 if (first.length() > 0)
276 subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
278 if (subStr.length() > 0)
283 * convert String date into long date (miliseconds since the epoch start)
285 private static long DateParsing(String datInput, SimpleDateFormat formatter) {
286 if (datInput == null) {
292 dateWorkSt = formatter.parse(datInput).getTime();
293 } catch (ParseException e) {
299 // convert long to date in string format
300 private static String DateFormat(long inDate) {
301 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
302 return datformat.format(new Date(inDate));
306 * set earlest date and current dates. earlestDate is static and should be
307 * set at the 1st call currentDate should be re-calculated every time
309 private static void SetDateRange() {
310 Calendar cal = Calendar.getInstance();
311 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH), formatYYMMDD);
312 if (0 == earlestDate) {
313 CassandraRequester cr = new CassandraRequester();
314 earlestDate = cr.earliestDate();
318 public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
319 if (dateToValidate == null || dateToValidate.equals("")) {
323 // if not valid, this will throw ParseException
324 sdf.setLenient(false);
325 Date date = sdf.parse(dateToValidate);
326 } catch (ParseException e) {
334 * find the earliest date in the database
336 public long earliestDate() {
337 earlestDate = CassandraNativeConnector.getEarliestDateInDB();