refactor: 删除Blog模块

This commit is contained in:
2025-06-10 23:29:25 +08:00
parent 1bc78e80b7
commit 43500ede69
107 changed files with 155 additions and 1535 deletions

View File

@@ -7,7 +7,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan(basePackages = {"com.cxx.common.mapper", "com.cxx.admin.mapper", "com.cxx.admin.dao"})
@EnableScheduling
public class AdminServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServiceApplication.class, args);

View File

@@ -1,58 +0,0 @@
package com.cxx.admin.controller;
import com.cxx.admin.service.BillManageService;
import com.cxx.common.WriteView;
import com.cxx.common.dto.bill.BillCategoryDto;
import com.cxx.common.dto.bill.BillItemDto;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/bill-manage")
@Tag(name = "账单管理")
public class BillManageController {
@Resource
private BillManageService billManageService;
@Operation(summary = "新增支付账户")
@PostMapping("/pay")
public Boolean addBillPay(@JsonView(WriteView.class) @RequestBody BillItemDto billItemDto) {
return billManageService.addBillPay(billItemDto);
}
@Operation(summary = "更新支付账户")
@PutMapping("/pay/{id}")
public Boolean updateBillPay(@PathVariable("id") Long id,
@JsonView(WriteView.class) @RequestBody BillItemDto billItemDto) {
return billManageService.updateBillPay(id, billItemDto);
}
@Operation(summary = "新增账本类型")
@PostMapping("/book")
public Boolean addBillBook(@JsonView(WriteView.class) @RequestBody BillItemDto billItemDto) {
return billManageService.addBillBook(billItemDto);
}
@Operation(summary = "更新账本类型")
@PutMapping("/book/{id}")
public Boolean updateBillBook(@PathVariable("id") Long id,
@JsonView(WriteView.class) @RequestBody BillItemDto billItemDto) {
return billManageService.updateBillBook(id, billItemDto);
}
@Operation(summary = "新增账单类型")
@PostMapping("/category")
public Boolean addBillCategory(@RequestBody BillCategoryDto billCategoryDto) {
return billManageService.addBillCategory(billCategoryDto);
}
@Operation(summary = "更新账单类型")
@PutMapping("/category/{id}")
public Boolean updateBillCategory(@PathVariable("id") Long id,
@RequestBody BillCategoryDto billCategoryDto) {
return billManageService.updateBillCategory(id, billCategoryDto);
}
}

View File

@@ -4,9 +4,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.admin.service.BlogManageService;
import com.cxx.common.ReadView;
import com.cxx.common.WriteView;
import com.cxx.common.dto.blog.BlogCategoryDto;
import com.cxx.common.dto.blog.BlogDto;
import com.cxx.common.dto.blog.BlogVisitDto;
import com.cxx.admin.dto.blog.BlogCategoryDto;
import com.cxx.admin.dto.blog.BlogDto;
import com.cxx.admin.dto.blog.BlogVisitDto;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

View File

@@ -1,27 +0,0 @@
package com.cxx.admin.controller;
import com.cxx.admin.service.BudgetManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/budget-manage")
@Tag(name = "预算管理")
public class BudgetManageController {
@Resource
private BudgetManageService budgetManageService;
@Operation(summary = "新增预算类别")
@PostMapping("/category")
public Boolean addBudgetCategory(@RequestParam String category) {
return budgetManageService.addBudgetCategory(category);
}
@Operation(summary = "更新预算类别")
@PutMapping("/category/{id}")
public Boolean updateBudgetCategory(@PathVariable("id") Long id, @RequestParam String category) {
return budgetManageService.updateBudgetCategory(id, category);
}
}

View File

