How to use addStat method of org.cerberus.service.har.impl.HarService class

Best Cerberus-source code snippet using org.cerberus.service.har.impl.HarService.addStat

Source:HarService.java Github

copy

Full Screen

...121 }122 JSONObject stat = new JSONObject();123 JSONObject thirdPartyStat = new JSONObject();124 // Adding total to HAR JSON.125 stat = addStat("total", harTotalStat, stat, firstEver);126 // Adding all providers to HAR JSON.127 int nbTP = 0;128 for (Map.Entry<String, HarStat> entry : target.entrySet()) {129 String key = entry.getKey();130 HarStat val = entry.getValue();131 // Build Recap of the provider132 val = processRecap(val);133 if (key.equals(PROVIDER_INTERNAL) || key.equals(PROVIDER_UNKNOWN) || key.equals(PROVIDER_IGNORE)) {134 stat = addStat(key, val, stat, firstEver);135 } else {136 nbTP++;137 thirdPartyStat = addStat(key, val, thirdPartyStat, firstEver);138 }139 }140 stat.put(PROVIDER_THIRDPARTY, thirdPartyStat);141 // Adding total ThirdParty nb to root level142 stat.put("nbThirdParty", nbTP);143 JSONArray req = new JSONArray();144 for (JSONObject jSONObject : harTotalStat.getUrlList()) {145 jSONObject.put("start", jSONObject.getLong("start") - firstEver.getTime());146 req.put(jSONObject);147 }148 stat.put("requests", req);149 har.put("stat", stat);150 return har;151 } catch (JSONException ex) {152 LOG.error("Exception when trying to enrich har file : " + ex.toString());153 } catch (Exception ex) {154 LOG.error("Exception when trying to enrich har file.", ex);155 }156 return har;157 }158 private HashMap<String, List<String>> loadProvidersExternal() {159 HashMap<String, List<String>> rules = new HashMap<>();160 try {161 String configFile = parameterService.getParameterStringByKey("cerberus_webperf_thirdpartyfilepath", "", "");162 if (StringUtil.isNullOrEmpty(configFile)) {163 LOG.warn("Could not load config file of Web Third Party. Please define a valid parameter for cerberus_webperf_thirdpartyfilepath.");164 return rules;165 }166 if (!Files.exists(Paths.get(configFile))) {167 LOG.error("Could not load config file of Web Third Party. File " + configFile + " does not exist. Please define a valid parameter for cerberus_webperf_thirdpartyfilepath.");168 return rules;169 }170 StringBuilder fileContent = new StringBuilder();171 try (Stream<String> stream = Files.lines(Paths.get(configFile), StandardCharsets.UTF_8)) {172 stream.forEach(s -> fileContent.append(s).append("\n"));173 } catch (Exception e) {174 LOG.error(e, e);175 }176 String thirdPartyList = fileContent.toString();177// LOG.debug(thirdPartyList);178 JSONArray json = new JSONArray(thirdPartyList);179 for (int i = 0; i < json.length(); i++) {180 List<String> tmpList = new ArrayList<>();181 JSONObject thirdParty = json.getJSONObject(i);182 for (int j = 0; j < thirdParty.getJSONArray("domains").length(); j++) {183 tmpList.add(thirdParty.getJSONArray("domains").getString(j));184 }185 rules.put(thirdParty.getString("name"), tmpList);186 }187 return rules;188 } catch (JSONException ex) {189 LOG.error("JSON Exception during loading of Third Party config.", ex);190 }191 return rules;192 }193 private HashMap<String, List<String>> loadProvidersInternal(HashMap<String, List<String>> list) {194 try {195 List<Invariant> invList = new ArrayList<>();196 invList = invariantService.readByIdName("WEBPERFTHIRDPARTY");197 for (Invariant invariant : invList) {198 List<String> provInterRules = new ArrayList<>();199 String[] dList = invariant.getGp1().split(",");200 for (String domain : dList) {201 provInterRules.add(domain.trim());202 }203 list.put(invariant.getValue(), provInterRules);204 }205 return list;206 } catch (CerberusException ex) {207 LOG.error(ex, ex);208 }209 return list;210 }211 private String getProvider(String url, List<String> internalRules, List<String> ingoreRules, HashMap<String, List<String>> providersRules) {212 try {213 URL myURL = new URL(url);214 // We first try from local provider.215 for (String string : internalRules) {216 string = string.replace("*", "");217// LOG.debug("urlHost : " + myURL.getHost() + " domain : " + string + " URL : " + url);218 if (myURL.getHost().toLowerCase().endsWith(string.toLowerCase())) {219 return PROVIDER_INTERNAL;220 }221 }222 // We ignore some requests.223 for (String string : ingoreRules) {224 if ((!StringUtil.isNullOrEmpty(string)) && (myURL.getHost().toLowerCase().endsWith(string.toLowerCase()))) {225 return PROVIDER_IGNORE;226 }227 }228 // We then try from third party provider.229 for (Map.Entry<String, List<String>> entry : providersRules.entrySet()) {230 String key = entry.getKey();231 List<String> val = entry.getValue();232 for (String string : val) {233 string = string.replace("*", "");234// LOG.debug("urlHost : " + myURL.getHost() + " domain : " + string + " URL : " + url);235 if (myURL.getHost().toLowerCase().endsWith(string.toLowerCase())) {236 return key;237 }238 }239 }240 return PROVIDER_UNKNOWN;241 } catch (MalformedURLException ex) {242 Logger.getLogger(HarService.class.getName()).log(Level.SEVERE, null, ex);243 }244 return PROVIDER_UNKNOWN;245 }246 private HarStat processRecap(HarStat harStat) {247 if (harStat.getLastEnd() != null && harStat.getFirstStart() != null) {248 long totDur = harStat.getLastEnd().getTime() - harStat.getFirstStart().getTime();249 harStat.setTimeTotalDuration(Integer.valueOf(String.valueOf(totDur)));250 }251 if (harStat.getNbRequests() != 0) {252 harStat.setTimeAvg(harStat.getTimeSum() / harStat.getNbRequests());253 }254 return harStat;255 }256 private HarStat processEntry(HarStat harStat, JSONObject entry, String url, String provider, boolean isTotal) {257 try {258 String responseType = guessType(entry);259 List<String> tempList;260 int httpS = entry.getJSONObject("response").getInt("status");261 int reqSize = 0;262 if (entry.getJSONObject("response").getInt("headersSize") > 0) {263 reqSize = reqSize + entry.getJSONObject("response").getInt("headersSize");264 }265 if (entry.getJSONObject("response").getInt("bodySize") > 0) {266 reqSize = reqSize + entry.getJSONObject("response").getInt("bodySize");267 }268 int reqTime = entry.getInt("time");269 URL curUrl = new URL(url);270 HashMap<String, String> tmpHost = harStat.getHosts();271 tmpHost.put(curUrl.getHost(), "");272 harStat.setHosts(tmpHost);273 // Dates are in javascript format : 2020-02-18T20:53:11.118Z274 String startD = entry.getString("startedDateTime");275 long startL = new SimpleDateFormat(DATE_FORMAT).parse(startD).getTime();276 if (startD != null) {277 long endDate = new SimpleDateFormat(DATE_FORMAT).parse(startD).getTime() + reqTime;278 if (harStat.getFirstStartS() == null || startD.compareTo(harStat.getFirstStartS()) < 0) {279 harStat.setFirstStartS(startD);280 harStat.setFirstStart(new SimpleDateFormat(DATE_FORMAT).parse(startD));281 harStat.setFirstEnd(new Date(endDate));282 harStat.setFirstURL(url);283 harStat.setFirstDuration(reqTime);284 }285 if (harStat.getLastStartS() == null || harStat.getLastEnd().before(new Date(endDate))) {286 harStat.setLastStartS(startD);287 harStat.setLastStart(new SimpleDateFormat(DATE_FORMAT).parse(startD));288 harStat.setLastEnd(new Date(endDate));289 harStat.setLastURL(url);290 harStat.setLastDuration(reqTime);291 }292 }293 if (isTotal) {294 JSONObject urlEntry = new JSONObject();295 urlEntry.put("domain", curUrl.getHost());296 urlEntry.put("size", reqSize);297 urlEntry.put("start", startL);298 urlEntry.put("time", reqTime);299 urlEntry.put("url", url);300 urlEntry.put("contentType", responseType);301 urlEntry.put("httpStatus", httpS);302 urlEntry.put("provider", provider);303 harStat.appendUrlList(urlEntry);304 }305 switch (responseType) {306 case "js":307 if (reqSize > 0) {308 harStat.setJsSizeSum(harStat.getJsSizeSum() + reqSize);309 }310 if (reqSize > harStat.getJsSizeMax()) {311 harStat.setJsSizeMax(reqSize);312 harStat.setUrlJsSizeMax(url);313 }314 harStat.setJsRequests(harStat.getJsRequests() + 1);315 tempList = harStat.getJsList();316 tempList.add(url);317 harStat.setJsList(tempList);318 break;319 case "css":320 if (reqSize > 0) {321 harStat.setCssSizeSum(harStat.getCssSizeSum() + reqSize);322 }323 if (reqSize > harStat.getCssSizeMax()) {324 harStat.setCssSizeMax(reqSize);325 harStat.setUrlCssSizeMax(url);326 }327 harStat.setCssRequests(harStat.getCssRequests() + 1);328 tempList = harStat.getCssList();329 tempList.add(url);330 harStat.setCssList(tempList);331 break;332 case "html":333 if (reqSize > 0) {334 harStat.setHtmlSizeSum(harStat.getHtmlSizeSum() + reqSize);335 }336 if (reqSize > harStat.getHtmlSizeMax()) {337 harStat.setHtmlSizeMax(reqSize);338 harStat.setUrlHtmlSizeMax(url);339 }340 harStat.setHtmlRequests(harStat.getHtmlRequests() + 1);341 tempList = harStat.getHtmlList();342 tempList.add(url);343 harStat.setHtmlList(tempList);344 break;345 case "img":346 if (reqSize > 0) {347 harStat.setImgSizeSum(harStat.getImgSizeSum() + reqSize);348 }349 if (reqSize > harStat.getImgSizeMax()) {350 harStat.setImgSizeMax(reqSize);351 harStat.setUrlImgSizeMax(url);352 }353 harStat.setImgRequests(harStat.getImgRequests() + 1);354 tempList = harStat.getImgList();355 tempList.add(url);356 harStat.setImgList(tempList);357 break;358 case "content":359 if (reqSize > 0) {360 harStat.setContentSizeSum(harStat.getContentSizeSum() + reqSize);361 }362 if (reqSize > harStat.getContentSizeMax()) {363 harStat.setContentSizeMax(reqSize);364 harStat.setUrlContentSizeMax(url);365 }366 harStat.setContentRequests(harStat.getContentRequests() + 1);367 tempList = harStat.getContentList();368 tempList.add(url);369 harStat.setContentList(tempList);370 break;371 case "font":372 if (reqSize > 0) {373 harStat.setFontSizeSum(harStat.getFontSizeSum() + reqSize);374 }375 if (reqSize > harStat.getFontSizeMax()) {376 harStat.setFontSizeMax(reqSize);377 harStat.setUrlFontSizeMax(url);378 }379 harStat.setFontRequests(harStat.getFontRequests() + 1);380 tempList = harStat.getFontList();381 tempList.add(url);382 harStat.setFontList(tempList);383 break;384 case "media":385 if (reqSize > 0) {386 harStat.setMediaSizeSum(harStat.getMediaSizeSum() + reqSize);387 }388 if (reqSize > harStat.getMediaSizeMax()) {389 harStat.setMediaSizeMax(reqSize);390 harStat.setUrlMediaSizeMax(url);391 }392 harStat.setMediaRequests(harStat.getMediaRequests() + 1);393 tempList = harStat.getMediaList();394 tempList.add(url);395 harStat.setMediaList(tempList);396 break;397 case "other":398 if (reqSize > 0) {399 harStat.setOtherSizeSum(harStat.getOtherSizeSum() + reqSize);400 }401 if (reqSize > harStat.getOtherSizeMax()) {402 harStat.setOtherSizeMax(reqSize);403 harStat.setUrlOtherSizeMax(url);404 }405 harStat.setOtherRequests(harStat.getOtherRequests() + 1);406 tempList = harStat.getOtherList();407 tempList.add(url);408 harStat.setOtherList(tempList);409 break;410 }411 HashMap<Integer, Integer> tmpStat = harStat.getHttpStatusCode();412 if (httpS == 0) {413 harStat.setNbError(harStat.getNbError() + 1);414 } else {415 if (tmpStat.containsKey(httpS)) {416 tmpStat.put(httpS, tmpStat.get(httpS) + 1);417 } else {418 tmpStat.put(httpS, 1);419 }420 harStat.setHttpStatusCode(tmpStat);421 }422 harStat.setNbRequests(harStat.getNbRequests() + 1);423 if (reqSize > 0) {424 harStat.setSizeSum(harStat.getSizeSum() + reqSize);425 }426 if (reqSize > 0 && reqSize > harStat.getSizeMax()) {427 harStat.setSizeMax(reqSize);428 harStat.setUrlSizeMax(url);429 }430 harStat.setTimeSum(harStat.getTimeSum() + reqTime);431 if (reqTime > 0 && reqTime > harStat.getTimeMax()) {432 harStat.setTimeMax(reqTime);433 harStat.setUrlTimeMax(url);434 }435 return harStat;436 } catch (JSONException ex) {437 LOG.error("Exception when trying to process entry and enrich HarStat.", ex);438 } catch (Exception ex) {439 LOG.error("Exception when trying to process entry and enrich HarStat.", ex);440 LOG.error(ex, ex);441 }442 return harStat;443 }444 /**445 * Transform the HarStat Object to a JSONObject and add it to stat Object446 * under statKey value.447 *448 * @param har449 * @param domains450 * @param system451 * @return452 */453 private JSONObject addStat(String statKey, HarStat harStat, JSONObject stat, Date firstEver) {454 try {455 JSONObject total = new JSONObject();456 JSONObject type = new JSONObject();457 JSONObject js = new JSONObject();458 js.put("sizeSum", harStat.getJsSizeSum());459 js.put("sizeMax", harStat.getJsSizeMax());460 js.put("requests", harStat.getJsRequests());461 js.put("urlMax", harStat.getUrlJsSizeMax());462// js.put("url", harStat.getJsList());463 type.put("js", js);464 JSONObject css = new JSONObject();465 css.put("sizeSum", harStat.getCssSizeSum());466 css.put("sizeMax", harStat.getCssSizeMax());467 css.put("requests", harStat.getCssRequests());...

