Simplify the jobdateinfo table (remove Total, Program, Version)
[proteocache.git] / datadb / compbio / cassandra / CassandraRemover.java
1 package compbio.cassandra;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.List;
8
9 import com.datastax.driver.core.ResultSet;
10 import com.datastax.driver.core.Row;
11 import compbio.cassandra.readers.CassandraReader;
12
13 /**
14  * The class removes jobs from the cassandra database. 4 different strategies
15  * are possiable: 1. remove 1 job with given job ID 2. remove jobs launched from
16  * an IP 3. remove jobs with particular protein sequence 4. remove jobs launched
17  * within a time range (date1, data2)
18  * 
19  * @author Alexander Sherstnev
20  * @author Natasha Sherstneva
21  * @version 1.0
22  * @since Nov 2013
23  */
24 public class CassandraRemover extends CassandraReader {
25         static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
26
27         /**
28          * private method for real deleting one job
29          * 
30          * @param jobid
31          *            job ID
32          * @param date
33          *            job execution date
34          * 
35          * @return nothing
36          */
37         private int RemoveJob(String jobid, long date) {
38
39                 if (date < 0L) {
40                         log.error("CassandraRemover error: job " + jobid + " with date " + date
41                                         + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
42                         return 0;
43                 }
44
45                 String status = FindStatus(jobid);
46                 String com1 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
47                 System.out.println("Command: " + com1);
48                 CassandraQuery(com1);
49
50                 String com2 = "UPDATE jpredarchive SET finalstatus = 'DELETED'  WHERE JobID = '" + jobid + "' ;";
51                 System.out.println("Command: " + com2);
52                 CassandraQuery(com2);
53
54                 String com3 = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
55                 System.out.println("Command: " + com3);
56                 ResultSet results = CassandraQuery(com3);
57                 if (results.isExhausted()) {
58                         log.error("CassandraRemover error: job " + jobid + " with date " + date
59                                         + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
60                         return 0;
61                 }
62                 Row row = results.one();
63                 if (status.equals("OK")) {
64                         long njobsOK = row.getLong("TotalOK") - 1;
65                         String com4 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
66                         System.out.println("Command: " + com4);
67                         CassandraQuery(com4);
68
69                         String com5 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
70                         System.out.println("Command: " + com5);
71                         CassandraQuery(com5);
72                         UpdateJobDateInfo(date, "TotalOK", njobsOK);
73                 } else {
74                         String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
75                         System.out.println("Command: " + com6);
76                         CassandraQuery(com6);
77                         if (status.equals("STOPPED")) {
78                                 long njobsStopped = row.getLong("TotalStopped") - 1;
79                                 UpdateJobDateInfo(date, "TotalStopped", njobsStopped);
80                         } else if (status.equals("ERROR")) {
81                                 long njobsError = row.getLong("TotalError") - 1;
82                                 UpdateJobDateInfo(date, "TotalError", njobsError);
83                         } else if (status.equals("TIMEDOUT")) {
84                                 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
85                                 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut);
86                         }
87                 }
88                 System.out.println("Job " + jobid + " removed...");
89                 return 1;
90         }
91
92         /**
93          * update a pariticular column in the JobDateInfo table
94          * 
95          * @param jobid
96          *            job ID
97          * 
98          * @return nothing
99          * 
100          */
101         private void UpdateJobDateInfo(long date, String ColumnName, long totalcol) {
102                 String com = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + " WHERE jobday = " + date + ";";
103                 CassandraQuery(com);
104         }
105
106         /**
107          * external method for deleting job with given job ID (strategy 1)
108          * 
109          * @param jobid
110          *            job ID
111          * 
112          * @return a number of deleted jobs
113          * 
114          */
115         public int RemoveJobById(String jobid) {
116                 if (jobid == null)
117                         return 0;
118                 long date = FindJobDate(jobid);
119                 return RemoveJob(jobid, date);
120         }
121
122         /**
123          * external method for deleting jobs within a time range (strategy 4)
124          * 
125          * @param date1
126          *            starting date
127          * 
128          * @param date2
129          *            ending date
130          * 
131          * @return a number of deleted jobs
132          * 
133          */
134         public int RemoveJobByDate(String date1, String date2) {
135                 if (date1 == null || date2 == null)
136                         return 0;
137
138                 int njobs = 0;
139                 Long dateBegin = convertDate(date1);
140                 Long dateEnd = convertDate(date2);
141                 Calendar start = Calendar.getInstance();
142                 start.setTime(new Date(dateBegin));
143                 Calendar end = Calendar.getInstance();
144                 end.setTime(new Date(dateEnd));
145
146                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
147                         String com1 = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
148                         System.out.println("Command: " + com1);
149                         ResultSet results = CassandraQuery(com1);
150                         if (!results.isExhausted()) {
151                                 List<Row> rows = results.all();
152                                 for (Row r : rows) {
153                                         String jobid = r.getString("JobID");
154                                         if (jobid != null) {
155                                                 njobs += RemoveJob(jobid, date.getTime());
156                                         }
157                                 }
158                         }
159
160                         String com2 = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
161                         ResultSet resultsfail = CassandraQuery(com2);
162                         if (!resultsfail.isExhausted()) {
163                                 List<Row> rows = resultsfail.all();
164                                 for (Row r : rows) {
165                                         String jobid = r.getString("JobID");
166                                         if (jobid != null) {
167                                                 njobs += RemoveJob(jobid, date.getTime());
168                                         }
169                                 }
170                         }
171                 }
172                 return njobs;
173         }
174
175         /**
176          * external method for deleting jobs launched from a particular IP (strategy
177          * 2)
178          * 
179          * @param ip
180          *            the IP
181          * 
182          * @return a number of deleted jobs
183          * 
184          */
185         public int RemoveJobByIp(String ip) {
186                 int njobs = 0;
187                 if (ip == null)
188                         return 0;
189                 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
190                 ResultSet results = CassandraQuery(com);
191                 if (!results.isExhausted()) {
192                         List<Row> rows = results.all();
193                         for (Row r : rows) {
194                                 Long date = convertDate(r.getString("databegin"));
195                                 String jobid = r.getString("JobID");
196                                 if (date != null || jobid != null) {
197                                         njobs += RemoveJob(jobid, date);
198                                 }
199                         }
200                 }
201                 return njobs;
202         }
203
204         /**
205          * external method for deleting jobs with a protein sequence (strategy 3)
206          * 
207          * @param sequence
208          *            the sequence
209          * 
210          * @return a number of deleted jobs
211          * 
212          */
213         public int RemoveJobBySequence(String sequence) {
214                 int njobs = 0;
215                 if (sequence == null)
216                         return 0;
217                 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + sequence + "';";
218                 ResultSet results = CassandraQuery(com);
219                 if (!results.isExhausted()) {
220                         List<Row> rows = results.all();
221                         for (Row r : rows) {
222                                 String jobid = r.getString("JobID");
223                                 long date = FindJobDate(jobid);
224                                 njobs += RemoveJob(jobid, date);
225                         }
226                 }
227                 return njobs;
228         }
229
230         private long FindJobDate(String jobid) {
231                 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
232                 ResultSet results = CassandraQuery(com);
233                 if (!results.isExhausted()) {
234                         return convertDate(results.one().getString("databegin"));
235                 }
236                 return -1L;
237         }
238
239         private String FindStatus(String jobid) {
240                 String com = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
241                 System.out.println("Command: " + com);
242                 ResultSet results = CassandraQuery(com);
243                 if (!results.isExhausted()) {
244                         return results.one().getString("FinalStatus");
245                 }
246                 return "UNKNOWN";
247         }
248
249         protected long convertDate(String d) {
250                 try {
251                         if (null != d) {
252                                 Date startdate = dateformatter.parse(d);
253                                 return startdate.getTime();
254                         }
255                 } catch (ParseException e) {
256                         e.printStackTrace();
257                 }
258                 return 0L;
259         }
260
261 }