@@ -1,30 +0,0 @@
package com.cxx.admin.controller;
import com.cxx.admin.dto.server.*;
import com.cxx.admin.service.ServerMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/server-monitor")
@Tag(name = "服务器监控")
public class ServerMonitorController {
@Resource
private ServerMonitorService serverMonitorService;
@Operation(summary = "系统信息")
@GetMapping("/system/info")
public SystemInfoDto getSystemInfo() {
return serverMonitorService.getSystemInfo();
}
@Operation(summary = "系统指标")
@GetMapping("/system/metrics")
public List<SystemMetricsDto> querySystemMetrics(@RequestParam String range) {
return serverMonitorService.querySystemMetrics(range);
}
}

View File

@@ -1,11 +1,10 @@
package com.cxx.admin.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.common.dto.blog.BlogCategoryDto;
import com.cxx.common.dto.blog.BlogDto;
import com.cxx.common.dto.blog.BlogVisitDto;
import com.cxx.common.entity.Blog;
import com.cxx.common.vo.blog.BlogQueryVo;
import com.cxx.admin.dto.blog.BlogCategoryDto;
import com.cxx.admin.dto.blog.BlogDto;
import com.cxx.admin.dto.blog.BlogVisitDto;
import com.cxx.admin.entity.Blog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

View File

@@ -0,0 +1,15 @@
package com.cxx.admin.dto.blog;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class BlogCategoryDto {
@Schema(description = "类别名称")
private String name;
@Schema(description = "类别数量")
private Integer count;
}

View File

@@ -0,0 +1 @@
package com.cxx.admin.dto.blog;

View File

@@ -0,0 +1,20 @@
package com.cxx.admin.dto.blog;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
/**
* @Author: Cxx
* @Date: 2024/9/16 19:47
* @Description:
*/
@Getter
@Setter
public class BlogLatestDto {
@Schema(description = "id")
private Long id;
@Schema(description = "博客标题")
private String title;
}

View File

@@ -0,0 +1,20 @@
package com.cxx.admin.dto.blog;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
/**
* @Author: Cxx
* @Date: 2024/9/16 19:47
* @Description:
*/
@Getter
@Setter
public class BlogStatsDto {
@Schema(description = "博客数量")
private Long blogCount;
@Schema(description = "博客类别数量")
private Long categoryCount;
}

View File

