public static Map<String, Object> getResultByTag(String tag) throws Exception { Map<String, Object> result = new HashMap<>(); visualizator = new Visualizator(); try { ArrayList tags = new ArrayList<>(); tags.add(tag); tags = visualizator.getCounterHistory(tags); Map<String, Object> data = visualizator.exportData(tags, "datasets_win"); data.put("datasets_lose", ""); result.put("success", true); result.put("data", data); } catch (Exception ex) { result.put("success", false); result.put("message", ex.getMessage()); } visualizator.close(); return result; }
/* Get JSON for visualization of one day */ public static Map<String, Object> getResult(String name) throws Exception { Map<String, Object> result = new HashMap<>(); try { Map<String, Object> data = new HashMap<>(); // will be put to result Map<String, Object> dataLose = new HashMap<>(); // lose tag Map<String, Object> dataWin = exportJSON(Config.resultLocation + "/" + name + ".JSON"); // today's trending tag /* change key name of today's trending tag */ Object datasets = (Object) dataWin.get("datasets"); data.put("datasets_win", datasets); /* put date */ datasets = (Object) dataWin.get("labels"); data.put("labels", datasets); /* initialization */ DateUtility d = new DateUtility(name.substring(0, 10)); visualizator = new Visualizator(); /* save list of today's trending tag for comparison */ datasets = (Object) dataWin.get("datasets"); DataJSON[] todayTop = gson.fromJson(gson.toJson(datasets), DataJSON[].class); Set<String> todayTopTag = new HashSet<>(); for (DataJSON aTodayTop : todayTop) { todayTopTag.add(aTodayTop.label); } /* process every trending tag except today */ boolean done = false; while (!done) { d.prevDay(); String location = Config.resultLocation + "/" + d.toString() + "-top.JSON"; File f = new File(location); if (f.exists() && !f.isDirectory()) { /* save list of d's trending tag */ Map<String, Object> temp = exportJSON(location); datasets = (Object) temp.get("datasets"); DataJSON[] historyTop = gson.fromJson(gson.toJson(datasets), DataJSON[].class); /* get list of lose tag */ ArrayList<String> historyTopTag = new ArrayList<>(); for (DataJSON aHistoryTop : historyTop) { if (!todayTopTag.contains(aHistoryTop.label)) { historyTopTag.add(aHistoryTop.label); } } ArrayList<Tag> t = visualizator.getCounterHistory(historyTopTag); dataLose.putAll(visualizator.exportData(t, "datasets_lose")); } else { done = true; } } if (dataLose.get("datasets_lose") == null) { data.put("datasets_lose", ""); } else { data.put("datasets_lose", dataLose.get("datasets_lose")); } result.put("data", data); result.put("success", true); } catch (Exception ex) { result.put("success", false); result.put("message", ex.getMessage()); } visualizator.close(); return result; }