Merge pull request #2145 from dataease/pr@dev@refactor_panel-cache

refactor: 仪表板编辑缓存优化
This commit is contained in:
王嘉豪 2022-04-20 17:26:28 +08:00 committed by GitHub
commit 162b51ee1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 213 additions and 92 deletions

View File

@ -11,14 +11,10 @@ public class PanelConstants {
public final static String COMPONENT_TYPE_PUBLIC = "public";
public final static String TEMPLATE_TYPE_SYSTEM = "system";
public final static String TEMPLATE_TYPE_SELF = "self";
public final static String PANEL_TYPE_SYSTEM = "system";
public final static String PANEL_TYPE_SELF = "self";
public final static String PANEL_NODE_TYPE_FOlDER = "folder";
@ -47,4 +43,26 @@ public class PanelConstants {
}
//仪表板类型
public static final class PANEL_TYPE {
// 普通仪表板
public static final String SELF = "self";
// 默认仪表板
public static final String SYSTEM = "system";
}
//仪表板查询来源
public static final class QUERY_FROM {
// 普通查询
public static final String NORMAL = "normal";
// 编辑查询
public static final String EDIT = "edit";
}
}

View File

@ -42,7 +42,7 @@ public class ChartViewController {
@DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_MANAGE)
@ApiOperation("保存")
@PostMapping("/save/{panelId}")
public ChartViewDTO save(@PathVariable String panelId, @RequestBody ChartViewCacheRequest request) {
public ChartViewDTO save(@PathVariable String panelId, @RequestBody ChartViewRequest request) {
return chartViewService.save(request);
}
@ -54,10 +54,10 @@ public class ChartViewController {
}
@DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_MANAGE)
@ApiOperation("保存到缓存表")
@PostMapping("/save2Cache/{panelId}")
public void save2Cache(@PathVariable String panelId, @RequestBody ChartViewCacheWithBLOBs chartViewWithBLOBs) {
chartViewService.save2Cache(chartViewWithBLOBs);
@ApiOperation("保存编辑的视图信息")
@PostMapping("/viewEditSave/{panelId}")
public void viewEditSave(@PathVariable String panelId, @RequestBody ChartViewWithBLOBs chartViewWithBLOBs) {
chartViewService.viewEditSave(chartViewWithBLOBs);
}
@ApiIgnore
@ -147,12 +147,11 @@ public class ChartViewController {
chartViewService.initViewCache(panelId);
}
@DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_VIEW, paramIndex = 1)
@ApiOperation("重置视图缓存")
@ApiOperation("重置视图")
@PostMapping("/resetViewCache/{id}/{panelId}")
public void resetViewCache(@PathVariable String id, @PathVariable String panelId) {
chartViewCacheService.refreshCache(id);
chartViewCacheService.resetView(id);
}
@ApiOperation("校验视图Title")

View File

@ -0,0 +1,19 @@
package io.dataease.controller.request.panel;
import lombok.Data;
/**
* Author: wangjiahao
* Date: 2022/4/20
* Description:
*/
@Data
public class PanelGroupQueryRequest {
private String panelId;
private String panelType; //仪表板类型 self 普通仪表板 or system默认仪表板
private String queryFrom; //查询来源 edit 编辑初始化查询
}

View File

