View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.presentation;
23  
24  import de.laures.cewolf.DatasetProduceException;
25  import de.laures.cewolf.DatasetProducer;
26  import java.io.Serializable;
27  import java.util.Calendar;
28  import java.util.Date;
29  import java.util.GregorianCalendar;
30  import java.util.Map;
31  import javax.xml.datatype.DatatypeFactory;
32  import org.miloss.fgsms.services.interfaces.dataaccessservice.GetOperationalStatusLogResponseMsg;
33  import org.miloss.fgsms.services.interfaces.dataaccessservice.OperationalRecord;
34  import org.apache.log4j.Level;
35  import org.miloss.fgsms.common.Logger;;
36  import org.jfree.data.time.Millisecond;
37  import org.jfree.data.time.TimeSeries;
38  import org.jfree.data.time.TimeSeriesCollection;
39  import org.jfree.data.time.TimeSeriesDataItem;
40  
41  /**
42   *
43   * @author AO
44   */
45  public class AvailabilityChart implements DatasetProducer, Serializable {
46  
47      /**
48       * used from service profile requires the following items in the parameter
49       * map fgsms.availabledata typeof GetOperationalStatusLogResponseMsg
50       * fgsms.laststatus type Boolean fgsms.laststatustimestamp type Long
51       *
52       * @param params
53       * @return
54       * @throws DatasetProduceException
55       */
56      public Object produceDataset(Map params) throws DatasetProduceException {
57          LogHelper.getLog().log(Level.DEBUG, "fgsmsWeb produceDataset");
58          TimeSeriesCollection col = new TimeSeriesCollection();
59          try {
60              /*
61               * if (dasu == null) { dasu = new URL((String)
62               * params.get("dasurl")); } int days = 365; SecurityWrapper c =
63               * (SecurityWrapper) params.get("currentclassification"); try { days
64               * = (Integer) params.get("fgsms.days"); if (days <= 0) { days =
65               * 365; } } catch (Exception ex) {
66               * //Logger.getLogger(PerformanceViewerInvocations.class.getName()).log(Level.SEVERE,
67               * "fgsmsWeb produceDataset", ex); }
68               */
69              // String username = "";
70              // String password = "";
71             /*
72               * AuthMode mode_ = AuthMode.None; try { mode_ = (AuthMode)
73               * params.get("fgsms.authmode"); if (mode_ ==
74               * AuthMode.UsernamePassword) { username = (String)
75               * params.get("fgsms.username"); password = (String)
76               * params.get("fgsms.password"); } } catch (Exception ex) {
77               * Logger.getLogger(PerformanceViewerInvocations.class.getName()).log(Level.ERROR,
78               * "fgsmsWeb produceDataset", ex); return null; }
79               *
80               * try { // serviceurl = (String) params.get("fgsms.url"); } catch
81               * (Exception ex) {
82               * Logger.getLogger(PerformanceViewerInvocations.class.getName()).log(Level.ERROR,
83               * "fgsmsWeb produceDataset", ex); return null; }
84               */
85              GetOperationalStatusLogResponseMsg res = null;
86              try {
87                  res = (GetOperationalStatusLogResponseMsg) params.get("fgsms.availabledata");
88              } catch (Exception ex) {
89                  LogHelper.getLog().log(Level.ERROR, "fgsmsWeb produceDataset, can't get the oplog", ex);
90                  return col;
91              }
92              if (res == null) {
93                  LogHelper.getLog().log(Level.ERROR, "fgsmsWeb produceDataset, oplog is null");
94                  return col;
95              }
96              Boolean laststatus = true;
97              Long laststatusat = System.currentTimeMillis();
98              try {
99                  laststatus = (Boolean) params.get("fgsms.laststatus");
100                 laststatusat = (Long) params.get("fgsms.laststatustimestamp");
101             } catch (Exception ex) {
102             }
103             //add last known status item
104             GregorianCalendar cal = new GregorianCalendar();
105             if (laststatusat != null) {
106                 cal.setTimeInMillis(laststatusat.longValue());
107             }
108             
109             OperationalRecord cur = new OperationalRecord();
110             cur.setOperational(laststatus);
111             cur.setTimestamp((cal));
112             res.getOperationalRecord().add(cur);
113 
114             if (res != null && res.getOperationalRecord() != null && res.getOperationalRecord().size() > 1) {
115 
116                 TimeSeries s1 = new TimeSeries("Operational", org.jfree.data.time.Millisecond.class);
117                 for (int i = 1; i < res.getOperationalRecord().size(); i++) {
118                     Calendar tempcal = res.getOperationalRecord().get(i).getTimestamp();
119                     tempcal.add(Calendar.SECOND, -1);
120                     Millisecond current = new Millisecond(tempcal.getTime());
121                     Millisecond previous = new Millisecond(res.getOperationalRecord().get(i - 1).getTimestamp().getTime());
122                     TimeSeriesDataItem t = null;
123                     if (res.getOperationalRecord().get(i - 1).isOperational()) {
124                         try {
125                             t = new TimeSeriesDataItem(previous, 1);
126                             s1.add(t);
127                         } catch (Exception ex) {
128                         }
129                         try {
130                             t = new TimeSeriesDataItem(current, 1);
131                             s1.add(t);
132                         } catch (Exception ex) {
133                         }
134                     } else {
135                         try {
136                             t = new TimeSeriesDataItem(previous, 0);
137                             s1.add(t);
138                         } catch (Exception ex) {
139                         }
140                         try {
141                             t = new TimeSeriesDataItem(current, 0);
142                             s1.add(t);
143                         } catch (Exception ex) {
144                         }
145 
146                     }
147 
148                 }
149                 if (res.getOperationalRecord().get(res.getOperationalRecord().size() - 1).isOperational()) {
150 
151                     TimeSeriesDataItem t = null;
152                     t = new TimeSeriesDataItem(new Millisecond(res.getOperationalRecord().get(res.getOperationalRecord().size() - 1).getTimestamp().getTime()), 1);
153                     s1.add(t);
154                     t = new TimeSeriesDataItem(new Millisecond(GregorianCalendar.getInstance().getTime()), 1);
155                     s1.add(t);
156                 } else {
157 
158                     TimeSeriesDataItem t = null;
159                     t = new TimeSeriesDataItem(new Millisecond(res.getOperationalRecord().get(res.getOperationalRecord().size() - 1).getTimestamp().getTime()), 0);
160                     s1.add(t);
161                     t = new TimeSeriesDataItem(new Millisecond(GregorianCalendar.getInstance().getTime()), 0);
162                     s1.add(t);
163                 }
164 
165                 col.addSeries(s1);
166 
167             }
168 
169             return col;
170         } catch (Exception ex) {
171             LogHelper.getLog().log(Level.ERROR, "fgsmsWeb produceDataset, error caught", ex);
172             //throw new DatasetProduceException("error", ex);
173         }
174         return col;
175     }
176 
177     public boolean hasExpired(Map params, Date since) {
178         return (System.currentTimeMillis() - since.getTime()) > 5000;
179     }
180 
181     public String getProducerId() {
182         return "fgsms.AvailabiltiyChart";
183     }
184 }