@trap_stevo/filetide
Version:
Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities wi
188 lines (187 loc) • 5.14 kB
JavaScript
function getNestedData(obj, path) {
return path.split(".").reduce((acc, part) => acc?.[part], obj);
}
;
function pickData(data, keys) {
if (!keys) {
return data;
}
return keys.reduce((acc, key) => {
if (data[key] !== undefined) {
acc[key] = data[key];
}
return acc;
}, {});
}
;
function parseSizeString(str) {
if (typeof str === "number") {
return str;
}
if (typeof str !== "string") {
return 0;
}
const units = {
B: 1,
KB: 1024,
MB: 1024 ** 2,
GB: 1024 ** 3,
TB: 1024 ** 4
};
const match = str.trim().toUpperCase().match(/^([\d.]+)\s*(B|KB|MB|GB|TB)$/);
if (!match) {
return 0;
}
const [, numStr, unit] = match;
return parseFloat(numStr) * units[unit];
}
;
function aggregateMultiAverageChartData(events, {
groupBy = ["user"],
valueKey = "metadata.fileSize",
timestampKey = "timestamp",
metadataFields = null,
previewLimit = 3
} = {}) {
const buckets = {};
for (const event of events) {
const labelParts = groupBy.map(path => {
if (path === "time") {
return event[timestampKey];
}
if (path === "user") {
return event.metadata?.clientID || event.metadata?.senderID || "unknown";
}
if (path === "type") {
return event.type || "unknown";
}
return getNestedData(event, path) ?? "unknown";
});
const label = labelParts.join(" | ");
let rawValue = getNestedData(event, valueKey);
let value;
if (mode === "sum") {
if (valueKey.includes("speed") || valueKey.includes("fileSize")) {
value = parseSizeString(rawValue);
} else {
value = parseFloat(rawValue) || 0;
}
} else {
value = 1;
}
if (typeof value !== "number") {
continue;
}
if (!buckets[label]) {
buckets[label] = {
previewFileNames: [],
previewMetadata: [],
timestamps: [],
values: [],
total: 0,
count: 0
};
}
const bucket = buckets[label];
bucket.total += value;
bucket.count += 1;
bucket.values.push(value);
bucket.timestamps.push(event[timestampKey]);
const fileName = getNestedData(event, "metadata.fileName");
if (fileName && bucket.previewFileNames.length < previewLimit) {
bucket.previewFileNames.push(fileName);
}
if (bucket.previewMetadata.length < previewLimit) {
bucket.previewMetadata.push(pickData(event.metadata || {}, metadataFields));
}
}
return Object.entries(buckets).map(([label, bucket]) => ({
firstTimestamp: Math.min(...bucket.timestamps),
lastTimestamp: Math.max(...bucket.timestamps),
previewFileNames: bucket.previewFileNames,
previewMetadata: bucket.previewMetadata,
minValue: Math.min(...bucket.values),
maxValue: Math.max(...bucket.values),
average: +(total / count).toFixed(2),
total: bucket.total,
count: bucket.count,
label
}));
}
;
function aggregateChartData(events, {
groupBy = "type",
typeKey = "type",
userKey = "metadata.clientID",
timestampKey = "timestamp",
valueKey = "value",
mode = "count",
metadataFields = null,
previewLimit = 3
} = {}) {
const buckets = {};
for (const event of events) {
let label;
if (groupBy === "time") {
label = event[timestampKey];
} else if (groupBy === "user") {
label = getNestedData(event, userKey) || "unknown";
} else {
label = event[typeKey] || "other";
}
const type = event[typeKey] || "other";
let rawValue = mode === "sum" ? getNestedData(event, valueKey) || 0 : 1;
let value;
if (mode === "sum") {
if (valueKey.includes("speed") || valueKey.includes("fileSize")) {
value = parseSizeString(rawValue);
} else {
value = parseFloat(rawValue) || 0;
}
} else {
value = 1;
}
if (!buckets[type]) {
buckets[type] = {
previewFileNames: [],
previewMetadata: [],
timestamps: [],
values: [],
count: 0,
total: 0,
type
};
}
const bucket = buckets[type];
bucket.total += value;
bucket.count += 1;
bucket.values.push(value);
bucket.timestamps.push(event[timestampKey]);
const fileName = getNestedData(event, "metadata.fileName");
if (fileName && bucket.previewFileNames.length < previewLimit) {
bucket.previewFileNames.push(fileName);
}
if (bucket.previewMetadata.length < previewLimit) {
bucket.previewMetadata.push(pickData(event.metadata || {}, metadataFields));
}
}
return Object.entries(buckets).map(([type, bucket]) => ({
average: +(bucket.total / bucket.count).toFixed(2),
firstTimestamp: Math.min(...bucket.timestamps),
lastTimestamp: Math.max(...bucket.timestamps),
previewFileNames: bucket.previewFileNames,
previewMetadata: bucket.previewMetadata,
minValue: Math.min(...bucket.values),
maxValue: Math.max(...bucket.values),
total: bucket.total,
count: bucket.count,
label: type,
type
}));
}
module.exports = {
aggregateMultiAverageChartData,
aggregateChartData,
getNestedData
};
;