@icgio/icg-utils 1.7.26 → 1.7.28

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.
Files changed (2) hide show
  1. package/lib/influxdb.js +59 -59
  2. package/package.json +3 -1
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
+ this.influx_db = new InfluxDB({ url, token })
31
+ this.buckets_api = new BucketsAPI(this.influx_db)
32
+ this.org = org
33
+ this.org_id = org_id
34
+ this.bucket = bucket
35
+ this.bucket_exist = false
36
+ this.schema = schema
54
37
  }
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
38
+ // query(query_string, cb) {
39
+ // this.query_api = this.influx_db.getQueryApi(org)
40
+ // this.db
41
+ // .query(query_string)
42
+ // .then((res) => {
43
+ // res.map((e) => {
44
+ // e.date = new Date(e.time.toNanoISOString())
45
+ // return e
46
+ // })
47
+ // cb({ success: true, body: res })
48
+ // })
49
+ // .catch((err) => {
50
+ // cb({ success: false, error: err })
51
+ // })
52
+ // }
53
+ async write_points_with_check(points, cb) {
54
+ const write_api = this.influx_db.getWriteApi(this.org, this.bucket)
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
+ write_api.writePoints(points)
81
+ 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.26",
3
+ "version": "1.7.28",
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",