From b240e98948033c8a089bd11ebc384675f1ff6915 Mon Sep 17 00:00:00 2001 From: "alex.sung" Date: Wed, 2 Jan 2019 18:05:50 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-14022=20=E3=80=90=E9=9A=BE=E8=BF=98?= =?UTF-8?q?=E5=8E=9F=E3=80=91=E6=9C=80=E6=96=B0=E7=9A=8429=E5=8F=B7release?= =?UTF-8?q?=EF=BC=8C=E8=8E=AB=E5=90=8D=E5=85=B6=E5=A6=99=E7=9A=84=E5=8D=A1?= =?UTF-8?q?=E4=BD=8F=E5=BE=88=E4=B9=85=EF=BC=8C=E5=90=8E=E5=8F=B0=E7=99=BE?= =?UTF-8?q?=E5=88=86=E7=99=BE=E7=9A=84cpu=E5=8D=A0=E7=94=A8=20=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=80=81=EF=BC=8C=E5=8E=BB=E9=87=8D=EF=BC=9B?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1query=20200=E6=9D=A1=EF=BC=8C=E6=94=BE?= =?UTF-8?q?=E5=85=A5=E5=86=85=E5=AD=98=EF=BC=8C=E5=8F=91=E9=80=81=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=86=8D=E8=BF=9B=E8=A1=8C=E6=96=B0=E7=9A=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=20=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=94=B9=E5=88=B010.0?= =?UTF-8?q?=20master?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/InformationCollector.java | 186 ++++++++++-------- 1 file changed, 104 insertions(+), 82 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java index 61b583c615..49ad7d6a37 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java @@ -89,11 +89,15 @@ public class InformationCollector implements XMLReadable, XMLWriter { 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_TIMES = "times"; private static final String ATTR_TITLE = "title"; private static final String ATTR_USER_NAME = "username"; private static final String ATTR_UUID = "uuid"; + private static final String ATTR_ITEMS = "items"; private static final String ATTR_FUNCTION_ARRAY = "functionArray"; private static final int MAX_EACH_REQUEST_RECORD_COUNT = 200; + private static final int PAGE_SIZE = 200; + private long totalCount = -1; private static InformationCollector collector; @@ -214,26 +218,14 @@ public class InformationCollector implements XMLReadable, XMLWriter { if (currentTime - lastTime <= DELTA) { return; } - JSONArray content = getFunctionsContent(currentTime, lastTime); - boolean success = false; FineLoggerFactory.getLogger().info("Start sent function records to the cloud center..."); - String url = CloudCenter.getInstance().acquireUrlByKind(TABLE_FUNCTION_RECORD); - try { - for(int i=0;i 0){ - success = sendFunctionRecord(url, functionArray); - } - } - //服务器返回true, 说明已经获取成功, 更新最后一次发送时间 - if (success) { - this.lastTime = dateToString(); - FineLoggerFactory.getLogger().info("Function records successfully sent to the cloud center."); - } - }catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); + queryAndSendOnePageFunctionContent(currentTime, lastTime, 0); + long page = (totalCount/PAGE_SIZE) + 1; + for(int i=1; i focusPoints = MetricRegistry.getMetric().find(FocusPoint.class,condition); + //第一次查询获取总记录数 + if(page == 0){ + totalCount = focusPoints.getTotalCount(); + } + sendThisPageFunctionContent(focusPoints); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + private void sendThisPageFunctionContent(DataList focusPoints) { + String url = CloudCenter.getInstance().acquireUrlByKind(TABLE_FUNCTION_RECORD); + try { + JSONObject jsonObject = dealWithSendFunctionContent(focusPoints); + sendFunctionRecord(url, jsonObject); + } catch (JSONException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + } + + private JSONObject dealWithSendFunctionContent(DataList focusPoints) throws JSONException { + JSONObject jsonObject = new JSONObject(); + Map map = new HashMap<>(); + if(!focusPoints.isEmpty()){ + DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); + String bbsUserName = MarketConfig.getInstance().getBbsUsername(); + String uuid = envManager.getUUID(); + jsonObject.put(ATTR_UUID, uuid); + jsonObject.put(ATTR_USER_NAME, bbsUserName); + for(FocusPoint focusPoint : focusPoints.getList()) { + FunctionRecord functionRecord = getOneRecord(focusPoint); + if (map.containsKey(focusPoint.getId())) { + functionRecord.setTimes(functionRecord.getTimes() + 1); + map.put(focusPoint.getId(), functionRecord); + } else { + map.put(focusPoint.getId(), functionRecord); + } + } + jsonObject.put(ATTR_ITEMS, mapToJSONArray(map)); + } + return jsonObject; + } + + private JSONArray mapToJSONArray(Map map) throws JSONException { + JSONArray jsonArray = new JSONArray(); + for(String keys : map.keySet()){ + FunctionRecord functionRecord = (FunctionRecord)map.get(keys); + JSONObject jo = new JSONObject(); + jo.put(ATTR_ID, functionRecord.getId()); + jo.put(ATTR_TEXT, functionRecord.getText()); + jo.put(ATTR_SOURCE, functionRecord.getSource()); + jo.put(ATTR_TIME, functionRecord.getTime()); + jo.put(ATTR_TITLE, functionRecord.getTitle()); + jo.put(ATTR_TIMES, functionRecord.getTimes()); + jsonArray.put(jo); + } + return jsonArray; + } + + private void sendFunctionRecord(String url, JSONObject record) { boolean success = false; try { HashMap para = new HashMap<>(); @@ -260,12 +320,28 @@ public class InformationCollector implements XMLReadable, XMLWriter { para.put("content", record); String res = HttpToolbox.post(url, para); success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); + if (success) { + this.lastTime = dateToString(); + } else { + FineLoggerFactory.getLogger().error("Error occured when sent function records to the cloud center."); + } } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } - return success; } + private FunctionRecord getOneRecord(FocusPoint focusPoint) { + FunctionRecord functionRecord = new FunctionRecord(); + functionRecord.setId(focusPoint.getId() == null?StringUtils.EMPTY : focusPoint.getId()); + functionRecord.setText(focusPoint.getText() == null?StringUtils.EMPTY : focusPoint.getText()); + functionRecord.setSource(focusPoint.getSource()); + functionRecord.setTime(focusPoint.getTime().getTime()); + functionRecord.setTitle(focusPoint.getTitle() == null?StringUtils.EMPTY : focusPoint.getTitle()); + functionRecord.setUsername(MarketConfig.getInstance().getBbsUsername() == null?StringUtils.EMPTY : MarketConfig.getInstance().getBbsUsername()); + functionRecord.setUuid(DesignerEnvManager.getEnvManager().getUUID() == null?StringUtils.EMPTY : DesignerEnvManager.getEnvManager().getUUID()); + return functionRecord; + } + /** * 收集开始使用时间,发送信息 */ @@ -426,69 +502,6 @@ public class InformationCollector implements XMLReadable, XMLWriter { }); } - public JSONArray getFunctionsContent(long current, long last) { - //记录当前条数,达到200条合并成一个请求 - int count = 0; - JSONArray functionArray = new JSONArray(); - QueryCondition condition = QueryFactory.create() - .addRestriction(RestrictionFactory.lte(COLUMN_TIME, current)) - .addRestriction(RestrictionFactory.gte(COLUMN_TIME, last)); - try { - DataList focusPoints = MetricRegistry.getMetric().find(FocusPoint.class,condition); - TreeSet focusPointsList = new TreeSet<>(); - if(!focusPoints.isEmpty()){ - for(int i=0;i< focusPoints.getList().size();i++){ - FocusPoint focusPoint = focusPoints.getList().get(i); - if(focusPoint != null){ - if((++count <= MAX_EACH_REQUEST_RECORD_COUNT)){ - focusPointsList.add(getOneRecord(focusPoint)); - } else { - count = 0; - functionArray.put(setToJSONArray(focusPointsList)); - focusPointsList.add(getOneRecord(focusPoint)); - } - if(i == (focusPoints.getList().size() -1)){ - functionArray.put(setToJSONArray(focusPointsList)); - } - } - } - } - - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - return functionArray; - } - - private FunctionRecord getOneRecord(FocusPoint focusPoint) { - FunctionRecord functionRecord = new FunctionRecord(); - functionRecord.setId(focusPoint.getId() == null?StringUtils.EMPTY : focusPoint.getId()); - functionRecord.setText(focusPoint.getText() == null?StringUtils.EMPTY : focusPoint.getText()); - functionRecord.setSource(focusPoint.getSource()); - functionRecord.setTime(focusPoint.getTime().getTime()); - functionRecord.setTitle(focusPoint.getTitle() == null?StringUtils.EMPTY : focusPoint.getTitle()); - functionRecord.setUsername(MarketConfig.getInstance().getBbsUsername() == null?StringUtils.EMPTY : MarketConfig.getInstance().getBbsUsername()); - functionRecord.setUuid(DesignerEnvManager.getEnvManager().getUUID() == null?StringUtils.EMPTY : DesignerEnvManager.getEnvManager().getUUID()); - return functionRecord; - } - - private JSONArray setToJSONArray(Set set) throws JSONException { - JSONArray jsonArray = new JSONArray(); - for(Iterator iter = set.iterator(); iter.hasNext(); ) { - FunctionRecord functionRecord = (FunctionRecord)iter.next(); - com.fr.json.JSONObject record = new com.fr.json.JSONObject(); - record.put(ATTR_ID, functionRecord.getId()); - record.put(ATTR_TEXT, functionRecord.getText()); - record.put(ATTR_SOURCE, functionRecord.getSource()); - record.put(ATTR_TIME, functionRecord.getTime()); - record.put(ATTR_TITLE, functionRecord.getTitle()); - record.put(ATTR_USER_NAME, functionRecord.getUsername()); - record.put(ATTR_UUID, functionRecord.getUuid()); - jsonArray.put(record); - } - return jsonArray; - } - private class StartStopTime implements XMLReadable, XMLWriter { private String startDate; @@ -533,6 +546,7 @@ public class InformationCollector implements XMLReadable, XMLWriter { private String text; private int source; private long time; + private int times = 1; private String title; private String username; private String uuid; @@ -548,6 +562,14 @@ public class InformationCollector implements XMLReadable, XMLWriter { this.id = id; } + public int getTimes() { + return times; + } + + public void setTimes(int times) { + this.times = times; + } + public String getText() { return text; }