functionator 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a60f63f5b13bd75c67507ee3326b6db811411036f5df352f3987ab464891dbd8
4
- data.tar.gz: 5f11f90e991f592ddb9c54d522d7ca57188ca75b71cd63b68dded9fc314e7a54
3
+ metadata.gz: 005f8e3f95d8d89a3836fd24d4665b37a62201571e569e06b88f5643a5839704
4
+ data.tar.gz: 8c31ec6d017e2c3edb0e2dc8e4c584e68e04cb95e1481335e85af2f684b38223
5
5
  SHA512:
6
- metadata.gz: '09d242b6e2c17e859bc843f61e0212bddcfa132fbb845a26c474c19b4049beb34fb60c8678d7ff8aa41ae444aca010c7f5e52250181f83fa26215d4f1585c00f'
7
- data.tar.gz: 3a6983b3ae5705389621e2d60f24f1344fa0efc59f131017badbe1cd1c44f95ad2d3c6654f06e6364fcdc7dd041bd740273e45b24ba12826a53fe71413841fc0
6
+ metadata.gz: 8207c04fd12fd46ff58d25d5fe3cdc3a9626dcfb586a03fb3f2ba81e5887abe8c3179532465484584b555ad2c31c74c199c01f69553ada7c5636a50a9ffac45c
7
+ data.tar.gz: b694e21077538c5a973adcce3a8315ebad4830e2d4df70dfaf7084b1c9fe5d7a0df694316adb274805e63047dbfeeab68901c79390a20b56f1fb65835f24b9ac
@@ -1,3 +1,3 @@
1
1
  module Functionator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: functionator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaique Leonardo
@@ -82,8 +82,6 @@ files:
82
82
  - lib/functionator/mongodb/client.rb
83
83
  - lib/functionator/version.rb
84
84
  - lib/mongodb_factory.rb
85
- - lib/mongodb_factory/client.rb
86
- - lib/mongodb_factory/version.rb
87
85
  homepage: https://github.com/seuusuario/functionator
88
86
  licenses:
89
87
  - MIT
