底层流量数据改为bit,测试金山云服务器API,准备1.1分支

This commit is contained in:
gaoyutao
2025-10-09 17:38:24 +08:00
parent ab578ee17f
commit 7b5c782079
16 changed files with 211 additions and 655 deletions
@@ -29,7 +29,7 @@ public class CalculateUtil {
// 处理纯数字(单位为空)
if (unit.isEmpty()) {
unit = "B/S"; // 默认单位: Bytes/s
unit = "B/S"; // 默认单位: Bit/s
}
switch (mode) {
case "1000":
@@ -41,88 +41,88 @@ public class CalculateUtil {
}
}
// 1000进制换算(最小单位为bit)
// private static BigDecimal convertWithDecimalCoefficients(BigDecimal value, String unit) {
// switch (unit) {
// case "B/S": // bit per second
// return value.divide(new BigDecimal("1000000000"), 2, RoundingMode.HALF_UP); // 10^9
// case "KB/S": // Kilobit per second
// return value.divide(new BigDecimal("1000000"), 2, RoundingMode.HALF_UP); // 10^6
// case "MB/S": // Megabit per second
// return value.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP); // 10^3
// case "GB/S": // Gigabit per second
// return value.setScale(2, RoundingMode.HALF_UP); // 10^0
// case "TB/S": // Terabit per second
// return value.multiply(new BigDecimal("1000")) // 10^3
// .setScale(2, RoundingMode.HALF_UP);
// default:
// throw new IllegalArgumentException("Unsupported DECIMAL unit: " + unit);
// }
// }
// // 1024进制换算(最小单位为bit)
// private static BigDecimal convertWithBinaryCoefficients(BigDecimal value, String unit) {
// BigDecimal base1024 = new BigDecimal(1024);
// BigDecimal base1048576 = base1024.multiply(base1024); // 1024^2 = 1048576
// BigDecimal base1073741824 = base1048576.multiply(base1024); // 1024^3 = 1073741824
//
// switch (unit) {
// case "B/S": // bit per second
// return value.divide(base1073741824, 2, RoundingMode.HALF_UP); // 1024^3
// case "KB/S": // Kilobit per second
// return value.divide(base1048576, 2, RoundingMode.HALF_UP); // 1024^2
// case "MB/S": // Megabit per second
// return value.divide(base1024, 2, RoundingMode.HALF_UP); // 1024^1
// case "GB/S": // Gigabit per second
// return value.setScale(2, RoundingMode.HALF_UP); // 1024^0
// case "TB/S": // Terabit per second
// return value.multiply(base1024) // 1024^1
// .setScale(2, RoundingMode.HALF_UP);
// default:
// throw new IllegalArgumentException("Unsupported BINARY unit: " + unit);
// }
// }
// 字节1000进制换算
private static BigDecimal convertWithDecimalCoefficients(BigDecimal value, String unit) {
switch (unit.toUpperCase()) {
case "B/S":
return value.multiply(new BigDecimal("0.000008")) // 8/1000000
.setScale(2, RoundingMode.HALF_UP);
case "KB/S":
return value.multiply(new BigDecimal("0.008")) // 8/1000
.setScale(2, RoundingMode.HALF_UP);
case "MB/S":
return value.setScale(2, RoundingMode.HALF_UP);
case "GB/S":
return value.multiply(new BigDecimal("8000")) // 1000 * 8
.setScale(2, RoundingMode.HALF_UP);
case "TB/S":
return value.multiply(new BigDecimal("8000000")) // 1000 * 1000 * 8
switch (unit) {
case "B/S": // bit per second
return value.divide(new BigDecimal("1000000000"), 2, RoundingMode.HALF_UP); // 10^9
case "KB/S": // Kilobit per second
return value.divide(new BigDecimal("1000000"), 2, RoundingMode.HALF_UP); // 10^6
case "MB/S": // Megabit per second
return value.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP); // 10^3
case "GB/S": // Gigabit per second
return value.setScale(2, RoundingMode.HALF_UP); // 10^0
case "TB/S": // Terabit per second
return value.multiply(new BigDecimal("1000")) // 10^3
.setScale(2, RoundingMode.HALF_UP);
default:
throw new IllegalArgumentException("Unsupported DECIMAL unit: " + unit);
}
}
// 字节1024进制换算
// 1024进制换算(最小单位为bit)
private static BigDecimal convertWithBinaryCoefficients(BigDecimal value, String unit) {
switch (unit.toUpperCase()) {
case "B/S":
return value.multiply(new BigDecimal("0.00000762939")) // 8/1048576
.setScale(2, RoundingMode.HALF_UP);
case "KB/S":
return value.multiply(new BigDecimal("0.0078125")) // 8/1024
.setScale(2, RoundingMode.HALF_UP);
case "MB/S":
return value.setScale(2, RoundingMode.HALF_UP);
case "GB/S":
return value.multiply(new BigDecimal("8192")) // 1024 * 8
.setScale(2, RoundingMode.HALF_UP);
case "TB/S":
return value.multiply(new BigDecimal("8388608")) // 1024 * 1024 * 8
BigDecimal base1024 = new BigDecimal(1024);
BigDecimal base1048576 = base1024.multiply(base1024); // 1024^2 = 1048576
BigDecimal base1073741824 = base1048576.multiply(base1024); // 1024^3 = 1073741824
switch (unit) {
case "B/S": // bit per second
return value.divide(base1073741824, 2, RoundingMode.HALF_UP); // 1024^3
case "KB/S": // Kilobit per second
return value.divide(base1048576, 2, RoundingMode.HALF_UP); // 1024^2
case "MB/S": // Megabit per second
return value.divide(base1024, 2, RoundingMode.HALF_UP); // 1024^1
case "GB/S": // Gigabit per second
return value.setScale(2, RoundingMode.HALF_UP); // 1024^0
case "TB/S": // Terabit per second
return value.multiply(base1024) // 1024^1
.setScale(2, RoundingMode.HALF_UP);
default:
throw new IllegalArgumentException("Unsupported BINARY unit: " + unit);
}
}
// 字节1000进制换算
// private static BigDecimal convertWithDecimalCoefficients(BigDecimal value, String unit) {
// switch (unit.toUpperCase()) {
// case "B/S":
// return value.multiply(new BigDecimal("0.000008")) // 8/1000000
// .setScale(2, RoundingMode.HALF_UP);
// case "KB/S":
// return value.multiply(new BigDecimal("0.008")) // 8/1000
// .setScale(2, RoundingMode.HALF_UP);
// case "MB/S":
// return value.setScale(2, RoundingMode.HALF_UP);
// case "GB/S":
// return value.multiply(new BigDecimal("8000")) // 1000 * 8
// .setScale(2, RoundingMode.HALF_UP);
// case "TB/S":
// return value.multiply(new BigDecimal("8000000")) // 1000 * 1000 * 8
// .setScale(2, RoundingMode.HALF_UP);
// default:
// throw new IllegalArgumentException("Unsupported DECIMAL unit: " + unit);
// }
// }
//
// // 字节1024进制换算
// private static BigDecimal convertWithBinaryCoefficients(BigDecimal value, String unit) {
// switch (unit.toUpperCase()) {
// case "B/S":
// return value.multiply(new BigDecimal("0.00000762939")) // 8/1048576
// .setScale(2, RoundingMode.HALF_UP);
// case "KB/S":
// return value.multiply(new BigDecimal("0.0078125")) // 8/1024
// .setScale(2, RoundingMode.HALF_UP);
// case "MB/S":
// return value.setScale(2, RoundingMode.HALF_UP);
// case "GB/S":
// return value.multiply(new BigDecimal("8192")) // 1024 * 8
// .setScale(2, RoundingMode.HALF_UP);
// case "TB/S":
// return value.multiply(new BigDecimal("8388608")) // 1024 * 1024 * 8
// .setScale(2, RoundingMode.HALF_UP);
// default:
// throw new IllegalArgumentException("Unsupported BINARY unit: " + unit);
// }
// }
/**
* 计算百分位值
@@ -0,0 +1,91 @@
package com.ruoyi.system.util;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
public class HmacUtils {
private static final String MAC_NAME = "HmacSHA1";
private static final String ENCODING = "UTF-8";
/**
*@param encryptKey 秘钥由中控系统分配
*@param plainText 加密内容:token + 时间戳 + URI
*/
public static String getHmac(String plainText,String encryptKey) {
try{
byte[] dataKey=encryptKey.getBytes(ENCODING);
byte[] dataValue=plainText.getBytes(ENCODING);
//根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
SecretKey secretKey = new SecretKeySpec(dataKey, MAC_NAME);
// 2、创建MAC对象
Mac mac = Mac.getInstance(MAC_NAME);
// 3、设置密钥
mac.init(secretKey);
// 4、数据加密
byte[] bytes = mac.doFinal(dataValue);
// 5、生成数据
String rs = encodeHex(bytes,false);
return rs;
}catch (Exception e){
throw new IllegalArgumentException(e.getMessage());
}
}
// 数据转16进制编码
public static String encodeHex(final byte[] data, final boolean toLowerCase) {
final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
final char[] toDigits = toLowerCase ? DIGITS_LOWER : DIGITS_UPPER;
final int l = data.length;
final char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return new String(out);
}
public static void main(String[] args) {
long timestamp = DateUtils.getNowDate().getTime();
String plainText = "efea5f0218c84a24b9fdab3264de3da5" + timestamp + "/supplier/outer/dev/getFlow";
String secretKey = getHmac(plainText, "91a73fd806ab2c005c13b4dc19130a884e909dea3f72d46e30266fe1a1f588d8");
System.out.println(secretKey);
RestTemplate restTemplate = new RestTemplate();
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("TOKEN", "efea5f0218c84a24b9fdab3264de3da5");
headers.set("secret-key", secretKey);
headers.set("timestamps", String.valueOf(timestamp));
// 使用 UriComponentsBuilder 构建URL(推荐)
URI uri = UriComponentsBuilder
.fromHttpUrl("https://ecscm-openapi.ksyun.com/supplier/outer/dev/getFlow")
.queryParam("srmChannel", "1000121954")
.queryParam("startTime", "2025-10-09 00:00:00")
.queryParam("endTime", "2025-10-09 01:00:00")
.build()
.toUri();
HttpEntity<String> entity = new HttpEntity<>(headers);
// GET 请求
ResponseEntity<String> response = restTemplate.exchange(
uri,
HttpMethod.GET,
entity,
String.class
);
System.out.println(response.getBody());
}
}