Module: Lesli::Interfaces::Application::Logger

Included in:
ApplicationDeviseController, ApplicationLesliController
Defined in:
app/controllers/lesli/interfaces/application/logger.rb

Instance Method Summary collapse

Instance Method Details

#get_user_agent(as_string = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/lesli/interfaces/application/logger.rb', line 37

def get_user_agent(as_string=true)

    http_user_agent = request.env["HTTP_USER_AGENT"]

    # parse user agent
    user_agent = UserAgent.parse(http_user_agent)

    user_agent_version = user_agent.version.to_a.first(2).join(".")

    # return user agent as object
    if as_string == false
        return {
            platform: user_agent.platform,
            os: user_agent.os,
            browser: user_agent.browser,
            version: user_agent_version
        }
    end

    # return user agent info as string
    "#{user_agent.platform} #{user_agent.os} - #{user_agent.browser} #{user_agent_version}"
end

#log_account_activity(system_module, system_process, title = nil, payload = nil, description = nil) ⇒ Object

Track specific account activity this is disabled by default in the settings file



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/lesli/interfaces/application/logger.rb', line 118

def (system_module, system_process, title = nil, payload = nil, description = nil)
    # return unless Rails.application.config.lesli.dig(:security, :enable_analytics)

    # account = Account.first

    # account.activities.create({
    #     system_module:,
    #     system_process:,
    #     description:,
    #     payload:,
    #     title:
    # })
end

#log_account_requestsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/lesli/interfaces/application/logger.rb', line 65

def 
    return unless Lesli.config.security.dig(:enable_analytics)
    return unless defined?(LesliAudit)
    return unless current_user

    # Try to save a unique record for this request configuration
    current_user..audit..upsert(
        {
            request_controller: controller_path,
            request_action: action_name,
            request_count: 1
        },
        
        # group of columns to consider a request as unique
        unique_by: %i[request_controller request_action created_at account_id],

        # if request id is not unique, increase the counter for this configuration
        on_duplicate: Arel.sql("request_count = lesli_audit_account_requests.request_count + 1")
    )
end

#log_requestsObject



60
61
62
63
# File 'app/controllers/lesli/interfaces/application/logger.rb', line 60

def log_requests
    
    log_user_requests
end

#log_user_requestsObject

Track all user activity this is disabled by default in the settings file



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
# File 'app/controllers/lesli/interfaces/application/logger.rb', line 88

def log_user_requests
    return unless Lesli.config.security.dig(:enable_analytics)
    return unless defined?(LesliAudit)
    return unless current_user
    return unless session[:user_session_id]
    
    # Try to save a unique record for this request configuration
    current_user..audit.user_requests.upsert(
        {
            request_controller: controller_path,
            request_action: action_name,
            session_id: session[:user_session_id],
            user_id: current_user.id,
            request_count: 1
        },
                
        # group of columns to consider a request as unique
        unique_by: %i[request_controller request_action created_at user_id session_id],

        # if request id is not unique
        #   - increase the counter for this configuration
        #   - update the datetime of the last request
        on_duplicate: Arel.sql(
            'request_count = lesli_audit_user_requests.request_count + 1'
        )
    )
end