import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import compbio.cassandra.AnnotatedProteinSequenceBean;
import compbio.cassandra.CassandraNativeConnector;
+import compbio.cassandra.CassandraReader;
import compbio.cassandra.DataBase;
import compbio.cassandra.Pair;
+import compbio.cassandra.StructureJobLog;
public class CassandraRequester {
- private CassandraNativeConnector DBInstance = new CassandraNativeConnector();
+ private CassandraReader db = new CassandraReader();
private ArrayList<DataBase> 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
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)
List<Integer> totalTime = new ArrayList<Integer>();
for (int i = 0; i < nbins; i++)
totalTime.add(i, 0);
- List<Pair<String, String>> res = DBInstance.ReadProteinDataTable();
+ List<Pair<String, String>> res = db.ReadProteinDataTable();
List<Pair<Date, Long>> numres = new ArrayList<Pair<Date, Long>>();
for (Pair<String, String> entry : res) {
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) {
+ 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, 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<DataBase>();
+ for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
+ List<Long> 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);
+ }
+ return query;
+ }
+
+ /*
+ * query: jobs and sequence at date
+ */
+ public List<DataBase> readJobByDay (String date) {
+ if (null == date) {
+ return null;
+ }
+ 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;
+ List<Pair<String, String>> res = db.ReadProteinData(day);
+ if (res == null)
+ return null;
+ query = new ArrayList<DataBase>();
+ for (Pair<String, String> entry : res) {
+ DataBase bean = new DataBase();
+ bean.setDate(date);
+ bean.setId(entry.getElement0());
+ bean.setProt(entry.getElement1());
+ query.add(bean);
+ }
+ return query;
+ }
+
+ /*
+ * query: protein sequence
+ * */
+ public List<DataBase> readProteins(String protIn, String flag) {
+ query = new ArrayList<DataBase>();
+ List<AnnotatedProteinSequenceBean> res;
+ if (flag.equals("whole"))
+ res = db.ReadWholeSequence(protIn);
+ else
+ res = db.ReadPartOfSequence(protIn);
+ if (res == null)
+ 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));
+ }
+ query.add(db);
+ }
+ return query;
+ }
+
+
+ /*
+ * query protein sequences with number of jobs
+ */
+ public List<DataBase> readProteinByCounter(int minimalcounter) {
+ query = new ArrayList<DataBase>();
+ Map<String, Integer> map = db.ReadProteinSequenceByCounter();
+ 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());
+ query.add(bean);
+ }
+ }
+ return query;
+ }
+
+ /*
+ * query ip with number of jobs
+ */
+ public List<DataBase> readIpByCounter(Integer minimalcounter) {
+ query = new ArrayList<DataBase>();
+ 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) {
+ DataBase bean = new DataBase();
+ bean.setTotalId(entry.getValue());
+ bean.setIp(entry.getKey());
+ query.add(bean);
+ }
+ }
+ return query;
+ }
+ /*
+ * query jobs log info
+ */
+ public DataBase readJobLog(String jobid) {
+ if (jobid == null)
+ return null;
+ StructureJobLog res = db.ReadJobLog(jobid);
+ if (res == null)
+ return null;
+ DataBase query = new DataBase();
+ query.setLogInfo(res);
+ return query;
+ }
+
+
+ /*
+ * query jobs by ipStructureJobLog
+ */
+ public List<DataBase> readIp(String ip) {
+ if (ip == null)
+ return null;
+ List<Pair<String, String>> res = db.ReadIpWithJobs(ip);
+ if (res == null)
+ return null;
+ query = new ArrayList<DataBase>();
+ for (Pair<String, String> entry : res) {
+ DataBase bean = new DataBase();
+ bean.setIp(ip);
+ bean.setId(entry.getElement0());
+ bean.setProt(entry.getElement1());
+ query.add(bean);
+ }
+ return query;
+ }
+
+ /*
+ * create list of parts of protein sequence;
+ */
+ 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)) {
+ String first = subStr.substring(0, subStr.indexOf(subprot));
+ if (first.length() > 0)
+ sub.add(first);
+ sub.add(subprot);
+ subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
+ }
+ if (subStr.length() > 0)
+ 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) {
*/
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);
* find the earliest date in the database
*/
public long earliestDate() {
- earlestDate = DBInstance.getEarliestDateInDB();
+ earlestDate = CassandraNativeConnector.getEarliestDateInDB();
return earlestDate;
}
+
}