f20f45774779772b5b0fe7a9bca4dd66a9682d55
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
1 package compbio.cassandra;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6 import org.springframework.dao.DataIntegrityViolationException;
7
8 import com.datastax.driver.core.Row;
9 import com.datastax.driver.core.Session;
10 import com.datastax.driver.core.ResultSet;
11 import com.datastax.driver.core.exceptions.QueryExecutionException;
12 import com.datastax.driver.core.exceptions.QueryValidationException;
13
14 import compbio.data.sequence.FastaSequence;
15 import compbio.engine.JpredJob;
16 import compbio.engine.ProteoCachePropertyHelperManager;
17 import compbio.util.PropertyHelper;
18
19 public class CassandraWriter {
20         private Session session;
21         private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
22         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
23
24         CassandraWriter() {
25                 Session inis = CassandraNativeConnector.getSession();
26                 setSession(inis);
27         }
28
29         public void setSession(Session s) {
30                 assert s != null;
31                 session = s;
32         }
33
34         private ResultSet execute(String command) {
35                 try {
36                         ResultSet results = session.execute(command);
37                         return results;
38                 } catch (QueryExecutionException e) {
39                         String mess = "CassandraWriter: query execution exception...";
40                         System.out.println(mess);
41                         log.error(mess);
42                         log.error(e.getLocalizedMessage(), e.getCause());
43                         return null;
44                 } catch (QueryValidationException e) {
45                         String mess = "CassandraWriter: query validation exception... Command: " + command;
46                         System.out.println(mess);
47                         log.error(mess);
48                         log.error(e.getLocalizedMessage(), e.getCause());
49                         return null;
50                 }
51         }
52
53         public boolean JobisNotInsterted(String jobid) {
54                 ResultSet results = execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
55                 if (null != results && results.isExhausted()) {
56                         return true;
57                 }
58                 return false;
59         }
60
61         public boolean JobisNotArchived(String jobid) {
62                 ResultSet results = execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
63                 if (null != results && results.isExhausted()) {
64                         return true;
65                 }
66                 return false;
67         }
68
69         /*
70          * inserting data into the tables for queries
71          */
72         public int FormQueryTables(JpredJob job) {
73                 if (JobisNotInsterted(job.getJobID())) {
74                         String id = job.getJobID();
75                         String protein = job.getProtein();
76                         String finalstatus = job.getFinalStatus();
77                         String execstatus = job.getExecutionStatus();
78
79                         String com1 = "INSERT INTO ProteinLog (JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein) VALUES ('" + id
80                                         + "','" + job.getIP() + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus + "','"
81                                         + execstatus + "','" + protein + "');";
82                         ResultSet insert = execute(com1);
83                         if (null == insert) {
84                                 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinLog");
85                                 // return 0;
86                         }
87                         if (finalstatus.equals("OK")) {
88                                 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate()
89                                                 + ",'" + id + "'," + job.getExecutionTime() + ",'" + protein + "');";
90                                 if (null == execute(com2)) {
91                                         System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinData");
92                                         // return 0;
93                                 }
94
95                                 String allpredictions = "";
96                                 List<FastaSequence> pr = job.getPredictions();
97                                 for (FastaSequence pred : pr) {
98                                         String predictionname = pred.getId();
99                                         String prediction = pred.getSequence().replaceAll("\n", "");
100                                         allpredictions += "'" + predictionname + "':'" + prediction + "',";
101                                 }
102                                 String final_prediction = "";
103                                 if (!allpredictions.equals("")) {
104                                         final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
105                                 }
106
107                                 ResultSet results2 = execute("SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';");
108                                 if (null != results2 && results2.isExhausted()) {
109                                         String com3 = "INSERT INTO ProteinRow (Protein, JobID, Predictions) VALUES ('" + protein + "','" + id + "',{"
110                                                         + final_prediction + "});";
111                                         if (null == execute(com3)) {
112                                                 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinRow");
113                                                 return 0;
114                                         }
115                                 }
116                         } else {
117                                 String com5 = "INSERT INTO FailLog (jobtime, JobID, ExecTime, ip, FinalStatus) VALUES (" + job.getStartingDate() + ",'"
118                                                 + id + "'," + job.getExecutionTime() + ",'" + job.getIP() + "', '" + finalstatus + "');";
119                                 if (null == execute(com5)) {
120                                         System.out.println("CassandraWriter.FormQueryTables: couldn't insert into FailLog");
121                                         return 0;
122                                 }
123                         }
124
125                         // update Main parameters if the job is the earliest job so far
126                         ResultSet results3 = execute("SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';");
127                         if (null == results3) {
128                                 System.out.println("CassandraWriter.FormQueryTables: couldn't get results from MainParameters");
129                                 // return 0;
130                         }
131                         boolean updateparameter = true;
132                         if (!results3.isExhausted()) {
133                                 Row r = results3.one();
134                                 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
135                                         updateparameter = false;
136                         }
137                         if (updateparameter) {
138                                 String com6 = "INSERT INTO MainParameters (Name, Value) VALUES ('EarliestJobDate','" + job.getStartingDate() + "');";
139                                 if (null == execute(com6)) {
140                                         System.out.println("CassandraWriter.FormQueryTables: couldn't insert into MainParameters");
141                                         return 0;
142                                 }
143                         }
144
145                         // update internal job counts (used by the Daily Statistics
146                         // requests)
147                         // TODO I don't like the bit of code. There should not be so many
148                         // counters...
149                         int njobsTotal = 1;
150                         int njobsOk = 0;
151                         int njobsStop = 0;
152                         int njobsError = 0;
153                         int njobsTimeOut = 0;
154                         if (finalstatus.equals("OK"))
155                                 njobsOk = 1;
156                         else if (finalstatus.equals("TIMEDOUT"))
157                                 njobsTimeOut = 1;
158                         else if (finalstatus.equals("JPREDERROR"))
159                                 njobsError = 1;
160                         else if (finalstatus.equals("STOPPED"))
161                                 njobsStop = 1;
162                         ResultSet results4 = execute("SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";");
163                         if (null == results4) {
164                                 System.out.println("CassandraWriter.FormQueryTables: couldn't get data from JobDateInfo");
165                                 // return 0;
166                         }
167                         if (!results4.isExhausted()) {
168                                 Row r = results4.one();
169                                 njobsTotal += r.getLong("Total");
170                                 njobsOk += r.getLong("TotalOK");
171                                 njobsError += r.getLong("TotalError");
172                                 njobsStop += r.getLong("TotalStopped");
173                                 njobsTimeOut += r.getLong("TotalTimeOut");
174                         }
175                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
176                                         + job.getStartingDate() + "," + njobsTotal + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut
177                                         + ");";
178                         if (null == execute(com)) {
179                                 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into JobDateInfo");
180                                 // return 0;
181                         }
182                         return 1;
183                 }
184                 return 0;
185         }
186
187         /*
188          * insert data from a real Jpred job: timing+IP, Execution Status, Final
189          * status, protein sequence, predictions, alignment, LOG and tar.gz files
190          */
191         public int ArchiveData(JpredJob job, String archivepath) {
192                 if (JobisNotArchived(job.getJobID())) {
193                         String id = job.getJobID();
194                         String log = job.getLog().replaceAll("'", "");
195                         String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('"
196                                         + id
197                                         + "','"
198                                         + job.getProtein()
199                                         + "','"
200                                         + job.getIP()
201                                         + "',"
202                                         + job.getStartingTime()
203                                         + ","
204                                         + job.getExecutionTime()
205                                         + ",'" + job.getFinalStatus() + "','" + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
206                         if (null == execute(com)) {
207                                 System.out.println("CassandraWriter.ArchiveData: couldn't insert into JpredArchive");
208                         }
209                         List<FastaSequence> predictions = job.getPredictions();
210                         for (FastaSequence p : predictions) {
211                                 if (null == execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
212                                                 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';")) {
213                                         System.out.println("CassandraWriter.ArchiveData: couldn't update data in JpredArchive");
214                                 }
215                         }
216
217                         List<FastaSequence> seqs = job.getAlignment();
218
219                         for (FastaSequence s : seqs) {
220                                 String com2 = "UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
221                                                 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';";
222                                 if (null == execute(com2)) {
223                                         System.out.println("CassandraWriter.ArchiveData: couldn't update data in JpredArchive");
224                                 }
225                         }
226                         return 1;
227                 }
228                 return 0;
229         }
230
231 }