Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails中,在保存到数据库之前对文件中的数据进行汇总_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 在RubyonRails中,在保存到数据库之前对文件中的数据进行汇总

Ruby on rails 在RubyonRails中,在保存到数据库之前对文件中的数据进行汇总,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,我有一个概念性的问题;我试图编写代码从s3下载日志,然后在rails应用程序中解析并将其中的一些数据存储在数据库中 由于这完全是内部的,所以我只有一个模型,其中包含下载和解析日志所需的代码。我的主要解析方法是打开一个文件,遍历每一行,解析出我想保存到数据库中的某些数据 我的目标是汇总一个包含多个日志的文件中的所有数据,然后将其保存到数据库中 我很难理解的是,在将数据保存到Rails中的数据库之前,我将如何总结数据 例如,如果我有以下日志: 日志/帐户/6 100 日志/帐户/7 250 日志/帐

我有一个概念性的问题;我试图编写代码从s3下载日志,然后在rails应用程序中解析并将其中的一些数据存储在数据库中

由于这完全是内部的,所以我只有一个模型,其中包含下载和解析日志所需的代码。我的主要解析方法是打开一个文件,遍历每一行,解析出我想保存到数据库中的某些数据

我的目标是汇总一个包含多个日志的文件中的所有数据,然后将其保存到数据库中

我很难理解的是,在将数据保存到Rails中的数据库之前,我将如何总结数据

例如,如果我有以下日志:

日志/帐户/6 100 日志/帐户/7 250 日志/帐户/6 50 日志/帐户/5 100

我的目标是遍历所有行,并保存每个帐户ID的合计金额,因此在这个原因中,我希望为帐户6150保存总和。由于某些原因,我只能理解1个日志的1个数据库条目,而不能汇总文件中的日志并将其转换为1个数据库条目

当前分析过程:

   def self.create_from_log_file(file)
    s3log = File.open(file).each do |line|
    line_match = S3_LINE_REGEXP.match(line)# get the matchdata
    captures = Hash[ line_match.names.zip( line_match.captures ) ]# convert the matchdata to a hash key value pairs (both strings)
    validate_log_file(captures["timestamp"])# validate file is unique
    captures["http_status"] != 200 # figure out if API request was a http 200
    current_account = extract_account_id(captures["request_path"])# extract account id and find that account
    account_log = S3Log.new # instantiate a new S3Log instance
    account_log.account_id = Account.find_by_id(current_account) # assign the S3Log object its account id
    account_log.total_bytes = calculate_total_bytes_for_file(captures["bytes_sent"])# assign the log bytes to that accounts total for the file
    account_log.total_requests = calculate_total_requests_for_file(acount_log.account_id)# calculate total requests for that account on the file
    account_log.date = Date.parse(captures["timestamp"])
  end

  account_log.save!
end

一些高级指针。首先,由于您的代码可能是一个运行时间较长的作业,因此使用或作为后台作业运行它可能是值得的

其次,将您的工作分解为定义良好的小函数,然后为这些小函数编写测试。然后,您将有信心将它们组合成更大的部分,即练习功能分解。或者,采用面向对象的方式,创建模型来封装解析逻辑,另一个模型表示感兴趣的行,可能还有第三个模型表示可以在其上执行聚合方法的行集合


希望这有帮助。

您应该使用控制器,然后在调用该模型对象的“保存”之前,可以打印出对象数据。