From 9b7da3519aedbec9c884bdb2e3818baca1c126a4 Mon Sep 17 00:00:00 2001 From: carlad Date: Tue, 2 Aug 2016 16:02:52 +0200 Subject: [PATCH] establish connection to logs db, update result, update query --- lib/travis/api/v3/models/log.rb | 3 ++- lib/travis/api/v3/models/log_part.rb | 1 + lib/travis/api/v3/queries/log.rb | 30 ++++++++++++-------------- lib/travis/api/v3/services/log/find.rb | 1 + 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/travis/api/v3/models/log.rb b/lib/travis/api/v3/models/log.rb index d1c3219b..75555014 100644 --- a/lib/travis/api/v3/models/log.rb +++ b/lib/travis/api/v3/models/log.rb @@ -1,5 +1,6 @@ module Travis::API::V3 - class Models::Log < Model + class Models::Log < Model + establish_connection 'logs_database' belongs_to :job belongs_to :removed_by, class_name: 'User', foreign_key: :removed_by has_many :log_parts, dependent: :destroy diff --git a/lib/travis/api/v3/models/log_part.rb b/lib/travis/api/v3/models/log_part.rb index 43fc7370..103ef0e1 100644 --- a/lib/travis/api/v3/models/log_part.rb +++ b/lib/travis/api/v3/models/log_part.rb @@ -1,5 +1,6 @@ module Travis::API::V3 class Models::LogPart < Model + establish_connection 'logs_database' belongs_to :log end end diff --git a/lib/travis/api/v3/queries/log.rb b/lib/travis/api/v3/queries/log.rb index e799c9ed..460de7ea 100644 --- a/lib/travis/api/v3/queries/log.rb +++ b/lib/travis/api/v3/queries/log.rb @@ -4,24 +4,22 @@ module Travis::API::V3 require 'uri' def find(job) - #check for the log in the DB + #check for the log in the Logs DB log = Models::Log.find_by_job_id - #if the log exists and has not been archived yet, then collect the log_parts and return the contents - unless log.nil? || !log.archived_at.nil? - log_parts = Models::Log::Part.where(log_id: log.id) - log_data = [] - log_parts.each { |log_part| log_data << log_part.content } - log_data + #if the log exists and has not been archived yet, then collect the log_parts and return the Log query object + unless !log.archived_at.nil? + log_parts = Models::LogPart.where(log_id: log.id).to_a elsif log.archived_at? - # if it's not there then fetch it from S3. - archived_log_path = archive_url("/jobs/#{params[:job.id]}/log.txt") - - content = open(Net::HTTP.get(URI.parse(archived_log_path))) - archived_log_data = [] - content.each_line do |line| - archived_log_data << line.chop - end - archived_log_data + ## if it's not there then fetch it from S3, and return it wrapped as a compatible log_parts object with a hard coded #number (log_parts have a number) and the parts chunked (not sure how to do this) + # archived_log_path = archive_url("/jobs/#{params[:job.id]}/log.txt") + # content = open(Net::HTTP.get(URI.parse(archived_log_path))) + # log_parts = [] + # content.each_line do |line| + # log_parts << line.chop + # end + # log_parts + else + raise EntityMissing, 'log not found'.freeze end end diff --git a/lib/travis/api/v3/services/log/find.rb b/lib/travis/api/v3/services/log/find.rb index 0bc78e72..947e0df5 100644 --- a/lib/travis/api/v3/services/log/find.rb +++ b/lib/travis/api/v3/services/log/find.rb @@ -5,6 +5,7 @@ module Travis::API::V3 def run! job = find(:job) query.find(job) + result(log, parts: log_parts) end end end