All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glowroot.agent.live.LiveAggregateRepositoryImpl Maven / Gradle / Ivy

There is a newer version: 0.9.28
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed 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 org.glowroot.agent.live;

import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import org.glowroot.agent.shaded.google.common.collect.Lists;

import org.glowroot.agent.impl.AggregateIntervalCollector;
import org.glowroot.agent.impl.Aggregator;
import org.glowroot.common.live.LiveAggregateRepository;
import org.glowroot.wire.api.model.AggregateOuterClass.Aggregate;
import org.glowroot.wire.api.model.ProfileTreeOuterClass.ProfileTree;

public class LiveAggregateRepositoryImpl implements LiveAggregateRepository {

    private final Aggregator aggregator;

    public LiveAggregateRepositoryImpl(Aggregator aggregator) {
        this.aggregator = aggregator;
    }

    @Override
    public Set getLiveTransactionTypes(String serverId) {
        return aggregator.getLiveTransactionTypes();
    }

    // from is non-inclusive
    @Override
    public @Nullable LiveResult getLiveOverallSummary(String serverId,
            final String transactionType, long from, long to) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable OverallSummary map(AggregateIntervalCollector collector) {
                return collector.getLiveOverallSummary(transactionType);
            }
        });
    }

    // from is non-inclusive
    @Override
    public @Nullable LiveResult> getLiveTransactionSummaries(
            String serverId, final String transactionType, long from, long to) throws Exception {
        return map(from, to, new Mapper>() {
            @Override
            public List map(AggregateIntervalCollector collector) {
                return collector.getLiveTransactionSummaries(transactionType);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLiveOverviewAggregates(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to, final long liveCaptureTime) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable OverviewAggregate map(AggregateIntervalCollector collector)
                    throws Exception {
                return collector.getLiveOverviewAggregate(transactionType, transactionName,
                        liveCaptureTime);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLivePercentileAggregates(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to, final long liveCaptureTime) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable PercentileAggregate map(AggregateIntervalCollector collector)
                    throws Exception {
                return collector.getLivePercentileAggregate(transactionType, transactionName,
                        liveCaptureTime);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLiveThroughputAggregates(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to, final long liveCaptureTime) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable ThroughputAggregate map(AggregateIntervalCollector collector)
                    throws Exception {
                return collector.getLiveThroughputAggregate(transactionType, transactionName,
                        liveCaptureTime);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLiveProfileTree(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable ProfileTree map(AggregateIntervalCollector collector)
                    throws Exception {
                return collector.getLiveProfile(transactionType, transactionName);
            }
        });
    }

    @Override
    public @Nullable LiveResult> getLiveQueries(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to) throws Exception {
        return map(from, to, new Mapper>() {
            @Override
            public List map(AggregateIntervalCollector collector)
                    throws Exception {
                return collector.getLiveQueries(transactionType, transactionName);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLiveOverallErrorSummary(String serverId,
            final String transactionType, long from, long to) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable OverallErrorSummary map(AggregateIntervalCollector collector) {
                return collector.getLiveOverallErrorSummary(transactionType);
            }
        });
    }

    @Override
    public @Nullable LiveResult> getLiveTransactionErrorSummaries(
            String serverId, final String transactionType, long from, long to) throws Exception {
        return map(from, to, new Mapper>() {
            @Override
            public List map(AggregateIntervalCollector collector) {
                return collector.getLiveTransactionErrorSummaries(transactionType);
            }
        });
    }

    @Override
    public @Nullable LiveResult getLiveErrorPoints(String serverId,
            final String transactionType, final @Nullable String transactionName, long from,
            long to, final long liveCaptureTime) throws Exception {
        return map(from, to, new Mapper() {
            @Override
            public @Nullable ErrorPoint map(AggregateIntervalCollector collector) throws Exception {
                return collector.getLiveErrorPoint(transactionType, transactionName,
                        liveCaptureTime);
            }
        });
    }

    @Override
    public void clearAll() {
        aggregator.clearAll();
    }

    @Nullable
    private  LiveResult map(long from, long to, Mapper mapper) throws Exception {
        List collectors =
                aggregator.getOrderedIntervalCollectorsInRange(from, to);
        if (collectors.isEmpty()) {
            return null;
        }
        long initialCaptureTime = collectors.get(0).getCaptureTime();
        List list = Lists.newArrayList();
        for (AggregateIntervalCollector collector : collectors) {
            T item = mapper.map(collector);
            if (item == null) {
                continue;
            }
            if (item instanceof List && ((List) item).isEmpty()) {
                continue;
            }
            list.add(item);
        }
        if (list.isEmpty()) {
            return null;
        }
        return new LiveResult(list, initialCaptureTime);
    }

    private interface Mapper {
        @Nullable
        T map(AggregateIntervalCollector collector) throws Exception;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy