001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.sla;
020
021import java.sql.Timestamp;
022import java.util.Date;
023import java.util.List;
024
025import javax.persistence.Basic;
026import javax.persistence.Column;
027import javax.persistence.Entity;
028import javax.persistence.Id;
029import javax.persistence.NamedQueries;
030import javax.persistence.NamedQuery;
031import javax.persistence.Table;
032
033import org.apache.oozie.AppType;
034import org.apache.oozie.client.event.SLAEvent;
035import org.apache.oozie.client.rest.JsonBean;
036import org.apache.oozie.client.rest.JsonTags;
037import org.apache.oozie.client.rest.JsonUtils;
038import org.apache.oozie.util.DateUtils;
039import org.apache.openjpa.persistence.jdbc.Index;
040import org.json.simple.JSONArray;
041import org.json.simple.JSONObject;
042
043@Entity
044@Table(name = "SLA_SUMMARY")
045@NamedQueries({
046
047 @NamedQuery(name = "UPDATE_SLA_SUMMARY_FOR_SLA_STATUS", query = "update  SLASummaryBean w set w.slaStatus = :slaStatus, w.eventStatus = :eventStatus, w.eventProcessed = :eventProcessed, w.lastModifiedTS = :lastModifiedTS where w.jobId = :jobId"),
048
049 @NamedQuery(name = "UPDATE_SLA_SUMMARY_FOR_STATUS_ACTUAL_TIMES", query = "update SLASummaryBean w set w.slaStatus = :slaStatus, w.eventStatus = :eventStatus, w.eventProcessed = :eventProcessed, w.jobStatus = :jobStatus, w.lastModifiedTS = :lastModifiedTS, w.actualStartTS = :actualStartTS, w.actualEndTS = :actualEndTS, w.actualDuration = :actualDuration where w.jobId = :jobId"),
050
051 @NamedQuery(name = "UPDATE_SLA_SUMMARY_FOR_ACTUAL_TIMES", query = "update SLASummaryBean w set w.eventProcessed = :eventProcessed, w.actualStartTS = :actualStartTS, w.actualEndTS = :actualEndTS, w.actualEndTS = :actualEndTS, w.actualDuration = :actualDuration, w.lastModifiedTS = :lastModifiedTS where w.jobId = :jobId"),
052
053 @NamedQuery(name = "UPDATE_SLA_SUMMARY_EVENTPROCESSED", query = "update SLASummaryBean w set w.eventProcessed = :eventProcessed where w.jobId = :jobId"),
054
055 @NamedQuery(name = "UPDATE_SLA_SUMMARY_ALL", query = "update SLASummaryBean w set w.jobId = :jobId, w.appName = :appName, w.appType = :appType, w.nominalTimeTS = :nominalTime, w.expectedStartTS = :expectedStartTime, w.expectedEndTS = :expectedEndTime, w.expectedDuration = :expectedDuration, w.jobStatus = :jobStatus, w.slaStatus = :slaStatus, w.eventStatus = :eventStatus, w.lastModifiedTS = :lastModTime, w.user = :user, w.parentId = :parentId, w.eventProcessed = :eventProcessed, w.actualDuration = :actualDuration, w.actualEndTS = :actualEndTS, w.actualStartTS = :actualStartTS where w.jobId = :jobId"),
056
057 @NamedQuery(name = "GET_SLA_SUMMARY", query = "select OBJECT(w) from SLASummaryBean w where w.jobId = :id"),
058
059 @NamedQuery(name = "GET_SLA_SUMMARY_RECORDS_RESTART", query = "select OBJECT(w) from SLASummaryBean w where w.eventProcessed <= 7 AND w.lastModifiedTS >= :lastModifiedTime"),
060
061 @NamedQuery(name = "GET_SLA_SUMMARY_EVENTPROCESSED", query = "select w.eventProcessed from SLASummaryBean w where w.jobId = :id")
062})
063
064/**
065 * Class to store all the SLA related details (summary) per job
066 */
067public class SLASummaryBean implements JsonBean {
068
069    @Id
070    @Basic
071    @Column(name = "job_id")
072    private String jobId;
073
074    @Basic
075    @Index
076    @Column(name = "parent_id")
077    private String parentId;
078
079    @Basic
080    @Index
081    @Column(name = "app_name")
082    private String appName;
083
084    @Basic
085    @Column(name = "app_type")
086    private String appType;
087
088    @Basic
089    @Column(name = "user_name")
090    private String user;
091
092    @Basic
093    @Column(name = "created_time")
094    private Timestamp createdTimeTS = null;
095
096    @Basic
097    @Index
098    @Column(name = "nominal_time")
099    private Timestamp nominalTimeTS = null;
100
101    @Basic
102    @Column(name = "expected_start")
103    private Timestamp expectedStartTS = null;
104
105    @Basic
106    @Column(name = "expected_end")
107    private Timestamp expectedEndTS = null;
108
109    @Basic
110    @Column(name = "expected_duration")
111    private long expectedDuration = -1;
112
113    @Basic
114    @Column(name = "actual_start")
115    private Timestamp actualStartTS = null;
116
117    @Basic
118    @Column(name = "actual_end")
119    private Timestamp actualEndTS = null;
120
121    @Basic
122    @Column(name = "actual_duration")
123    private long actualDuration = -1;
124
125    @Basic
126    @Column(name = "job_status")
127    private String jobStatus;
128
129    @Basic
130    @Column(name = "event_status")
131    private String eventStatus;
132
133    @Basic
134    @Column(name = "sla_status")
135    private String slaStatus;
136
137    @Basic
138    @Index
139    @Column(name = "event_processed")
140    private byte eventProcessed = 0;
141
142    @Basic
143    @Index
144    @Column(name = "last_modified")
145    private Timestamp lastModifiedTS = null;
146
147    public SLASummaryBean() {
148    }
149
150    public SLASummaryBean(SLACalcStatus slaCalc) {
151        SLARegistrationBean reg = slaCalc.getSLARegistrationBean();
152        setId(slaCalc.getId());
153        setAppName(reg.getAppName());
154        setAppType(reg.getAppType());
155        setNominalTime(reg.getNominalTime());
156        setExpectedStart(reg.getExpectedStart());
157        setExpectedEnd(reg.getExpectedEnd());
158        setExpectedDuration(reg.getExpectedDuration());
159        setJobStatus(slaCalc.getJobStatus());
160        setSLAStatus(slaCalc.getSLAStatus());
161        setEventStatus(slaCalc.getEventStatus());
162        setLastModifiedTime(slaCalc.getLastModifiedTime());
163        setUser(reg.getUser());
164        setParentId(reg.getParentId());
165        setEventProcessed(slaCalc.getEventProcessed());
166        setActualDuration(slaCalc.getActualDuration());
167        setActualEnd(slaCalc.getActualEnd());
168        setActualStart(slaCalc.getActualStart());
169    }
170
171    public String getId() {
172        return jobId;
173    }
174
175    public void setId(String jobId) {
176        this.jobId = jobId;
177    }
178
179    public String getParentId() {
180        return parentId;
181    }
182
183    public void setParentId(String parentId) {
184        this.parentId = parentId;
185    }
186
187    public Timestamp getCreatedTimestamp() {
188        return createdTimeTS;
189    }
190
191    public void setCreatedTimestamp(Timestamp createdTime) {
192        this.createdTimeTS = createdTime;
193    }
194
195    public Date getCreatedTime() {
196        return DateUtils.toDate(createdTimeTS);
197    }
198
199    public void setCreatedTime(Date createdTime) {
200        this.createdTimeTS = DateUtils.convertDateToTimestamp(createdTime);
201    }
202
203    public Date getNominalTime() {
204        return DateUtils.toDate(nominalTimeTS);
205    }
206
207    public Timestamp getNominalTimestamp() {
208        return this.nominalTimeTS;
209    }
210
211    public void setNominalTime(Date nominalTime) {
212        this.nominalTimeTS = DateUtils.convertDateToTimestamp(nominalTime);
213    }
214
215
216    public Date getExpectedStart() {
217        return DateUtils.toDate(expectedStartTS);
218    }
219
220    public Timestamp getExpectedStartTimestamp() {
221        return this.expectedStartTS;
222    }
223
224    public void setExpectedStart(Date expectedStart) {
225        this.expectedStartTS = DateUtils.convertDateToTimestamp(expectedStart);
226    }
227
228    public Date getExpectedEnd() {
229        return DateUtils.toDate(expectedEndTS);
230    }
231
232    public Timestamp getExpectedEndTimestamp() {
233        return this.expectedEndTS;
234    }
235    public void setExpectedEnd(Date expectedEnd) {
236        this.expectedEndTS = DateUtils.convertDateToTimestamp(expectedEnd);
237    }
238
239    public long getExpectedDuration() {
240        return expectedDuration;
241    }
242
243    public void setExpectedDuration(long expectedDuration) {
244        this.expectedDuration = expectedDuration;
245    }
246
247    public Date getActualStart() {
248        return DateUtils.toDate(actualStartTS);
249    }
250
251    public Timestamp getActualStartTimestamp() {
252        return this.actualStartTS;
253    }
254
255    public void setActualStart(Date actualStart) {
256        this.actualStartTS = DateUtils.convertDateToTimestamp(actualStart);
257    }
258
259    public Date getActualEnd() {
260        return DateUtils.toDate(actualEndTS);
261    }
262
263    public Timestamp getActualEndTimestamp() {
264        return this.actualEndTS;
265    }
266
267    public void setActualEnd(Date actualEnd) {
268        this.actualEndTS = DateUtils.convertDateToTimestamp(actualEnd);
269    }
270
271    public long getActualDuration() {
272        return actualDuration;
273    }
274
275    public void setActualDuration(long actualDuration) {
276        this.actualDuration = actualDuration;
277    }
278
279    public String getJobStatus() {
280        return jobStatus;
281    }
282
283    public void setJobStatus(String status) {
284        this.jobStatus = status;
285    }
286
287    public SLAEvent.EventStatus getEventStatus() {
288        return (eventStatus != null ? SLAEvent.EventStatus.valueOf(eventStatus) : null);
289    }
290
291    public void setEventStatus(SLAEvent.EventStatus eventStatus) {
292        this.eventStatus = (eventStatus != null ? eventStatus.name() : null);
293    }
294
295    public SLAEvent.SLAStatus getSLAStatus() {
296        return (slaStatus != null ? SLAEvent.SLAStatus.valueOf(slaStatus) : null);
297    }
298
299    public String getSLAStatusString() {
300        return slaStatus;
301    }
302
303    public String getEventStatusString() {
304        return eventStatus;
305    }
306
307    public void setSLAStatus(SLAEvent.SLAStatus stage) {
308        this.slaStatus = (stage != null ? stage.name() : null);
309    }
310
311    public String getUser() {
312        return user;
313    }
314
315    public void setUser(String user) {
316        this.user = user;
317    }
318
319    public String getAppName() {
320        return appName;
321    }
322
323    public void setAppName(String appName) {
324        this.appName = appName;
325    }
326
327    public AppType getAppType() {
328        return AppType.valueOf(appType);
329    }
330
331    public void setAppType(AppType appType) {
332        this.appType = appType.toString();
333    }
334
335    public byte getEventProcessed() {
336        return eventProcessed;
337    }
338
339    public void setEventProcessed(int eventProcessed) {
340        this.eventProcessed = (byte)eventProcessed;
341    }
342
343    public Date getLastModifiedTime() {
344        return DateUtils.toDate(lastModifiedTS);
345    }
346
347    public Timestamp getLastModifiedTimestamp() {
348        return this.lastModifiedTS;
349    }
350
351    public void setLastModifiedTime(Date lastModified) {
352        this.lastModifiedTS = DateUtils.convertDateToTimestamp(lastModified);
353    }
354
355    @SuppressWarnings("unchecked")
356    @Override
357    public JSONObject toJSONObject() {
358        JSONObject json = new JSONObject();
359        json.put(JsonTags.SLA_SUMMARY_ID, jobId);
360        if (parentId != null) {
361            json.put(JsonTags.SLA_SUMMARY_PARENT_ID, parentId);
362        }
363        json.put(JsonTags.SLA_SUMMARY_APP_NAME, appName);
364        json.put(JsonTags.SLA_SUMMARY_APP_TYPE, appType);
365        json.put(JsonTags.SLA_SUMMARY_USER, user);
366        json.put(JsonTags.SLA_SUMMARY_NOMINAL_TIME, nominalTimeTS.getTime());
367        if (expectedStartTS != null) {
368            json.put(JsonTags.SLA_SUMMARY_EXPECTED_START, expectedStartTS.getTime());
369        }
370        else {
371            json.put(JsonTags.SLA_SUMMARY_EXPECTED_START, null);
372        }
373        if (actualStartTS != null) {
374            json.put(JsonTags.SLA_SUMMARY_ACTUAL_START, actualStartTS.getTime());
375        }
376        else {
377            json.put(JsonTags.SLA_SUMMARY_ACTUAL_START, null);
378        }
379        json.put(JsonTags.SLA_SUMMARY_EXPECTED_END, expectedEndTS.getTime());
380        if (actualEndTS != null) {
381            json.put(JsonTags.SLA_SUMMARY_ACTUAL_END, actualEndTS.getTime());
382        }
383        else {
384            json.put(JsonTags.SLA_SUMMARY_ACTUAL_END, null);
385        }
386        json.put(JsonTags.SLA_SUMMARY_EXPECTED_DURATION, expectedDuration);
387        json.put(JsonTags.SLA_SUMMARY_ACTUAL_DURATION, actualDuration);
388        json.put(JsonTags.SLA_SUMMARY_JOB_STATUS, jobStatus);
389        json.put(JsonTags.SLA_SUMMARY_SLA_STATUS, slaStatus);
390        json.put(JsonTags.SLA_SUMMARY_LAST_MODIFIED, lastModifiedTS.getTime());
391        return json;
392    }
393
394    @SuppressWarnings("unchecked")
395    @Override
396    public JSONObject toJSONObject(String timeZoneId) {
397        if (timeZoneId == null) {
398            return toJSONObject();
399        }
400        else {
401            JSONObject json = new JSONObject();
402            json.put(JsonTags.SLA_SUMMARY_ID, jobId);
403            if (parentId != null) {
404                json.put(JsonTags.SLA_SUMMARY_PARENT_ID, parentId);
405            }
406            json.put(JsonTags.SLA_SUMMARY_APP_NAME, appName);
407            json.put(JsonTags.SLA_SUMMARY_APP_TYPE, appType);
408            json.put(JsonTags.SLA_SUMMARY_USER, user);
409            json.put(JsonTags.SLA_SUMMARY_NOMINAL_TIME, JsonUtils.formatDateRfc822(nominalTimeTS, timeZoneId));
410            if (expectedStartTS != null) {
411                json.put(JsonTags.SLA_SUMMARY_EXPECTED_START, JsonUtils.formatDateRfc822(expectedStartTS, timeZoneId));
412            }
413            else {
414                json.put(JsonTags.SLA_SUMMARY_EXPECTED_START, null);
415            }
416            if (actualStartTS != null) {
417                json.put(JsonTags.SLA_SUMMARY_ACTUAL_START, JsonUtils.formatDateRfc822(actualStartTS, timeZoneId));
418            }
419            else {
420                json.put(JsonTags.SLA_SUMMARY_ACTUAL_START, null);
421            }
422            json.put(JsonTags.SLA_SUMMARY_EXPECTED_END, JsonUtils.formatDateRfc822(expectedEndTS, timeZoneId));
423            if (actualEndTS != null) {
424                json.put(JsonTags.SLA_SUMMARY_ACTUAL_END, JsonUtils.formatDateRfc822(actualEndTS, timeZoneId));
425            }
426            else {
427                json.put(JsonTags.SLA_SUMMARY_ACTUAL_END, null);
428            }
429            json.put(JsonTags.SLA_SUMMARY_EXPECTED_DURATION, expectedDuration);
430            json.put(JsonTags.SLA_SUMMARY_ACTUAL_DURATION, actualDuration);
431            json.put(JsonTags.SLA_SUMMARY_JOB_STATUS, jobStatus);
432            json.put(JsonTags.SLA_SUMMARY_SLA_STATUS, slaStatus);
433            json.put(JsonTags.SLA_SUMMARY_LAST_MODIFIED, JsonUtils.formatDateRfc822(lastModifiedTS, timeZoneId));
434            return json;
435        }
436    }
437
438    /**
439     * Convert a sla summary list into a json object.
440     *
441     * @param slaSummaryList sla summary list.
442     * @param timeZoneId time zone to use for dates in the JSON array.
443     * @return the corresponding JSON object.
444     */
445    @SuppressWarnings("unchecked")
446    public static JSONObject toJSONObject(List<? extends SLASummaryBean> slaSummaryList, String timeZoneId) {
447        JSONObject json = new JSONObject();
448        JSONArray array = new JSONArray();
449        if (slaSummaryList != null) {
450            for (SLASummaryBean summary : slaSummaryList) {
451                array.add(summary.toJSONObject(timeZoneId));
452            }
453        }
454        json.put(JsonTags.SLA_SUMMARY_LIST, array);
455        return json;
456    }
457
458}