new readers for the queries
[proteocache.git] / server / compbio / statistic / CassandraRequester.java
index 410a722..0d2ffc0 100755 (executable)
@@ -5,19 +5,25 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
-import java.util.Iterator;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import compbio.cassandra.AnnotatedProteinSequenceBean;
+import compbio.beans.DateBean;
+import compbio.beans.JobBean;
+import compbio.beans.ProteinBean;
+import compbio.beans.Total;
+import compbio.beans.TotalByCounterBean;
+import compbio.beans.TotalJobsStatisticBean;
+import compbio.beans.UserBean;
 import compbio.cassandra.CassandraNativeConnector;
-import compbio.cassandra.CassandraReader;
+import compbio.cassandra.CassandraReaderOld;
 import compbio.cassandra.DataBase;
 import compbio.cassandra.Pair;
-import compbio.cassandra.StructureJobLog;
+import compbio.engine.JobStatus;
 
 public class CassandraRequester {
-       private CassandraReader db = new CassandraReader();
+       private CassandraReaderOld db = new CassandraReaderOld();
        private ArrayList<DataBase> query;
        private static long currentDate = 0;
        private static long earlestDate = 0;
@@ -34,8 +40,8 @@ public class CassandraRequester {
                if (null == date2) {
                        date1 = "2100/1/1";
                }
-               if (!isThisDateValid(date1,formatYYMMDD) || !isThisDateValid(date2,formatYYMMDD)) {
-                       System.out.println("Wrong date: point 3");
+               if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
+                       System.out.println("CassandraRequester.extractExecutionTime: wrong format for date1 " + date1 + "or date2 " + date2);
                        return null;
                }
                SetDateRange();
@@ -109,119 +115,177 @@ public class CassandraRequester {
                DataBase db = new DataBase();
                db.setTimeTotalExec(totalTime);
                query.add(db);
-               System.out.println("StatisticsProt.readLength: total number of dates = " + query.size());
                return query;
        }
-       
+
        /*
-        * query: total number of jobs  for the period from date1 till date2
-        */
-       public List<DataBase> countJobs(String date1, String date2) {
-               if (null == date1) {
+        * query: total number of jobs for the period from date1 till date2
+       */ 
+       public TotalJobsStatisticBean countJobs(String date1, String date2) {
+       /*      if (null == date1) {
                        date1 = "1970/1/1";
                }
                if (null == date2) {
                        date1 = "2100/1/1";
                }
                if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
-                       System.out.println("Wrong date: point 3");
+                       System.out.println("CassandraRequester.countJobs: wrong format for date1 " + date1 + "or date2 " + date2);
                        return null;
-               }
+               }*/
                SetDateRange();
                long dateStart = DateParsing(date1, formatYYMMDD);
                long dateEnd = DateParsing(date2, formatYYMMDD);
-               if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
+/*             if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
                        return null;
                if (dateStart < earlestDate)
                        dateStart = earlestDate;
                if (dateEnd > currentDate)
-                       dateStart = currentDate;
-
+                       dateStart = currentDate;*/
                Calendar start = Calendar.getInstance();
                start.setTime(new Date(dateStart));
                Calendar end = Calendar.getInstance();
                end.setTime(new Date(dateEnd));
-               query = new ArrayList<DataBase>();
+               TotalJobsStatisticBean query = new TotalJobsStatisticBean();
+               Total wholeTotal = new Total(0, 0, 0, 0, 0);
                for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
-                       List<Long> res = db.ReadDateTable(date.getTime());
+                       Total res = db.ReadDateTable(date.getTime());
                        if (res == null)
                                continue;
-                       DataBase bean = new DataBase();
-                       bean.setTotal((int)(long)res.get(0));
-                       bean.setTotalOK((int)(long)res.get(1));
-                       bean.setTotalStopped((int)(long)res.get(2));
-                       bean.setTotalError((int)(long)res.get(3));
-                       bean.setTotalTimeOut((int)(long)res.get(4));
-                       bean.setDate(DateFormat(date.getTime()));
-                       query.add(bean);
+                       query.setDateTotal(DateFormat(date.getTime()), res);
+                       wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
+                       wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
+                       wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
+                       wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
+                       wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
                }
-               System.out.println("StatisticsProt.readLength: total number of dates = " + query.size());
+               query.setWholeTotal(wholeTotal);
                return query;
        }
 
-       /* 
-        * query: protein sequence
-        * */
-       public List<DataBase> readProteins(String protIn, String flag) {
-               if (protIn == null)
+       /*
+        * query: jobs and sequence at date
+        */
+       public DateBean readJobByDay(String date, JobStatus status) {
+               if (!isThisDateValid(date, formatDDMMYY)) {
+                       System.out.println("CassandraRequester.readJobByDay: Wrong date format for " + date);
                        return null;
-               System.out.println(protIn.length());
-               query = new ArrayList<DataBase>();
-               List<AnnotatedProteinSequenceBean> res;
-               if (flag.equals("whole")) 
-                       res = db.ReadWholeSequence(protIn);
-                else 
-                        res = (protIn.length() > 0) ? db.ReadPartOfSequence(protIn) : null;
-               if (res == null)
+               }
+               SetDateRange();
+               long day = DateParsing(date, formatDDMMYY);
+               if (day < earlestDate || day > currentDate)
                        return null;
-               for (AnnotatedProteinSequenceBean entry : res) {
-                       DataBase db = new DataBase();
-                       db.setProt(entry.getSequence());
-                       db.setPredictions(entry);
-                       db.setId(entry.getJobid());
-                       if (flag.equals("part")) {
-                               db.setSubProt(CreateSubprot (entry.getSequence(), protIn));
+               
+               if (status == JobStatus.OK) {
+                       return db.ReadProteinData(day, date);
+               }
+               return db.ReadFailedJobs(day, date, status);
+       }
+
+       /*
+        * query: protein sequence
+        */
+       public List<ProteinBean> readProteins(String protIn, String searchtype) {
+               List<ProteinBean> result;
+               if (searchtype.equals("whole"))
+                       result = db.ReadWholeSequence(protIn);
+               else
+                       result = db.ReadPartOfSequence(protIn);
+               if (result == null)
+                       return null;
+
+               if (searchtype.equals("partial")) {
+                       for (ProteinBean entry : result) {
+                               entry.setSubProt(CreateSubprot(entry.getSequence(), protIn));
                        }
-                       query.add(db);
+               }
+               return result;
+       }
+       
+       /*
+        * query: protein feature
+        */
+       public Map<String, String> readProteinsPrediction(String feature, int percent) {
+               Map<String, String> result = db.ReadProtein();;
+               if (result == null)
+                       return null;
+               Map<String, String> query = new HashMap<String, String>();
+               for (Map.Entry<String, String> entry : result.entrySet()) {
+                       String pred = entry.getValue();                 
+                       if (pred.replaceAll("[^"+feature+"]", "").length() > pred.length() * percent / 100 && (!entry.getKey().equals(""))) {
+                       //      if (!entry.getKey().equals(""))
+                                       query.put(entry.getKey(), pred);
+                       }       
                }
                return query;
        }
 
-       /* 
+       /*
         * query protein sequences with number of jobs
         */
-       public List<DataBase> readProteinByCounter(int minimalcounter) {
-               query = new ArrayList<DataBase>();
+       public List<TotalByCounterBean> readProteinByCounter(int minimalcounter) {
+               List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
                Map<String, Integer> map = db.ReadProteinSequenceByCounter();
+               if (map == null)
+                       return null;
                for (Map.Entry<String, Integer> entry : map.entrySet()) {
-                       if (entry.getValue() > minimalcounter && entry.getKey().length() > 0) {
-                               DataBase bean = new DataBase();
-                               bean.setTotalId(entry.getValue());
-                               bean.setProt(entry.getKey());
+                       if (entry.getValue() > minimalcounter) {
+                               TotalByCounterBean bean = new TotalByCounterBean();
+                               bean.setTotaljobs(entry.getValue());
+                               bean.setName(entry.getKey());
                                query.add(bean);
                        }
                }
                return query;
        }
-       
+
+       /*
+        * query ip with number of jobs
+        */
+       public List<TotalByCounterBean> readIpByCounter(Integer minimalcounter) {
+               List<TotalByCounterBean> query = new ArrayList<TotalByCounterBean>();
+               Map<String, Integer> map = db.ReadIpByCounter();
+               if (minimalcounter == null)
+                       minimalcounter = 0;
+               if (map == null)
+                       return null;
+               for (Map.Entry<String, Integer> entry : map.entrySet()) {
+                       if (entry.getValue() > minimalcounter) {
+                               TotalByCounterBean bean = new TotalByCounterBean();
+                               bean.setTotaljobs(entry.getValue());
+                               bean.setName(entry.getKey());
+                               query.add(bean);
+                       }
+               }
+               return query;
+       }
+
        /*
         * query jobs log info
         */
-       public DataBase readJobLog(String jobid) {
+       public JobBean readJobLog(String jobid) {
                if (jobid == null)
                        return null;
-               StructureJobLog res = db.ReadJobLog(jobid);
+               return db.ReadJobLog(jobid);
+       }
+
+       /*
+        * query jobs by ipStructureJobLog
+        */
+       public UserBean readIp(String ip) {
+               if (ip == null)
+                       return null;
+               Map<String, String[]> res = db.ReadIpWithJobs(ip);
                if (res == null)
                        return null;
-               DataBase query = new DataBase();
-               query.setLogInfo(res);
+               UserBean query = new UserBean(ip);
+               query.setMainInfo(res);
                return query;
        }
-       
+
        /*
         * create list of parts of protein sequence;
         */
-       private static List<String> CreateSubprot (String protein, String subprot) {
+       private static List<String> CreateSubprot(String protein, String subprot) {
                List<String> sub = new ArrayList<String>();
                String subStr = protein;
                while (subStr.length() > 0 && subStr.contains(subprot)) {
@@ -235,6 +299,7 @@ public class CassandraRequester {
                        sub.add(subStr);
                return sub;
        }
+
        /*
         * convert String date into long date (miliseconds since the epoch start)
         */
@@ -243,7 +308,7 @@ public class CassandraRequester {
                        return 0;
                }
                long dateWorkSt = 0;
-               
+
                try {
                        dateWorkSt = formatter.parse(datInput).getTime();
                } catch (ParseException e) {
@@ -264,17 +329,16 @@ public class CassandraRequester {
         */
        private static void SetDateRange() {
                Calendar cal = Calendar.getInstance();
-               currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH), formatYYMMDD);
+               currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH),
+                               formatYYMMDD);
                if (0 == earlestDate) {
                        CassandraRequester cr = new CassandraRequester();
                        earlestDate = cr.earliestDate();
-                       System.out.println("Set earlest Date = " + earlestDate);
                }
        }
 
        public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
                if (dateToValidate == null || dateToValidate.equals("")) {
-                       System.out.println("Undefined date");
                        return false;
                }
                try {
@@ -295,5 +359,5 @@ public class CassandraRequester {
                earlestDate = CassandraNativeConnector.getEarliestDateInDB();
                return earlestDate;
        }
-       
+
 }