-
-
Save tnine/395c8b9814e089602e9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get a list of UserSumRange objects that should be used in calculating data. Note that any returned UserSumRange could already be persistent | |
* | |
* @param customer The customer to use | |
* @param startTime The time this process started in epoch milliseconds | |
* @param zone The Time | |
* @return | |
*/ | |
private List<UserSumRange> getRanges(Customer customer, long startTime) { | |
// the start date of our first registration range | |
// get all open ranges for this customer | |
List<UserSumRange> ranges = new ArrayList<UserSumRange>(userSumRangeDao.getOpenRanges(customer.getId(), 1000)); | |
//truncate our created date to be at exactly 00:00 on the day it was created | |
DateTime createdDate = new DateTime(customer.getCreatedDate()); | |
DateTime startDateRange = new DateTime(createdDate.getYear(),createdDate.getMonthOfYear(), createdDate.getDayOfMonth(), 0, 0, 0, 0, customer.getTimeZone().getTimeZone()); | |
UserSumRange latest = getLast(ranges); | |
// if there are no sum ranges, we've never generated anything for this | |
// customer. | |
// The customer MUST always have at least 1 open sum range. Otherwise this process has never been run for them | |
if (latest == null) { | |
DateTime startDateEnd = startDateRange.plusMonths(1); | |
latest = new UserSumRange(); | |
latest.setStart(startDateRange); | |
latest.setEnd(startDateEnd); | |
latest.setUserId(customer.getId()); | |
ranges.add(latest); | |
} | |
// add all sums required to bring us up to "current" | |
while (latest.getEnd().isBefore(startTime)) { | |
Months months = Months.monthsBetween(startDateRange, latest.getEnd()); | |
DateTime startDate = startDateRange.plus(months); | |
DateTime endDate = startDateRange.plus(months.plus(1)); | |
UserSumRange newRange = new UserSumRange(); | |
newRange.setStart(startDate); | |
newRange.setEnd(endDate); | |
newRange.setUserId(customer.getId()); | |
ranges.add(newRange); | |
latest = newRange; | |
} | |
return ranges; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment