Class: AppCommand::ProductionLogs

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Includes:
Columnist
Defined in:
lib/routes/production_logs.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/routes/production_logs.rb', line 10

def execute

    @opts = command_options
    @args = arguments

    @ec2_connection = App::MySQL::ec2
    @terminal_width = App::Terminal::get_terminal_width

    opts_validate
    opts_routing

end

#opts_routingObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/routes/production_logs.rb', line 56

def opts_routing

    if @opts[:versionList]
        show_version_list
    elsif @opts[:watchPHP]
        watch_php
    elsif @opts[:watchJava]
        watch_java
    end

end

#opts_validateObject



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
# File 'lib/routes/production_logs.rb', line 23

def opts_validate

    watch = 0
    watch = watch + 1 if @opts[:watchPHP]
    watch = watch + 1 if @opts[:watchJava]

    if watch >= 2
        App::Terminal::error('You can only watch one set of logs at a time', "Please set only 1 one of the following flags: #{App::Terminal::format_flag('p', false)} #{App::Terminal::format_flag('j', false)}", true)
    elsif watch == 0
        @opts[:watchPHP] = true
    end

    if @opts[:versionList]
        if App::UtilsRoutes::flags_set(@opts) > 1
            App::Terminal::error('Invalid number of flags', "When using the #{App::Terminal::format_flag('V')}, no other flags should be set.", true)
        end
    end

    if @opts[:compare]
        [@args[0], @args[1]].each do |version_number|
            if version_number.nil?
                App::Terminal::error('Missing arguments', ["When using the #{App::Terminal::format_flag('c')} the system expects #{App::Terminal::format_highlight('2 arguments ')} \xe2\x80\x94 application version numbers.", "An example of a valid command would be: #{App::Terminal::format_command('bp p l -c 4.76.6 4.78.4')}"], true)
            end
            validate_version_number(version_number)
        end
    else
        unless @args[0].nil?
            validate_version_number(@args[0])
        end
    end

end

#show_version_listObject



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

def show_version_list
    versions_array = []
    sql = 'SELECT account_version FROM brightpearl.php_logs GROUP BY account_version;'
    versions = @ec2_connection.query(sql)
    versions.each do|result|
        versions_array << "         \x1B[38;5;154m#{result['account_version']}\x1B[0m"
    end
    App::Terminal::info('Versions you can compare to are:', versions_array)
end

#watch_javaObject



138
139
140
141
142
143
# File 'lib/routes/production_logs.rb', line 138

def watch_java

    App::Terminal::info('Not yet implemented', 'Please wait for Albert to program this and try again later.')
    exit

end

#watch_phpObject



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
# File 'lib/routes/production_logs.rb', line 68

def watch_php

    unless @opts[:skipFetch]
        fetch_php_logs
    end

    begin

        data_previous = nil
        if @opts[:compare]
            data_previous = []
            sql_1 = "SELECT COUNT(*) AS frequency, account_version, error_message, error_file, error_line FROM brightpearl.php_logs WHERE account_version='#{@args[0]}' GROUP BY error_message, error_file, error_line ORDER BY frequency DESC"
            data_previous_result = @ec2_connection.query(sql_1)
            data_previous_result.each do |result|
                data_previous << result
            end
            sql_2 = "SELECT COUNT(*) AS frequency, account_version, error_message, error_file, error_line FROM brightpearl.php_logs WHERE account_version='#{@args[1]}' GROUP BY error_message, error_file, error_line HAVING frequency > 1 ORDER BY frequency DESC"
            data = @ec2_connection.query(sql_2)
        else
            if @args[0].nil?
                sql = 'SELECT COUNT(*) AS frequency, account_version, error_message, error_file, error_line FROM brightpearl.php_logs GROUP BY error_file, error_line ORDER BY frequency DESC'
            else
                sql = "SELECT COUNT(*) AS frequency, account_version, error_message, error_file, error_line FROM brightpearl.php_logs WHERE account_version='#{@args[0]}' GROUP BY error_file, error_line ORDER BY frequency DESC"
            end
            data = @ec2_connection.query(sql)
        end

        column_width_1 = 5
        column_width_2 = 8
        column_width_3 = ((@terminal_width - 21).to_f * 0.7).round
        column_width_4 = (@terminal_width - (column_width_1 + column_width_2 + column_width_3 + 5)) - 5
        column_width_5 = 5

        puts # PUTS SPACE BEFORE TABLE
        table(:border => false) do
            row do
                column('', :width => column_width_1, :color => 255, :align => 'right')
                column('Version', :width => column_width_2, :color => 255, :align => 'right')
                column('Error', :width => column_width_3, :color => 255)
                column('', :width => column_width_4, :color => 255, :align => 'right')
                column('', :width => column_width_5, :color => 255, :align => 'left')
            end
            row do
                column('')
                column('')
                column('')
                column('')
                column('')
            end
            data.each do |row_data|
                row_color = get_color_php_logs(data_previous, row_data)
                error_file = row_data['error_file'].split('/')
                error_file = "#{error_file[error_file.count - 2]}/#{error_file[error_file.count - 1]}"
                row do
                    column(row_data['frequency'], :color => row_color)
                    column(row_data['account_version'], :color => row_color)
                    column(row_data['error_message'][0..(column_width_3 - 1)], :color => row_color)
                    column(error_file[0..(column_width_4 - 1)], :color => row_color)
                    column(row_data['error_line'], :color => row_color)
                end
            end
        end
        puts # PUTS SPACE AFTER TABLE

    rescue Exception => e
        App::Terminal::error('Something went wrong', "#{e.message}", true)
    end

end