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. ansible异步任务

    转载于简书博客 https://www.jianshu.com/p/3962bf94ae70 ansible方便在于能批量下发,并返回结果和呈现.简单.高效. 但有的任务执行起来却不那么直接,可能会花 ...

  2. Python中的抽象基类

    1.说在前头 "抽象基类"这个词可能听着比较"深奥",其实"基类"就是"父类","抽象"就是&quo ...

  3. oracle(9) 序列和约束

    序列 SEQUENCE 也是数据库对象之一,作用:根据指定的规则生成一些列数字. 序列通常是为某张表的主键提供值使用. 主键:通常每张表都会有主键字段,该字段的值要求非空且唯一, 使用该字段来确定表中 ...

  4. 18 12 4 SQL 的基本 语法

    数据库的基本语法 -- 数据库的操作 -- 链接数据库 mysql -uroot -p mysql -uroot -pmysql -- 退出数据库 exit/quit/ctrl+d -- sql语句最 ...

  5. 7. react 基础 - React Developer Tools 的安装 及 使用

    1. 安装 react 开发调试工具 React Developer Tools 打开 chrome 浏览器访问 chrome://extensions/ 点击右上角的 拓展程序 -> 打开 c ...

  6. .NET 软件下面win10自动启动配置

    1.设置所有用户登录都能启动,打开文件夹 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 2.给要启动的应用程序创建快捷方式, ...

  7. django_filter的values / values_list

    from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagli ...

  8. 转载:Apache优化:修改最大并发连接数

    本文转自:https://blog.csdn.net/bjash/article/details/50394894 Apache是一个跨平台的web服务器,由于其简单.稳定安全的特性,被广泛应用于计算 ...

  9. JavaScript—面向对象贪吃蛇_1

    前面说了.面向对象的思考方式和面向过程的思考方式有着本质的区别. 贪吃蛇.作为各大培训机构.面向对象的练手项目,的确好.我昨天看完视频,有一种领悟面向对象的感觉,当然可能只针对贪吃蛇..要想在实际开发 ...

  10. 计蒜客 数独(DFS)

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