VisitsService
package me.zhengjie.monitor.domain; import lombok.Data;
import org.hibernate.annotations.CreationTimestamp; import javax.persistence.*;
import java.sql.Timestamp; /**
* pv 与 ip 统计
*
* @author jie
* @date 2018-12-13
*/
@Entity
@Data
@Table(name = "visits")
public class Visits { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; @Column(unique = true)
private String date; @Column(name = "pv_counts")
private Long pvCounts; @Column(name = "ip_counts")
private Long ipCounts; @CreationTimestamp
private Timestamp createTime; private String weekDay;
}
package me.zhengjie.monitor.repository; import me.zhengjie.monitor.domain.Visits;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import java.util.List; /**
* @author jie
* @date 2018-12-13
*/
@Repository
public interface VisitsRepository extends JpaRepository<Visits,Long> { /**
* findByDate
* @param date
* @return
*/
Visits findByDate(String date); /**
* 获得一个时间段的记录
* @param date1
* @param date2
* @return
*/
@Query(value = "select * FROM visits where " +
"createTime between ?1 and ?2",nativeQuery = true)
List<Visits> findAllVisits(String date1, String date2);
}
package me.zhengjie.monitor.service; import org.springframework.scheduling.annotation.Async; import javax.servlet.http.HttpServletRequest; /**
* @author jie
* @date 2018-12-13
*/
public interface VisitsService { /**
* 提供给定时任务,每天0点执行
*/
void save(); /**
* 新增记录
* @param request
*/
@Async
void count(HttpServletRequest request); /**
* 获取数据
* @return
*/
Object get(); /**
* getChartData
* @return
*/
Object getChartData();
}
package me.zhengjie.monitor.service.impl; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.common.utils.TimeUtil;
import me.zhengjie.monitor.domain.Visits;
import me.zhengjie.monitor.repository.LoggingRepository;
import me.zhengjie.monitor.repository.VisitsRepository;
import me.zhengjie.monitor.service.VisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; /**
* @author jie
* @date 2018-12-13
*/
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class VisitsServiceImpl implements VisitsService { @Autowired
private VisitsRepository visitsRepository; @Autowired
private LoggingRepository loggingRepository; @Override
public void save() {
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
if(visits == null){
visits = new Visits();
visits.setWeekDay(TimeUtil.getWeekDay());
visits.setPvCounts(1L);
visits.setIpCounts(1L);
visits.setDate(localDate.toString());
visitsRepository.save(visits);
}
} @Override
public void count(HttpServletRequest request) {
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
visits.setPvCounts(visits.getPvCounts()+1);
long ipCounts = loggingRepository.findIp(localDate.toString(), localDate.plusDays(1).toString());
visits.setIpCounts(ipCounts);
visitsRepository.save(visits);
} @Override
public Object get() {
Map map = new HashMap();
LocalDate localDate = LocalDate.now();
Visits visits = visitsRepository.findByDate(localDate.toString());
List<Visits> list = visitsRepository.findAllVisits(localDate.minusDays(6).toString(),localDate.plusDays(1).toString()); long recentVisits = 0, recentIp = 0;
for (Visits data : list) {
recentVisits += data.getPvCounts();
recentIp += data.getIpCounts();
}
map.put("newVisits",visits.getPvCounts());
map.put("newIp",visits.getIpCounts());
map.put("recentVisits",recentVisits);
map.put("recentIp",recentIp);
return map;
} @Override
public Object getChartData() {
Map map = new HashMap();
LocalDate localDate = LocalDate.now();
List<Visits> list = visitsRepository.findAllVisits(localDate.minusDays(6).toString(),localDate.plusDays(1).toString());
map.put("weekDays",list.stream().map(Visits::getWeekDay).collect(Collectors.toList()));
map.put("visitsData",list.stream().map(Visits::getPvCounts).collect(Collectors.toList()));
map.put("ipData",list.stream().map(Visits::getIpCounts).collect(Collectors.toList()));
return map;
}
}
package me.zhengjie.monitor.rest; import me.zhengjie.common.utils.RequestHolder;
import me.zhengjie.monitor.service.VisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author jie
* @date 2018-12-13
*/
@RestController
@RequestMapping("api")
public class VisitsController { @Autowired
private VisitsService visitsService; @PostMapping(value = "/visits")
public ResponseEntity create(){
visitsService.count(RequestHolder.getHttpServletRequest());
return new ResponseEntity(HttpStatus.CREATED);
} @GetMapping(value = "/visits")
public ResponseEntity get(){
return new ResponseEntity(visitsService.get(),HttpStatus.OK);
} @GetMapping(value = "/visits/chartData")
public ResponseEntity getChartData(){
return new ResponseEntity(visitsService.getChartData(),HttpStatus.OK);
}
}
package me.zhengjie.monitor.config; import me.zhengjie.monitor.service.VisitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* @author jie
* @date 2018-12-25
*/
@Component
@Async
public class VisitsScheduling { @Autowired
private VisitsService visitsService; /**
* 每天0点运行
*/
@Scheduled(cron = "0 0 0 * * ?")
public void save(){
visitsService.save();
}
}
package me.zhengjie.monitor.config; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.monitor.service.VisitsService;
import org.springframework.context.annotation.Configuration; /**
* 初始化站点统计
*/
@Slf4j
@Configuration
public class VisitsInitialization { public VisitsInitialization(VisitsService visitsService){
log.info("--------------- 初始化站点统计,如果存在今日统计则跳过 ---------------");
visitsService.save();
}
}
VisitsService的更多相关文章
随机推荐
- vSphere Replication5.5安装
vSphere Replication5.5概述 VMware vSphere Replication简称VR是 VMwarevCenter Server 的扩展,提供基于管理程序的虚拟机复制和恢复功 ...
- 【SpringBoot】SpringBoot Web开发(八)
本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...
- 基于python的小波阈值去噪算法
https://blog.csdn.net/alwaystry/article/details/52756051 发表于 2018-01-10 16:32:17 嵌入式设计应用 +关注 小波图像去噪原 ...
- 如何在MySQL目录下找到my.ini
1. 打开ProgramData目录 2. 进入目录C:\ProgramData\MySQL\MySQL Server 8.0
- nginx worker进程循环
worker进程启动后,其首先会初始化自身运行所需要的环境,然后会进入一个循环,在该循环中不断检查是否有需要执行的事件,然后处理事件.在这个过程中,worker进程也是需要与master进程交互的,更 ...
- 201712-2 游戏 Java
思路: 第一感觉有点像约瑟夫环.想到用队列解决比较好理解 import java.util.LinkedList; import java.util.Queue; import java.util.S ...
- javaweb01
Java web应用由一组servlet.HTML页,类,以及它可以被绑定的资源构成,它可以在各种供应商提供的实现servlet规范容器中运行javaweb包括这些:Servlet jsp 实用类 静 ...
- mui 上拉加载
最近做到移动端页面的开发,需要mui 的上拉刷新功能,最后实现后整理代码如下: 1.需要引入的js <link href="../resource/css/mui.min.css&qu ...
- 特斯拉私有化VS蔚来上市,电动汽车站在十字路口上
当下,对于电动汽车来说既是一个最好的时代,也是一个最坏的时代.好的一面是业界.投资者.消费者对电动汽车的关注度愈来愈高,坏的一面则是电动汽车正处于一个非常尴尬的处境.从大环境来看,电动汽车自身的产品力 ...
- SpringBoot+SpringSecurity+jwt整合及初体验
原来一直使用shiro做安全框架,配置起来相当方便,正好有机会接触下SpringSecurity,学习下这个.顺道结合下jwt,把安全信息管理的问题扔给客户端, 准备 首先用的是SpringBoot, ...