NormalTopicController.java 17.9 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6
package com.xiaojukeji.kafka.manager.web.api.versionone.normal;

import com.xiaojukeji.kafka.manager.common.constant.Constant;
import com.xiaojukeji.kafka.manager.common.constant.KafkaMetricsCollections;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
Z
zengqiao 已提交
7
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicConnection;
Z
zengqiao 已提交
8 9 10 11 12 13
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicPartitionDTO;
import com.xiaojukeji.kafka.manager.common.entity.dto.normal.TopicDataSampleDTO;
import com.xiaojukeji.kafka.manager.common.entity.metrics.BaseMetrics;
import com.xiaojukeji.kafka.manager.common.entity.vo.common.RealTimeMetricsVO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.TopicBusinessInfoVO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.topic.*;
Z
zengqiao 已提交
14
import com.xiaojukeji.kafka.manager.common.utils.DateUtils;
Z
zengqiao 已提交
15 16 17 18
import com.xiaojukeji.kafka.manager.common.utils.SpringTool;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.KafkaBillDO;
Z
zengqiao 已提交
19
import com.xiaojukeji.kafka.manager.common.utils.jmx.JmxAttributeEnum;
Z
zengqiao 已提交
20
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.topic.TopicStatisticMetricsVO;
Z
zengqiao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.cache.PhysicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.*;
import com.xiaojukeji.kafka.manager.service.service.gateway.TopicConnectionService;
import com.xiaojukeji.kafka.manager.common.constant.ApiPrefix;
import com.xiaojukeji.kafka.manager.web.converters.CommonModelConverter;
import com.xiaojukeji.kafka.manager.web.converters.TopicModelConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author zengqiao
 * @date 20/3/31
 */
@Api(tags = "Normal-Topic详情相关接口(REST)")
@RestController
@RequestMapping(ApiPrefix.API_V1_NORMAL_PREFIX)
public class NormalTopicController {
    @Autowired
    private ClusterService clusterService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private TopicManagerService topicManagerService;

    @Autowired
    private TopicConnectionService connectionService;

    @Autowired
    private LogicalClusterMetadataManager logicalClusterMetadataManager;

    @Autowired
    private KafkaBillService kafkaBillService;

    @ApiOperation(value = "Topic基本信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/basic-info", method = RequestMethod.GET)
    @ResponseBody
    public Result<TopicBasicVO> getTopicBasic(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        return new Result<>(TopicModelConverter.convert2TopicBasicVO(
                topicService.getTopicBasicDTO(physicalClusterId, topicName),
Z
zengqiao 已提交
74 75
                clusterService.getById(physicalClusterId),
                logicalClusterMetadataManager.getTopicLogicalClusterId(physicalClusterId, topicName)
Z
zengqiao 已提交
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        ));
    }

    @ApiOperation(value = "Topic实时流量信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/metrics", method = RequestMethod.GET)
    @ResponseBody
    public Result<RealTimeMetricsVO> getTopicMetrics(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        return new Result<>(CommonModelConverter.convert2RealTimeMetricsVO(
                topicService.getTopicMetricsFromJMX(
                        physicalClusterId,
                        topicName,
                        KafkaMetricsCollections.COMMON_DETAIL_METRICS,
                        true
                )
        ));
    }

    @ApiOperation(value = "Topic历史流量信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/metrics-history", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicMetricVO>> getTopicMetrics(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

        if (ValidateUtils.isBlank(appId)) {
            return new Result<>(TopicModelConverter.convert2TopicMetricsVOList(
                    topicService.getTopicMetricsFromDB(
                            physicalClusterId,
                            topicName,
                            new Date(startTime),
                            new Date(endTime)
                    )
            ));
        }
        return new Result<>(TopicModelConverter.convert2TopicMetricVOList(
                topicService.getTopicMetricsFromDB(
                        appId,
                        physicalClusterId,
                        topicName,
                        new Date(startTime),
                        new Date(endTime)
                )
        ));
    }

    @ApiOperation(value = "Topic实时请求耗时信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/request-time", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicRequestTimeDetailVO>> getTopicRequestMetrics(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
Z
zengqiao 已提交
142 143
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId,
            @RequestParam(value = "percentile", required = false, defaultValue = "75thPercentile") String percentile) {
Z
zengqiao 已提交
144 145 146 147
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
Z
zengqiao 已提交
148 149 150 151 152 153 154 155

        Boolean isPercentileLegal = Arrays.stream(JmxAttributeEnum.PERCENTILE_ATTRIBUTE.getAttribute())
                .anyMatch(percentile::equals);

        if (!isPercentileLegal) {
            return Result.buildFrom(ResultStatus.PARAM_ILLEGAL);
        }

Z
zengqiao 已提交
156 157 158 159 160 161
        BaseMetrics metrics = topicService.getTopicMetricsFromJMX(
                physicalClusterId,
                topicName,
                KafkaMetricsCollections.TOPIC_REQUEST_TIME_DETAIL_PAGE_METRICS,
                false
        );
Z
zengqiao 已提交
162
        return new Result<>(TopicModelConverter.convert2TopicRequestTimeDetailVOList(metrics, percentile));
Z
zengqiao 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    }

    @ApiOperation(value = "Topic历史请求耗时信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/request-time-history", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicRequestTimeVO>> getTopicRequestMetrics(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        return new Result<>(TopicModelConverter.convert2TopicRequestTimeMetricsVOList(
                topicService.getTopicRequestMetricsFromDB(
                        physicalClusterId,
                        topicName,
                        new Date(startTime),
                        new Date(endTime)
                ))
        );
    }

    @ApiOperation(value = "Topic连接信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/connections", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicConnectionVO>> getTopicConnections(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

Z
zengqiao 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        List<TopicConnection> connections;

        if (ValidateUtils.isBlank(appId)) {
            connections = connectionService.getByTopicName(
                    physicalClusterId,
                    topicName,
                    new Date(System.currentTimeMillis() - Constant.TOPIC_CONNECTION_LATEST_TIME_MS),
                    new Date()
            );
        } else {
            connections = connectionService.getByTopicName(
                    physicalClusterId,
                    topicName,
                    appId,
                    new Date(System.currentTimeMillis() - Constant.TOPIC_CONNECTION_LATEST_TIME_MS),
                    new Date()
            );
        }

        return new Result<>(TopicModelConverter.convert2TopicConnectionVOList(connections));
Z
zengqiao 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    }

    @ApiOperation(value = "Topic分区信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/partitions", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicPartitionVO>> getTopicPartitions(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

        ClusterDO clusterDO = clusterService.getById(physicalClusterId);
        if (clusterDO == null || !PhysicalClusterMetadataManager.isTopicExist(physicalClusterId, topicName)) {
            return Result.buildFrom(ResultStatus.TOPIC_NOT_EXIST);
        }
        List<TopicPartitionDTO> dtoList = topicService.getTopicPartitionDTO(clusterDO, topicName, true);
        return new Result<>(TopicModelConverter.convert2TopicPartitionVOList(dtoList));
    }

    @ApiOperation(value = "Topic采样信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/sample", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<TopicDataSampleVO>> previewTopic(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestBody TopicDataSampleDTO reqObj) {
        reqObj.adjustConfig();

        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, reqObj.getIsPhysicalClusterId());
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

        ClusterDO clusterDO = clusterService.getById(physicalClusterId);
        if (ValidateUtils.isNull(clusterDO)
                || !PhysicalClusterMetadataManager.isTopicExist(physicalClusterId, topicName)) {
            return Result.buildFrom(ResultStatus.TOPIC_NOT_EXIST);
        }

        List<String> dataList = topicService.fetchTopicData(clusterDO, topicName, reqObj);
        if (ValidateUtils.isNull(dataList)) {
            return Result.buildFrom(ResultStatus.OPERATION_FAILED);
        }
        return new Result<>(TopicModelConverter.convert2TopicDataSampleVOList(dataList));
    }

    @ApiOperation(value = "Topic账单信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/bills", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicBillVO>> getTopicBills(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam("startTime") Long startTime,
            @RequestParam("endTime") Long endTime,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        List<KafkaBillDO> kafkaBillDOList =
                kafkaBillService.getByTopicName(physicalClusterId, topicName, new Date(startTime), new Date(endTime));

        List<TopicBillVO> voList = new ArrayList<>();
        for (KafkaBillDO kafkaBillDO: kafkaBillDOList) {
            TopicBillVO vo = new TopicBillVO();
            vo.setQuota(kafkaBillDO.getQuota().longValue());
            vo.setCost(kafkaBillDO.getCost());
            vo.setGmtMonth(kafkaBillDO.getGmtDay());
            voList.add(vo);
        }
        return new Result<>(voList);
    }

    @ApiOperation(value = "获取Topic业务信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/business", method = RequestMethod.GET)
    @ResponseBody
    public Result<TopicBusinessInfoVO> getTopicBusinessInfo(
            @PathVariable Long clusterId,
            @PathVariable String topicName,
            @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        return new Result<>(TopicModelConverter.convert2TopicBusinessInfoVO(
                topicManagerService.getTopicBusinessInfo(physicalClusterId, topicName)
        ));
    }

    @ApiOperation(value = "Topic有权限的应用信息", notes = "")
    @RequestMapping(value = {"{clusterId}/topics/{topicName}/apps"}, method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicAuthorizedAppVO>> getTopicAuthorizedApps(@PathVariable Long clusterId,
                                                                     @PathVariable String topicName,
                                                                     @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }
        return new Result<>(TopicModelConverter.convert2TopicAuthorizedAppVOList(
                topicManagerService.getTopicAuthorizedApps(physicalClusterId, topicName))
        );
    }

    @ApiOperation(value = "Topic我的应用信息", notes = "")
    @RequestMapping(value = {"{clusterId}/topics/{topicName}/my-apps"}, method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TopicMyAppVO>> getTopicMyApps(@PathVariable Long clusterId,
                                                     @PathVariable String topicName,
                                                     @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

        return new Result<>(TopicModelConverter.convert2TopicMineAppVOList(
                topicManagerService.getTopicMineApps(physicalClusterId, topicName, SpringTool.getUserName()))
        );
    }

Z
zengqiao 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    @ApiOperation(value = "Topic流量统计信息", notes = "")
    @RequestMapping(value = "{clusterId}/topics/{topicName}/statistic-metrics", method = RequestMethod.GET)
    @ResponseBody
    public Result<TopicStatisticMetricsVO> getTopicStatisticMetrics(@PathVariable Long clusterId,
                                                                    @PathVariable String topicName,
                                                                    @RequestParam(value = "isPhysicalClusterId", required = false) Boolean isPhysicalClusterId,
                                                                    @RequestParam("latest-day") Integer latestDay) {
        Long physicalClusterId = logicalClusterMetadataManager.getPhysicalClusterId(clusterId, isPhysicalClusterId);
        if (ValidateUtils.isNull(physicalClusterId)) {
            return Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST);
        }

        Double maxAvgBytesIn = topicManagerService.getTopicMaxAvgBytesIn(physicalClusterId, topicName, new Date(DateUtils.getDayStarTime(-1 * latestDay)), new Date(), 1);
        if (ValidateUtils.isNull(maxAvgBytesIn)) {
            return Result.buildFrom(ResultStatus.MYSQL_ERROR);
        }
        return new Result<>(new TopicStatisticMetricsVO(maxAvgBytesIn));
    }

Z
zengqiao 已提交
363
}