Full Screen

Full Screen

addStat

Using AI Code Generation

copy

Full Screen

1import org.cerberus.service.har.impl.HarService;2import org.cerberus.service.har.impl.HarServiceImpl;3HarService harService = new HarServiceImpl();4harService.addStat("MY_STAT", 1);5import org.cerberus.service.har.impl.HarService;6import org.cerberus.service.har.impl.HarServiceImpl;7HarService harService = new HarServiceImpl();8harService.getStat("MY_STAT");9import org.cerberus.service.har.impl.HarService;10import org.cerberus.service.har.impl.HarServiceImpl;11HarService harService = new HarServiceImpl();12harService.getStats();13import org.cerberus.service.har.impl.HarService;14import org.cerberus.service.har.impl.HarServiceImpl;15HarService harService = new HarServiceImpl();16harService.getStat("MY_STAT");17import org.cerberus.service.har.impl.HarService;18import org.cerberus.service.har.impl.HarServiceImpl;19HarService harService = new HarServiceImpl();20harService.getStats();21import org.cerberus.service.har.impl.HarService;22import org.cerberus.service.har.impl.HarServiceImpl;23HarService harService = new HarServiceImpl();24harService.getStat("MY_STAT");25import org.cerberus.service.har.impl.HarService;26import org.cerberus.service.har.impl.HarServiceImpl;27HarService harService = new HarServiceImpl();28harService.getStats();29import org.cerberus.service.har.impl.HarService;30import org.cerberus.service.har.impl.HarServiceImpl;31HarService harService = new HarServiceImpl();32harService.getStat("MY_STAT");

