时间类型统计

1.事件类型工具类:

package com.rk.eai.basic.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.rk.eai.basic.common.consts.Consts;
import com.rk.eai.basic.common.utils.DateUtils;
import lombok.Getter;
import lombok.var;

import java.security.acl.LastOwnerException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * 时间类型
 */
@Getter
public enum TimeTypeDayToSeasonEnum {
    DAY(1, "天"),
    WEEK(2, "周"),
    MONTH(3, "月"),
    SEASON(4, "季"),
    YEAR(5, "年"),
    LATE_WEEK(6, "近7天"),
    LATE_MONTH(7, "近一个月");


    TimeTypeDayToSeasonEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @EnumValue
    private final int code;
    private final String desc;

    // 获取开始时间,结束时间
    public List<LocalDateTime> getStartTimeAndEndTime(LocalDateTime t) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        switch (this) {
            case DAY:
                startTime = LocalDateTime.of(t.getYear(), t.getMonth(), t.getDayOfMonth(), 0, 0);
                endTime = LocalDateTime.of(t.getYear(), t.getMonth(), t.getDayOfMonth(), 23, 59, 59);
                break;
            case WEEK:
                // 以周一开始
                var weekDiff = t.getDayOfWeek().getValue() - 1;
                startTime = LocalDateTime.of(t.getYear(), t.getMonth(), t.getDayOfMonth() - weekDiff, 0, 0);
                endTime = startTime.plusWeeks(1).minusSeconds(1);
                break;
            case LATE_WEEK:
                ZoneId zoneId = ZoneId.systemDefault();
                Calendar calendar = Calendar.getInstance();
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendar.set(Calendar.HOUR_OF_DAY, 24);
                endTime = LocalDateTime.ofInstant(calendar.getTime().toInstant(), zoneId);
                calendar.set(Calendar.HOUR_OF_DAY, -168);
                startTime = LocalDateTime.ofInstant(calendar.getTime().toInstant(), zoneId);
                break;
            case LATE_MONTH:
                Calendar calendars = Calendar.getInstance();
                ZoneId zoneIds = ZoneId.systemDefault();
                calendars.set(calendars.get(Calendar.YEAR), calendars.get(Calendar.MONTH), calendars.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
                calendars.set(Calendar.HOUR_OF_DAY, 24);
                endTime = LocalDateTime.ofInstant(calendars.getTime().toInstant(), zoneIds);
                calendars.set(Calendar.HOUR_OF_DAY, -720);
                startTime = LocalDateTime.ofInstant(calendars.getTime().toInstant(), zoneIds);
                break;
            case MONTH:
                startTime = LocalDateTime.of(t.getYear(), t.getMonth(), 1, 0, 0);
                endTime = startTime.plusMonths(1).minusSeconds(1);
                break;
            case YEAR:
                LocalDateTime date = LocalDateTime.now();
                startTime = date.with(TemporalAdjusters.firstDayOfYear());
                endTime = date.with(TemporalAdjusters.lastDayOfMonth());
                break;
            case SEASON:
                var season = (t.getMonth().getValue() - 1) / 3;
                switch (season) {
                    case 0:
                        startTime = LocalDateTime.of(t.getYear(), 1, 1, 0, 0);
                        endTime = LocalDateTime.of(t.getYear(), 3, 31, 23, 59, 59);
                        break;
                    case 1:
                        startTime = LocalDateTime.of(t.getYear(), 4, 1, 0, 0);
                        endTime = LocalDateTime.of(t.getYear(), 6, 30, 23, 59, 59);
                        break;
                    case 2:
                        startTime = LocalDateTime.of(t.getYear(), 7, 1, 0, 0);
                        endTime = LocalDateTime.of(t.getYear(), 9, 30, 23, 59, 59);
                        break;
                    case 3:
                        startTime = LocalDateTime.of(t.getYear(), 10, 1, 0, 0);
                        endTime = LocalDateTime.of(t.getYear(), 12, 31, 23, 59, 59);
                        break;
                }

                break;
        }
        return Arrays.asList(startTime, endTime);
    }


