ruby-grafana-reporter 0.2.1 → 0.4.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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +126 -85
  3. data/bin/ruby-grafana-reporter +2 -2
  4. data/lib/VERSION.rb +3 -2
  5. data/lib/grafana/abstract_datasource.rb +146 -0
  6. data/lib/grafana/dashboard.rb +21 -23
  7. data/lib/grafana/errors.rb +18 -3
  8. data/lib/grafana/grafana.rb +64 -66
  9. data/lib/grafana/grafana_alerts_datasource.rb +57 -0
  10. data/lib/grafana/grafana_annotations_datasource.rb +56 -0
  11. data/lib/grafana/grafana_property_datasource.rb +30 -0
  12. data/lib/grafana/graphite_datasource.rb +72 -0
  13. data/lib/grafana/image_rendering_datasource.rb +44 -0
  14. data/lib/grafana/influxdb_datasource.rb +70 -0
  15. data/lib/grafana/panel.rb +9 -3
  16. data/lib/grafana/prometheus_datasource.rb +67 -0
  17. data/lib/grafana/sql_datasource.rb +78 -0
  18. data/lib/grafana/unsupported_datasource.rb +7 -0
  19. data/lib/grafana/variable.rb +1 -1
  20. data/lib/grafana/webrequest.rb +71 -0
  21. data/lib/grafana_reporter/abstract_query.rb +460 -0
  22. data/lib/grafana_reporter/abstract_report.rb +139 -18
  23. data/lib/grafana_reporter/alerts_table_query.rb +39 -0
  24. data/lib/grafana_reporter/annotations_table_query.rb +38 -0
  25. data/lib/grafana_reporter/application/application.rb +34 -286
  26. data/lib/grafana_reporter/application/webservice.rb +50 -15
  27. data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +91 -0
  28. data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +90 -0
  29. data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +74 -0
  30. data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +76 -0
  31. data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +70 -0
  32. data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +95 -0
  33. data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +90 -0
  34. data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +49 -0
  35. data/lib/grafana_reporter/asciidoctor/report.rb +40 -81
  36. data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +46 -0
  37. data/lib/grafana_reporter/asciidoctor/show_help_include_processor.rb +35 -0
  38. data/lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb +90 -0
  39. data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +86 -0
  40. data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
  41. data/lib/grafana_reporter/configuration.rb +59 -52
  42. data/lib/grafana_reporter/console_configuration_wizard.rb +311 -0
  43. data/lib/grafana_reporter/demo_report_wizard.rb +105 -0
  44. data/lib/grafana_reporter/erb/report.rb +30 -0
  45. data/lib/grafana_reporter/erb/report_jail.rb +21 -0
  46. data/lib/grafana_reporter/errors.rb +55 -0
  47. data/lib/grafana_reporter/help.rb +443 -0
  48. data/lib/grafana_reporter/logger/{two_way_logger.rb → two_way_delegate_logger.rb} +1 -1
  49. data/lib/grafana_reporter/panel_image_query.rb +25 -0
  50. data/lib/grafana_reporter/panel_property_query.rb +22 -0
  51. data/lib/grafana_reporter/query_value_query.rb +61 -0
  52. data/lib/grafana_reporter/report_webhook.rb +35 -0
  53. data/lib/ruby_grafana_extension.rb +8 -0
  54. data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +1 -2
  55. metadata +47 -38
  56. data/lib/grafana/abstract_panel_query.rb +0 -22
  57. data/lib/grafana/abstract_query.rb +0 -132
  58. data/lib/grafana/abstract_sql_query.rb +0 -51
  59. data/lib/grafana/panel_image_query.rb +0 -52
  60. data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +0 -104
  61. data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -99
  62. data/lib/grafana_reporter/asciidoctor/errors.rb +0 -40
  63. data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +0 -92
  64. data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +0 -91
  65. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +0 -69
  66. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +0 -68
  67. data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +0 -61
  68. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +0 -78
  69. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +0 -73
  70. data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +0 -20
  71. data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +0 -43
  72. data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +0 -228
  73. data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +0 -70
  74. data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +0 -66
  75. data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +0 -86
  76. data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -34
  77. data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -25
  78. data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
  79. data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -38
  80. data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -294
  81. data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -32
  82. data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -34
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'processor_mixin'
4
+
5
+ module GrafanaReporter
6
+ module Asciidoctor
7
+ # Implements the hook
8
+ # grafana_panel_property:<panel_id>[<options>]
9
+ #
10
+ # Returns the requested panel property.
11
+ #
12
+ # == Used document parameters
13
+ # +grafana_default_instance+ - name of grafana instance, 'default' if not specified
14
+ #
15
+ # +grafana_default_dashboard+ - uid of grafana default dashboard to use
16
+ #
17
+ # == Supported options
18
+ # +field+ - property to query for, e.g. +description+ or +title+ (*mandatory*)
19
+ #
20
+ # +instance+ - name of grafana instance, 'default' if not specified
21
+ #
22
+ # +dashboard+ - uid of grafana dashboard to use
23
+ class PanelPropertyInlineMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
24
+ include ProcessorMixin
25
+ use_dsl
26
+
27
+ named :grafana_panel_property
28
+ name_positional_attributes :field
29
+
30
+ # :nodoc:
31
+ def process(parent, target, attrs)
32
+ return if @report.cancel
33
+
34
+ @report.next_step
35
+ instance = attrs['instance'] || parent.document.attr('grafana_default_instance') || 'default'
36
+ dashboard = attrs['dashboard'] || parent.document.attr('grafana_default_dashboard')
37
+ @report.logger.debug("Processing PanelPropertyInlineMacro (instance: #{instance}, dashboard: #{dashboard},"\
38
+ " panel: #{target}, property: #{attrs[:field]})")
39
+
40
+ begin
41
+ query = PanelPropertyQuery.new(@report.grafana(instance).dashboard(dashboard).panel(target), variables: build_attribute_hash(parent.document.attributes, attrs))
42
+ query.raw_query = { property_name: attrs[:field] }
43
+
44
+ description = query.execute
45
+ rescue GrafanaReporterError => e
46
+ @report.logger.error(e.message)
47
+ return create_inline(parent, :quoted, e.message)
48
+ rescue StandardError => e
49
+ @report.logger.fatal(e.message)
50
+ return create_inline(parent, :quoted, e.message)
51
+ end
52
+
53
+ # translate linebreaks to asciidoctor syntax
54
+ # and HTML encode to make sure, that HTML formattings are respected
55
+ create_inline(parent, :quoted, CGI.escapeHTML(description.gsub(%r{//[^\n]*(?:\n)?}, '').gsub(/\n/, " +\n")))
56
+ end
57
+
58
+ # @see ProcessorMixin#build_demo_entry
59
+ def build_demo_entry(panel)
60
+ return nil unless panel
61
+ return nil unless panel.model['title']
62
+ return nil if panel.model['title'].strip == ''
63
+ return nil if panel.model['title'].strip == 'Panel Title'
64
+
65
+ "this text includes the panel with title grafana_panel_property:#{panel.id}[\"title\","\
66
+ "dashboard=\"#{panel.dashboard.id}\"]"
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'processor_mixin'
4
+
5
+ module GrafanaReporter
6
+ module Asciidoctor
7
+ # Implements the hook
8
+ # include::grafana_panel_query_table:<panel_id>[<options>]
9
+ #
10
+ # Returns the results of the SQL query as a asciidoctor table.
11
+ #
12
+ # == Used document parameters
13
+ # +grafana_default_instance+ - name of grafana instance, 'default' if not specified
14
+ #
15
+ # +grafana_default_dashboard+ - uid of grafana default dashboard to use
16
+ #
17
+ # +from+ - 'from' time for the sql query
18
+ #
19
+ # +to+ - 'to' time for the sql query
20
+ #
21
+ # All other variables starting with +var-+ will be used to replace grafana templating strings
22
+ # in the given SQL query.
23
+ #
24
+ # == Supported options
25
+ # +query+ - query letter, which shall be used, e.g. +C+ (*mandatory*)
26
+ #
27
+ # +instance+ - name of grafana instance, 'default' if not specified
28
+ #
29
+ # +dashboard+ - uid of grafana dashboard to use
30
+ #
31
+ # +from+ - 'from' time for the sql query
32
+ #
33
+ # +to+ - 'to' time for the sql query
34
+ #
35
+ # +format+ - see {AbstractQuery#format_columns}
36
+ #
37
+ # +replace_values+ - see {AbstractQuery#replace_values}
38
+ #
39
+ # +filter_columns+ - see {AbstractQuery#filter_columns}
40
+ class PanelQueryTableIncludeProcessor < ::Asciidoctor::Extensions::IncludeProcessor
41
+ include ProcessorMixin
42
+
43
+ # :nodoc:
44
+ def handles?(target)
45
+ target.start_with? 'grafana_panel_query_table:'
46
+ end
47
+
48
+ # :nodoc:
49
+ def process(doc, reader, target, attrs)
50
+ return if @report.cancel
51
+
52
+ @report.next_step
53
+ panel_id = target.split(':')[1]
54
+ instance = attrs['instance'] || doc.attr('grafana_default_instance') || 'default'
55
+ dashboard = attrs['dashboard'] || doc.attr('grafana_default_dashboard')
56
+ attrs['result_type'] = 'panel_table'
57
+ @report.logger.debug("Processing PanelQueryTableIncludeProcessor (instance: #{instance}, "\
58
+ "dashboard: #{dashboard}, panel: #{panel_id}, query: #{attrs['query']})")
59
+
60
+ begin
61
+ panel = @report.grafana(instance).dashboard(dashboard).panel(panel_id)
62
+ query = QueryValueQuery.new(panel, variables: build_attribute_hash(doc.attributes, attrs))
63
+
64
+ reader.unshift_lines query.execute
65
+ rescue GrafanaReporterError => e
66
+ @report.logger.error(e.message)
67
+ reader.unshift_line "|#{e.message}"
68
+ rescue StandardError => e
69
+ @report.logger.fatal(e.message)
70
+ reader.unshift_line "|#{e.message}"
71
+ end
72
+
73
+ reader
74
+ end
75
+
76
+ # @see ProcessorMixin#build_demo_entry
77
+ def build_demo_entry(panel)
78
+ return nil unless panel
79
+ return nil unless panel.model['type'].include?('table')
80
+
81
+ ref_id = nil
82
+ panel.model['targets'].each do |item|
83
+ if !item['hide'] && !panel.query(item['refId']).to_s.empty?
84
+ ref_id = item['refId']
85
+ break
86
+ end
87
+ end
88
+ return nil unless ref_id
89
+
90
+ "|===\ninclude::grafana_panel_query_table:#{panel.id}[query=\"#{ref_id}\",filter_columns=\"time\","\
91
+ "dashboard=\"#{panel.dashboard.id}\"]\n|==="
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'processor_mixin'
4
+
5
+ module GrafanaReporter
6
+ module Asciidoctor
7
+ # Implements the hook
8
+ # grafana_panel_query_value:<panel_id>[<options>]
9
+ #
10
+ # Returns the first value of the resulting SQL query.
11
+ #
12
+ # == Used document parameters
13
+ # +grafana_default_instance+ - name of grafana instance, 'default' if not specified
14
+ #
15
+ # +grafana_default_dashboard+ - uid of grafana default dashboard to use
16
+ #
17
+ # +from+ - 'from' time for the sql query
18
+ #
19
+ # +to+ - 'to' time for the sql query
20
+ #
21
+ # All other variables starting with +var-+ will be used to replace grafana templating strings
22
+ # in the given SQL query.
23
+ #
24
+ # == Supported options
25
+ # +query+ - query letter, which shall be used, e.g. +C+ (*mandatory*)
26
+ #
27
+ # +instance+ - name of grafana instance, 'default' if not specified
28
+ #
29
+ # +dashboard+ - uid of grafana dashboard to use
30
+ #
31
+ # +from+ - 'from' time for the sql query
32
+ #
33
+ # +to+ - 'to' time for the sql query
34
+ #
35
+ # +format+ - see {AbstractQuery#format_columns}
36
+ #
37
+ # +replace_values+ - see {AbstractQuery#replace_values}
38
+ #
39
+ # +filter_columns+ - see {AbstractQuery#filter_columns}
40
+ class PanelQueryValueInlineMacro < ::Asciidoctor::Extensions::InlineMacroProcessor
41
+ include ProcessorMixin
42
+ use_dsl
43
+
44
+ named :grafana_panel_query_value
45
+
46
+ # :nodoc:
47
+ def process(parent, target, attrs)
48
+ return if @report.cancel
49
+
50
+ @report.next_step
51
+ instance = attrs['instance'] || parent.document.attr('grafana_default_instance') || 'default'
52
+ dashboard = attrs['dashboard'] || parent.document.attr('grafana_default_dashboard')
53
+ attrs['result_type'] = 'panel_value'
54
+ @report.logger.debug("Processing PanelQueryValueInlineMacro (instance: #{instance}, dashboard: #{dashboard},"\
55
+ " panel: #{target}, query: #{attrs['query']})")
56
+
57
+ begin
58
+ panel = @report.grafana(instance).dashboard(dashboard).panel(target)
59
+ query = QueryValueQuery.new(panel, variables: build_attribute_hash(parent.document.attributes, attrs))
60
+
61
+ create_inline(parent, :quoted, query.execute)
62
+ rescue GrafanaReporterError => e
63
+ @report.logger.error(e.message)
64
+ create_inline(parent, :quoted, e.message)
65
+ rescue StandardError => e
66
+ @report.logger.fatal(e.message)
67
+ create_inline(parent, :quoted, e.message)
68
+ end
69
+ end
70
+
71
+ # @see ProcessorMixin#build_demo_entry
72
+ def build_demo_entry(panel)
73
+ return nil unless panel
74
+ return nil unless panel.model['type'] == 'singlestat'
75
+
76
+ ref_id = nil
77
+ panel.model['targets'].each do |item|
78
+ if !item['hide'] && !panel.query(item['refId']).to_s.empty?
79
+ ref_id = item['refId']
80
+ break
81
+ end
82
+ end
83
+ return nil unless ref_id
84
+
85
+ "it's easily possible to include the query value: grafana_panel_query_value:#{panel.id}[query=\"#{ref_id}\""\
86
+ ",dashboard=\"#{panel.dashboard.id}\"] - just within this text."
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GrafanaReporter
4
+ module Asciidoctor
5
+ # This module contains common methods for all asciidoctor extensions.
6
+ module ProcessorMixin
7
+ # Used when initializing a object instance, to set the report object, which is currently in progress.
8
+ # @param report [GrafanaReporter::Asciidoctor::Report] current report
9
+ # @return [::Asciidoctor::Extensions::Processor] self
10
+ def current_report(report)
11
+ @report = report
12
+ self
13
+ end
14
+
15
+ # This method is called if a demo report shall be built for the given {Grafana::Panel}.
16
+ # @param panel [Grafana::Panel] panel object, for which a demo entry shall be created.
17
+ # @return [String] String containing the entry, or nil if not possible for given panel
18
+ def build_demo_entry(panel)
19
+ raise NotImplementedError
20
+ end
21
+
22
+ # Merges the given hashes to a common attribute Hash. It respects the priorities of the hashes and the
23
+ # object and allows only valid variables to be used.
24
+ # @param document_hash [Hash] variables from report template level
25
+ # @param item_hash [Hash] variables from item configuration level, i.e. specific call, which may override document
26
+ # @return [Hash] containing accepted variable names including values
27
+ def build_attribute_hash(document_hash, item_hash)
28
+ result = {}
29
+
30
+ result['grafana_report_timestamp'] = document_hash['localdatetime']
31
+ result.merge!(document_hash.select do |k, _v|
32
+ k =~ /^var-/ ||
33
+ k =~ /^(?:from|to)$/ ||
34
+ k =~ /^grafana_default_(?:from_timezone|to_timezone|timeout)$/
35
+ end)
36
+
37
+ result.merge!(item_hash.select do |k, _v|
38
+ # TODO: specify accepted options for each processor class individually
39
+ k =~ /^(?:var-|render-)/ ||
40
+ k =~ /^(?:timeout|from|to)$/ ||
41
+ k =~ /filter_columns|format|replace_values_.*|transpose|column_divider|
42
+ row_divider|from_timezone|to_timezone|result_type|query/x
43
+ end)
44
+
45
+ result
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,27 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrafanaReporter
4
+ # This module contains all classes, which are necessary to use the reporter in conjunction with asciidoctor.
4
5
  module Asciidoctor
5
6
  # Implementation of a specific {AbstractReport}. It is used to
6
7
  # build reports specifically for asciidoctor results.
7
- class Report < GrafanaReporter::AbstractReport
8
- # (see AbstractReport#initialize)
9
- def initialize(config, template, destination_file_or_path = nil, custom_attributes = {})
8
+ class Report < ::GrafanaReporter::AbstractReport
9
+ # @see AbstractReport#initialize
10
+ def initialize(config)
10
11
  super
11
- @current_pos = 0
12
12
  @image_files = []
13
- @grafana_instances = {}
14
13
  end
15
14
 
16
- # Starts to create an asciidoctor report. It utilizes all {Extensions} to
17
- # realize the conversion.
18
- # @see AbstractReport#create_report
19
- # @return [void]
20
- def create_report
21
- @start_time = Time.now
15
+ # Starts to create an asciidoctor report. It utilizes all extensions in the {GrafanaReporter::Asciidoctor}
16
+ # namespace to realize the conversion.
17
+ # @see AbstractReport#build
18
+ def build(template, destination_file_or_path, custom_attributes)
22
19
  attrs = { 'convert-backend' => 'pdf' }.merge(@config.default_document_attributes.merge(@custom_attributes))
23
- attrs['grafana-report-timestamp'] = @start_time.to_s
24
- logger.info("Report started at #{@start_time}")
25
20
  logger.debug("Document attributes: #{attrs}")
26
21
 
27
22
  initialize_step_counter
@@ -30,19 +25,18 @@ module GrafanaReporter
30
25
  ::Asciidoctor::LoggerManager.logger = logger
31
26
 
32
27
  registry = ::Asciidoctor::Extensions::Registry.new
33
- # TODO: dynamically register macros, which is also needed when supporting custom macros
34
- registry.inline_macro Extensions::PanelImageInlineMacro.new.current_report(self)
35
- registry.inline_macro Extensions::PanelQueryValueInlineMacro.new.current_report(self)
36
- registry.inline_macro Extensions::PanelPropertyInlineMacro.new.current_report(self)
37
- registry.inline_macro Extensions::SqlValueInlineMacro.new.current_report(self)
38
- registry.block_macro Extensions::PanelImageBlockMacro.new.current_report(self)
39
- registry.include_processor Extensions::ValueAsVariableIncludeProcessor.new.current_report(self)
40
- registry.include_processor Extensions::PanelQueryTableIncludeProcessor.new.current_report(self)
41
- registry.include_processor Extensions::SqlTableIncludeProcessor.new.current_report(self)
42
- registry.include_processor Extensions::ShowEnvironmentIncludeProcessor.new.current_report(self)
43
- registry.include_processor Extensions::ShowHelpIncludeProcessor.new.current_report(self)
44
- registry.include_processor Extensions::AnnotationsTableIncludeProcessor.new.current_report(self)
45
- registry.include_processor Extensions::AlertsTableIncludeProcessor.new.current_report(self)
28
+ registry.inline_macro PanelImageInlineMacro.new.current_report(self)
29
+ registry.inline_macro PanelQueryValueInlineMacro.new.current_report(self)
30
+ registry.inline_macro PanelPropertyInlineMacro.new.current_report(self)
31
+ registry.inline_macro SqlValueInlineMacro.new.current_report(self)
32
+ registry.block_macro PanelImageBlockMacro.new.current_report(self)
33
+ registry.include_processor ValueAsVariableIncludeProcessor.new.current_report(self)
34
+ registry.include_processor PanelQueryTableIncludeProcessor.new.current_report(self)
35
+ registry.include_processor SqlTableIncludeProcessor.new.current_report(self)
36
+ registry.include_processor ShowEnvironmentIncludeProcessor.new.current_report(self)
37
+ registry.include_processor ShowHelpIncludeProcessor.new.current_report(self)
38
+ registry.include_processor AnnotationsTableIncludeProcessor.new.current_report(self)
39
+ registry.include_processor AlertsTableIncludeProcessor.new.current_report(self)
46
40
 
47
41
  ::Asciidoctor.convert_file(@template, extension_registry: registry, backend: attrs['convert-backend'],
48
42
  to_file: path, attributes: attrs, header_footer: true)
@@ -51,29 +45,28 @@ module GrafanaReporter
51
45
 
52
46
  # store report including als images as ZIP file, if the result is not a PDF
53
47
  if attrs['convert-backend'] != 'pdf'
54
- dest_path = nil
55
- case
56
- when @destination_file_or_path.is_a?(File)
57
- dest_path = @destination_file_or_path.path
58
- when @destination_file_or_path.is_a?(Tempfile)
59
- dest_path = @destination_file_or_path.path
60
- else
61
- dest_path = @destination_file_or_path
62
- end
48
+ dest_path = if @destination_file_or_path.is_a?(File) || @destination_file_or_path.is_a?(Tempfile)
49
+ @destination_file_or_path.path
50
+ else
51
+ @destination_file_or_path
52
+ end
63
53
 
64
54
  # build zip file
65
55
  zip_file = Tempfile.new('gf_zip')
66
- Zip::File.open(zip_file.path, Zip::File::CREATE) do |zipfile|
56
+ buffer = Zip::OutputStream.write_buffer do |zipfile|
67
57
  # add report file
68
- zipfile.get_output_stream("#{dest_path.gsub(@config.reports_folder, '')}.#{attrs['convert-backend']}") do |f|
69
- f.puts File.read(dest_path)
70
- end
58
+ zipfile.put_next_entry("#{dest_path.gsub(@config.reports_folder, '')}.#{attrs['convert-backend']}")
59
+ zipfile.write File.read(dest_path)
71
60
 
72
61
  # add image files
73
62
  @image_files.each do |file|
74
- zipfile.get_output_stream(file.path.gsub(@config.images_folder, '')) { |f| f.puts File.read(file.path) }
63
+ zipfile.put_next_entry(file.path.gsub(@config.images_folder, ''))
64
+ zipfile.write File.read(file.path)
75
65
  end
76
66
  end
67
+ File.open(zip_file, 'wb') do |f|
68
+ f.write buffer.string
69
+ end
77
70
 
78
71
  # replace original file with zip file
79
72
  zip_file.rewind
@@ -89,40 +82,6 @@ module GrafanaReporter
89
82
  end
90
83
 
91
84
  clean_image_files
92
- @end_time = Time.now
93
- logger.info("Report finished after #{@end_time - @start_time} seconds.")
94
- @done = true
95
- rescue StandardError => e
96
- # catch all errors during execution
97
- died_with_error(e)
98
- raise e
99
- end
100
-
101
- # @see AbstractReport#progress
102
- # @return [Float] number between 0 and 1 reflecting the current progress.
103
- def progress
104
- return 0 if @total_steps.to_i.zero?
105
-
106
- @current_pos.to_f / @total_steps
107
- end
108
-
109
- # @param instance [String] requested grafana instance
110
- # @return [Grafana::Grafana] the requested grafana instance.
111
- def grafana(instance)
112
- unless @grafana_instances[instance]
113
- @grafana_instances[instance] = ::Grafana::Grafana.new(@config.grafana_host(instance),
114
- @config.grafana_api_key(instance),
115
- logger: @logger,
116
- datasources: @config.grafana_datasources(instance))
117
- end
118
- @grafana_instances[instance]
119
- end
120
-
121
- # Increments the progress.
122
- # @return [Integer] number of the current progress position.
123
- def next_step
124
- @current_pos += 1
125
- @current_pos
126
85
  end
127
86
 
128
87
  # Called to save a temporary image file. After the final generation of the
@@ -131,6 +90,7 @@ module GrafanaReporter
131
90
  # @return [String] path to the temporary file.
132
91
  def save_image_file(img_data)
133
92
  file = Tempfile.new(['gf_image_', '.png'], @config.images_folder.to_s)
93
+ file.binmode
134
94
  file.write(img_data)
135
95
  path = file.path.gsub(/#{@config.images_folder}/, '')
136
96
 
@@ -140,13 +100,11 @@ module GrafanaReporter
140
100
  path
141
101
  end
142
102
 
143
- # Called, if the report generation has died with an error.
144
- # @param error [StandardError] occured error
145
- # @return [void]
146
- def died_with_error(error)
147
- @error = [error.message] << [error.backtrace]
148
- @end_time = Time.now
149
- @done = true
103
+ # @see AbstractReport#demo_report_classes
104
+ def self.demo_report_classes
105
+ [AlertsTableIncludeProcessor, AnnotationsTableIncludeProcessor, PanelImageBlockMacro, PanelImageInlineMacro,
106
+ PanelPropertyInlineMacro, PanelQueryTableIncludeProcessor, PanelQueryValueInlineMacro,
107
+ SqlTableIncludeProcessor, SqlValueInlineMacro, ShowHelpIncludeProcessor, ShowEnvironmentIncludeProcessor]
150
108
  end
151
109
 
152
110
  private
@@ -160,6 +118,7 @@ module GrafanaReporter
160
118
  @total_steps = 0
161
119
  File.readlines(@template).each do |line|
162
120
  begin
121
+ # TODO: move these calls to the specific processors to ensure all are counted properly
163
122
  @total_steps += line.gsub(%r{//.*}, '').scan(/(?:grafana_panel_image|grafana_panel_query_value|
164
123
  grafana_panel_query_table|grafana_sql_value|
165
124
  grafana_sql_table|grafana_environment|grafana_help|