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