From 7efaf25572681e4fa6661b20e393ea2b1e2e9e8c Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Sun, 16 Jun 2019 12:49:51 +0100 Subject: [PATCH] added logging to token refresh, added user column to log and log display --- .../discourse/templates/admin-wizards-api.hbs | 10 ++++++++-- lib/api/authorization.rb | 10 ++++++++-- lib/api/endpoint.rb | 7 ++++--- lib/api/logentry.rb | 15 +++++++++++++-- lib/builder.rb | 2 +- serializers/api/log_serializer.rb | 9 +++++++-- 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/assets/javascripts/discourse/templates/admin-wizards-api.hbs b/assets/javascripts/discourse/templates/admin-wizards-api.hbs index 6a0a8067..160225ea 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-api.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-api.hbs @@ -260,14 +260,20 @@
+ - + {{#each api.log as |logentry|}} + - + {{/each}} diff --git a/lib/api/authorization.rb b/lib/api/authorization.rb index 9dd51d50..f6a11794 100644 --- a/lib/api/authorization.rb +++ b/lib/api/authorization.rb @@ -116,8 +116,14 @@ class CustomWizard::Api::Authorization :method => 'GET', :query => URI.encode_www_form(body) ) - - result = connection.request() + begin + result = connection.request() + log_params = {time: Time.now, user_id: 0, status: 'SUCCESS', url: token_url, error: ""} + CustomWizard::Api::LogEntry.set(name, log_params) + rescue + log_params = {time: Time.now, user_id: 0, status: 'FAILURE', url: token_url, error: "Token refresh request failed"} + CustomWizard::Api::LogEntry.set(name, log_params) + end self.handle_token_result(name, result) end diff --git a/lib/api/endpoint.rb b/lib/api/endpoint.rb index 356687e7..3036a49f 100644 --- a/lib/api/endpoint.rb +++ b/lib/api/endpoint.rb @@ -62,7 +62,7 @@ class CustomWizard::Api::Endpoint end end - def self.request(api_name, endpoint_id, body) + def self.request(user, api_name, endpoint_id, body) endpoint = self.get(api_name, endpoint_id) auth = CustomWizard::Api::Authorization.get_header_authorization_string(api_name) @@ -87,12 +87,13 @@ class CustomWizard::Api::Endpoint begin response = connection.request(params) - log_params = {time: Time.now, status: 'SUCCESS', endpoint_url: endpoint.url, error: ""} + log_params = {time: Time.now, user_id: user.id, status: 'SUCCESS', url: endpoint.url, error: ""} + CustomWizard::Api::LogEntry.set(api_name, log_params) return JSON.parse(response.body) rescue # TODO: improve error detail - log_params = {time: Time.now, status: 'FAILURE', endpoint_url: endpoint.url, error: "API request failed"} + log_params = {time: Time.now, user_id: user.id, status: 'FAILURE', url: endpoint.url, error: "API request failed"} CustomWizard::Api::LogEntry.set(api_name, log_params) return JSON.parse "[{\"error\":\"API request failed\"}]" end diff --git a/lib/api/logentry.rb b/lib/api/logentry.rb index 083262aa..6b7831bd 100644 --- a/lib/api/logentry.rb +++ b/lib/api/logentry.rb @@ -3,9 +3,14 @@ class CustomWizard::Api::LogEntry attr_accessor :log_id, :time, + :user_id, :status, - :endpoint_url, - :error + :url, + :error, + :username, + :userpath, + :name, + :avatar_template def initialize(api_name, data={}) @api_name = api_name @@ -58,6 +63,12 @@ class CustomWizard::Api::LogEntry api_name = record['plugin_name'].sub("custom_wizard_api_", "") data = ::JSON.parse(record['value']) data[:log_id] = record['key'].split('_').last + this_user = User.find_by(id: data['user_id']) + data[:user_id] = this_user.id || nil + data[:username] = this_user.username || "" + data[:userpath] = "/u/#{this_user.username_lower}/activity" + data[:name] = this_user.name || "" + data[:avatar_template] = "/user_avatar/default/#{this_user.username_lower}/97/#{this_user.uploaded_avatar_id}.png" self.new(api_name, data) end end diff --git a/lib/builder.rb b/lib/builder.rb index 7fd4fa6d..c2be9282 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -406,7 +406,7 @@ class CustomWizard::Builder api_body = CustomWizard::Builder.fill_placeholders(JSON.generate(api_body_parsed), user, data) end - result = CustomWizard::Api::Endpoint.request(action['api'], action['api_endpoint'], api_body) + result = CustomWizard::Api::Endpoint.request(user, action['api'], action['api_endpoint'], api_body) if error = result['error'] || (result[0] && result[0]['error']) error = error['message'] || error diff --git a/serializers/api/log_serializer.rb b/serializers/api/log_serializer.rb index 754c15f3..13ee1555 100644 --- a/serializers/api/log_serializer.rb +++ b/serializers/api/log_serializer.rb @@ -2,6 +2,11 @@ class CustomWizard::Api::LogSerializer < ApplicationSerializer attributes :log_id, :time, :status, - :endpoint_url, - :error + :url, + :error, + :user_id, + :username, + :userpath, + :name, + :avatar_template end
DatetimeUser StatusEndpointURL Error
{{logentry.time}} + + {{logentry.status}}{{logentry.endpoint_url}}{{logentry.url}} {{logentry.error}}