SegmentCostH2DAO.java 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19 20
package org.skywalking.apm.collector.ui.dao;

clevertension's avatar
clevertension 已提交
21 22 23 24 25
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

clevertension's avatar
clevertension 已提交
26 27 28 29 30 31 32 33
import org.elasticsearch.search.sort.SortOrder;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.global.GlobalTraceTable;
import org.skywalking.apm.collector.storage.define.segment.SegmentCostTable;
clevertension's avatar
clevertension 已提交
34
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
P
pengys5 已提交
35
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
clevertension's avatar
clevertension 已提交
36 37 38
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

clevertension's avatar
clevertension 已提交
39 40
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
P
pengys5 已提交
41 42

/**
clevertension's avatar
clevertension 已提交
43
 * @author pengys5, clevertension
P
pengys5 已提交
44 45
 */
public class SegmentCostH2DAO extends H2DAO implements ISegmentCostDAO {
clevertension's avatar
clevertension 已提交
46 47
    private final Logger logger = LoggerFactory.getLogger(SegmentCostH2DAO.class);
    private static final String GET_SEGMENT_COST_SQL = "select * from {0} where {1} >= ? and {1} <= ?";
P
pengys5 已提交
48
    @Override public JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName,
49
        Error error, int applicationId, List<String> segmentIds, int limit, int from, Sort sort) {
clevertension's avatar
clevertension 已提交
50 51 52
        H2Client client = getClient();
        String sql = GET_SEGMENT_COST_SQL;
        List<Object> params = new ArrayList<>();
clevertension's avatar
clevertension 已提交
53 54 55
        List<Object> columns = new ArrayList<>();
        columns.add(SegmentCostTable.TABLE);
        columns.add(SegmentCostTable.COLUMN_TIME_BUCKET);
clevertension's avatar
clevertension 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        params.add(startTime);
        params.add(endTime);
        int paramIndex = 1;
        if (minCost != -1 || maxCost != -1) {
            if (minCost != -1) {
                paramIndex++;
                sql = sql + " and {" + paramIndex + "} >= ?";
                params.add(minCost);
                columns.add(SegmentCostTable.COLUMN_COST);
            }
            if (maxCost != -1) {
                paramIndex++;
                sql = sql + " and {" + paramIndex + "} <= ?";
                params.add(maxCost);
                columns.add(SegmentCostTable.COLUMN_COST);
            }
        }
        if (StringUtils.isNotEmpty(operationName)) {
            paramIndex++;
            sql = sql + " and {" + paramIndex + "} = ?";
            params.add(operationName);
            columns.add(SegmentCostTable.COLUMN_SERVICE_NAME);
        }
        if (CollectionUtils.isNotEmpty(segmentIds)) {
            paramIndex++;
            sql = sql + " and {" + paramIndex + "} in (";
            columns.add(SegmentCostTable.COLUMN_SEGMENT_ID);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < segmentIds.size(); i++) {
                builder.append("?,");
            }
            builder.delete(builder.length() - 1, builder.length());
            builder.append(")");
            sql = sql + builder;
            for (String segmentId : segmentIds) {
                params.add(segmentId);
            }
        }
        if (Error.True.equals(error)) {
            paramIndex++;
            sql = sql + " and {" + paramIndex + "} = ?";
            params.add(true);
            columns.add(SegmentCostTable.COLUMN_IS_ERROR);
        } else if (Error.False.equals(error)) {
            paramIndex++;
            sql = sql + " and {" + paramIndex + "} = ?";
            params.add(false);
            columns.add(SegmentCostTable.COLUMN_IS_ERROR);
        }
        if (applicationId != 0) {
            paramIndex++;
            sql = sql + " and {" + paramIndex + "} = ?";
            params.add(applicationId);
            columns.add(SegmentCostTable.COLUMN_APPLICATION_ID);
        }

        if (Sort.Cost.equals(sort)) {
            sql = sql + " order by " + SegmentCostTable.COLUMN_COST + " " + SortOrder.DESC;
        } else if (Sort.Time.equals(sort)) {
            sql = sql + " order by " + SegmentCostTable.COLUMN_START_TIME + " " + SortOrder.DESC;
        }

        sql = sql + " limit " + from + "," + limit;
clevertension's avatar
clevertension 已提交
119
        sql = SqlBuilder.buildSql(sql, columns);
clevertension's avatar
clevertension 已提交
120 121 122 123 124 125 126 127 128 129
        Object[] p = params.toArray(new Object[0]);

        JsonObject topSegPaging = new JsonObject();


        JsonArray topSegArray = new JsonArray();
        topSegPaging.add("data", topSegArray);
        int cnt = 0;
        int num = from;
        try (ResultSet rs = client.executeQuery(sql, p)) {
clevertension's avatar
clevertension 已提交
130 131 132 133 134 135 136
            while (rs.next()) {
                JsonObject topSegmentJson = new JsonObject();
                topSegmentJson.addProperty("num", num);
                String segmentId = rs.getString(SegmentCostTable.COLUMN_SEGMENT_ID);
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_SEGMENT_ID, segmentId);
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_START_TIME, rs.getLong(SegmentCostTable.COLUMN_START_TIME));
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_END_TIME, rs.getLong(SegmentCostTable.COLUMN_END_TIME));
clevertension's avatar
clevertension 已提交
137

clevertension's avatar
clevertension 已提交
138 139 140 141 142
                IGlobalTraceDAO globalTraceDAO = (IGlobalTraceDAO) DAOContainer.INSTANCE.get(IGlobalTraceDAO.class.getName());
                List<String> globalTraces = globalTraceDAO.getGlobalTraceId(segmentId);
                if (CollectionUtils.isNotEmpty(globalTraces)) {
                    topSegmentJson.addProperty(GlobalTraceTable.COLUMN_GLOBAL_TRACE_ID, globalTraces.get(0));
                }
clevertension's avatar
clevertension 已提交
143

clevertension's avatar
clevertension 已提交
144 145 146 147
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_APPLICATION_ID, rs.getInt(SegmentCostTable.COLUMN_APPLICATION_ID));
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_SERVICE_NAME, rs.getString(SegmentCostTable.COLUMN_SERVICE_NAME));
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_COST, rs.getLong(SegmentCostTable.COLUMN_COST));
                topSegmentJson.addProperty(SegmentCostTable.COLUMN_IS_ERROR, rs.getBoolean(SegmentCostTable.COLUMN_IS_ERROR));
clevertension's avatar
clevertension 已提交
148

clevertension's avatar
clevertension 已提交
149 150 151 152
                num++;
                topSegArray.add(topSegmentJson);
                cnt++;
            }
clevertension's avatar
clevertension 已提交
153 154 155 156 157
        } catch (SQLException | H2ClientException e) {
            logger.error(e.getMessage(), e);
        }
        topSegPaging.addProperty("recordsTotal", cnt);
        return topSegPaging;
P
pengys5 已提交
158 159
    }
}