001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.metric;
012:
013: import com.versant.core.jdo.VersantPersistenceManagerFactory;
014:
015: import java.util.Date;
016: import java.io.Serializable;
017: import java.io.PrintStream;
018:
019: /**
020: * A number of metric snapshots stored in parallel arrays (one array per metric).
021: *
022: * @see VersantPersistenceManagerFactory#getMetrics()
023: * @see com.versant.core.jdo.VersantPersistenceManagerFactory#getNewMetricSnapshots(int)
024: */
025: public final class MetricSnapshotPacket implements Serializable,
026: MetricDataSource {
027:
028: private final Date[] dates; // date for each snapshot
029: private final int[] ids; // unique ID for each snapshot
030: private final int[][] buf; // the snapshot data in parallel arrays
031:
032: public MetricSnapshotPacket(Date[] dates, int[] ids, int[][] buf) {
033: this .dates = dates;
034: this .ids = ids;
035: this .buf = buf;
036: }
037:
038: /**
039: * Get the number of snapshots.
040: */
041: public int getSize() {
042: return ids.length;
043: }
044:
045: /**
046: * Get the Date of each snapshot.
047: */
048: public Date[] getDates() {
049: return dates;
050: }
051:
052: /**
053: * Get the unique ID of each snapshot.
054: */
055: public int[] getIds() {
056: return ids;
057: }
058:
059: /**
060: * Get the snapshot data. There is one int[] per configured server metric
061: * holding the data for all the snapshots in the packet.
062: * @see com.versant.core.jdo.VersantPersistenceManagerFactory#getMetrics()
063: */
064: public int[][] getBuf() {
065: return buf;
066: }
067:
068: /**
069: * Get the ID of the most recent sample.
070: */
071: public int getMostRecentID() {
072: return ids[ids.length - 1];
073: }
074:
075: /**
076: * Get the Date of the most recent sample.
077: */
078: public Date getMostRecentDate() {
079: return dates[dates.length - 1];
080: }
081:
082: /**
083: * Get the value of sampleNo for the given metricIndex. If sampleNo is
084: * before the first sample then return the first sample. If sampleNo is
085: * after the last sample then return the last sample.
086: */
087: public int getSample(int sampleNo, int metricIndex) {
088: if (sampleNo < 0)
089: sampleNo = 0;
090: else if (sampleNo >= ids.length)
091: sampleNo = ids.length - 1;
092: return buf[metricIndex][sampleNo];
093: }
094:
095: /**
096: * Get the time in seconds between the two samples. The sampleNo's are
097: * adjusted to fit in range.
098: */
099: public double getSeconds(int firstSampleNo, int lastSampleNo) {
100: if (firstSampleNo < 0)
101: firstSampleNo = 0;
102: if (lastSampleNo >= ids.length)
103: lastSampleNo = ids.length - 1;
104: if (lastSampleNo > firstSampleNo) {
105: long diff = dates[lastSampleNo].getTime()
106: - dates[firstSampleNo].getTime();
107: return diff / 1000.0;
108: } else {
109: return 0.0;
110: }
111: }
112:
113: /**
114: * Get the most recent sample in this packet for the metric using the
115: * supplied calculation method. If calc < 0 then the default calculation
116: * method for the metric is used.
117: * @see Metric#CALC_AVERAGE
118: * @see Metric#CALC_DELTA
119: * @see Metric#CALC_DELTA_PER_SECOND
120: * @see Metric#CALC_RAW
121: */
122: public double getMostRecentValue(Metric m, int calc) {
123: int lastSampleNo = ids.length - 1;
124: int firstSampleNo = lastSampleNo - 1;
125: return m.get(this , firstSampleNo, lastSampleNo, calc < 0 ? m
126: .getDefaultCalc() : calc, getSeconds(firstSampleNo,
127: lastSampleNo));
128: }
129:
130: /**
131: * Get the most recent sample in this packet for the metric using its
132: * default calculation method.
133: * @see #getMostRecentValue(Metric, int)
134: */
135: public double getMostRecentValue(Metric m) {
136: return getMostRecentValue(m, -1);
137: }
138:
139: /**
140: * Get the most recent raw int sample in this packet for the metric.
141: * @see #getMostRecentValue(Metric, int)
142: */
143: public int getMostRecentSample(BaseMetric m) {
144: return getSample(ids.length - 1, m.getIndex());
145: }
146:
147: /**
148: * Dump raw sample data to out (for debugging).
149: */
150: public void dump(Metric[] all, int sampleNo, PrintStream out) {
151: for (int i = 0; i < all.length; i++) {
152: if (all[i] instanceof BaseMetric) {
153: BaseMetric m = (BaseMetric) all[i];
154: int mi = m.getIndex();
155: out.println(m + " buf[" + mi + "][" + sampleNo + "] = "
156: + buf[mi][sampleNo]);
157: }
158: }
159: }
160:
161: }
|