kirei 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/cli/commands/new_app/files/rakefile.rb +3 -0
- data/lib/kirei/model/class_methods.rb +31 -3
- data/lib/kirei/routing/base.rb +12 -3
- data/lib/kirei/routing/rack_env_type.rb +2 -2
- data/lib/kirei/services/runner.rb +31 -11
- data/lib/kirei/version.rb +1 -1
- data/lib/tasks/routes.rake +26 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1adc30a0abdfee8e565869c726e247ab07f3ab1cd535c2d7874182f501962a9
|
4
|
+
data.tar.gz: 533abff887e8aca8eda259ad66679b76b7d25d4236966a80fcf32dfafe18dcd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed22516b55aa7677b85dbf09e1f6f88822646574897e55831318eead12759f275e36394076be292be54d02f08223f78e827da3990d82871bcef6bd8d98a6f26c
|
7
|
+
data.tar.gz: 6aeb8fcc2cdf887da4824e1075c935f186f67d4639e01d6b099341a60e6c9ae7b6ce7ed30ad81ab1a40583152cf7006f4205c78d10b6aa775f45c345a53ff3f0
|
data/README.md
CHANGED
@@ -85,12 +85,40 @@ module Kirei
|
|
85
85
|
return unless App.config.db_extensions.include?(:pg_json)
|
86
86
|
|
87
87
|
attributes.each_pair do |key, value|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
if vector_column?(key.to_s)
|
89
|
+
attributes[key] = cast_to_vector(value)
|
90
|
+
elsif value.is_a?(Hash) || value.is_a?(Array)
|
91
|
+
attributes[key] = T.unsafe(Sequel).pg_jsonb_wrap(value)
|
92
|
+
end
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
96
|
+
#
|
97
|
+
# install the gem "pgvector" if you need to use vector columns
|
98
|
+
# also add `:pgvector` to the `App.config.db_extensions` array
|
99
|
+
# and enable the vector extension on the database.
|
100
|
+
#
|
101
|
+
sig { params(column_name: String).returns(T::Boolean) }
|
102
|
+
def vector_column?(column_name)
|
103
|
+
_col_name, col_info = T.let(
|
104
|
+
db.schema(table_name.to_sym).find { _1[0] == column_name.to_sym },
|
105
|
+
[Symbol, T::Hash[Symbol, T.untyped]],
|
106
|
+
)
|
107
|
+
col_info.fetch(:db_type).match?(/vector\(\d+\)/)
|
108
|
+
end
|
109
|
+
|
110
|
+
# New method to cast an array to a vector
|
111
|
+
sig { params(value: T.any(T::Array[Numeric], Sequel::SQL::Expression)).returns(Sequel::SQL::Expression) }
|
112
|
+
def cast_to_vector(value)
|
113
|
+
return value if value.is_a?(Sequel::SQL::Expression) || value.is_a?(Sequel::SQL::PlaceholderLiteralString)
|
114
|
+
|
115
|
+
Kernel.raise("'pg_array' extension is not enabled") unless db.extension(:pg_array)
|
116
|
+
|
117
|
+
pg_array = T.unsafe(Sequel).pg_array(value)
|
118
|
+
|
119
|
+
Sequel.lit("?::vector", pg_array)
|
120
|
+
end
|
121
|
+
|
94
122
|
sig do
|
95
123
|
override.params(
|
96
124
|
hash: T::Hash[Symbol, T.untyped],
|
data/lib/kirei/routing/base.rb
CHANGED
@@ -8,6 +8,8 @@ module Kirei
|
|
8
8
|
class Base
|
9
9
|
extend T::Sig
|
10
10
|
|
11
|
+
NOT_FOUND = T.let([404, {}, ["Not Found"]], RackResponseType) # rubocop:disable Style/MutableConstant
|
12
|
+
|
11
13
|
sig { params(params: T::Hash[String, T.untyped]).void }
|
12
14
|
def initialize(params: {})
|
13
15
|
@router = T.let(Router.instance, Router)
|
@@ -23,6 +25,7 @@ module Kirei
|
|
23
25
|
sig { params(env: RackEnvType).returns(RackResponseType) }
|
24
26
|
def call(env)
|
25
27
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
28
|
+
status = 500 # we use it in the "ensure" block, so we need to define early (Sorbet doesn't like `status ||= 418`)
|
26
29
|
|
27
30
|
http_verb = Verb.deserialize(env.fetch("REQUEST_METHOD"))
|
28
31
|
req_path = T.cast(env.fetch("REQUEST_PATH"), String)
|
@@ -33,7 +36,7 @@ module Kirei
|
|
33
36
|
#
|
34
37
|
|
35
38
|
route = router.get(http_verb, req_path)
|
36
|
-
return
|
39
|
+
return NOT_FOUND if route.nil?
|
37
40
|
|
38
41
|
params = case route.verb
|
39
42
|
when Verb::GET
|
@@ -66,7 +69,13 @@ module Kirei
|
|
66
69
|
Kirei::Logging::Logger.call(
|
67
70
|
level: Kirei::Logging::Level::INFO,
|
68
71
|
label: "Request Started",
|
69
|
-
meta:
|
72
|
+
meta: {
|
73
|
+
"http.method" => route.verb.serialize,
|
74
|
+
"http.route" => route.path,
|
75
|
+
"http.host" => env.fetch("HTTP_HOST"),
|
76
|
+
"http.request_params" => params,
|
77
|
+
"http.client_ip" => env.fetch("CF-Connecting-IP", env.fetch("REMOTE_ADDR")),
|
78
|
+
},
|
70
79
|
)
|
71
80
|
|
72
81
|
statsd_timing_tags = {
|
@@ -101,7 +110,7 @@ module Kirei
|
|
101
110
|
::StatsD.measure("request", latency_in_ms, tags: statsd_timing_tags)
|
102
111
|
|
103
112
|
Kirei::Logging::Logger.call(
|
104
|
-
level: Kirei::Logging::Level::INFO,
|
113
|
+
level: status >= 500 ? Kirei::Logging::Level::ERROR : Kirei::Logging::Level::INFO,
|
105
114
|
label: "Request Finished",
|
106
115
|
meta: { "response.body" => response_body, "response.latency_in_ms" => latency_in_ms },
|
107
116
|
)
|
@@ -10,12 +10,12 @@ module Kirei
|
|
10
10
|
sig do
|
11
11
|
type_parameters(:T)
|
12
12
|
.params(
|
13
|
-
class_name:
|
13
|
+
class_name: T.untyped, # @TODO(lud, 17.08.2024): replace all non-word characters with `.`?
|
14
14
|
log_tags: T::Hash[String, T.untyped],
|
15
|
-
|
15
|
+
block: T.proc.returns(T.type_parameter(:T)),
|
16
16
|
).returns(T.type_parameter(:T))
|
17
17
|
end
|
18
|
-
def self.call(class_name, log_tags: {}, &
|
18
|
+
def self.call(class_name, log_tags: {}, &block)
|
19
19
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
20
20
|
service = yield
|
21
21
|
|
@@ -24,23 +24,43 @@ module Kirei
|
|
24
24
|
stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
25
25
|
latency_in_ms = stop - T.must(start)
|
26
26
|
|
27
|
-
result =
|
28
|
-
when Services::Result
|
29
|
-
service.success? ? "success" : "failure"
|
30
|
-
else
|
31
|
-
"unknown"
|
32
|
-
end
|
27
|
+
result = service_result(service)
|
33
28
|
|
34
29
|
metric_tags = Logging::Metric.inject_defaults({ "service.result" => result })
|
35
30
|
::StatsD.measure(class_name, latency_in_ms, tags: metric_tags)
|
36
31
|
|
37
32
|
logtags = {
|
38
|
-
"service.name" => class_name,
|
33
|
+
"service.name" => class_name.to_s,
|
39
34
|
"service.latency_in_ms" => latency_in_ms,
|
40
35
|
"service.result" => result,
|
36
|
+
"service.source_location" => source_location(block),
|
41
37
|
}
|
42
38
|
logtags.merge!(log_tags)
|
43
|
-
|
39
|
+
|
40
|
+
Logging::Logger.call(level: log_level(result), label: "Service Finished", meta: logtags)
|
41
|
+
end
|
42
|
+
|
43
|
+
sig { params(proc: T.proc.returns(T.untyped)).returns(String) }
|
44
|
+
private_class_method def self.source_location(proc)
|
45
|
+
proc.source_location.join(":").gsub(App.root.to_s, "")
|
46
|
+
end
|
47
|
+
|
48
|
+
sig { params(service: T.untyped).returns(String) }
|
49
|
+
def self.service_result(service)
|
50
|
+
case service
|
51
|
+
when Services::Result
|
52
|
+
service.success? ? "success" : "failure"
|
53
|
+
else
|
54
|
+
"unknown"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
sig { params(result: String).returns(Logging::Level) }
|
59
|
+
private_class_method def self.log_level(result)
|
60
|
+
return Logging::Level::INFO if result == "success"
|
61
|
+
return Logging::Level::WARN if result == "failure"
|
62
|
+
|
63
|
+
Logging::Level::UNKNOWN
|
44
64
|
end
|
45
65
|
end
|
46
66
|
end
|
data/lib/kirei/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
# typed: false
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative("../kirei")
|
5
|
+
|
6
|
+
namespace :kirei do
|
7
|
+
desc "Prints all available routes"
|
8
|
+
task :routes do
|
9
|
+
router = Kirei::Routing::Router.instance
|
10
|
+
|
11
|
+
longest_path = router.routes.keys.map(&:length).max
|
12
|
+
|
13
|
+
routes_by_controller = router.routes.values.group_by(&:controller)
|
14
|
+
|
15
|
+
puts "\n"
|
16
|
+
|
17
|
+
routes_by_controller.each do |controller, routes|
|
18
|
+
puts "#{controller}:"
|
19
|
+
routes.each do |route|
|
20
|
+
verb = route.verb.serialize.upcase.ljust(7 + 3) # 7 is the length of the longest verb
|
21
|
+
puts "#{verb} #{route.path.ljust(longest_path + 1)} => ##{route.action}"
|
22
|
+
end
|
23
|
+
puts "\n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kirei
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ludwig Reinmiedl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -203,6 +203,7 @@ files:
|
|
203
203
|
- lib/kirei/services/result.rb
|
204
204
|
- lib/kirei/services/runner.rb
|
205
205
|
- lib/kirei/version.rb
|
206
|
+
- lib/tasks/routes.rake
|
206
207
|
- sorbet/rbi/shims/base_model.rbi
|
207
208
|
- sorbet/rbi/shims/ruby.rbi
|
208
209
|
homepage: https://github.com/swiknaba/kirei
|