diff --git a/src/main/java/run/halo/app/controller/admin/api/StatisticController.java b/src/main/java/run/halo/app/controller/admin/api/StatisticController.java new file mode 100644 index 000000000..7da92a497 --- /dev/null +++ b/src/main/java/run/halo/app/controller/admin/api/StatisticController.java @@ -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(); + } +} diff --git a/src/main/java/run/halo/app/controller/content/api/JournalController.java b/src/main/java/run/halo/app/controller/content/api/JournalController.java index a463ce481..d655825fb 100644 --- a/src/main/java/run/halo/app/controller/content/api/JournalController.java +++ b/src/main/java/run/halo/app/controller/content/api/JournalController.java @@ -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 { diff --git a/src/main/java/run/halo/app/controller/content/api/SheetController.java b/src/main/java/run/halo/app/controller/content/api/SheetController.java index 7302bc347..9785cc613 100644 --- a/src/main/java/run/halo/app/controller/content/api/SheetController.java +++ b/src/main/java/run/halo/app/controller/content/api/SheetController.java @@ -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 { diff --git a/src/main/java/run/halo/app/controller/content/api/StatisticController.java b/src/main/java/run/halo/app/controller/content/api/StatisticController.java new file mode 100644 index 000000000..9faca5bf8 --- /dev/null +++ b/src/main/java/run/halo/app/controller/content/api/StatisticController.java @@ -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(); + } +} diff --git a/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java b/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java index f2ceae190..6465fe425 100644 --- a/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java +++ b/src/main/java/run/halo/app/handler/file/SmmsFileHandler.java @@ -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. diff --git a/src/main/java/run/halo/app/model/dto/EnvironmentDTO.java b/src/main/java/run/halo/app/model/dto/EnvironmentDTO.java index b2fddafa4..3b66bc064 100644 --- a/src/main/java/run/halo/app/model/dto/EnvironmentDTO.java +++ b/src/main/java/run/halo/app/model/dto/EnvironmentDTO.java @@ -14,7 +14,7 @@ public class EnvironmentDTO { private String database; - private long startTime; + private Long startTime; private String version; diff --git a/src/main/java/run/halo/app/model/dto/StatisticDTO.java b/src/main/java/run/halo/app/model/dto/StatisticDTO.java index 5b8694345..a2ee103f8 100644 --- a/src/main/java/run/halo/app/model/dto/StatisticDTO.java +++ b/src/main/java/run/halo/app/model/dto/StatisticDTO.java @@ -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; } diff --git a/src/main/java/run/halo/app/model/dto/StatisticWithUserDTO.java b/src/main/java/run/halo/app/model/dto/StatisticWithUserDTO.java new file mode 100644 index 000000000..4c876c71c --- /dev/null +++ b/src/main/java/run/halo/app/model/dto/StatisticWithUserDTO.java @@ -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 { + + private UserDTO user; +} diff --git a/src/main/java/run/halo/app/model/support/CommentPage.java b/src/main/java/run/halo/app/model/support/CommentPage.java index 58b04664e..7a27146c3 100644 --- a/src/main/java/run/halo/app/model/support/CommentPage.java +++ b/src/main/java/run/halo/app/model/support/CommentPage.java @@ -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; diff --git a/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java b/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java index fca9303e2..b5ba1b8c5 100644 --- a/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java +++ b/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java @@ -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; diff --git a/src/main/java/run/halo/app/service/AdminService.java b/src/main/java/run/halo/app/service/AdminService.java index a6dd663d3..b4cc79417 100644 --- a/src/main/java/run/halo/app/service/AdminService.java +++ b/src/main/java/run/halo/app/service/AdminService.java @@ -59,6 +59,7 @@ public interface AdminService { * @return count dto */ @NonNull + @Deprecated StatisticDTO getCount(); /** diff --git a/src/main/java/run/halo/app/service/StatisticService.java b/src/main/java/run/halo/app/service/StatisticService.java new file mode 100644 index 000000000..7f4f19ef8 --- /dev/null +++ b/src/main/java/run/halo/app/service/StatisticService.java @@ -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(); +} diff --git a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java index c9a42a1f5..c3ce2dddf 100644 --- a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java @@ -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; diff --git a/src/main/java/run/halo/app/service/impl/StatisticServiceImpl.java b/src/main/java/run/halo/app/service/impl/StatisticServiceImpl.java new file mode 100644 index 000000000..ddeb96cdd --- /dev/null +++ b/src/main/java/run/halo/app/service/impl/StatisticServiceImpl.java @@ -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; + } +}