Class: FlatKit::Command::Stats

Inherits:
FlatKit::Command show all
Defined in:
lib/flat_kit/command/stats.rb

Instance Attribute Summary

Attributes inherited from FlatKit::Command

#argv, #env, #logger, #opts, #readers, #writer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlatKit::Command

for, #initialize, names

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Constructor Details

This class inherits a constructor from FlatKit::Command

Class Method Details

.descriptionObject



9
10
11
# File 'lib/flat_kit/command/stats.rb', line 9

def self.description
  "Collect and report stats on the inputfile"
end

.nameObject



5
6
7
# File 'lib/flat_kit/command/stats.rb', line 5

def self.name
  "stats"
end

.parserObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/flat_kit/command/stats.rb', line 13

def self.parser
  ::Optimist::Parser.new do
    banner "#{Sort.description}"
    banner ""

    banner "    Given an input file collect basic statistics.\n\n    The statistics can vary based upon the datatype of the field.\n\n    Numeric fields will report the basic count, min, max, mean, standard deviation and sum.\n    Non-numeric fields that are comparable, like dates, will report count, min and max.\n    Other non-numeric fields will only report the count.\n\n    Adding --cardinality will report the count, and frequency of distinct values in the result.\n    This will allow for reporting the median value.\n\n    The fields upon which stats are collected may be selected with the --fields parameter.\n    By default statistics are collected on all fields.\n\n    The flatfile type(s) will be automatically determined by the file name.\n\n    The output can be dumped as a CSV, JSON or a a formated ascii table.\n\n    BANNER\n\n    banner <<~USAGE\n\n    Usage:\n      fk stats --everything file.json\n      fk stats --select surname,given_name file.csv\n      fk stats --select surname,given_name --output-format json file.csv > stats.json\n      fk stats --select field1,field2 --output-format json input.csv\n      fk stats --select field1 file.json.gz -o stats.csv\n      gunzip -c file.json.gz | fk stats --input-format json --output-format text\n\n    USAGE\n\n    banner <<~OPTIONS\n\n    Options:\n\n    OPTIONS\n\n    opt :output, \"Send the output to the given path instead of standard out.\", default: \"<stdout>\"\n    opt :input_format, \"Input format, csv or json\", default: \"auto\", short: :none\n    opt :output_format, \"Output format, csv or json\", default: \"auto\", short: :none\n    opt :select, \"The comma separted list of field(s) to report stats on\", required: false, type: :string\n    opt :everything, \"Show all statistics that are possible\", default: false\n    opt :cardinality, \"Show the cardinality of the fields, this requires additional memory\", default: false\n  end\nend\n"

Instance Method Details

#callObject



89
90
91
# File 'lib/flat_kit/command/stats.rb', line 89

def call
  @stats.call
end

#parseObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flat_kit/command/stats.rb', line 66

def parse
  parser = self.class.parser
  ::Optimist::with_standard_exception_handling(parser) do
    begin
      opts = parser.parse(argv)
      fields = ::FlatKit::Stats::AllFields
      fields = CSV.parse_line(opts[:select]) if opts[:select]

      stats = [FieldStats::CORE_STATS]
      stats << FieldStats::CARDINALITY_STATS if opts[:cardinality] || opts[:everything]

      paths = parser.leftovers
      raise ::Optimist::CommandlineError, "1 and only 1 input file is allowed" if paths.size > 1
      path = paths.first || "-" # default to stdin
      @stats = ::FlatKit::Stats.new(input: path, input_fallback: opts[:input_format],
                                   output: opts[:output], output_fallback: opts[:output_format],
                                   fields_to_stat: fields, stats_to_collect: stats)
    rescue ::FlatKit::Error => e
      raise ::Optimist::CommandlineError, e.message
    end
  end
end