@@ -0,0 +1,31 @@
package com.cxx.admin.dto.blog;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter
public class BlogVisitDto {
@Schema(description = "访问ip")
private String ip;
@Schema(description = "访问系统")
private String os;
@Schema(description = "访问浏览器")
private String browser;
@Schema(description = "访问路径")
private String uri;
@Schema(description = "博客标题")
private String title;
@Schema(description = "访问时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date visitTime;
}

View File

@@ -0,0 +1,36 @@
package com.cxx.admin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cxx.framework.data.AbstractEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("blog")
public class Blog extends AbstractEntity {
@TableField("title")
private String title;
@TableField("top_value")
private Integer topValue;
@TableField("is_great")
private Boolean isGreat;
@TableField("category_id")
private Long categoryId;
@TableField("summary")
private String summary;
@TableField("content_id")
private Long contentId;
@TableField("word_count")
private Integer wordCount;
@TableField("read_duration")
private Double readDuration;
}

View File

@@ -0,0 +1,20 @@
package com.cxx.admin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cxx.framework.data.AbstractEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @Author: Cxx
* @Date: 2024/9/15 10:11
* @Description: 博客分类
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("blog_category")
public class BlogCategory extends AbstractEntity {
@TableField("name")
private String name;
}

View File

@@ -0,0 +1,20 @@
package com.cxx.admin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cxx.framework.data.AbstractIdEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @Author: Cxx
* @Date: 2024/9/15 10:09
* @Description: 博客内容
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("blog_content")
public class BlogContent extends AbstractIdEntity {
@TableField("content")
private String content;
}

View File

@@ -0,0 +1,27 @@
package com.cxx.admin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cxx.framework.data.AbstractEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("blog_visit")
public class BlogVisit extends AbstractEntity {
@TableField("ip")
private String ip;
@TableField("os")
private String os;
@TableField("browser")
private String browser;
@TableField("uri")
private String uri;
@TableField("blog_id")
private Long blogId;
}

View File

@@ -0,0 +1,24 @@
package com.cxx.admin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cxx.framework.data.AbstractEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("file_record")
public class FileRecord extends AbstractEntity {
@TableField("name")
private String name;
@TableField("md5")
private String md5;
@TableField("path")
private String path;
@TableField("url")
private String url;
}

View File

@@ -0,0 +1,9 @@
package com.cxx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxx.admin.entity.BlogCategory;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BlogCategoryMapper extends BaseMapper<BlogCategory> {
}

View File

@@ -0,0 +1,9 @@
package com.cxx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxx.admin.entity.BlogContent;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BlogContentMapper extends BaseMapper<BlogContent> {
}

View File

@@ -0,0 +1,9 @@
package com.cxx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxx.admin.entity.Blog;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BlogMapper extends BaseMapper<Blog> {
}

View File

@@ -0,0 +1,9 @@
package com.cxx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxx.admin.entity.BlogVisit;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BlogVisitMapper extends BaseMapper<BlogVisit> {
}

View File

@@ -0,0 +1,9 @@
package com.cxx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cxx.admin.entity.FileRecord;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface FileRecordMapper extends BaseMapper<FileRecord> {
}

View File

@@ -1,21 +0,0 @@
package com.cxx.admin.schedule;
import com.cxx.admin.service.ServerMonitorService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ServerMonitor {
@Resource
private ServerMonitorService serverMonitorService;
// @Scheduled(fixedRate = 60000)
public void collectAndStoreMetrics() {
log.info("开始存储服务器状态");
serverMonitorService.storeSystemMetrics();
log.info("结束存储服务器状态");
}
}

View File

@@ -1,18 +0,0 @@
package com.cxx.admin.service;
import com.cxx.common.dto.bill.BillCategoryDto;
import com.cxx.common.dto.bill.BillItemDto;
public interface BillManageService {
Boolean addBillPay(BillItemDto billItemDto);
Boolean updateBillPay(Long id, BillItemDto billItemDto);
Boolean addBillBook(BillItemDto billItemDto);
Boolean updateBillBook(Long id,BillItemDto billItemDto);
Boolean addBillCategory(BillCategoryDto billCategoryDto);
Boolean updateBillCategory(Long id, BillCategoryDto billCategoryDto);
}

View File

@@ -1,10 +1,9 @@
package com.cxx.admin.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.common.dto.blog.BlogCategoryDto;
import com.cxx.common.dto.blog.BlogDto;
import com.cxx.common.dto.blog.BlogVisitDto;
import com.cxx.common.vo.blog.BlogQueryVo;
import com.cxx.admin.dto.blog.BlogCategoryDto;
import com.cxx.admin.dto.blog.BlogDto;
import com.cxx.admin.dto.blog.BlogVisitDto;
import java.util.List;

View File

@@ -1,8 +0,0 @@
package com.cxx.admin.service;
public interface BudgetManageService {
Boolean addBudgetCategory(String category);
Boolean updateBudgetCategory(Long id, String category);
}

View File

@@ -1,19 +0,0 @@
package com.cxx.admin.service;
import com.cxx.admin.dto.server.*;
import java.util.List;
public interface ServerMonitorService {
CpuDto getCpuInfo();
MemoryDto getMemoryInfo();
DiskDto getDiskInfo();
SystemInfoDto getSystemInfo();
void storeSystemMetrics();
List<SystemMetricsDto> querySystemMetrics(String range) ;
}

View File

@@ -1,134 +0,0 @@
package com.cxx.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxx.admin.service.BillManageService;
import com.cxx.common.dto.bill.BillCategoryDto;
import com.cxx.common.dto.bill.BillItemDto;
import com.cxx.common.entity.BillBook;
import com.cxx.common.entity.BillCategory;
import com.cxx.common.entity.BillPay;
import com.cxx.common.mapper.BillBookMapper;
import com.cxx.common.mapper.BillCategoryMapper;
import com.cxx.common.mapper.BillPayMapper;
import com.cxx.framework.web.CustomException;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class BillManageServiceImpl implements BillManageService {
@Resource
private BillPayMapper billPayMapper;
@Resource
private BillBookMapper billBookMapper;
@Resource
private BillCategoryMapper billCategoryMapper;
@Override
public Boolean addBillPay(BillItemDto billItemDto) {
// 1. 校验是否存在
LambdaQueryWrapper<BillPay> queryWrapper = new LambdaQueryWrapper<>();
if (billPayMapper.exists(queryWrapper.eq(BillPay::getName, billItemDto.getName()))) {
throw new CustomException("该账户已经存在");
}
// 2. 新增账户
BillPay newbillPay = new BillPay();
BeanUtils.copyProperties(billItemDto, newbillPay);
return billPayMapper.insert(newbillPay) == 1;
}
@Override
public Boolean updateBillPay(Long id, BillItemDto billItemDto) {
// 1. 校验是否存在
if (Objects.isNull(billPayMapper.selectById(id))) {
throw new RuntimeException("该账户不存在");
}
// 2. 更新账户
BillPay billPay = new BillPay();
BeanUtils.copyProperties(billItemDto, billPay);
billPay.setId(id);
return billPayMapper.updateById(billPay) == 1;
}
@Override
public Boolean addBillBook(BillItemDto billItemDto) {
// 1. 校验是否存在
LambdaQueryWrapper<BillBook> queryWrapper = new LambdaQueryWrapper<>();
if (billBookMapper.exists(queryWrapper.eq(BillBook::getName, billItemDto.getName()))) {
throw new CustomException("该账本已经存在");
}
// 2. 新增账本
BillBook billBook = new BillBook();
BeanUtils.copyProperties(billItemDto, billBook);
return billBookMapper.insert(billBook) == 1;
}
@Override
public Boolean updateBillBook(Long id, BillItemDto billItemDto) {
// 1. 校验是否存在
if (Objects.isNull(billBookMapper.selectById(id))) {
throw new CustomException("该账单不存在");
}
// 2. 更新账本
BillBook billBook = new BillBook();
BeanUtils.copyProperties(billItemDto, billBook);
billBook.setId(id);
return billBookMapper.updateById(billBook) == 1;
}
private Long getBillBook(String name) {
LambdaQueryWrapper<BillBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BillBook::getName, name);
BillBook billBook = billBookMapper.selectOne(queryWrapper);
if (Objects.isNull(billBook)) {
throw new CustomException("该账本类型不存在");
}
return billBook.getId();
}
@Override
public Boolean addBillCategory(BillCategoryDto billCategoryDto) {
// 1. 校验是否存在
LambdaQueryWrapper<BillCategory> queryWrapper = new LambdaQueryWrapper<>();
if (billCategoryMapper.exists(queryWrapper.eq(BillCategory::getName, billCategoryDto.getName()))) {
throw new CustomException("该账单类型已经存在");
}
// 2. 新增账单类型
BillCategory newBillCategory = new BillCategory();
BeanUtils.copyProperties(billCategoryDto, newBillCategory);
newBillCategory.setBookId(getBillBook(billCategoryDto.getBookName()));
return billCategoryMapper.insert(newBillCategory) == 1;
}
@Override
public Boolean updateBillCategory(Long id, BillCategoryDto billCategoryDto) {
// 1. 校验是否存在
if (Objects.isNull(billCategoryMapper.selectById(id))) {
throw new CustomException("该账单类型不存在");
}
// 2. 更新账单类型
BillCategory billCategory = new BillCategory();
BeanUtils.copyProperties(billCategoryDto, billCategory);
billCategory.setBookId(getBillBook(billCategoryDto.getBookName()));
billCategory.setId(id);
return billCategoryMapper.updateById(billCategory) == 1;
}
}

