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