    public int getDurationHour() {
        switch (this) {
            case DAY:
                return 1;
            case WEEK:
            case LATE_WEEK:
            case LATE_MONTH:
            case SEASON:
            case MONTH:
                return 24;
            default:
                return 1;
        }
    }








    public static void main(String[] args) {
      LocalDateTime  startTime = LocalDateTime.of(LocalDateTime.now(Consts.CST).getYear(), LocalDateTime.now(Consts.CST).getMonth(), 1, 0, 0);
       LocalDateTime endTime = startTime.plusMonths(1).minusSeconds(1);
        System.out.println("开始时间:"+startTime+",结束时间:"+endTime);
    }

}

2.根据时间段统计数据(时间是连续的)

VO类:

package com.rk.eai.basic.common.vo.project_ai_model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;


@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "AI模型事件折线图", description = "AI模型事件折线图")
public class AiModelEventGraphVO implements Serializable {
    @Getter
    @Setter
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    @ApiModel(value = "AI模型事件折线图单元", description = "AI模型事件折线图单元")
    public static class ModelEventGraphUnit implements Serializable {
        @ApiModelProperty(value = "时间")
        private LocalDateTime time;
        @ApiModelProperty(value = "事件数量")
        private int eventNum;
    }

    @ApiModelProperty(value = "事件监测数")
    private int total;
    @ApiModelProperty(value = "折线图")
    private List<ModelEventGraphUnit> data;
}

实现类

 // 项目模型折线图
    public AiModelEventGraphVO getProjectModelDayEvent(Long projectID,String sence, String modelID, TimeTypeDayToSeasonEnum timeType) {
        var startTimeAndEndTime = timeType.getStartTimeAndEndTime(LocalDateTime.now(Consts.CST));
        var durationHour = timeType.getDurationHour();
        var startTime = startTimeAndEndTime.get(0);
        var endTime = startTimeAndEndTime.get(1);
        List<Long> cdids = new ArrayList<>(1);
        // 查询AI事件
        var aiEventQw = new LambdaQueryWrapper<AiEvent>()
                .select(AiEvent::getId, AiEvent::getCreatedTime, AiEvent::getStatus, AiEvent::getUpdatedTime)
                .eq(AiEvent::getProjectId, projectID)
                .eq(AiEvent::getEventType,sence)
                .between(AiEvent::getCreatedTime, startTime.toEpochSecond(Consts.CST) * 1000, endTime.toEpochSecond(Consts.CST) * 1000);
        if(StringUtils.isNotEmpty(modelID)){
            if (modelID.contains(",")) {
                cdids = Arrays.asList(modelID.split(",")).parallelStream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            } else {
                cdids.add(Long.valueOf(modelID));
            }
            aiEventQw.in(AiEvent::getModelId, cdids);
        }

        var graph = new ArrayList<AiModelEventGraphVO.ModelEventGraphUnit>();
        int total = 0;


        try {
            var aiEventList = aiEventMapper.selectList(aiEventQw);
            var createTimes = aiEventList.stream()
                    .map(e -> LocalDateTime.ofEpochSecond(e.getCreatedTime() / 1000L, 0, Consts.CST))
                    .collect(Collectors.toList());
            while (startTime.isBefore(endTime)) {
                LocalDateTime finalStartTime = startTime;
                var count = createTimes.stream()
                        .filter(t -> t.isAfter(finalStartTime) && t.isBefore(finalStartTime.plus(durationHour, ChronoUnit.HOURS)))
                        .count();
                total += count;
                graph.add(new AiModelEventGraphVO.ModelEventGraphUnit().setTime(startTime).setEventNum((int) count));
                startTime = startTime.plus(durationHour, ChronoUnit.HOURS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AiModelEventGraphVO().setTotal(total).setData(graph);

    }

3.调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值