Full Screen

Full Screen

addStat

Using AI Code Generation

copy

Full Screen

1package org.cerberus.service.har.impl;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.cerberus.service.har.HarService;7import org.cerberus.service.har.model.Har;8import org.cerberus.service.har.model.HarEntry;9import org.cerberus.service.har.model.HarLog;10import org.cerberus.service.har.model.HarPage;11import org.cerberus.service.har.model.HarRequest;12import org.cerberus.service.har.model.HarResponse;13import org.cerberus.service.har.model.HarStat;14import org.cerberus.service.har.model.HarStatType;15import org.cerberus.service.har.model.HarTimings;16import com.fasterxml.jackson.databind.ObjectMapper;17public class HarServiceImpl implements HarService {18 private static final String HAR_FILE = "C:\\Users\\cerberus\\Desktop\\cerberus.har";19 public void addStat(String name, String value, HarStatType type) {20 ObjectMapper mapper = new ObjectMapper();21 try {22 Har har = mapper.readValue(new File(HAR_FILE), Har.class);23 HarLog log = har.getLog();24 List<HarPage> pages = log.getPages();25 HarPage page = pages.get(0);26 HarStat stat = new HarStat();27 stat.setName(name);28 stat.setValue(value);29 stat.setType(type);30 List<HarStat> stats = page.getStats();31 if (stats == null) {32 stats = new ArrayList<>();33 }34 stats.add(stat);35 page.setStats(stats);36 mapper.writeValue(new File(HAR_FILE), har);37 } catch (IOException e) {38 e.printStackTrace();39 }40 }41 public void addEntry(String url, String method, int status, String statusText, long time) {42 ObjectMapper mapper = new ObjectMapper();43 try {44 Har har = mapper.readValue(new File(HAR_FILE), Har.class);45 HarLog log = har.getLog();46 List<HarEntry> entries = log.getEntries();47 if (entries == null) {48 entries = new ArrayList<>();49 }50 HarEntry entry = new HarEntry();51 entry.setPageref("page_1");52 HarRequest request = new HarRequest();53 request.setMethod(method);

Full Screen

Full Screen

addStat

Using AI Code Generation

copy

Full Screen

1import org.cerberus.service.har.impl.HarService;2import org.cerberus.service.har.impl.HarStat;3HarService harService = new HarService();4harService.addStat(new HarStat("myStat", "myValue", "myCategory"));5harService.save();6import org.cerberus.service.har.impl.HarService;7import org.cerberus.service.har.impl.HarStat;8HarService harService = new HarService();9harService.addStat(new HarStat("myStat", "myValue", "myCategory"));10harService.save();11import org.cerberus.service.har.impl.HarService;12import org.cerberus.service.har.impl.HarStat;13HarService harService = new HarService();14harService.addStat(new HarStat("myStat", "myValue", "myCategory"));15harService.save();16import org.cerberus.service.har.impl.HarService;17import org.cerberus.service.har.impl.HarStat;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful