Class: Charted::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/charted/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_loadedObject

Returns the value of attribute config_loaded.



3
4
5
# File 'lib/charted/command.rb', line 3

def config_loaded
  @config_loaded
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/charted/command.rb', line 3

def output
  @output
end

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/charted/command.rb', line 4

def site
  @site
end

Instance Method Details

#clean(label = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/charted/command.rb', line 6

def clean(label=nil)
  load_config
  sys_exit("Please set 'delete_after' config.") if Charted.config.delete_after.nil?

  threshold = Date.today - Charted.config.delete_after
  Visit.where { created_at < threshold }.delete
  Event.where { created_at < threshold }.delete
  Conversion.where { created_at < threshold }.delete
  Experiment.where { created_at < threshold }.delete
  Visitor.where { created_at < threshold }.each do |visitor|
    visitor.delete if visitor.visits.count == 0 &&
      visitor.events.count == 0 &&
      visitor.conversions.count == 0 &&
      visitor.experiments.count == 0
  end

  if label
    Event.where(label: label).delete
    Conversion.where(label: label).delete
    Experiment.where(label: label).delete
  end
end

#dashboardObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/charted/command.rb', line 29

def dashboard
  site_required
  nodes = []
  max_width = [`tput cols`.to_i / 2, 60].min
  table = Dashes::Table.new.
    align(:right, :right, :left).
    row('Total', 'Unique', 'Visits').
    separator
  (0..11).each do |delta|
    date = Charted.prev_month(Date.today, delta)
    query = Charted::Visit.
      join(:visitors, id: :visitor_id).
      where(visitors__site_id: @site.id).
      where('visits.created_at >= ? AND visits.created_at < ?', date, Charted.next_month(date))
    visits = query.count
    unique = query.select(:visitor_id).distinct.count
    table.row(format(visits), format(unique), date.strftime('%B %Y'))
  end
  nodes += [table]
  [[:browser, 'Browsers', Charted::Visitor],
   [:resolution, 'Resolutions', Charted::Visitor],
   [:platform, 'Platforms', Charted::Visitor],
   [:country, 'Countries', Charted::Visitor],
   [:title, 'Pages', Charted::Visit],
   [:referrer, 'Referrers', Charted::Visit],
   [:search_terms, 'Searches', Charted::Visit]].each do |field, column, type|
    table = Dashes::Table.new.
      max_width(max_width).
      spacing(:min, :min, :max).
      align(:right, :right, :left).
      row('Total', '%', column).separator
    rows = []
    query = type.exclude(field => nil)
    query = query.join(:visitors, id: :visitor_id) if type == Charted::Visit
    query = query.where(visitors__site_id: @site.id)
    total = query.count
    query.group_and_count(field).each do |row|
      count = row[:count]
      label = row[field].to_s.strip
      next if label == ""
      label = "#{label[0..37]}..." if label.length > 40
      rows << [format(count), "#{((count / total.to_f) * 100).round}%", label]
    end
    add_truncated(table, rows)
    nodes << table
  end
  table = Dashes::Table.new.
    max_width(max_width).
    spacing(:min, :min, :max).
    align(:right, :right, :left).
    row('Total', 'Unique', 'Events').
    separator
  rows = []
  events = Charted::Event.join(:visitors, id: :visitor_id).where(visitors__site_id: @site.id)
  events.group_and_count(:label).all.each do |row|
    label, count = row[:label], row[:count]
    unique = Charted::Visitor.
      join(:events, visitor_id: :id).
      where(site_id: @site.id, events__label: label).
      select(:visitors__id).distinct.count
    rows << [format(count), format(unique), label]
  end
  add_truncated(table, rows)
  nodes << table

  table = Dashes::Table.new.
    max_width(max_width).
    spacing(:min, :min, :max).
    align(:right, :right, :left).
    row('Start', 'End', 'Conversions').
    separator
  rows = []
  conversions = Charted::Conversion.join(:visitors, id: :visitor_id).where(visitors__site_id: @site.id)
  conversions.group_and_count(:label).all.each do |row|
    label, count = row[:label], row[:count]
    ended = Charted::Visitor.
      join(:conversions, visitor_id: :id).
      where(site_id: @site.id, conversions__label: label).
      exclude(conversions__ended_at: nil).
      select(:visitors__id).distinct.count
    rows << [format(count), format(ended), label]
  end
  add_truncated(table, rows)
  nodes << table

  table = Dashes::Table.new.
    max_width(max_width).
    spacing(:min, :min, :max).
    align(:right, :right, :left).
    row('Start', 'End', 'Experiments').
    separator
  rows = []
  experiments = Charted::Experiment.join(:visitors, id: :visitor_id).where(visitors__site_id: @site.id)
  experiments.group_and_count(:label, :experiments__bucket).all.each do |row|
    label, bucket, count = row[:label], row[:bucket], row[:count]
    ended = Charted::Visitor.
      join(:experiments, visitor_id: :id).
      where(site_id: @site.id, experiments__label: label, experiments__bucket: bucket).
      exclude(experiments__ended_at: nil).
      select(:visitors__id).distinct.count
    rows << [format(count), format(ended), "#{label}: #{bucket}"]
  end
  add_truncated(table, rows)
  nodes << table

  nodes.reject! do |node|
    minimum = node.is_a?(Dashes::Table) ? 1 : 0
    node.instance_variable_get(:@rows).size == minimum # TODO: hacked
  end
  print(Dashes::Grid.new.width(`tput cols`.to_i).add(*nodes))
end

#jsObject



141
142
143
# File 'lib/charted/command.rb', line 141

def js
  print(File.read(JS_FILE))
end

#migrateObject



145
146
147
148
149
150
151
152
153
# File 'lib/charted/command.rb', line 145

def migrate
  load_config
  Charted::Migrate.run
  Charted.config.sites.each do |domain|
    if Site.first(domain: domain).nil?
      Site.create(domain: domain)
    end
  end
end