Skip to content

Commit

Permalink
[manager, web-app] Support Monitor config export and import (#863)
Browse files Browse the repository at this point in the history
* [manager] feat support import and export monitor config

* [manager] feat support import and export monitor config

* [manager] fix import and export button not danger

* [manager] fix export response can be void

* [web-app] working on export monitor

* bugfix get blob error

* update import export monitors ui

* Optimize import and export

* fix PMD check

* fix PMD check

---------
  • Loading branch information
gcdd1993 authored Apr 10, 2023
1 parent e560504 commit ebc6371
Show file tree
Hide file tree
Showing 18 changed files with 654 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* Monitoring management API
* 监控管理API
*
* @author tomsun28
* @date 2021/11/14 10:57
*/
Expand Down Expand Up @@ -118,21 +116,22 @@ public ResponseEntity<Message<Void>> detectMonitor(@Valid @RequestBody MonitorDt
}

@PostMapping("/optional")
@Operation(summary = "Add a monitor that can select metrics",description = "新增一个可选指标的监控器")
public ResponseEntity<Message<Void>> addNewMonitorOptionalMetrics(@Valid @RequestBody MonitorDto monitorDto){
@Operation(summary = "Add a monitor that can select metrics", description = "新增一个可选指标的监控器")
public ResponseEntity<Message<Void>> addNewMonitorOptionalMetrics(@Valid @RequestBody MonitorDto monitorDto) {
monitorService.validate(monitorDto, false);
if (monitorDto.isDetected()) {
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
}
monitorService.addNewMonitorOptionalMetrics(monitorDto.getMetrics(),monitorDto.getMonitor(),monitorDto.getParams());
monitorService.addNewMonitorOptionalMetrics(monitorDto.getMetrics(), monitorDto.getMonitor(), monitorDto.getParams());
return ResponseEntity.ok(new Message<>("Add success"));
}

@GetMapping(value = {"/metric/{app}","/metric"})
@GetMapping(value = {"/metric/{app}", "/metric"})
@Operation(summary = "get app metric", description = "根据app名称获取该app可监控指标,不传为获取全部指标")
public ResponseEntity<Message<List<String>>> getMonitorMetrics(
@PathVariable(value = "app",required = false) String app) {
@PathVariable(value = "app", required = false) String app) {
List<String> metricNames = monitorService.getMonitorMetrics(app);
return ResponseEntity.ok(new Message<>(metricNames));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@

package org.dromara.hertzbeat.manager.controller;

import org.dromara.hertzbeat.common.entity.dto.Message;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.manager.service.MonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.hertzbeat.common.entity.dto.Message;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.manager.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* Monitor and manage batch API
* 监控管理批量API
*
* @author tom
* @date 2021/12/1 20:43
*/
Expand Down Expand Up @@ -170,4 +170,19 @@ public ResponseEntity<Message<Void>> enableManageMonitors(
Message<Void> message = new Message<>();
return ResponseEntity.ok(message);
}

@GetMapping("/export")
@Operation(summary = "export monitor config", description = "导出监控配置")
public void export(@RequestParam List<Long> ids, @RequestParam(defaultValue = "JSON") String type,
HttpServletResponse res) throws IOException {
monitorService.export(ids, type, res);
}

@PostMapping("/import")
@Operation(summary = "import monitor config", description = "导入监控配置")
public ResponseEntity<Message<Void>> export(MultipartFile file) throws IOException {
monitorService.importConfig(file);
return ResponseEntity.ok(new Message<>("Import success"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

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

/**
* tag repository
*
* @author tom
* @date 2022/5/1 13:55
*/
public interface TagDao extends JpaRepository<Tag, Long>, JpaSpecificationExecutor<Tag> {

/**
* delete tags by tag id
*
* @param ids id list
*/
void deleteTagsByIdIn(Set<Long> ids);

/**
* find tags by tag id
*
* @param ids id list
* @return tag list
*/
List<Tag> findByIdIn(Set<Long> ids);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.dromara.hertzbeat.manager.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
* Configuration Import Export
* 配置导入导出
*
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a>
* Created by gcdd1993 on 2023/3/31
*/
public interface ImExportService {

/**
* Import Configuration
* 导入配置
*
* @param is 输入流
*/
void importConfig(InputStream is);

/**
* Export Configuration
* 导出配置
*
* @param os 输出流
* @param configList 配置列表
*/
void exportConfig(OutputStream os, List<Long> configList);

/**
* Export file type
* 导出文件类型
*
* @return 文件类型
*/
String type();

/**
* Get Export File Name
* 获取导出文件名
*
* @return 文件名
*/
String getFileName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

package org.dromara.hertzbeat.manager.service;

import org.dromara.hertzbeat.manager.pojo.dto.AppCount;
import org.dromara.hertzbeat.manager.pojo.dto.MonitorDto;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.entity.manager.Param;
import org.dromara.hertzbeat.manager.pojo.dto.AppCount;
import org.dromara.hertzbeat.manager.pojo.dto.MonitorDto;
import org.dromara.hertzbeat.manager.support.exception.MonitorDetectException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -169,17 +172,39 @@ public interface MonitorService {

/**
* 新增一个可选指标的监控
* @param metrics 用户指标
* @param monitor 监控提示
* @param params 配置参数
*
* @param metrics 用户指标
* @param monitor 监控提示
* @param params 配置参数
*/
void addNewMonitorOptionalMetrics(List<String> metrics, Monitor monitor, List<Param> params);

/**
* 根据App名称获取可监控指标,不传为获取全部指标
*
* @param app app name
* @return metrics
*/
List<String> getMonitorMetrics(String app);

/**
* Export Monitoring Configuration
* 导出监控配置
*
* @param ids 监控配置ID列表
* @param type 文件类型
* @param res response
* @throws IOException This exception will be thrown if the export fails
*/
void export(List<Long> ids, String type, HttpServletResponse res) throws IOException;

/**
* Import Monitoring Configuration
* 导入监控配置
*
* @param file 配置文件
* @throws IOException This exception will be thrown if the export fails
*/
void importConfig(MultipartFile file) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 标签服务
Expand All @@ -35,27 +36,40 @@ public interface TagService {

/**
* new tags
*
* @param tags tag
*/
void addTags(List<Tag> tags);

/**
* update tag
*
* @param tag Tag
*/
void modifyTag(Tag tag);

/**
* get tag page list
*
* @param specification 查询条件
* @param pageRequest 分页条件
* @param pageRequest 分页条件
* @return Tags
*/
Page<Tag> getTags(Specification<Tag> specification, PageRequest pageRequest);

/**
* delete tags
*
* @param ids tag id list
*/
void deleteTags(HashSet<Long> ids);

/**
* list tags
*
* @param ids tag id list
* @return tag list
*/
List<Tag> listTag(Set<Long> ids);

}
Loading

0 comments on commit ebc6371

Please sign in to comment.