@ -35,7 +35,7 @@ public interface ExtChartViewMapper {
List<ChartViewDTO> searchViewsWithPanelId(@Param("panelId") String panelId);
ChartViewDTO searchOneFromCache(@Param("id") String id );
// ChartViewDTO searchOneFromCache(@Param("id") String id );
void copyToCache(@Param("id") String id );
@ -47,7 +47,11 @@ public interface ExtChartViewMapper {
int updateToCache(@Param("viewId") String viewId );
int updateToViewFromCache(@Param("viewId") String viewId );
void copyCache(@Param("sourceViewId") String sourceViewId,@Param("newViewId") String newViewId);
void deleteNoUseView(@Param("viewIds") List<String> viewIds,@Param("panelId") String panelId );
void initPanelChartViewCache(@Param("panelId") String panelId);
}

View File

@ -20,12 +20,6 @@
from chart_view where id = #{id}
</select>
<select id="searchOneFromCache" resultMap="BaseResultMapDTO">
select
chart_view_cache.*
from chart_view_cache where id = #{id}
</select>
<insert id="copyToCache">
INSERT INTO chart_view_cache (
id,
@ -152,23 +146,68 @@
chart_view_cache.id = #{sourceViewId}
</insert>
<!-- <select id="searchOne" resultMap="BaseResultMapDTO">-->
<!-- select-->
<!-- id, `name`, scene_id, table_id, `type`, title, create_by, create_time, update_time,-->
<!-- style_priority,x_axis, y_axis, custom_attr, custom_style, custom_filter, snapshot-->
<!-- from chart_view-->
<!-- <where>-->
<!-- <if test="sceneId != null">-->
<!-- and scene_id = #{sceneId,jdbcType=VARCHAR}-->
<!-- </if>-->
<!-- <if test="id != null">-->
<!-- and id = #{id,jdbcType=VARCHAR}-->
<!-- </if>-->
<!-- </where>-->
<!-- <if test="sort != null">-->
<!-- order by ${sort}-->
<!-- </if>-->
<!-- </select>-->
<insert id="initPanelChartViewCache">
INSERT INTO chart_view_cache (
id,
`name`,
title,
scene_id,
table_id,
`type`,
render,
result_count,
result_mode,
create_by,
create_time,
update_time,
style_priority,
chart_type,
is_plugin,
x_axis,
x_axis_ext,
y_axis,
y_axis_ext,
ext_stack,
ext_bubble,
custom_attr,
custom_style,
custom_filter,
drill_fields,
senior,
SNAPSHOT,
data_from
) SELECT
id,
`name`,
title,
scene_id,
table_id,
`type`,
render,
result_count,
result_mode,
create_by,
create_time,
update_time,
style_priority,
chart_type,
is_plugin,
x_axis,
x_axis_ext,
y_axis,
y_axis_ext,
ext_stack,
ext_bubble,
custom_attr,
custom_style,
custom_filter,
drill_fields,
senior,
SNAPSHOT,
data_from from chart_view
WHERE
chart_view.scene_id = #{panelId}
</insert>
<select id="search" resultMap="BaseResultMapDTO">
select
@ -503,6 +542,40 @@
where cve.id = cv.id and cv.id =#{viewId}
</update>
<update id="updateToViewFromCache">
UPDATE chart_view_cache cve,
chart_view cv
SET cv.`name` = cve.`name`,
cv.title = cve.title,
cv.scene_id = cve.scene_id,
cv.table_id = cve.table_id,
cv.`type` = cve.`type`,
cv.render = cve.render,
cv.result_count = cve.result_count,
cv.result_mode = cve.result_mode,
cv.create_by = cve.create_by,
cv.create_time = cve.create_time,
cv.update_time = cve.update_time,
cv.style_priority = cve.style_priority,
cv.chart_type = cve.chart_type,
cv.is_plugin = cve.is_plugin,
cv.x_axis = cve.x_axis,
cv.x_axis_ext = cve.x_axis_ext,
cv.y_axis = cve.y_axis,
cv.y_axis_ext = cve.y_axis_ext,
cv.ext_stack = cve.ext_stack,
cv.ext_bubble = cve.ext_bubble,
cv.custom_attr = cve.custom_attr,
cv.custom_style = cve.custom_style,
cv.custom_filter = cve.custom_filter,
cv.drill_fields = cve.drill_fields,
cv.senior = cve.senior,
cv.SNAPSHOT = cve.SNAPSHOT,
cv.data_from = cve.data_from
where cve.id = cv.id and cv.id =#{viewId}
</update>
<delete id="deleteNoUseView">
delete from chart_view
where scene_id = #{panelId}

View File

@ -34,19 +34,19 @@
<select id="getViewLinkageGather" resultMap="LinkageGatherMap">
SELECT
chart_view_cache.`name` as 'targetViewName',
chart_view_cache.id as 'target_view_id',
chart_view_cache.table_id,
chart_view.`name` as 'targetViewName',
chart_view.id as 'target_view_id',
chart_view.table_id,
(case when panel_view_linkage.target_view_id is null then 0 else 1 end) as 'linkageActive',
panel_view_linkage_field.source_field,
panel_view_linkage_field.target_field
FROM
chart_view_cache
LEFT JOIN panel_view_linkage ON chart_view_cache.id = panel_view_linkage.target_view_id
chart_view
LEFT JOIN panel_view_linkage ON chart_view.id = panel_view_linkage.target_view_id
AND panel_view_linkage.panel_id = #{panelId}
AND panel_view_linkage.source_view_id = #{sourceViewId}
LEFT JOIN panel_view_linkage_field ON panel_view_linkage.id = panel_view_linkage_field.linkage_id
where chart_view_cache.id in
where chart_view.id in
<foreach collection="targetViewIds" item="targetViewId" index="index" open="(" close=")" separator=",">
#{targetViewId}
</foreach>

View File

@ -24,4 +24,8 @@ public class ChartViewCacheService {
}
}
public void resetView(String viewId){
extChartViewMapper.updateToViewFromCache(viewId);
}
}

