/*
 * Copyright 2017 NAVER Corp.
 *
 * 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.navercorp.pinpoint.web.vo.stat.chart;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.web.util.TimeWindow;
import com.navercorp.pinpoint.web.view.DataSourceChartGroupSerializer;
import com.navercorp.pinpoint.web.vo.chart.Chart;
import com.navercorp.pinpoint.web.vo.chart.Point;
import com.navercorp.pinpoint.web.vo.chart.TimeSeriesChartBuilder;
import com.navercorp.pinpoint.web.vo.stat.SampledDataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Taejin Koo
 */
@JsonSerialize(using = DataSourceChartGroupSerializer.class)
public class DataSourceChartGroup implements AgentStatChartGroup {

    private static final Integer UNCOLLECTED_VALUE = -1;
    private static final String UNCOLLECTED_STRING_VALUE = null;

    public enum DataSourceChartType implements ChartType {
        ACTIVE_CONNECTION_SIZE,
        MAX_CONNECTION_SIZE
    }

    private final Map<ChartType, Chart> dataSourceCharts;

    private final int id;
    private final String serviceTypeName;
    private final String databaseName;
    private final String jdbcUrl;

    public DataSourceChartGroup(TimeWindow timeWindow, List<SampledDataSource> sampledDataSourceList, ServiceTypeRegistryService serviceTypeRegistryService) {
        this.dataSourceCharts = new HashMap<>();

        int size = sampledDataSourceList.size();
        List<Point<Long, Integer>> activeConnectionSizes = new ArrayList<>(size);
        List<Point<Long, Integer>> maxConnectionSizes = new ArrayList<>(size);
        for (SampledDataSource sampledDataSource : sampledDataSourceList) {
            activeConnectionSizes.add(sampledDataSource.getActiveConnectionSize());
            maxConnectionSizes.add(sampledDataSource.getMaxConnectionSize());
        }
        this.dataSourceCharts.put(DataSourceChartType.ACTIVE_CONNECTION_SIZE, new TimeSeriesChartBuilder<>(timeWindow, UNCOLLECTED_VALUE).build(activeConnectionSizes));
        this.dataSourceCharts.put(DataSourceChartType.MAX_CONNECTION_SIZE, new TimeSeriesChartBuilder<>(timeWindow, UNCOLLECTED_VALUE).build(maxConnectionSizes));

        if (CollectionUtils.nullSafeSize(sampledDataSourceList) == 0) {
            this.id = UNCOLLECTED_VALUE;
            this.serviceTypeName = UNCOLLECTED_STRING_VALUE;
            this.databaseName = UNCOLLECTED_STRING_VALUE;
            this.jdbcUrl = UNCOLLECTED_STRING_VALUE;
        } else {
            SampledDataSource defaultDataSource = sampledDataSourceList.get(0);

            this.id = defaultDataSource.getId();
            this.serviceTypeName = serviceTypeRegistryService.findServiceType(defaultDataSource.getServiceTypeCode()).getName();
            this.databaseName = extractDatabaseName(sampledDataSourceList);
            this.jdbcUrl = defaultDataSource.getJdbcUrl();
        }
    }

    private String extractDatabaseName(List<SampledDataSource> sampledDataSourceList) {
        for (SampledDataSource sampledDataSource : sampledDataSourceList) {
            if (sampledDataSource != null && sampledDataSource.getDatabaseName() != null) {
                return sampledDataSource.getDatabaseName();
            }
        }

        return null;
    }

    @Override
    public Map<ChartType, Chart> getCharts() {
        return dataSourceCharts;
    }

    public int getId() {
        return id;
    }

    public String getServiceTypeName() {
        return serviceTypeName;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

}

@@ -836,16 +836,66 @@
 nUtils;%0A
+import com.navercorp.pinpoint.rpc.util.ListUtils;%0A
 import c
@@ -3282,28 +3282,52 @@
 rce 
-defaultDataSource = 
+latestSampledDataSource = ListUtils.getLast(
 samp
@@ -3347,14 +3347,8 @@
 List
-.get(0
 );%0A%0A
@@ -3369,23 +3369,29 @@
 is.id = 
-default
+latestSampled
 DataSour
@@ -3480,23 +3480,29 @@
 iceType(
-default
+latestSampled
 DataSour
@@ -3573,489 +3573,125 @@
 e = 
-extractDatabaseName(sampledDataSourceList);%0A            this.jdbcUrl = defaultDataSource.getJdbcUrl();%0A        %7D%0A    %7D%0A%0A    private String extractDatabaseName(List%3CSampledDataSource%3E sampledDataSourceList) %7B%0A        for (SampledDataSource sampledDataSource : sampledDataSourceList) %7B%0A            if (sampledDataSource != null && sampledDataSource.getDatabaseName() != null) %7B%0A                return sampledDataSource.getDatabaseName();%0A            %7D%0A        %7D%0A%0A        return null;
+latestSampledDataSource.getDatabaseName();%0A            this.jdbcUrl = latestSampledDataSource.getJdbcUrl();%0A        %7D
 %0A   

Add datasource metric collection