Browse Source

Merge pull request #574 in DESIGN/design from ~ALEX.SUNG/design:feature/10.0 to feature/10.0

REPORT-11622 信息收集移至云中心

* commit 'ec5f13f510bd731a5212538cbe1bbbf26325438e':
  代码质量
  REPORT-11622 信息收集移至云中心
  REPORT-11622 信息收集移至云中心
research/10.0
Alex.Sung 6 years ago
parent
commit
8b3fcf118b
  1. 265
      designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java

265
designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package com.fr.design.mainframe; package com.fr.design.mainframe;
@ -20,15 +20,25 @@ import com.fr.general.DesUtils;
import com.fr.general.GeneralUtils; import com.fr.general.GeneralUtils;
import com.fr.general.IOUtils; import com.fr.general.IOUtils;
import com.fr.general.http.HttpClient; import com.fr.general.http.HttpClient;
import com.fr.general.http.HttpToolbox;
import com.fr.intelli.record.FocusPoint;
import com.fr.intelli.record.MetricException;
import com.fr.intelli.record.MetricRegistry;
import com.fr.json.JSONArray; import com.fr.json.JSONArray;
import com.fr.json.JSONException; import com.fr.json.JSONException;
import com.fr.json.JSONObject; import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import com.fr.log.message.ParameterMessage;
import com.fr.record.DBRecordXManager; import com.fr.record.DBRecordXManager;
import com.fr.stable.ArrayUtils; import com.fr.stable.ArrayUtils;
import com.fr.stable.EncodeConstants; 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.StringUtils;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.condition.QueryCondition;
import com.fr.stable.query.data.DataList;
import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.stable.xml.XMLPrintWriter; import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLReadable; import com.fr.stable.xml.XMLReadable;
import com.fr.stable.xml.XMLTools; import com.fr.stable.xml.XMLTools;
@ -65,7 +75,7 @@ import java.util.Map;
* @date: 2015-4-8-下午5:11:46 * @date: 2015-4-8-下午5:11:46
*/ */
public class InformationCollector implements XMLReadable, XMLWriter { public class InformationCollector implements XMLReadable, XMLWriter {
// 24小时上传一次 // 24小时上传一次
private static final long DELTA = 24 * 3600 * 1000L; private static final long DELTA = 24 * 3600 * 1000L;
private static final long SEND_DELAY = 30 * 1000L; private static final long SEND_DELAY = 30 * 1000L;
@ -84,25 +94,34 @@ public class InformationCollector implements XMLReadable, XMLWriter {
public static final String TABLE_NAME = "fr_functionrecord"; public static final String TABLE_NAME = "fr_functionrecord";
public static final String FUNC_COLUMNNAME = "func"; public static final String FUNC_COLUMNNAME = "func";
public static final String COLUMN_TIME = "time";
public static final String TABLE_FUNCTION_RECORD = "function.record";
private static final String ATTR_ID = "id";
private static final String ATTR_TEXT = "text";
private static final String ATTR_SOURCE = "source";
private static final String ATTR_TIME = "time";
private static final String ATTR_TITLE = "title";
private static final String ATTR_USER_NAME = "username";
private static final String ATTR_UUID = "uuid";
private static InformationCollector collector; private static InformationCollector collector;
//启动时间与关闭时间列表 //启动时间与关闭时间列表
private List<StartStopTime> startStop = new ArrayList<StartStopTime>(); private List<StartStopTime> startStop = new ArrayList<StartStopTime>();
//上一次的发送时间 //上一次的发送时间
private String lastTime; private String lastTime;
private StartStopTime current = new StartStopTime(); private StartStopTime current = new StartStopTime();
public static InformationCollector getInstance(){ public static InformationCollector getInstance(){
if (collector == null) { if (collector == null) {
collector = new InformationCollector(); collector = new InformationCollector();
readEncodeXMLFile(collector, collector.getInfoFile()); readEncodeXMLFile(collector, collector.getInfoFile());
} }
return collector; return collector;
} }
private static void readEncodeXMLFile(XMLReadable xmlReadable, File xmlFile){ private static void readEncodeXMLFile(XMLReadable xmlReadable, File xmlFile){
if (xmlFile == null || !xmlFile.exists()) { if (xmlFile == null || !xmlFile.exists()) {
return; return;
@ -128,29 +147,29 @@ public class InformationCollector implements XMLReadable, XMLWriter {
} }
} }
private static String getDecodeFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException{ private static String getDecodeFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException{
InputStream encodeInputStream = new FileInputStream(xmlFile); InputStream encodeInputStream = new FileInputStream(xmlFile);
String encodeContent = IOUtils.inputStream2String(encodeInputStream); String encodeContent = IOUtils.inputStream2String(encodeInputStream);
return DesUtils.getDecString(encodeContent); return DesUtils.getDecString(encodeContent);
} }
private long getLastTimeMillis(){ private long getLastTimeMillis(){
if (StringUtils.isEmpty(this.lastTime)) { if (StringUtils.isEmpty(this.lastTime)) {
return 0; return 0;
} }
try { try {
return DateUtils.string2Date(this.lastTime, true).getTime(); return DateUtils.string2Date(this.lastTime, true).getTime();
} catch (Exception e) { } catch (Exception e) {
return -1; return -1;
} }
} }
private byte[] getJSONContentAsByte(){ private byte[] getJSONContentAsByte(){
JSONObject content = new JSONObject(); JSONObject content = new JSONObject();
JSONArray startStopArray = new JSONArray(); JSONArray startStopArray = new JSONArray();
for (int i = 0; i < startStop.size(); i++) { for (int i = 0; i < startStop.size(); i++) {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
@ -182,7 +201,7 @@ public class InformationCollector implements XMLReadable, XMLWriter {
private void sendUserInfo(){ private void sendUserInfo(){
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long lastTime = getLastTimeMillis(); long lastTime = getLastTimeMillis();
if (currentTime - lastTime <= DELTA) { if (currentTime - lastTime <= DELTA) {
return; return;
} }
@ -207,139 +226,50 @@ public class InformationCollector implements XMLReadable, XMLWriter {
private void sendFunctionsInfo(){ private void sendFunctionsInfo(){
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long lastTime = getLastTimeMillis(); ArrayList<Map<String, Object>> content = null;
if (currentTime - lastTime <= DELTA) {
return;
}
// byte[] content = ArrayUtils.EMPTY_BYTE_ARRAY;
// Connection conn = null;
// Table table = new Table(TABLE_NAME);
// try {
// conn = DBRecordXManager.getDB().createConnection();
// ResultSet rs = selectAllFromLogDB(conn, table);
//
// if(rs == null){
// return;
// }
// content = getFunctionsContentAsByte(rs);
// } catch (Exception e) {
// FRContext.getLogger().error(e.getMessage(), e);
// } finally {
// DBUtils.closeConnection(conn);
// }
//
// HttpClient httpClient = new HttpClient(CloudCenter.getInstance().acquireUrlByKind("functions.info"));
// httpClient.setContent(content);
// httpClient.setTimeout(5000);
//
// if (!httpClient.isServerAlive()) {
// return;
// }
//
// String res = httpClient.getResponseText();
// boolean success = false;
// try {
// success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success");
// } catch (JSONException e) {
// FRContext.getLogger().error(e.getMessage(), e);
// }
// //服务器返回true, 说明已经获取成功, 清空当前记录的信息
// if (success) {
// deleteLogDB(conn, table);
// }
}
private void deleteLogDB(Connection conn, Table table) {
try { try {
conn = DBRecordXManager.getDB().createConnection(); content = getFunctionsContentAsByte(currentTime);
Delete delete = new Delete(table); } catch (JSONException e) {
delete.execute(conn); FineLoggerFactory.getLogger().error(e.getMessage(), e);
} catch (Exception e) {
FRContext.getLogger().error(e.getMessage(), e);
} finally {
DBUtils.closeConnection(conn);
} }
} boolean success = false;
String url = CloudCenter.getInstance().acquireUrlByKind(TABLE_FUNCTION_RECORD);
if(content.size() > 0){
private byte[] getFunctionsContentAsByte(ResultSet rs) throws JSONException{ for(int i=0; i<content.size(); i++){
com.fr.json.JSONObject content = new com.fr.json.JSONObject(); success = sendFunctionRecord(url, content.get(i));
HashMap resultMap = new HashMap(); }
//服务器返回true, 说明已经获取成功, 清空当前记录的信息
try { if (success) {
while (rs.next()) { deleteFunctionRecords(currentTime);
com.fr.json.JSONObject js = new com.fr.json.JSONObject(rs.getString(FUNC_COLUMNNAME));
Map tempMap = js.toMap();
for (Object key : tempMap.keySet()) {
if(resultMap.containsKey(key)){
int cacheCount = Integer.parseInt(resultMap.get(key).toString());
int currentCount = Integer.parseInt(tempMap.get(key).toString());
resultMap.put(key, cacheCount + currentCount);
} else {
resultMap.put(key, tempMap.get(key));
}
}
} }
rs.close();
} catch (SQLException e) {
//这边不记,可能还没建表
}
JSONArray functionArray = new JSONArray();
for(Object key : resultMap.keySet()){
com.fr.json.JSONObject jsonObject = new com.fr.json.JSONObject();
jsonObject.put("point", key);
jsonObject.put("times", resultMap.get(key));
functionArray.put(jsonObject);
}
DesignerEnvManager envManager = DesignerEnvManager.getEnvManager();
content.put("username", MarketConfig.getInstance().getBbsUsername());
content.put("uuid", envManager.getUUID());
content.put("functions", functionArray);
try {
return content.toString().getBytes(EncodeConstants.ENCODING_UTF_8);
} catch (UnsupportedEncodingException e) {
FRContext.getLogger().error(e.getMessage(), e);
return ArrayUtils.EMPTY_BYTE_ARRAY;
} }
} }
private ResultSet selectAllFromLogDB(Connection conn, Table table) { private boolean sendFunctionRecord(String url, Map<String,Object> record) {
HashMap<String, String> para = new HashMap<>();
Select select = new Select(table, DialectFactory.generateDialect(conn)); para.put("token", SiteCenterToken.generateToken());
PreparedStatement ps; para.put("content", new JSONObject(record).toString());
ResultSet rs; String res = null;
try { try {
ps = select.createPreparedStatement(conn); res = HttpToolbox.get(url, para);
} catch (SQLException e) { } catch (IOException e) {
return null; FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
boolean success = false;
try { try {
rs = ps.executeQuery(); success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success");
} catch (SQLException e) { } catch (JSONException e) {
try { FineLoggerFactory.getLogger().error(e.getMessage(), e);
ps.close();
} catch (SQLException e1) {
//这边不记,可能还没建表
}
return null;
} }
return rs; return success;
} }
/** /**
* 收集开始使用时间发送信息 * 收集开始使用时间发送信息
*/ */
public void collectStartTime(){ public void collectStartTime(){
this.current.setStartDate(dateToString()); this.current.setStartDate(dateToString());
sendUserInfoInOtherThread(); sendUserInfoInOtherThread();
} }
@ -347,9 +277,9 @@ public class InformationCollector implements XMLReadable, XMLWriter {
if (!DesignerEnvManager.getEnvManager().isJoinProductImprove() || !FRContext.isChineseEnv()) { if (!DesignerEnvManager.getEnvManager().isJoinProductImprove() || !FRContext.isChineseEnv()) {
return; return;
} }
Thread sendThread = new Thread(new Runnable() { Thread sendThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -373,17 +303,17 @@ public class InformationCollector implements XMLReadable, XMLWriter {
public void collectStopTime(){ public void collectStopTime(){
this.current.setStopDate(dateToString()); this.current.setStopDate(dateToString());
} }
private String dateToString(){ private String dateToString(){
DateFormat df = FRContext.getDefaultValues().getDateTimeFormat(); DateFormat df = FRContext.getDefaultValues().getDateTimeFormat();
return df.format(new Date()); return df.format(new Date());
} }
private void reset(){ private void reset(){
this.startStop.clear(); this.startStop.clear();
this.lastTime = dateToString(); this.lastTime = dateToString();
} }
private File getInfoFile() { private File getInfoFile() {
return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME));
} }
@ -403,10 +333,10 @@ public class InformationCollector implements XMLReadable, XMLWriter {
writeEncodeContentToFile(encodeCotent, xmlFile); writeEncodeContentToFile(encodeCotent, xmlFile);
}catch (Exception e) { }catch (Exception e) {
FRContext.getLogger().error(e.getMessage(), e); FRContext.getLogger().error(e.getMessage(), e);
} }
} }
/** /**
* 将文件内容写到输出流中 * 将文件内容写到输出流中
*/ */
@ -438,10 +368,10 @@ public class InformationCollector implements XMLReadable, XMLWriter {
writeStartStopList(writer); writeStartStopList(writer);
//上一次更新的时间 //上一次更新的时间
writeTag(XML_LAST_TIME, this.lastTime, writer); writeTag(XML_LAST_TIME, this.lastTime, writer);
writer.end(); writer.end();
} }
private void writeStartStopList(XMLPrintWriter writer){ private void writeStartStopList(XMLPrintWriter writer){
//启停 //启停
writer.startTAG(XML_START_STOP_LIST); writer.startTAG(XML_START_STOP_LIST);
@ -450,12 +380,12 @@ public class InformationCollector implements XMLReadable, XMLWriter {
} }
writer.end(); writer.end();
} }
private void writeTag(String tag, String content, XMLPrintWriter writer){ private void writeTag(String tag, String content, XMLPrintWriter writer){
if (StringUtils.isEmpty(content)) { if (StringUtils.isEmpty(content)) {
return; return;
} }
writer.startTAG(tag); writer.startTAG(tag);
writer.textNode(content); writer.textNode(content);
writer.end(); writer.end();
@ -472,17 +402,17 @@ public class InformationCollector implements XMLReadable, XMLWriter {
} }
} }
} }
private void readLastTime(XMLableReader reader){ private void readLastTime(XMLableReader reader){
String tmpVal; String tmpVal;
if (StringUtils.isNotBlank(tmpVal = reader.getElementValue())) { if (StringUtils.isNotBlank(tmpVal = reader.getElementValue())) {
this.lastTime = tmpVal; this.lastTime = tmpVal;
} }
} }
private void readStartStopList(XMLableReader reader){ private void readStartStopList(XMLableReader reader){
startStop.clear(); startStop.clear();
reader.readXMLObject(new XMLReadable() { reader.readXMLObject(new XMLReadable() {
public void readXML(XMLableReader reader) { public void readXML(XMLableReader reader) {
if (XML_START_STOP.equals(reader.getTagName())) { if (XML_START_STOP.equals(reader.getTagName())) {
@ -493,9 +423,50 @@ public class InformationCollector implements XMLReadable, XMLWriter {
} }
}); });
} }
public static ArrayList getFunctionsContentAsByte(long currentTime) throws JSONException{
ArrayList<Map<String,Object>> records = new ArrayList<Map<String,Object>>();
QueryCondition condition = QueryFactory.create().addRestriction(
RestrictionFactory.lte(COLUMN_TIME, currentTime)
);
try {
DataList<FocusPoint> focusPoints = MetricRegistry.getMetric().find(FocusPoint.class,condition);
focusPoints.getList();
DesignerEnvManager envManager = DesignerEnvManager.getEnvManager();
String bbsUserName = MarketConfig.getInstance().getBbsUsername();
String uuid = envManager.getUUID();
if(!focusPoints.isEmpty()){
for(FocusPoint focusPoint : focusPoints.getList()){
Map<String,Object> record = new HashMap<>();
record.put(ATTR_ID, focusPoint.getId());
record.put(ATTR_TEXT, focusPoint.getText());
record.put(ATTR_SOURCE, focusPoint.getSource());
record.put(ATTR_TIME, focusPoint.getTime().getTime());
record.put(ATTR_TITLE, focusPoint.getTitle());
record.put(ATTR_USER_NAME, bbsUserName);
record.put(ATTR_UUID, uuid);
records.add(record);
}
}
} catch (MetricException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return records;
}
private void deleteFunctionRecords(long currentTime) {
QueryCondition condition = QueryFactory.create().addRestriction(
RestrictionFactory.lte(COLUMN_TIME, currentTime)
);
try {
MetricRegistry.getMetric().clean(condition);
}catch (Exception e){
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
private class StartStopTime implements XMLReadable, XMLWriter { private class StartStopTime implements XMLReadable, XMLWriter {
private String startDate; private String startDate;
private String stopDate; private String stopDate;

Loading…
Cancel
Save