Merge branch 'DAO'
[proteocache.git] / server / compbio / statistic / CassandraRequester.java
1 package compbio.statistic;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import compbio.beans.DateBean;
13 import compbio.beans.JobBean;
14 import compbio.beans.ProteinBean;
15 import compbio.beans.Total;
16 import compbio.beans.TotalByCounterBean;
17 import compbio.beans.TotalJobsStatisticBean;
18 import compbio.beans.UserBean;
19 import compbio.cassandra.CassandraNativeConnector;
20 import compbio.cassandra.CassandraReaderOld;
21 import compbio.cassandra.DataBase;
22 import compbio.cassandra.Pair;
23 import compbio.engine.JobStatus;
24
25 public class CassandraRequester {
26         private CassandraReaderOld db = new CassandraReaderOld();
27         private ArrayList<DataBase> query;
28         private static long currentDate = 0;
29         private static long earlestDate = 0;
30         private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
31         private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
32
33         /*
34          * query: execution time for the period from date1 till date2
35          */
36         public List<DataBase> extractExecutionTime(String date1, String date2) {
37                 if (null == date1) {
38                         date1 = "1970/1/1";
39                 }
40                 if (null == date2) {
41                         date1 = "2100/1/1";
42                 }
43                 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
44                         System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
45                         return null;
46                 }
47                 SetDateRange();
48                 int nbins = 5;
49                 long dateStart = DateParsing(date1, formatYYMMDD);
50                 long dateEnd = DateParsing(date2, formatYYMMDD);
51                 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
52                         return null;
53                 if (dateStart < earlestDate)
54                         dateStart = earlestDate;
55                 if (dateEnd > currentDate)
56                         dateStart = currentDate;
57
58                 Calendar start = Calendar.getInstance();
59                 start.setTime(new Date(dateStart));
60                 Calendar end = Calendar.getInstance();
61                 end.setTime(new Date(dateEnd));
62                 query = new ArrayList<DataBase>();
63                 List<Integer> totalTime = new ArrayList<Integer>();
64                 for (int i = 0; i < nbins; i++)
65                         totalTime.add(i, 0);
66                 List<Pair<String, String>> res = db.ReadProteinDataTable();
67                 List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
68
69                 for (Pair<String, String> entry : res) {
70                         SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
71                         try {
72                                 Date jobstartdate = dateformatter.parse(entry.getElement0());
73                                 long date = jobstartdate.getTime();
74                                 if (dateStart <= date && date <= dateEnd) {
75                                         SimpleDateFormat datetimeformatter = new SimpleDateFormat("yyyy/MM/dd:H:m:s");
76                                         Date jobstarttime = datetimeformatter.parse(entry.getElement0());
77                                         Date jobendtime = datetimeformatter.parse(entry.getElement1());
78                                         long diff = (jobendtime.getTime() - jobstarttime.getTime()) / 1000;
79                                         Pair<Date, Long> pair = new Pair<Date, Long>(jobstartdate, Long.valueOf(diff));
80                                         numres.add(pair);
81                                 }
82                         } catch (ParseException e) {
83                                 e.printStackTrace();
84                         }
85                 }
86
87                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
88                         List<Integer> timeResult = new ArrayList<Integer>();
89                         for (int i = 0; i < nbins; i++)
90                                 timeResult.add(i, 0);
91                         for (Pair<Date, Long> p : numres) {
92                                 if (date.equals(p.getElement0())) {
93                                         long lenResult = p.getElement1().longValue();
94                                         if (lenResult <= 30)
95                                                 timeResult.set(0, timeResult.get(0) + 1);
96                                         else if (lenResult > 30 && lenResult <= 60)
97                                                 timeResult.set(1, timeResult.get(1) + 1);
98                                         else if (lenResult > 60 && lenResult <= 120)
99                                                 timeResult.set(2, timeResult.get(2) + 1);
100                                         else if (lenResult > 120 && lenResult <= 600)
101                                                 timeResult.set(3, timeResult.get(3) + 1);
102                                         else {
103                                                 timeResult.set(4, timeResult.get(4) + 1);
104                                         }
105                                 }
106                         }
107                         for (int i = 0; i < nbins; i++)
108                                 totalTime.set(i, totalTime.get(i) + timeResult.get(i));
109                         DataBase db = new DataBase();
110                         db.setTimeRez(timeResult);
111                         db.setDate(DateFormat(date.getTime()));
112                         query.add(db);
113                 }
114
115                 /* ???? Very strange code...
116                 DataBase db = new DataBase();
117                 db.setTimeTotalExec(totalTime);
118                 query.add(db);
119                 */
120                 return query;
121         }
122
123         /*
124          * query: total number of jobs for the period from date1 till date2
125         */ 
126         public TotalJobsStatisticBean countJobs(String date1, String date2) {
127                 /*
128                  * if (null == date1) { date1 = "1970/1/1"; } if (null == date2) { date1
129                  * = "2100/1/1"; } if (!isThisDateValid(date1, formatYYMMDD) ||
130                  * !isThisDateValid(date2, formatYYMMDD)) { System.out.println(
131                  * "CassandraRequester.countJobs: wrong format for date1 " + date1 +
132                  * "or date2 " + date2); return null; }
133                  */
134                 SetDateRange();
135                 long dateStart = DateParsing(date1, formatYYMMDD);
136                 long dateEnd = DateParsing(date2, formatYYMMDD);
137                 /*
138                  * if (dateEnd < earlestDate || dateStart > currentDate || dateStart >
139                  * dateEnd) return null; if (dateStart < earlestDate) dateStart =
140                  * earlestDate; if (dateEnd > currentDate) dateStart = currentDate;
141                  */
142                 Calendar start = Calendar.getInstance();
143                 start.setTime(new Date(dateStart));
144                 Calendar end = Calendar.getInstance();
145                 end.setTime(new Date(dateEnd));
146                 TotalJobsStatisticBean query = new TotalJobsStatisticBean();
147                 Total wholeTotal = new Total(0, 0, 0, 0, 0);
148                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
149                         Total res = db.ReadDateTable(date.getTime());
150                         if (res == null)
151                                 continue;
152                         query.setDateTotal(DateFormat(date.getTime()), res);
153                         wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
154                         wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
155                         wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
156                         wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
157                         wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
158                 }
159                 query.setWholeTotal(wholeTotal);
160                 return query;
161         }
162
163         /*
164          * query: jobs and sequence at date
165          */
166         public DateBean readJobByDay(String date, JobStatus status) {
167                 if (!isThisDateValid(date, formatDDMMYY)) {
168                         System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
169                         return null;
170                 }
171                 SetDateRange();
172                 long day = DateParsing(date, formatDDMMYY);
173                 if (day < earlestDate || day > currentDate)
174                         return null;
175
176                 if (status == JobStatus.OK) {
177                         return db.ReadProteinData(day, date);
178                 }
179                 return db.ReadFailedJobs(day, date, status);
180         }
181
182         /*
183          * query: protein sequence
184          */
185         public List<ProteinBean> readProteins(String protIn, String searchtype) {
186                 List<ProteinBean> result;
187                 if (searchtype.equals("whole"))
188                         result = db.ReadWholeSequence(protIn);
189                 else
190                         result = db.ReadPartOfSequence(protIn);
191                 if (result == null)
192                         return null;
193
194                 if (searchtype.equals("partial")) {
195                         for (ProteinBean entry : result) {
196                                 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
197                         }
198                 }
199                 return result;
200         }
201
202         /*
203          * query: protein feature
204          */
205         public Map<String, String> readProteinsPrediction(String feature, int percent) {
206                 Map<String, String> result = db.ReadProtein();
207                 ;
208                 if (result == null)
209                         return null;
210                 Map<String, String> query = new HashMap<String, String>();
211                 for (Map.Entry<String, String> entry : result.entrySet()) {
212                         String pred = entry.getValue();
213                         if (pred.replaceAll("[^" + feature + "]", "").length() > pred.length() * percent / 100 && (!entry.getKey().equals(""))) {
214                                 // if (!entry.getKey().equals(""))
215                                 query.put(entry.getKey(), pred);
216                         }
217                 }
218                 return query;
219         }
220
221         /*
222          * query protein sequences with number of jobs
223          */
224         public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
225                 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
226                 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
227                 if (map == null)
228                         return null;
229                 for (Map.Entry<String, Integer> entry : map.entrySet()) {
230                         if (entry.getValue() > minimalcounter) {
231                                 TotalByCounterBean bean = new TotalByCounterBean();
232                                 bean.setTotaljobs(entry.getValue());
233                                 bean.setName(entry.getKey());
234                                 query.add(bean);
235                         }
236                 }
237                 return query;
238         }
239
240         /*
241          * query ip with number of jobs
242          */
243         public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
244                 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
245                 Map<String, Integer> map = db.ReadIpByCounter();
246                 if (minimalcounter == null)
247                         minimalcounter = 0;
248                 if (map == null)
249                         return null;
250                 for (Map.Entry<String, Integer> entry : map.entrySet()) {
251                         if (entry.getValue() > minimalcounter) {
252                                 TotalByCounterBean bean = new TotalByCounterBean();
253                                 bean.setTotaljobs(entry.getValue());
254                                 bean.setName(entry.getKey());
255                                 query.add(bean);
256                         }
257                 }
258                 return query;
259         }
260
261         /*
262          * query jobs log info
263          */
264         public JobBean readJobLog(String jobid) {
265                 if (jobid == null)
266                         return null;
267                 return db.ReadJobLog(jobid);
268         }
269
270         /*
271          * query jobs by ipStructureJobLog
272          */
273         public UserBean readIp(String ip) {
274                 if (ip == null)
275                         return null;
276                 Map<String, String[]> res = db.ReadIpWithJobs(ip);
277                 if (res == null)
278                         return null;
279                 UserBean query = new UserBean(ip);
280                 query.setMainInfo(res);
281                 return query;
282         }
283
284         /*
285          * create list of parts of protein sequence;
286          */
287         private static List<String> CreateSubprot(String protein, String subprot) {
288                 List<String> sub = new ArrayList<String>();
289                 String subStr = protein;
290                 while (subStr.length() > 0 && subStr.contains(subprot)) {
291                         String first = subStr.substring(0, subStr.indexOf(subprot));
292                         if (first.length() > 0)
293                                 sub.add(first);
294                         sub.add(subprot);
295                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
296                 }
297                 if (subStr.length() > 0)
298                         sub.add(subStr);
299                 return sub;
300         }
301
302         /*
303          * convert String date into long date (miliseconds since the epoch start)
304          */
305         private static long DateParsing(String datInput, SimpleDateFormat formatter) {
306                 if (datInput == null) {
307                         return 0;
308                 }
309                 long dateWorkSt = 0;
310
311                 try {
312                         dateWorkSt = formatter.parse(datInput).getTime();
313                 } catch (ParseException e) {
314                         e.printStackTrace();
315                 }
316                 return dateWorkSt;
317         }
318
319         // convert long to date in string format
320         private static String DateFormat(long inDate) {
321                 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
322                 return datformat.format(new Date(inDate));
323         }
324
325         /*
326          * set earlest date and current dates. earlestDate is static and should be
327          * set at the 1st call currentDate should be re-calculated every time
328          */
329         private static void SetDateRange() {
330                 Calendar cal = Calendar.getInstance();
331                 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
332                                 formatYYMMDD);
333                 if (0 == earlestDate) {
334                         CassandraRequester cr = new CassandraRequester();
335                         earlestDate = cr.earliestDate();
336                 }
337         }
338
339         public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
340                 if (dateToValidate == null || dateToValidate.equals("")) {
341                         return false;
342                 }
343                 try {
344                         // if not valid, this will throw ParseException
345                         sdf.setLenient(false);
346                         Date date = sdf.parse(dateToValidate);
347                 } catch (ParseException e) {
348                         e.printStackTrace();
349                         return false;
350                 }
351                 return true;
352         }
353
354         /*
355          * find the earliest date in the database
356          */
357         public long earliestDate() {
358                 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
359                 return earlestDate;
360         }
361
362         /**
363          * prepares an example of either job id or IP for the DB
364          * 
365          * @param exampletype
366          *            defines which example you need (an existing job from the DB -
367          *            jobid, an IP - "ip")
368          * @return a string representation of the requested example, if the example
369          *         type is not known empty string is returned
370          */
371         public String getExample(String exampletype) {
372                 if (exampletype.equals("jobid")) {
373                         return "jp_NzBOJKo";
374                 } else if (exampletype.equals("ip")) {
375                         return "127.0.0.1";
376                 }
377                 return "";
378         }
379
380 }