76cc78758e9c0ea01d23cd11312dcf28750b6a5e
[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 org.apache.log4j.Logger;
10
11 import com.datastax.driver.core.ResultSet;
12 import com.datastax.driver.core.Row;
13 import com.datastax.driver.core.Session;
14 import com.datastax.driver.core.exceptions.QueryExecutionException;
15 import com.datastax.driver.core.exceptions.QueryValidationException;
16
17 public class CassandraRemover {
18         private Session session;
19         static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
20         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
21
22         public CassandraRemover() {
23                 Session inis = CassandraNativeConnector.getSession();
24                 setSession(inis);
25         }
26
27         public void setSession(Session s) {
28                 assert s != null;
29                 session = s;
30         }
31
32         /*
33          * delete a record from CF for current jobId
34          */
35         private void RemoveJob(String jobid, long date) {
36                 String status = FindStatus(jobid);
37                 String com0 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
38                 System.out.println("Command: " + com0);
39                 session.execute(com0);
40                 String com3 = "UPDATE jpredarchive SET finalstatus = 'DELETED'  WHERE JobID = '" + jobid + "' ;";
41                 System.out.println("Command: " + com3);
42                 session.execute(com3);
43                 String com = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
44                 System.out.println("Command: " + com);
45                 ResultSet results = session.execute(com);
46                 Row row = results.one();
47                 long njobs = row.getLong("Total") - 1;
48                 if (status.equals("OK")) {
49                         long njobsOK = row.getLong("TotalOK") - 1;
50                         String com1 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
51                         System.out.println("Command: " + com1);
52                         session.execute(com1);
53                         String com2 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
54                         System.out.println("Command: " + com2);
55                         session.execute(com2);
56                         UpdateJobDateInfo(date, "TotalOK", njobsOK, njobs);
57                 } else {
58                         String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
59                         System.out.println("Command: " + com6);
60                         session.execute(com6);
61                         if (status.equals("STOPPED")) {
62                                 long njobsStopped = row.getLong("TotalStopped") - 1;
63                                 UpdateJobDateInfo(date, "TotalStopped", njobsStopped, njobs);
64                         } else if (status.equals("ERROR")) {
65                                 long njobsError = row.getLong("TotalError") - 1;
66                                 UpdateJobDateInfo(date, "TotalError", njobsError, njobs);
67                         } else if (status.equals("TIMEDOUT")) {
68                                 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
69                                 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut, njobs);
70                         }
71                 }
72                 System.out.println("Remove jobs: " + jobid);
73         }
74
75         private void UpdateJobDateInfo(long date, String ColumnName, long totalcol, long total) {
76                 String com4 = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + ", Total = " + total + " WHERE jobday = " + date + ";";
77                 System.out.println("Command: " + com4);
78                 session.execute(com4);
79         }
80
81         public int RemoveJobById(String jobid) {
82                 if (jobid == null)
83                         return 0;
84                 Long date = FindDate(jobid);
85                 RemoveJob(jobid, date);
86                 return 1;
87         }
88
89         public int RemoveJobByDate(String date1, String date2) {
90                 int numremover = 0;
91                 if (date1 == null || date2 == null)
92                         return 0;
93                 Long dateBegin = convertDate(date1);
94                 Long dateEnd = convertDate(date2);
95                 Calendar start = Calendar.getInstance();
96                 start.setTime(new Date(dateBegin));
97                 Calendar end = Calendar.getInstance();
98                 end.setTime(new Date(dateEnd));
99                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
100                         System.out.println("--------------------------------------------------------------------: ");
101                         String com = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
102                         System.out.println("Command: " + com);
103                         ResultSet results = session.execute(com);
104                         if (!results.isExhausted()) {
105                                 List<Row> rows = results.all();
106                                 for (Row r : rows) {
107                                         String jobid = r.getString("JobID");
108                                         if (jobid != null) {
109                                                 RemoveJob(jobid, date.getTime());
110                                                 numremover++;
111                                         }
112                                 }
113                         }
114                         String comm = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
115                         ResultSet resultsfail = session.execute(comm);
116                         if (!resultsfail.isExhausted()) {
117                                 List<Row> rows = resultsfail.all();
118                                 for (Row r : rows) {
119                                         String jobid = r.getString("JobID");
120                                         if (jobid != null) {
121                                                 RemoveJob(jobid, date.getTime());
122                                                 numremover++;
123                                         }
124                                 }
125                         }
126                 }
127                 return numremover;
128         }
129
130         public int RemoveJobByIp(String ip) {
131                 int numremover = 0;
132                 if (ip == null)
133                         return 0;
134                 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
135                 ResultSet results = session.execute(com);
136                 if (!results.isExhausted()) {
137                         List<Row> rows = results.all();
138                         for (Row r : rows) {
139                                 Long date = convertDate(r.getString("databegin"));
140                                 String jobid = r.getString("JobID");
141                                 if (date == null || jobid == null)
142                                         continue;
143                                 RemoveJob(jobid, date);
144                                 numremover++;
145                         }
146                 }
147                 return numremover;
148         }
149
150         public int RemoveJobBySequence(String seq) {
151                 int numremover = 0;
152                 if (seq == null)
153                         return 0;
154                 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + seq + "';";
155                 ResultSet results = session.execute(com);
156                 if (!results.isExhausted()) {
157                         List<Row> rows = results.all();
158                         for (Row r : rows) {
159                                 String jobid = r.getString("JobID");
160                                 Long date = FindDate(jobid);
161                                 RemoveJob(jobid, date);
162                                 numremover++;
163                         }
164                 }
165                 return numremover;
166         }
167
168         private Long FindDate(String jobid) {
169                 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
170                 ResultSet results = session.execute(com);
171                 Long date = convertDate(results.one().getString("databegin"));
172                 return date;
173         }
174
175         private String FindStatus(String jobid) {
176                 String status = "UNKNOWN";
177                 String command = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
178                 try {
179                         System.out.println("Command: " + command);
180                         ResultSet results = session.execute(command);
181                         Row raw = results.one();
182                         if (null != raw) {
183                                 status = raw.getString("FinalStatus");
184                         }
185                 } catch (QueryExecutionException e) {
186                         String mess = "CassandraRemover: query execution exception...";
187                         System.out.println(mess);
188                         log.error(mess);
189                         log.error(e.getLocalizedMessage(), e.getCause());
190                 } catch (QueryValidationException e) {
191                         String mess = "CassandraRemover: query validation exception... Command: " + command;
192                         System.out.println(mess);
193                         log.error(mess);
194                         log.error(e.getLocalizedMessage(), e.getCause());
195                 }
196                 System.out.println("*****status: " + status);
197                 return status;
198         }
199
200         protected long convertDate(String d) {
201                 try {
202                         if (null != d) {
203                                 Date startdate = dateformatter.parse(d);
204                                 return startdate.getTime();
205                         }
206                 } catch (ParseException e) {
207                         e.printStackTrace();
208                 }
209                 return 0L;
210         }
211
212 }