X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=server%2Fcompbio%2Fstatistic%2FCassandraRequester.java;h=1791c0fb9288764885047b678dbd5abd1b559d15;hb=75431c815ea42cf3ffa18f485721146636575265;hp=53350237a8b91c43b5b1b2a50101d07ba1a16f64;hpb=e371943c7ba7e4d2efe3c11bca6ed9ad1a94632b;p=proteocache.git diff --git a/server/compbio/statistic/CassandraRequester.java b/server/compbio/statistic/CassandraRequester.java index 5335023..1791c0f 100755 --- a/server/compbio/statistic/CassandraRequester.java +++ b/server/compbio/statistic/CassandraRequester.java @@ -5,20 +5,30 @@ 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.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.CassandraReaderOld; import compbio.cassandra.DataBase; -import compbio.cassandra.Pair; -import compbio.cassandra.StructureProteinPrediction; +import compbio.engine.JobStatus; +import compbio.engine.Pair; public class CassandraRequester { - private CassandraNativeConnector DBInstance = new CassandraNativeConnector(); + private CassandraReaderOld db = new CassandraReaderOld(); private ArrayList query; private static long currentDate = 0; private static long earlestDate = 0; + private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd"); + private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy"); /* * query: execution time for the period from date1 till date2 @@ -30,14 +40,14 @@ public class CassandraRequester { if (null == date2) { date1 = "2100/1/1"; } - if (!isThisDateValid(date1) || !isThisDateValid(date2)) { - 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(); int nbins = 5; - long dateStart = DateParsing(date1); - long dateEnd = DateParsing(date2); + long dateStart = DateParsing(date1, formatYYMMDD); + long dateEnd = DateParsing(date2, formatYYMMDD); if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd) return null; if (dateStart < earlestDate) @@ -53,7 +63,7 @@ public class CassandraRequester { List totalTime = new ArrayList(); for (int i = 0; i < nbins; i++) totalTime.add(i, 0); - List> res = DBInstance.ReadProteinDataTable(); + List> res = db.ReadProteinDataTable(); List> numres = new ArrayList>(); for (Pair entry : res) { @@ -102,93 +112,178 @@ public class CassandraRequester { query.add(db); } - DataBase db = new DataBase(); - db.setTimeTotalExec(totalTime); - query.add(db); - System.out.println("StatisticsProt.readLength: total number of dates = " + query.size()); + /* + * ???? Very strange code... DataBase db = new DataBase(); + * db.setTimeTotalExec(totalTime); query.add(db); + */ return query; } - + /* - * query: total number of jobs for the period from date1 till date2 + * query: total number of jobs for the period from date1 till date2 */ - public List countJobs(String date1, String date2) { - if (null == date1) { - date1 = "1970/1/1"; - } - if (null == date2) { - date1 = "2100/1/1"; - } - if (!isThisDateValid(date1) || !isThisDateValid(date2)) { - System.out.println("Wrong date: point 3"); - return null; - } + 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( + * "CassandraRequester.countJobs: wrong format for date1 " + date1 + + * "or date2 " + date2); return null; } + */ SetDateRange(); - long dateStart = DateParsing(date1); - long dateEnd = DateParsing(date2); - if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd) - return null; - if (dateStart < earlestDate) - dateStart = earlestDate; - if (dateEnd > currentDate) - dateStart = currentDate; - + long dateStart = DateParsing(date1, formatYYMMDD); + long dateEnd = DateParsing(date2, formatYYMMDD); + /* + * if (dateEnd < earlestDate || dateStart > currentDate || dateStart > + * dateEnd) return null; if (dateStart < earlestDate) dateStart = + * earlestDate; if (dateEnd > currentDate) dateStart = currentDate; + */ Calendar start = Calendar.getInstance(); start.setTime(new Date(dateStart)); Calendar end = Calendar.getInstance(); end.setTime(new Date(dateEnd)); - query = new ArrayList(); + 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()) { - Integer res = DBInstance.ReadDateTable(date.getTime()); + Total res = db.ReadDateTable(date.getTime()); if (res == null) continue; - DataBase db = new DataBase(); - db.setTotal(res); - db.setDate(DateFormat(date.getTime())); - query.add(db); - } - System.out.println("StatisticsProt.readLength: total number of dates = " + query.size()); + 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()); + } + query.setWholeTotal(wholeTotal); return query; } - /* + + /* + * 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; + } + SetDateRange(); + long day = DateParsing(date, formatDDMMYY); + if (day < earlestDate || day > currentDate) + return null; + + if (status == JobStatus.OK) { + return db.ReadProteinData(day, date); + } + return db.ReadFailedJobs(day, date, status); + } + + /* * query: protein sequence - * */ - public List readProteins(String protIn, String flag) { - query = new ArrayList(); - List res; - if (flag.equals("whole")) - res = DBInstance.ReadWholeSequence(protIn); - else - res = DBInstance.ReadPartOfSequence(protIn); - for (StructureProteinPrediction entry : res) { - Map pred = entry.getPrediction(); - Iterator it = pred.entrySet().iterator(); - while (it.hasNext()) { - DataBase db = new DataBase(); - db.setProt(entry.getSequence()); - Map.Entry pairs = (Map.Entry)it.next(); - db.setId(entry.getJobid()); - db.setJpred(pairs.getValue().toString()); - if (flag.equals("part")) - db.setSubProt(CreateSubprot (entry.getSequence(), protIn)); - query.add(db); + */ + public List readProteins(String protIn, String searchtype) { + List 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)); + } + } + return result; + } + + /* + * query: protein feature + */ + public Map readProteinsPrediction(String feature, int percent) { + Map result = db.ReadProtein(); + ; + if (result == null) + return null; + Map query = new HashMap(); + for (Map.Entry 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 readProteinByCounter(int counter) { - query = new ArrayList(); - // List> res = DBInstance.ReadProteinDataByCounter(counter); - + + /* + * query protein sequences with number of jobs + */ + public List readProteinByCounter(int minimalcounter) { + List query = new ArrayList(); + Map map = db.ReadProteinSequenceByCounter(); + if (map == null) + return null; + for (Map.Entry 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 ip with number of jobs + */ + public List readIpByCounter(Integer minimalcounter) { + List query = new ArrayList(); + Map map = db.ReadIpByCounter(); + if (minimalcounter == null) + minimalcounter = 0; + if (map == null) + return null; + for (Map.Entry 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 JobBean readJobLog(String jobid) { + if (jobid == null) + return null; + return db.ReadJobLog(jobid); + } + + /* + * query jobs by ipStructureJobLog + */ + public UserBean readIp(String ip) { + if (ip == null) + return null; + Map res = db.ReadIpWithJobs(ip); + if (res == null) + return null; + UserBean query = new UserBean(ip); + query.setMainInfo(res); return query; } - - - private static List CreateSubprot (String protein, String subprot) { + + /* + * create list of parts of protein sequence; + */ + private static List CreateSubprot(String protein, String subprot) { List sub = new ArrayList(); String subStr = protein; while (subStr.length() > 0 && subStr.contains(subprot)) { @@ -202,15 +297,16 @@ public class CassandraRequester { sub.add(subStr); return sub; } + /* * convert String date into long date (miliseconds since the epoch start) */ - private static long DateParsing(String datInput) { + private static long DateParsing(String datInput, SimpleDateFormat formatter) { if (datInput == null) { return 0; } long dateWorkSt = 0; - SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); + try { dateWorkSt = formatter.parse(datInput).getTime(); } catch (ParseException e) { @@ -231,20 +327,18 @@ 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)); + 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) { + public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) { if (dateToValidate == null || dateToValidate.equals("")) { - System.out.println("Undefined date"); return false; } - SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { // if not valid, this will throw ParseException sdf.setLenient(false); @@ -260,8 +354,26 @@ public class CassandraRequester { * find the earliest date in the database */ public long earliestDate() { - earlestDate = DBInstance.getEarliestDateInDB(); + earlestDate = CassandraNativeConnector.getEarliestDateInDB(); return earlestDate; } - + + /** + * prepares an example of either job id or IP for the DB + * + * @param exampletype + * defines which example you need (an existing job from the DB - + * jobid, an IP - "ip") + * @return a string representation of the requested example, if the example + * type is not known empty string is returned + */ + public String getExample(String exampletype) { + if (exampletype.equals("jobid")) { + return "jp_NzBOJKo"; + } else if (exampletype.equals("ip")) { + return "127.0.0.1"; + } + return ""; + } + }