From 4dff760fad79d4309a02a876abdf9f79e5258af7 Mon Sep 17 00:00:00 2001 From: Akshitha2106 Date: Sun, 2 Jun 2024 23:02:30 +0530 Subject: [PATCH] Made db querry otptimization. --- .../dynamodb/repository/EventRepository.java | 8 ++ .../graphql/builder/EventBuilder.java | 93 +++++++++++++++---- 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/EventRepository.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/EventRepository.java index d7bc89c3..fd5180ab 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/EventRepository.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/repository/EventRepository.java @@ -2,8 +2,16 @@ import com.flickmatch.platform.dynamodb.model.Event; import org.socialsignin.spring.data.dynamodb.repository.EnableScan; +import org.socialsignin.spring.data.dynamodb.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; + +import java.util.List; @EnableScan public interface EventRepository extends CrudRepository { + @Query("SELECT e FROM Event e WHERE e.cityId = :cityId AND e.date BETWEEN :startDate AND :endDate") + List findByCityIdAndDateRange(@Param("cityId") String cityId, + @Param("startDate") String startDate, + @Param("endDate") String endDate); } diff --git a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java index 65c286be..601b29db 100644 --- a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java +++ b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java @@ -89,34 +89,87 @@ public Event joinEventRazorPayment(final RazorPaymentRequest paymentRequest) { +// public List getEvents(String cityId, String localTimeZone) { +// Date currentTime = new Date(System.currentTimeMillis()); +// List eventList = new ArrayList<>(); +//// List prevDayList = getPastEvents(cityId, 2, localTimeZone); +//// eventList.addAll(prevDayList); +// for(int i=1;i<=2;i++) { +// Date prevDate = Date.from(currentTime.toInstant().minus(i, ChronoUnit.DAYS)); +// String prevFormattedDate = DateUtil.extractDateFromISOFormatDate(prevDate, localTimeZone); +// Event.EventId prevEventId = Event.EventId.builder().cityId(cityId).date(prevFormattedDate).build(); +// Optional pastEventData = eventRepository.findById(prevEventId); +// List pastEventList = +// pastEventData.get().getEventDetailsList().stream() +// .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) +// .map(eventDetails -> mapEventToGQLType(eventDetails, prevFormattedDate, localTimeZone, cityId)) +// .toList(); +// eventList.addAll(pastEventList); +// } +//// Date prevDate = Date.from(currentTime.toInstant().minus(1, ChronoUnit.DAYS)); +//// String prevFormattedDate = DateUtil.extractDateFromISOFormatDate(prevDate, localTimeZone); +//// Event.EventId prevEventId = Event.EventId.builder().cityId(cityId).date(prevFormattedDate).build(); +//// Optional pastEventData = eventRepository.findById(prevEventId); +//// List pastEventList = +//// pastEventData.get().getEventDetailsList().stream() +//// // TODO: Add condition here to check the the event is starting no later than 7 days +//// .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) +//// .map(eventDetails -> mapEventToGQLType(eventDetails, prevFormattedDate, localTimeZone, cityId)) +//// .toList(); +//// eventList.addAll(pastEventList); +// final int GET_EVENT_DAYS = 7; +// log.info(format("Fetching events for cityId %s in the next %d days", cityId, GET_EVENT_DAYS)); +// +// for (int i = 0; i < GET_EVENT_DAYS; i++) { +// Date currentDate = Date.from(currentTime.toInstant().plus(i, ChronoUnit.DAYS)); +// String formattedDate = DateUtil.extractDateFromISOFormatDate(currentDate, localTimeZone); +// Event.EventId eventId = Event.EventId.builder().cityId(cityId).date(formattedDate).build(); +// Optional eventData = eventRepository.findById(eventId); +// +// if (eventData.isPresent()) { +// List dailyEventList = +// eventData.get().getEventDetailsList().stream() +// // TODO: Add condition here to check the the event is starting no later than 7 days +// .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) +// .map(eventDetails -> mapEventToGQLType(eventDetails, formattedDate, localTimeZone, cityId)) +// .toList(); +// eventList.addAll(dailyEventList); +// } +// } +// return eventList; +// } + public List getEvents(String cityId, String localTimeZone) { Date currentTime = new Date(System.currentTimeMillis()); List eventList = new ArrayList<>(); - List prevDayList = getPastEvents(cityId, 2, localTimeZone); - eventList.addAll(prevDayList); - final int GET_EVENT_DAYS = 7; - log.info(format("Fetching events for cityId %s in the next %d days", cityId, GET_EVENT_DAYS)); - - for (int i = 0; i < GET_EVENT_DAYS; i++) { - Date currentDate = Date.from(currentTime.toInstant().plus(i, ChronoUnit.DAYS)); - String formattedDate = DateUtil.extractDateFromISOFormatDate(currentDate, localTimeZone); - Event.EventId eventId = Event.EventId.builder().cityId(cityId).date(formattedDate).build(); - Optional eventData = eventRepository.findById(eventId); - - if (eventData.isPresent()) { - List dailyEventList = - eventData.get().getEventDetailsList().stream() - // TODO: Add condition here to check the the event is starting no later than 7 days - .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) - .map(eventDetails -> mapEventToGQLType(eventDetails, formattedDate, localTimeZone, cityId)) - .toList(); - eventList.addAll(dailyEventList); - } + final int LOOKBACK_DAYS = 2; + + // Calculate date range + Date startDate = Date.from(currentTime.toInstant().minus(LOOKBACK_DAYS, ChronoUnit.DAYS)); + Date endDate = Date.from(currentTime.toInstant().plus(GET_EVENT_DAYS - 1, ChronoUnit.DAYS)); + + String startFormattedDate = DateUtil.extractDateFromISOFormatDate(startDate, localTimeZone); + String endFormattedDate = DateUtil.extractDateFromISOFormatDate(endDate, localTimeZone); + + log.info(String.format("Fetching events for cityId %s from %s to %s", cityId, startFormattedDate, endFormattedDate)); + + // Fetch all events within the date range + List eventsInRange = eventRepository.findByCityIdAndDateRange(cityId, startFormattedDate, endFormattedDate); + + for (Event event : eventsInRange) { + String eventDate = event.getDate(); + List dailyEventList = event.getEventDetailsList().stream() + .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) + .map(eventDetails -> mapEventToGQLType(eventDetails, eventDate, localTimeZone, cityId)) + .toList(); + eventList.addAll(dailyEventList); } + return eventList; } + public List getPastEvents(String cityId, Integer inDays, String localTimeZone) { List pastEventList = new ArrayList<>(); Date currentTime = new Date(System.currentTimeMillis());