2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2020-04-15 02:46:44 +02:00
|
|
|
class CustomWizard::Log
|
|
|
|
include ActiveModel::Serialization
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-09 08:07:12 +02:00
|
|
|
attr_reader :date, :wizard_id, :action, :username, :message
|
|
|
|
attr_accessor :user
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-04-15 02:46:44 +02:00
|
|
|
PAGE_LIMIT = 100
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-04-15 02:46:44 +02:00
|
|
|
def initialize(attrs)
|
2024-10-16 13:52:03 +02:00
|
|
|
@date = attrs["date"]
|
|
|
|
@action = attrs["action"]
|
|
|
|
@message = attrs["message"]
|
|
|
|
@wizard_id = attrs["wizard_id"]
|
|
|
|
@username = attrs["username"]
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2023-05-01 22:49:44 +02:00
|
|
|
def self.create(wizard_id, action, username, message, date = Time.now)
|
2020-04-15 02:46:44 +02:00
|
|
|
log_id = SecureRandom.hex(12)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
PluginStore.set(
|
|
|
|
"custom_wizard_log",
|
2020-04-15 04:34:39 +02:00
|
|
|
log_id.to_s,
|
2024-10-16 13:52:03 +02:00
|
|
|
{ date: date, wizard_id: wizard_id, action: action, username: username, message: message },
|
2020-04-15 02:46:44 +02:00
|
|
|
)
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-09 08:07:12 +02:00
|
|
|
def self.list_query(wizard_id = nil)
|
2024-10-16 13:52:03 +02:00
|
|
|
query =
|
|
|
|
PluginStoreRow.where(
|
|
|
|
"plugin_name = 'custom_wizard_log' AND (value::json->'date') IS NOT NULL",
|
|
|
|
)
|
2021-09-09 08:07:12 +02:00
|
|
|
query = query.where("(value::json->>'wizard_id') = ?", wizard_id) if wizard_id
|
|
|
|
query.order("value::json->>'date' DESC")
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-09 08:07:12 +02:00
|
|
|
def self.list(page = 0, limit = nil, wizard_id = nil)
|
2020-11-03 01:24:20 +01:00
|
|
|
limit = limit.to_i > 0 ? limit.to_i : PAGE_LIMIT
|
|
|
|
page = page.to_i
|
2021-09-09 08:07:12 +02:00
|
|
|
logs = self.list_query(wizard_id)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-09 08:07:12 +02:00
|
|
|
result = OpenStruct.new(logs: [], total: nil)
|
|
|
|
result.total = logs.size
|
2024-10-16 13:52:03 +02:00
|
|
|
result.logs = logs.limit(limit).offset(page * limit).map { |r| self.new(JSON.parse(r.value)) }
|
2021-09-09 08:07:12 +02:00
|
|
|
|
|
|
|
result
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
end
|