KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metric > MetricSnapshotPacket


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metric;
13
14 import com.versant.core.jdo.VersantPersistenceManagerFactory;
15
16 import java.util.Date JavaDoc;
17 import java.io.Serializable JavaDoc;
18 import java.io.PrintStream JavaDoc;
19
20 /**
21  * A number of metric snapshots stored in parallel arrays (one array per metric).
22  *
23  * @see VersantPersistenceManagerFactory#getMetrics()
24  * @see com.versant.core.jdo.VersantPersistenceManagerFactory#getNewMetricSnapshots(int)
25  */

26 public final class MetricSnapshotPacket
27         implements Serializable JavaDoc, MetricDataSource {
28
29     private final Date JavaDoc[] dates; // date for each snapshot
30
private final int[] ids; // unique ID for each snapshot
31
private final int[][] buf; // the snapshot data in parallel arrays
32

33     public MetricSnapshotPacket(Date JavaDoc[] dates, int[] ids, int[][] buf) {
34         this.dates = dates;
35         this.ids = ids;
36         this.buf = buf;
37     }
38
39     /**
40      * Get the number of snapshots.
41      */

42     public int getSize() {
43         return ids.length;
44     }
45
46     /**
47      * Get the Date of each snapshot.
48      */

49     public Date JavaDoc[] getDates() {
50         return dates;
51     }
52
53     /**
54      * Get the unique ID of each snapshot.
55      */

56     public int[] getIds() {
57         return ids;
58     }
59
60     /**
61      * Get the snapshot data. There is one int[] per configured server metric
62      * holding the data for all the snapshots in the packet.
63      * @see com.versant.core.jdo.VersantPersistenceManagerFactory#getMetrics()
64      */

65     public int[][] getBuf() {
66         return buf;
67     }
68
69     /**
70      * Get the ID of the most recent sample.
71      */

72     public int getMostRecentID() {
73         return ids[ids.length - 1];
74     }
75
76     /**
77      * Get the Date of the most recent sample.
78      */

79     public Date JavaDoc getMostRecentDate() {
80         return dates[dates.length - 1];
81     }
82
83     /**
84      * Get the value of sampleNo for the given metricIndex. If sampleNo is
85      * before the first sample then return the first sample. If sampleNo is
86      * after the last sample then return the last sample.
87      */

88     public int getSample(int sampleNo, int metricIndex) {
89         if (sampleNo < 0) sampleNo = 0;
90         else if (sampleNo >= ids.length) sampleNo = ids.length - 1;
91         return buf[metricIndex][sampleNo];
92     }
93
94     /**
95      * Get the time in seconds between the two samples. The sampleNo's are
96      * adjusted to fit in range.
97      */

98     public double getSeconds(int firstSampleNo, int lastSampleNo) {
99         if (firstSampleNo < 0) firstSampleNo = 0;
100         if (lastSampleNo >= ids.length) lastSampleNo = ids.length - 1;
101         if (lastSampleNo > firstSampleNo) {
102             long diff = dates[lastSampleNo].getTime()
103                     - dates[firstSampleNo].getTime();
104             return diff / 1000.0;
105         } else {
106             return 0.0;
107         }
108     }
109
110     /**
111      * Get the most recent sample in this packet for the metric using the
112      * supplied calculation method. If calc < 0 then the default calculation
113      * method for the metric is used.
114      * @see Metric#CALC_AVERAGE
115      * @see Metric#CALC_DELTA
116      * @see Metric#CALC_DELTA_PER_SECOND
117      * @see Metric#CALC_RAW
118      */

119     public double getMostRecentValue(Metric m, int calc) {
120         int lastSampleNo = ids.length - 1;
121         int firstSampleNo = lastSampleNo - 1;
122         return m.get(this, firstSampleNo, lastSampleNo,
123                 calc < 0 ? m.getDefaultCalc() : calc,
124                 getSeconds(firstSampleNo, lastSampleNo));
125     }
126
127     /**
128      * Get the most recent sample in this packet for the metric using its
129      * default calculation method.
130      * @see #getMostRecentValue(Metric, int)
131      */

132     public double getMostRecentValue(Metric m) {
133         return getMostRecentValue(m, -1);
134     }
135
136     /**
137      * Get the most recent raw int sample in this packet for the metric.
138      * @see #getMostRecentValue(Metric, int)
139      */

140     public int getMostRecentSample(BaseMetric m) {
141         return getSample(ids.length - 1, m.getIndex());
142     }
143
144     /**
145      * Dump raw sample data to out (for debugging).
146      */

147     public void dump(Metric[] all, int sampleNo, PrintStream JavaDoc out) {
148         for (int i = 0; i < all.length; i++) {
149             if (all[i] instanceof BaseMetric) {
150                 BaseMetric m = (BaseMetric)all[i];
151                 int mi = m.getIndex();
152                 out.println(m + " buf[" + mi + "][" + sampleNo + "] = " + buf[mi][sampleNo]);
153             }
154         }
155     }
156
157 }
158
Popular Tags