/******************************************************************************* * Copyright 2014 Miami-Dade County * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package org.sharegov.cirm.stats; import java.util.Date; import java.util.Map; import mjson.Json; import org.sharegov.cirm.utils.GenUtilsMod; /** * Parses CirmStatistics Json. * * @author Thomas Hilpold */ public class CirmStatisticsParser { /** * Parses an array of Json objects like below into a CirmStatistics object * (Empty array allowed) * { LegacyEmulator/create/legacy:311DUMP: { lastSuccessTime: "2014-07-03T15:55:55.129-0400", lastFailureException: null, failureCount: 0, lastFailureId: null, lastFailureTime: null, successCount: 5, lastFailureMessage: null, firstEntryTime: "2014-07-03T09:46:55.023-0400", lastSuccessId: "14-10409192" } }, * @param cirmStatisticsJson an array of statsKey Strings / Sta * @return * @throws all sorts of exceptions and fails easily, so try for it. */ public CirmStatistics read(Json cirmStatisticsJson) { CirmStatistics stats = new CirmStatistics(); int counter = 0; for (Json statsKeyValObject : cirmStatisticsJson.asJsonList()) { Map.Entry<String, Json> statsKeyValObjectEntry = statsKeyValObject.asJsonMap().entrySet().iterator().next(); String keyAsString = statsKeyValObjectEntry.getKey(); String[] componentActionType = parseStatsKeyFromString(keyAsString); //parse statsvalue CirmStatistics.StatsValue v = stats.getEntry(componentActionType[0], componentActionType[1], componentActionType[2]); Json valObject = statsKeyValObjectEntry.getValue(); parseStatsValue(valObject, v); counter ++; } System.out.println("read CirmStatisticsJson read " + counter + " keys and values"); return stats; } /** * Mandatory with a proper value are: * firstEntryTime * successCount * failureCount * * All others' keys must be there, but value will be set to null if non string value (e.g.null) * @param statsValObject * @param statsValue */ public void parseStatsValue(Json statsValObject, CirmStatistics.StatsValue statsValue) { Date fet = GenUtilsMod.parseDate(statsValObject.at("firstEntryTime").asString()); statsValue.setFirstEntryTime(fet); Date lst = statsValObject.at("lastSuccessTime").isString()? GenUtilsMod.parseDate(statsValObject.at("lastSuccessTime").asString()) : null; statsValue.setLastSuccessTime(lst); Date lft = statsValObject.at("lastFailureTime").isString()? GenUtilsMod.parseDate(statsValObject.at("lastFailureTime").asString()) : null; statsValue.setLastFailureTime(lft); statsValue.setSuccessCount(statsValObject.at("successCount").asLong()); statsValue.setLastSuccessId(statsValObject.at("lastSuccessId").isString()? statsValObject.at("lastSuccessId").asString() : null); statsValue.setFailureCount(statsValObject.at("failureCount").asLong()); statsValue.setLastFailureException(statsValObject.at("lastFailureException").isString()? statsValObject.at("lastFailureException").asString() : null); statsValue.setLastFailureId(statsValObject.at("lastFailureId").isString()? statsValObject.at("lastFailureId").asString() : null); statsValue.setLastFailureMessage(statsValObject.at("lastFailureMessage").isString()? statsValObject.at("lastFailureMessage").asString() : null); } /** * parses a key as string expected as follows: component/action/type * it will fail, if no two slashes are there. * * @param statsKeyString * @return */ public String[] parseStatsKeyFromString(String statsKeyString) { String [] componentActionType = new String[3]; //find the first / starting before first char int componentEnd = statsKeyString.indexOf('/'); //component componentActionType[0] = statsKeyString.substring(0, componentEnd); statsKeyString = statsKeyString.substring(componentEnd + 1); //action int actionEnd = statsKeyString.indexOf('/'); componentActionType[1] = statsKeyString.substring(0, actionEnd); statsKeyString = statsKeyString.substring(actionEnd + 1); //type componentActionType[2] = statsKeyString; return componentActionType; } }