Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-26 02:50:28 +01:00
added logging for api calls
Dieser Commit ist enthalten in:
Ursprung
1544d4494b
Commit
95ba52f973
10 geänderte Dateien mit 145 neuen und 2 gelöschten Zeilen
|
@ -165,6 +165,19 @@ export default Ember.Controller.extend({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).finally(() => this.set('updating', false));
|
}).finally(() => this.set('updating', false));
|
||||||
|
},
|
||||||
|
|
||||||
|
clearLogs() {
|
||||||
|
ajax(`/admin/wizards/apis/logs/${name.underscore()}`, {
|
||||||
|
type: 'DELETE'
|
||||||
|
}).catch(popupAjaxError)
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
this.transitionToRoute('adminWizardsApis').then(() => {
|
||||||
|
this.send('refreshModel');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).finally(() => this.set('updating', false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -35,7 +35,8 @@ CustomWizardApi.reopenClass({
|
||||||
tokenExpiresAt: authorization.token_expires_at,
|
tokenExpiresAt: authorization.token_expires_at,
|
||||||
tokenRefreshAt: authorization.token_refresh_at,
|
tokenRefreshAt: authorization.token_refresh_at,
|
||||||
endpoints: Ember.A(endpoints),
|
endpoints: Ember.A(endpoints),
|
||||||
isNew: params.isNew
|
isNew: params.isNew,
|
||||||
|
log: params.log
|
||||||
});
|
});
|
||||||
|
|
||||||
return api;
|
return api;
|
||||||
|
|
|
@ -237,3 +237,29 @@
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="wizard-header">
|
||||||
|
{{i18n 'admin.wizard.api.log.label'}}
|
||||||
|
{{d-button action='clearLogs'
|
||||||
|
icon='trash-alt'
|
||||||
|
class='clear-logs'}}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wizard-api-log">
|
||||||
|
<div class="log-list">
|
||||||
|
<table class="wizard-api-log-table">
|
||||||
|
<th>Datetime</th>
|
||||||
|
<th>Status</th>
|
||||||
|
<th>Endpoint</th>
|
||||||
|
<th>Error</th>
|
||||||
|
{{#each api.log as |logentry|}}
|
||||||
|
<tr>
|
||||||
|
<td>{{logentry.time}}</td>
|
||||||
|
<td>{{logentry.status}}</td>
|
||||||
|
<td>{{logentry.endpoint_url}}</td>
|
||||||
|
<td>{{logentry.error}}</td>
|
||||||
|
</tr>
|
||||||
|
{{/each}}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
|
@ -184,6 +184,9 @@ en:
|
||||||
method: "Select a method"
|
method: "Select a method"
|
||||||
url: "Enter a url"
|
url: "Enter a url"
|
||||||
|
|
||||||
|
log:
|
||||||
|
label: "Logs"
|
||||||
|
|
||||||
wizard_js:
|
wizard_js:
|
||||||
location:
|
location:
|
||||||
name:
|
name:
|
||||||
|
|
|
@ -57,11 +57,17 @@ class CustomWizard::ApiController < ::ApplicationController
|
||||||
CustomWizard::Api.remove(api_params[:name])
|
CustomWizard::Api.remove(api_params[:name])
|
||||||
CustomWizard::Api::Authorization.remove(api_params[:name])
|
CustomWizard::Api::Authorization.remove(api_params[:name])
|
||||||
CustomWizard::Api::Endpoint.remove(api_params[:name])
|
CustomWizard::Api::Endpoint.remove(api_params[:name])
|
||||||
|
CustomWizard::Api::LogEntry.clear(api_params[:name])
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clearlogs
|
||||||
|
CustomWizard::Api::LogEntry.clear(api_params[:name])
|
||||||
|
render json: success_json
|
||||||
|
end
|
||||||
|
|
||||||
def redirect
|
def redirect
|
||||||
params.require(:name)
|
params.require(:name)
|
||||||
params.require(:code)
|
params.require(:code)
|
||||||
|
|
|
@ -87,8 +87,13 @@ class CustomWizard::Api::Endpoint
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = connection.request(params)
|
response = connection.request(params)
|
||||||
|
log_params = {time: Time.now, status: 'SUCCESS', endpoint_url: endpoint.url, error: ""}
|
||||||
|
CustomWizard::Api::LogEntry.set(api_name, log_params)
|
||||||
return JSON.parse(response.body)
|
return JSON.parse(response.body)
|
||||||
rescue
|
rescue
|
||||||
|
# TODO: improve error detail
|
||||||
|
log_params = {time: Time.now, status: 'FAILURE', endpoint_url: endpoint.url, error: "API request failed"}
|
||||||
|
CustomWizard::Api::LogEntry.set(api_name, log_params)
|
||||||
return JSON.parse "[{\"error\":\"API request failed\"}]"
|
return JSON.parse "[{\"error\":\"API request failed\"}]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
69
lib/api/logentry.rb
Normale Datei
69
lib/api/logentry.rb
Normale Datei
|
@ -0,0 +1,69 @@
|
||||||
|
class CustomWizard::Api::LogEntry
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
attr_accessor :log_id,
|
||||||
|
:time,
|
||||||
|
:status,
|
||||||
|
:endpoint_url,
|
||||||
|
:error
|
||||||
|
|
||||||
|
def initialize(api_name, data={})
|
||||||
|
@api_name = api_name
|
||||||
|
|
||||||
|
data.each do |k, v|
|
||||||
|
self.send "#{k}=", v if self.respond_to?(k)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set(api_name, new_data)
|
||||||
|
if new_data['log_id']
|
||||||
|
data = self.get(api_name, new_data['log_id'], data_only: true)
|
||||||
|
log_id = new_data['log_id']
|
||||||
|
else
|
||||||
|
data = {}
|
||||||
|
log_id = SecureRandom.hex(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
new_data.each do |k, v|
|
||||||
|
data[k.to_sym] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
PluginStore.set("custom_wizard_api_#{api_name}", "log_#{log_id}", data)
|
||||||
|
|
||||||
|
self.get(api_name, log_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get(api_name, log_id, opts={})
|
||||||
|
return nil if !log_id
|
||||||
|
|
||||||
|
if data = PluginStore.get("custom_wizard_api_#{api_name}", "log_#{log_id}")
|
||||||
|
if opts[:data_only]
|
||||||
|
data
|
||||||
|
else
|
||||||
|
data[:log_id] = log_id
|
||||||
|
self.new(api_name, data)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.remove(api_name)
|
||||||
|
PluginStoreRow.where("plugin_name = 'custom_wizard_api_#{api_name}' AND key LIKE 'log_%'").destroy_all
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.list(api_name)
|
||||||
|
PluginStoreRow.where("plugin_name LIKE 'custom_wizard_api_#{api_name}' AND key LIKE 'log_%'")
|
||||||
|
.map do |record|
|
||||||
|
api_name = record['plugin_name'].sub("custom_wizard_api_", "")
|
||||||
|
data = ::JSON.parse(record['value'])
|
||||||
|
data[:log_id] = record['key'].split('_').last
|
||||||
|
self.new(api_name, data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.clear(api_name)
|
||||||
|
PluginStoreRow.where("plugin_name = 'custom_wizard_api_#{api_name}' AND key LIKE 'log_%'").destroy_all
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -72,6 +72,7 @@ after_initialize do
|
||||||
get 'admin/wizards/apis/:name' => 'api#find'
|
get 'admin/wizards/apis/:name' => 'api#find'
|
||||||
put 'admin/wizards/apis/:name' => 'api#save'
|
put 'admin/wizards/apis/:name' => 'api#save'
|
||||||
delete 'admin/wizards/apis/:name' => 'api#remove'
|
delete 'admin/wizards/apis/:name' => 'api#remove'
|
||||||
|
delete 'admin/wizards/apis/logs/:name' => 'api#clearlogs'
|
||||||
get 'admin/wizards/apis/:name/redirect' => 'api#redirect'
|
get 'admin/wizards/apis/:name/redirect' => 'api#redirect'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -92,12 +93,14 @@ after_initialize do
|
||||||
load File.expand_path('../lib/api/api.rb', __FILE__)
|
load File.expand_path('../lib/api/api.rb', __FILE__)
|
||||||
load File.expand_path('../lib/api/authorization.rb', __FILE__)
|
load File.expand_path('../lib/api/authorization.rb', __FILE__)
|
||||||
load File.expand_path('../lib/api/endpoint.rb', __FILE__)
|
load File.expand_path('../lib/api/endpoint.rb', __FILE__)
|
||||||
|
load File.expand_path('../lib/api/logentry.rb', __FILE__)
|
||||||
load File.expand_path('../controllers/api.rb', __FILE__)
|
load File.expand_path('../controllers/api.rb', __FILE__)
|
||||||
load File.expand_path('../serializers/api/api_serializer.rb', __FILE__)
|
load File.expand_path('../serializers/api/api_serializer.rb', __FILE__)
|
||||||
load File.expand_path('../serializers/api/authorization_serializer.rb', __FILE__)
|
load File.expand_path('../serializers/api/authorization_serializer.rb', __FILE__)
|
||||||
load File.expand_path('../serializers/api/basic_api_serializer.rb', __FILE__)
|
load File.expand_path('../serializers/api/basic_api_serializer.rb', __FILE__)
|
||||||
load File.expand_path('../serializers/api/endpoint_serializer.rb', __FILE__)
|
load File.expand_path('../serializers/api/endpoint_serializer.rb', __FILE__)
|
||||||
load File.expand_path('../serializers/api/basic_endpoint_serializer.rb', __FILE__)
|
load File.expand_path('../serializers/api/basic_endpoint_serializer.rb', __FILE__)
|
||||||
|
load File.expand_path('../serializers/api/log_serializer.rb', __FILE__)
|
||||||
|
|
||||||
::UsersController.class_eval do
|
::UsersController.class_eval do
|
||||||
def wizard_path
|
def wizard_path
|
||||||
|
|
|
@ -2,7 +2,8 @@ class CustomWizard::ApiSerializer < ApplicationSerializer
|
||||||
attributes :name,
|
attributes :name,
|
||||||
:title,
|
:title,
|
||||||
:authorization,
|
:authorization,
|
||||||
:endpoints
|
:endpoints,
|
||||||
|
:log
|
||||||
|
|
||||||
def authorization
|
def authorization
|
||||||
if authorization = CustomWizard::Api::Authorization.get(object.name)
|
if authorization = CustomWizard::Api::Authorization.get(object.name)
|
||||||
|
@ -21,4 +22,13 @@ class CustomWizard::ApiSerializer < ApplicationSerializer
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def log
|
||||||
|
if log = CustomWizard::Api::LogEntry.list(object.name)
|
||||||
|
ActiveModel::ArraySerializer.new(
|
||||||
|
log,
|
||||||
|
each_serializer: CustomWizard::Api::LogSerializer
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
7
serializers/api/log_serializer.rb
Normale Datei
7
serializers/api/log_serializer.rb
Normale Datei
|
@ -0,0 +1,7 @@
|
||||||
|
class CustomWizard::Api::LogSerializer < ApplicationSerializer
|
||||||
|
attributes :log_id,
|
||||||
|
:time,
|
||||||
|
:status,
|
||||||
|
:endpoint_url,
|
||||||
|
:error
|
||||||
|
end
|
Laden …
In neuem Issue referenzieren