Skip to content

Commit

Permalink
Made db querry otptimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshithaFM committed Jun 2, 2024
1 parent 038b800 commit 4dff760
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, Event.EventId> {
@Query("SELECT e FROM Event e WHERE e.cityId = :cityId AND e.date BETWEEN :startDate AND :endDate")
List<Event> findByCityIdAndDateRange(@Param("cityId") String cityId,
@Param("startDate") String startDate,
@Param("endDate") String endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,87 @@ public Event joinEventRazorPayment(final RazorPaymentRequest paymentRequest) {



// public List<com.flickmatch.platform.graphql.type.Event> getEvents(String cityId, String localTimeZone) {
// Date currentTime = new Date(System.currentTimeMillis());
// List<com.flickmatch.platform.graphql.type.Event> eventList = new ArrayList<>();
//// List<com.flickmatch.platform.graphql.type.Event> 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<Event> pastEventData = eventRepository.findById(prevEventId);
// List<com.flickmatch.platform.graphql.type.Event> 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<Event> pastEventData = eventRepository.findById(prevEventId);
//// List<com.flickmatch.platform.graphql.type.Event> 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<Event> eventData = eventRepository.findById(eventId);
//
// if (eventData.isPresent()) {
// List<com.flickmatch.platform.graphql.type.Event> 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<com.flickmatch.platform.graphql.type.Event> getEvents(String cityId, String localTimeZone) {
Date currentTime = new Date(System.currentTimeMillis());
List<com.flickmatch.platform.graphql.type.Event> eventList = new ArrayList<>();
List<com.flickmatch.platform.graphql.type.Event> 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<Event> eventData = eventRepository.findById(eventId);

if (eventData.isPresent()) {
List<com.flickmatch.platform.graphql.type.Event> 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<Event> eventsInRange = eventRepository.findByCityIdAndDateRange(cityId, startFormattedDate, endFormattedDate);

for (Event event : eventsInRange) {
String eventDate = event.getDate();
List<com.flickmatch.platform.graphql.type.Event> 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<com.flickmatch.platform.graphql.type.Event> getPastEvents(String cityId, Integer inDays, String localTimeZone) {
List<com.flickmatch.platform.graphql.type.Event> pastEventList = new ArrayList<>();
Date currentTime = new Date(System.currentTimeMillis());
Expand Down

0 comments on commit 4dff760

Please sign in to comment.