Class: Ragdoll::CLI::Analytics

Inherits:
Thor
  • Object
show all
Defined in:
lib/ragdoll/cli/commands/analytics.rb

Instance Method Summary collapse

Instance Method Details

#cleanupObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ragdoll/cli/commands/analytics.rb', line 146

def cleanup
  client = StandaloneClient.new
  
  days = (options && options[:days]) || 30
  dry_run = if options && options.key?(:dry_run)
              options[:dry_run]
            else
              true  # Default to true
            end
  force = (options && options[:force]) == true
  
  cleanup_options = {
    days: days,
    dry_run: dry_run && !force
  }

  if cleanup_options[:dry_run]
    puts "DRY RUN: Showing what would be cleaned up (use --force to actually clean up)"
    puts
  else
    puts "Performing actual cleanup of search records older than #{days} days..."
    puts
  end

  result = client.cleanup_searches(**cleanup_options)

  if result.is_a?(Hash)
    puts "Cleanup Results:"
    puts "  Orphaned searches: #{result[:orphaned_count] || 0}"
    puts "  Old unused searches: #{result[:unused_count] || 0}"
    puts "  Total cleaned: #{(result[:orphaned_count] || 0) + (result[:unused_count] || 0)}"
  else
    puts "Searches cleaned up: #{result}"
  end

  if cleanup_options[:dry_run]
    puts
    puts "Use --force to actually perform the cleanup"
  end
rescue StandardError => e
  puts "Error during cleanup: #{e.message}"
end

#historyObject



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
# File 'lib/ragdoll/cli/commands/analytics.rb', line 47

def history
  client = StandaloneClient.new
  limit = (options && options[:limit]) || 20
  filter_options = {}
  filter_options[:user_id] = options[:user_id] if options && options[:user_id]
  filter_options[:session_id] = options[:session_id] if options && options[:session_id]

  searches = client.search_history(limit: limit, **filter_options)

  case (options && options[:format]) || 'table'
  when 'json'
    puts JSON.pretty_generate(searches || [])
  when 'plain'
    if searches.nil? || searches.empty?
      puts 'No search history found.'
      return
    end
    searches.each_with_index do |search, index|
      puts "#{index + 1}. #{search[:query]} (#{search[:search_type]})"
      puts "   Time: #{search[:created_at]}"
      puts "   Results: #{search[:results_count]}"
      puts "   Execution: #{search[:execution_time_ms]}ms"
      puts "   Session: #{search[:session_id]}" if search[:session_id]
      puts "   User: #{search[:user_id]}" if search[:user_id]
      puts
    end
  else
    # Table format
    if searches.nil? || searches.empty?
      puts 'No search history found.'
      return
    end
    
    puts "Recent Search History (#{searches.length} searches):"
    puts
    puts 'Time'.ljust(20) + 'Query'.ljust(30) + 'Type'.ljust(10) + 'Results'.ljust(8) + 'Time(ms)'
    puts '-' * 80

    searches.each do |search|
      time = format_time(search[:created_at]).ljust(20)
      query = (search[:query] || 'N/A')[0..29].ljust(30)
      type = (search[:search_type] || 'N/A')[0..9].ljust(10)
      results = (search[:results_count] || 0).to_s.ljust(8)
      exec_time = (search[:execution_time_ms] || 0).to_s

      puts "#{time}#{query}#{type}#{results}#{exec_time}"
    end
  end
rescue StandardError => e
  puts "Error retrieving search history: #{e.message}"
end

#overviewObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ragdoll/cli/commands/analytics.rb', line 14

def overview
  client = StandaloneClient.new
  days = (options && options[:days]) || 30
  analytics = client.search_analytics(days: days)

  case (options && options[:format]) || 'table'
  when 'json'
    puts JSON.pretty_generate(analytics)
  else
    puts "Search Analytics (last #{days} days):"
    puts
    puts 'Metric'.ljust(30) + 'Value'
    puts '-' * 50
    
    analytics.each do |key, value|
      metric = format_metric_name(key).ljust(30)
      formatted_value = format_metric_value(key, value)
      puts "#{metric}#{formatted_value}"
    end
  end
rescue StandardError => e
  puts "Error retrieving analytics: #{e.message}"
end


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
# File 'lib/ragdoll/cli/commands/analytics.rb', line 106

def trending
  client = StandaloneClient.new
  limit = (options && options[:limit]) || 10
  days = (options && options[:days]) || 7
  trending_queries = client.trending_queries(limit: limit, days: days)

  case (options && options[:format]) || 'table'
  when 'json'
    puts JSON.pretty_generate(trending_queries || [])
  else
    if trending_queries.nil? || trending_queries.empty?
      puts "No trending queries found for the last #{days} days."
      return
    end
    
    puts "Trending Search Queries (last #{days} days):"
    puts
    puts 'Rank'.ljust(5) + 'Query'.ljust(40) + 'Count'.ljust(8) + 'Avg Results'
    puts '-' * 80

    trending_queries.each_with_index do |query_data, index|
      rank = (index + 1).to_s.ljust(5)
      query = (query_data[:query] || 'N/A')[0..39].ljust(40)
      count = (query_data[:count] || 0).to_s.ljust(8)
      avg_results = (query_data[:avg_results] || 0).round(1).to_s

      puts "#{rank}#{query}#{count}#{avg_results}"
    end
  end
rescue StandardError => e
  puts "Error retrieving trending queries: #{e.message}"
end