feat: add blog statistics api.

pull/435/head
ruibaby 2019-12-16 22:25:29 +08:00
parent 26da4bd3c7
commit f1dbeef81a
14 changed files with 252 additions and 19 deletions

View File

@ -0,0 +1,38 @@
package run.halo.app.controller.admin.api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.dto.StatisticWithUserDTO;
import run.halo.app.service.StatisticService;
/**
* Statistic controller.
*
* @author ryan0up
* @date 2019-12-16
*/
@RestController
@RequestMapping("/api/admin/statistics")
public class StatisticController {
private final StatisticService statisticService;
public StatisticController(StatisticService statisticService) {
this.statisticService = statisticService;
}
@GetMapping
@ApiOperation("Gets blog statistics.")
public StatisticDTO statistics() {
return statisticService.getStatistic();
}
@GetMapping("user")
@ApiOperation("Gets blog statistics with user")
public StatisticWithUserDTO statisticsWithUser() {
return statisticService.getStatisticWithUser();
}
}

View File

@ -35,7 +35,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
* @author ryanwang
* @date 2019-04-26
*/
@RestController("PortalJournalController")
@RestController("ApiContentJournalController")
@RequestMapping("/api/content/journals")
public class JournalController {

View File

@ -35,7 +35,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
* @author ryanwang
* @date 19-4-26
*/
@RestController("PortalSheetController")
@RestController("ApiContentSheetController")
@RequestMapping("/api/content/sheets")
public class SheetController {

View File

@ -0,0 +1,38 @@
package run.halo.app.controller.content.api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.dto.StatisticWithUserDTO;
import run.halo.app.service.StatisticService;
/**
* Statistic controller.
*
* @author ryan0up
* @date 2019-12-16
*/
@RestController("ApiContentStatisticController")
@RequestMapping("/api/content/statistics")
public class StatisticController {
private final StatisticService statisticService;
public StatisticController(StatisticService statisticService) {
this.statisticService = statisticService;
}
@GetMapping
@ApiOperation("Gets blog statistics.")
public StatisticDTO statistics() {
return statisticService.getStatistic();
}
@GetMapping("user")
@ApiOperation("Gets blog statistics with user")
public StatisticWithUserDTO statisticsWithUser() {
return statisticService.getStatisticWithUser();
}
}

View File

@ -4,9 +4,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
@ -25,7 +23,8 @@ import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HttpClientUtils;
import java.io.IOException;
import java.util.*;
import java.util.Collections;
import java.util.Objects;
/**
* Sm.ms file handler.

View File

@ -14,7 +14,7 @@ public class EnvironmentDTO {
private String database;
private long startTime;
private Long startTime;
private String version;

View File

@ -6,24 +6,32 @@ import lombok.Data;
* Statistic DTO.
*
* @author johnniang
* @date 3/19/19
* @author ryanwang
* @date 2019-03-19
*/
@Data
public class StatisticDTO {
private long postCount;
private Long postCount;
private long commentCount;
private Long commentCount;
private long attachmentCount;
private Long categoryCount;
private long birthday;
@Deprecated
private Long attachmentCount;
private long establishDays;
private Long tagCount;
private long linkCount;
private Long journalCount;
private long visitCount;
private Long birthday;
private long likeCount;
private Long establishDays;
private Long linkCount;
private Long visitCount;
private Long likeCount;
}

View File

@ -0,0 +1,16 @@
package run.halo.app.model.dto;
import lombok.Data;
import run.halo.app.model.dto.base.OutputConverter;
/**
* Statistic with user info DTO.
*
* @author ryanwang
* @date 2019-12-16
*/
@Data
public class StatisticWithUserDTO extends StatisticDTO implements OutputConverter<StatisticWithUserDTO, StatisticDTO> {
private UserDTO user;
}

View File

@ -1,6 +1,8 @@
package run.halo.app.model.support;
import lombok.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

View File

@ -7,9 +7,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.Transactional;
import run.halo.app.model.entity.BaseComment;
import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.projection.CommentChildrenCountProjection;
import run.halo.app.model.projection.CommentCountProjection;

View File

@ -59,6 +59,7 @@ public interface AdminService {
* @return count dto
*/
@NonNull
@Deprecated
StatisticDTO getCount();
/**

View File

@ -0,0 +1,27 @@
package run.halo.app.service;
import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.dto.StatisticWithUserDTO;
/**
* Statistic service interface.
*
* @author ryanwang
* @date 2019-12-16
*/
public interface StatisticService {
/**
* Get blog statistic.
*
* @return statistic dto.
*/
StatisticDTO getStatistic();
/**
* Get blog statistic with user info.
*
* @return statistic with user info dto.
*/
StatisticWithUserDTO getStatisticWithUser();
}

View File

@ -13,7 +13,6 @@ import run.halo.app.event.post.SheetVisitEvent;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.InternalSheetDTO;
import run.halo.app.model.entity.PostComment;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.entity.SheetComment;
import run.halo.app.model.entity.SheetMeta;

View File

@ -0,0 +1,107 @@
package run.halo.app.service.impl;
import org.springframework.stereotype.Service;
import run.halo.app.exception.ServiceException;
import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.dto.StatisticWithUserDTO;
import run.halo.app.model.dto.UserDTO;
import run.halo.app.model.entity.User;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.*;
/**
* Statistic service implementation.
*
* @author ryanwang
* @date 2019-12-16
*/
@Service
public class StatisticServiceImpl implements StatisticService {
private final PostService postService;
private final SheetService sheetService;
private final JournalService journalService;
private final PostCommentService postCommentService;
private final SheetCommentService sheetCommentService;
private final JournalCommentService journalCommentService;
private final OptionService optionService;
private final LinkService linkService;
private final CategoryService categoryService;
private final TagService tagService;
private final UserService userService;
public StatisticServiceImpl(PostService postService,
SheetService sheetService,
JournalService journalService,
PostCommentService postCommentService,
SheetCommentService sheetCommentService,
JournalCommentService journalCommentService,
OptionService optionService,
LinkService linkService,
CategoryService categoryService,
TagService tagService,
UserService userService) {
this.postService = postService;
this.sheetService = sheetService;
this.journalService = journalService;
this.postCommentService = postCommentService;
this.sheetCommentService = sheetCommentService;
this.journalCommentService = journalCommentService;
this.optionService = optionService;
this.linkService = linkService;
this.categoryService = categoryService;
this.tagService = tagService;
this.userService = userService;
}
@Override
public StatisticDTO getStatistic() {
StatisticDTO statisticDTO = new StatisticDTO();
statisticDTO.setPostCount(postService.countByStatus(PostStatus.PUBLISHED) + sheetService.countByStatus(PostStatus.PUBLISHED));
// Handle comment count
long postCommentCount = postCommentService.countByStatus(CommentStatus.PUBLISHED);
long sheetCommentCount = sheetCommentService.countByStatus(CommentStatus.PUBLISHED);
long journalCommentCount = journalCommentService.countByStatus(CommentStatus.PUBLISHED);
statisticDTO.setCommentCount(postCommentCount + sheetCommentCount + journalCommentCount);
statisticDTO.setTagCount(tagService.count());
statisticDTO.setCategoryCount(categoryService.count());
statisticDTO.setJournalCount(journalService.count());
long birthday = optionService.getBirthday();
long days = (System.currentTimeMillis() - birthday) / (1000 * 24 * 3600);
statisticDTO.setEstablishDays(days);
statisticDTO.setBirthday(birthday);
statisticDTO.setLinkCount(linkService.count());
statisticDTO.setVisitCount(postService.countVisit() + sheetService.countVisit());
statisticDTO.setLikeCount(postService.countLike() + sheetService.countLike());
return statisticDTO;
}
@Override
public StatisticWithUserDTO getStatisticWithUser() {
StatisticDTO statisticDTO = getStatistic();
StatisticWithUserDTO statisticWithUserDTO = new StatisticWithUserDTO();
statisticWithUserDTO.convertFrom(statisticDTO);
User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("未查询到博主信息"));
statisticWithUserDTO.setUser(new UserDTO().convertFrom(user));
return statisticWithUserDTO;
}
}