private void parseGroupBy(String context, QueryMetric queryMetric, JsonArray groupBys) throws QueryException, BeanValidationException { for (int J = 0; J < groupBys.size(); J++) { String groupContext = "group_by[" + J + "]"; JsonObject jsGroupBy = groupBys.get(J).getAsJsonObject(); JsonElement nameElement = jsGroupBy.get("name"); if (nameElement == null || nameElement.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation(groupContext, "must have a name"), context); String name = nameElement.getAsString(); GroupBy groupBy = m_groupByFactory.createGroupBy(name); if (groupBy == null) throw new BeanValidationException( new SimpleConstraintViolation(groupContext + "." + name, "invalid group_by name"), context); deserializeProperties(context + "." + groupContext, jsGroupBy, name, groupBy); validateObject(groupBy, context + "." + groupContext); groupBy.setStartDate(queryMetric.getStartTime()); queryMetric.addGroupBy(groupBy); } }
private void parsePlugins(String context, QueryMetric queryMetric, JsonArray plugins) throws BeanValidationException, QueryException { for (int I = 0; I < plugins.size(); I++) { JsonObject pluginJson = plugins.get(I).getAsJsonObject(); JsonElement name = pluginJson.get("name"); if (name == null || name.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation("plugins[" + I + "]", "must have a name"), context); String pluginContext = context + ".plugins[" + I + "]"; String pluginName = name.getAsString(); QueryPlugin plugin = m_pluginFactory.createQueryPlugin(pluginName); if (plugin == null) throw new BeanValidationException( new SimpleConstraintViolation(pluginName, "invalid query plugin name"), pluginContext); deserializeProperties(pluginContext, pluginJson, pluginName, plugin); validateObject(plugin, pluginContext); queryMetric.addPlugin(plugin); } }
private static JsonArray deepCopyArr(JsonArray from) { JsonArray result = new JsonArray(); for (JsonElement element : from) { result.add(deepCopy(element)); } return result; }
public static Object gsonToPrimitive(JsonElement element) { if (element.isJsonPrimitive()) { JsonPrimitive prim = element.getAsJsonPrimitive(); if (prim.isString()) { return prim.getAsString(); } else if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isNumber()) { return prim.getAsNumber(); } else { throw new IllegalArgumentException("Unknown Gson primitive: " + prim); } } else if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); List<Object> list = new ArrayList<Object>(); for (int i = 0; i < array.size(); i++) { list.add(gsonToPrimitive(array.get(i))); } return list; } else if (element.isJsonNull()) { return null; } else if (element.isJsonObject()) { Map<String, Object> map = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) { map.put(entry.getKey(), gsonToPrimitive(entry.getValue())); } return map; } else { throw new IllegalArgumentException("Unknown Gson value: " + element); } }
public List<CIBuild> getBuilds(CIJob job) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.getCIBuilds(CIJob job)"); } List<CIBuild> ciBuilds = null; try { if (debugEnabled) { S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); } JsonArray jsonArray = getBuildsArray(job); ciBuilds = new ArrayList<CIBuild>(jsonArray.size()); Gson gson = new Gson(); CIBuild ciBuild = null; for (int i = 0; i < jsonArray.size(); i++) { ciBuild = gson.fromJson(jsonArray.get(i), CIBuild.class); setBuildStatus(ciBuild, job); String buildUrl = ciBuild.getUrl(); String jenkinUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); buildUrl = buildUrl.replaceAll( "localhost:" + job.getJenkinsPort(), jenkinUrl); // when displaying url it should display setup machine ip ciBuild.setUrl(buildUrl); ciBuilds.add(ciBuild); } } catch (Exception e) { if (debugEnabled) { S_LOGGER.debug( "Entering Method CIManagerImpl.getCIBuilds(CIJob job) " + e.getLocalizedMessage()); } } return ciBuilds; }
public List<RollupTask> parseRollupTasks(String json) throws BeanValidationException, QueryException { List<RollupTask> tasks = new ArrayList<RollupTask>(); JsonParser parser = new JsonParser(); JsonArray rollupTasks = parser.parse(json).getAsJsonArray(); for (int i = 0; i < rollupTasks.size(); i++) { JsonObject taskObject = rollupTasks.get(i).getAsJsonObject(); RollupTask task = parseRollupTask(taskObject, "tasks[" + i + "]"); task.addJson(taskObject.toString().replaceAll("\\n", "")); tasks.add(task); } return tasks; }
private void setBuildStatus(CIBuild ciBuild, CIJob job) throws PhrescoException { S_LOGGER.debug("Entering Method CIManagerImpl.setBuildStatus(CIBuild ciBuild)"); S_LOGGER.debug("setBuildStatus() url = " + ciBuild.getUrl()); String buildUrl = ciBuild.getUrl(); String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); buildUrl = buildUrl.replaceAll( "localhost:" + job.getJenkinsPort(), jenkinsUrl); // display the jenkins running url in ci list String response = getJsonResponse(buildUrl + API_JSON); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(response); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT); JsonElement idJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_ID); JsonElement timeJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_TIME_STAMP); JsonArray asJsonArray = jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS); if (jsonObject .get(FrameworkConstants.CI_JOB_BUILD_RESULT) .toString() .equals(STRING_NULL)) { // when build is result is not known ciBuild.setStatus(INPROGRESS); } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG) && asJsonArray.size() < 1) { // when build is success and zip relative path is not added in json ciBuild.setStatus(INPROGRESS); } else { ciBuild.setStatus(resultJson.getAsString()); // download path for (JsonElement jsonArtElement : asJsonArray) { String buildDownloadZip = jsonArtElement .getAsJsonObject() .get(FrameworkConstants.CI_JOB_BUILD_DOWNLOAD_PATH) .toString(); if (buildDownloadZip.endsWith(CI_ZIP)) { if (debugEnabled) { S_LOGGER.debug("download artifact " + buildDownloadZip); } ciBuild.setDownload(buildDownloadZip); } } } ciBuild.setId(idJson.getAsString()); String dispFormat = DD_MM_YYYY_HH_MM_SS; ciBuild.setTimeStamp(getDate(timeJson.getAsString(), dispFormat)); }
private void parseAggregators( String context, QueryMetric queryMetric, JsonArray aggregators, DateTimeZone timeZone) throws QueryException, BeanValidationException { for (int J = 0; J < aggregators.size(); J++) { JsonObject jsAggregator = aggregators.get(J).getAsJsonObject(); JsonElement name = jsAggregator.get("name"); if (name == null || name.getAsString().isEmpty()) throw new BeanValidationException( new SimpleConstraintViolation("aggregators[" + J + "]", "must have a name"), context); String aggContext = context + ".aggregators[" + J + "]"; String aggName = name.getAsString(); Aggregator aggregator = m_aggregatorFactory.createAggregator(aggName); if (aggregator == null) throw new BeanValidationException( new SimpleConstraintViolation(aggName, "invalid aggregator name"), aggContext); // If it is a range aggregator we will default the start time to // the start of the query. if (aggregator instanceof RangeAggregator) { RangeAggregator ra = (RangeAggregator) aggregator; ra.setStartTime(queryMetric.getStartTime()); } if (aggregator instanceof TimezoneAware) { TimezoneAware ta = (TimezoneAware) aggregator; ta.setTimeZone(timeZone); } if (aggregator instanceof GroupByAware) { GroupByAware groupByAware = (GroupByAware) aggregator; groupByAware.setGroupBys(queryMetric.getGroupBys()); } deserializeProperties(aggContext, jsAggregator, aggName, aggregator); validateObject(aggregator, aggContext); queryMetric.addAggregator(aggregator); } }
public RollupTask parseRollupTask(JsonObject rollupTask, String context) throws BeanValidationException, QueryException { RollupTask task = m_gson.fromJson(rollupTask.getAsJsonObject(), RollupTask.class); validateObject(task); JsonArray rollups = rollupTask.getAsJsonObject().getAsJsonArray("rollups"); if (rollups != null) { for (int j = 0; j < rollups.size(); j++) { JsonObject rollupObject = rollups.get(j).getAsJsonObject(); Rollup rollup = m_gson.fromJson(rollupObject, Rollup.class); context = context + "rollup[" + j + "]"; validateObject(rollup, context); JsonObject queryObject = rollupObject.getAsJsonObject("query"); List<QueryMetric> queries = parseQueryMetric(queryObject, context); for (int k = 0; k < queries.size(); k++) { QueryMetric query = queries.get(k); context += ".query[" + k + "]"; validateHasRangeAggregator(query, context); // Add aggregators needed for rollups SaveAsAggregator saveAsAggregator = (SaveAsAggregator) m_aggregatorFactory.createAggregator("save_as"); saveAsAggregator.setMetricName(rollup.getSaveAs()); TrimAggregator trimAggregator = (TrimAggregator) m_aggregatorFactory.createAggregator("trim"); trimAggregator.setTrim(TrimAggregator.Trim.LAST); query.addAggregator(saveAsAggregator); query.addAggregator(trimAggregator); } rollup.addQueries(queries); task.addRollup(rollup); } } return task; }
public static SiebelPropertySet JsonObjectToPropertySet(JsonObject obj, SiebelPropertySet ps) { Iterator<Entry<String, JsonElement>> iterator = obj.entrySet().iterator(); ps.setType("SiebelMessage"); SiebelPropertySet child; while (iterator.hasNext()) { JsonArray jsonArray = new JsonArray(); JsonObject jsonObject = new JsonObject(); Map.Entry mapEntry = (Map.Entry) iterator.next(); if (mapEntry != null) { JsonElement jsonelement = (JsonElement) mapEntry.getValue(); if (jsonelement.isJsonArray()) { jsonArray = jsonelement.getAsJsonArray(); child = new SiebelPropertySet(); child.setType("ListOf-" + mapEntry.getKey().toString()); for (int i = 0; i < jsonArray.size(); i++) { if (jsonArray.get(i).isJsonObject() || jsonArray.get(i).isJsonArray()) { SiebelPropertySet temp = new SiebelPropertySet(); temp.setType("" + i); child.addChild(JsonObjectToPropertySet(jsonArray.get(i).getAsJsonObject(), temp)); } else child.setProperty("" + i, jsonArray.get(i).getAsString()); } ps.addChild(child); } else if (jsonelement.isJsonObject()) { jsonObject = jsonelement.getAsJsonObject(); child = new SiebelPropertySet(); child.setType(mapEntry.getKey().toString()); ps.addChild(JsonObjectToPropertySet(jsonObject, child)); } else { ps.setProperty(mapEntry.getKey().toString(), mapEntry.getValue().toString()); } } } return ps; }
private int getTotalBuilds(CIJob job) throws PhrescoException { try { S_LOGGER.debug("Entering Method CIManagerImpl.getTotalBuilds(CIJob job)"); S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); JsonArray jsonArray = getBuildsArray(job); Gson gson = new Gson(); CIBuild ciBuild = null; if (jsonArray.size() > 0) { ciBuild = gson.fromJson(jsonArray.get(0), CIBuild.class); String buildUrl = ciBuild.getUrl(); String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); // display the jenkins running url in ci buildUrl = buildUrl.replaceAll("localhost:" + job.getJenkinsPort(), jenkinsUrl); // list String response = getJsonResponse(buildUrl + API_JSON); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(response); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT); JsonArray asJsonArray = jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS); // when build result is not known if (jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT).toString().equals(STRING_NULL)) { // it indicates the job is in progress and not yet completed return -1; // when build is success and build zip relative path is unknown } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG) && asJsonArray.size() < 1) { return -1; } else { return jsonArray.size(); } } else { return -1; // When the project is build first time, } } catch (ClientHandlerException ex) { S_LOGGER.error(ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
private List<QueryMetric> parseQueryMetric(JsonObject obj, String contextPrefix) throws QueryException, BeanValidationException { List<QueryMetric> ret = new ArrayList<QueryMetric>(); Query query; try { query = m_gson.fromJson(obj, Query.class); validateObject(query); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), "query"); } JsonArray metricsArray = obj.getAsJsonArray("metrics"); if (metricsArray == null) { throw new BeanValidationException( new SimpleConstraintViolation("metric[]", "must have a size of at least 1"), contextPrefix + "query"); } for (int I = 0; I < metricsArray.size(); I++) { String context = (!contextPrefix.isEmpty() ? contextPrefix + "." : contextPrefix) + "query.metric[" + I + "]"; try { Metric metric = m_gson.fromJson(metricsArray.get(I), Metric.class); validateObject(metric, context); long startTime = getStartTime(query, context); QueryMetric queryMetric = new QueryMetric(startTime, query.getCacheTime(), metric.getName()); queryMetric.setExcludeTags(metric.isExcludeTags()); queryMetric.setLimit(metric.getLimit()); long endTime = getEndTime(query); if (endTime > -1) queryMetric.setEndTime(endTime); if (queryMetric.getEndTime() < startTime) throw new BeanValidationException( new SimpleConstraintViolation("end_time", "must be greater than the start time"), context); queryMetric.setCacheString(query.getCacheString() + metric.getCacheString()); JsonObject jsMetric = metricsArray.get(I).getAsJsonObject(); JsonElement group_by = jsMetric.get("group_by"); if (group_by != null) { JsonArray groupBys = group_by.getAsJsonArray(); parseGroupBy(context, queryMetric, groupBys); } JsonElement aggregators = jsMetric.get("aggregators"); if (aggregators != null) { JsonArray asJsonArray = aggregators.getAsJsonArray(); if (asJsonArray.size() > 0) parseAggregators(context, queryMetric, asJsonArray, query.getTimeZone()); } JsonElement plugins = jsMetric.get("plugins"); if (plugins != null) { JsonArray pluginArray = plugins.getAsJsonArray(); if (pluginArray.size() > 0) parsePlugins(context, queryMetric, pluginArray); } JsonElement order = jsMetric.get("order"); if (order != null) queryMetric.setOrder(Order.fromString(order.getAsString(), context)); queryMetric.setTags(metric.getTags()); ret.add(queryMetric); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), context); } } return (ret); }
public JsonArray copy() { JsonArray copy = new JsonArray(list); copy.setNeedsCopy(); return copy; }
// 对接收的(click,mouseover,scroll)数据进行处理,并存入events @Path("/events/store") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public void StoreEvents(String data, @Context HttpServletRequest request) throws UnsupportedEncodingException, IOException { String schema = "browser,os,url,ip,loadtime,time,element,id,text,semantics,event,left,top,height,width"; HashSet<String> hs_log = new HashSet<String>(); // System.out.println(data); // 读取获得的json数据 JsonReader reader = Json.createReader(new StringReader(data)); JsonObject jsonobj = reader.readObject(); reader.close(); // 获取来源IP,读取url,time,events等信息 String ip = ""; String browser = ""; String os = ""; String url = ""; String loadtime = ""; ip = getRemoteHost(request); browser = jsonobj.getString("browser"); os = jsonobj.getString("os"); url = jsonobj.getString("url").split("\\?")[0]; url = url.replace("http://", "").replace("HTTP://", "").replace("https://", ""); loadtime = jsonobj.getString("loadtime"); JsonArray events = jsonobj.getJsonArray("events"); // 整理数据 for (int i = 0; i < events.size(); i++) { JsonObject obj = events.getJsonObject(i); StringBuffer elebuf = new StringBuffer(); // 先存储头信息 elebuf.append(browser); elebuf.append(","); elebuf.append(os); elebuf.append(","); elebuf.append(url); elebuf.append(","); elebuf.append(ip); elebuf.append(","); elebuf.append(loadtime); elebuf.append(","); // 各个事件的信息 elebuf.append(obj.getString("time")); elebuf.append(","); elebuf.append(obj.getString("element")); elebuf.append(","); try { elebuf.append(obj.getString("id")); } catch (Exception e) { elebuf.append(""); } elebuf.append(","); elebuf.append(obj.getString("text")); elebuf.append(","); // 在这里可以引入SVM标注,标注后在存入数据库 // 或者可以读取数据在标注 elebuf.append(""); // semantics,标注后更新 elebuf.append(","); elebuf.append(obj.getString("event")); elebuf.append(","); elebuf.append(obj.getString("left")); elebuf.append(","); elebuf.append(obj.getString("top")); elebuf.append(","); elebuf.append(obj.getString("height")); elebuf.append(","); elebuf.append(obj.getString("width")); // 添加set中,去重 hs_log.add(elebuf.toString()); } eventsDAO.insertEvents(schema, hs_log); // return "Post Data Success!"; }