View File

@ -49,6 +49,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.pentaho.di.core.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@ -104,23 +105,23 @@ public class ChartViewService {
private ReentrantLock lock = new ReentrantLock();
// 直接保存统一到缓存表
public ChartViewDTO save(ChartViewCacheRequest chartView) {
public ChartViewDTO save(ChartViewRequest chartView) {
long timestamp = System.currentTimeMillis();
chartView.setUpdateTime(timestamp);
chartViewCacheMapper.updateByPrimaryKeySelective(chartView);
chartViewMapper.updateByPrimaryKeySelective(chartView);
Optional.ofNullable(chartView.getId()).ifPresent(id -> {
CacheUtils.remove(JdbcConstants.VIEW_CACHE_KEY, id);
});
return getOne(chartView.getId(), "panel_edit");
}
public String checkTitle(ChartViewCacheRequest chartView){
public String checkTitle(ChartViewCacheRequest chartView) {
ChartViewCacheExample example = new ChartViewCacheExample();
example.createCriteria().andTitleEqualTo(chartView.getTitle()).andSceneIdEqualTo(chartView.getSceneId()).andIdNotEqualTo(chartView.getId());
List<ChartViewCache> result = chartViewCacheMapper.selectByExample(example);
if(CollectionUtils.isNotEmpty(result)){
if (CollectionUtils.isNotEmpty(result)) {
return "fail";
}else{
} else {
return "success";
}
}
@ -131,7 +132,7 @@ public class ChartViewService {
ChartViewExample queryExample = new ChartViewExample();
queryExample.createCriteria().andSceneIdEqualTo(chartView.getSceneId()).andNameEqualTo(chartView.getName());
List<ChartView> result = chartViewMapper.selectByExample(queryExample);
if(CollectionUtils.isNotEmpty(result)){
if (CollectionUtils.isNotEmpty(result)) {
DEException.throwException(Translator.get("theme_name_repeat"));
}
chartView.setUpdateTime(timestamp);
@ -155,11 +156,14 @@ public class ChartViewService {
}
// 直接保存统一到缓存表
public void save2Cache(ChartViewCacheWithBLOBs chartView) {
/**
* @Description 保存编辑的视图信息
* @param chartView
*/
public void viewEditSave(ChartViewWithBLOBs chartView) {
long timestamp = System.currentTimeMillis();
chartView.setUpdateTime(timestamp);
chartViewCacheMapper.updateByPrimaryKeySelective(chartView);
chartViewMapper.updateByPrimaryKeySelective(chartView);
Optional.ofNullable(chartView.getId()).ifPresent(id -> {
CacheUtils.remove(JdbcConstants.VIEW_CACHE_KEY, id);
});
@ -250,25 +254,14 @@ public class ChartViewService {
}
@Transactional
public ChartViewDTO getOne(String id, String queryFrom) {
try{
ChartViewDTO result;
if(CommonConstants.VIEW_QUERY_FROM.PANEL_EDIT.equals(queryFrom)) {
//仪表板编辑页面 从缓存表中取数据 缓存表中没有数据则进行插入
result = extChartViewMapper.searchOneFromCache(id);
try {
ChartViewDTO result = extChartViewMapper.searchOne(id);
if (result == null) {
chartViewCacheService.refreshCache(id);
result = extChartViewMapper.searchOneFromCache(id);
}
} else {
result = extChartViewMapper.searchOne(id);
}
if(result==null){
DataEaseException.throwException(Translator.get("i18n_chart_delete"));
}
return result;
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
DataEaseException.throwException(e);
}
@ -437,12 +430,12 @@ public class ChartViewService {
List<ChartExtFilterRequest> filters = new ArrayList<>();
// 联动条件
if(ObjectUtils.isNotEmpty(requestList.getLinkageFilters())){
if (ObjectUtils.isNotEmpty(requestList.getLinkageFilters())) {
filters.addAll(requestList.getLinkageFilters());
}
// 外部参数条件
if(ObjectUtils.isNotEmpty(requestList.getOuterParamsFilters())){
if (ObjectUtils.isNotEmpty(requestList.getOuterParamsFilters())) {
filters.addAll(requestList.getOuterParamsFilters());
}
@ -517,10 +510,10 @@ public class ChartViewService {
// 如果是插件视图 走插件内部的逻辑
if (ObjectUtils.isNotEmpty(view.getIsPlugin()) && view.getIsPlugin()) {
Map<String, List<ChartViewFieldDTO>> fieldMap = new HashMap<>();
fieldMap.put("xAxis",xAxis);
fieldMap.put("yAxis",yAxis);
fieldMap.put("extStack",extStack);
fieldMap.put("extBubble",extBubble);
fieldMap.put("xAxis", xAxis);
fieldMap.put("yAxis", yAxis);
fieldMap.put("extStack", extStack);
fieldMap.put("extBubble", extBubble);
PluginViewParam pluginViewParam = buildPluginParam(fieldMap, fieldCustomFilter, extFilterList, ds, table, view);
String sql = pluginViewSql(pluginViewParam, view);
if (StringUtils.isBlank(sql)) {
@ -532,7 +525,7 @@ public class ChartViewService {
Map<String, Object> mapChart = pluginViewResult(pluginViewParam, view, data, isDrill);
Map<String, Object> mapTableNormal = ChartDataBuild.transTableNormal(xAxis, yAxis, view, data, extStack, desensitizationList);
return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal,view, isDrill, drillFilters);
return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal, view, isDrill, drillFilters);
// 如果是插件到此结束
}
@ -771,7 +764,7 @@ public class ChartViewService {
}
// table组件明细表也用于导出数据
Map<String, Object> mapTableNormal = ChartDataBuild.transTableNormal(xAxis, yAxis, view, data, extStack, desensitizationList);
return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal,view, isDrill, drillFilters);
return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal, view, isDrill, drillFilters);
}
public ChartViewDTO uniteViewResult(String sql, Map<String, Object> chartData, Map<String, Object> tabelData, ChartViewDTO view, Boolean isDrill, List<ChartExtFilterRequest> drillFilters) {
@ -800,8 +793,6 @@ public class ChartViewService {
PluginViewLimit pluginViewLimit = BeanUtils.copyBean(new PluginViewLimit(), view);
List<PluginChartFieldCustomFilter> fieldFilters = customFilters.stream().map(filter -> gson.fromJson(gson.toJson(filter), PluginChartFieldCustomFilter.class)).collect(Collectors.toList());
List<PluginChartExtFilter> panelFilters = extFilters.stream().map(filter -> gson.fromJson(gson.toJson(filter), PluginChartExtFilter.class)).collect(Collectors.toList());
@ -1040,8 +1031,8 @@ public class ChartViewService {
public String chartCopy(String id, String panelId) {
String newChartId = UUID.randomUUID().toString();
extChartViewMapper.chartCopy(newChartId, id, panelId);
extChartViewMapper.copyCache(id,newChartId);
extPanelGroupExtendDataMapper.copyExtendData(id,newChartId,panelId);
extChartViewMapper.copyCache(id, newChartId);
extPanelGroupExtendDataMapper.copyExtendData(id, newChartId, panelId);
chartViewCacheService.refreshCache(newChartId);
return newChartId;
}
@ -1072,8 +1063,14 @@ public class ChartViewService {
}
}
/**
* @param panelId
* @Description 初始化仪表板内部视图的cache表
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void initViewCache(String panelId) {
extChartViewMapper.deleteCacheWithPanel(null,panelId);
extChartViewMapper.deleteCacheWithPanel(null, panelId);
extChartViewMapper.initPanelChartViewCache(panelId);
}
}

View File

@ -8,6 +8,7 @@ import io.dataease.commons.utils.LogUtil;
import io.dataease.commons.utils.TreeUtils;
import io.dataease.controller.request.authModel.VAuthModelRequest;
import io.dataease.controller.request.dataset.DataSetTableRequest;
import io.dataease.controller.request.panel.PanelGroupQueryRequest;
import io.dataease.controller.request.panel.PanelGroupRequest;
import io.dataease.controller.request.panel.PanelViewDetailsRequest;
import io.dataease.dto.PanelGroupExtendDataDTO;
@ -131,7 +132,7 @@ public class PanelGroupService {
} else if ("toDefaultPanel".equals(request.getOptType())) { // 转存为默认仪表板
panelId = UUID.randomUUID().toString();
PanelGroupWithBLOBs newDefaultPanel = panelGroupMapper.selectByPrimaryKey(request.getId());
newDefaultPanel.setPanelType(PanelConstants.PANEL_TYPE_SYSTEM);
newDefaultPanel.setPanelType(PanelConstants.PANEL_TYPE.SYSTEM);
newDefaultPanel.setNodeType(PanelConstants.PANEL_NODE_TYPE_PANEL);
newDefaultPanel.setName(request.getName());
newDefaultPanel.setId(panelId);
@ -221,7 +222,13 @@ public class PanelGroupService {
}
/**
* @Description 查询仪表板信息
* @param panelId
* @return
*/
public PanelGroupDTO findOne(String panelId) {
Assert.notNull(panelId,"Method findOne panelId can not be null");
PanelGroupDTO panelGroup = extPanelGroupMapper.findOneWithPrivileges(panelId, String.valueOf(AuthUtils.getUser().getUserId()));
// 默认仪表板取源仪表板样式
if (panelGroup != null && StringUtils.isNotEmpty(panelGroup.getSource())) {

View File

@ -125,7 +125,7 @@ public class PanelViewService {
extPanelViewMapper.savePanelView(panelViewInsertDTOList);
//将视图从cache表中更新到正式表中
viewIds = panelViewInsertDTOList.stream().map(panelView ->panelView.getChartViewId()).collect(Collectors.toList());
extChartViewMapper.copyCacheToView(viewIds);
// extChartViewMapper.copyCacheToView(viewIds);
}
extChartViewMapper.deleteCacheWithPanel(viewIds,panelId);
extChartViewMapper.deleteNoUseView(viewIds,panelId);

View File

@ -86,9 +86,9 @@ export function getChartDetails(id, panelId, data) {
})
}
export function save2Cache(panelId, data) {
export function viewEditSave(panelId, data) {
return request({
url: '/chart/view/save2Cache/' + panelId,
url: '/chart/view/viewEditSave/' + panelId,
method: 'post',
loading: false,
data

View File

@ -1160,7 +1160,7 @@ import {
ajaxGetDataOnly,
post,
getChartDetails,
save2Cache,
viewEditSave,
resetViewCacheCallBack
} from '@/api/chart/chart'
import DimensionItem from '../components/drag-item/DimensionItem'
@ -1792,7 +1792,7 @@ export default {
this.changeEditStatus(true)
const view = this.buildParam(true, 'chart', false, switchType)
if (!view) return
save2Cache(this.panelInfo.id, view).then(() => {
viewEditSave(this.panelInfo.id, view).then(() => {
bus.$emit('view-in-cache', { type: 'propChange', viewId: this.param.id })
})
},
@ -1819,7 +1819,7 @@ export default {
//
const viewSave = this.buildParam(true, 'chart', false, false)
if (!viewSave) return
save2Cache(this.panelInfo.id, viewSave)
viewEditSave(this.panelInfo.id, viewSave)
bus.$emit('view-in-cache', { type: 'styleChange', viewId: this.param.id, viewInfo: view })
},
@ -2565,10 +2565,10 @@ export default {
},
reset() {
this.changeEditStatus(false)
this.getChart(this.param.id, 'panel')
const _this = this
resetViewCacheCallBack(this.param.id, this.panelInfo.id, function(rsp) {
resetViewCacheCallBack(_this.param.id, _this.panelInfo.id, function(rsp) {
_this.changeEditStatus(false)
_this.getChart(_this.param.id, 'panel')
bus.$emit('view-in-cache', { type: 'propChange', viewId: _this.param.id })
})
},