View Javadoc

1   package org.lsst.ccs.localdb.statusdb.model;
2   
3   import java.util.logging.Logger;
4   import javax.persistence.Column;
5   import javax.persistence.Entity;
6   import javax.persistence.GeneratedValue;
7   import javax.persistence.GenerationType;
8   import javax.persistence.Id;
9   import javax.persistence.ManyToOne;
10  import javax.persistence.Table;
11  import javax.persistence.Transient;
12  
13  @Entity
14  @Table(name = "statdata")
15  public class StatData {
16  
17      static Logger log = Logger.getLogger("org.lsst.ccs.localdb");
18      private long id;
19      private StatDesc descr;
20      private long tstampFirst;
21      private long tstampLast;
22      private double data;
23      private double sum2;
24      private int n;
25  
26      //TODO keep min and max?
27      public StatData() {
28      }
29  
30      public StatData(StatDesc d) {
31          descr = d;
32      }
33  
34      public StatData(StatDesc d, RawData x) {
35          log.info("new StatData from " + d + " and " + x);
36          log.info(x.getTstamp() + " " + x.getDoubleData());
37          descr = d;
38          data = x.getDoubleData();
39          sum2 = data * data;
40          n = 1;
41          tstampFirst = x.getTstamp();
42          tstampLast = x.getTstamp();
43      }
44  
45      @Transient
46      public double getSum() {
47          return getData() * getN();
48      }
49  
50      public void setSum(double s) {
51          setData(s / getN());
52      }
53  
54      @Transient
55      public double getRMS() {
56          return Math.sqrt(getSum2() / getN() - getData() * getData());
57      }
58  
59      public void accumulate(RawData x) {
60          double sum = getSum();
61          sum += x.getDoubleData();
62          setSum2(getSum2() + x.getDoubleData() * x.getDoubleData());
63          setN(getN() + 1);
64          setSum(sum);
65          setTstampLast(x.getTstamp());
66      }
67  
68      /**
69       * @return the id
70       */
71      @Id
72      @GeneratedValue(strategy = GenerationType.AUTO)
73      public long getId() {
74          return id;
75      }
76  
77      /**
78       * @param id the id to set
79       */
80      public void setId(long id) {
81          this.id = id;
82      }
83  
84      /**
85       * @return the descr
86       */
87      @ManyToOne
88      public StatDesc getDescr() {
89          return descr;
90      }
91  
92      /**
93       * @param descr the descr to set
94       */
95      public void setDescr(StatDesc descr) {
96          this.descr = descr;
97      }
98  
99      /**
100      * @return the tstampFirst
101      */
102     @Column(name = "tstampmills1")
103     public long getTstampFirst() {
104         return tstampFirst;
105     }
106 
107     /**
108      * @param tstampFirst the tstampFirst to set
109      */
110     public void setTstampFirst(long tstampFirst) {
111         this.tstampFirst = tstampFirst;
112     }
113 
114     /**
115      * @return the tstampLast
116      */
117     @Column(name = "tstampmills2")
118     public long getTstampLast() {
119         return tstampLast;
120     }
121 
122     /**
123      * @param tstampLast the tstampLast to set
124      */
125     public void setTstampLast(long tstampLast) {
126         this.tstampLast = tstampLast;
127     }
128 
129     /**
130      * @return the data
131      */
132     public double getData() {
133         return data;
134     }
135 
136     /**
137      * @param data the data to set
138      */
139     public void setData(double data) {
140         this.data = data;
141     }
142 
143     /**
144      * @return the sum2
145      */
146     public double getSum2() {
147         return sum2;
148     }
149 
150     /**
151      * @param sum2 the sum2 to set
152      */
153     public void setSum2(double sum2) {
154         this.sum2 = sum2;
155     }
156 
157     /**
158      * @return the n
159      */
160     public int getN() {
161         return n;
162     }
163 
164     /**
165      * @param n the n to set
166      */
167     public void setN(int n) {
168         this.n = n;
169     }
170 }