View File

@@ -6,15 +6,15 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxx.admin.dao.BlogDao;
import com.cxx.admin.service.BlogManageService;
import com.cxx.admin.util.BlogUtils;
import com.cxx.common.dto.blog.BlogCategoryDto;
import com.cxx.common.dto.blog.BlogDto;
import com.cxx.common.dto.blog.BlogVisitDto;
import com.cxx.common.entity.Blog;
import com.cxx.common.entity.BlogCategory;
import com.cxx.common.entity.BlogContent;
import com.cxx.common.mapper.BlogCategoryMapper;
import com.cxx.common.mapper.BlogContentMapper;
import com.cxx.common.mapper.BlogMapper;
import com.cxx.admin.dto.blog.BlogCategoryDto;
import com.cxx.admin.dto.blog.BlogDto;
import com.cxx.admin.dto.blog.BlogVisitDto;
import com.cxx.admin.entity.Blog;
import com.cxx.admin.entity.BlogCategory;
import com.cxx.admin.entity.BlogContent;
import com.cxx.admin.mapper.BlogCategoryMapper;
import com.cxx.admin.mapper.BlogContentMapper;
import com.cxx.admin.mapper.BlogMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

View File

@@ -1,43 +0,0 @@
package com.cxx.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxx.admin.service.BudgetManageService;
import com.cxx.common.entity.BudgetCategory;
import com.cxx.common.mapper.BudgetCategoryMapper;
import com.cxx.framework.web.CustomException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class BudgetManageServiceImpl implements BudgetManageService {
@Resource
private BudgetCategoryMapper budgetCategoryMapper;
@Override
public Boolean addBudgetCategory(String category) {
LambdaQueryWrapper<BudgetCategory> queryWrapper = new LambdaQueryWrapper<>();
if (budgetCategoryMapper.exists(queryWrapper.eq(BudgetCategory::getName, category))) {
throw new CustomException("该预算类别已存在");
}
BudgetCategory budgetCategory = new BudgetCategory();
budgetCategory.setName(category);
return budgetCategoryMapper.insert(budgetCategory) == 1;
}
@Override
public Boolean updateBudgetCategory(Long id, String category) {
if (Objects.isNull(budgetCategoryMapper.selectById(id))) {
throw new CustomException("该预算类别不存在");
}
BudgetCategory budgetCategory = new BudgetCategory();
budgetCategory.setId(id);
budgetCategory.setName(category);
return budgetCategoryMapper.updateById(budgetCategory) == 1;
}
}

