Fix problem due to new internal java beans
[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          * query: protein sequence
182          */
183         public List<ProteinBean> readProteins(String protIn, String flag) {
184                 List<ProteinBean> result;
185                 if (flag.equals("whole"))
186                         result = db.ReadWholeSequence(protIn);
187                 else
188                         result = db.ReadPartOfSequence(protIn);
189                 if (result == null)
190                         return null;
191
192                 if (flag.equals("part")) {
193                         for (ProteinBean entry : result) {
194                                 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
195                         }
196                 }
197                 return result;
198         }
199
200         /*
201          * query protein sequences with number of jobs
202          */
203         public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
204                 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
205                 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
206                 if (map == null)
207                         return null;
208                 for (Map.Entry<String, Integer> entry : map.entrySet()) {
209                         if (entry.getValue() > minimalcounter) {
210                                 TotalByCounterBean bean = new TotalByCounterBean();
211                                 bean.setTotaljobs(entry.getValue());
212                                 bean.setName(entry.getKey());
213                                 query.add(bean);
214                         }
215                 }
216                 return query;
217         }
218
219         /*
220          * query ip with number of jobs
221          */
222         public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
223                 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
224                 Map<String, Integer> map = db.ReadIpByCounter();
225                 if (minimalcounter == null)
226                         minimalcounter = 0;
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 jobs log info
242          */
243         public JobBean readJobLog(String jobid) {
244                 if (jobid == null)
245                         return null;
246                 return db.ReadJobLog(jobid);
247         }
248
249         /*
250          * query jobs by ipStructureJobLog
251          */
252         public UserBean readIp(String ip) {
253                 if (ip == null)
254                         return null;
255                 Map<String, String[]> res = db.ReadIpWithJobs(ip);
256                 if (res == null)
257                         return null;
258                 UserBean query = new UserBean(ip);
259                 query.setMainInfo(res);
260                 return query;
261         }
262
263         /*
264          * create list of parts of protein sequence;
265          */
266         private static List<String> CreateSubprot(String protein, String subprot) {
267                 List<String> sub = new ArrayList<String>();
268                 String subStr = protein;
269                 while (subStr.length() > 0 && subStr.contains(subprot)) {
270                         String first = subStr.substring(0, subStr.indexOf(subprot));
271                         if (first.length() > 0)
272                                 sub.add(first);
273                         sub.add(subprot);
274                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
275                 }
276                 if (subStr.length() > 0)
277                         sub.add(subStr);
278                 return sub;
279         }
280
281         /*
282          * convert String date into long date (miliseconds since the epoch start)
283          */
284         private static long DateParsing(String datInput, SimpleDateFormat formatter) {
285                 if (datInput == null) {
286                         return 0;
287                 }
288                 long dateWorkSt = 0;
289
290                 try {
291                         dateWorkSt = formatter.parse(datInput).getTime();
292                 } catch (ParseException e) {
293                         e.printStackTrace();
294                 }
295                 return dateWorkSt;
296         }
297
298         // convert long to date in string format
299         private static String DateFormat(long inDate) {
300                 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
301                 return datformat.format(new Date(inDate));
302         }
303
304         /*
305          * set earlest date and current dates. earlestDate is static and should be
306          * set at the 1st call currentDate should be re-calculated every time
307          */
308         private static void SetDateRange() {
309                 Calendar cal = Calendar.getInstance();
310                 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
311                                 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 }