This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
history_manual.rb
73 lines (62 loc) · 2.21 KB
/
history_manual.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'active_record'
require 'aws-sdk-s3'
require 'ndjson'
require_relative 'history'
# s3 connection
Aws.config[:region] = 'us-west-2'
Aws.config[:credentials] = Aws::Credentials.new(ENV.fetch('CCHECKS_S3_WRITE_ACCESS_KEY'), ENV.fetch('CCHECKS_S3_WRITE_SECRET_KEY'))
$s3_x = Aws::S3::Resource.new(region: 'us-west-2')
# sql connection
$config = YAML::load_file(File.join(__dir__, 'config.yaml'))
ActiveSupport::Deprecation.silenced = true
ActiveRecord::Base.establish_connection($config['db']['cchecks'])
## History model for querying by date
class HistoryDate < ActiveRecord::Base
self.table_name = 'histories'
def self.query(date)
fields = %w(package summary checks check_details date_updated)
select(fields.join(', '))
.where("DATE(date_updated) = '%s'" % date)
end
end
# class HistoryDate2 < ActiveRecord::Base
# self.table_name = 'histories'
# def self.query(date)
# fields = %w(package)
# select(fields.join(', '))
# .where("DATE(date_updated) = '%s'" % date)
# end
# end
# fme, takes 5 minutes to get data for one date
# hmmmmmmmmm
#
def write_history(date)
puts "pulling data from MariaDB"
z = HistoryDate.query(date); nil
puts "converting to Ruby Hash"
data = z.as_json; nil
puts "writing file to disk"
json_file = date + ".json"
nd = NDJSON::Generator.new json_file
data.each do |x|; nil
nd.write(x); nil
end; nil
# compress json file
compress_file(json_file)
json_file_gz = json_file + ".gz"
# upload
puts "uploading to S3"
obj = $s3_x.bucket("cchecks-history").object(json_file_gz)
obj.upload_file(json_file_gz)
end
# write_history('2019-09-19')
# dates = ["3", "4", "5"].map { |z| "2019-05-0" + z }
# (1.month.ago.to_date..Date.today).map{ |date| date.strftime("%Y-%m-%d") }
# Time.now.beginning_of_month - 1.day
# dates = ("2019-05-01".to_date..Time.now.beginning_of_month).map{ |date| date.strftime("%Y-%m-%d") }
# dates = ("2019-01-01".to_date.."2019-01-31".to_date).map{ |date| date.strftime("%Y-%m-%d") }
# dates = ["2018-12-18",("2018-12-20".to_date.."2018-12-31".to_date).map{ |date| date.strftime("%Y-%m-%d") }].flatten
dates = ("2019-09-20".to_date.."2019-09-26".to_date).map{ |date| date.strftime("%Y-%m-%d") }
dates.each do |z|
write_history(z)
end