Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.meter.api.user;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import com.ning.billing.meter.timeline.TimelineEventHandler;
import com.ning.billing.meter.timeline.chunks.TimelineChunk;
import com.ning.billing.meter.timeline.codec.DefaultSampleCoder;
import com.ning.billing.meter.timeline.codec.SampleCoder;
import com.ning.billing.meter.timeline.consumer.TimelineChunkConsumer;
import com.ning.billing.meter.timeline.persistent.TimelineDao;
import com.ning.billing.util.callcontext.TenantContext;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class JsonSamplesOutputer {
protected static final ObjectMapper objectMapper = new ObjectMapper();
protected final TimelineEventHandler timelineEventHandler;
protected final TimelineDao timelineDao;
protected final SampleCoder sampleCoder;
protected final TenantContext context;
public JsonSamplesOutputer(final TimelineEventHandler timelineEventHandler, final TimelineDao timelineDao, final TenantContext context) {
this.timelineEventHandler = timelineEventHandler;
this.timelineDao = timelineDao;
this.sampleCoder = new DefaultSampleCoder();
this.context = context;
}
protected abstract void writeJsonForChunks(final JsonGenerator generator, final Collection extends TimelineChunk> chunksForSourceAndMetric) throws IOException;
public void output(final OutputStream output, final List sources, final Map> metricsPerCategory,
final DateTime startTime, final DateTime endTime) throws IOException {
// Retrieve the source and metric ids
final List sourceIds = translateSourcesToSourceIds(sources);
final List metricIds = translateCategoriesAndMetricNamesToMetricIds(metricsPerCategory);
output(output, sourceIds, metricIds, startTime, endTime);
}
protected void output(final OutputStream output, final List sourceIds, final List metricIds,
final DateTime startTime, final DateTime endTime) throws IOException {
// Setup Jackson
final JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(output);
generator.writeStartArray();
// First, return all data stored in the database
writeJsonForStoredChunks(generator, sourceIds, metricIds, startTime, endTime);
// Now return all data in memory
writeJsonForInMemoryChunks(generator, sourceIds, metricIds, startTime, endTime);
// Allow implementers to flush their buffers
writeRemainingData(generator);
generator.writeEndArray();
generator.flush();
generator.close();
}
protected void writeRemainingData(final JsonGenerator generator) throws IOException {
// No-op
}
private List translateSourcesToSourceIds(final List sources) {
final List hostIds = new ArrayList(sources.size());
for (final String source : sources) {
final Integer sourceId = timelineDao.getSourceId(source, context);
if (sourceId == null) {
// Ignore
continue;
}
hostIds.add(sourceId);
}
return hostIds;
}
private List translateCategoriesAndMetricNamesToMetricIds(final Map> metricsPerCategory) {
final List metricIds = new ArrayList(metricsPerCategory.keySet().size());
for (final String category : metricsPerCategory.keySet()) {
final Integer categoryId = timelineDao.getEventCategoryId(category, context);
if (categoryId == null) {
// Ignore
continue;
}
for (final String metricName : metricsPerCategory.get(category)) {
final Integer sampleKindId = timelineDao.getMetricId(categoryId, metricName, context);
if (sampleKindId == null) {
// Ignore
continue;
}
metricIds.add(sampleKindId);
}
}
return metricIds;
}
private void writeJsonForStoredChunks(final JsonGenerator generator, final List hostIdsList, final List sampleKindIdsList,
final DateTime startTime, final DateTime endTime) throws IOException {
final AtomicReference lastHostId = new AtomicReference(null);
final AtomicReference lastSampleKindId = new AtomicReference(null);
final List chunksForHostAndSampleKind = new ArrayList();
timelineDao.getSamplesBySourceIdsAndMetricIds(hostIdsList, sampleKindIdsList, startTime, endTime, new TimelineChunkConsumer() {
@Override
public void processTimelineChunk(final TimelineChunk chunks) {
final Integer previousHostId = lastHostId.get();
final Integer previousSampleKindId = lastSampleKindId.get();
final Integer currentHostId = chunks.getSourceId();
final Integer currentSampleKindId = chunks.getMetricId();
chunksForHostAndSampleKind.add(chunks);
if (previousHostId != null && (!previousHostId.equals(currentHostId) || !previousSampleKindId.equals(currentSampleKindId))) {
try {
writeJsonForChunks(generator, chunksForHostAndSampleKind);
} catch (IOException e) {
throw new RuntimeException(e);
}
chunksForHostAndSampleKind.clear();
}
lastHostId.set(currentHostId);
lastSampleKindId.set(currentSampleKindId);
}
}, context);
if (chunksForHostAndSampleKind.size() > 0) {
writeJsonForChunks(generator, chunksForHostAndSampleKind);
chunksForHostAndSampleKind.clear();
}
}
private void writeJsonForInMemoryChunks(final JsonGenerator generator, final List hostIdsList, final List sampleKindIdsList,
@Nullable final DateTime startTime, @Nullable final DateTime endTime) throws IOException {
for (final Integer hostId : hostIdsList) {
final Collection extends TimelineChunk> inMemorySamples;
try {
inMemorySamples = timelineEventHandler.getInMemoryTimelineChunks(hostId, sampleKindIdsList, startTime, endTime, context);
} catch (ExecutionException e) {
throw new IOException(e);
}
writeJsonForChunks(generator, inMemorySamples);
}
}
}