Thursday, November 5, 2015

Performance Optimization - Customize Global promotions query

OOTB ATG updates the global promotions every 15 min as defined by the schedule internal property it it's PricingEngine component.

And ATG OOTB PricingEngines calls the findGlobalPromotions() to loads the global promotions based upon the property alwaysLoadGlobalPromotions

If the property is set to true, then every time pricing engine is called, a query is fired to fetch all the global promotions.
If the property is set to false, then it returns cache promotions set from the scheduler.

Loading global promotions always will be performance hit and if it is disabled then we may not get the latest promotions.
For example if the OrderPricingEngine scheduler has run at 12:00AM and we have a promotion that starts at 12:05AM. This promotion will not be returned if we have alwaysLoadGlobalPromotions=false.

To solve this problem we can customize the findGlobalPromotions() method to load all the promotions that starts at later time. If the scheduler runs every 15 min, then we will load all the promotions that starts 15 min later as well.

In below example, OrderPricingEngine is customized.


MyOrderPricingEngine extends OrderPricingEngineImpl
@SuppressWarnings("unchecked")
@Override
protected List<RepositoryItem> findGlobalPromotions() {
if (isEnableLoadPromotionsAhead()) {
return findGlobalPromotionsAhead();
}
return super.findGlobalPromotions();
}

findGlobalPromotionsAhead(){
String globalPromotionsAheadQuery=global=true AND enabled=true AND (beginUsable IS NULL OR beginUsable <= ?0) AND (endUsable IS NULL OR endUsable >= ?1);
RepositoryView view = getPromotionsRepository.getView(“orderDiscount”);
RqlStatement rqlStatement = RqlStatement.parseRqlStatement(globalPromotionsAheadQuery);
Timestamp endQueryTimestamp = new Timestamp( System.currentTimeMillis());
Timestamp startQueryTimestamp = new Timestamp(queryTimestamp.getTime() + getLoadPromotionsAheadTime());
Object[] params = { startQueryTimestamp, endQueryTimestamp };
RepositoryItem[] promotionArray = rqlStatement.executeQuery(view, params);
}

No comments:

Post a Comment