@Test public void testExecute() { final UsageMetricStreamSummaryRequest request = new UsageMetricStreamSummaryRequest(3, 4L); final List<DailyUsageSummary> results = new ArrayList<DailyUsageSummary>(); Calendar cal; cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); final Date yesterday = DateDayExtractor.getStartOfDay(cal.getTime()); cal = Calendar.getInstance(); cal.add(Calendar.DATE, -2); final Date twoDaysAgo = DateDayExtractor.getStartOfDay(cal.getTime()); cal = Calendar.getInstance(); cal.add(Calendar.DATE, -3); final Date threeDaysAgo = DateDayExtractor.getStartOfDay(cal.getTime()); cal = Calendar.getInstance(); cal.add(Calendar.DATE, -4); final Date fourDaysAgo = DateDayExtractor.getStartOfDay(cal.getTime()); results.add(new DailyUsageSummary(10L, 20L, 30L, 40L, 50L, 60L, 70L, fourDaysAgo, 1308238511000L, 80L, 91L, 101L, 121L)); results.add(new DailyUsageSummary(20L, 30L, 40L, 50L, 60L, 70L, 80L, threeDaysAgo, 1308238511000L, 90L, 100L, 110L, 130L)); results.add(new DailyUsageSummary(30L, 40L, 50L, 60L, 70L, 80L, 90L, twoDaysAgo, 1308238511000L, 100L, 110L, 120L, 140L)); results.add(new DailyUsageSummary(40L, 50L, 60L, 70L, 80L, 90L, 110L, yesterday, 1308238511000L, 110L, 120L, 130L, 150L)); context.checking(new Expectations() { { oneOf(summaryDataMapper).execute(with(request)); will(returnValue(results)); oneOf(weekdaysInDateRangeStrategy).getWeekdayCountBetweenDates( with(DateDayExtractor.getStartOfDay(threeDaysAgo)), with(DateDayExtractor.getStartOfDay(new Date()))); will(returnValue(10)); allowing(dayOfWeekStrategy).isWeekday(with(any(Date.class))); will(returnValue(true)); } }); UsageMetricSummaryDTO result = (UsageMetricSummaryDTO) sut.execute(TestContextCreator .createPrincipalActionContext(request, null)); Assert.assertEquals(10, result.getWeekdayRecordCount()); Assert.assertEquals(9, result.getAverageDailyUniqueVisitorCount()); Assert.assertEquals(12, result.getAverageDailyPageViewCount()); Assert.assertEquals(15, result.getAverageDailyStreamViewerCount()); Assert.assertEquals(18, result.getAverageDailyStreamViewCount()); Assert.assertEquals(21, result.getAverageDailyStreamContributorCount()); Assert.assertEquals(24, result.getAverageDailyMessageCount()); Assert.assertEquals(new Long(120), result.getTotalActivityCount()); Assert.assertEquals(new Long(130), result.getTotalCommentCount()); Assert.assertEquals(new Long(150), result.getTotalContributorCount()); Assert.assertEquals(1, result.getAverageDailyCommentPerActivityCount()); }
@Override public Serializable execute(final PrincipalActionContext inActionContext) { UsageMetricStreamSummaryRequest request = (UsageMetricStreamSummaryRequest) inActionContext.getParams(); List<DailyUsageSummary> results = summaryDataMapper.execute(request); logger.info("Found " + results.size() + " summary results"); UsageMetricSummaryDTO result = new UsageMetricSummaryDTO(); List<DailyUsageSummary> dailyStats = new ArrayList<DailyUsageSummary>(); if (results.size() == 0) { return result; } long msgCount = 0; long pageViewCount = 0; long streamContributorCount = 0; long streamViewCount = 0; long streamViewerCount = 0; long uniqueVisitorCount = 0; long avgActivityResponseTime = 0; long startingCommentCount = 0, finalCommentCount = 0; long startingActivityCount = 0, finalActivityCount = 0; Long totalActivityCount = null; Long totalCommentCount = null; Long totalContributorCount = null; Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -request.getNumberOfDays()); Date oldestAllowableReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); day = Calendar.getInstance(); day.add(Calendar.DATE, -1); Date latestReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); Date summaryDate; Date oldestAvailableReportDate = null; Date newestAvailableReportDate = null; logger.debug("Looking for data between " + oldestAllowableReportDate + " and " + latestReportDate); for (DailyUsageSummary dus : results) { summaryDate = DateDayExtractor.getStartOfDay(dus.getUsageDate()); if (summaryDate.before(oldestAllowableReportDate) || summaryDate.after(latestReportDate)) { logger.debug("Can't use data for " + summaryDate); continue; } dus.setUsageDate(summaryDate); dailyStats.add(dus); if (newestAvailableReportDate == null || summaryDate.after(newestAvailableReportDate)) { newestAvailableReportDate = summaryDate; totalActivityCount = dus.getTotalActivityCount(); totalCommentCount = dus.getTotalCommentCount(); totalContributorCount = dus.getTotalContributorCount(); finalCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); finalActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } if (oldestAvailableReportDate == null || summaryDate.before(oldestAvailableReportDate)) { oldestAvailableReportDate = summaryDate; startingCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); startingActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } msgCount += dus.getMessageCount(); pageViewCount += dus.getPageViewCount(); streamContributorCount += dus.getStreamContributorCount(); streamViewCount += dus.getStreamViewCount(); streamViewerCount += dus.getStreamViewerCount(); uniqueVisitorCount += dus.getUniqueVisitorCount(); avgActivityResponseTime += dus.getAvgActivityResponseTime(); } long weekdaysCount = 0; if (oldestAvailableReportDate != null) { weekdaysCount = weekdaysInDateRangeStrategy.getWeekdayCountBetweenDates(oldestAvailableReportDate, DateDayExtractor.getStartOfDay(new Date())); } result.setWeekdayRecordCount(weekdaysCount); result.setTotalActivityCount(totalActivityCount); result.setTotalCommentCount(totalCommentCount); result.setTotalContributorCount(totalContributorCount); logger.debug("Found " + weekdaysCount + " weekdays between " + oldestAvailableReportDate + " and " + latestReportDate); if (weekdaysCount > 0) { result.setAverageDailyMessageCount(Math.round(Math.ceil(msgCount * 1.0 / weekdaysCount))); result.setAverageDailyPageViewCount(Math.round(Math.ceil(pageViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamContributorCount(Math.round(Math.ceil(streamContributorCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewCount(Math.round(Math.ceil(streamViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewerCount(Math.round(Math.ceil(streamViewerCount * 1.0 / weekdaysCount))); result.setAverageDailyUniqueVisitorCount(Math.round(Math.ceil(uniqueVisitorCount * 1.0 / weekdaysCount))); result.setAverageDailyActivityResponseTime(Math.round(Math.ceil(avgActivityResponseTime * 1.0 / weekdaysCount))); long averageCommentPerActivity = 0; if (weekdaysCount > 1) { double top = (finalCommentCount - startingCommentCount) * 1.0; double bottom = (finalActivityCount - startingActivityCount) * 1.0; if (bottom > 0) { averageCommentPerActivity = Math.round(Math.ceil(top / (bottom * (weekdaysCount - 1)))); } } result.setAverageDailyCommentPerActivityCount(averageCommentPerActivity); } result.setDailyStatistics(new ArrayList<DailyUsageSummary>()); if (dailyStats.size() > 0) { Calendar cal = Calendar.getInstance(); cal.setTime(oldestAvailableReportDate); Date reportDate; long lastDateInMs = latestReportDate.getTime(); int dataIndex = 0; for (reportDate = cal.getTime(); reportDate.getTime() <= lastDateInMs && result.getDailyStatistics().size() < weekdaysCount; cal.add(Calendar.DATE, 1), reportDate = cal .getTime()) { if (!dayOfWeekStrategy.isWeekday(reportDate)) { continue; } if (dataIndex < dailyStats.size()) { DailyUsageSummary record = dailyStats.get(dataIndex); if (DateDayExtractor.getStartOfDay(record.getUsageDate()).getTime() == reportDate.getTime()) { result.getDailyStatistics().add(record); dataIndex++; } else { result.getDailyStatistics().add(null); } } else { result.getDailyStatistics().add(null); } } } return result; }
GetUsageMetricSummaryExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) { UsageMetricStreamSummaryRequest request = (UsageMetricStreamSummaryRequest) inActionContext.getParams(); List<DailyUsageSummary> results = summaryDataMapper.execute(request); logger.info("Found " + results.size() + " summary results"); UsageMetricSummaryDTO result = new UsageMetricSummaryDTO(); List<DailyUsageSummary> dailyStats = new ArrayList<DailyUsageSummary>(); if (results.size() == 0) { return result; } long msgCount = 0; long pageViewCount = 0; long streamContributorCount = 0; long streamViewCount = 0; long streamViewerCount = 0; long uniqueVisitorCount = 0; long avgActivityResponseTime = 0; long startingCommentCount = 0, finalCommentCount = 0; long startingActivityCount = 0, finalActivityCount = 0; Long totalActivityCount = null; Long totalCommentCount = null; Long totalContributorCount = null; Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -request.getNumberOfDays()); Date oldestAllowableReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); day = Calendar.getInstance(); day.add(Calendar.DATE, -1); Date latestReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); Date summaryDate; Date oldestAvailableReportDate = null; Date newestAvailableReportDate = null; logger.debug("Looking for data between " + oldestAllowableReportDate + " and " + latestReportDate); for (DailyUsageSummary dus : results) { summaryDate = DateDayExtractor.getStartOfDay(dus.getUsageDate()); if (summaryDate.before(oldestAllowableReportDate) || summaryDate.after(latestReportDate)) { logger.debug("Can't use data for " + summaryDate); continue; } dus.setUsageDate(summaryDate); dailyStats.add(dus); if (newestAvailableReportDate == null || summaryDate.after(newestAvailableReportDate)) { newestAvailableReportDate = summaryDate; totalActivityCount = dus.getTotalActivityCount(); totalCommentCount = dus.getTotalCommentCount(); totalContributorCount = dus.getTotalContributorCount(); finalCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); finalActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } if (oldestAvailableReportDate == null || summaryDate.before(oldestAvailableReportDate)) { oldestAvailableReportDate = summaryDate; startingCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); startingActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } msgCount += dus.getMessageCount(); pageViewCount += dus.getPageViewCount(); streamContributorCount += dus.getStreamContributorCount(); streamViewCount += dus.getStreamViewCount(); streamViewerCount += dus.getStreamViewerCount(); uniqueVisitorCount += dus.getUniqueVisitorCount(); avgActivityResponseTime += dus.getAvgActivityResponseTime(); } long weekdaysCount = 0; if (oldestAvailableReportDate != null) { weekdaysCount = weekdaysInDateRangeStrategy.getWeekdayCountBetweenDates(oldestAvailableReportDate, DateDayExtractor.getStartOfDay(new Date())); } result.setWeekdayRecordCount(weekdaysCount); result.setTotalActivityCount(totalActivityCount); result.setTotalCommentCount(totalCommentCount); result.setTotalContributorCount(totalContributorCount); logger.debug("Found " + weekdaysCount + " weekdays between " + oldestAvailableReportDate + " and " + latestReportDate); if (weekdaysCount > 0) { result.setAverageDailyMessageCount(Math.round(Math.ceil(msgCount * 1.0 / weekdaysCount))); result.setAverageDailyPageViewCount(Math.round(Math.ceil(pageViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamContributorCount(Math.round(Math.ceil(streamContributorCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewCount(Math.round(Math.ceil(streamViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewerCount(Math.round(Math.ceil(streamViewerCount * 1.0 / weekdaysCount))); result.setAverageDailyUniqueVisitorCount(Math.round(Math.ceil(uniqueVisitorCount * 1.0 / weekdaysCount))); result.setAverageDailyActivityResponseTime(Math.round(Math.ceil(avgActivityResponseTime * 1.0 / weekdaysCount))); long averageCommentPerActivity = 0; if (weekdaysCount > 1) { double top = (finalCommentCount - startingCommentCount) * 1.0; double bottom = (finalActivityCount - startingActivityCount) * 1.0; if (bottom > 0) { averageCommentPerActivity = Math.round(Math.ceil(top / (bottom * (weekdaysCount - 1)))); } } result.setAverageDailyCommentPerActivityCount(averageCommentPerActivity); } result.setDailyStatistics(new ArrayList<DailyUsageSummary>()); if (dailyStats.size() > 0) { Calendar cal = Calendar.getInstance(); cal.setTime(oldestAvailableReportDate); Date reportDate; long lastDateInMs = latestReportDate.getTime(); int dataIndex = 0; for (reportDate = cal.getTime(); reportDate.getTime() <= lastDateInMs && result.getDailyStatistics().size() < weekdaysCount; cal.add(Calendar.DATE, 1), reportDate = cal .getTime()) { if (!dayOfWeekStrategy.isWeekday(reportDate)) { continue; } if (dataIndex < dailyStats.size()) { DailyUsageSummary record = dailyStats.get(dataIndex); if (DateDayExtractor.getStartOfDay(record.getUsageDate()).getTime() == reportDate.getTime()) { result.getDailyStatistics().add(record); dataIndex++; } else { result.getDailyStatistics().add(null); } } else { result.getDailyStatistics().add(null); } } } return result; } }
GetUsageMetricSummaryExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) { UsageMetricStreamSummaryRequest request = (UsageMetricStreamSummaryRequest) inActionContext.getParams(); List<DailyUsageSummary> results = summaryDataMapper.execute(request); logger.info("Found " + results.size() + " summary results"); UsageMetricSummaryDTO result = new UsageMetricSummaryDTO(); List<DailyUsageSummary> dailyStats = new ArrayList<DailyUsageSummary>(); if (results.size() == 0) { return result; } long msgCount = 0; long pageViewCount = 0; long streamContributorCount = 0; long streamViewCount = 0; long streamViewerCount = 0; long uniqueVisitorCount = 0; long avgActivityResponseTime = 0; long startingCommentCount = 0, finalCommentCount = 0; long startingActivityCount = 0, finalActivityCount = 0; Long totalActivityCount = null; Long totalCommentCount = null; Long totalContributorCount = null; Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -request.getNumberOfDays()); Date oldestAllowableReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); day = Calendar.getInstance(); day.add(Calendar.DATE, -1); Date latestReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); Date summaryDate; Date oldestAvailableReportDate = null; Date newestAvailableReportDate = null; logger.debug("Looking for data between " + oldestAllowableReportDate + " and " + latestReportDate); for (DailyUsageSummary dus : results) { summaryDate = DateDayExtractor.getStartOfDay(dus.getUsageDate()); if (summaryDate.before(oldestAllowableReportDate) || summaryDate.after(latestReportDate)) { logger.debug("Can't use data for " + summaryDate); continue; } dus.setUsageDate(summaryDate); dailyStats.add(dus); if (newestAvailableReportDate == null || summaryDate.after(newestAvailableReportDate)) { newestAvailableReportDate = summaryDate; totalActivityCount = dus.getTotalActivityCount(); totalCommentCount = dus.getTotalCommentCount(); totalContributorCount = dus.getTotalContributorCount(); finalCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); finalActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } if (oldestAvailableReportDate == null || summaryDate.before(oldestAvailableReportDate)) { oldestAvailableReportDate = summaryDate; startingCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); startingActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } msgCount += dus.getMessageCount(); pageViewCount += dus.getPageViewCount(); streamContributorCount += dus.getStreamContributorCount(); streamViewCount += dus.getStreamViewCount(); streamViewerCount += dus.getStreamViewerCount(); uniqueVisitorCount += dus.getUniqueVisitorCount(); avgActivityResponseTime += dus.getAvgActivityResponseTime(); } long weekdaysCount = 0; if (oldestAvailableReportDate != null) { weekdaysCount = weekdaysInDateRangeStrategy.getWeekdayCountBetweenDates(oldestAvailableReportDate, DateDayExtractor.getStartOfDay(new Date())); } result.setWeekdayRecordCount(weekdaysCount); result.setTotalActivityCount(totalActivityCount); result.setTotalCommentCount(totalCommentCount); result.setTotalContributorCount(totalContributorCount); logger.debug("Found " + weekdaysCount + " weekdays between " + oldestAvailableReportDate + " and " + latestReportDate); if (weekdaysCount > 0) { result.setAverageDailyMessageCount(Math.round(Math.ceil(msgCount * 1.0 / weekdaysCount))); result.setAverageDailyPageViewCount(Math.round(Math.ceil(pageViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamContributorCount(Math.round(Math.ceil(streamContributorCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewCount(Math.round(Math.ceil(streamViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewerCount(Math.round(Math.ceil(streamViewerCount * 1.0 / weekdaysCount))); result.setAverageDailyUniqueVisitorCount(Math.round(Math.ceil(uniqueVisitorCount * 1.0 / weekdaysCount))); result.setAverageDailyActivityResponseTime(Math.round(Math.ceil(avgActivityResponseTime * 1.0 / weekdaysCount))); long averageCommentPerActivity = 0; if (weekdaysCount > 1) { double top = (finalCommentCount - startingCommentCount) * 1.0; double bottom = (finalActivityCount - startingActivityCount) * 1.0; if (bottom > 0) { averageCommentPerActivity = Math.round(Math.ceil(top / (bottom * (weekdaysCount - 1)))); } } result.setAverageDailyCommentPerActivityCount(averageCommentPerActivity); } result.setDailyStatistics(new ArrayList<DailyUsageSummary>()); if (dailyStats.size() > 0) { Calendar cal = Calendar.getInstance(); cal.setTime(oldestAvailableReportDate); Date reportDate; long lastDateInMs = latestReportDate.getTime(); int dataIndex = 0; for (reportDate = cal.getTime(); reportDate.getTime() <= lastDateInMs && result.getDailyStatistics().size() < weekdaysCount; cal.add(Calendar.DATE, 1), reportDate = cal .getTime()) { if (!dayOfWeekStrategy.isWeekday(reportDate)) { continue; } if (dataIndex < dailyStats.size()) { DailyUsageSummary record = dailyStats.get(dataIndex); if (DateDayExtractor.getStartOfDay(record.getUsageDate()).getTime() == reportDate.getTime()) { result.getDailyStatistics().add(record); dataIndex++; } else { result.getDailyStatistics().add(null); } } else { result.getDailyStatistics().add(null); } } } return result; } GetUsageMetricSummaryExecution(
            final DomainMapper<UsageMetricStreamSummaryRequest, List<DailyUsageSummary>> inSummaryDataMapper,
            final WeekdaysInDateRangeStrategy inWeekdaysInDateRangeStrategy,
            final DayOfWeekStrategy inDayOfWeekStrategy); }
GetUsageMetricSummaryExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) { UsageMetricStreamSummaryRequest request = (UsageMetricStreamSummaryRequest) inActionContext.getParams(); List<DailyUsageSummary> results = summaryDataMapper.execute(request); logger.info("Found " + results.size() + " summary results"); UsageMetricSummaryDTO result = new UsageMetricSummaryDTO(); List<DailyUsageSummary> dailyStats = new ArrayList<DailyUsageSummary>(); if (results.size() == 0) { return result; } long msgCount = 0; long pageViewCount = 0; long streamContributorCount = 0; long streamViewCount = 0; long streamViewerCount = 0; long uniqueVisitorCount = 0; long avgActivityResponseTime = 0; long startingCommentCount = 0, finalCommentCount = 0; long startingActivityCount = 0, finalActivityCount = 0; Long totalActivityCount = null; Long totalCommentCount = null; Long totalContributorCount = null; Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -request.getNumberOfDays()); Date oldestAllowableReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); day = Calendar.getInstance(); day.add(Calendar.DATE, -1); Date latestReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); Date summaryDate; Date oldestAvailableReportDate = null; Date newestAvailableReportDate = null; logger.debug("Looking for data between " + oldestAllowableReportDate + " and " + latestReportDate); for (DailyUsageSummary dus : results) { summaryDate = DateDayExtractor.getStartOfDay(dus.getUsageDate()); if (summaryDate.before(oldestAllowableReportDate) || summaryDate.after(latestReportDate)) { logger.debug("Can't use data for " + summaryDate); continue; } dus.setUsageDate(summaryDate); dailyStats.add(dus); if (newestAvailableReportDate == null || summaryDate.after(newestAvailableReportDate)) { newestAvailableReportDate = summaryDate; totalActivityCount = dus.getTotalActivityCount(); totalCommentCount = dus.getTotalCommentCount(); totalContributorCount = dus.getTotalContributorCount(); finalCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); finalActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } if (oldestAvailableReportDate == null || summaryDate.before(oldestAvailableReportDate)) { oldestAvailableReportDate = summaryDate; startingCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); startingActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } msgCount += dus.getMessageCount(); pageViewCount += dus.getPageViewCount(); streamContributorCount += dus.getStreamContributorCount(); streamViewCount += dus.getStreamViewCount(); streamViewerCount += dus.getStreamViewerCount(); uniqueVisitorCount += dus.getUniqueVisitorCount(); avgActivityResponseTime += dus.getAvgActivityResponseTime(); } long weekdaysCount = 0; if (oldestAvailableReportDate != null) { weekdaysCount = weekdaysInDateRangeStrategy.getWeekdayCountBetweenDates(oldestAvailableReportDate, DateDayExtractor.getStartOfDay(new Date())); } result.setWeekdayRecordCount(weekdaysCount); result.setTotalActivityCount(totalActivityCount); result.setTotalCommentCount(totalCommentCount); result.setTotalContributorCount(totalContributorCount); logger.debug("Found " + weekdaysCount + " weekdays between " + oldestAvailableReportDate + " and " + latestReportDate); if (weekdaysCount > 0) { result.setAverageDailyMessageCount(Math.round(Math.ceil(msgCount * 1.0 / weekdaysCount))); result.setAverageDailyPageViewCount(Math.round(Math.ceil(pageViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamContributorCount(Math.round(Math.ceil(streamContributorCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewCount(Math.round(Math.ceil(streamViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewerCount(Math.round(Math.ceil(streamViewerCount * 1.0 / weekdaysCount))); result.setAverageDailyUniqueVisitorCount(Math.round(Math.ceil(uniqueVisitorCount * 1.0 / weekdaysCount))); result.setAverageDailyActivityResponseTime(Math.round(Math.ceil(avgActivityResponseTime * 1.0 / weekdaysCount))); long averageCommentPerActivity = 0; if (weekdaysCount > 1) { double top = (finalCommentCount - startingCommentCount) * 1.0; double bottom = (finalActivityCount - startingActivityCount) * 1.0; if (bottom > 0) { averageCommentPerActivity = Math.round(Math.ceil(top / (bottom * (weekdaysCount - 1)))); } } result.setAverageDailyCommentPerActivityCount(averageCommentPerActivity); } result.setDailyStatistics(new ArrayList<DailyUsageSummary>()); if (dailyStats.size() > 0) { Calendar cal = Calendar.getInstance(); cal.setTime(oldestAvailableReportDate); Date reportDate; long lastDateInMs = latestReportDate.getTime(); int dataIndex = 0; for (reportDate = cal.getTime(); reportDate.getTime() <= lastDateInMs && result.getDailyStatistics().size() < weekdaysCount; cal.add(Calendar.DATE, 1), reportDate = cal .getTime()) { if (!dayOfWeekStrategy.isWeekday(reportDate)) { continue; } if (dataIndex < dailyStats.size()) { DailyUsageSummary record = dailyStats.get(dataIndex); if (DateDayExtractor.getStartOfDay(record.getUsageDate()).getTime() == reportDate.getTime()) { result.getDailyStatistics().add(record); dataIndex++; } else { result.getDailyStatistics().add(null); } } else { result.getDailyStatistics().add(null); } } } return result; } GetUsageMetricSummaryExecution(
            final DomainMapper<UsageMetricStreamSummaryRequest, List<DailyUsageSummary>> inSummaryDataMapper,
            final WeekdaysInDateRangeStrategy inWeekdaysInDateRangeStrategy,
            final DayOfWeekStrategy inDayOfWeekStrategy); @Override Serializable execute(final PrincipalActionContext inActionContext); }
GetUsageMetricSummaryExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) { UsageMetricStreamSummaryRequest request = (UsageMetricStreamSummaryRequest) inActionContext.getParams(); List<DailyUsageSummary> results = summaryDataMapper.execute(request); logger.info("Found " + results.size() + " summary results"); UsageMetricSummaryDTO result = new UsageMetricSummaryDTO(); List<DailyUsageSummary> dailyStats = new ArrayList<DailyUsageSummary>(); if (results.size() == 0) { return result; } long msgCount = 0; long pageViewCount = 0; long streamContributorCount = 0; long streamViewCount = 0; long streamViewerCount = 0; long uniqueVisitorCount = 0; long avgActivityResponseTime = 0; long startingCommentCount = 0, finalCommentCount = 0; long startingActivityCount = 0, finalActivityCount = 0; Long totalActivityCount = null; Long totalCommentCount = null; Long totalContributorCount = null; Calendar day = Calendar.getInstance(); day.add(Calendar.DATE, -request.getNumberOfDays()); Date oldestAllowableReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); day = Calendar.getInstance(); day.add(Calendar.DATE, -1); Date latestReportDate = DateDayExtractor.getStartOfDay(new Date(day.getTimeInMillis())); Date summaryDate; Date oldestAvailableReportDate = null; Date newestAvailableReportDate = null; logger.debug("Looking for data between " + oldestAllowableReportDate + " and " + latestReportDate); for (DailyUsageSummary dus : results) { summaryDate = DateDayExtractor.getStartOfDay(dus.getUsageDate()); if (summaryDate.before(oldestAllowableReportDate) || summaryDate.after(latestReportDate)) { logger.debug("Can't use data for " + summaryDate); continue; } dus.setUsageDate(summaryDate); dailyStats.add(dus); if (newestAvailableReportDate == null || summaryDate.after(newestAvailableReportDate)) { newestAvailableReportDate = summaryDate; totalActivityCount = dus.getTotalActivityCount(); totalCommentCount = dus.getTotalCommentCount(); totalContributorCount = dus.getTotalContributorCount(); finalCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); finalActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } if (oldestAvailableReportDate == null || summaryDate.before(oldestAvailableReportDate)) { oldestAvailableReportDate = summaryDate; startingCommentCount = dus.getTotalCommentCount() == null ? 0 : dus.getTotalCommentCount(); startingActivityCount = dus.getTotalActivityCount() == null ? 0 : dus.getTotalActivityCount(); } msgCount += dus.getMessageCount(); pageViewCount += dus.getPageViewCount(); streamContributorCount += dus.getStreamContributorCount(); streamViewCount += dus.getStreamViewCount(); streamViewerCount += dus.getStreamViewerCount(); uniqueVisitorCount += dus.getUniqueVisitorCount(); avgActivityResponseTime += dus.getAvgActivityResponseTime(); } long weekdaysCount = 0; if (oldestAvailableReportDate != null) { weekdaysCount = weekdaysInDateRangeStrategy.getWeekdayCountBetweenDates(oldestAvailableReportDate, DateDayExtractor.getStartOfDay(new Date())); } result.setWeekdayRecordCount(weekdaysCount); result.setTotalActivityCount(totalActivityCount); result.setTotalCommentCount(totalCommentCount); result.setTotalContributorCount(totalContributorCount); logger.debug("Found " + weekdaysCount + " weekdays between " + oldestAvailableReportDate + " and " + latestReportDate); if (weekdaysCount > 0) { result.setAverageDailyMessageCount(Math.round(Math.ceil(msgCount * 1.0 / weekdaysCount))); result.setAverageDailyPageViewCount(Math.round(Math.ceil(pageViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamContributorCount(Math.round(Math.ceil(streamContributorCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewCount(Math.round(Math.ceil(streamViewCount * 1.0 / weekdaysCount))); result.setAverageDailyStreamViewerCount(Math.round(Math.ceil(streamViewerCount * 1.0 / weekdaysCount))); result.setAverageDailyUniqueVisitorCount(Math.round(Math.ceil(uniqueVisitorCount * 1.0 / weekdaysCount))); result.setAverageDailyActivityResponseTime(Math.round(Math.ceil(avgActivityResponseTime * 1.0 / weekdaysCount))); long averageCommentPerActivity = 0; if (weekdaysCount > 1) { double top = (finalCommentCount - startingCommentCount) * 1.0; double bottom = (finalActivityCount - startingActivityCount) * 1.0; if (bottom > 0) { averageCommentPerActivity = Math.round(Math.ceil(top / (bottom * (weekdaysCount - 1)))); } } result.setAverageDailyCommentPerActivityCount(averageCommentPerActivity); } result.setDailyStatistics(new ArrayList<DailyUsageSummary>()); if (dailyStats.size() > 0) { Calendar cal = Calendar.getInstance(); cal.setTime(oldestAvailableReportDate); Date reportDate; long lastDateInMs = latestReportDate.getTime(); int dataIndex = 0; for (reportDate = cal.getTime(); reportDate.getTime() <= lastDateInMs && result.getDailyStatistics().size() < weekdaysCount; cal.add(Calendar.DATE, 1), reportDate = cal .getTime()) { if (!dayOfWeekStrategy.isWeekday(reportDate)) { continue; } if (dataIndex < dailyStats.size()) { DailyUsageSummary record = dailyStats.get(dataIndex); if (DateDayExtractor.getStartOfDay(record.getUsageDate()).getTime() == reportDate.getTime()) { result.getDailyStatistics().add(record); dataIndex++; } else { result.getDailyStatistics().add(null); } } else { result.getDailyStatistics().add(null); } } } return result; } GetUsageMetricSummaryExecution(
            final DomainMapper<UsageMetricStreamSummaryRequest, List<DailyUsageSummary>> inSummaryDataMapper,
            final WeekdaysInDateRangeStrategy inWeekdaysInDateRangeStrategy,
            final DayOfWeekStrategy inDayOfWeekStrategy); @Override Serializable execute(final PrincipalActionContext inActionContext);  }