Skip to content

Commit

Permalink
Merge pull request #251 from xuezechao1/issue_247
Browse files Browse the repository at this point in the history
backend添加展示agent插件界面
  • Loading branch information
fuziye01 authored Dec 20, 2021
2 parents f8d492b + 5d418c1 commit cd23a0d
Show file tree
Hide file tree
Showing 54 changed files with 39,258 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<compiler.plugin.version>3.8.1</compiler.plugin.version>
<source.plugin.version>3.0.1</source.plugin.version>
<jar.plugin.version>3.2.0</jar.plugin.version>
<resources.plugin.version>3.2.0</resources.plugin.version>
<resources.plugin.version>3.1.0</resources.plugin.version>
<shade.plugin.version>3.2.4</shade.plugin.version>
<assembly.plugin.version>2.5.3</assembly.plugin.version>
<grpc.version>1.36.1</grpc.version>
Expand Down
20 changes: 20 additions & 0 deletions sermant-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>

<resource>
<directory>src/main/webapp/</directory>
<excludes>
<exclude>frontend/node_modules/**</exclude>
</excludes>
</resource>
</resources>

</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public class KafkaConf {
@Value("${kafka.session.timeout.ms}")
private String kafkaSessionTimeoutMs;

@Value("${fetch.min.bytes}")
private String kafkaFetchMinBytes;

@Value("${fetch.max.wait.ms}")
private String kafkaFetchMaxWaitMs;

@Value("${kafka.key.serializer}")
private String kafkaKeySerializer;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.huawei.sermant.backend.common.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/frontend/dist/");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(false)
.allowedMethods("POST","GET","DELETE","PUT","OPTIONS")
.allowedOrigins("*")
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Getter
@Setter
Expand All @@ -33,5 +34,5 @@ public class AgentInfo {

private Object heartbeatTime;

private List<PluginInfo> pluginsInfos;
private Map<String, String> pluginsMap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.huawei.sermant.backend.entity;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class HeartbeatEntity {

private String app;

private String hostname;

private Long heartbeatVersion;

private String pluginVersion;

private Long lastHeartbeat;

private String pluginName;

private String appType;

private List<String> ip;

private String version;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private void setConsumerConf(KafkaConf conf) throws KafkaTopicException {
properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, conf.getKafkaAutoCommitIntervalMs());
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, conf.getKafkaAutoOffsetReset());
properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, conf.getKafkaSessionTimeoutMs());
properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, conf.getKafkaFetchMinBytes());
properties.put(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG, conf.getKafkaFetchMaxWaitMs());
consumer = new KafkaConsumer<String, String>(properties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
import com.huawei.sermant.backend.entity.AddressType;
import com.huawei.sermant.backend.entity.AgentInfo;
import com.huawei.sermant.backend.entity.HeartBeatResult;
import com.huawei.sermant.backend.entity.HeartbeatEntity;
import com.huawei.sermant.backend.entity.MonitorItem;
import com.huawei.sermant.backend.entity.PluginInfo;
import com.huawei.sermant.backend.entity.Protocol;
import com.huawei.sermant.backend.entity.PublishConfigEntity;
import com.huawei.sermant.backend.entity.RegisterResult;
import com.huawei.sermant.backend.kafka.KafkaConsumerManager;
import com.huawei.sermant.backend.service.dynamicconfig.DynamicConfigurationFactoryServiceImpl;
import com.huawei.sermant.backend.service.dynamicconfig.service.DynamicConfigurationService;
import com.huawei.sermant.backend.service.dynamicconfig.utils.LabelGroupUtils;
import com.huawei.sermant.backend.util.DateUtil;
import com.huawei.sermant.backend.util.RandomUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
Expand All @@ -48,6 +49,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Component
Expand All @@ -67,8 +69,11 @@ public class HttpServer {
private final String DEFAULT_PLUGIN_VERSION = "unknown";
private final String SUCCESS = "success";
private final String FAILED = "failed";
private final Integer DEFAULT_IP_INDEX = 0;
private final Integer NULL_IP_LENGTH = 0;

RandomUtil RANDOM_UTIL = new RandomUtil();
DateUtil DATE_UTIL = new DateUtil();
private final Integer MIN = 1;
private final Integer MAX = 10;

Expand Down Expand Up @@ -109,24 +114,7 @@ public String invokePost() {
@GetMapping("/getPluginsInfo")
public String invokeGet() {
ConsumerRecords<String, String> consumerRecords = getHeartbeatInfo();
AgentInfo agentInfo = new AgentInfo();
Map<String, PluginInfo> pluginCache = new HashMap<String, PluginInfo>();
for (ConsumerRecord<String, String> record : consumerRecords) {
HashMap hashMap = JSON.parseObject(record.value(), HashMap.class);
agentInfo.setIp(hashMap.get("ip"));
agentInfo.setHeartbeatTime(hashMap.get("heartbeatVersion"));
agentInfo.setLastHeartbeatTime(hashMap.get("lastHeartbeat"));
agentInfo.setVersion(hashMap.get("version"));
String name = (String) hashMap.getOrDefault("pluginName", DEFAULT_AGENT_NAME);
if (!name.equals(DEFAULT_AGENT_NAME)) {
PluginInfo pluginInfo = new PluginInfo();
pluginInfo.setName(name);
pluginInfo.setVersion((String) hashMap.getOrDefault("pluginVersion", DEFAULT_PLUGIN_VERSION));
pluginCache.put(name, pluginInfo);
}
}
agentInfo.setPluginsInfos(new ArrayList<>(pluginCache.values()));
return JSONObject.toJSONString(agentInfo);
return JSONObject.toJSONString(getPluginsInfo(consumerRecords));
}

@PostMapping("/publishConfig")
Expand Down Expand Up @@ -177,4 +165,40 @@ private ConsumerRecords<String, String> getHeartbeatInfo() {
}
return consumerRecords;
}

private String pluginMapToStr(Map<String, String> map) {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet()) {
result.append("\n").append(entry.getKey()).append("-").append(entry.getValue());
}
return result.toString();
}

private List<AgentInfo> getPluginsInfo(ConsumerRecords<String, String> consumerRecords) {
Map<String, AgentInfo> agentMap = new HashMap<>();
for (ConsumerRecord<String, String> record : consumerRecords) {
HeartbeatEntity heartbeatEntity = JSON.parseObject(record.value(), HeartbeatEntity.class);
List<String> ips = heartbeatEntity.getIp();
if (ips == null || ips.size() == NULL_IP_LENGTH) {
continue;
}
String ip = ips.get(DEFAULT_IP_INDEX);
if (agentMap.get(ip) == null) {
AgentInfo agentInfo = new AgentInfo();
agentInfo.setIp(ip);
agentInfo.setVersion(heartbeatEntity.getVersion());
agentInfo.setLastHeartbeatTime(DATE_UTIL.getFormatDate(heartbeatEntity.getLastHeartbeat()));
agentInfo.setHeartbeatTime(DATE_UTIL.getFormatDate(heartbeatEntity.getHeartbeatVersion()));
agentInfo.setPluginsMap(new HashMap<String, String>());
agentMap.put(ips.get(DEFAULT_IP_INDEX), agentInfo);
}
if (agentMap.get(ip) != null && heartbeatEntity.getPluginName() != null) {
AgentInfo agentInfo = agentMap.get(ip);
Map<String, String> pluginMap = agentInfo.getPluginsMap();
pluginMap.put(heartbeatEntity.getPluginName(), heartbeatEntity.getPluginVersion());
agentInfo.setPluginsMap(pluginMap);
}
}
return new ArrayList<>(agentMap.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.huawei.sermant.backend.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
public String getFormatDate(Long times){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(times);
}
}
4 changes: 3 additions & 1 deletion sermant-backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ kafka.value.deserializer=org.apache.kafka.common.serialization.StringDeserialize
kafka.group.id=test
kafka.enable.auto.commit=true
kafka.auto.commit.interval.ms=1000
kafka.auto.offset.reset=earliest
kafka.auto.offset.reset=latest
kafka.session.timeout.ms=30000
fetch.min.bytes=1048576
fetch.max.wait.ms=2000

kafka.key.serializer=org.apache.kafka.common.serialization.StringSerializer
kafka.value.serializer=org.apache.kafka.common.serialization.ByteArraySerializer
Expand Down
18 changes: 18 additions & 0 deletions sermant-backend/src/main/webapp/frontend/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2"
],
"plugins": ["transform-vue-jsx", "transform-runtime"],
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"]
}
}
}
9 changes: 9 additions & 0 deletions sermant-backend/src/main/webapp/frontend/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
5 changes: 5 additions & 0 deletions sermant-backend/src/main/webapp/frontend/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/build/
/config/
/dist/
/*.js
/test/unit/coverage/
29 changes: 29 additions & 0 deletions sermant-backend/src/main/webapp/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://eslint.org/docs/user-guide/configuring

module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint'
},
env: {
browser: true,
},
extends: [
// https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
// consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
'plugin:vue/essential',
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
'standard'
],
// required to lint *.vue files
plugins: [
'vue'
],
// add your custom rules here
rules: {
// allow async-await
'generator-star-spacing': 'off',
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
}
}
10 changes: 10 additions & 0 deletions sermant-backend/src/main/webapp/frontend/.postcssrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://github.com/michael-ciniawsky/postcss-load-config

module.exports = {
"plugins": {
"postcss-import": {},
"postcss-url": {},
// to edit target browsers: use "browserslist" field in package.json
"autoprefixer": {}
}
}
30 changes: 30 additions & 0 deletions sermant-backend/src/main/webapp/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frontend

> A Vue.js project
## Build Setup

``` bash
# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report

# run unit tests
npm run unit

# run e2e tests
npm run e2e

# run all tests
npm test
```

For a detailed explanation on how things work, check out the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).
Loading

0 comments on commit cd23a0d

Please sign in to comment.