Skip to content

Commit

Permalink
[feature] Implement custom parameters for plugins (apache#2616)
Browse files Browse the repository at this point in the history
Signed-off-by: liutianyou <[email protected]>
Co-authored-by: shown <[email protected]>
Co-authored-by: Kerwin Bryant <[email protected]>
Co-authored-by: Logic <[email protected]>
Co-authored-by: liutianyou <[email protected]>
  • Loading branch information
5 people authored Aug 29, 2024
1 parent 24678c2 commit 0e49e10
Show file tree
Hide file tree
Showing 17 changed files with 491 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void run() {
// Notice distribution
sendNotify(alert);
// Execute the plugin if enable
pluginService.pluginExecute(Plugin.class, plugin -> plugin.alert(alert));
pluginService.pluginExecute(Plugin.class, plugin -> plugin.alert(alert), (plugin, configMapList) -> plugin.alert(alert, configMapList));
}
} catch (IgnoreException ignored) {
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.hertzbeat.common.entity.dto.Message;
import org.apache.hertzbeat.common.entity.dto.PluginUpload;
import org.apache.hertzbeat.common.entity.manager.PluginMetadata;
import org.apache.hertzbeat.manager.pojo.dto.PluginParam;
import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO;
import org.apache.hertzbeat.manager.service.PluginService;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -85,4 +87,19 @@ public ResponseEntity<Message<Void>> updatePluginStatus(@RequestBody PluginMetad
pluginService.updateStatus(plugin);
return ResponseEntity.ok(Message.success("Update success"));
}

@GetMapping("/params/define")
@Operation(summary = "get param define", description = "get param define by jar path")
public ResponseEntity<Message<PluginParametersVO>> getParamDefine(@RequestParam Long pluginMetadataId) {
PluginParametersVO plugins = pluginService.getParamDefine(pluginMetadataId);
return ResponseEntity.ok(Message.success(plugins));
}

@PostMapping("/params")
@Operation(summary = "get param define", description = "get param define by jar path")
public ResponseEntity<Message<Boolean>> saveParams(@RequestBody List<PluginParam> pluginParams) {
pluginService.savePluginParam(pluginParams);
return ResponseEntity.ok(Message.success(true));
}

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

import java.util.List;
import java.util.Set;
import org.apache.hertzbeat.manager.pojo.dto.PluginParam;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* PluginParamDao database operations
*/
public interface PluginParamDao extends JpaRepository<PluginParam, Long> {

/**
* Query the list of parameters associated with the monitoring ID'
* @param pluginMetadataId Monitor ID
* @return list of parameter values
*/
List<PluginParam> findParamsByPluginMetadataId(Long pluginMetadataId);

/**
* Remove the parameter list associated with the pluginMetadata ID based on it
* @param pluginMetadataId Monitor Id
*/
void deletePluginParamsByPluginMetadataId(long pluginMetadataId);

/**
* Remove the parameter list associated with the pluginMetadata ID list based on it
* @param pluginMetadataIds Monitoring ID List
*/
void deletePluginParamsByPluginMetadataIdIn(Set<Long> pluginMetadataIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.apache.hertzbeat.manager.pojo.dto;

import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY;
import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
* PluginParam
*/
@Entity
@Table(name = "hzb_plugin_param", indexes = { @Index(columnList = "pluginMetadataId") },
uniqueConstraints = @UniqueConstraint(columnNames = {"pluginMetadataId", "field"}))
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "Parameter Entity")
@EntityListeners(AuditingEntityListener.class)
public class PluginParam {

/**
* Parameter primary key index ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Schema(title = "Parameter primary key index ID", example = "87584674384", accessMode = READ_ONLY)
private Long id;
/**
* Monitor ID
*/
@Schema(title = "Plugin task ID", example = "875846754543", accessMode = READ_WRITE)
@NotNull
private Long pluginMetadataId;

/**
* Parameter Field Identifier
*/
@Schema(title = "Parameter identifier field", example = "port", accessMode = READ_WRITE)
@Size(max = 100)
@NotNull
private String field;

/**
* Param Value
*/
@Schema(title = "parameter values", example = "8080", accessMode = READ_WRITE)
@Size(max = 8126)
@Column(length = 8126)
private String paramValue;

/**
* Parameter type 0: number 1: string 2: encrypted string 3: json string mapped by map
*/
@Schema(title = "Parameter types 0: number 1: string 2: encrypted string 3:map mapped json string 4:arrays string",
accessMode = READ_WRITE)
@Min(0)
private byte type;

/**
* Record create time
*/
@Schema(title = "Record create time", example = "1612198922000", accessMode = READ_ONLY)
@CreatedDate
private LocalDateTime gmtCreate;

/**
* Record the latest modification time
*/
@Schema(title = "Record modify time", example = "1612198444000", accessMode = READ_ONLY)
@LastModifiedDate
private LocalDateTime gmtUpdate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.apache.hertzbeat.manager.pojo.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.ParamDefine;
import java.util.List;

/**
* Popup rendering and parameter values
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginParametersVO {

/**
* Stencil rendering
*/
private List<ParamDefine> paramDefines;

/**
* specific parameter
*/
private List<PluginParam> pluginParams;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package org.apache.hertzbeat.manager.service;

import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.apache.hertzbeat.common.entity.dto.PluginUpload;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.manager.PluginMetadata;
import org.apache.hertzbeat.manager.pojo.dto.PluginParam;
import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO;
import org.springframework.data.domain.Page;

/**
Expand Down Expand Up @@ -58,7 +63,7 @@ public interface PluginService {
* @param execute run plugin logic
* @param <T> plugin type
*/
<T> void pluginExecute(Class<T> clazz, Consumer<T> execute);
<T> void pluginExecute(Class<T> clazz, Consumer<T> execute, BiConsumer<T, List<Configmap>> biConsumer);

/**
* delete plugin
Expand All @@ -69,4 +74,16 @@ public interface PluginService {

void updateStatus(PluginMetadata plugin);

/**
* get param define
* @param pluginMetadataId plugin id
*/
PluginParametersVO getParamDefine(Long pluginMetadataId);

/**
* save plugin param
* @param params params
*/
void savePluginParam(List<PluginParam> params);

}
Loading

0 comments on commit 0e49e10

Please sign in to comment.