com.seyren.core.service.live.MetricsTask.java Source code

Java tutorial

Introduction

Here is the source code for com.seyren.core.service.live.MetricsTask.java

Source

/**
 * 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 com.seyren.core.service.live;

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

import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.seyren.core.domain.Check;
import com.seyren.core.service.schedule.CheckRunner;
import com.seyren.core.service.schedule.CheckRunnerFactory;
import com.seyren.core.store.ChecksStore;

public class MetricsTask implements Runnable {
    private Set<Metric> metrics;
    private ChecksStore checksStore;
    private CheckRunnerFactory checkRunnerFactory;

    public MetricsTask(Set<Metric> metrics, ChecksStore checksStore, CheckRunnerFactory checkRunnerFactory) {
        this.metrics = metrics;
        this.checksStore = checksStore;
        this.checkRunnerFactory = checkRunnerFactory;
    }

    @Override
    public void run() {
        final List<Check> checks = checksStore.getChecks(true, true).getValues();
        for (final Check check : checks) {
            Iterator<Metric> metricByName = Iterables.filter(metrics, new FindByName(check.getName())).iterator();
            if (metricByName.hasNext()) {
                Metric found = metricByName.next();
                CheckRunner runner = checkRunnerFactory.create(check, found.getValue());
                runner.run();
            }
        }
    }

    private static class FindByName implements Predicate<Metric> {
        private String name;

        private FindByName(String name) {
            this.name = name;
        }

        @Override
        public boolean apply(Metric metric) {
            return Objects.equal(metric.getName(), name);
        }
    }
}