• 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();
        }
    }
  • 相关阅读:
    2013年第四届蓝桥杯C/C++ A组国赛 —— 第三题:埃及分数
    单链表————链表
    单链表————顺序表
    跳水板
    平衡二叉搜索树(最小高度树)
    删除字符串中的所有相邻重复项
    用两个栈实现队列
    删除最外层的括号
    合并两个有序链表
    C++ cout格式化输出(输出格式)完全攻略
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11220967.html
Copyright © 2020-2023  润新知