📈 完善数据分析功能

This commit is contained in:
ronger 2020-01-19 21:44:35 +08:00
parent 302620425d
commit 558c989254
6 changed files with 260 additions and 3 deletions

View File

@ -0,0 +1,15 @@
package com.rymcu.vertical.dto.admin;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class DashboardData {
private String label;
private Integer value;
}

View File

@ -1,7 +1,10 @@
package com.rymcu.vertical.mapper; package com.rymcu.vertical.mapper;
import com.rymcu.vertical.dto.admin.DashboardData;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* @author ronger * @author ronger
*/ */
@ -41,4 +44,38 @@ public interface DashboardMapper {
* @return * @return
*/ */
Integer selectTodayViewNum(); Integer selectTodayViewNum();
/**
* 获取最近 30 天文章数据
* @return
*/
List<DashboardData> selectLastThirtyDaysArticleData();
/**
* 获取最近 30 天文章数据
* @return
*/
List<DashboardData> selectLastThirtyDaysUserData();
/**
* 获取最近 30 天文章数据
* @return
*/
List<DashboardData> selectLastThirtyDaysVisitData();
/**
*
* @return
*/
List<DashboardData> selectHistoryArticleData();
/**
* @return
*/
List<DashboardData> selectHistoryUserData();
/**
* @return
*/
List<DashboardData> selectHistoryVisitData();
} }

View File

@ -2,6 +2,8 @@ package com.rymcu.vertical.service;
import com.rymcu.vertical.dto.admin.Dashboard; import com.rymcu.vertical.dto.admin.Dashboard;
import java.util.Map;
/** /**
* @author ronger * @author ronger
*/ */
@ -12,4 +14,16 @@ public interface DashboardService {
* @return * @return
* */ * */
Dashboard dashboard(); Dashboard dashboard();
/**
* 统计最近三十天数据
* @return
*/
Map lastThirtyDaysData();
/**
* 获取历史数据
* @return
*/
Map history();
} }

View File

