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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 788221745d889864fe2ec7075cce289f6d44361dece7711fbe91ac24c16f693b
4
- data.tar.gz: e03e3cf0c4c6db69bc0106107ccb002b8f472ba077ef457d88a84b92b188371f
3
+ metadata.gz: c1adc30a0abdfee8e565869c726e247ab07f3ab1cd535c2d7874182f501962a9
4
+ data.tar.gz: 533abff887e8aca8eda259ad66679b76b7d25d4236966a80fcf32dfafe18dcd2
5
5
  SHA512:
6
- metadata.gz: 30bef2a1458e1aeeebd774d577df1dc231b0cfa46fb7483b076b69ac05de1529527442599dca18bb278fec42e1a17888eee849feb1b3f81dd502fd92284dd879
7
- data.tar.gz: 181df5735f94c222fc29e9bb3c18aa33340995925a324a18a0bb28e1001eaeaee24f8c7442cc5852adf31ae7e5e7a707743441e1c64a585e27039e7b50ba96da
6
+ metadata.gz: ed22516b55aa7677b85dbf09e1f6f88822646574897e55831318eead12759f275e36394076be292be54d02f08223f78e827da3990d82871bcef6bd8d98a6f26c
7
+ data.tar.gz: 6aeb8fcc2cdf887da4824e1075c935f186f67d4639e01d6b099341a60e6c9ae7b6ce7ed30ad81ab1a40583152cf7006f4205c78d10b6aa775f45c345a53ff3f0
data/README.md CHANGED
@@ -13,7 +13,7 @@ TL;DR:
13
13
  * **zero magic**
14
14
  * **strict typing**
15
15
  * **very few low level dependencies**
16
- * ultra low memory footprint
16
+ * low memory footprint
17
17
  * high performance
18
18
  * simple to understand
19
19
 
@@ -15,6 +15,9 @@ module Cli
15
15
  # frozen_string_literal: true
16
16
 
17
17
  require "rake"
18
+ require_relative "app"
19
+
20
+ Dir.glob("#{Kirei::GEM_ROOT}/lib/tasks/**/*.rake").each { import(_1) }
18
21
 
19
22
  Dir.glob("lib/tasks/**/*.rake").each { import(_1) }
20
23
 
@@ -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
- next unless value.is_a?(Hash) || value.is_a?(Array)
89
-
90
- attributes[key] = T.unsafe(Sequel).pg_jsonb_wrap(value)
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],
@@ -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 [404, {}, ["Not Found"]] if route.nil?
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: params,
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
  )
@@ -13,9 +13,9 @@ module Kirei
13
13
  String,
14
14
  Numeric,
15
15
  TCPSocket,
16
- Puma::Client,
16
+ ::Puma::Client,
17
17
  StringIO,
18
- Puma::Configuration,
18
+ ::Puma::Configuration,
19
19
  )
20
20
  ]
21
21
  end
@@ -10,12 +10,12 @@ module Kirei
10
10
  sig do
11
11
  type_parameters(:T)
12
12
  .params(
13
- class_name: String,
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
- _: T.proc.returns(T.type_parameter(:T)),
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 = case service
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
- Logging::Logger.call(level: Logging::Level::INFO, label: "Service Finished", meta: logtags)
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
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Kirei
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.0"
6
6
  end
@@ -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.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-05-19 00:00:00.000000000 Z
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