datadog-sync 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d766665d8efbf5ea63faa76d8b6609c361a34f36
4
- data.tar.gz: 7d35002a21ea7204cc12b745224381a9cb6fe6ee
3
+ metadata.gz: 76d02c9e4e60bab67d69c9433d13f8c54a13ee66
4
+ data.tar.gz: 3689e39795d3a0af528add8829bb721df44636aa
5
5
  SHA512:
6
- metadata.gz: 75de678587c9cc0f753751ebfc4a80a67b38e9753a9f29c92f1fa685ae60d3aee222b8c9c7df130cffd704684b1389ab5a22aed4e9e5bdd82637a55a6ece0e9c
7
- data.tar.gz: ad5272730e374e2f2997958a15f97425b9c8a19365f314dfe94d1c6a78adf3cf88c8c7790c6606f4ce71375609110ef0e9dd0ffdeabf48b671b5998d3d37b1ee
6
+ metadata.gz: 869e8877bc21dd2e56d6a9bb6c6ff8e06fe4675b59fe1e8cf3fbf09abcb1f07cbb7dd52626522a9ba4f655ab2462ab8df34df55ae05ece84f56b6b3ba2752afd
7
+ data.tar.gz: 367443a485eb1c51f76da3778a9ed90f7307826883f45cd1b99b92af167d28851d47ebe12225e4ad4467623b0384863731d27e59ba8bc3fafdeb2702b1d2f597
data/README.md CHANGED
@@ -0,0 +1,4 @@
1
+ * Backup in case if someone accidentally deletes dashboard and ability to recreate it with single command
2
+ * Ability to keep dashboards in git repo, make modifications using text editor, suggest Pull Requests
3
+ * YAML format providing simpler way to describe dashboards without repeating a lot of things in chatty JSON. Example: https://github.com/xeron/datadog-sync/blob/master/examples/dashboard_example.yml
4
+ * Automation purposes (for example creating new dashboard or adding graphs when you deploy new version of application)
@@ -1,11 +1,27 @@
1
1
  class DatadogSync
2
- attr_reader :dd_client
2
+ attr_reader :dd_client, :logger
3
+
4
+ def initialize(api_key, app_key, options={})
5
+ default_options = {
6
+ log_level: :info,
7
+ log_target: STDOUT
8
+ }
9
+ final_options = default_options.merge(options)
3
10
 
4
- def initialize(api_key, app_key)
5
11
  @dd_client = Dogapi::Client.new(api_key, app_key)
12
+
13
+ set_logger(final_options)
6
14
  end
7
15
 
16
+ private
17
+
8
18
  def sanitize_filename(filename)
9
19
  filename.gsub(/[\?\*\/\\]/, "_")
10
20
  end
21
+
22
+ def set_logger(config)
23
+ @logger = Logger.new(config[:log_target])
24
+ @logger.level = config[:log_level]
25
+ @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
26
+ end
11
27
  end
@@ -1,3 +1,4 @@
1
1
  class DatadogSync
2
+ DoesNotExist = Class.new(RuntimeError)
2
3
  AlreadyExists = Class.new(RuntimeError)
3
4
  end
@@ -4,20 +4,21 @@ class DatadogSync
4
4
  base_path = File.expand_path(dashboards_path)
5
5
 
6
6
  if File.file?(base_path)
7
- raise AlreadyExists, "Provided gashboards path already exists and it's not a directory."
7
+ logger.error "Provided gashboards path already exists and it's not a directory."
8
+ raise AlreadyExists
8
9
  elsif !File.directory?(base_path)
9
- puts "Creating directory for dashboards: '#{base_path}'"
10
+ logger.info "Creating directory for dashboards: '#{base_path}'"
10
11
  FileUtils.mkdir_p(base_path)
11
12
  end
12
13
 
13
14
  all_dashes = dd_client.get_dashboards[1]["dashes"]
14
15
 
15
- puts "Found #{all_dashes.count} dashboards"
16
+ logger.info "Found #{all_dashes.count} dashboards"
16
17
 
17
18
  filtered_dashes = all_dashes.select { |dash| dash["title"] =~ regex }
18
19
  filtered_dashes_ids = filtered_dashes.collect { |dash| dash["id"] }
19
20
 
20
- puts "Saving #{filtered_dashes.count} dashboards with pattern /#{title_pattern}/ into '#{base_path}'"
21
+ logger.info "Saving #{filtered_dashes.count} dashboards with pattern /#{title_pattern}/ into '#{base_path}'"
21
22
 
22
23
  filtered_dashes_ids.each do |dash_id|
23
24
  dash_data = dd_client.get_dashboard(dash_id)[1]["dash"]
@@ -0,0 +1,28 @@
1
+ class DatadogSync
2
+ def update_dashboards(dashboards_path, title_pattern="")
3
+ regex = Regexp.new(title_pattern)
4
+ base_path = File.expand_path(dashboards_path)
5
+
6
+ unless File.directory?(base_path)
7
+ logger.error "Provided gashboards path does not exist."
8
+ raise DoesNotExist
9
+ end
10
+
11
+ all_dash_files = Dir.glob(File.join(base_path, "*"))
12
+
13
+ logger.info "Found #{all_dash_files.count} local dashboards"
14
+
15
+ filtered_dashes = []
16
+ all_dash_files.each do |file|
17
+ data = JSON.parse(File.read(file))
18
+ filtered_dashes << data if data["title"] =~ regex
19
+ end
20
+ # filtered_dashes_ids = filtered_dashes.collect { |dash| dash["id"] }
21
+
22
+ logger.info "Updating #{filtered_dashes.count} dashboards with pattern /#{title_pattern}/ from '#{base_path}'"
23
+
24
+ filtered_dashes.each do |dash|
25
+ dd_client.update_dashboard(dash["id"], dash["title"], dash["description"], dash["graphs"], dash["template_variables"])
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  class DatadogSync
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/datadog_sync.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'dogapi'
2
2
  require 'json'
3
3
  require 'fileutils'
4
+ require 'logger'
4
5
 
5
6
  require 'datadog_sync/version'
6
7
  require 'datadog_sync/errors'
7
8
  require 'datadog_sync/base'
8
9
  require 'datadog_sync/save_dashboards'
10
+ require 'datadog_sync/update_dashboards'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog-sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Larionov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-03 00:00:00.000000000 Z
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dogapi
@@ -56,6 +56,7 @@ files:
56
56
  - lib/datadog_sync/base.rb
57
57
  - lib/datadog_sync/errors.rb
58
58
  - lib/datadog_sync/save_dashboards.rb
59
+ - lib/datadog_sync/update_dashboards.rb
59
60
  - lib/datadog_sync/version.rb
60
61
  homepage: http://github.com/xeron/datadog-sync
61
62
  licenses: