From b5d3a283093ff2acebb84e096f75b9af7f035689 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sun, 22 Sep 2024 01:20:34 +0400 Subject: [PATCH] active_record 2 --- app/controllers/scores_controller.rb | 9 ++- lib/live/definable.rb | 5 ++ lib/semantic/ansi_colors.rb | 4 ++ lib/semantic/subscribers/action_controller.rb | 3 +- lib/semantic/subscribers/active_record.rb | 55 +++++++++++++++---- 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index d711872..dba92e0 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -37,12 +37,11 @@ class ScoresController < ApplicationController end def destroy - aazazaz - # @score.destroy! - # redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other + @score.destroy! + redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other - flash.now[:alert] = "this is 'destroyed!'" - render turbo_stream: turbo_stream.replace('notification', partial: 'layouts/notification') + # flash.now[:alert] = "this is 'destroyed!'" + # render turbo_stream: turbo_stream.replace('notification', partial: 'layouts/notification') end private diff --git a/lib/live/definable.rb b/lib/live/definable.rb index 4e44199..b5982b1 100644 --- a/lib/live/definable.rb +++ b/lib/live/definable.rb @@ -1,6 +1,7 @@ require 'dotenv' DEFINABLE_THREAD_GROUP ||= ThreadGroup.new # rubocop:disable Lint/OrAssignmentToConstant +DEFINABLE_LISTENERS ||= [] # rubocop:disable Lint/OrAssignmentToConstant module Live # offers typed constant defintions with default value, by using lots of introspecting... @@ -96,13 +97,17 @@ module Live end def start_listener + DEFINABLE_LISTENERS.each(&:stop) + DEFINABLE_LISTENERS.clear DEFINABLE_THREAD_GROUP.list.each(&:kill) + DEFINABLE_THREAD_GROUP.add(Thread.new do listener = Listen.to(Rails.root, only: /^\.env\.?/) do @@class_origin.reload_from_env rescue StandardError nil end + DEFINABLE_LISTENERS << listener listener.start end) end diff --git a/lib/semantic/ansi_colors.rb b/lib/semantic/ansi_colors.rb index 924f0ea..10a6ffb 100644 --- a/lib/semantic/ansi_colors.rb +++ b/lib/semantic/ansi_colors.rb @@ -18,6 +18,10 @@ module Semantic TEXT_CYAN = "\e[36m".freeze TEXT_WHITE = "\e[37m".freeze + # TEXT EXTRA COLOR NAME + TEXT__PINK = "\e[38;5;175m".freeze + TEXT__ORANGE = "\e[38;5;202m".freeze + # TEXT GRAY SHADES TEXT_GRAY_800 = "\e[38;5;232m".freeze TEXT_GRAY_700 = "\e[38;5;233m".freeze diff --git a/lib/semantic/subscribers/action_controller.rb b/lib/semantic/subscribers/action_controller.rb index e5c230b..e477ab4 100644 --- a/lib/semantic/subscribers/action_controller.rb +++ b/lib/semantic/subscribers/action_controller.rb @@ -63,7 +63,8 @@ module Semantic logger.warn process_duration(event, additions) elsif event.duration >= 300 logger.info process_duration(event, additions) - elsif event.duration >= 100 + # elsif event.duration >= 100 + else logger.debug process_duration(event, additions) end diff --git a/lib/semantic/subscribers/active_record.rb b/lib/semantic/subscribers/active_record.rb index cfb3e03..dd4576e 100644 --- a/lib/semantic/subscribers/active_record.rb +++ b/lib/semantic/subscribers/active_record.rb @@ -5,28 +5,49 @@ module Semantic include AnsiColors IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze + TRANSACTION_TAINT = AnsiColors::DARK_TEXT_CYAN def sql(event) name = event.payload[:name] return if IGNORE_PAYLOAD_NAMES.include?(name) - if name.end_with?('Load') - name = event.payload[:cached] ? "#{name} (cached)" : "#{name} (#{event.payload[:row_count]})" + category, model, *remaining = name.split.reverse + return if category == 'TRANSACTION' + + case category + when 'Count' + statement_taint = AnsiColors::TEXT_GRAY_300 + name = "#{category} #{model}" + when 'Load' + name = if event.payload[:cached] + statement_taint = AnsiColors::TEXT_GRAY_300 + name = "Cache #{model}" + else + statement_taint = TEXT_BLUE + row_count = event.payload[:row_count] + name = "#{category} #{row_count} #{model.pluralize(row_count)}" + end + when 'Update', 'Create', 'Destroy' + name = "#{category} #{model}" + category_taint = TRANSACTION_TAINT + statement_taint = TEXT__PINK + increment_transaction_local(event.payload[:transaction].uuid, category.downcase.to_sym) + else raise "unknown sql category: <#{category}>" end - name = colorize(name, TEXT_CYAN) - sql = colorize(event.payload[:sql], TEXT_BLUE) - logger.debug("#{name} #{sql}") + name = "#{name} #{remaining.join(' ')}" if remaining.any? + name = colorize(name, category_taint) if category_taint + statement = colorize(event.payload[:sql], statement_taint) + logger.debug("#{name} #{statement}") end def start_transaction(event) - short_uuid = short_uuid(event.payload[:transaction].uuid) - logger.info("TRANSACTION Begin #{short_uuid}") + logger.info(colorize('Begin', TRANSACTION_TAINT)) end def transaction(event) - outcome = event.payload[:outcome] - short_uuid = short_uuid(event.payload[:transaction].uuid) - logger.info("TRANSACTION #{outcome.capitalize} #{short_uuid}") + outcome = colorize(event.payload[:outcome].capitalize, TRANSACTION_TAINT) + summary = transaction_summary(event.payload[:transaction]) + logger.info("#{outcome} (#{summary})") end def instantiation(event); end @@ -35,7 +56,19 @@ module Semantic private - def short_uuid(uuid) = uuid.split('-').first + def transaction_summary(transaction) + transaction_local(transaction.uuid) + .select { |_, value| value.positive? } + .map { |k, v| "#{v} #{k.to_s.pluralize(v)}" } + .join(',') + end + + def increment_transaction_local(transaction_id, sql_command) = transaction_local(transaction_id)[sql_command] += 1 + def thread_local = Thread.current[self.class.to_s] ||= {} + + def transaction_local(transaction_id) + thread_local[transaction_id] ||= { update: 0, create: 0, destroy: 0 } + end end end end