View File

@@ -10,8 +10,8 @@ import com.cxx.admin.service.FileService;
import com.cxx.admin.dto.file.FileRecordDto;
import com.cxx.admin.util.FileUtils;
import com.cxx.admin.util.UploadFileUtil;
import com.cxx.common.entity.FileRecord;
import com.cxx.common.mapper.FileRecordMapper;
import com.cxx.admin.entity.FileRecord;
import com.cxx.admin.mapper.FileRecordMapper;
import com.cxx.framework.web.CustomException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

View File

@@ -1,214 +0,0 @@
package com.cxx.admin.service.impl;
import com.cxx.admin.dto.server.*;
import com.cxx.admin.service.ServerMonitorService;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApi;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxTable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
import java.io.File;
import java.text.DecimalFormat;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service
public class ServerMonitorServiceImpl implements ServerMonitorService {
private final SystemInfo systemInfo = new SystemInfo();
private final HardwareAbstractionLayer hardware = systemInfo.getHardware();
private final OperatingSystem os = systemInfo.getOperatingSystem();
private final CentralProcessor processor = hardware.getProcessor();
private final InfluxDBClient client;
private final QueryApi queryApi;
private final String SYSTEM_METRICS = "system_metrics";
private final long GB = 1024 * 1024 * 1024;
private final DecimalFormat df = new DecimalFormat("0.00");
public ServerMonitorServiceImpl(
@Value("${influxdb.url}") String url,
@Value("${influxdb.token}") String token,
@Value("${influxdb.org}") String org,
@Value("${influxdb.bucket}") String bucket) {
this.client = InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket);
this.queryApi = this.client.getQueryApi();
}
@Override
public CpuDto getCpuInfo() {
CpuDto cpuDto = new CpuDto();
cpuDto.setUsagePercent(formatPercent(hardware.getProcessor().getSystemCpuLoad(500) * 100));
return cpuDto;
}
@Override
public MemoryDto getMemoryInfo() {
GlobalMemory memory = hardware.getMemory();
MemoryDto memoryDto = new MemoryDto();
long total = memory.getTotal();
long free = memory.getAvailable();
long used = total - free;
memoryDto.setTotal(bytesToGb(total));
memoryDto.setUsed(bytesToGb(used));
memoryDto.setFree(bytesToGb(free));
memoryDto.setUsagePercent(formatPercent((used * 100.0) / total));
return memoryDto;
}
@Override
public DiskDto getDiskInfo() {
List<HWDiskStore> disks = hardware.getDiskStores();
if (disks.isEmpty()) {
return null;
}
// 获取根分区信息
HWDiskStore rootDisk = disks.get(0);
DiskDto diskInfo = new DiskDto();
diskInfo.setTotal(bytesToGb(rootDisk.getSize()));
// 获取文件系统使用情况
os.getFileSystem().getFileStores().stream()
.filter(fs -> fs.getMount().equals(File.separator))
.findFirst()
.ifPresent(fs -> {
long total = fs.getTotalSpace();
long free = fs.getFreeSpace();
long used = total - free;
diskInfo.setUsed(bytesToGb(used));
diskInfo.setFree(bytesToGb(free));
diskInfo.setUsagePercent(formatPercent((double) (used) * 100.0 / total));
});
return diskInfo;
}
@Override
public SystemInfoDto getSystemInfo() {
SystemInfoDto dto = new SystemInfoDto();
// 获取CPU信息
dto.setCpuName(processor.getProcessorIdentifier().getName());
dto.setCpuVendor(processor.getProcessorIdentifier().getVendor());
dto.setCpuLogicalCores(processor.getLogicalProcessorCount());
dto.setCpuPhysicalCores(processor.getPhysicalProcessorCount());
// 获取CPU负载
double[] loadAverage = processor.getSystemLoadAverage(3);
dto.setCpuSystemLoad(loadAverage[0]); // 1分钟平均负载
// 获取操作系统信息
dto.setOsFamily(os.getFamily());
dto.setOsVersion(os.getVersionInfo().getVersion());
dto.setOsManufacturer(os.getManufacturer());
// 获取系统运行时间(秒)
dto.setSystemUptime(TimeUnit.MILLISECONDS.toSeconds(systemInfo.getOperatingSystem().getSystemUptime()));
return dto;
}
@Override
public void storeSystemMetrics() {
try (WriteApi writeApi = client.getWriteApi()) {
CpuDto cpuDto = getCpuInfo();
MemoryDto memoryDto = getMemoryInfo();
DiskDto diskDto = getDiskInfo();
Point point = Point
.measurement(SYSTEM_METRICS)
.addTag("host", "localhost")
.addField("cpu_usage", cpuDto.getUsagePercent())
.addField("memory_total", memoryDto.getTotal())
.addField("memory_used", memoryDto.getUsed())
.addField("memory_free", memoryDto.getFree())
.addField("memory_usage", memoryDto.getUsagePercent())
.addField("disk_total", diskDto.getTotal())
.addField("disk_used", diskDto.getUsed())
.addField("disk_free", diskDto.getFree())
.addField("disk_usage", diskDto.getUsagePercent())
.time(Instant.now(), WritePrecision.S);
writeApi.writePoint(point);
}
}
@Override
public List<SystemMetricsDto> querySystemMetrics(String range) {
String fluxQuery = "from(bucket: \"server_metrics\")" +
"|> range(start: -" + range + ")" + // range 可以是 "1h", "24h" 等
"|> filter(fn: (r) => r._measurement == \"system_metrics\")" +
"|> pivot(rowKey: [\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
List<Map<String, Object>> results = executeQuery(fluxQuery);
List<SystemMetricsDto> systemMetricsList = new ArrayList<>();
for (Map<String, Object> data : results) {
SystemMetricsDto systemMetrics = new SystemMetricsDto();
systemMetrics.setCpuUsage((Double) data.get("cpu_usage"));
systemMetrics.setMemoryTotal((Double) data.get("memory_total"));
systemMetrics.setMemoryUsed((Double) data.get("memory_used"));
systemMetrics.setMemoryFree((Double) data.get("memory_free"));
systemMetrics.setMemoryUsage((Double) data.get("memory_usage"));
systemMetrics.setDiskTotal((Double) data.get("disk_total"));
systemMetrics.setDiskUsed((Double) data.get("disk_used"));
systemMetrics.setDiskFree((Double) data.get("disk_free"));
systemMetrics.setDiskUsage((Double) data.get("disk_usage"));
systemMetrics.setTime(Date.from((Instant) data.get("time")));
systemMetricsList.add(systemMetrics);
}
return systemMetricsList;
}
private List<Map<String, Object>> executeQuery(String fluxQuery) {
List<FluxTable> tables = queryApi.query(fluxQuery);
List<Map<String, Object>> results = new ArrayList<>();
for (FluxTable table : tables) {
table.getRecords().forEach(record -> {
Map<String, Object> data = new HashMap<>();
data.put("time", record.getTime());
record.getValues().forEach((key, value) -> {
if (!key.startsWith("_")) { // 过滤掉内部字段
data.put(key, value);
}
});
results.add(data);
});
}
return results;
}
private double bytesToGb(long bytes) {
double gb = bytes / (double) GB;
return Double.parseDouble(df.format(gb));
}
public double formatPercent(double percent) {
return Double.parseDouble(df.format(percent));
}
}

