ServiceNameService.java 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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 org.apache.skywalking.apm.collector.ui.service;

21
import java.text.ParseException;
22
import java.util.List;
23 24
import org.apache.skywalking.apm.collector.cache.CacheModule;
import org.apache.skywalking.apm.collector.cache.service.ServiceNameCacheService;
25 26
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.storage.StorageModule;
27
import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceMetricUIDAO;
28
import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceNameServiceUIDAO;
29
import org.apache.skywalking.apm.collector.storage.table.MetricSource;
30
import org.apache.skywalking.apm.collector.storage.table.register.ServiceName;
31
import org.apache.skywalking.apm.collector.storage.ui.common.ResponseTimeTrend;
32
import org.apache.skywalking.apm.collector.storage.ui.common.SLATrend;
33
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
34
import org.apache.skywalking.apm.collector.storage.ui.common.ThroughputTrend;
35
import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo;
36
import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric;
37 38
import org.apache.skywalking.apm.collector.storage.utils.DurationPoint;
import org.apache.skywalking.apm.collector.ui.utils.DurationUtils;
39 40
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
41 42 43 44 45 46

/**
 * @author peng-yongsheng
 */
public class ServiceNameService {

47 48
    private final Logger logger = LoggerFactory.getLogger(ServiceNameService.class);

49
    private final IServiceNameServiceUIDAO serviceNameServiceUIDAO;
50
    private final IServiceMetricUIDAO serviceMetricUIDAO;
51
    private final ServiceNameCacheService serviceNameCacheService;
52
    private final SecondBetweenService secondBetweenService;
53 54 55

    public ServiceNameService(ModuleManager moduleManager) {
        this.serviceNameServiceUIDAO = moduleManager.find(StorageModule.NAME).getService(IServiceNameServiceUIDAO.class);
56
        this.serviceMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(IServiceMetricUIDAO.class);
57
        this.serviceNameCacheService = moduleManager.find(CacheModule.NAME).getService(ServiceNameCacheService.class);
58
        this.secondBetweenService = new SecondBetweenService(moduleManager);
59 60 61 62 63
    }

    public int getCount() {
        return serviceNameServiceUIDAO.getCount();
    }
64 65 66 67

    public List<ServiceInfo> searchService(String keyword, int topN) {
        return serviceNameServiceUIDAO.searchService(keyword, topN);
    }
68

P
peng-yongsheng 已提交
69
    public ThroughputTrend getServiceTPSTrend(int serviceId, Step step, long startTimeBucket,
彭勇升 pengys 已提交
70
        long endTimeBucket) throws ParseException {
71
        ThroughputTrend throughputTrend = new ThroughputTrend();
P
peng-yongsheng 已提交
72
        List<DurationPoint> durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, startTimeBucket, endTimeBucket);
彭勇升 pengys 已提交
73 74
        List<Integer> throughputTrends = serviceMetricUIDAO.getServiceTPSTrend(serviceId, step, durationPoints);
        throughputTrend.setTrendList(throughputTrends);
75 76 77
        return throughputTrend;
    }

P
peng-yongsheng 已提交
78 79
    public ResponseTimeTrend getServiceResponseTimeTrend(int serviceId, Step step, long startTimeBucket,
        long endTimeBucket) throws ParseException {
80
        ResponseTimeTrend responseTimeTrend = new ResponseTimeTrend();
P
peng-yongsheng 已提交
81
        List<DurationPoint> durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, startTimeBucket, endTimeBucket);
82
        responseTimeTrend.setTrendList(serviceMetricUIDAO.getServiceResponseTimeTrend(serviceId, step, durationPoints));
83 84
        return responseTimeTrend;
    }
85

彭勇升 pengys 已提交
86 87
    public SLATrend getServiceSLATrend(int serviceId, Step step, long startTimeBucket,
        long endTimeBucket) throws ParseException {
88
        SLATrend slaTrend = new SLATrend();
P
peng-yongsheng 已提交
89
        List<DurationPoint> durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, startTimeBucket, endTimeBucket);
90 91 92
        slaTrend.setTrendList(serviceMetricUIDAO.getServiceSLATrend(serviceId, step, durationPoints));
        return slaTrend;
    }
93

94 95 96 97
    public List<ServiceMetric> getSlowService(Step step, long startTimeBucket, long endTimeBucket,
        long startSecondTimeBucket, long endSecondTimeBucket,
        Integer topN) throws ParseException {
        List<ServiceMetric> slowServices = serviceMetricUIDAO.getSlowService(0, step, startTimeBucket, endTimeBucket, topN, MetricSource.Callee);
98
        slowServices.forEach(slowService -> {
99 100 101 102 103 104 105
            ServiceName serviceName = serviceNameCacheService.get(slowService.getId());
            slowService.setName(serviceName.getServiceName());
            try {
                slowService.setCallsPerSec((int)(slowService.getCalls() / secondBetweenService.calculate(serviceName.getApplicationId(), startSecondTimeBucket, endSecondTimeBucket)));
            } catch (ParseException e) {
                logger.error(e.getMessage(), e);
            }
106 107 108
        });
        return slowServices;
    }
109
}