Skip to content

Commit

Permalink
#19 Implementation the count functions for groups and users
Browse files Browse the repository at this point in the history
  • Loading branch information
jameschen authored and jameschen committed Oct 28, 2019
1 parent 67398c3 commit 7bc17bf
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 378 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
<artifactId>aho-corasick-double-array-trie</artifactId>
<version>${aho-corasick-double-array-trie.version}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Map;

import static im.turms.turms.common.Constants.DEV_MODE;
import static im.turms.turms.common.Constants.STATUS;

@Component
Expand All @@ -32,21 +31,18 @@ public class GlobalErrorAttributes extends DefaultErrorAttributes {
public Map<String, Object> getErrorAttributes(
ServerRequest request,
boolean includeStackTrace) {
if (DEV_MODE) {
return super.getErrorAttributes(request, true);
}
Map<String, Object> errorAttributes = super.getErrorAttributes(request, false);
if ((Integer) errorAttributes.get(STATUS) == 500) {
Object messageObj = errorAttributes.get("message");
boolean isClientError;
if (messageObj == null) {
if (messageObj == null) { // For NullPointerException
isClientError = true;
} else {
String message = messageObj.toString();
isClientError = message.contains("WebFlux") || message.contains("cast");
}
if (isClientError) {
errorAttributes.put(STATUS, 404);
errorAttributes.put(STATUS, 400);
errorAttributes.remove("error");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,34 @@
import im.turms.turms.common.PageUtil;
import im.turms.turms.common.TurmsStatusCode;
import im.turms.turms.constant.AdminPermission;
import im.turms.turms.constant.DivideBy;
import im.turms.turms.pojo.domain.Group;
import im.turms.turms.pojo.domain.GroupType;
import im.turms.turms.pojo.dto.GroupDTO;
import im.turms.turms.service.group.GroupService;
import im.turms.turms.service.group.GroupTypeService;
import im.turms.turms.service.message.MessageService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/groups")
public class GroupController {
private final GroupService groupService;
private final GroupTypeService groupTypeService;
private final MessageService messageService;
private final PageUtil pageUtil;

public GroupController(GroupService groupService, GroupTypeService groupTypeService, PageUtil pageUtil) {
public GroupController(GroupService groupService, GroupTypeService groupTypeService, PageUtil pageUtil, MessageService messageService) {
this.groupService = groupService;
this.groupTypeService = groupTypeService;
this.pageUtil = pageUtil;
this.messageService = messageService;
}

@GetMapping
Expand Down Expand Up @@ -155,34 +160,64 @@ public Mono<ResponseEntity> deleteGroupType(@RequestParam Long groupTypeId) {

@GetMapping("/count")
public Mono<ResponseEntity> countGroups(
@RequestParam(required = false) String createStartDate,
@RequestParam(required = false) String createEndDate,
@RequestParam(required = false) String deleteStartDate,
@RequestParam(required = false) String deleteEndDate,
@RequestParam(required = false) String sendMessageStartDate,
@RequestParam(required = false) String sendMessageEndDate) {
try {
Mono<Long> count;
if (createStartDate != null || createEndDate != null) {
count = groupService.countOwnedGroups(
DateTimeUtil.parseDay(createStartDate),
DateTimeUtil.endOfDay(createEndDate));
} else if (deleteStartDate != null || deleteEndDate != null) {
count = groupService.countDeletedGroups(
DateTimeUtil.parseDay(deleteStartDate),
DateTimeUtil.endOfDay(deleteEndDate)
);
} else if (sendMessageStartDate != null || sendMessageEndDate != null) {
count = groupService.countGroupsThatSentMessages(
DateTimeUtil.parseDay(sendMessageStartDate),
DateTimeUtil.endOfDay(sendMessageEndDate));

} else {
count = groupService.count();
@RequestParam(required = false) Date createdStartDate,
@RequestParam(required = false) Date createdEndDate,
@RequestParam(required = false) Date deletedStartDate,
@RequestParam(required = false) Date deletedEndDate,
@RequestParam(required = false) Date deliveredMessageStartDate,
@RequestParam(required = false) Date deliveredMessageEndDate,
@RequestParam(defaultValue = "NOOP") DivideBy divideBy) {
if (divideBy == null || divideBy == DivideBy.NOOP) {
List<Mono<Pair<String, Long>>> counts = new LinkedList<>();
if (deletedStartDate != null || deletedEndDate != null) {
counts.add(groupService.countDeletedGroups(
deletedStartDate,
deletedEndDate)
.map(total -> Pair.of("deletedGroups", total)));
}
if (deliveredMessageStartDate != null || deliveredMessageEndDate != null) {
counts.add(messageService.countGroupsThatSentMessages(
deliveredMessageStartDate,
deliveredMessageEndDate)
.map(total -> Pair.of("groupsThatSentMessages", total)));
}
if (counts.isEmpty() || createdStartDate != null || createdEndDate != null) {
counts.add(groupService.countCreatedGroups(
createdStartDate,
createdEndDate)
.map(total -> Pair.of("createdGroups", total)));
}
return ResponseFactory.collectCountResults(counts);
} else {
List<Mono<Pair<String, List<Map<String, ?>>>>> counts = new LinkedList<>();
if (deletedStartDate != null && deletedEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"deletedGroups",
deletedStartDate,
deletedEndDate,
divideBy,
groupService::countDeletedGroups));
}
if (deliveredMessageStartDate != null && deliveredMessageEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"groupsThatSentMessages",
deliveredMessageStartDate,
deliveredMessageEndDate,
divideBy,
messageService::countGroupsThatSentMessages));
}
if (createdStartDate != null && createdEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"createdGroups",
createdStartDate,
createdEndDate,
divideBy,
groupService::countCreatedGroups));
}
if (counts.isEmpty()) {
return ResponseFactory.code(TurmsStatusCode.ILLEGAL_ARGUMENTS);
}
return ResponseFactory.okWhenTruthy(count);
} catch (ParseException | IllegalArgumentException e) {
return ResponseFactory.code(TurmsStatusCode.ILLEGAL_DATE_FORMAT);
return ResponseFactory.collectCountResults(counts);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.function.Function3;

import java.util.*;

Expand Down Expand Up @@ -100,13 +99,6 @@ public Mono<ResponseEntity> countMessages(
}
if (divideBy == null || divideBy == DivideBy.NOOP) {
List<Mono<Pair<String, Long>>> counts = new LinkedList<>();
if (deliveredStartDate != null || deliveredEndDate != null) {
counts.add(messageService.countDeliveredMessages(
deliveredStartDate,
deliveredEndDate,
chatType)
.map(total -> Pair.of("deliveredMessages", total)));
}
if (deliveredOnAverageStartDate != null || deliveredOnAverageEndDate != null) {
counts.add(messageService.countDeliveredMessagesOnAverage(
deliveredOnAverageStartDate,
Expand All @@ -128,37 +120,23 @@ public Mono<ResponseEntity> countMessages(
chatType)
.map(total -> Pair.of("acknowledgedMessagesOnAverage", total)));
}
if (counts.isEmpty()) {
return ResponseFactory.code(TurmsStatusCode.ILLEGAL_ARGUMENTS);
}
Mono<Map<String, Long>> resultMono = Flux.merge(counts)
.collectList()
.map(pairs -> {
Map<String, Long> resultMap = new HashMap<>(counts.size());
for (Pair<String, ?> pair : pairs) {
resultMap.put(pair.getLeft(), (Long) pair.getRight());
}
return resultMap;
});
return ResponseFactory.okWhenTruthy(resultMono);
} else {
List<Mono<Pair<String, List<Map<String, ?>>>>> counts = new LinkedList<>();
if (deliveredStartDate != null && deliveredEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"deliveredMessages",
if (counts.isEmpty() || deliveredStartDate != null || deliveredEndDate != null) {
counts.add(messageService.countDeliveredMessages(
deliveredStartDate,
deliveredEndDate,
divideBy,
(Function3<Date, Date, ChatType, Mono<Long>>) messageService::countDeliveredMessages,
chatType));
chatType)
.map(total -> Pair.of("deliveredMessages", total)));
}
return ResponseFactory.collectCountResults(counts);
} else {
List<Mono<Pair<String, List<Map<String, ?>>>>> counts = new LinkedList<>();
if (deliveredOnAverageStartDate != null && deliveredOnAverageEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"deliveredMessagesOnAverage",
deliveredOnAverageStartDate,
deliveredOnAverageEndDate,
divideBy,
(Function3<Date, Date, ChatType, Mono<Long>>) messageService::countDeliveredMessagesOnAverage,
messageService::countDeliveredMessagesOnAverage,
chatType));
}
if (acknowledgedStartDate != null && acknowledgedEndDate != null) {
Expand All @@ -167,7 +145,7 @@ public Mono<ResponseEntity> countMessages(
acknowledgedStartDate,
acknowledgedEndDate,
divideBy,
(Function3<Date, Date, ChatType, Mono<Long>>) messageService::countAcknowledgedMessages,
messageService::countAcknowledgedMessages,
chatType));
}
if (acknowledgedOnAverageStartDate != null && acknowledgedOnAverageEndDate != null) {
Expand All @@ -176,13 +154,22 @@ public Mono<ResponseEntity> countMessages(
acknowledgedOnAverageStartDate,
acknowledgedOnAverageEndDate,
divideBy,
(Function3<Date, Date, ChatType, Mono<Long>>) messageService::countAcknowledgedMessagesOnAverage,
messageService::countAcknowledgedMessagesOnAverage,
chatType));
}
if (deliveredStartDate != null && deliveredEndDate != null) {
counts.add(DateTimeUtil.queryBetweenDate(
"deliveredMessages",
deliveredStartDate,
deliveredEndDate,
divideBy,
messageService::countDeliveredMessages,
chatType));
}
if (counts.isEmpty()) {
return ResponseFactory.code(TurmsStatusCode.ILLEGAL_ARGUMENTS);
}
return ResponseFactory.okWhenTruthy(Flux.merge(counts));
return ResponseFactory.collectCountResults(counts);
}
}
}
Loading

0 comments on commit 7bc17bf

Please sign in to comment.