View File

@@ -0,0 +1,23 @@
package com.cxx.admin.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
/**
* @Author: Cxx
* @Date: 2024/9/16 19:52
* @Description:
*/
@Getter
@Setter
public class BlogQueryVo {
@Schema(description = "博客类别")
private String category;
@Schema(description = "博客标题")
private String title;
@Schema(description = "博客年份")
private Integer year;
}

View File

@@ -3,7 +3,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/sweet_hut?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: estun@medical
password: 123456
file:
path: D:\\temp\\

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxx.admin.dao.BlogDao">
<select id="queryBlogByPage" resultType="com.cxx.common.dto.blog.BlogDto">
<select id="queryBlogByPage" resultType="com.cxx.admin.dto.blog.BlogDto">
SELECT a.id AS id,
a.title AS title,
a.top_value AS topValue,
@@ -21,7 +21,7 @@
a.update_time DESC
</select>
<select id="queryBlogById" resultType="com.cxx.common.dto.blog.BlogDto">
<select id="queryBlogById" resultType="com.cxx.admin.dto.blog.BlogDto">
SELECT a.id AS id,
a.title AS title,
a.top_value AS topValue,
@@ -41,7 +41,7 @@
WHERE a.id = #{id} LIMIT 1
</select>
<select id="queryBlogCategory" resultType="com.cxx.common.dto.blog.BlogCategoryDto">
<select id="queryBlogCategory" resultType="com.cxx.admin.dto.blog.BlogCategoryDto">
SELECT b.name AS name,
count(b.name) AS count
FROM blog a
@@ -50,7 +50,7 @@
GROUP BY b.name
</select>
<select id="queryBlogVisit" resultType="com.cxx.common.dto.blog.BlogVisitDto">
<select id="queryBlogVisit" resultType="com.cxx.admin.dto.blog.BlogVisitDto">
SELECT a.ip AS ip,
a.os AS os,
a.browser AS browser,