m2pool_backend_app/public/score.js

49 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 计算标准差
function caculate_standar_deviation(data) {
// 计算数学期望(均值)
const calculateMean = (values) => {
const total = values.reduce((acc, value) => acc + parseFloat(value), 0);
return total / values.length;
};
// 计算标准差
const calculateStandardDeviation = (values, mean) => {
const variance = values.reduce((acc, value) => acc + Math.pow(parseFloat(value) - mean, 2), 0) / values.length;
return Math.sqrt(variance);
};
// 计算每个用户的标准差
const results = Object.keys(data).reduce((acc, user) => {
const values = data[user];
const mean = calculateMean(values);
const stddev = calculateStandardDeviation(values, mean);
acc[user] = stddev;
return acc;
}, {});
return results;
}
/**
* 计算每个用户最终得分满分为100分
* @param {Array} alluser_mhs24h 每个用户过去24小时平均算力
* @param {Number} hash_percent 24小时平均算力权重
* @returns 每个用户最终得分
*/
function score(alluser_mhs24h, hash_percent = 1) {
// 提取 mhs24h 数值
const hashrateValues = alluser_mhs24h.map((obj) => obj.mhs24h);
// 计算总和
const totalHashrate = hashrateValues.reduce((sum, value) => sum + value, 0);
const result = {};
// 计算每个用户的算力占比
for (let { user, mhs24h } of alluser_mhs24h) {
result[user] = (mhs24h / totalHashrate) * hash_percent;
}
return result;
}
module.exports = { caculate_standar_deviation, score };