Skip to content

Commit

Permalink
#874 - add month granularity
Browse files Browse the repository at this point in the history
(cherry picked from commit 56bfbe9)
  • Loading branch information
cbellone committed Mar 24, 2020
1 parent cdfb655 commit 291c7bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/main/java/alfio/controller/api/admin/EventApiController.java
Expand Up @@ -649,13 +649,22 @@ public ResponseEntity<TicketsStatistics> getTicketsStatistics(@PathVariable("eve
var reservedFrom = parseDate(f, zoneId, () -> eventStatisticsManager.getFirstReservationCreatedTimestamp(event.getId()), () -> ZonedDateTime.now(zoneId).minusDays(1));
var to = parseDate(t, zoneId, Optional::empty, () -> ZonedDateTime.now(zoneId)).plusDays(1L);

boolean weeksGranularity = ChronoUnit.MONTHS.between(from, to) > 3;
var ticketSoldStatistics = eventStatisticsManager.getTicketSoldStatistics(eventId, from, to, weeksGranularity);
var ticketReservedStatistics = eventStatisticsManager.getTicketReservedStatistics(eventId, reservedFrom, to, weeksGranularity);
return new TicketsStatistics(weeksGranularity ? "week" : "day", ticketSoldStatistics, ticketReservedStatistics);
var granularity = getGranularity(reservedFrom, to);
var ticketSoldStatistics = eventStatisticsManager.getTicketSoldStatistics(eventId, from, to, granularity);
var ticketReservedStatistics = eventStatisticsManager.getTicketReservedStatistics(eventId, reservedFrom, to, granularity);
return new TicketsStatistics(granularity, ticketSoldStatistics, ticketReservedStatistics);
}));
}

private String getGranularity(ZonedDateTime from, ZonedDateTime to) {
if(ChronoUnit.MONTHS.between(from, to) > 12) {
return "month";
} else if(ChronoUnit.MONTHS.between(from, to) > 3) {
return "week";
}
return "day";
}

private ZonedDateTime parseDate(String dateToParse,
ZoneId zoneId,
Supplier<Optional<ZonedDateTime>> dateLoader,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/alfio/manager/EventStatisticsManager.java
Expand Up @@ -160,12 +160,12 @@ public Optional<ZonedDateTime> getFirstReservationCreatedTimestamp(int eventId)
return ticketReservationRepository.getFirstReservationCreatedTimestampForEvent(eventId);
}

public List<TicketsByDateStatistic> getTicketSoldStatistics(int eventId, ZonedDateTime from, ZonedDateTime to, boolean weeksGranularity) {
return ticketReservationRepository.getSoldStatistic(eventId, from, to, weeksGranularity ? "week" : "day");
public List<TicketsByDateStatistic> getTicketSoldStatistics(int eventId, ZonedDateTime from, ZonedDateTime to, String granularity) {
return ticketReservationRepository.getSoldStatistic(eventId, from, to, granularity);
}

public List<TicketsByDateStatistic> getTicketReservedStatistics(int eventId, ZonedDateTime from, ZonedDateTime to, boolean weeksGranularity) {
return ticketReservationRepository.getReservedStatistic(eventId, from, to, weeksGranularity ? "week" : "day");
public List<TicketsByDateStatistic> getTicketReservedStatistics(int eventId, ZonedDateTime from, ZonedDateTime to, String granularity) {
return ticketReservationRepository.getReservedStatistic(eventId, from, to, granularity);
}


Expand Down
Expand Up @@ -254,6 +254,7 @@
var soldByDay = stats.sold;
var reservedByDay = stats.reserved;
var weeks = stats.granularity === 'week';
var months = stats.granularity === 'month';
var labels = _.chain(soldByDay)
.concat(reservedByDay)
.map('date')
Expand All @@ -270,6 +271,8 @@
var startFormat = 'MMM Do' + (differentYear ? ' YYYY': '');
var endFormat = (differentMonth ? 'MMM ': '') + 'Do YYYY';
return date.format(startFormat) + ' - ' + end.format(endFormat);
} else if(months) {
return date.format('MMM YYYY');
}
return date.format('MMM Do YYYY');
}
Expand Down

0 comments on commit 291c7bb

Please sign in to comment.