From 06e57f36b2bd75e60302cf5c9df3424bb37dfa4a Mon Sep 17 00:00:00 2001
From: nattanon <nattanon@hospital-os.com>
Date: Wed, 7 Jul 2021 15:31:50 +0700
Subject: [PATCH] Add lib

---
 Gemfile.lock                         | 149 +++++++++++++++++++++++++++
 README.md                            |  28 ++++-
 lib/latias/influxdb.rb               |   8 +-
 lib/latias/influxdb/bucket.rb        |  81 +++++++++++++++
 lib/latias/influxdb/configuration.rb |  28 +++++
 lib/latias/influxdb/engine.rb        |   3 +
 lib/latias/influxdb/version.rb       |   2 +
 7 files changed, 295 insertions(+), 4 deletions(-)
 create mode 100644 Gemfile.lock
 create mode 100644 lib/latias/influxdb/bucket.rb
 create mode 100644 lib/latias/influxdb/configuration.rb

diff --git a/Gemfile.lock b/Gemfile.lock
new file mode 100644
index 0000000..02e5f2d
--- /dev/null
+++ b/Gemfile.lock
@@ -0,0 +1,149 @@
+GIT
+  remote: git://github.com/karn18/influxdb-client-ruby.git
+  revision: f68a9cea5e88219db71b230438d843ccd329e438
+  specs:
+    influxdb-client (1.16.0)
+
+PATH
+  remote: .
+  specs:
+    latias-influxdb (0.1.0)
+      rails (~> 6.1.4)
+
+GEM
+  remote: https://rubygems.org/
+  specs:
+    actioncable (6.1.4)
+      actionpack (= 6.1.4)
+      activesupport (= 6.1.4)
+      nio4r (~> 2.0)
+      websocket-driver (>= 0.6.1)
+    actionmailbox (6.1.4)
+      actionpack (= 6.1.4)
+      activejob (= 6.1.4)
+      activerecord (= 6.1.4)
+      activestorage (= 6.1.4)
+      activesupport (= 6.1.4)
+      mail (>= 2.7.1)
+    actionmailer (6.1.4)
+      actionpack (= 6.1.4)
+      actionview (= 6.1.4)
+      activejob (= 6.1.4)
+      activesupport (= 6.1.4)
+      mail (~> 2.5, >= 2.5.4)
+      rails-dom-testing (~> 2.0)
+    actionpack (6.1.4)
+      actionview (= 6.1.4)
+      activesupport (= 6.1.4)
+      rack (~> 2.0, >= 2.0.9)
+      rack-test (>= 0.6.3)
+      rails-dom-testing (~> 2.0)
+      rails-html-sanitizer (~> 1.0, >= 1.2.0)
+    actiontext (6.1.4)
+      actionpack (= 6.1.4)
+      activerecord (= 6.1.4)
+      activestorage (= 6.1.4)
+      activesupport (= 6.1.4)
+      nokogiri (>= 1.8.5)
+    actionview (6.1.4)
+      activesupport (= 6.1.4)
+      builder (~> 3.1)
+      erubi (~> 1.4)
+      rails-dom-testing (~> 2.0)
+      rails-html-sanitizer (~> 1.1, >= 1.2.0)
+    activejob (6.1.4)
+      activesupport (= 6.1.4)
+      globalid (>= 0.3.6)
+    activemodel (6.1.4)
+      activesupport (= 6.1.4)
+    activerecord (6.1.4)
+      activemodel (= 6.1.4)
+      activesupport (= 6.1.4)
+    activestorage (6.1.4)
+      actionpack (= 6.1.4)
+      activejob (= 6.1.4)
+      activerecord (= 6.1.4)
+      activesupport (= 6.1.4)
+      marcel (~> 1.0.0)
+      mini_mime (>= 1.1.0)
+    activesupport (6.1.4)
+      concurrent-ruby (~> 1.0, >= 1.0.2)
+      i18n (>= 1.6, < 2)
+      minitest (>= 5.1)
+      tzinfo (~> 2.0)
+      zeitwerk (~> 2.3)
+    builder (3.2.4)
+    concurrent-ruby (1.1.9)
+    crass (1.0.6)
+    erubi (1.10.0)
+    globalid (0.4.2)
+      activesupport (>= 4.2.0)
+    i18n (1.8.10)
+      concurrent-ruby (~> 1.0)
+    loofah (2.10.0)
+      crass (~> 1.0.2)
+      nokogiri (>= 1.5.9)
+    mail (2.7.1)
+      mini_mime (>= 0.1.1)
+    marcel (1.0.1)
+    method_source (1.0.0)
+    mini_mime (1.1.0)
+    minitest (5.14.4)
+    nio4r (2.5.7)
+    nokogiri (1.11.7-x86_64-darwin)
+      racc (~> 1.4)
+    racc (1.5.2)
+    rack (2.2.3)
+    rack-test (1.1.0)
+      rack (>= 1.0, < 3)
+    rails (6.1.4)
+      actioncable (= 6.1.4)
+      actionmailbox (= 6.1.4)
+      actionmailer (= 6.1.4)
+      actionpack (= 6.1.4)
+      actiontext (= 6.1.4)
+      actionview (= 6.1.4)
+      activejob (= 6.1.4)
+      activemodel (= 6.1.4)
+      activerecord (= 6.1.4)
+      activestorage (= 6.1.4)
+      activesupport (= 6.1.4)
+      bundler (>= 1.15.0)
+      railties (= 6.1.4)
+      sprockets-rails (>= 2.0.0)
+    rails-dom-testing (2.0.3)
+      activesupport (>= 4.2.0)
+      nokogiri (>= 1.6)
+    rails-html-sanitizer (1.3.0)
+      loofah (~> 2.3)
+    railties (6.1.4)
+      actionpack (= 6.1.4)
+      activesupport (= 6.1.4)
+      method_source
+      rake (>= 0.13)
+      thor (~> 1.0)
+    rake (13.0.4)
+    sprockets (4.0.2)
+      concurrent-ruby (~> 1.0)
+      rack (> 1, < 3)
+    sprockets-rails (3.2.2)
+      actionpack (>= 4.0)
+      activesupport (>= 4.0)
+      sprockets (>= 3.0.0)
+    thor (1.1.0)
+    tzinfo (2.0.4)
+      concurrent-ruby (~> 1.0)
+    websocket-driver (0.7.5)
+      websocket-extensions (>= 0.1.0)
+    websocket-extensions (0.1.5)
+    zeitwerk (2.4.2)
+
+PLATFORMS
+  x86_64-darwin-19
+
+DEPENDENCIES
+  influxdb-client!
+  latias-influxdb!
+
+BUNDLED WITH
+   2.2.17
diff --git a/README.md b/README.md
index 508b09f..bf60d14 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,30 @@
 # Latias::Influxdb
-Short description and motivation.
+
+Ruby lib for use influxdb version 2 on Ruby language
 
 ## Usage
-How to use my plugin.
+
+```ruby
+class MdbEnergyBucket < Latias::Influxdb::Bucket
+  def initialize
+    @url = 'http://localhost:8086'
+    @token = 'your token'
+    @bucket = 'MDBEnergy'
+    @org = 'MDBEnergy'
+    @use_ssl = false
+    @verify_mode = OpenSSL::SSL::VERIFY_NONE
+    @precision = InfluxDB2::WritePrecision::NANOSECOND
+    @client = client
+  end
+end
+
+mdb_energy_bucket = MdbEnergyBucket.new
+mdb_energy_bucket.range('start: -1m').filter('fn: (r) => r["_measurement"] == "cpu"')
+mdb_energy_bucket.call
+```
 
 ## Installation
+
 Add this line to your application's Gemfile:
 
 ```ruby
@@ -12,17 +32,21 @@ gem 'latias-influxdb'
 ```
 
 And then execute:
+
 ```bash
 $ bundle
 ```
 
 Or install it yourself as:
+
 ```bash
 $ gem install latias-influxdb
 ```
 
 ## Contributing
+
 Contribution directions go here.
 
 ## License
+
 The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
diff --git a/lib/latias/influxdb.rb b/lib/latias/influxdb.rb
index 558a49b..ac94642 100644
--- a/lib/latias/influxdb.rb
+++ b/lib/latias/influxdb.rb
@@ -1,7 +1,11 @@
-require "latias/influxdb/version"
-require "latias/influxdb/engine"
+# frozen_string_literal: true
+
+require 'latias/influxdb/bucket'
+require 'latias/influxdb/version'
+require 'latias/influxdb/engine'
 
 module Latias
