Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flat File Export and Daily Update Task #338

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions app/controllers/api/v1/data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class DataController < ApplicationController
def index

@data ||= DataView.all

# Specify the file name you want to check in the public directory
file_path = Rails.root.join("public", "all_data.json")

# Check if none of the params keys start with "query_" and if the file exists
if params.keys.none? { |key| key.to_s.start_with?('query_') } && File.exist?(file_path)
send_file(file_path)
return
end

# lab_identifier (v1: labnr)
unless params[:query_labnr].nil?
Expand Down Expand Up @@ -66,13 +75,8 @@ def index
)
end

logger.debug "====================="
logger.debug DataView.select("json_agg(json_build_object('measurement', measurement)) AS measurements").from(@data).to_sql
logger.debug "====================="

#render json: @data.to_json
render json: C14.connection.exec_query(DataView.select("json_agg(json_build_object('measurement', subquery)) AS measurements").from(@data).to_sql)[0]['measurements'], adapter: nil, serializer: nil

render json: C14.connection.exec_query(DataView.select("json_agg(json_build_object('measurement', subquery)) AS measurements").from(@data).to_sql)[0]['measurements'], adapter: nil, serializer: nil
end

def show
Expand Down
14 changes: 14 additions & 0 deletions app/models/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def filter_value_string(*args)
@filters.dig(*args).join(",") if @filters.dig(*args).present?
end

def self.store_data_as_json
query = <<-SQL
SELECT json_agg(json_build_object('measurement', subquery)) AS measurements
FROM (SELECT "data_views".* FROM "data_views") subquery
SQL

result = C14.connection.exec_query(query)
measurements = result[0]['measurements']

File.open(Rails.root.join('public', 'all_data.json'), 'w') do |file|
file.write(measurements)
end
end

private

def decode_range_filters(filters)
Expand Down
8 changes: 7 additions & 1 deletion config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
every :day, at: '12:00 am' do
rake "refreshers:data_views"
end
end
end

if @environment == 'production'
every 1.day, at: '12:00 am' do
runner "Data.store_data_as_json"
end
end
Loading