白岳
5 years ago
14 changed files with 381 additions and 348 deletions
@ -0,0 +1,130 @@
|
||||
package com.fr.design.mainframe.burying.point; |
||||
|
||||
import com.fr.base.FRContext; |
||||
import com.fr.base.io.XMLReadHelper; |
||||
import com.fr.design.DesignerEnvManager; |
||||
import com.fr.design.mainframe.template.info.SendHelper; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.xml.XMLPrintWriter; |
||||
import com.fr.stable.xml.XMLTools; |
||||
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.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* @author Bjorn |
||||
* @version 10.0 |
||||
* Created by Bjorn on 2020-02-21 |
||||
*/ |
||||
public abstract class AbstractPointCollector<T extends AbstractPointInfo> implements BasePointCollector { |
||||
|
||||
protected Map<String, T> pointInfoMap; |
||||
|
||||
private static final int MAX_SIZE = 512 * 1024 * 1024; |
||||
|
||||
public AbstractPointCollector() { |
||||
pointInfoMap = new ConcurrentHashMap<>(); |
||||
loadFromFile(); |
||||
} |
||||
|
||||
/** |
||||
* 获取是否满足触发埋点的要求 |
||||
*/ |
||||
protected boolean shouldCollectInfo() { |
||||
return FileUtils.sizeOf(getInfoFile()) <= MAX_SIZE && DesignerEnvManager.getEnvManager().isJoinProductImprove() && FRContext.isChineseEnv(); |
||||
} |
||||
|
||||
@Override |
||||
public void sendPointInfo() { |
||||
|
||||
addIdleDayCount(); |
||||
|
||||
List<String> removeList = new ArrayList<>(); |
||||
List<String> sendList = new ArrayList<>(); |
||||
|
||||
for (String key : pointInfoMap.keySet()) { |
||||
AbstractPointInfo pointInfo = pointInfoMap.get(key); |
||||
pointInfo.selectPoint(removeList, sendList); |
||||
} |
||||
|
||||
// 发送记录
|
||||
for (String key : sendList) { |
||||
if(SendHelper.sendPointInfo(pointInfoMap.get(key))){ |
||||
removeList.add(key); |
||||
} |
||||
} |
||||
|
||||
// 清空记录
|
||||
for (String key : removeList) { |
||||
pointInfoMap.remove(key); |
||||
} |
||||
|
||||
saveInfo(); |
||||
} |
||||
|
||||
/** |
||||
* 从文件中读取埋点信息 |
||||
*/ |
||||
protected 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); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 保存埋点信息到文件中 |
||||
*/ |
||||
protected 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()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取缓存文件存放路径 |
||||
*/ |
||||
protected abstract File getInfoFile(); |
||||
|
||||
protected abstract void addIdleDayCount(); |
||||
} |
@ -0,0 +1,55 @@
|
||||
package com.fr.design.mainframe.burying.point; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author Bjorn |
||||
* @version 10.0 |
||||
* Created by Bjorn on 2020-02-21 |
||||
*/ |
||||
public abstract class AbstractPointInfo implements BasePointInfo { |
||||
|
||||
protected int idleDayCount; // 到现在为止,埋点闲置的天数
|
||||
|
||||
@Override |
||||
public void resetIdleDayCount() { |
||||
this.idleDayCount = 0; |
||||
} |
||||
|
||||
@Override |
||||
public void addIdleDayCountByOne() { |
||||
this.idleDayCount += 1; |
||||
} |
||||
|
||||
/** |
||||
* 上传前判断该埋点,是否需要被上传,或者删除,或者什么都不做。 |
||||
*/ |
||||
@Override |
||||
public void selectPoint(List<String> removeList, List<String> sendList) { |
||||
//埋点还未完成,直接返回
|
||||
if (!isComplete()) { |
||||
return; |
||||
} |
||||
//属于测试模板,直接删除,否则发送信息
|
||||
if (isTestTemplate()) { |
||||
removeList.add(key()); |
||||
} else { |
||||
sendList.add(key()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 是否为测试模板 |
||||
*/ |
||||
protected abstract boolean isTestTemplate(); |
||||
|
||||
/** |
||||
* 是否已经制作完成 |
||||
*/ |
||||
protected abstract boolean isComplete(); |
||||
|
||||
/** |
||||
* 埋点记录的主键 |
||||
*/ |
||||
protected abstract String key(); |
||||
} |
@ -0,0 +1,23 @@
|
||||
package com.fr.design.mainframe.burying.point; |
||||
|
||||
import com.fr.design.mainframe.template.info.TemplateProcessInfo; |
||||
import com.fr.stable.xml.XMLReadable; |
||||
import com.fr.stable.xml.XMLWriter; |
||||
|
||||
/** |
||||
* @author Bjorn |
||||
* @version 10.0 |
||||
* Created by Bjorn on 2020-02-21 |
||||
*/ |
||||
public interface BasePointCollector extends XMLReadable, XMLWriter { |
||||
|
||||
/** |
||||
* 发送埋点信息到服务器 |
||||
*/ |
||||
public void sendPointInfo(); |
||||
|
||||
/** |
||||
* 保存埋点的信息到本地 |
||||
*/ |
||||
public void collectInfo(String templateID, String originID, TemplateProcessInfo processInfo, int timeConsume); |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.fr.design.mainframe.burying.point; |
||||
|
||||
import com.fr.stable.xml.XMLReadable; |
||||
import com.fr.stable.xml.XMLWriter; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Bjorn |
||||
* @version 10.0 |
||||
* Created by Bjorn on 2020-02-21 |
||||
*/ |
||||
public interface BasePointInfo extends XMLReadable, XMLWriter { |
||||
|
||||
/** |
||||
* 重置埋点的未编辑天数 |
||||
*/ |
||||
void resetIdleDayCount(); |
||||
|
||||
/** |
||||
* 增加一天埋点的未编辑天数 |
||||
*/ |
||||
void addIdleDayCountByOne(); |
||||
|
||||
/** |
||||
* 上传前判断该埋点,是否需要被上传,或者删除,或者什么都不做。 |
||||
*/ |
||||
void selectPoint(List<String> removeList, List<String> sendList); |
||||
|
||||
/** |
||||
* 获取要上传的内容,key→上传路径,value→上传内容 |
||||
*/ |
||||
Map<String, String> getSendInfo(); |
||||
} |
Loading…
Reference in new issue