eitje_s3 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3fab11f169aa044513495cd47f13853ec305a31fe72e5075b681f042d9712c8
4
+ data.tar.gz: 464f079e85d4e7df5373d70732ba93852ff9489b3d543404eee769d6d5e86dec
5
+ SHA512:
6
+ metadata.gz: a8c6436222d6708cbe69ae3d4d38e97c6a015db22e870639520ea05ee36ad721fadb7cae6eac3daeb53a5649035ead3562d654865f785c2f2e8e77468b2647cf
7
+ data.tar.gz: 47f1b3af7e66c4740d8a4fcba65d824096e4287a0b22d122d2e132fbc37b660e573ac18ff3c38b72e1a0539dbf8540c792fcab032f9e1ebe77efc0ad0c399319
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Jurriaan Schrofer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # S3 gem
2
+
3
+ Never stops lovin AWS, yay!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+
11
+ gem 's3', git: 'https://github.com/eitje-app/s3_engine', branch: 'production'
12
+
13
+ ```
14
+
15
+ Or, for development purposes, add instead:
16
+
17
+ ```ruby
18
+
19
+ local_paths = %w$ /Users/jurriaanschrofer/Documents/s3 $
20
+ if ENV['IS_LOCAL_ENGINE'] && ( path = local_paths.find {|path| Dir.glob(path).any?} )
21
+ gem 's3', path: path
22
+ else
23
+ gem 's3', git: 'https://github.com/eitje-app/s3_engine', branch: 'production'
24
+ end
25
+
26
+ ```
27
+
28
+ Create and run the required migrations:
29
+
30
+ ```bash
31
+
32
+ $ rails s3:install:migrations
33
+ $ rails db:migrate
34
+
35
+ ```
36
+ Add the following to your routes.rb:
37
+ ```ruby
38
+
39
+ mount S3::Engine => "/s3"
40
+
41
+ ```
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
9
+
10
+ require "rake/testtask"
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1,5 @@
1
+ module S3
2
+ class ApplicationController < ActionController::API
3
+
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ class S3::RecordsController < API::Admin::BaseController #ApplicationController
2
+
3
+ # before_action :authorize_admin
4
+
5
+ # queries files split per date and environment
6
+ def deleted_records
7
+ data = S3::NewDeletedRecordsService.get_records(**slice_params(:db_table, :start_date, :end_date, :env_id))
8
+ render json: {deleted_records: data}
9
+ end
10
+
11
+ # queries files split per date and table
12
+ def legacy_deleted_records
13
+ data = S3::OldDeletedRecordsService.get_records(**slice_params(:db_table, :start_date, :end_date, :env_id, :env_name))
14
+ render json: {deleted_records: data}
15
+ end
16
+
17
+ private
18
+
19
+ def slice_params(*args)
20
+ params.slice(*args).as_json.symbolize_keys
21
+ end
22
+
23
+ end
@@ -0,0 +1,49 @@
1
+ module S3::NewDeletedRecordsService
2
+ class << self
3
+
4
+ DB_TABLES = %w$ shifts teams users contracts infos posts $
5
+
6
+ def test(table)
7
+ S3::NewDeletedRecordsService.get_records(
8
+ db_table: table, start_date: '2021-03-28', end_date: '2021-04-3', env_id: 513
9
+ )
10
+ end
11
+
12
+ def get_records(db_table:, start_date:, end_date:, env_id:)
13
+ @date_range = Date.parse(start_date)..Date.parse(end_date)
14
+ @s3 = Aws::S3::Client.new
15
+ @db_table = db_table
16
+ @env_id = env_id
17
+
18
+ set_file_name
19
+ validate_args
20
+
21
+ query_records
22
+ filter_records
23
+ @records
24
+ end
25
+
26
+ def validate_args
27
+ throw :db_table_name_is_not_valid unless DB_TABLES.include?(@db_table)
28
+ end
29
+
30
+ def set_file_name
31
+ @file_name = "env_#{@env_id}_deleted_#{@db_table}.json"
32
+ end
33
+
34
+ def query_records
35
+ file = @s3.get_object(bucket: 'eitje-deleted-jurr', key: @file_name)
36
+ @records = JSON.parse(file.body.read.as_json).map(&:symbolize_keys)
37
+ end
38
+
39
+ def filter_records
40
+ return if no_deleted_at?
41
+ @records.select! { |record| @date_range.include?(Date.parse(record[:deleted_at])) }
42
+ end
43
+
44
+ def no_deleted_at?
45
+ %w[infos].include? @db_table # files without deleted_at field
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,95 @@
1
+ module S3::OldDeletedRecordsService
2
+ class << self
3
+
4
+ DB_TABLES = %w$ shifts teams users contracts infos posts $
5
+
6
+ # S3::OldDeletedRecordsService.get_records(env_id: 307, env_name: 'Kua - Den Haag', db_table: 'verlofverzoeks', start_date: '2021-02-01', end_date: '2021-04-01')
7
+
8
+ def get_records(db_table:, start_date:, end_date:, env_id:, env_name:)
9
+
10
+ # validate_args(db_table)
11
+
12
+ @start_date = start_date
13
+ @end_date = end_date
14
+ @db_table = db_table
15
+ @env_name = env_name
16
+ @env_id = env_id
17
+ @date_range = get_date_range
18
+
19
+ @file_names_filtered_by_table = request_object_names
20
+ @file_names_filtered_by_date = filter_by_date
21
+
22
+ read_all_files
23
+
24
+ end
25
+
26
+ # validations
27
+
28
+ def validate_args(db_table)
29
+ throw :db_table_name_is_not_valid unless DB_TABLES.include?(db_table)
30
+ end
31
+
32
+ # base method
33
+
34
+ def read_all_files
35
+ @file_names_filtered_by_date.map do |file_name|
36
+
37
+ @file_name = file_name
38
+ @file = request_object
39
+ @db_table == 'users' ? filter_users_table_by_env : filter_file_by_env
40
+
41
+ end.flatten
42
+ end
43
+
44
+ # filter methods
45
+
46
+ def filter_file_by_env
47
+ has_env_id ? filter_by_env_id : filter_by_env_name
48
+ end
49
+
50
+ def filter_by_env_id
51
+ @file.select { |row| row[:environment_id] == @env_id }
52
+ end
53
+
54
+ def filter_by_env_name
55
+ @file.select { |row| row[:env] == @env_name }
56
+ end
57
+
58
+ def filter_users_table_by_env
59
+ @file.select { |row| row[:envs].include? @env_name }
60
+ end
61
+
62
+ def filter_by_date
63
+ @file_names_filtered_by_table.select { |file_name| @date_range.include?(get_date(file_name)) }
64
+ end
65
+
66
+ def has_env_id
67
+ @file.first.has_key?(:env_id)
68
+ end
69
+
70
+ # date methods
71
+
72
+ def get_date_range
73
+ (Date.parse(@start_date)..Date.parse(@end_date)).to_a.map {|x| x.strftime("%Y-%m-%d") }
74
+ end
75
+
76
+ def get_date(file_name)
77
+ file_name.match(/\d{4}-\d{2}-\d{2}/).to_s
78
+ end
79
+
80
+ # AWS methods
81
+
82
+ def request_object_names
83
+ s3 = Aws::S3::Client.new
84
+ objects = s3.list_objects(bucket: 'eitje-backups', prefix: @db_table).contents
85
+ names = objects.collect &:key
86
+ end
87
+
88
+ def request_object
89
+ s3 = Aws::S3::Client.new
90
+ object = s3.get_object(bucket: 'eitje-backups', key: @file_name)
91
+ json = JSON.parse(object.body.read.as_json).map(&:symbolize_keys)
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,85 @@
1
+ require 'json'
2
+
3
+ module S3::TransformDeletedFilesService
4
+ class << self
5
+
6
+ BUCKET = 'eitje-deleted-jurr-2'
7
+
8
+ def migrate_files(start_date: '2019-07-18')
9
+ @start_date = start_date
10
+
11
+ set_logger
12
+ set_bucket
13
+ set_tables
14
+ set_dates(start_date)
15
+
16
+ Environment.find_each do |env|
17
+ @env = env
18
+
19
+ @tables.each do |table|
20
+ @table = table
21
+ compose_file
22
+ end
23
+ end
24
+ end
25
+
26
+ def set_logger
27
+ @logger = Logger.new "log/migrate_deleted_records_#{DateTime.now.strftime('%Y_%m_%d_%H:%M:%S')}.log"
28
+ end
29
+
30
+ def set_bucket
31
+ @s3 = Aws::S3::Client.new
32
+ end
33
+
34
+ def set_tables
35
+ # @tables = S3::OldDeletedRecordsService::singleton_class::DB_TABLES
36
+ @tables = ['verlof_verzoeken']
37
+ end
38
+
39
+ def set_dates(start_date)
40
+ format_start_date
41
+ @dates = {start_date: @start_date, end_date: Date.today.strftime("%Y-%m-%d")}
42
+ end
43
+
44
+ def format_start_date
45
+ @start_date = @start_date.class == String ? @start_date : @start_date.strftime("%Y-%m-%d")
46
+ end
47
+
48
+ def set_records
49
+ @records = S3::OldDeletedRecordsService.get_records(env_id: @env.id, env_name: @env.naam, db_table: 'verlofverzoeks', **@dates)
50
+ end
51
+
52
+ def set_json
53
+ @json = JSON.pretty_generate(@records)
54
+ end
55
+
56
+ def set_file_name
57
+ @file_name = "env_#{@env.id}_deleted_#{@table}.json"
58
+ end
59
+
60
+ def set_existing_records
61
+ object = @s3.get_object(bucket: BUCKET, key: @file_name)
62
+ @existing_records = JSON.parse(object.body.read.as_json).map(&:symbolize_keys)
63
+
64
+ rescue Aws::S3::Errors::NoSuchKey => e
65
+ @existing_records = nil
66
+ end
67
+
68
+ def compose_file
69
+ set_records
70
+ set_file_name
71
+ set_existing_records
72
+
73
+ (@records += @existing_records) if @existing_records
74
+ set_json
75
+ upload_file
76
+ rescue => e
77
+ @logger.error "Error for env #{@env.naam} (##{@env.id}) with table '#{@table}' => #{e.class}: #{e.message}.\n\nBacktrace:#{e.backtrace}\n"
78
+ end
79
+
80
+ def upload_file
81
+ @s3.put_object(bucket: BUCKET, key: @file_name, body: @json)
82
+ end
83
+
84
+ end
85
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ S3::Engine.routes.draw do
2
+
3
+ get '/deleted_records', to: 'records#deleted_records'
4
+ get '/legacy_deleted_records', to: 'records#legacy_deleted_records'
5
+
6
+ end
data/lib/s3.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "s3/version"
2
+ require "s3/engine"
3
+
4
+ module S3
5
+ # Your code goes here...
6
+ end
data/lib/s3/engine.rb ADDED
@@ -0,0 +1,6 @@
1
+ module S3
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace S3
4
+ config.generators.api_only = true
5
+ end
6
+ end
data/lib/s3/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module S3
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eitje_s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jurriaan Schrofer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-s3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: figaro
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: S3
56
+ email:
57
+ - jschrofer@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/controllers/s3/application_controller.rb
66
+ - app/controllers/s3/records_controller.rb
67
+ - app/services/s3/new_deleted_records_service.rb
68
+ - app/services/s3/old_deleted_records_service.rb
69
+ - app/services/s3/transform_deleted_files_service.rb
70
+ - config/routes.rb
71
+ - lib/s3.rb
72
+ - lib/s3/engine.rb
73
+ - lib/s3/version.rb
74
+ homepage: https://github.com/eitje-app/s3_engine
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/eitje-app/s3_engine
79
+ source_code_uri: https://github.com/eitje-app/s3_engine
80
+ changelog_uri: https://github.com/eitje-app/s3_engine
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: S3
100
+ test_files: []