update 新增2miners 实时算力更新 定时任务
This commit is contained in:
@@ -96,4 +96,17 @@ public interface LeaseOrderItemMapper extends BaseMapper<LeaseOrderItem> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int updateSettleAmount(@Param("list") List<LeasePayRecordMessage> reocrdList);
|
int updateSettleAmount(@Param("list") List<LeasePayRecordMessage> reocrdList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取租约生效中的订单详情(status=1)
|
||||||
|
* @return 订单详情列表
|
||||||
|
*/
|
||||||
|
List<LeaseOrderItem> getActiveOrderItems();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新订单详情的实时算力
|
||||||
|
* @param list 订单详情列表
|
||||||
|
* @return 更新行数
|
||||||
|
*/
|
||||||
|
int updatePracticalPowerBatch(@Param("list") List<LeaseOrderItem> list);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,4 +95,14 @@ public interface LeaseOrderMiningMapper extends BaseMapper<LeaseOrderMining> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<PurchasedMachineDto> getPurchasedMachineInfo(@Param("info") MiningConfigInfoDto info,@Param("authId") Long authId);
|
List<PurchasedMachineDto> getPurchasedMachineInfo(@Param("info") MiningConfigInfoDto info,@Param("authId") Long authId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定时间点的实时算力
|
||||||
|
* @param tableName 表名(如:2miners)
|
||||||
|
* @param datetime 时间点
|
||||||
|
* @param list 查询条件列表
|
||||||
|
* @return 实时算力信息列表
|
||||||
|
*/
|
||||||
|
@MiningDB
|
||||||
|
List<RealHashrateInfoDto> getRealTimeHashrate(@Param("tableName") String tableName, @Param("datetime") String datetime, @Param("list") List<RealTimeHashrateQueryDto> list);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -903,7 +903,7 @@ public class LeaseOrderInfoServiceImpl extends ServiceImpl<LeaseOrderInfoMapper,
|
|||||||
.productMachineId(machineId)
|
.productMachineId(machineId)
|
||||||
.price(leaseMachinePrice.getPrice())
|
.price(leaseMachinePrice.getPrice())
|
||||||
.pool(orderMiningInfoDto.getPoolName())
|
.pool(orderMiningInfoDto.getPoolName())
|
||||||
.user(orderMiningInfoDto.getPoolUser())
|
.user(StringUtils.isNotEmpty(orderMiningInfoDto.getPoolUser()) ? orderMiningInfoDto.getPoolUser() : orderMiningInfoDto.getWalletAddress())
|
||||||
.miner(orderMiningInfoDto.getWorkerId())
|
.miner(orderMiningInfoDto.getWorkerId())
|
||||||
.coin(orderMiningInfoDto.getCoin())
|
.coin(orderMiningInfoDto.getCoin())
|
||||||
.algorithm(orderMiningInfoDto.getAlgorithm())
|
.algorithm(orderMiningInfoDto.getAlgorithm())
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ package com.m2pool.lease.task;
|
|||||||
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.m2pool.lease.dto.ProductMachineDto;
|
import com.m2pool.lease.dto.ProductMachineDto;
|
||||||
|
import com.m2pool.lease.dto.v2.RealHashrateInfoDto;
|
||||||
|
import com.m2pool.lease.dto.v2.RealTimeHashrateQueryDto;
|
||||||
|
import com.m2pool.lease.entity.LeaseOrderItem;
|
||||||
import com.m2pool.lease.entity.LeaseProductMachine;
|
import com.m2pool.lease.entity.LeaseProductMachine;
|
||||||
|
import com.m2pool.lease.mapper.LeaseOrderItemMapper;
|
||||||
|
import com.m2pool.lease.mapper.LeaseOrderMiningMapper;
|
||||||
import com.m2pool.lease.mapper.LeaseProductMachineMapper;
|
import com.m2pool.lease.mapper.LeaseProductMachineMapper;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
@@ -15,13 +19,17 @@ import org.springframework.scheduling.annotation.Scheduled;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description 矿机实时算力入库
|
* @Description 矿机实时算力入库
|
||||||
@@ -36,6 +44,12 @@ public class RealPowerInsetTask {
|
|||||||
@Resource
|
@Resource
|
||||||
private LeaseProductMachineMapper leaseProductMachineMapper;
|
private LeaseProductMachineMapper leaseProductMachineMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LeaseOrderItemMapper leaseOrderItemMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LeaseOrderMiningMapper leaseOrderMiningMapper;
|
||||||
|
|
||||||
private static final int BATCH_SIZE = 1000;
|
private static final int BATCH_SIZE = 1000;
|
||||||
private static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();
|
private static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();
|
||||||
private final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
private final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
||||||
@@ -90,7 +104,7 @@ public class RealPowerInsetTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
//@Scheduled(cron = "0 0/5 * * * ? ")
|
||||||
@Async("scheduledTaskExecutor")
|
@Async("scheduledTaskExecutor")
|
||||||
@DSTransactional
|
@DSTransactional
|
||||||
public void nexaRealPowerInset(){
|
public void nexaRealPowerInset(){
|
||||||
@@ -99,7 +113,7 @@ public class RealPowerInsetTask {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
//@Scheduled(cron = "0 0/5 * * * ? ")
|
||||||
@Async("scheduledTaskExecutor")
|
@Async("scheduledTaskExecutor")
|
||||||
@DSTransactional
|
@DSTransactional
|
||||||
public void monaRealPowerInset(){
|
public void monaRealPowerInset(){
|
||||||
@@ -107,7 +121,7 @@ public class RealPowerInsetTask {
|
|||||||
batchInsert("mona",monaPower);
|
batchInsert("mona",monaPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
//@Scheduled(cron = "0 0/5 * * * ? ")
|
||||||
@Async("scheduledTaskExecutor")
|
@Async("scheduledTaskExecutor")
|
||||||
@DSTransactional
|
@DSTransactional
|
||||||
public void rxdRealPowerInset(){
|
public void rxdRealPowerInset(){
|
||||||
@@ -115,7 +129,7 @@ public class RealPowerInsetTask {
|
|||||||
batchInsert("rxd",rxdPower);
|
batchInsert("rxd",rxdPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
//@Scheduled(cron = "0 0/5 * * * ? ")
|
||||||
@Async("scheduledTaskExecutor")
|
@Async("scheduledTaskExecutor")
|
||||||
@DSTransactional
|
@DSTransactional
|
||||||
public void grsRealPowerInset(){
|
public void grsRealPowerInset(){
|
||||||
@@ -123,4 +137,95 @@ public class RealPowerInsetTask {
|
|||||||
batchInsert("grs",rxdPower);
|
batchInsert("grs",rxdPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------v2版本实时算力------------------------------**/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 2miners实时算力更新任务
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "50 0/5 * * * ? ")
|
||||||
|
@Async("scheduledTaskExecutor")
|
||||||
|
@DSTransactional
|
||||||
|
public void RealPower2minersTask(){
|
||||||
|
try {
|
||||||
|
System.out.println("GPU实时算力更新任务开始执行:" + LocalDateTime.now());
|
||||||
|
//1.查找lease_order_item中所有状态status为1的订单详情
|
||||||
|
List<LeaseOrderItem> activeOrderItems = leaseOrderItemMapper.getActiveOrderItems();
|
||||||
|
if (activeOrderItems == null || activeOrderItems.isEmpty()) {
|
||||||
|
System.out.println("没有需要更新实时算力的订单详情");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println("查询到 " + activeOrderItems.size() + " 条需要更新实时算力的订单详情");
|
||||||
|
//2. 根据订单详情信息构建查询条件,去@MiningDB库中的矿池实时算力表查询数据
|
||||||
|
// 计算当前时间的前5分钟的整点时间(如:2026-01-28 01:05:50 -> 2026-01-28 01:05:00)
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
LocalDateTime queryTime = now.minusMinutes(5).withSecond(0).withNano(0);
|
||||||
|
String queryDateTime = queryTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
System.out.println("查询时间点:" + queryDateTime);
|
||||||
|
|
||||||
|
// 按矿池分组查询
|
||||||
|
Map<String, List<LeaseOrderItem>> poolGroups = activeOrderItems.stream()
|
||||||
|
.collect(Collectors.groupingBy(LeaseOrderItem::getPool));
|
||||||
|
|
||||||
|
List<LeaseOrderItem> updatedItems = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Map.Entry<String, List<LeaseOrderItem>> entry : poolGroups.entrySet()) {
|
||||||
|
String pool = entry.getKey();
|
||||||
|
List<LeaseOrderItem> items = entry.getValue();
|
||||||
|
|
||||||
|
// 构建查询条件列表
|
||||||
|
List<RealTimeHashrateQueryDto> queryList = items.stream()
|
||||||
|
.map(item -> RealTimeHashrateQueryDto.builder()
|
||||||
|
.orderItemId(item.getId())
|
||||||
|
.walletAddress(item.getUser())
|
||||||
|
.miner(item.getMiner())
|
||||||
|
.coin(item.getCoin())
|
||||||
|
.algorithm(item.getAlgorithm())
|
||||||
|
.pool(item.getPool())
|
||||||
|
.build())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
String tableName = "2miners"; //表名
|
||||||
|
// 查询实时算力
|
||||||
|
List<RealHashrateInfoDto> hashrateList = leaseOrderMiningMapper.getRealTimeHashrate(
|
||||||
|
tableName, queryDateTime, queryList);
|
||||||
|
|
||||||
|
System.out.println("矿池 " + pool + " 查询到 " + hashrateList.size() + " 条算力数据");
|
||||||
|
|
||||||
|
// 构建算力数据映射表:key = walletAddress + "_" + miner + "_" + coin + "_" + algorithm
|
||||||
|
Map<String, BigDecimal> hashrateMap = hashrateList.stream()
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
dto -> dto.getWalletAddress() + "_" + dto.getMiner() + "_" + dto.getCoin() + "_" + dto.getAlgorithm(),
|
||||||
|
RealHashrateInfoDto::getPower,
|
||||||
|
(v1, v2) -> v1 // 如果有重复,取第一个
|
||||||
|
));
|
||||||
|
|
||||||
|
// 更新订单详情的实时算力
|
||||||
|
for (LeaseOrderItem item : items) {
|
||||||
|
String key = item.getUser() + "_" + item.getMiner() + "_" + item.getCoin() + "_" + item.getAlgorithm();
|
||||||
|
BigDecimal power = hashrateMap.get(key);
|
||||||
|
|
||||||
|
// 如果查询到算力数据则使用,否则设置为0
|
||||||
|
item.setPracticalPower(power != null ? power : BigDecimal.ZERO);
|
||||||
|
updatedItems.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//3. 批量更新lease_order_item的practical_power实时算力字段
|
||||||
|
if (!updatedItems.isEmpty()) {
|
||||||
|
int updateCount = leaseOrderItemMapper.updatePracticalPowerBatch(updatedItems);
|
||||||
|
System.out.println("成功更新 " + updateCount + " 条订单详情的实时算力");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("GPU实时算力更新任务执行完成:" + LocalDateTime.now());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("GPU实时算力更新任务执行失败:" + e.getMessage());
|
||||||
|
throw new RuntimeException("GPU实时算力更新任务执行失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*---------------------v2版本实时算力------------------------------**/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,21 +102,39 @@
|
|||||||
#{id}
|
#{id}
|
||||||
</foreach>
|
</foreach>
|
||||||
</select>
|
</select>
|
||||||
<select id="getOneDayApartOrder" resultType="com.m2pool.lease.dto.v2.OrderTimeInfoDto">
|
<select id="getOneDayApartOrder" resultType="com.m2pool.lease.dto.v2.OrderTimeInfoDto">
|
||||||
|
select id,create_time as createTime,lease_time as leaseTime from lease_order_item where status = 1
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="getActiveOrderItems" resultType="com.m2pool.lease.entity.LeaseOrderItem">
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
machine_id AS machineId,
|
`user`,
|
||||||
order_id AS orderId,
|
miner,
|
||||||
order_item_id as orderItemId,
|
coin,
|
||||||
lease_time AS leaseTime,
|
algorithm,
|
||||||
create_time AS startTime,
|
pool,
|
||||||
price,
|
practical_power as practicalPower
|
||||||
numbers
|
FROM lease_order_item
|
||||||
FROM
|
WHERE status = 1 AND del = false
|
||||||
lease_order_item
|
|
||||||
WHERE
|
|
||||||
STATUS = 1
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<update id="updatePracticalPowerBatch">
|
||||||
|
UPDATE lease_order_item
|
||||||
|
SET practical_power = CASE
|
||||||
|
<foreach collection="list" item="item">
|
||||||
|
WHEN id = #{item.id}
|
||||||
|
THEN #{item.practicalPower}
|
||||||
|
</foreach>
|
||||||
|
ELSE practical_power
|
||||||
|
END
|
||||||
|
WHERE id IN (
|
||||||
|
<foreach collection="list" item="item" separator=",">
|
||||||
|
#{item.id}
|
||||||
|
</foreach>
|
||||||
|
)
|
||||||
|
</update>
|
||||||
|
|
||||||
<select id="checkShopExistRunningOrderNumbers" resultType="java.lang.Long">
|
<select id="checkShopExistRunningOrderNumbers" resultType="java.lang.Long">
|
||||||
SELECT COUNT(*)
|
SELECT COUNT(*)
|
||||||
FROM lease_order_item
|
FROM lease_order_item
|
||||||
|
|||||||
@@ -137,6 +137,24 @@
|
|||||||
</foreach>
|
</foreach>
|
||||||
)
|
)
|
||||||
</select>
|
</select>
|
||||||
|
<select id="getRealTimeHashrate" resultType="com.m2pool.lease.dto.v2.RealHashrateInfoDto">
|
||||||
|
SELECT
|
||||||
|
hashrate AS power,
|
||||||
|
pool_name AS pool,
|
||||||
|
wallet AS walletAddress,
|
||||||
|
miner AS miner,
|
||||||
|
algorithm,
|
||||||
|
coin
|
||||||
|
FROM
|
||||||
|
`${tableName}`
|
||||||
|
WHERE
|
||||||
|
datetime = #{datetime}
|
||||||
|
AND (wallet, coin, miner, algorithm) IN (
|
||||||
|
<foreach collection="list" item="item" separator=",">
|
||||||
|
(#{item.walletAddress}, #{item.coin}, #{item.miner}, #{item.algorithm})
|
||||||
|
</foreach>
|
||||||
|
)
|
||||||
|
</select>
|
||||||
<select id="getMiningInfoByOrderId" resultType="com.m2pool.lease.dto.v2.PurchasedMachineListDto">
|
<select id="getMiningInfoByOrderId" resultType="com.m2pool.lease.dto.v2.PurchasedMachineListDto">
|
||||||
select
|
select
|
||||||
id ,
|
id ,
|
||||||
|
|||||||
Reference in New Issue
Block a user