Skip to content

Commit

Permalink
dowloader finished
Browse files Browse the repository at this point in the history
  • Loading branch information
kostik committed Oct 16, 2023
1 parent 8ca8a4b commit 3e6bc86
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
23 changes: 22 additions & 1 deletion app/models/study_downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ def self.download(nct_ids)

nct_ids.each do |nct_id|
record = StudyJsonRecord.find_by(nct_id: nct_id) || StudyJsonRecord.create(nct_id: nct_id, content: {})
record.update_from_api
update_from_apiV2(record, nct_id)
puts "Study id: #{record.id}, study nct_id: #{record.nct_id}, version: #{record.version}"
end
# example for testing ['NCT02071602', 'NCT00430612', 'NCT00785954', 'NCT00103350', 'NCT03445728', 'NCT03649711']

end


def self.update_from_apiV2(record, nct_id)
data = nil
response = nil
attempts = 0
begin
attempts += 1
s = Time.now
body = Faraday.get("https://clinicaltrials.gov/api/v2/studies/#{nct_id}").body
record.update(content: JSON.parse(body), version: "2")
rescue Faraday::ConnectionFailed
return false if attempts > 5
retry
rescue JSON::ParserError
return false if attempts > 5
retry
end
end

end
1 change: 1 addition & 0 deletions app/models/study_json_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def update_from_api
end
end


###### Utils ######

def key_check(key)
Expand Down
16 changes: 15 additions & 1 deletion db/admin_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ SET row_security = off;
CREATE SCHEMA ctgov;


--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA public;


--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON SCHEMA public IS 'standard public schema';


--
-- Name: support; Type: SCHEMA; Schema: -; Owner: -
--
Expand Down Expand Up @@ -4544,7 +4558,7 @@ ALTER TABLE ONLY support.file_records
-- PostgreSQL database dump complete
--

SET search_path TO ctgov, support, public;
SET search_path TO ctgov,support,public;

INSERT INTO "schema_migrations" (version) VALUES
('20160630191037'),
Expand Down
16 changes: 15 additions & 1 deletion db/public_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ SET row_security = off;
CREATE SCHEMA ctgov;


--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA public;


--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON SCHEMA public IS 'standard public schema';


--
-- Name: support; Type: SCHEMA; Schema: -; Owner: -
--
Expand Down Expand Up @@ -4544,7 +4558,7 @@ ALTER TABLE ONLY support.file_records
-- PostgreSQL database dump complete
--

SET search_path TO ctgov, support, public;
SET search_path TO ctgov,support,public;

INSERT INTO "schema_migrations" (version) VALUES
('20160630191037'),
Expand Down

0 comments on commit 3e6bc86

Please sign in to comment.