Class: Thing

Inherits:
Teron
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::NumberHelper, GreenHat::FileTypes, GreenHat::Formatters, GreenHat::Kind, GreenHat::Spinner, GreenHat::ThingHelpers
Defined in:
lib/greenhat/thing.rb

Overview

Top Level what everything is

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GreenHat::FileTypes

#types

Methods included from GreenHat::Kind

#check_oj_parse?, #kind_collect, #kind_pattern_match, #kind_setup, #prompt, #prompt_for_kind

Methods included from GreenHat::Spinner

#humanize, #spin_done, #spin_start, #time

Methods included from GreenHat::ThingHelpers

#build_path, #inspect

Methods included from GreenHat::Formatters

#dmesg_split, #flatten_hash, #format_api_json, #format_bracket_log, #format_clean_raw, #format_colon_split_strip, #format_dotenv, #format_exporters, #format_free_m, #format_gitlab_status, #format_gitlab_tail, #format_identify_db, #format_json, #format_json_row, #format_json_shell, #format_json_traverse, #format_kube_json, #format_kube_nginx, #format_kube_webservice, #format_multiline_json, #format_nginx, #format_raw, #format_runner_log, #format_shellwords, #format_syslog, #format_syslog_row, #format_table, #format_time, #format_time_fields, #format_time_json, #format_time_numeric, #format_time_parse, #format_time_registry, #format_time_shellwords, #format_time_space, #header_count, #json_shellwords_fallback, #mia, #process_runner_log_row, #process_runner_row_message, #registry_json, #registry_shell_words

Class Method Details

.listObject

Helper for all things that can be hash/value filtered



115
116
117
# File 'lib/greenhat/thing.rb', line 115

def self.list
  all.select.select(&:process?)
end

Instance Method Details

#archive?(archive_names) ⇒ Boolean

Filter / Archive Pattern Matching

Returns:

  • (Boolean)


87
88
89
# File 'lib/greenhat/thing.rb', line 87

def archive?(archive_names)
  archive_names.map(&:to_s).any? { |x| archive.name.include? x }
end

#blank?Boolean

Helper for empty logs

Returns:

  • (Boolean)


92
93
94
# File 'lib/greenhat/thing.rb', line 92

def blank?
  data.blank?
end

#dataObject

Processor



63
64
65
66
67
# File 'lib/greenhat/thing.rb', line 63

def data
  process unless parsed

  result
end

#field_processingObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/greenhat/thing.rb', line 144

def field_processing
  if data.instance_of?(Array)
    data.select { |x| x.instance_of?(Hash) }.map(&:keys).flatten.uniq
  else
    []
  end
rescue StandardError => e
  LogBot.fatal('Process', message: e.message, backtrace: e.backtrace.first)

  []
end

#fieldsObject

Processor



70
71
72
73
74
# File 'lib/greenhat/thing.rb', line 70

def fields
  process unless parsed

  result_fields
end

#formatterObject

Helper Formatter Method



120
121
122
# File 'lib/greenhat/thing.rb', line 120

def formatter
  :"format_#{kind}"
end

#friendly_nameObject



30
31
32
# File 'lib/greenhat/thing.rb', line 30

def friendly_name
  "#{archive.friendly_name.pastel(:blue)} #{name.pastel(:green)}"
end

#grep(term) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/greenhat/thing.rb', line 50

def grep(term)
  selectable = if query?
                 data
               elsif process?
                 hash_to_a_query
               else
                 raw_full
               end

  selectable.select { |r| r.to_s.include?(term) }
end

#hash_to_a_queryObject

Helper to make it easier to query through hashed / parsed objects



46
47
48
# File 'lib/greenhat/thing.rb', line 46

def hash_to_a_query
  data.to_a.map { |x| x.join(': ') }
end

#output(print_it = true) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/greenhat/thing.rb', line 96

def output(print_it = true)
  if print_it
    puts raw.map(&:to_s).map(&:chomp)
  else
    raw.map(&:to_s).map(&:chomp)
  end
end

#process(quiet = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/greenhat/thing.rb', line 124

def process(quiet = false)
  if methods.include? formatter
    spin_start("Parse #{name.pastel(:blue)} #{kind.to_s.pastel(:bright_black)} ") unless quiet
    begin
      send(formatter)
    rescue StandardError => e
      LogBot.fatal('Process', message: e.message, backtrace: e.backtrace.first)
    end
    spin_done unless quiet
  else
    LogBot.fatal('Thing', "No Formatter for #{formatter}")
  end

  self.parsed = true

  self.result_fields = field_processing

  save!
end

#process?Boolean

Hashed values searching

Returns:

  • (Boolean)


105
106
107
# File 'lib/greenhat/thing.rb', line 105

def process?
  kind != :raw && methods.include?(formatter)
end

#query?Boolean

Things that can be queried (Query Helper)

Returns:

  • (Boolean)


110
111
112
# File 'lib/greenhat/thing.rb', line 110

def query?
  data.instance_of?(Array) || data.instance_of?(Enumerator)
end

#query_save(results, name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/greenhat/thing.rb', line 156

def query_save(results, name)
  self.archive = Archive.first
  self.result = results
  self.name = name
  self.log = true
  self.parsed = true
  self.type = 'json'
  self.kind = :json
  self.result_fields = field_processing
  save!
end

#rawObject

Readlines rather than putting whole thing into memory



77
78
79
# File 'lib/greenhat/thing.rb', line 77

def raw
  File.foreach(file)
end

#raw_fullObject

Where full read is needed



82
83
84
# File 'lib/greenhat/thing.rb', line 82

def raw_full
  File.read(file).split("\n")
end

#setupObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/greenhat/thing.rb', line 34

def setup
  self.name = build_path('/')
  self.path = build_path.gsub(/[^\w\s]/, '_')
  self.size = number_to_human_size File.size(file)

  kind_collect # Set Kind and Type
  kind_setup

  save!
end