if (reportAccountList.size() == 0) {
return null;
} else {
// Creating a new Kratu with the base Account Info
Kratu kratu = new Kratu(topAccountId, account, day);
// Process ReportAccount Info
for (ReportAccount reportAccount : reportAccountList) {
kratu.addSpend(reportAccount.getCost());
kratu.addConversions(reportAccount.getConversions());
// SEARCH_NETWORK Info
if (reportAccount.getAdNetwork() != null
&& reportAccount.getAdNetwork().equals(SEARCH_NETWORK)) {
kratu.setTotalClicksSearch(reportAccount.getClicks());
kratu.setImpressionsSearch(reportAccount.getImpressions());
if (reportAccount.getSearchImpressionShareBigDecimal() != null &&
reportAccount.getSearchImpressionShareBigDecimal().compareTo(BigDecimal.ZERO) == 1) {
kratu.setElegibleImpressionsSearch(new BigDecimal(kratu.getImpressionsSearch()).divide(
reportAccount.getSearchImpressionShareBigDecimal().divide(BIGDECIMAL_100), SCALE, ROUNDING));
kratu.setLostImpressionsDueToBudgetSearch(reportAccount.getSearchLostISBudgetBigDecimal().divide(
BIGDECIMAL_100).multiply(kratu.getElegibleImpressionsSearch()));
kratu.setLostImpressionsDueToBidAdRankSearch(reportAccount.getSearchLostISRankBigDecimal().divide(
BIGDECIMAL_100).multiply(kratu.getElegibleImpressionsSearch()));
}
kratu.setCtrSearch(reportAccount.getCtrBigDecimal());
kratu.setAverageCpcSearch(reportAccount.getAvgCpc());
kratu.setAverageCpmSearch(reportAccount.getAvgCpm());
kratu.setAveragePositionSearch(reportAccount.getAvgPositionBigDecimal());
}
// DISPLAY_NETWORK Info
if (reportAccount.getAdNetwork() != null
&& reportAccount.getAdNetwork().equals(DISPLAY_NETWORK)) {
kratu.setTotalClicksDisplay(reportAccount.getClicks());
kratu.setImpressionsDisplay(reportAccount.getImpressions());
if (reportAccount.getContentImpressionShareBigDecimal() != null &&
reportAccount.getContentImpressionShareBigDecimal().compareTo(BigDecimal.ZERO) == 1) {
kratu.setElegibleImpressionsDisplay(new BigDecimal(kratu.getImpressionsDisplay()).divide(
reportAccount.getContentImpressionShareBigDecimal().divide(BIGDECIMAL_100), SCALE, ROUNDING));
kratu.setLostImpressionsDueToBudgetDisplay(reportAccount.getContentLostISBudgetBigDecimal().divide(
BIGDECIMAL_100).multiply(kratu.getElegibleImpressionsDisplay()));
kratu.setLostImpressionsDueToBidAdRankDisplay(reportAccount.getContentLostISRankBigDecimal().divide(
BIGDECIMAL_100).multiply(kratu.getElegibleImpressionsDisplay()));
}
kratu.setCtrDisplay(reportAccount.getCtrBigDecimal());
kratu.setAverageCpcDisplay(reportAccount.getAvgCpc());
kratu.setAverageCpmDisplay(reportAccount.getAvgCpm());
kratu.setAveragePositionDisplay(reportAccount.getAvgPositionBigDecimal());
}
}
if (kratu.getImpressionsDisplay() + kratu.getImpressionsSearch() > 0) {
kratu.setAccountActive("Yes");
}
// Process ReportCampaign Info
List<ReportCampaign> reportCampaignList = storageHelper.getReportByAccountId(ReportCampaign.class, accountId, day, day);
for (ReportCampaign reportCampaign : reportCampaignList) {
if (reportCampaign.getStatus().equals(ACTIVE)) {
kratu.addNumberOfActiveCampaigns(BigDecimal.ONE);
}
if (reportCampaign.getBudget() != null) {
kratu.addSumBudget(reportCampaign.getBudget());
}
}
// Process ReportAdGroup Info
List<ReportAdGroup> reportAdGroupList = storageHelper.getReportByAccountId(ReportAdGroup.class, accountId, day, day);
for (ReportAdGroup reportAdGroup : reportAdGroupList) {
if (reportAdGroup.getStatus().equals(ENABLE)) {
kratu.addNumberOfActiveAdGroups(BigDecimal.ONE);
}
}
// Process ReportAd Info
List<ReportAd> reportAdList = storageHelper.getReportByAccountId(ReportAd.class, accountId, day, day);
Map<Long, Integer> activeAdsPerAdGroup = new HashMap<Long, Integer>();
for (ReportAd reportAd : reportAdList) {
if (reportAd.getAdState().equals(ENABLE)) {
kratu.addNumberOfActiveAds(BigDecimal.ONE);
// Counting the activeAdsPerAdGroup
if (activeAdsPerAdGroup.containsKey(reportAd.getAdGroupId())) {
activeAdsPerAdGroup.put(reportAd.getAdGroupId(),
activeAdsPerAdGroup.get(reportAd.getAdGroupId()) + 1);
} else {
activeAdsPerAdGroup.put(reportAd.getAdGroupId(), 1);
}
}
if (reportAd.getCreativeApprovalStatus().equals(DISAPPROVED)) {
kratu.addNumberOfDisapprovedAds(BigDecimal.ONE);
}
}
// Process activeAdsPerAdGroup
for (Integer activeAds : activeAdsPerAdGroup.values()) {
if (activeAds == 1) {
kratu.addNumberOfAdgroupsWithoneActiveAd(BigDecimal.ONE);
}
if (activeAds == 2) {
kratu.addNumberOfAdgroupsWithTwoActiveAds(BigDecimal.ONE);
}
}
// Process ReportKeyword Info
List<ReportKeyword> reportKeywordList = storageHelper.getReportByAccountId(ReportKeyword.class, accountId, day, day);
Long sumImpressions = 0l;
BigDecimal totalPositions = BigDecimal.ZERO;
BigDecimal totalWeight = BigDecimal.ZERO;
for (ReportKeyword reportKeyword : reportKeywordList) {
if (reportKeyword.getStatus().equals(ENABLE)) {
if (!reportKeyword.isNegative()) {
kratu.addNumberOfPositiveActiveKeywords(BigDecimal.ONE);
// numberOfActiveGoodQualityScoreKeywords: QS > 7
if (reportKeyword.getQualityScoreAsBigDecimal().compareTo(BigDecimal.valueOf(7)) == 1) {
kratu.addNumberOfActiveGoodQualityScoreKeywords(BigDecimal.ONE);
}
// numberOfActiveAverageQualityScoreKeywords: QS>4 && <=7
if (reportKeyword.getQualityScoreAsBigDecimal().compareTo(BigDecimal.valueOf(4)) == 1
&& reportKeyword.getQualityScoreAsBigDecimal().compareTo(BigDecimal.valueOf(7)) < 1) {
kratu.addNumberOfActiveAverageQualityScoreKeywords(BigDecimal.ONE);
}
// numberOfActivePoorQualityScoreKeywords: QS <= 4
if (reportKeyword.getQualityScoreAsBigDecimal().compareTo(BigDecimal.valueOf(4)) < 1) {
kratu.addNumberOfActivePoorQualityScoreKeywords(BigDecimal.ONE);
}
if (reportKeyword.getKeywordMatchType().equals(BROAD)) {
kratu.addNumberOfActiveBroadMatchingKeywords(BigDecimal.ONE);
}
if (reportKeyword.getKeywordMatchType().equals(PHRASE)) {
kratu.addNumberOfActivePhraseMatchingKeywords(BigDecimal.ONE);
}
if (reportKeyword.getKeywordMatchType().equals(EXACT)) {
kratu.addNumberOfActiveExactMatchingKeywords(BigDecimal.ONE);
}
sumImpressions += reportKeyword.getImpressions();
totalPositions = totalPositions.add(reportKeyword.getAvgPositionBigDecimal().multiply(
BigDecimal.valueOf(reportKeyword.getImpressions())));
totalWeight = totalWeight.add(reportKeyword.getQualityScoreAsBigDecimal().multiply(
BigDecimal.valueOf(reportKeyword.getImpressions())));
} else {
kratu.addNumberOfNegativeActiveKeywords(BigDecimal.ONE);
kratu.addNumberOfAdGroupNegativeActiveKeywords(BigDecimal.ONE);
}
}
}
if (sumImpressions > 0) {
kratu.setWeightedAverageKeywordPosition(totalPositions.divide(BigDecimal.valueOf(sumImpressions), SCALE, ROUNDING));
kratu.setWeightedAverageQualityScore(totalWeight.divide(BigDecimal.valueOf(sumImpressions), SCALE, ROUNDING));
}
// Process ReportAdExtension Info
List<ReportAdExtension> reportAdExtensionsList = storageHelper.getReportByAccountId(ReportAdExtension.class, accountId, day, day);
for (ReportAdExtension reportAdExtension : reportAdExtensionsList) {
if (reportAdExtension.getStatus().equals(ACTIVE)) {
if (reportAdExtension.getAdExtensionType().equals(LOCATION_EXTENSION)) {
kratu.addNumberOfCampaignsWithLocationExtensionEnabled(BigDecimal.ONE);
}
if (reportAdExtension.getAdExtensionType().equals(MOBILE_EXTENSION)) {
kratu.addNumberOfCampaignsWithCallExtensionEnabled(BigDecimal.ONE);
}
if (reportAdExtension.getAdExtensionType().equals(SITE_LINKS_EXTENSION)) {
kratu.addNumberOfCampaignsWithSiteLinksEnabled(BigDecimal.ONE);
}
}
}
// Process ReportCampaignNegativeKeyword Info
List<ReportCampaignNegativeKeyword> reportCampaignNegativeKeywordList =
storageHelper.getReportCampaignNegativeKeywordByAccountAndEndDateInRange(accountId, day, day);
kratu.addNumberOfNegativeActiveKeywords(new BigDecimal(reportCampaignNegativeKeywordList.size()));
kratu.addNumberOfCampaignNegativeActiveKeywords(new BigDecimal(reportCampaignNegativeKeywordList.size()));
kratu.setSpend(kratu.getSpend().setScale(SCALE, ROUNDING));
return kratu;
}
}