c150d9da1cc0e01776c8360a3c34e79ec3843614
[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.Iterator;
9 import java.util.List;
10 import java.util.Map;
11
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;
23
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");
31
32         /*
33          * query: execution time for the period from date1 till date2
34          */
35         public List<DataBase> extractExecutionTime(String date1, String date2) {
36                 if (null == date1) {
37                         date1 = "1970/1/1";
38                 }
39                 if (null == date2) {
40                         date1 = "2100/1/1";
41                 }
42                 if (!isThisDateValid(date1,formatYYMMDD) || !isThisDateValid(date2,formatYYMMDD)) {
43                         System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
44                         return null;
45                 }
46                 SetDateRange();
47                 int nbins = 5;
48                 long dateStart = DateParsing(date1, formatYYMMDD);
49                 long dateEnd = DateParsing(date2, formatYYMMDD);
50                 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
51                         return null;
52                 if (dateStart < earlestDate)
53                         dateStart = earlestDate;
54                 if (dateEnd > currentDate)
55                         dateStart = currentDate;
56
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++)
64                         totalTime.add(i, 0);
65                 List<Pair<String, String>> res = db.ReadProteinDataTable();
66                 List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
67
68                 for (Pair<String, String> entry : res) {
69                         SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
70                         try {
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));
79                                         numres.add(pair);
80                                 }
81                         } catch (ParseException e) {
82                                 e.printStackTrace();
83                         }
84                 }
85
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++)
89                                 timeResult.add(i, 0);
90                         for (Pair<Date, Long> p : numres) {
91                                 if (date.equals(p.getElement0())) {
92                                         long lenResult = p.getElement1().longValue();
93                                         if (lenResult <= 30)
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);
101                                         else {
102                                                 timeResult.set(4, timeResult.get(4) + 1);
103                                         }
104                                 }
105                         }
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()));
111                         query.add(db);
112                 }
113
114                 DataBase db = new DataBase();
115                 db.setTimeTotalExec(totalTime);
116                 query.add(db);
117                 return query;
118         }
119         
120         /*
121          * query: total number of jobs  for the period from date1 till date2
122          */
123         public TotalJobsStatisticBean countJobs(String date1, String date2) {
124                 if (null == date1) {
125                         date1 = "1970/1/1";
126                 }
127                 if (null == date2) {
128                         date1 = "2100/1/1";
129                 }
130                 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
131                         System.out.println("CassandraRequester.countJobs: wrong format for date1 " + date1 + "or date2 " + date2);
132                         return null;
133                 }
134                 SetDateRange();
135                 long dateStart = DateParsing(date1, formatYYMMDD);
136                 long dateEnd = DateParsing(date2, formatYYMMDD);
137                 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
138                         return null;
139                 if (dateStart < earlestDate)
140                         dateStart = earlestDate;
141                 if (dateEnd > currentDate)
142                         dateStart = currentDate;
143
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());
152                         if (res == null)
153                                 continue;
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());
160                 }
161                 query.setWholeTotal(wholeTotal);
162                 return query;
163         }
164
165         /*
166          * query: jobs and sequence at date
167          */
168         public DateBean readJobByDay (String date) {
169                 if (!isThisDateValid(date, formatDDMMYY)) {
170                         System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
171                         return null;
172                 }
173                 SetDateRange();
174                 long day = DateParsing(date, formatDDMMYY);
175                 if (day < earlestDate || day > currentDate)
176                         return null;    
177                 return db.ReadProteinData(day, date);
178         }
179
180
181         /* 
182          * query: protein sequence
183          **/
184         public List<ProteinBean> readProteins(String protIn, String flag) {
185                 List<ProteinBean> result;
186                 if (flag.equals("whole")) 
187                         result = db.ReadWholeSequence(protIn);
188                  else 
189                          result = db.ReadPartOfSequence(protIn);
190                 if (result == null)
191                         return null;
192                 
193                 if (flag.equals("part")) {
194                         for (ProteinBean entry : result) {
195                                 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
196                         }
197                 }                                       
198                 return result;
199         }
200         
201
202         /* 
203          * query protein sequences with number of jobs
204          */
205         public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {              
206                 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
207                 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
208                 if (map == null)
209                         return null;
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());
215                                 query.add(bean);
216                         }
217                 }
218                 return query;
219         }
220         
221         /* 
222          * query ip with number of jobs
223          */
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)
228                         minimalcounter = 0;
229                 if (map == null)
230                         return 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());
236                                 query.add(bean);
237                         }
238                 }
239                 return query;
240         }
241         /*
242          * query jobs log info
243          */
244         public JobBean readJobLog(String jobid) {
245                 if (jobid == null)
246                         return null;
247                 return db.ReadJobLog(jobid);
248         }
249         
250         
251         /*
252          * query jobs by ipStructureJobLog
253          */
254         public UserBean readIp(String ip) {
255                 if (ip == null)
256                         return null;
257                 Map<String, String[]> res = db.ReadIpWithJobs(ip);
258                 if (res == null) 
259                         return null;
260                 UserBean query = new UserBean(ip);
261                 query.setMainInfo(res);
262                 return query;
263         }
264         
265         /*
266          * create list of parts of protein sequence;
267          */
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)
274                                 sub.add(first);
275                         sub.add(subprot);
276                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
277                 }
278                 if (subStr.length() > 0)
279                         sub.add(subStr);
280                 return sub;
281         }
282         /*
283          * convert String date into long date (miliseconds since the epoch start)
284          */
285         private static long DateParsing(String datInput, SimpleDateFormat formatter) {
286                 if (datInput == null) {
287                         return 0;
288                 }
289                 long dateWorkSt = 0;
290                 
291                 try {
292                         dateWorkSt = formatter.parse(datInput).getTime();
293                 } catch (ParseException e) {
294                         e.printStackTrace();
295                 }
296                 return dateWorkSt;
297         }
298
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));
303         }
304
305         /*
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
308          */
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();
315                 }
316         }
317
318         public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
319                 if (dateToValidate == null || dateToValidate.equals("")) {
320                         return false;
321                 }
322                 try {
323                         // if not valid, this will throw ParseException
324                         sdf.setLenient(false);
325                         Date date = sdf.parse(dateToValidate);
326                 } catch (ParseException e) {
327                         e.printStackTrace();
328                         return false;
329                 }
330                 return true;
331         }
332
333         /*
334          * find the earliest date in the database
335          */
336         public long earliestDate() {
337                 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
338                 return earlestDate;
339         }
340         
341 }