@icgio/icg-utils 1.7.25 → 1.7.27

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 CHANGED
@@ -1,11 +1,12 @@
1
1
  const _ = require('lodash')
2
2
 
3
- const InfluxDB = require('influx')
3
+ const { InfluxDB, Point } = require('@influxdata/influxdb-client')
4
+ const { BucketsAPI } = require('@influxdata/influxdb-client-apis')
4
5
 
5
- function schema_check(fields) {
6
+ function fields_check(fields) {
6
7
  let result = true
7
8
  for (let key in fields) {
8
- if (!fields[key] && fields[key] !== 0) {
9
+ if (_.isNaN(fields[key]) || _.isNull(fields[key]) || _.isUndefined(fields[key])) {
9
10
  result = false
10
11
  }
11
12
  }
@@ -14,10 +15,10 @@ function schema_check(fields) {
14
15
 
15
16
  function check_points(points) {
16
17
  return points.filter((point) => {
17
- if (schema_check(point.fields) && (!point.timestamp || point.timestamp instanceof Date)) {
18
+ if (fields_check(point.fields) && (!point.timestamp || point.timestamp instanceof Date)) {
18
19
  return true
19
20
  } else {
20
- console.error(point)
21
+ console.error('influx check_points', point)
21
22
  return false
22
23
  }
23
24
  })
@@ -25,63 +26,62 @@ function check_points(points) {
25
26
 
26
27
  module.exports = class Influx {
27
28
  constructor(config) {
28
- let config_db = {
29
- host: config.server,
30
- database: config.database,
31
- }
32
- if (config.schema) {
33
- config_db.schema = config.schema
34
- }
35
- if (config.password) {
36
- config_db.username = config.user || config.username
37
- config_db.password = config.password
38
- }
39
- this.db = new InfluxDB.InfluxDB(config_db)
40
- this.database = config.database
41
- this.database_exist = false
42
- }
43
- static get FLOAT() {
44
- return InfluxDB.FieldType.FLOAT
45
- }
46
- static get INTEGER() {
47
- return InfluxDB.FieldType.INTEGER
48
- }
49
- static get STRING() {
50
- return InfluxDB.FieldType.STRING
51
- }
52
- static get BOOLEAN() {
53
- return InfluxDB.FieldType.BOOLEAN
29
+ const { url, token, org, org_id, bucket, schema } = config
30
+ const influx_db = new InfluxDB({ url, token })
31
+ this.buckets_api = new BucketsAPI(influx_db)
32
+ this.write_api = influx_db.getWriteApi(org, bucket)
33
+ this.query_api = influx_db.getQueryApi(org)
34
+ this.org = org
35
+ this.org_id = org_id
36
+ this.bucket = bucket
37
+ this.bucket_exist = false
38
+ this.schema = schema
54
39
  }
55
- query(query_string, cb) {
56
- this.db
57
- .query(query_string)
58
- .then((res) => {
59
- res.map((e) => {
60
- e.date = new Date(e.time.toNanoISOString())
61
- return e
62
- })
63
- cb({ success: true, body: res })
64
- })
65
- .catch((err) => {
66
- cb({ success: false, error: err })
67
- })
68
- }
69
- write_points_with_check(points, cb) {
70
- if (!this.database_exist) {
71
- this.db.getDatabaseNames().then((res) => {
72
- if (res.indexOf(this.database) === -1) {
73
- this.db.createDatabase(this.database).then((res) => {
74
- this.database_exist = true
75
- })
76
- } else {
77
- this.database_exist = true
40
+ // query(query_string, cb) {
41
+ // this.db
42
+ // .query(query_string)
43
+ // .then((res) => {
44
+ // res.map((e) => {
45
+ // e.date = new Date(e.time.toNanoISOString())
46
+ // return e
47
+ // })
48
+ // cb({ success: true, body: res })
49
+ // })
50
+ // .catch((err) => {
51
+ // cb({ success: false, error: err })
52
+ // })
53
+ // }
54
+ async write_points_with_check(points, cb) {
55
+ if (!this.bucket_exist) {
56
+ const { buckets } = await this.buckets_api.getBuckets({ org: this.org })
57
+ const names = buckets.map((b) => b.name)
58
+ if (!_.includes(names, this.bucket)) {
59
+ const bucket = await this.buckets_api.postBuckets({ body: { orgID: this.org_id, name: this.bucket } })
60
+ console.log('successfully create bucket:', bucket.name)
61
+ } else {
62
+ this.bucket_exist = true
63
+ }
64
+ } else if (check_points(points)) {
65
+ points = points.map((p) => {
66
+ let p_temp = new Point(p.measurement)
67
+ for (let tag in p.tags) {
68
+ p_temp = p_temp.tag(tag, p.tags[tag])
69
+ }
70
+ for (let field in p.fields) {
71
+ let field_type = this.schema[p.measurement].fields[field]
72
+ if (field_type === 'float') {
73
+ p_temp = p_temp.floatField(field, p.fields[field])
74
+ } else if (field_type === 'integer') {
75
+ p_temp = p_temp.intField(field, p.fields[field])
76
+ }
78
77
  }
78
+ return p_temp
79
79
  })
80
- } else if (check_points(points)) {
81
- this.db
82
- .writePoints(points)
83
- .then((res) => {
84
- cb({ success: true, body: res })
80
+ this.write_api.writePoints(points)
81
+ this.write_api
82
+ .close()
83
+ .then(() => {
84
+ cb({ success: true, body: points.length })
85
85
  })
86
86
  .catch((err) => {
87
87
  cb({ success: false, error: err })
package/lib/utils.js CHANGED
@@ -421,49 +421,49 @@ module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
421
421
  return { asks, bids }
422
422
  }
423
423
 
424
- console.log(
425
- module.exports.combine_orderbooks(
426
- {
427
- // binance: {
428
- // // asks: [
429
- // // [1, 1],
430
- // // [2, 2],
431
- // // [3, 3],
432
- // // ],
433
- // // bids: [
434
- // // [0.9, 0.9],
435
- // // [0.5, 0.5],
436
- // // [0.3, 0.2],
437
- // // ],
438
- // },
439
- bittrex: {
440
- // asks: [
441
- // // [4, 4],
442
- // // [5, 5],
443
- // // [8, 8],
444
- // ],
445
- bids: [
446
- [0.6, 0.9],
447
- [0.5, 0.2],
448
- [0.2, 0.2],
449
- ],
450
- },
451
- poloniex: {
452
- // asks: [
453
- // // [1, 1],
454
- // // [6, 6],
455
- // // [7, 7],
456
- // ],
457
- bids: [
458
- [0.95, 0.9],
459
- [0.7, 0.5],
460
- [0.4, 0.2],
461
- ],
462
- },
463
- },
464
- 100
465
- )
466
- )
424
+ // console.log(
425
+ // module.exports.combine_orderbooks(
426
+ // {
427
+ // binance: {
428
+ // asks: [
429
+ // [1, 1],
430
+ // [2, 2],
431
+ // [3, 3],
432
+ // ],
433
+ // bids: [
434
+ // // [0.9, 0.9],
435
+ // // [0.5, 0.5],
436
+ // // [0.3, 0.2],
437
+ // ],
438
+ // },
439
+ // bittrex: {
440
+ // // asks: [
441
+ // // // [4, 4],
442
+ // // // [5, 5],
443
+ // // // [8, 8],
444
+ // // ],
445
+ // bids: [
446
+ // [0.6, 0.9],
447
+ // [0.5, 0.2],
448
+ // [0.2, 0.2],
449
+ // ],
450
+ // },
451
+ // poloniex: {
452
+ // asks: [
453
+ // [1, 1],
454
+ // [6, 6],
455
+ // [7, 7],
456
+ // ],
457
+ // bids: [
458
+ // [0.95, 0.9],
459
+ // [0.7, 0.5],
460
+ // [0.4, 0.2],
461
+ // ],
462
+ // },
463
+ // },
464
+ // 100
465
+ // )
466
+ // )
467
467
  // console.log(module.exports.combine_orderbooks({}, 100))
468
468
 
469
469
  module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.25",
3
+ "version": "1.7.27",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -20,6 +20,8 @@
20
20
  },
21
21
  "homepage": "https://github.com/icgio/icg-utils#readme",
22
22
  "dependencies": {
23
+ "@influxdata/influxdb-client": "^1.33.0",
24
+ "@influxdata/influxdb-client-apis": "^1.33.0",
23
25
  "etherscan-api": "^8.1.5",
24
26
  "external-ip": "^2.3.1",
25
27
  "htmlparser2": "^3.10.1",