Browse Source

使用websocket处理前端发来的埋点消息;

根据产品新规定的埋点标识命名;
文件操作放在FileEntityBuilder里;
research/10.0
1 6 years ago
parent
commit
15cadcebad
  1. 30
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java
  2. 131
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java
  3. 26
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java
  4. 17
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java

30
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java

@ -1,30 +0,0 @@
package com.fr.design.mainframe.messagecollect;
import com.fr.stable.query.data.DataList;
/**
* @author alex sung
* @date 2019/3/22
*/
public interface SendDataToCloudProvider {
/**
* 获取要回传的数据
* @param currentTime 当前时间
* @param lastTime 上次回传时间
* @param tClass 埋点对象类型
* @throws Exception 取数过程中可能的异常
*/
<T> void queryData(long currentTime, long lastTime, Class<T> tClass) throws Exception;
/**
* @param points 从swift获取的埋点数据
* @throws Exception 解析或存储临时文件时可能的异常
*/
<T> void dealWithData(DataList<T> points) throws Exception;
/**
* 回传Zip到云中心
*/
void sendToCloudCenter();
}

131
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java

@ -1,65 +1,41 @@
package com.fr.design.mainframe.messagecollect.impl; package com.fr.design.mainframe.messagecollect.impl;
import com.fr.design.mainframe.messagecollect.SendDataToCloudProvider; import com.fr.design.mainframe.messagecollect.entity.FileEntityBuilder;
import com.fr.design.mainframe.messagecollect.entity.FileEntity;
import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils;
import com.fr.general.CloudCenter;
import com.fr.general.IOUtils;
import com.fr.general.http.HttpRequestType;
import com.fr.general.http.HttpToolbox;
import com.fr.intelli.record.MetricRegistry; import com.fr.intelli.record.MetricRegistry;
import com.fr.json.JSONException; import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.stable.CommonUtils;
import com.fr.stable.EncodeConstants;
import com.fr.stable.ProductConstants; import com.fr.stable.ProductConstants;
import com.fr.stable.StableUtils; import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;
import com.fr.stable.query.QueryFactory; import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.condition.QueryCondition;
import com.fr.stable.query.data.DataList; import com.fr.stable.query.data.DataList;
import com.fr.stable.query.restriction.RestrictionFactory; import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.stable.xml.XMLTools; import com.fr.stable.xml.XMLTools;
import com.fr.stable.xml.XMLable; import com.fr.stable.xml.XMLable;
import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder;
import com.fr.third.org.apache.http.entity.mime.content.FileBody;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
/** /**
* @author alex sung * @author alex sung
* @date 2019/3/22 * @date 2019/3/22
*/ */
public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider, XMLable { public abstract class AbstractSendDataToCloud implements XMLable {
private static final String INTELLI_OPERATION_URL = "intelli.operation.url";
private static final String OPERATION_URL = "https://cloud.fanruan.com/config/protect/operation";
private static final String ATTR_SIGNATURE = "signature";
private static final String ATTR_KEY = "key";
private static final String FILE_NAME = "messagecollect.info"; private static final String FILE_NAME = "messagecollect.info";
private static final String COLUMN_TIME = "time"; private static final String COLUMN_TIME = "time";
protected String lastTime; protected String lastTime;
private static final int PAGE_SIZE = 200; private static final int PAGE_SIZE = 200;
private long totalCount = -1; private long totalCount = -1;
private FileEntity fileEntity; private FileEntityBuilder fileEntityBuilder;
public FileEntity getFileEntity() { public FileEntityBuilder getFileEntityBuilder() {
return fileEntity; return fileEntityBuilder;
} }
public void setFileEntity(FileEntity fileEntity) { public void setFileEntityBuilder(FileEntityBuilder fileEntityBuilder) {
this.fileEntity = fileEntity; this.fileEntityBuilder = fileEntityBuilder;
} }
public String getLastTime() { public String getLastTime() {
@ -71,6 +47,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
} }
public void saveLastTime() { public void saveLastTime() {
setLastTime(MessageCollectUtils.dateToString());
try { try {
FileOutputStream out = new FileOutputStream(getLastTimeFile()); FileOutputStream out = new FileOutputStream(getLastTimeFile());
XMLTools.writeOutputStreamXML(this, out); XMLTools.writeOutputStreamXML(this, out);
@ -83,7 +60,6 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME));
} }
@Override
public <T> void queryData(long currentTime, long lastTime, Class<T> tClass) { public <T> void queryData(long currentTime, long lastTime, Class<T> tClass) {
queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass); queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass);
long page = (totalCount / PAGE_SIZE) + 1; long page = (totalCount / PAGE_SIZE) + 1;
@ -117,7 +93,6 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
} }
} }
@Override
public <T> void dealWithData(DataList<T> tDataList) throws Exception { public <T> void dealWithData(DataList<T> tDataList) throws Exception {
generateThisPageFile(tDataList); generateThisPageFile(tDataList);
} }
@ -125,15 +100,15 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
private <T> void generateThisPageFile(DataList<T> points) { private <T> void generateThisPageFile(DataList<T> points) {
File file = null; File file = null;
try { try {
JSONObject jsonObject = dealWithSendFunctionContent(points); JSONArray jsonArray = dealWithSendFunctionContent(points);
//生成json文件 //生成json文件
generateFile(jsonObject); fileEntityBuilder.generateFile(jsonArray, getFileEntityBuilder().getPathName());
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
} }
public abstract <T> JSONObject dealWithSendFunctionContent(DataList<T> focusPoints); public abstract <T> JSONArray dealWithSendFunctionContent(DataList<T> focusPoints);
/** /**
* 生成zip并发送zip文件 * 生成zip并发送zip文件
@ -143,92 +118,16 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
File file = null; File file = null;
try { try {
file = generateZipFile(pathName); file = fileEntityBuilder.generateZipFile(pathName);
if (file != null) { if (file != null) {
uploadFile(file, file.getName()); fileEntityBuilder.uploadFile(file, file.getName());
} }
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); FineLoggerFactory.getLogger().error(e.getMessage(), e);
return; return;
} }
deleteFileAndZipFile(file, pathName); fileEntityBuilder.deleteFileAndZipFile(file, pathName);
}
private File generateZipFile(String pathName) {
File zipFile = null;
try {
File file = new File(pathName);
zipFile = new File(pathName + ".zip");
InputStream input = null;
java.util.zip.ZipOutputStream zipOut = null;
zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile));
int temp = 0;
if (file.isDirectory()) {
File lists[] = file.listFiles();
for (int i = 0; i < lists.length; i++) {
input = new FileInputStream(lists[i]);
zipOut.putNextEntry(new ZipEntry(file.getName()
+ File.separator + lists[i].getName()));
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return zipFile;
}
private void generateFile(JSONObject jsonObject) {
try {
String content = jsonObject.toString();
File file = new File(getFileEntity().getPathName() + ".json");
StableUtils.makesureFileExist(file);
FileOutputStream out = new FileOutputStream(file);
InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8));
IOUtils.copyBinaryTo(in, out);
out.close();
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
private static void uploadFile(File file, String keyFileName) throws IOException {
String url = generateSignedUploadUrl(keyFileName);
if(StringUtils.isEmpty(url)){
FineLoggerFactory.getLogger().error("url is null.");
}else {
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file));
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/zip");
HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT);
}
} }
private void deleteFileAndZipFile(File zipFile, String pathName) {
File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName));
MessageCollectUtils.deleteDir(file);
CommonUtils.deleteFile(zipFile);
}
private static String generateSignedUploadUrl(String fileKeyName) throws IOException {
String url = CloudCenter.getInstance().acquireUrlByKind(INTELLI_OPERATION_URL, OPERATION_URL);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(ATTR_KEY, fileKeyName);
parameters.put(ATTR_SIGNATURE, String.valueOf(CommonUtils.signature()));
String responseText = HttpToolbox.get(url, parameters);
try {
JSONObject data = new JSONObject(responseText);
if ("success".equals(data.optString("status"))) {
return data.optString("url");
}
} catch (JSONException e) {
FineLoggerFactory.getLogger().error("Illegal response text.");
}
return null;
}
} }

26
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java

@ -2,9 +2,10 @@ package com.fr.design.mainframe.messagecollect.impl;
import com.fr.config.MarketConfig; import com.fr.config.MarketConfig;
import com.fr.design.DesignerEnvManager; import com.fr.design.DesignerEnvManager;
import com.fr.design.mainframe.messagecollect.entity.FileEntity; import com.fr.design.mainframe.messagecollect.entity.FileEntityBuilder;
import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils; import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils;
import com.fr.intelli.record.FocusPoint; import com.fr.intelli.record.FocusPoint;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject; import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.stable.ProductConstants; import com.fr.stable.ProductConstants;
@ -39,11 +40,24 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
} }
@Override @Override
public <T> JSONObject dealWithSendFunctionContent(DataList<T> focusPoints) { public <T> JSONArray dealWithSendFunctionContent(DataList<T> focusPoints) {
return new JSONObject(); JSONArray ja = new JSONArray();
for(T t:focusPoints.getList()){
FocusPoint focusPoint = (FocusPoint)t;
JSONObject jo = new JSONObject();
jo.put("id",focusPoint.getId());
jo.put("text",focusPoint.getId());
jo.put("source",focusPoint.getId());
jo.put("time",focusPoint.getId());
jo.put("username",focusPoint.getId());
jo.put("ip",focusPoint.getId());
jo.put("title",focusPoint.getId());
jo.put("body",focusPoint.getId());
ja.put(jo);
}
return ja;
} }
@Override
public void sendToCloudCenter() { public void sendToCloudCenter() {
MessageCollectUtils.readXMLFile(instance, getLastTimeFile()); MessageCollectUtils.readXMLFile(instance, getLastTimeFile());
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
@ -54,7 +68,7 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage()); FineLoggerFactory.getLogger().error(e.getMessage());
} }
sendZipFile(getFileEntity().getFolderName()); sendZipFile(getFileEntityBuilder().getFolderName());
saveLastTime(); saveLastTime();
} }
@ -86,6 +100,6 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
String fileName = String.valueOf(UUID.randomUUID()); String fileName = String.valueOf(UUID.randomUUID());
String pathName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), fileName); String pathName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), fileName);
String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()); String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString());
setFileEntity(new FileEntity(fileName, pathName, folderName)); setFileEntityBuilder(new FileEntityBuilder(fileName, pathName, folderName));
} }
} }

17
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java

@ -1,5 +1,6 @@
package com.fr.design.mainframe.messagecollect.utils; package com.fr.design.mainframe.messagecollect.utils;
import com.fr.base.FRContext;
import com.fr.general.DateUtils; import com.fr.general.DateUtils;
import com.fr.general.IOUtils; import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
@ -15,6 +16,8 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;
/** /**
* @author alex sung * @author alex sung
@ -53,16 +56,8 @@ public class MessageCollectUtils {
} }
} }
public static boolean deleteDir(File dir) { public static String dateToString(){
if (dir.isDirectory()) { DateFormat df = FRContext.getDefaultValues().getDateTimeFormat();
String[] children = dir.list(); return df.format(new Date());
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
} }
} }

Loading…
Cancel
Save