Details
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVConsumer.java b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVConsumer.java
new file mode 100644
index 0000000..879e944
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVConsumer.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.consumer;
+
+import java.io.IOException;
+
+import javax.annotation.Nullable;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.usage.timeline.chunks.TimelineChunk;
+import com.ning.billing.usage.timeline.codec.SampleCoder;
+import com.ning.billing.usage.timeline.filter.DecimatingSampleFilter;
+
+public class CSVConsumer {
+
+ private CSVConsumer() {
+ }
+
+ public static String getSamplesAsCSV(final SampleCoder sampleCoder, final TimelineChunk chunk, final DecimatingSampleFilter rangeSampleProcessor) throws IOException {
+ sampleCoder.scan(chunk, rangeSampleProcessor);
+ return rangeSampleProcessor.getSampleConsumer().toString();
+ }
+
+ public static String getSamplesAsCSV(final SampleCoder sampleCoder, final TimelineChunk chunk, @Nullable final DateTime startTime, @Nullable final DateTime endTime) throws IOException {
+ final CSVOutputProcessor processor = new CSVOutputProcessor(startTime, endTime);
+ sampleCoder.scan(chunk, processor);
+ return processor.toString();
+ }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVOutputProcessor.java b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVOutputProcessor.java
new file mode 100644
index 0000000..6e5aa11
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVOutputProcessor.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.consumer;
+
+import javax.annotation.Nullable;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.usage.timeline.codec.TimeRangeSampleProcessor;
+import com.ning.billing.usage.timeline.samples.SampleOpcode;
+
+public class CSVOutputProcessor extends TimeRangeSampleProcessor {
+
+ private final SampleConsumer delegate = new CSVSampleConsumer();
+ private int sampleNumber = 0;
+
+ public CSVOutputProcessor(@Nullable final DateTime startTime, @Nullable final DateTime endTime) {
+ super(startTime, endTime);
+ }
+
+ @Override
+ public void processOneSample(final DateTime sampleTimestamp, final SampleOpcode opcode, final Object value) {
+ delegate.consumeSample(sampleNumber, opcode, value, sampleTimestamp);
+ sampleNumber++;
+ }
+
+ @Override
+ public String toString() {
+ return delegate.toString();
+ }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVSampleConsumer.java b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVSampleConsumer.java
new file mode 100644
index 0000000..cde8725
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/consumer/CSVSampleConsumer.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.consumer;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.usage.timeline.samples.SampleOpcode;
+import com.ning.billing.usage.timeline.util.DateTimeUtils;
+
+public class CSVSampleConsumer implements SampleConsumer {
+
+ private final StringBuilder builder = new StringBuilder();
+ // Use our private counter because of the decimating filter
+ private int builderSampleNumber = 0;
+
+ @Override
+ public void consumeSample(final int sampleNumber, final SampleOpcode opcode, final Object value, final DateTime time) {
+ if (time != null) {
+ final String valueString = value == null ? "0" : value.toString();
+ if (builderSampleNumber > 0) {
+ builder.append(",");
+ }
+
+ builder.append(DateTimeUtils.unixSeconds(time))
+ .append(",")
+ .append(valueString);
+ builderSampleNumber++;
+ }
+ }
+
+ @Override
+ public synchronized String toString() {
+ final String value = builder.toString();
+ // Allow for re-use
+ builder.setLength(0);
+ builderSampleNumber = 0;
+ return value;
+ }
+}