View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rundeck.api.domain;
17  
18  import java.io.Serializable;
19  import java.util.Date;
20  import java.util.concurrent.TimeUnit;
21  import org.apache.commons.lang.time.DurationFormatUtils;
22  
23  /**
24   * Represents a RunDeck event
25   * 
26   * @author Vincent Behar
27   */
28  public class RundeckEvent implements Serializable {
29  
30      private static final long serialVersionUID = 1L;
31  
32      private String title;
33  
34      private EventStatus status;
35  
36      private String summary;
37  
38      private NodeSummary nodeSummary;
39  
40      private String user;
41  
42      private String project;
43  
44      private Date startedAt;
45  
46      private Date endedAt;
47  
48      /** only if the execution was aborted */
49      private String abortedBy;
50  
51      /** only if associated with an execution */
52      private Long executionId;
53  
54      /** only if associated with a job */
55      private String jobId;
56  
57      /**
58       * @return the duration of the event in milliseconds (or null if the dates are invalid)
59       */
60      public Long getDurationInMillis() {
61          if (startedAt == null || endedAt == null) {
62              return null;
63          }
64          return endedAt.getTime() - startedAt.getTime();
65      }
66  
67      /**
68       * @return the duration of the event in seconds (or null if the dates are invalid)
69       */
70      public Long getDurationInSeconds() {
71          Long durationInMillis = getDurationInMillis();
72          return durationInMillis != null ? TimeUnit.MILLISECONDS.toSeconds(durationInMillis) : null;
73      }
74  
75      /**
76       * @return the duration of the event, as a human-readable string : "3 minutes 34 seconds" (or null if the dates are
77       *         invalid)
78       */
79      public String getDuration() {
80          Long durationInMillis = getDurationInMillis();
81          return durationInMillis != null ? DurationFormatUtils.formatDurationWords(durationInMillis, true, true) : null;
82      }
83  
84      /**
85       * @return the duration of the event, as a "short" human-readable string : "0:03:34.187" (or null if the dates are
86       *         invalid)
87       */
88      public String getShortDuration() {
89          Long durationInMillis = getDurationInMillis();
90          return durationInMillis != null ? DurationFormatUtils.formatDurationHMS(durationInMillis) : null;
91      }
92  
93      /**
94       * @return true if this event is for an ad-hoc command or script, false otherwise (for a job)
95       */
96      public boolean isAdhoc() {
97          return "adhoc".equals(title);
98      }
99  
100     public String getTitle() {
101         return title;
102     }
103 
104     public void setTitle(String title) {
105         this.title = title;
106     }
107 
108     /**
109      * @return the status of the event - see {@link EventStatus}
110      */
111     public EventStatus getStatus() {
112         return status;
113     }
114 
115     public void setStatus(EventStatus status) {
116         this.status = status;
117     }
118 
119     public String getSummary() {
120         return summary;
121     }
122 
123     public void setSummary(String summary) {
124         this.summary = summary;
125     }
126 
127     /**
128      * @return the node summary - see {@link NodeSummary}
129      */
130     public NodeSummary getNodeSummary() {
131         return nodeSummary;
132     }
133 
134     public void setNodeSummary(NodeSummary nodeSummary) {
135         this.nodeSummary = nodeSummary;
136     }
137 
138     public String getUser() {
139         return user;
140     }
141 
142     public void setUser(String user) {
143         this.user = user;
144     }
145 
146     public String getProject() {
147         return project;
148     }
149 
150     public void setProject(String project) {
151         this.project = project;
152     }
153 
154     public Date getStartedAt() {
155         return (startedAt != null) ? new Date(startedAt.getTime()) : null;
156     }
157 
158     public void setStartedAt(Date startedAt) {
159         this.startedAt = ((startedAt != null) ? new Date(startedAt.getTime()) : null);
160     }
161 
162     public Date getEndedAt() {
163         return (endedAt != null) ? new Date(endedAt.getTime()) : null;
164     }
165 
166     public void setEndedAt(Date endedAt) {
167         this.endedAt = ((endedAt != null) ? new Date(endedAt.getTime()) : null);
168     }
169 
170     public String getAbortedBy() {
171         return abortedBy;
172     }
173 
174     public void setAbortedBy(String abortedBy) {
175         this.abortedBy = abortedBy;
176     }
177 
178     /**
179      * @return the ID of the execution associated with this event, or null if there is not
180      */
181     public Long getExecutionId() {
182         return executionId;
183     }
184 
185     public void setExecutionId(Long executionId) {
186         this.executionId = executionId;
187     }
188 
189     /**
190      * @return the ID of the job associated with this event, or null in the case of an ad-hoc command or script
191      */
192     public String getJobId() {
193         return jobId;
194     }
195 
196     public void setJobId(String jobId) {
197         this.jobId = jobId;
198     }
199 
200     @Override
201     public String toString() {
202         return "RundeckEvent [abortedBy=" + abortedBy + ", endedAt=" + endedAt + ", executionId=" + executionId
203                + ", jobId=" + jobId + ", nodeSummary=" + nodeSummary + ", project=" + project + ", startedAt="
204                + startedAt + ", status=" + status + ", summary=" + summary + ", title=" + title + ", user=" + user
205                + "]";
206     }
207 
208     @Override
209     public int hashCode() {
210         final int prime = 31;
211         int result = 1;
212         result = prime * result + ((abortedBy == null) ? 0 : abortedBy.hashCode());
213         result = prime * result + ((endedAt == null) ? 0 : endedAt.hashCode());
214         result = prime * result + ((executionId == null) ? 0 : executionId.hashCode());
215         result = prime * result + ((jobId == null) ? 0 : jobId.hashCode());
216         result = prime * result + ((nodeSummary == null) ? 0 : nodeSummary.hashCode());
217         result = prime * result + ((project == null) ? 0 : project.hashCode());
218         result = prime * result + ((startedAt == null) ? 0 : startedAt.hashCode());
219         result = prime * result + ((status == null) ? 0 : status.hashCode());
220         result = prime * result + ((summary == null) ? 0 : summary.hashCode());
221         result = prime * result + ((title == null) ? 0 : title.hashCode());
222         result = prime * result + ((user == null) ? 0 : user.hashCode());
223         return result;
224     }
225 
226     @Override
227     public boolean equals(Object obj) {
228         if (this == obj)
229             return true;
230         if (obj == null)
231             return false;
232         if (getClass() != obj.getClass())
233             return false;
234         RundeckEvent other = (RundeckEvent) obj;
235         if (abortedBy == null) {
236             if (other.abortedBy != null)
237                 return false;
238         } else if (!abortedBy.equals(other.abortedBy))
239             return false;
240         if (endedAt == null) {
241             if (other.endedAt != null)
242                 return false;
243         } else if (!endedAt.equals(other.endedAt))
244             return false;
245         if (executionId == null) {
246             if (other.executionId != null)
247                 return false;
248         } else if (!executionId.equals(other.executionId))
249             return false;
250         if (jobId == null) {
251             if (other.jobId != null)
252                 return false;
253         } else if (!jobId.equals(other.jobId))
254             return false;
255         if (nodeSummary == null) {
256             if (other.nodeSummary != null)
257                 return false;
258         } else if (!nodeSummary.equals(other.nodeSummary))
259             return false;
260         if (project == null) {
261             if (other.project != null)
262                 return false;
263         } else if (!project.equals(other.project))
264             return false;
265         if (startedAt == null) {
266             if (other.startedAt != null)
267                 return false;
268         } else if (!startedAt.equals(other.startedAt))
269             return false;
270         if (status == null) {
271             if (other.status != null)
272                 return false;
273         } else if (!status.equals(other.status))
274             return false;
275         if (summary == null) {
276             if (other.summary != null)
277                 return false;
278         } else if (!summary.equals(other.summary))
279             return false;
280         if (title == null) {
281             if (other.title != null)
282                 return false;
283         } else if (!title.equals(other.title))
284             return false;
285         if (user == null) {
286             if (other.user != null)
287                 return false;
288         } else if (!user.equals(other.user))
289             return false;
290         return true;
291     }
292 
293     /**
294      * Summary for nodes
295      */
296     public static class NodeSummary implements Serializable {
297 
298         private static final long serialVersionUID = 1L;
299 
300         private int succeeded;
301 
302         private int failed;
303 
304         private int total;
305 
306         public int getSucceeded() {
307             return succeeded;
308         }
309 
310         public void setSucceeded(int succeeded) {
311             this.succeeded = succeeded;
312         }
313 
314         public int getFailed() {
315             return failed;
316         }
317 
318         public void setFailed(int failed) {
319             this.failed = failed;
320         }
321 
322         public int getTotal() {
323             return total;
324         }
325 
326         public void setTotal(int total) {
327             this.total = total;
328         }
329 
330         @Override
331         public String toString() {
332             return "NodeSummary [succeeded=" + succeeded + ", failed=" + failed + ", total=" + total + "]";
333         }
334 
335         @Override
336         public int hashCode() {
337             final int prime = 31;
338             int result = 1;
339             result = prime * result + failed;
340             result = prime * result + succeeded;
341             result = prime * result + total;
342             return result;
343         }
344 
345         @Override
346         public boolean equals(Object obj) {
347             if (this == obj)
348                 return true;
349             if (obj == null)
350                 return false;
351             if (getClass() != obj.getClass())
352                 return false;
353             NodeSummary other = (NodeSummary) obj;
354             if (failed != other.failed)
355                 return false;
356             if (succeeded != other.succeeded)
357                 return false;
358             if (total != other.total)
359                 return false;
360             return true;
361         }
362 
363     }
364 
365     /**
366      * The status of an event
367      */
368     public static enum EventStatus {
369         SUCCEEDED, FAILED, ABORTED;
370     }
371 
372 }