@ -1,11 +1,14 @@
package com.rymcu.vertical.service.impl; package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.dto.admin.Dashboard; import com.rymcu.vertical.dto.admin.Dashboard;
import com.rymcu.vertical.dto.admin.DashboardData;
import com.rymcu.vertical.mapper.DashboardMapper; import com.rymcu.vertical.mapper.DashboardMapper;
import com.rymcu.vertical.service.DashboardService; import com.rymcu.vertical.service.DashboardService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
/** /**
* @author ronger * @author ronger
@ -27,4 +30,151 @@ public class DashboardServiceImpl implements DashboardService {
dashboard.setTodayViewNum(dashboardMapper.selectTodayViewNum()); dashboard.setTodayViewNum(dashboardMapper.selectTodayViewNum());
return dashboard; return dashboard;
} }
@Override
public Map lastThirtyDaysData() {
Map map = new HashMap(4);
ArrayList<String> dates = new ArrayList(30);
ArrayList<Integer> articleData = new ArrayList(30);
ArrayList<Integer> userData = new ArrayList(30);
ArrayList<Integer> visitData = new ArrayList(30);
List<DashboardData> articles = dashboardMapper.selectLastThirtyDaysArticleData();
List<DashboardData> users = dashboardMapper.selectLastThirtyDaysUserData();
List<DashboardData> visits = dashboardMapper.selectLastThirtyDaysVisitData();
LocalDate now = LocalDate.now().plusDays(1);
LocalDate localDate = LocalDate.now().plusDays(-29);
while (now.isAfter(localDate)) {
String date = localDate.toString();
dates.add(date);
articles.forEach(article->{
if (date.equals(article.getLabel())) {
articleData.add(article.getValue());
return;
}
});
if (articleData.size() < dates.size()) {
articleData.add(0);
}
users.forEach(user->{
if (date.equals(user.getLabel())) {
userData.add(user.getValue());
return;
}
});
if (userData.size() < dates.size()) {
userData.add(0);
}
visits.forEach(visit->{
if (date.equals(visit.getLabel())) {
visitData.add(visit.getValue());
return;
}
});
if (visitData.size() < dates.size()) {
visitData.add(0);
}
localDate = localDate.plusDays(1);
}
map.put("dates", dates);
map.put("articles", articleData);
map.put("users", userData);
map.put("visits", visitData);
return map;
}
@Override
public Map history() {
Map map = new HashMap(4);
ArrayList<String> dates = new ArrayList(30);
ArrayList<Integer> articleData = new ArrayList(30);
ArrayList<Integer> userData = new ArrayList(30);
ArrayList<Integer> visitData = new ArrayList(30);
List<DashboardData> articles = dashboardMapper.selectHistoryArticleData();
List<DashboardData> users = dashboardMapper.selectHistoryUserData();
List<DashboardData> visits = dashboardMapper.selectHistoryVisitData();
LocalDate now = LocalDate.now().plusMonths(1);
LocalDate localDate = LocalDate.now().plusYears(-1).plusMonths(1);
while (now.getYear() >= localDate.getYear()) {
if (now.getYear() == localDate.getYear()) {
if (now.getMonthValue() > localDate.getMonthValue()){
String date = localDate.getYear() + "-" + (localDate.getMonthValue() < 10 ? "0" + localDate.getMonthValue() : localDate.getMonthValue());
dates.add(date);
articles.forEach(article->{
if (date.equals(article.getLabel())) {
articleData.add(article.getValue());
return;
}
});
if (articleData.size() < dates.size()) {
articleData.add(0);
}
users.forEach(user->{
if (date.equals(user.getLabel())) {
userData.add(user.getValue());
return;
}
});
if (userData.size() < dates.size()) {
userData.add(0);
}
visits.forEach(visit->{
if (date.equals(visit.getLabel())) {
visitData.add(visit.getValue());
return;
}
});
if (visitData.size() < dates.size()) {
visitData.add(0);
}
}
} else {
String date = localDate.getYear() + "-" + (localDate.getMonthValue() < 10 ? "0" + localDate.getMonthValue() : localDate.getMonthValue());
dates.add(date);
articles.forEach(article->{
if (date.equals(article.getLabel())) {
articleData.add(article.getValue());
return;
}
});
if (articleData.size() < dates.size()) {
articleData.add(0);
}
users.forEach(user->{
if (date.equals(user.getLabel())) {
userData.add(user.getValue());
return;
}
});
if (userData.size() < dates.size()) {
userData.add(0);
}
visits.forEach(visit->{
if (date.equals(visit.getLabel())) {
visitData.add(visit.getValue());
return;
}
});
if (visitData.size() < dates.size()) {
visitData.add(0);
}
}
localDate = localDate.plusMonths(1);
}
map.put("dates", dates);
map.put("articles", articleData);
map.put("users", userData);
map.put("visits", visitData);
return map;
}
} }

View File

@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Map;
/** /**
* @author ronger * @author ronger
@ -25,4 +26,16 @@ public class DashboardController {
Dashboard dashboard = dashboardService.dashboard(); Dashboard dashboard = dashboardService.dashboard();
return GlobalResultGenerator.genSuccessResult(dashboard); return GlobalResultGenerator.genSuccessResult(dashboard);
} }
@GetMapping("/last-thirty-days")
public GlobalResult LastThirtyDaysData() {
Map map = dashboardService.lastThirtyDaysData();
return GlobalResultGenerator.genSuccessResult(map);
}
@GetMapping("/history")
public GlobalResult history() {
Map map = dashboardService.history();
return GlobalResultGenerator.genSuccessResult(map);
}
} }

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rymcu.vertical.mapper.DashboardMapper"> <mapper namespace="com.rymcu.vertical.mapper.DashboardMapper">
<resultMap id="DashboardDataResultMap" type="com.rymcu.vertical.dto.admin.DashboardData">
<result column="label" property="label"></result>
<result column="value" property="value"></result>
</resultMap>
<select id="selectUserCount" resultType="java.lang.Integer"> <select id="selectUserCount" resultType="java.lang.Integer">
select count(*) from vertical_user select count(*) from vertical_user
</select> </select>
@ -19,7 +23,31 @@
select count(*) from vertical_visit select count(*) from vertical_visit
</select> </select>
<select id="selectTodayViewNum" resultType="java.lang.Integer"> <select id="selectTodayViewNum" resultType="java.lang.Integer">
select count(*) from vertical_visit where created_time between date_sub(sysdate(),interval 1 day) select count(*) from vertical_visit where created_time between str_to_date(date_format(sysdate(),'%Y-%m-%d'),'%Y-%m-%d')
and date_sub(sysdate(),interval - 1 day) and str_to_date(date_format(date_sub(sysdate(),interval - 1 day),'%Y-%m-%d'),'%Y-%m-%d')
</select>
<select id="selectLastThirtyDaysArticleData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m-%d') as label from vertical_article
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 30 day),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m-%d')
</select>
<select id="selectLastThirtyDaysUserData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m-%d') as label from vertical_user
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 30 day),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m-%d')
</select>
<select id="selectLastThirtyDaysVisitData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m-%d') as label from vertical_visit
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 30 day),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m-%d')
</select>
<select id="selectHistoryArticleData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m') as label from vertical_article
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 1 year),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m')
</select>
<select id="selectHistoryUserData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m') as label from vertical_user
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 1 year),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m')
</select>
<select id="selectHistoryVisitData" resultMap="DashboardDataResultMap">
select COUNT(*) as value, date_format(created_time, '%Y-%m') as label from vertical_visit
where created_time > str_to_date(date_format(date_sub(sysdate(),interval + 1 year),'%Y-%m-%d'),'%Y-%m-%d') GROUP BY date_format(created_time, '%Y-%m')
</select> </select>
</mapper> </mapper>