hooktrace 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "monitor"
5
+
6
+ module Hooktrace
7
+ module Storage
8
+ class ActiveRecordStore
9
+ @model_mutex = Monitor.new
10
+
11
+ class << self
12
+ attr_reader :model_mutex
13
+ end
14
+
15
+ def self.available?
16
+ return false unless defined?(::ActiveRecord::Base)
17
+
18
+ ::ActiveRecord::Base.connection_pool.with_connection do |conn|
19
+ conn.table_exists?("hooktrace_events")
20
+ end
21
+ rescue ::StandardError
22
+ false
23
+ end
24
+
25
+ def model_class
26
+ return self.class.const_get(:Event, false) if self.class.const_defined?(:Event, false)
27
+
28
+ self.class.model_mutex.synchronize do
29
+ return self.class.const_get(:Event, false) if self.class.const_defined?(:Event, false)
30
+
31
+ unless defined?(::ActiveRecord::Base)
32
+ raise Hooktrace::StandardError, "ActiveRecord::Base is required for ActiveRecordStore"
33
+ end
34
+
35
+ klass = Class.new(::ActiveRecord::Base) do
36
+ self.table_name = "hooktrace_events"
37
+ end
38
+ self.class.const_set(:Event, klass)
39
+ end
40
+ end
41
+
42
+ def save(record)
43
+ rec = record.is_a?(EventRecord) ? record : EventRecord.from_h(record)
44
+ attributes = {
45
+ event_type: rec.event_type,
46
+ provider: rec.provider,
47
+ valid_event: rec.valid?,
48
+ error_count: rec.errors.size,
49
+ validation_errors: serialize_json_column(:validation_errors, rec.errors),
50
+ payload: serialize_json_column(:payload, rec.payload),
51
+ received_at: parse_time(rec.received_at)
52
+ }
53
+
54
+ row = model_class.find_or_initialize_by(event_id: rec.id)
55
+ row.update!(attributes)
56
+ rec
57
+ rescue ::ActiveRecord::RecordNotUnique
58
+ row = model_class.find_by(event_id: rec.id)
59
+ row&.update!(attributes)
60
+ rec
61
+ end
62
+
63
+ def find(id)
64
+ id_str = id.to_s
65
+ row = model_class.where(event_id: id_str).order(received_at: :desc, id: :desc).first
66
+ row ||= model_class.find_by(id: id_str) if id_str.match?(/\A\d+\z/)
67
+ row ? to_event_record(row) : nil
68
+ end
69
+
70
+ def all(limit: 100, event_type: nil, status: nil)
71
+ scope = model_class.order(received_at: :desc, id: :desc)
72
+ scope = scope.where(event_type: event_type) if event_type && !event_type.empty?
73
+ scope = scope.where(valid_event: status == "valid") if status && !status.empty?
74
+
75
+ scope.limit(limit).map { |row| to_event_record(row) }
76
+ end
77
+
78
+ def count
79
+ model_class.count
80
+ end
81
+
82
+ def stats
83
+ total = model_class.count
84
+ passed = model_class.where(valid_event: true).count
85
+ failed = total - passed
86
+ rate = total.positive? ? ((passed.to_f / total) * 100).round(1) : 100.0
87
+
88
+ {
89
+ total: total,
90
+ passed: passed,
91
+ failed: failed,
92
+ pass_rate: rate
93
+ }
94
+ end
95
+
96
+ def clear!
97
+ model_class.delete_all
98
+ end
99
+
100
+ private
101
+
102
+ def to_event_record(row)
103
+ EventRecord.new(
104
+ id: row.event_id,
105
+ event_type: row.event_type,
106
+ provider: row.provider,
107
+ received_at: row.received_at&.utc&.iso8601,
108
+ valid: row.valid_event,
109
+ errors: deserialize_json_column(row.validation_errors, []),
110
+ payload: deserialize_json_column(row.payload, {})
111
+ )
112
+ end
113
+
114
+ def serialize_json_column(column_name, value)
115
+ col = model_class.columns_hash[column_name.to_s]
116
+ if col && %i[json jsonb].include?(col.type)
117
+ value
118
+ else
119
+ JSON.generate(value)
120
+ end
121
+ end
122
+
123
+ def deserialize_json_column(value, fallback)
124
+ return fallback if value.nil?
125
+ return value unless value.is_a?(String)
126
+
127
+ JSON.parse(value)
128
+ rescue JSON::ParserError
129
+ fallback
130
+ end
131
+
132
+ def parse_time(value)
133
+ return value if value.is_a?(Time)
134
+
135
+ Time.parse(value.to_s).utc
136
+ rescue ArgumentError
137
+ Time.now.utc
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "json"
5
+
6
+ module Hooktrace
7
+ module Storage
8
+ class FileStore
9
+ DEFAULT_DIR = "tmp/hooktrace/events"
10
+
11
+ attr_reader :directory
12
+
13
+ def initialize(directory = DEFAULT_DIR)
14
+ @directory = File.expand_path(directory)
15
+ @mutex = Monitor.new
16
+ ensure_directory!
17
+ end
18
+
19
+ def save(record)
20
+ ensure_directory!
21
+ rec = record.is_a?(EventRecord) ? record : EventRecord.from_h(record)
22
+ safe_id = sanitize_filename(rec.id)
23
+ filename = "#{sanitize_filename(rec.received_at)}_#{safe_id}.json"
24
+ filepath = File.join(@directory, filename)
25
+
26
+ @mutex.synchronize do
27
+ FileUtils.rm_f(Dir.glob(File.join(@directory, "*_#{safe_id}.json")))
28
+ File.write(filepath, JSON.pretty_generate(rec.to_h))
29
+ end
30
+ rec
31
+ end
32
+
33
+ def find(id)
34
+ id_str = sanitize_filename(id)
35
+ pattern = File.join(@directory, "*_#{id_str}.json")
36
+ file = Dir.glob(pattern).first
37
+ return nil unless file && File.exist?(file)
38
+
39
+ data = JSON.parse(File.read(file))
40
+ EventRecord.from_h(data)
41
+ rescue JSON::ParserError
42
+ nil
43
+ end
44
+
45
+ def all(limit: 100, event_type: nil, status: nil)
46
+ ensure_directory!
47
+ files = Dir.glob(File.join(@directory, "*.json")).reverse
48
+ records = files.lazy.map do |file|
49
+ load_file(file)
50
+ end.compact
51
+
52
+ records = records.select { |r| r.event_type == event_type } if event_type
53
+ records = records.select { |r| status == "valid" ? r.valid? : r.invalid? } if status
54
+
55
+ records.take(limit).to_a
56
+ end
57
+
58
+ def count
59
+ ensure_directory!
60
+ Dir.glob(File.join(@directory, "*.json")).size
61
+ end
62
+
63
+ def stats
64
+ records = all(limit: 500)
65
+ total = records.size
66
+ passed = records.count(&:valid?)
67
+ failed = total - passed
68
+ rate = total.positive? ? ((passed.to_f / total) * 100).round(1) : 100.0
69
+
70
+ {
71
+ total: total,
72
+ passed: passed,
73
+ failed: failed,
74
+ pass_rate: rate
75
+ }
76
+ end
77
+
78
+ def clear!
79
+ ensure_directory!
80
+ @mutex.synchronize do
81
+ FileUtils.rm_f(Dir.glob(File.join(@directory, "*.json")))
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def ensure_directory!
88
+ FileUtils.mkdir_p(@directory) unless File.directory?(@directory)
89
+ end
90
+
91
+ def load_file(file)
92
+ data = JSON.parse(File.read(file))
93
+ EventRecord.from_h(data)
94
+ rescue StandardError
95
+ nil
96
+ end
97
+
98
+ def sanitize_filename(string)
99
+ string.to_s.gsub(/[^0-9A-Za-z_-]/, "_")
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+ require_relative "result"
5
+
6
+ module Hooktrace
7
+ class Validator
8
+ DEFAULT_MAX_ERRORS = 100
9
+
10
+ def initialize(contract, strict: false, max_errors: DEFAULT_MAX_ERRORS)
11
+ @contract = contract
12
+ @strict = strict
13
+ @max_errors = max_errors
14
+ @errors = []
15
+ end
16
+
17
+ def validate(payload)
18
+ @errors = []
19
+
20
+ unless payload.is_a?(Hash)
21
+ add_error("$", "type_mismatch", "Expected object/hash for payload, got #{payload.class}")
22
+ return Result.new(errors: @errors)
23
+ end
24
+
25
+ validate_object(payload, @contract.fields, "")
26
+
27
+ Result.new(errors: @errors)
28
+ end
29
+
30
+ private
31
+
32
+ def validate_object(hash, fields, prefix)
33
+ expected_names = fields.map(&:name)
34
+
35
+ fields.each do |field|
36
+ break if max_errors_reached?
37
+
38
+ validate_field(hash, field, prefix)
39
+ end
40
+
41
+ check_unknown_fields(hash, expected_names, prefix) if @strict
42
+ end
43
+
44
+ def validate_field(hash, field, prefix)
45
+ path = build_path(prefix, field.name)
46
+ has_key, value = fetch_field_value(hash, field.name)
47
+
48
+ unless has_key
49
+ add_error(path, "required", "Field is required") if field.required?
50
+ return
51
+ end
52
+
53
+ if value.nil?
54
+ validate_nil_value(path, field)
55
+ return
56
+ end
57
+
58
+ unless type_matches?(value, field.type)
59
+ add_error(path, "type_mismatch", "Expected #{field.type}, got #{actual_type_name(value)}")
60
+ return
61
+ end
62
+
63
+ validate_complex_field(value, field, path)
64
+ end
65
+
66
+ def validate_nil_value(path, field)
67
+ return if field.nullable? || field.type == :null
68
+
69
+ add_error(path, "type_mismatch", "Expected #{field.type}, got null")
70
+ end
71
+
72
+ def validate_complex_field(value, field, path)
73
+ if field.object? && field.fields.any?
74
+ validate_object(value, field.fields, path)
75
+ elsif field.array? && (field.item_type || field.item_fields.any?)
76
+ validate_array(value, field, path)
77
+ end
78
+ end
79
+
80
+ def validate_array(array, field, prefix)
81
+ array.each_with_index do |item, index|
82
+ break if max_errors_reached?
83
+
84
+ item_path = "#{prefix}[#{index}]"
85
+ validate_array_item(item, field, item_path)
86
+ end
87
+ end
88
+
89
+ def validate_array_item(item, field, item_path)
90
+ if item.nil?
91
+ unless field.nullable? || field.item_type == :null
92
+ expected_type = field.item_type || "item"
93
+ add_error(item_path, "type_mismatch", "Expected #{expected_type}, got null")
94
+ end
95
+ return
96
+ end
97
+
98
+ if field.item_fields.any?
99
+ validate_array_object_item(item, field.item_fields, item_path)
100
+ elsif field.item_type && !type_matches?(item, field.item_type)
101
+ add_error(item_path, "type_mismatch", "Expected #{field.item_type}, got #{actual_type_name(item)}")
102
+ end
103
+ end
104
+
105
+ def validate_array_object_item(item, item_fields, item_path)
106
+ if item.is_a?(Hash)
107
+ validate_object(item, item_fields, item_path)
108
+ else
109
+ add_error(item_path, "type_mismatch", "Expected object item, got #{actual_type_name(item)}")
110
+ end
111
+ end
112
+
113
+ def check_unknown_fields(hash, expected_names, prefix)
114
+ hash.each_key do |key|
115
+ break if max_errors_reached?
116
+
117
+ key_str = key.to_s
118
+ next if expected_names.include?(key_str)
119
+
120
+ path = build_path(prefix, key_str)
121
+ add_error(path, "unknown_field", "Unknown field: #{key_str}")
122
+ end
123
+ end
124
+
125
+ def max_errors_reached?
126
+ @errors.size >= @max_errors
127
+ end
128
+
129
+ def fetch_field_value(hash, field_name)
130
+ if hash.key?(field_name)
131
+ [true, hash[field_name]]
132
+ elsif hash.key?(field_name.to_sym)
133
+ [true, hash[field_name.to_sym]]
134
+ else
135
+ [false, nil]
136
+ end
137
+ end
138
+
139
+ def type_matches?(value, type)
140
+ case type
141
+ when :string then value.is_a?(String)
142
+ when :integer then value.is_a?(Integer) && ![true, false].include?(value)
143
+ when :number then value.is_a?(Numeric) && (!value.is_a?(Float) || value.finite?)
144
+ when :boolean then [true, false].include?(value)
145
+ when :object then value.is_a?(Hash)
146
+ when :array then value.is_a?(Array)
147
+ when :null then value.nil?
148
+ else false
149
+ end
150
+ end
151
+
152
+ def actual_type_name(value)
153
+ case value
154
+ when String then "string"
155
+ when Integer then "integer"
156
+ when Numeric then "number"
157
+ when TrueClass, FalseClass then "boolean"
158
+ when Hash then "object"
159
+ when Array then "array"
160
+ when NilClass then "null"
161
+ else value.class.name.downcase
162
+ end
163
+ end
164
+
165
+ def build_path(prefix, segment)
166
+ prefix.empty? ? segment : "#{prefix}.#{segment}"
167
+ end
168
+
169
+ def add_error(path, code, message)
170
+ @errors << Error.new(path: path, code: code, message: message)
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hooktrace
4
+ VERSION = "0.1.0"
5
+ end