Added stats endpoint

This commit is contained in:
Rohan Sircar 2020-07-21 22:20:02 +05:30
parent 4e40f0bdd8
commit 77bff3924c
3 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package org.ros.chatto.controller;
import java.time.Instant;
import org.ros.chatto.dto.StatsDTO;
import org.ros.chatto.service.StatisticsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
@RestController
@RequestMapping("/api/stats")
@RequiredArgsConstructor
public class StatisticsController {
private final StatisticsService statisticsService;
@GetMapping
public StatsDTO rootStats() {
return StatsDTO.builder()
.totalMessages(statisticsService.totalMessage())
.totalOnlineUsers(statisticsService.totalUsersOnline())
.numMessagesToday(
statisticsService.messagesOnDay(Instant.now()))
.totalUsers(statisticsService.totalUsers()).build();
}
}

View File

@ -0,0 +1,21 @@
package org.ros.chatto.dto;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class StatsDTO {
@Builder.Default
private long totalUsers = 0;
@Builder.Default
private long totalOnlineUsers = 0;
@Builder.Default
private long totalMessages = 0;
@Builder.Default
private long numMessagesToday = 0;
}

View File

@ -6,11 +6,13 @@ import org.ros.chatto.repository.ChatMessageRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserSessionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class StatisticsService {
private final ChatMessageRepository chatMessageRepository;
private final UserRepository userRepo;