Merge pull request #9518 from dataease/pr@dev-v2@feat_inner-template

feat(仪表板): 增加内嵌仪表板相关功能
This commit is contained in:
王嘉豪 2024-05-08 09:05:34 +08:00 committed by GitHub
commit 0fb8a95981
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 456 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package io.dataease.listener;
import io.dataease.license.utils.LogUtil;
import io.dataease.template.manage.TemplateLocalParseManage;
import jakarta.annotation.Resource;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value = 3)
public class TemplateInitListener implements ApplicationListener<ApplicationReadyEvent> {
@Resource
private TemplateLocalParseManage templateLocalParseManage;
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
LogUtil.info("=====Template init from code [Start]=====");
try{
templateLocalParseManage.doInit();
}catch (Exception e){
LogUtil.error("=====Template init from code ERROR=====");
}
LogUtil.info("=====Template init from code [End]=====");
}
}

View File

@ -0,0 +1,137 @@
package io.dataease.template.dao.auto.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
*
* </p>
*
* @author fit2cloud
* @since 2024-05-07
*/
@TableName("de_template_version")
public class DeTemplateVersion implements Serializable {
private static final long serialVersionUID = 1L;
@TableId("installed_rank")
private Integer installedRank;
private String version;
private String description;
private String type;
private String script;
private Integer checksum;
private String installedBy;
private LocalDateTime installedOn;
private Integer executionTime;
private Boolean success;
public Integer getInstalledRank() {
return installedRank;
}
public void setInstalledRank(Integer installedRank) {
this.installedRank = installedRank;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
public Integer getChecksum() {
return checksum;
}
public void setChecksum(Integer checksum) {
this.checksum = checksum;
}
public String getInstalledBy() {
return installedBy;
}
public void setInstalledBy(String installedBy) {
this.installedBy = installedBy;
}
public LocalDateTime getInstalledOn() {
return installedOn;
}
public void setInstalledOn(LocalDateTime installedOn) {
this.installedOn = installedOn;
}
public Integer getExecutionTime() {
return executionTime;
}
public void setExecutionTime(Integer executionTime) {
this.executionTime = executionTime;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
@Override
public String toString() {
return "DeTemplateVersion{" +
"installedRank = " + installedRank +
", version = " + version +
", description = " + description +
", type = " + type +
", script = " + script +
", checksum = " + checksum +
", installedBy = " + installedBy +
", installedOn = " + installedOn +
", executionTime = " + executionTime +
", success = " + success +
"}";
}
}

View File

@ -0,0 +1,18 @@
package io.dataease.template.dao.auto.mapper;
import io.dataease.template.dao.auto.entity.DeTemplateVersion;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author fit2cloud
* @since 2024-05-07
*/
@Mapper
public interface DeTemplateVersionMapper extends BaseMapper<DeTemplateVersion> {
}

View File

@ -0,0 +1,98 @@
package io.dataease.template.manage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dataease.api.visualization.request.DataVisualizationBaseRequest;
import io.dataease.license.utils.LogUtil;
import io.dataease.template.dao.auto.entity.DeTemplateVersion;
import io.dataease.template.dao.auto.mapper.DeTemplateVersionMapper;
import io.dataease.utils.JsonUtil;
import io.dataease.visualization.server.StaticResourceServer;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.repository.init.ResourceReader;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* @author : WangJiaHao
* @date : 2024/5/7
*/
@Service
public class TemplateLocalParseManage {
@Resource
private StaticResourceServer staticResourceServer;
@Resource
private DeTemplateVersionMapper deTemplateVersionMapper;
@Resource(type = ResourceLoader.class)
private ResourceLoader resourceLoader;
public void doInit() throws Exception {
org.springframework.core.io.Resource[] templateFiles = getAllFilesInResourceDirectory("template");
if (templateFiles != null && templateFiles.length > 0) {
for (int i = 0; i < templateFiles.length; i++) {
org.springframework.core.io.Resource templateFile = templateFiles[i];
String templateName = templateFile.getFilename();
QueryWrapper<DeTemplateVersion> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("script", templateName);
if (!deTemplateVersionMapper.exists(queryWrapper)) {
DeTemplateVersion version = new DeTemplateVersion();
version.setScript(templateName);
version.setInstalledOn(LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES));
try {
String content = new String(templateFile.getInputStream().readAllBytes());;
DataVisualizationBaseRequest template = JsonUtil.parseObject(content, DataVisualizationBaseRequest.class);
parseCore(template);
version.setSuccess(true);
deTemplateVersionMapper.insert(version);
} catch (Exception e) {
LogUtil.error("De Template Version Error : " + templateName);
version.setSuccess(false);
deTemplateVersionMapper.insert(version);
break;
}
}
}
}
}
public void parseCore(DataVisualizationBaseRequest template) {
// 解析静态文件并保存
staticResourceServer.saveFilesToServe(template.getStaticResource());
}
public org.springframework.core.io.Resource[] getAllFilesInResourceDirectory(String directoryName) throws Exception {
// 创建一个 PathMatchingResourcePatternResolver 对象
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
// 获取 classpath template 目录下所有文件的 Resource 数组
org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:template/*");
return resources;
}
public static String readFileContent(File file) throws IOException {
StringBuilder content = new StringBuilder();
try (InputStream inputStream = Files.newInputStream(file.toPath());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line);
}
}
return content.toString();
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long