Added basic statistics service #11

This commit is contained in:
Rohan Sircar 2020-07-21 20:16:14 +05:30
parent b725458a8a
commit 4e40f0bdd8
4 changed files with 55 additions and 5 deletions

View File

@ -10,7 +10,8 @@ import org.springframework.stereotype.Repository;
import java.time.Instant; import java.time.Instant;
@Repository @Repository
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> { public interface ChatMessageRepository
extends JpaRepository<ChatMessage, Long> {
@Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu " @Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu "
+ "where (tu.userName = ?1 or tu.userName = ?2) and " + "where (tu.userName = ?1 or tu.userName = ?2) and "
+ "(fu.userName = ?1 or fu.userName = ?2) order by m.messageTime asc") + "(fu.userName = ?1 or fu.userName = ?2) order by m.messageTime asc")
@ -19,10 +20,18 @@ public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long>
@Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu " @Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu "
+ "where (tu.userName = ?1 or tu.userName = ?2) and " + "where (tu.userName = ?1 or tu.userName = ?2) and "
+ "(fu.userName = ?1 or fu.userName = ?2) and (m.messageTime > ?3) order by m.messageTime asc") + "(fu.userName = ?1 or fu.userName = ?2) and (m.messageTime > ?3) order by m.messageTime asc")
public List<ChatMessage> getNewMessages(String fromUser, String toUser, Instant lastMessageTime); public List<ChatMessage> getNewMessages(String fromUser, String toUser,
Instant lastMessageTime);
@Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu " @Query("select m from ChatMessage m join fetch m.messageCipher mc join fetch m.toUser tu join fetch m.fromUser fu "
+ "where (tu.userName = ?1 or tu.userName = ?2) and " + "where (tu.userName = ?1 or tu.userName = ?2) and "
+ "(fu.userName = ?1 or fu.userName = ?2) order by m.messageTime desc") + "(fu.userName = ?1 or fu.userName = ?2) order by m.messageTime desc")
public List<ChatMessage> getAllMessages(String fromUser, String toUser, PageRequest pageRequest); public List<ChatMessage> getAllMessages(String fromUser, String toUser,
PageRequest pageRequest);
@Query("select count(c) from ChatMessage c")
public Long totalMessages();
@Query("select count(c) from ChatMessage c where c.messageTime=?1")
public Long messagesOnDate(Instant date);
} }

View File

@ -15,4 +15,7 @@ public interface UserRepository extends JpaRepository<ChatUser, Long> {
@Query("select cu.userName from ChatUser cu where cu.userName != ?1") @Query("select cu.userName from ChatUser cu where cu.userName != ?1")
public List<String> findAllOtherUserNames(String userName); public List<String> findAllOtherUserNames(String userName);
@Query("select count(u) from ChatUser u")
public Long totalUsers();
} }

View File

@ -8,10 +8,14 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UserSessionRepository extends JpaRepository<UserSession, Long> { public interface UserSessionRepository
extends JpaRepository<UserSession, Long> {
@Query("select us from UserSession us join fetch us.user where us.user.userName = ?1 ") @Query("select us from UserSession us join fetch us.user where us.user.userName = ?1 ")
public UserSession findByUserName(String userName); public UserSession findByUserName(String userName);
@Query("select us from UserSession us join fetch us.user") @Query("select us from UserSession us join fetch us.user")
public List<UserSession> findAllUserSessions(); public List<UserSession> findAllUserSessions();
@Query("select count(us) from UserSession us where us.online=true")
public Long totalOnlineUsers();
} }

View File

@ -0,0 +1,34 @@
package org.ros.chatto.service;
import java.time.Instant;
import org.ros.chatto.repository.ChatMessageRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserSessionRepository;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Service
public class StatisticsService {
private final ChatMessageRepository chatMessageRepository;
private final UserRepository userRepo;
private final UserSessionRepository userSessionRepo;
public Long totalMessage() {
return chatMessageRepository.totalMessages();
}
public Long totalUsers() {
return userRepo.totalUsers();
}
public Long totalUsersOnline() {
return userSessionRepo.totalOnlineUsers();
}
public Long messagesOnDay(Instant date) {
return chatMessageRepository.messagesOnDate(date);
}
}