Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'issue-2012' of dlab/hive
Browse files Browse the repository at this point in the history
from pull-request 1434

* refs/heads/issue-2012:
  Enhanced performance for counting notifications
  Using raw sql to get user's notifications

Reviewed-by: 이응준 <eungjun.yi@navercorp.com>
  • Loading branch information
이응준 committed Jan 27, 2015
2 parents f3aa924 + 6aee007 commit e2dd660
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 26 additions & 7 deletions app/models/NotificationEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package models;

import com.avaje.ebean.RawSql;
import com.avaje.ebean.RawSqlBuilder;
import controllers.UserApp;
import controllers.routes;
import models.enumeration.*;
Expand Down Expand Up @@ -1020,12 +1022,29 @@ public static void onStart() {
* @return
*/
public static List<NotificationEvent> findByReceiver(User user, int from, int size) {
List<NotificationEvent> allEvents = user.notificationEvents;
int allEventsCount = allEvents.size();
if (allEventsCount <= from) {
return new ArrayList<>();
}
int to = (allEventsCount < from + size) ? allEventsCount : from + size;
return allEvents.subList(from, to);
String sql = "select t1.id, t1.title, t1.sender_id, t1.created, t1.resource_type, t1.resource_id, t1.event_type, " +
"t1.old_value, t1.new_value " +
"from n4user t0 " +
"left outer join notification_event_n4user t1z_ on t1z_.n4user_id = t0.id " +
"left outer join notification_event t1 on t1.id = t1z_.notification_event_id " +
"left outer join notification_mail t2 on t2.notification_event_id = t1.id " +
"where t0.id = " + user.id + " and t1.id IS NOT NULL " +
"order by t1.created DESC";

return find.setRawSql(RawSqlBuilder.parse(sql).create())
.setFirstRow(from)
.setMaxRows(size)
.findList();
}

public static int getNotificationsCount(User user) {
String sql = "select t1.id " +
"from n4user t0 " +
"left outer join notification_event_n4user t1z_ on t1z_.n4user_id = t0.id " +
"left outer join notification_event t1 on t1.id = t1z_.notification_event_id " +
"left outer join notification_mail t2 on t2.notification_event_id = t1.id " +
"where t0.id = " + user.id + " and t1.id IS NOT NULL ";

return find.setRawSql(RawSqlBuilder.parse(sql).create()).findList().size();
}
}
2 changes: 1 addition & 1 deletion app/views/index/partial_notifications.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
}
}

@if(UserApp.currentUser.notificationEvents.size > size){
@if(NotificationEvent.getNotificationsCount(UserApp.currentUser()) > size){
<li><a href="javascript: void(0);" id="notification-more" class="ybtn">More</a></li>
<script type="text/javascript">
$(document).ready(function(){
Expand Down

0 comments on commit e2dd660

Please sign in to comment.