走势图通用写法

需求:

天走势图,周走势图,月走势图

天走势图需要每十分钟(或者其他时间间隔)。周走势图和月走势图为一天。时间和数据对应,并且需要排序。

需要数据:

天走势图的原始数据每五分钟存在一次数据。(或者其他时间间隔)

周以上的走势图按照每天聚合一次数据。

使用数据库:

mongodb

做法:

先生成排好序的hashmap。

//sn天hash
    private ImmutableSortedMap<Long, Flow> generateDayHash(Date startDate, Date endDate, String sn) {
        Builder<Long, Flow> builder = ImmutableSortedMap.naturalOrder();
        Long start = startDate.getTime();
        Long end = endDate.getTime();
        Date nextHour = DateUtil.calHour2DateStandard(startDate, 1);
        Long next = nextHour.getTime();

        for(Long l = next; l > start; l = l - HOUR_AGGREGATION_TIME) {
            Flow flowlog = new Flow();
            Date date = new Date(l);
            String dateString = DateUtil.date2String("yyyy-MM-dd HH:mm:ss", date);
            flowlog.setTimeString(dateString);
            flowlog.setSn(sn);
            flowlog.setRxBytes(0l);
            builder.put(l, flowlog);
        }
        ImmutableSortedMap<Long, Flow> map = builder.build();
        for(Long l = next; l < end; l = l + HOUR_AGGREGATION_TIME) {
            if(!map.containsKey(l)) {
                Flow flowlog = new Flow();
                Date date = new Date(l);
                String dateString = DateUtil.date2String("yyyy-MM-dd HH:mm:ss", date);
                flowlog.setTimeString(dateString);
                flowlog.setSn(sn);
                flowlog.setRxBytes(0l);
                builder.put(l, flowlog);
            }
        }
        map = builder.build();
        return map;
    }

获取最接近的时间整数

//取得最近的整数时间,10分钟间隔。例如12:26则为12:30。
    private Long getAggregatedTime(String time) {
        String[] tmp = time.split(":");
        int i = Integer.parseInt(tmp[1].substring(0,1)) + 1; //调整这个1,可以调整到20分钟间隔,30分钟间隔等
        String dateString = tmp[0] + ":" + i + "0:00";
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(dateString).getTime();
        } catch (ParseException e) {
            LOGGER.error("failed to execute method getAggregatedTime in flowService {}", e);
        }
        
        return 0l;
    }

 

返回天走势图

//返回sn天走势图-间隔10分钟
    public List<Flow> getDayFlowBySn(Date startDate, Date endDate, String sn, String type) {
        List<Flow> result = new ArrayList<>();
        List<Flowlog> list = new ArrayList<>();
        if(!legalParam(startDate, endDate))
            return result;
        Long start = startDate.getTime();
        Long end = endDate.getTime();
        try {
            list = mongo.find(new Query(Criteria
                    .where(FlowlogField.START)
                    .gte(start).lte(end).and(FlowlogField.SN).is(sn).and(FlowlogField.INTF_TYPE).is(type))
                    , Flowlog.class
                    );
            ImmutableSortedMap<Long, Flow> map = generateDayHash(startDate, endDate, sn);
            result = getDayFlow(list, map);
            return result;
        } catch (Exception e) {
            LOGGER.error("execute method getDayFlowBySn failed {}", e);
            return result;
        }
    }

 

posted @ 2016-04-29 10:53  無限大  阅读(500)  评论(0编辑  收藏  举报