Module: S3::OldDeletedRecordsService

Defined in:
app/services/s3/old_deleted_records_service.rb

Constant Summary collapse

DB_TABLES =
%w$ shifts teams users contracts infos posts topics verlof_verzoeken$

Class Method Summary collapse

Class Method Details

.filter_by_dateObject



73
74
75
# File 'app/services/s3/old_deleted_records_service.rb', line 73

def filter_by_date   
  @file_names_filtered_by_table.select { |file_name| @date_range.include?(get_date(file_name)) }
end

.filter_by_env_idObject



57
58
59
# File 'app/services/s3/old_deleted_records_service.rb', line 57

def filter_by_env_id
  @file.select { |row| row[:environment_id] == @env_id }
end

.filter_by_env_nameObject



61
62
63
# File 'app/services/s3/old_deleted_records_service.rb', line 61

def filter_by_env_name
  @file.select { |row| row[:env] == @env_name }
end

.filter_file_by_envObject

filter methods



53
54
55
# File 'app/services/s3/old_deleted_records_service.rb', line 53

def filter_file_by_env
  has_env_id ? filter_by_env_id : filter_by_env_name
end

.filter_topics_table_by_envObject



69
70
71
# File 'app/services/s3/old_deleted_records_service.rb', line 69

def filter_topics_table_by_env
  @file.select { |row| row[:environment_ids]&.include?(@env_id) }
end

.filter_users_table_by_envObject



65
66
67
# File 'app/services/s3/old_deleted_records_service.rb', line 65

def filter_users_table_by_env
  @file.select { |row| row[:envs].include? @env_name }
end

.get_date(file_name) ⇒ Object



87
88
89
# File 'app/services/s3/old_deleted_records_service.rb', line 87

def get_date(file_name)
  file_name.match(/\d{4}-\d{2}-\d{2}/).to_s
end

.get_date_rangeObject

date methods



83
84
85
# File 'app/services/s3/old_deleted_records_service.rb', line 83

def get_date_range
  (Date.parse(@start_date)..Date.parse(@end_date)).to_a.map {|x| x.strftime("%Y-%m-%d") }
end

.get_records(db_table:, start_date:, end_date:, env_id:, env_name:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/s3/old_deleted_records_service.rb', line 6

def get_records(db_table:, start_date:, end_date:, env_id:, env_name:)

  # validate_args(db_table)
  
  @start_date = start_date
  @end_date   = end_date
  @db_table   = db_table
  @env_name   = env_name
  @env_id     = env_id
  @date_range = get_date_range

  @file_names_filtered_by_table = request_object_names
  @file_names_filtered_by_date  = filter_by_date

  read_all_files
end

.has_env_idObject



77
78
79
# File 'app/services/s3/old_deleted_records_service.rb', line 77

def has_env_id
  @file.first.has_key?(:env_id)
end

.read_all_filesObject

base method



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/s3/old_deleted_records_service.rb', line 31

def read_all_files
  @file_names_filtered_by_date.map do |file_name|       
    
    @file_name = file_name
    @file      = request_object 

    next unless @file

    case @db_table
    when 'users'
      filter_users_table_by_env
    when 'topics'
      filter_topics_table_by_env
    else
      filter_file_by_env
    end

  end.flatten.compact
end

.request_objectObject



109
110
111
112
113
114
115
116
# File 'app/services/s3/old_deleted_records_service.rb', line 109

def request_object
  s3     = Aws::S3::Client.new
  object = s3.get_object(bucket: 'eitje-backups', key: @file_name)
  body   = object.body.read.as_json
  json   = JSON.parse(body).map(&:symbolize_keys)
  rescue JSON::ParserError => e
    puts "Invalid file... skip this file"
end

.request_object_namesObject

AWS methods



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/s3/old_deleted_records_service.rb', line 93

def request_object_names
  folder = @db_table
  folder = "verlofverzoeks" if @db_table == "verlof_verzoeken"

  s3      = Aws::S3::Client.new

  objects = []

  # Pagination: AWS returns 1000 items at max
  s3.list_objects(bucket: 'eitje-backups', prefix: folder).each do |response|
    objects += response.contents
  end

  names = objects.collect &:key
end

.validate_args(db_table) ⇒ Object

validations



25
26
27
# File 'app/services/s3/old_deleted_records_service.rb', line 25

def validate_args(db_table)
  throw :db_table_name_is_not_valid unless DB_TABLES.include?(db_table)
end