@icgio/icg-utils 1.9.64 → 1.9.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/influxdb.js +4 -4
- package/lib/utils.js +17 -0
- package/package.json +1 -1
package/lib/influxdb.js
CHANGED
|
@@ -28,10 +28,6 @@ class Influx {
|
|
|
28
28
|
}
|
|
29
29
|
async query(query_string, cb) {
|
|
30
30
|
const query_api = this.influx_db.getQueryApi(this.org)
|
|
31
|
-
let result = await query_api.queryRaw(query_string)
|
|
32
|
-
// Split by empty lines into table blocks — each InfluxDB table may have
|
|
33
|
-
// different columns (different tag sets), so parse each with its own headers
|
|
34
|
-
const tables = result.split(/\r?\n\s*\r?\n/).filter((b) => b.trim())
|
|
35
31
|
const parse_table = (csv) =>
|
|
36
32
|
new Promise((resolve, reject) => {
|
|
37
33
|
parse(csv, { columns: true, comment: '#', skip_empty_lines: true, relax_column_count: true }, (err, res) => {
|
|
@@ -40,6 +36,10 @@ class Influx {
|
|
|
40
36
|
})
|
|
41
37
|
})
|
|
42
38
|
try {
|
|
39
|
+
let result = await query_api.queryRaw(query_string)
|
|
40
|
+
// Split by empty lines into table blocks — each InfluxDB table may have
|
|
41
|
+
// different columns (different tag sets), so parse each with its own headers
|
|
42
|
+
const tables = result.split(/\r?\n\s*\r?\n/).filter((b) => b.trim())
|
|
43
43
|
const results = await Promise.all(tables.map((t) => parse_table(t)))
|
|
44
44
|
cb({ success: true, body: results.flat() })
|
|
45
45
|
} catch (err) {
|
package/lib/utils.js
CHANGED
|
@@ -641,6 +641,22 @@ function number_with_commas(x) {
|
|
|
641
641
|
return parts.join('.')
|
|
642
642
|
}
|
|
643
643
|
|
|
644
|
+
// Format a number for display with magnitude-aware precision: show roughly `sig_figs`
|
|
645
|
+
// significant figures, but always between `min_decimals` and `max_decimals` decimal places,
|
|
646
|
+
// with trailing zeros trimmed back down to `min_decimals`. Optionally group the integer part
|
|
647
|
+
// with commas. Keeps small amounts (e.g. 0.0000952) visible while sparing large amounts
|
|
648
|
+
// (e.g. 985,864,704.82) a tail of meaningless digits.
|
|
649
|
+
function format_amount(value, { sig_figs = 6, min_decimals = 2, max_decimals = 8, commas = true } = {}) {
|
|
650
|
+
let num = typeof value === 'string' ? parseFloat(value) : value
|
|
651
|
+
if (!Number.isFinite(num)) return String(value)
|
|
652
|
+
let decimals = _.clamp(sig_figs - 1 - Math.floor(Math.log10(Math.abs(num))), min_decimals, max_decimals)
|
|
653
|
+
let str = num.toFixed(decimals)
|
|
654
|
+
if (str.includes('.')) {
|
|
655
|
+
str = str.replace(new RegExp(`(\\.\\d{${min_decimals}}\\d*?)0+$`), '$1').replace(/\.$/, '')
|
|
656
|
+
}
|
|
657
|
+
return commas ? number_with_commas(str) : str
|
|
658
|
+
}
|
|
659
|
+
|
|
644
660
|
// =============================================================================
|
|
645
661
|
// Exchange Key Utilities
|
|
646
662
|
//
|
|
@@ -897,6 +913,7 @@ module.exports = {
|
|
|
897
913
|
reverse_pair,
|
|
898
914
|
cur_dict_to_pair_list,
|
|
899
915
|
number_with_commas,
|
|
916
|
+
format_amount,
|
|
900
917
|
normalize_exchange_key,
|
|
901
918
|
is_exchange_account_key,
|
|
902
919
|
parse_exchange_account_key,
|