|
|
|
@ -1,11 +1,8 @@
|
|
|
|
|
package com.fr.design.mainframe.chart.info; |
|
|
|
|
|
|
|
|
|
import com.fr.base.FRContext; |
|
|
|
|
import com.fr.base.io.BaseBook; |
|
|
|
|
import com.fr.base.io.XMLReadHelper; |
|
|
|
|
import com.fr.chartx.attr.ChartProvider; |
|
|
|
|
import com.fr.design.DesignerEnvManager; |
|
|
|
|
import com.fr.design.mainframe.template.info.SendHelper; |
|
|
|
|
import com.fr.design.mainframe.burying.point.AbstractPointCollector; |
|
|
|
|
import com.fr.design.mainframe.template.info.TemplateInfo; |
|
|
|
|
import com.fr.design.mainframe.template.info.TemplateProcessInfo; |
|
|
|
|
import com.fr.general.ComparatorUtils; |
|
|
|
@ -15,46 +12,27 @@ import com.fr.stable.ProductConstants;
|
|
|
|
|
import com.fr.stable.StableUtils; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
import com.fr.stable.xml.XMLPrintWriter; |
|
|
|
|
import com.fr.stable.xml.XMLReadable; |
|
|
|
|
import com.fr.stable.xml.XMLTools; |
|
|
|
|
import com.fr.stable.xml.XMLWriter; |
|
|
|
|
import com.fr.stable.xml.XMLableReader; |
|
|
|
|
import com.fr.third.javax.xml.stream.XMLStreamException; |
|
|
|
|
import com.fr.third.org.apache.commons.io.FileUtils; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Calendar; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Bjorn |
|
|
|
|
* @version 10.0 |
|
|
|
|
* Created by Bjorn on 2020-02-18 |
|
|
|
|
*/ |
|
|
|
|
public class ChartInfoCollector implements XMLReadable, XMLWriter { |
|
|
|
|
public class ChartInfoCollector extends AbstractPointCollector<ChartInfo> { |
|
|
|
|
private static final String XML_TAG = "ChartInfoCollector"; |
|
|
|
|
private static final String XML_LAST_EDIT_DAY = "lastEditDay"; |
|
|
|
|
|
|
|
|
|
private static final String XML_CHART_INFO_LIST = "ChartInfoList"; |
|
|
|
|
private static final String XML_FILE_NAME = "chart.info"; |
|
|
|
|
|
|
|
|
|
private static final int VALID_CELL_COUNT = 5; // 有效报表模板的格子数
|
|
|
|
|
private static final int VALID_WIDGET_COUNT = 5; // 有效报表模板的控件数
|
|
|
|
|
|
|
|
|
|
private static ChartInfoCollector instance; |
|
|
|
|
private static final int MAX_SIZE = 512 * 1024 * 1024; |
|
|
|
|
private Map<String, ChartInfo> chartInfoMap; |
|
|
|
|
|
|
|
|
|
private Map<String, ChartInfo> chartInfoCacheMap; |
|
|
|
|
|
|
|
|
@ -65,9 +43,7 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void init() { |
|
|
|
|
chartInfoMap = new ConcurrentHashMap<>(); |
|
|
|
|
chartInfoCacheMap = new HashMap<>(); |
|
|
|
|
loadFromFile(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static ChartInfoCollector getInstance() { |
|
|
|
@ -149,8 +125,8 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
return chartInfoCacheMap.get(chartId); |
|
|
|
|
} |
|
|
|
|
//缓存中没有从文件中读取的信息中拷贝到缓存
|
|
|
|
|
if (chartInfoMap.containsKey(chartId)) { |
|
|
|
|
ChartInfo chartInfo = chartInfoMap.get(chartId).clone(); |
|
|
|
|
if (pointInfoMap.containsKey(chartId)) { |
|
|
|
|
ChartInfo chartInfo = pointInfoMap.get(chartId).clone(); |
|
|
|
|
chartInfoCacheMap.put(chartId, chartInfo); |
|
|
|
|
return chartInfo; |
|
|
|
|
} |
|
|
|
@ -163,7 +139,8 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
/** |
|
|
|
|
* 保存模板的时候将该模板中的图表埋点信息保存 |
|
|
|
|
*/ |
|
|
|
|
public void collectInfo(String templateId, String originID, TemplateProcessInfo processInfo) { |
|
|
|
|
@Override |
|
|
|
|
public void collectInfo(String templateId, String originID, TemplateProcessInfo processInfo, int timeConsume) { |
|
|
|
|
if (!shouldCollectInfo()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -172,7 +149,7 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
} |
|
|
|
|
boolean testTemplate = isTestTemplate(processInfo); |
|
|
|
|
|
|
|
|
|
for (ChartInfo chartInfo : chartInfoMap.values()) { |
|
|
|
|
for (ChartInfo chartInfo : pointInfoMap.values()) { |
|
|
|
|
if (originID.equals(chartInfo.getTemplateId())) { |
|
|
|
|
chartInfo.setTemplateId(templateId); |
|
|
|
|
chartInfo.setTestTemplate(testTemplate); |
|
|
|
@ -185,7 +162,7 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
originID.equals(chartInfo.getTemplateId())) { |
|
|
|
|
chartInfo.setTemplateId(templateId); |
|
|
|
|
chartInfo.setTestTemplate(testTemplate); |
|
|
|
|
chartInfoMap.put(chartInfo.getChartId(), chartInfo); |
|
|
|
|
pointInfoMap.put(chartInfo.getChartId(), chartInfo); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -203,83 +180,28 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
return TemplateInfo.isTestTemplate(reportType, cellCount, floatCount, blockCount, widgetCount); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 发送本地图表信息到服务器,并清空已发送图表的本地记录 |
|
|
|
|
*/ |
|
|
|
|
public void sendChartInfo() { |
|
|
|
|
|
|
|
|
|
addIdleDayCount(); |
|
|
|
|
|
|
|
|
|
List<String> removeLaterList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
for (String key : chartInfoMap.keySet()) { |
|
|
|
|
ChartInfo chartInfo = chartInfoMap.get(key); |
|
|
|
|
if (chartInfo.isComplete()) { |
|
|
|
|
if (!chartInfo.isTestTemplate()) { |
|
|
|
|
if (SendHelper.sendChartInfo(chartInfo)) { |
|
|
|
|
removeLaterList.add(key); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
removeLaterList.add(key); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 清空记录
|
|
|
|
|
for (String key : removeLaterList) { |
|
|
|
|
chartInfoMap.remove(key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
saveInfo(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新 day_count:打开设计器却未编辑图表的连续日子 |
|
|
|
|
*/ |
|
|
|
|
private void addIdleDayCount() { |
|
|
|
|
@Override |
|
|
|
|
protected void addIdleDayCount() { |
|
|
|
|
// 判断今天是否第一次打开设计器,为了防止同一天内,多次 addIdleDayCount
|
|
|
|
|
String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); |
|
|
|
|
if (ComparatorUtils.equals(today, lastEditDay)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
for (ChartInfo chartInfo : chartInfoMap.values()) { |
|
|
|
|
for (ChartInfo chartInfo : pointInfoMap.values()) { |
|
|
|
|
chartInfo.addIdleDayCountByOne(); |
|
|
|
|
} |
|
|
|
|
lastEditDay = today; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void loadFromFile() { |
|
|
|
|
if (!getInfoFile().exists()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
XMLableReader reader = null; |
|
|
|
|
try (InputStream in = new FileInputStream(getInfoFile())) { |
|
|
|
|
// XMLableReader 还是应该考虑实现 Closable 接口的,这样就能使用 try-with 语句了
|
|
|
|
|
reader = XMLReadHelper.createXMLableReader(in, XMLPrintWriter.XML_ENCODER); |
|
|
|
|
if (reader == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
reader.readXMLObject(this); |
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
// do nothing
|
|
|
|
|
} catch (XMLStreamException | IOException e) { |
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
|
} finally { |
|
|
|
|
try { |
|
|
|
|
if (reader != null) { |
|
|
|
|
reader.close(); |
|
|
|
|
} |
|
|
|
|
} catch (XMLStreamException e) { |
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取缓存文件存放路径 |
|
|
|
|
*/ |
|
|
|
|
private static File getInfoFile() { |
|
|
|
|
@Override |
|
|
|
|
protected File getInfoFile() { |
|
|
|
|
File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME)); |
|
|
|
|
try { |
|
|
|
|
if (!file.exists()) { |
|
|
|
@ -291,27 +213,6 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
return file; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean shouldCollectInfo() { |
|
|
|
|
return FileUtils.sizeOf(getInfoFile()) <= MAX_SIZE && DesignerEnvManager.getEnvManager().isJoinProductImprove() && FRContext.isChineseEnv(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 将包含所有信息的对象保存到文件 |
|
|
|
|
*/ |
|
|
|
|
private void saveInfo() { |
|
|
|
|
try { |
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
|
XMLTools.writeOutputStreamXML(this, out); |
|
|
|
|
out.flush(); |
|
|
|
|
out.close(); |
|
|
|
|
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); |
|
|
|
|
FileUtils.writeStringToFile(getInfoFile(), fileContent, StandardCharsets.UTF_8); |
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
FineLoggerFactory.getLogger().error(ex.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void readXML(XMLableReader reader) { |
|
|
|
|
if (reader.isChildNode()) { |
|
|
|
@ -319,7 +220,7 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
String name = reader.getTagName(); |
|
|
|
|
if (ChartInfo.XML_TAG.equals(name)) { |
|
|
|
|
ChartInfo chartInfo = ChartInfo.newInstanceByRead(reader); |
|
|
|
|
chartInfoMap.put(chartInfo.getChartId(), chartInfo); |
|
|
|
|
pointInfoMap.put(chartInfo.getChartId(), chartInfo); |
|
|
|
|
} else if (XML_LAST_EDIT_DAY.equals(name)) { |
|
|
|
|
lastEditDay = reader.getElementValue(); |
|
|
|
|
} |
|
|
|
@ -338,7 +239,7 @@ public class ChartInfoCollector implements XMLReadable, XMLWriter {
|
|
|
|
|
writer.end(); |
|
|
|
|
|
|
|
|
|
writer.startTAG(XML_CHART_INFO_LIST); |
|
|
|
|
for (ChartInfo chartInfo : chartInfoMap.values()) { |
|
|
|
|
for (ChartInfo chartInfo : pointInfoMap.values()) { |
|
|
|
|
chartInfo.writeXML(writer); |
|
|
|
|
} |
|
|
|
|
writer.end(); |
|
|
|
|