1 package compbio.statistic;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
8 import java.util.HashMap;
12 import compbio.cassandra.DateBean;
13 import compbio.cassandra.ProteinBean;
14 import compbio.cassandra.CassandraNativeConnector;
15 import compbio.cassandra.CassandraReaderOld;
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 import compbio.engine.JobStatus;
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");
34 * query: execution time for the period from date1 till date2
36 public List<DataBase> extractExecutionTime(String date1, String date2) {
43 if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
44 System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
49 long dateStart = DateParsing(date1, formatYYMMDD);
50 long dateEnd = DateParsing(date2, formatYYMMDD);
51 if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
53 if (dateStart < earlestDate)
54 dateStart = earlestDate;
55 if (dateEnd > currentDate)
56 dateStart = currentDate;
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++)
66 List<Pair<String, String>> res = db.ReadProteinDataTable();
67 List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
69 for (Pair<String, String> entry : res) {
70 SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
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));
82 } catch (ParseException e) {
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++)
91 for (Pair<Date, Long> p : numres) {
92 if (date.equals(p.getElement0())) {
93 long lenResult = p.getElement1().longValue();
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);
103 timeResult.set(4, timeResult.get(4) + 1);
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()));
115 DataBase db = new DataBase();
116 db.setTimeTotalExec(totalTime);
122 * query: total number of jobs for the period from date1 till date2
124 public TotalJobsStatisticBean countJobs(String date1, String date2) {
126 * if (null == date1) { date1 = "1970/1/1"; } if (null == date2) { date1
127 * = "2100/1/1"; } if (!isThisDateValid(date1, formatYYMMDD) ||
128 * !isThisDateValid(date2, formatYYMMDD)) { System.out.println(
129 * "CassandraRequester.countJobs: wrong format for date1 " + date1 +
130 * "or date2 " + date2); return null; }
133 long dateStart = DateParsing(date1, formatYYMMDD);
134 long dateEnd = DateParsing(date2, formatYYMMDD);
136 * if (dateEnd < earlestDate || dateStart > currentDate || dateStart >
137 * dateEnd) return null; if (dateStart < earlestDate) dateStart =
138 * earlestDate; if (dateEnd > currentDate) dateStart = currentDate;
140 Calendar start = Calendar.getInstance();
141 start.setTime(new Date(dateStart));
142 Calendar end = Calendar.getInstance();
143 end.setTime(new Date(dateEnd));
144 TotalJobsStatisticBean query = new TotalJobsStatisticBean();
145 Total wholeTotal = new Total(0, 0, 0, 0, 0);
146 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
147 Total res = db.ReadDateTable(date.getTime());
150 query.setDateTotal(DateFormat(date.getTime()), res);
151 wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
152 wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
153 wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
154 wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
155 wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
157 query.setWholeTotal(wholeTotal);
162 * query: jobs and sequence at date
164 public DateBean readJobByDay(String date, JobStatus status) {
165 if (!isThisDateValid(date, formatDDMMYY)) {
166 System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
170 long day = DateParsing(date, formatDDMMYY);
171 if (day < earlestDate || day > currentDate)
174 if (status == JobStatus.OK) {
175 return db.ReadProteinData(day, date);
177 return db.ReadFailedJobs(day, date, status);
181 * query: protein sequence
183 public List<ProteinBean> readProteins(String protIn, String searchtype) {
184 List<ProteinBean> result;
185 if (searchtype.equals("whole"))
186 result = db.ReadWholeSequence(protIn);
188 result = db.ReadPartOfSequence(protIn);
192 if (searchtype.equals("partial")) {
193 for (ProteinBean entry : result) {
194 entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
201 * query: protein feature
203 public Map<String, String> readProteinsPrediction(String feature, int percent) {
204 Map<String, String> result = db.ReadProtein();
208 Map<String, String> query = new HashMap<String, String>();
209 for (Map.Entry<String, String> entry : result.entrySet()) {
210 String pred = entry.getValue();
211 if (pred.replaceAll("[^" + feature + "]", "").length() > pred.length() * percent / 100 && (!entry.getKey().equals(""))) {
212 // if (!entry.getKey().equals(""))
213 query.put(entry.getKey(), pred);
220 * query protein sequences with number of jobs
222 public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
223 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
224 Map<String, Integer> map = db.ReadProteinSequenceByCounter();
227 for (Map.Entry<String, Integer> entry : map.entrySet()) {
228 if (entry.getValue() > minimalcounter) {
229 TotalByCounterBean bean = new TotalByCounterBean();
230 bean.setTotaljobs(entry.getValue());
231 bean.setName(entry.getKey());
239 * query ip with number of jobs
241 public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
242 List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
243 Map<String, Integer> map = db.ReadIpByCounter();
244 if (minimalcounter == null)
248 for (Map.Entry<String, Integer> entry : map.entrySet()) {
249 if (entry.getValue() > minimalcounter) {
250 TotalByCounterBean bean = new TotalByCounterBean();
251 bean.setTotaljobs(entry.getValue());
252 bean.setName(entry.getKey());
260 * query jobs log info
262 public JobBean readJobLog(String jobid) {
265 return db.ReadJobLog(jobid);
269 * query jobs by ipStructureJobLog
271 public UserBean readIp(String ip) {
274 Map<String, String[]> res = db.ReadIpWithJobs(ip);
277 UserBean query = new UserBean(ip);
278 query.setMainInfo(res);
283 * create list of parts of protein sequence;
285 private static List<String> CreateSubprot(String protein, String subprot) {
286 List<String> sub = new ArrayList<String>();
287 String subStr = protein;
288 while (subStr.length() > 0 && subStr.contains(subprot)) {
289 String first = subStr.substring(0, subStr.indexOf(subprot));
290 if (first.length() > 0)
293 subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
295 if (subStr.length() > 0)
301 * convert String date into long date (miliseconds since the epoch start)
303 private static long DateParsing(String datInput, SimpleDateFormat formatter) {
304 if (datInput == null) {
310 dateWorkSt = formatter.parse(datInput).getTime();
311 } catch (ParseException e) {
317 // convert long to date in string format
318 private static String DateFormat(long inDate) {
319 SimpleDateFormat datformat = new SimpleDateFormat("dd/MM/yyyy");
320 return datformat.format(new Date(inDate));
324 * set earlest date and current dates. earlestDate is static and should be
325 * set at the 1st call currentDate should be re-calculated every time
327 private static void SetDateRange() {
328 Calendar cal = Calendar.getInstance();
329 currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
331 if (0 == earlestDate) {
332 CassandraRequester cr = new CassandraRequester();
333 earlestDate = cr.earliestDate();
337 public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
338 if (dateToValidate == null || dateToValidate.equals("")) {
342 // if not valid, this will throw ParseException
343 sdf.setLenient(false);
344 Date date = sdf.parse(dateToValidate);
345 } catch (ParseException e) {
353 * find the earliest date in the database
355 public long earliestDate() {
356 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
361 * prepares an example of either job id or IP for the DB
364 * defines which example you need (an existing job from the DB -
365 * jobid, an IP - "ip")
366 * @return a string representation of the requested example, if the example
367 * type is not known empty string is returned
369 public String getExample(String exampletype) {
370 if (exampletype.equals("jobid")) {
372 } else if (exampletype.equals("ip")) {