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