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