绑定图表的时候使用的是ECharts插件,此示例只有后端代码,绑定的是折线图,类似下图
代码是做项目的时候写的,翻看代码的时候突然看到,想记录一下
众所周知,这种情况最重要的就是写对sql语句,所以我们把建表语句和xml贴上来
DDL
create table cap_api_access_log
(
api_access_log_id bigint auto_incrementcomment '日志id' primary key,
api_service_id bigint not null comment '访问服务id',
api_access_name varchar(36) not null comment '访问示例名,直接填入uuid即可',
api_result_status varchar(50) null comment '执行结果状态码',
api_result_info text null comment '执行结果信息',
api_param_info text null comment '参数信息',
access_addr varchar(50) not null comment '访问地址',
access_start_time timestamp default CURRENT_TIMESTAMP not null comment '访问开始时间',
response_time int null comment '响应时间',
api_release_version varchar(36) null, comment '版本号'
authorize_app_name varchar(100) null comment '被授权应用名'
);
mapper.xml
api_access_log_id,api_service_id,api_access_name,api_result_status,api_result_info,api_param_info,access_addr,access_start_time,response_time,api_release_version,authorize_app_name
sql写好了,并且没有问题,但是我们绑定图表的时候有问题啊
比如sql查询近一小时数据的时候,10分钟的时候有数据,20分钟这个时间段没有数据,那么20分钟这个坐标就不会显示,所以我在java代码中处理了这种情况
以下为涉及到的实体类和枚举类(@Data为Lombok插件注解,代替了get,set等方法)
package com.hiynn.provider.dto;
import lombok.Data;
/**
* @author lidai
* @date 2019/6/12 11:08
*/
@Data
public class AccessData {
/**
* 访问时间
*/
private String times;
/**
* 访问次数
*/
private Integer num;
}
package com.hiynn.provider.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
/**
* @author lidai
* @date 2019/6/11 14:05
*/
@Data
@AllArgsConstructor
public class ECharts {
/**
* 横轴
*/
private Horizontal horizontal;
/**
* 数据
*/
private List
package com.hiynn.provider.entity;
import com.hiynn.provider.enums.HorizontalUnit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author lidai
* @date 2019/6/11 14:10
*
* 横轴
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Horizontal {
/**
* 横轴数据
*/
private List names;
/**
* 横轴单位
*/
private HorizontalUnit unit;
}
package com.hiynn.provider.enums;
/**
* @author lidai
* @date 2019/6/11 14:13
*/
public enum HorizontalUnit {
MINUTE(1, "MINUTE"),
HOUR(2, "HOUR"),
DAY(3, "DAY");
private int name;
private String value;
HorizontalUnit(int name, String value) {
this.name = name;
this.value = value;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
dao层 Mapper.java
package com.hiynn.provider.dao;
import com.hiynn.provider.dto.AccessData;
import com.hiynn.provider.dto.ApiAccessLogDto;
import com.hiynn.provider.entity.ApiAccessLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author lidai
* @date 2019/1/25 10:37
*/
@Mapper
public interface ApiAccessLogMapper {
/**
* 获取近一小时的访问量,每十分钟为一组
*/
List getCountByHour(@Param("apiReleaseVersion") String apiReleaseVersion, @Param("apiServiceId") Integer apiServiceId, @Param("isError") Boolean isError);
/**
* 获取近一天的访问量,每一个小时为一组
*/
List getCountByDay(@Param("apiReleaseVersion") String apiReleaseVersion, @Param("apiServiceId") Integer apiServiceId, @Param("isError") Boolean isError);
/**
* 获取近一周的访问量,每天为一组
*/
List getCountByWeek(@Param("apiReleaseVersion") String apiReleaseVersion, @Param("apiServiceId") Integer apiServiceId, @Param("isError") Boolean isError);
}
service
package com.hiynn.provider.service.impl;
import com.hiynn.common.entity.Result;
import com.hiynn.provider.dao.ApiAccessLogMapper;
import com.hiynn.provider.dto.AccessData;
import com.hiynn.provider.dto.ApiAccessLogDto;
import com.hiynn.provider.entity.ECharts;
import com.hiynn.provider.entity.Horizontal;
import com.hiynn.provider.enums.HorizontalUnit;
import com.hiynn.provider.service.ApiAccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lidai
* @date 2019/1/25 10:54
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiAccessLogServiceImpl implements ApiAccessLogService {
@Autowired
private ApiAccessLogMapper apiAccessLogMapper;
/**
* 获取近一小时的访问量
*
* @param apiReleaseVersion
* @return
*/
public Result getCountByHour(String apiReleaseVersion, Integer apiServiceId, Boolean isError) {
List horizontalNames = new ArrayList<>();
List accessDatas = apiAccessLogMapper.getCountByHour(apiReleaseVersion, apiServiceId, isError);
//获取当前时间和前一小时的时间
LocalDateTime oldHourTime = LocalDateTime.now().minusHours(1);
LocalDateTime currentTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00");
while (oldHourTime.isBefore(currentTime) || oldHourTime.equals(currentTime)) {
horizontalNames.add(oldHourTime.format(formatter));
oldHourTime = oldHourTime.plusMinutes(10);
}
Map map = accessData2Map(accessDatas);
List
controller
package com.hiynn.provider.controller;
import com.hiynn.common.entity.Result;
import com.hiynn.provider.service.ApiAccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author lidai
* @date 2019/1/25 10:46
*/
@RestController
@RequestMapping("/accessLog")
public class ApiAccessLogController {
@Autowired
private ApiAccessLogService apiAccessLogService;
@GetMapping("/accessCount")
public Result getCountBySelective(@RequestParam("apiReleaseVersion") String apiReleaseVersion,
@RequestParam("type") Integer type,
@RequestParam("apiServiceId") Integer apiServiceId,
@RequestParam("isError") Boolean isError) {
try {
return apiAccessLogService.getCountBySelective(apiReleaseVersion, type, apiServiceId, isError);
} catch (Exception e) {
e.printStackTrace();
return Result.build().fail(e.getMessage());
}
}
}
获取近一小时数据的结果如下图
如上图所示,只有最近10分钟有数据,horizontal代码横轴坐标,unit为单位,data对应横轴坐标的数据