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的更多相关文章

随机推荐

  1. Arduino IIC 主从设备连接通信

    目的:        实现Arduino主从设备之间的互相IIC通信,掌握IIC通信协议的使用方法. 器材: Arduino UNO R3 一块 Arduino Nano 三块 面包板   导线 3K ...

  2. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  3. vmbox 导入虚拟电脑之后无法上网

    先执行 ip addr 查看有没有分配ip 用root执行dhclient -v命令去通过DHCP协议获取一个ip,在下图的最后一行可以看到ip已经分配成功dhclient命令可以用来释放你的电脑的I ...

  4. ci框架与smarty的整合

    ci框架与smarty的整合 来源:未知    时间:2014-10-20 11:38   阅读数:108   作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...

  5. 20个GitHub最热门的Java开源项目:文档、框架、工具

    专注于Java领域优质技术,欢迎关注 文章来源:JavaGuide 以下涉及到的数据统计,数据来源:https://github.com/trending/java?since=monthly[1] ...

  6. epoll机制

    一.参考网址 1.epoll机制:epoll_create.epoll_ctl.epoll_wait.close 2.Linux网络编程 使用epoll实现一个高性能TCP Echo服务器 3.用C写 ...

  7. share团队冲刺3

    团队冲刺第三天 昨天:完成了对输出文字,按钮控件的添加,能够将其在模拟器上运行 今天:学习输入的添加方式 问题:Android resource linking failed 在改变按钮样式的时候,出 ...

  8. 计蒜客 数独(DFS)

    蒜头君今天突然开始还念童年了,想回忆回忆童年.他记得自己小时候,有一个很火的游戏叫做数独.便开始来了一局紧张而又刺激的高阶数独.蒜头君做完发现没有正解,不知道对不对? 不知道聪明的你能否给出一个标准答 ...

  9. 64)vertor 简单使用

    1)简单 代码样例:我的理解   vector  其实就是一个简单的数组,然后通过迭代器来进行 遍历数组中的值,而且有自带push_back()来添加元素 #include<iostream&g ...

  10. UML-SSD总结

    1.不是所有场景都需要画SSD.需要画SSD的场景: 1).主成功场景 2).频繁发生的场景 3).复杂的场景 2.角色 1).参与者 2).系统(没有类,即黑盒) 3.画SSD时间不要过长,一般几分 ...