521f98ca9d724de9c30eee75ee0fb793a61c4a9c
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
1 package compbio.cassandra;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.List;
7
8 import org.apache.log4j.Logger;
9
10 import com.datastax.driver.core.Row;
11 import com.datastax.driver.core.Session;
12 import com.datastax.driver.core.ResultSet;
13
14 import compbio.engine.JpredJob;
15 import compbio.engine.ProteoCachePropertyHelperManager;
16 import compbio.util.PropertyHelper;
17
18 public class CassandraWriter {
19         private Session session;
20         private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
21         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
22
23         CassandraWriter() {
24                 Session inis = CassandraNativeConnector.getSession();
25                 setSession(inis);
26         }
27
28         public void setSession(Session s) {
29                 assert s != null;
30                 session = s;
31         }
32
33         public boolean JobisNotInsterted(String jobid) {
34                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
35                 if (results1.isExhausted()) {
36                         return true;
37                 }
38                 return false;
39         }
40
41         public boolean JobisNotArchived(String jobid) {
42                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
43                 if (results1.isExhausted()) {
44                         return true;
45                 }
46                 return false;
47         }
48
49         /*
50          * inserting data into the tables for queries
51          */
52         public int FormQueryTables(JpredJob job) {
53                 if (JobisNotInsterted(job.getJobID())) {
54                         int njobsOk = 0;
55                         int njobsStop = 0;
56                         int njobsError = 0;
57                         int njobsTimeOut = 0;
58                         String id = job.getJobID();
59                         String ip = job.getIP();
60                         String protein = job.getProtein();
61                         String finalstatus = job.getFinalStatus();
62                         String execstatus = job.getExecutionStatus();
63                         String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
64                                         + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
65                                         + "','" + execstatus + "','" + protein + "');";
66                         session.execute(com1);
67                         if (execstatus.equals("OK")) {
68                                 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
69                                                 + "'," + job.getExecutionTime() + ",'" + protein + "');";
70                                 session.execute(com2);
71                                 
72                                 String allpredictions = "";
73                                 List<FastaSequence> pr = job.getPredictions();
74                                 for (FastaSequence pred : pr) {
75                                         String predictionname = pred.getId();
76                                         String prediction = pred.getSequence().replaceAll("\n", "");
77                                         allpredictions += "'" + predictionname + "':'" + prediction + "',";
78                                 }
79                                 String final_prediction = "";
80                                 if (!allpredictions.equals("")) {
81                                         final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
82                                 }
83
84                                 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
85                                 ResultSet results2 = session.execute(check2);
86                                 if (results2.isExhausted()) {
87                                         String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
88                                                         + final_prediction + "});";
89                                         session.execute(com3);
90                                 }
91                                 njobsOk = 1;
92                         } else {
93                                 String com5 = "INSERT INTO FailLog " + "(jobtime, JobID, ExecTime, ip, FinalStatus)" + " VALUES ("
94                                                 + job.getStartingDate() + ",'" + id
95                                                 + "'," + job.getExecutionTime() + ",'" + ip + "', '" +  finalstatus + "');";
96                                 session.execute(com5);
97                                 System.out.println(com5);
98                                 if (finalstatus.equals("TIMEDOUT"))
99                                         njobsTimeOut = 1;
100                                 else if (finalstatus.equals("JPREDERROR"))
101                                         njobsError = 1;
102                                 else if (finalstatus.equals("STOPPED"))
103                                         njobsStop = 1;
104                         }
105                         // update some internal query tables
106                         String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
107                         ResultSet results3 = session.execute(check3);
108                         boolean updateparameter = true;
109                         if (!results3.isExhausted()) {
110                                 Row r = results3.one();         
111                                 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
112                                         updateparameter = false;
113                         }
114                         if (updateparameter) {
115                                 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDate()
116                                                 + "');";
117                                 session.execute(com);
118                         }
119                         String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
120                         ResultSet results4 = session.execute(check4);
121                         updateparameter = true;
122                         int njobs = 1;
123                         if (!results4.isExhausted()) {
124                                 Row r = results4.one();
125                                 njobs += r.getLong("Total");
126                                 njobsOk += r.getLong("TotalOK");
127                                 njobsError += r.getLong("TotalError");
128                                 njobsStop += r.getLong("TotalStopped");
129                                 njobsTimeOut += r.getLong("TotalTimeOut");
130                         }
131                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
132                                         + job.getStartingDate() + "," + njobs + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut + ");";
133                         System.out.println(com);
134                         session.execute(com);
135                         return 1;
136                 }
137                 return 0;
138         }
139
140         /*
141          * insert data from a real Jpred job: timing+IP, Execution Status, Final
142          * status, protein sequence, predictions, alignment, LOG and tar.gz files
143          */
144         public int ArchiveData(JpredJob job, String archivepath) {
145                 if (JobisNotArchived(job.getJobID())) {
146                         String id = job.getJobID();
147                         String log = job.getLog().replaceAll("'", "");
148                         String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('" + id + "','"
149                                         + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + job.getFinalStatus() + "','"
150                                         + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
151                         session.execute(com);
152                         List<FastaSequence> predictions = job.getPredictions();
153                         for (FastaSequence p : predictions) {
154                                 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
155                                                 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
156                         }
157
158                         List<FastaSequence> seqs = job.getAlignment();
159                         for (FastaSequence s : seqs) {
160                                 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
161                                                 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
162                         }
163                         return 1;
164                 }
165                 return 0;
166         }
167
168 }