1 2 12 package com.versant.core.metric; 13 14 import com.versant.core.jdo.VersantPersistenceManagerFactory; 15 16 import java.util.Date ; 17 import java.io.Serializable ; 18 import java.io.PrintStream ; 19 20 26 public final class MetricSnapshotPacket 27 implements Serializable , MetricDataSource { 28 29 private final Date [] dates; private final int[] ids; private final int[][] buf; 33 public MetricSnapshotPacket(Date [] dates, int[] ids, int[][] buf) { 34 this.dates = dates; 35 this.ids = ids; 36 this.buf = buf; 37 } 38 39 42 public int getSize() { 43 return ids.length; 44 } 45 46 49 public Date [] getDates() { 50 return dates; 51 } 52 53 56 public int[] getIds() { 57 return ids; 58 } 59 60 65 public int[][] getBuf() { 66 return buf; 67 } 68 69 72 public int getMostRecentID() { 73 return ids[ids.length - 1]; 74 } 75 76 79 public Date getMostRecentDate() { 80 return dates[dates.length - 1]; 81 } 82 83 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 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 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 132 public double getMostRecentValue(Metric m) { 133 return getMostRecentValue(m, -1); 134 } 135 136 140 public int getMostRecentSample(BaseMetric m) { 141 return getSample(ids.length - 1, m.getIndex()); 142 } 143 144 147 public void dump(Metric[] all, int sampleNo, PrintStream 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 |