+  # Influxdb
   module Influxdb
     # Your code goes here...
   end
diff --git a/lib/latias/influxdb/bucket.rb b/lib/latias/influxdb/bucket.rb
new file mode 100644
index 0000000..cdd7929
--- /dev/null
+++ b/lib/latias/influxdb/bucket.rb
@@ -0,0 +1,81 @@
+# frozen_string_literal: true
+
+require 'influxdb-client'
+
+module Latias
+  # Influxdb
+  module Influxdb
+    # Bucket
+    class Bucket
+      attr_reader :url, :token, :bucket, :org, :use_ssl, :verify_mode, :precision
+
+      def initialize
+        @query = ''
+        @url = 'http://localhost:8086'
+        @token = 'token'
+        @bucket = 'bucket'
+        @org = 'org'
+        @use_ssl = false
+        @verify_mode = OpenSSL::SSL::VERIFY_NONE
+      end
+
+      def call
+        all_query = "from(bucket: \"#{@bucket}\") #{@query}"
+        @query = ''
+        @client.create_query_api.query(query: all_query, org: @org)
+      end
+
+      # old code
+      # def call
+      #   all_query = "from(bucket: \"#{@bucket}\")" \
+      #           '|> range(start: -1m)
+      #           |> filter(fn: (r) => r["_measurement"] == "cpu")
+      #           |> filter(fn: (r) => r["_field"] == "usage_user")
+      #           |> filter(fn: (r) => r["cpu"] == "cpu-total" or r["cpu"] == "cpu0" or r["cpu"] == "cpu1")
+      #           |> yield(name: "sum")'
+
+      #   @client.create_query_api.query(query: all_query, org: @org)
+      # end
+
+      def yield(query)
+        @query += yield_format(query)
+        self
+      end
+
+      def range(query)
+        @query += range_format(query)
+        self
+      end
+
+      def filter(query)
+        @query += filter_format(query)
+        self
+      end
+
+      private
+
+      def yield_format(query)
+        "#{new_command} yield(#{query})"
+      end
+
+      def range_format(query)
+        "#{new_command} range(#{query})"
+      end
+
+      def filter_format(query)
+        "#{new_command} filter(#{query})"
+      end
+
+      def new_command
+        '|> '
+      end
+
+      def client
+        @client = InfluxDB2::Client.new(@url, @token,
+                                        use_ssl: @use_ssl,
+                                        verify_mode: @verify_mode.present? ? @verify_mode : OpenSSL::SSL::VERIFY_NONE,
+                                        precision: InfluxDB2::WritePrecision::NANOSECOND)
+      end
+    end
+  end
+end
diff --git a/lib/latias/influxdb/configuration.rb b/lib/latias/influxdb/configuration.rb
new file mode 100644
index 0000000..158b044
--- /dev/null
+++ b/lib/latias/influxdb/configuration.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Latias
+  # Influxdb
+  module Influxdb
+    # Configuration
+    class Configuration
+      attr_accessor :async
+
+      def initialize
+        @async = true
+      end
+    end
+
+    # Borrow syntax from Clearance: https://github.com/thoughtbot/clearance/blob/master/lib/clearance/configuration.rb
+    def self.configuration
+      @configuration ||= Configuration.new
+    end
+
+    def self.configuration=(config)
+      @configuration = config
+    end
+
+    def self.configure
+      yield configuration
+    end
+  end
+end
diff --git a/lib/latias/influxdb/engine.rb b/lib/latias/influxdb/engine.rb
index 181e8c5..8099512 100644
--- a/lib/latias/influxdb/engine.rb
+++ b/lib/latias/influxdb/engine.rb
@@ -1,5 +1,8 @@
+# frozen_string_literal: true
+
 module Latias
   module Influxdb
+    # Engine
     class Engine < ::Rails::Engine
       isolate_namespace Latias::Influxdb
     end
diff --git a/lib/latias/influxdb/version.rb b/lib/latias/influxdb/version.rb
index 5099f25..d48dada 100644
--- a/lib/latias/influxdb/version.rb
+++ b/lib/latias/influxdb/version.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 module Latias
   module Influxdb
     VERSION = '0.1.0'
-- 
GitLab