@@ -1,167 +0,0 @@
1
- require 'mongo'
2
-
3
- module MongodbFactory
4
- class Client
5
- @instance = nil
6
-
7
- def self.instance
8
- @instance ||= new
9
- end
10
-
11
- def initialize(connection_string = nil)
12
- @connection_string = connection_string || build_connection_string
13
- @client = Mongo::Client.new(@connection_string)
14
- end
15
-
16
- def client
17
- @client
18
- end
19
-
20
- def insert_many(collection_name, data)
21
- client[collection_name.to_sym].insert_many(data)
22
- rescue StandardError => e
23
- puts "Error: #{e.message}"
24
- end
25
-
26
- def find(collection_name, query)
27
- client[collection_name.to_sym].find(query).to_a
28
- rescue StandardError => e
29
- puts "Error: #{e.message}"
30
- []
31
- end
32
-
33
- def find_sort(collection_name, query, sort, limit)
34
- client[collection_name.to_sym].find(query).sort(sort).limit(limit).to_a
35
- rescue StandardError => e
36
- puts "Error: #{e.message}"
37
- []
38
- end
39
-
40
- def update(collection_name, query, query_up)
41
- client[collection_name.to_sym].update_one(query, query_up)
42
- rescue StandardError => e
43
- puts "Error: #{e.message}"
44
- end
45
-
46
- def update_log_group(collection_name, query, query_up, upsert: false)
47
- client[collection_name.to_sym].update_one(query, query_up, upsert: upsert)
48
- rescue StandardError => e
49
- puts "Error: #{e.message}"
50
- end
51
-
52
- def count_status_requests(collection_name)
53
- begin
54
- pipeline = [
55
- { '$match' => { 'status' => { '$regex' => '^(Success|Failure)', '$options' => 'i' } } },
56
- { '$project' => {
57
- 'domain' => 1,
58
- 'status_code' => {
59
- '$let' => {
60
- 'vars' => {
61
- 'regex_result' => {
62
- '$regexFind' => {
63
- 'input' => '$status',
64
- 'regex' => 'HTTP Status Code (\d{3})',
65
- 'options' => 'i'
66
- }
67
- }
68
- },
69
- 'in' => { '$ifNull' => ['$$regex_result.match', 'N/A'] }
70
- }
71
- },
72
- 'status' => 1
73
- }
74
- },
75
- { '$group' => {
76
- '_id' => '$domain',
77
- 'total' => { '$sum' => 1 },
78
- 'status_success' => { '$sum' => { '$cond' => [{ '$regexMatch' => { 'input' => '$status', 'regex' => '^Success', 'options' => 'i' } }, 1, 0] } },
79
- 'status_failure' => { '$sum' => { '$cond' => [{ '$regexMatch' => { 'input' => '$status', 'regex' => '^Failure', 'options' => 'i' } }, 1, 0] } },
80
- 'status_codes' => { '$push' => '$status_code' }
81
- }
82
- },
83
- { '$project' => {
84
- 'domain' => '$_id',
85
- 'total_requests' => '$total',
86
- 'status_success' => 1,
87
- 'status_failure' => 1,
88
- 'status_codes' => 1,
89
- '_id' => 0
90
- }
91
- }
92
- ]
93
-
94
- result = client[collection_name.to_sym].aggregate(pipeline).to_a
95
- result.map do |client_data|
96
- status_codes_count = client_data['status_codes'].tally
97
- {
98
- domain: client_data['domain'],
99
- total_requests: client_data['total_requests'] || 0,
100
- status_success: client_data['status_success'] || 0,
101
- status_failure: client_data['status_failure'] || 0,
102
- status_codes_count: status_codes_count
103
- }
104
- end
105
- rescue StandardError => e
106
- puts "Error: #{e.message}"
107
- []
108
- end
109
- end
110
-
111
- def upsert_log_to_mongo(collection_name, document)
112
- existing_document = find(collection_name, { ms: document[:ms] }).first
113
- document.delete(:created_at) if existing_document
114
- result = update(collection_name, { ms: document[:ms] }, { '$set' => document })
115
-
116
- if result&.upserted_count&.positive?
117
- puts "Inserted new document for ms: #{document[:ms]}"
118
- else
119
- puts "Updated document for ms: #{document[:ms]}"
120
- end
121
- rescue StandardError => e
122
- puts "Error upserting into MongoDB: #{e.message}"
123
- end
124
-
125
- def upsert_log_to_mongo_log_group(collection_name, document)
126
- existing_document = find(collection_name, { ms: document[:ms] }).first
127
- document.delete(:created_at) if existing_document
128
- result = update_log_group(collection_name, { ms: document[:ms] }, { '$set' => document }, upsert: true)
129
-
130
- if result&.upserted_count&.positive?
131
- puts "Inserted new document for ms: #{document[:ms]}"
132
- else
133
- puts "Updated document for ms: #{document[:ms]}"
134
- end
135
- rescue StandardError => e
136
- puts "Error upserting into MongoDB: #{e.message}"
137
- end
138
-
139
- def fetch_log_groups_from_mongo
140
- log_groups = {}
141
- find('log_group_lambda', {}).each do |doc|
142
- next unless doc['ms'] && doc['log_groups'].is_a?(Array)
143
- log_groups[doc['ms']] = doc['log_groups']
144
- end
145
- log_groups
146
- end
147
-
148
- def get_last_run_from_mongo(collection_name, ms)
149
- collection = find_sort(collection_name, { ms: ms }, { last_run: -1 }, 1).first
150
- collection ? collection[:last_run] : nil
151
- rescue StandardError => e
152
- puts "Error fetching last_run: #{e.message}"
153
- nil
154
- end
155
-
156
- private
157
-
158
- def build_connection_string
159
- user = ENV['USER'] || ENV['MONGODB_USER']
160
- password = ENV['PASSWORD'] || ENV['MONGODB_PASSWORD']
161
- host = ENV['HOST'] || ENV['MONGODB_HOST']
162
- database = ENV['DATABASE'] || ENV['MONGODB_DATABASE']
163
-
164
- "mongodb+srv://#{user}:#{password}@#{host}/#{database}?retryWrites=true&w=majority&readPreference=primaryPreferred"
165
- end
166
- end
167
- end
@@ -1,3 +0,0 @@
1
- module Functionator
2
- VERSION = "0.1.0"
3
- end