Skip to content

Commit

Permalink
修改 完善输出节点相关
Browse files Browse the repository at this point in the history
  • Loading branch information
kkangert committed Jul 22, 2024
1 parent 5e5c52c commit d587bcb
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public enum OutputType {
* 数据库输出
*/
DATABASE("output-database"),


/**
* UI输出,主要用于测试
*/
UI("output-ui"),

/**
* CSV文件输出
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public class SpiderContext {
@Setter
private Long flowId;

/**
* 任务 ID
*/
@Getter
@Setter
private Long taskId;

/**
* Cookie 上下文
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* 当前执行器节点的配置项(用于前端动态渲染该节点的配置项)
*/
@Data
@AllArgsConstructor
public class ConfigItem {
/**
* 输入项标签名称
Expand Down Expand Up @@ -64,6 +63,12 @@ public class ConfigItem {

public ConfigItem(String labelName, ComponentType componentType, DataType dataType, String propName,
String placeholder, Object value, Map<String, Object> attributes, List<SelectItem> childrenItem) {
this(labelName, componentType, dataType, propName, placeholder, value, attributes, childrenItem, false);
}

public ConfigItem(String labelName, ComponentType componentType, DataType dataType, String propName,
String placeholder, Object value, Map<String, Object> attributes, List<SelectItem> childrenItem,
Boolean required) {
this.labelName = labelName;
this.componentType = componentType;
this.dataType = dataType;
Expand All @@ -72,6 +77,7 @@ public ConfigItem(String labelName, ComponentType componentType, DataType dataTy
this.value = value;
this.attributes = attributes;
this.childrenItem = childrenItem;
this.required = required;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static SpiderJobContext create(String workspace, Long flowId, Long taskId
OutputStream os = new FileOutputStream(file, true);
SpiderJobContext context = new SpiderJobContext(os, allowOutput);
context.setFlowId(flowId);
context.setTaskId(taskId);
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import top.kangert.kspider.constant.Constants;
import top.kangert.kspider.constant.OutputType;
import top.kangert.kspider.context.SpiderContext;
import top.kangert.kspider.executor.NodeExecutor;
import top.kangert.kspider.executor.node.event.OutputEventPublisher;
import top.kangert.kspider.io.SpiderResponse;
import top.kangert.kspider.listener.SpiderListener;
import top.kangert.kspider.model.ConfigItem;
import top.kangert.kspider.model.Shape;
import top.kangert.kspider.model.SpiderNode;
import top.kangert.kspider.model.SpiderOutput;
Expand All @@ -30,6 +32,11 @@
@Slf4j
public class OutputExecutor implements NodeExecutor, SpiderListener {

/**
* 输出方式
*/
String OUTPUT_TYPE = "output-type";

/**
* 输出其他变量
*/
Expand Down Expand Up @@ -161,6 +168,40 @@ public Shape shape() {
return new Shape(supportType(), "输出", "输出", "ele-DArrowRight", "此节点将所有SpiderOutput数据输出");
}

@Override
public List<ConfigItem> configItems() {
List<ConfigItem> configItemList = new ArrayList<>();
List<ConfigItem.SelectItem> outputTypeConfigItem = new ArrayList<>();
// 输出至数据库
ConfigItem.SelectItem dbItem = new ConfigItem.SelectItem("数据库(TODO)", OutputType.DATABASE.getVariableName(), ConfigItem.DataType.STRING);
outputTypeConfigItem.add(dbItem);

// 输出至UI
ConfigItem.SelectItem uiItem = new ConfigItem.SelectItem("UI(TODO)", OutputType.UI.getVariableName(),ConfigItem.DataType.STRING);
outputTypeConfigItem.add(uiItem);

// 输出至CSV
ConfigItem.SelectItem csvItem = new ConfigItem.SelectItem("CSV", OutputType.CSV.getVariableName(),ConfigItem.DataType.STRING);
outputTypeConfigItem.add(csvItem);

// 输出方式
ConfigItem outputType = new ConfigItem("输出方式", ConfigItem.ComponentType.EL_MULT_SELECT, ConfigItem.DataType.LIST_STRING, OUTPUT_TYPE, "请选择输出方式", Arrays.asList(OutputType.CSV.getVariableName()), null, outputTypeConfigItem, true);
configItemList.add(outputType);

// 输出文件名称(输出方式为CSV时生效)
ConfigItem csvName = new ConfigItem("文件名称", ConfigItem.ComponentType.EL_INPUT, ConfigItem.DataType.LIST_STRING, OUTPUT_TYPE, "请输入CSV文件名称", "", null, outputTypeConfigItem, false);
configItemList.add(csvName);

// 是否输出所有变量
List<ConfigItem.SelectItem> outputAllVar = new ArrayList<>();
outputAllVar.add(new ConfigItem.SelectItem("是", "true", ConfigItem.DataType.BOOLEAN));
outputAllVar.add(new ConfigItem.SelectItem("否", "false", ConfigItem.DataType.BOOLEAN));
ConfigItem outputAllVarConfigItem = new ConfigItem("输出全部参数", ConfigItem.ComponentType.EL_SWITCH, ConfigItem.DataType.BOOLEAN, OUTPUT_OTHERS, "", false, null, outputAllVar);
configItemList.add(outputAllVarConfigItem);

return configItemList;
}

private void releasePrinter(String key) {
log.debug("release printer:key = {}", key);
for (Iterator<Map.Entry<String, CSVPrinter>> iterator = cachePrinter.entrySet().iterator(); iterator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.stereotype.Component;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
Expand Down Expand Up @@ -117,6 +118,10 @@ private void outputCSV(OutputEventBean eventBean) {

// 获取文件名
String csvName = node.getJsonProperty(OUTPUT_CSV_NAME);
if (StrUtil.isBlank(csvName)) {
// 生成一个不重复的hash字符串
csvName = StrUtil.uuid().substring(0, RandomUtil.randomInt(8, 12));
}
if (outputItems == null || outputItems.isEmpty()) {
return;
}
Expand All @@ -135,8 +140,9 @@ private void outputCSV(OutputEventBean eventBean) {
printer = cachePrinter.get(key);
if (printer == null) {
CSVFormat format = CSVFormat.DEFAULT.withHeader(headers.toArray(new String[headers.size()]));
FileOutputStream os = new FileOutputStream(
spiderConfig.getWorkspace() + File.separator + csvName);
String fileName = spiderConfig.getWorkspace() + File.separator + "files" + File.separator
+ context.getFlowId() + "_" + context.getTaskId() + File.separator + csvName + ".csv";
FileOutputStream os = new FileOutputStream(fileName);
String csvEncoding = node.getJsonProperty(OUTPUT_CSV_ENCODING);
if ("UTF-8BOM".equals(csvEncoding)) {
csvEncoding = csvEncoding.substring(0, 5);
Expand Down

0 comments on commit d587bcb

Please sign in to comment.