Pi Horse
Pi Horse

Reputation: 2430

Ruby parsing Array (Special Case)

I am executing a query and getting the following data from the database in an array (MySql2 type object):

 +-----------+---------------+---------------+------+------+---------------+
 | build     | platform_type | category_name | pass | fail | indeterminate |
 +-----------+---------------+---------------+------+------+---------------+
 | 10.0.1.50 | 8k            | UMTS          |   10 |    2 |             5 |   
 | 10.0.1.50 | 8k            | UMTS          |   10 |    2 |             5 | 
 | 10.0.1.50 | 8k            | IP            |   10 |    2 |             5 | 
 | 10.0.1.50 | 8k            | IP            |   14 |    1 |             3 | 
 | 10.0.1.50 | 9k            | IP            |   14 |    1 |             3 | 
 | 10.0.1.50 | 9k            | IP            |   12 |    1 |             1 | 
 | 10.0.1.50 | 9k            | UMTS          |   12 |    1 |             1 | 
 | 10.0.1.50 | 9k            | UMTS          |   12 |    1 |             1 | 
 | 10.0.1.50 | 9k            | UMTS          |   12 |    1 |             1 | 
 | 10.0.1.50 | 9k            | Stability     |    9 |    4 |             0 | 
 | 10.0.1.50 | 9k            | Stability     |   15 |    1 |             0 | 

I want to display it on my UI in a table, something like this :

 +-----------+---------------+---------------+------+------+---------------+
 | build     | platform_type | category_name | pass | fail | indeterminate |
 +-----------+---------------+---------------+------+------+---------------+
 |           |               | UMTS          |   20 |    4 |            10 |
 |           | 8k            |---------------------------------------------|
 |           |               | IP            |   24 |    3 |             8 |
 |           |---------------|---------------------------------------------|
 | 10.0.1.50 |               | IP            |   26 |    2 |             4 |
 |           |               |---------------------------------------------|
 |           | 9k            | UMTS          |   36 |    3 |             3 |
 |           |               |---------------------------------------------|
 |           |               | Stability     |   24 |    5 |             0 |
 ---------------------------------------------------------------------------

I did try using hash to find unique platform types for the build. But as I am very new to ruby, I am having trouble using the hash properly. I would appreciate if someone can help me parse the data.

Upvotes: 2

Views: 103

Answers (1)

Harish Shetty
Harish Shetty

Reputation: 64363

Assuming you have an array of arrays:

@data = sql_results.group_by(&:first).map do |b, bl|   
  [b, bl.group_by(&:second).map{|p, pl| [p, pl.map{|r| r[2..-1]}] }.sort_by(&:first)]
end.sort_by(&:first)

Here is how to break down the logic.

  • group the rows by first column. This will return a hash with key as the first column name and value as an array of rows.
  • group each build list by 2nd column(platform type). Each group should contain the array of col values from 3 to the last col.
  • sort the platform lists by platform type
  • sort the build lists by build name

The resultant structure would like this:

[
  [
    "10.0.1.50", [
      [
        "8k", [
          ["UMTS", 20, 4, 10],
          ["IP", 24, 3, 8]
        ]
      ], 
      [
        "9k", [
          ["IP", 26, 2, 4],
          ["UMTS", 36, 3, 3],
          ["UMTS", 24, 5, 0]
        ]
      ]
    ]
  ]
]

You can use this in your view layout example:

%table
  %tr
    - %w(build platform_type category_name pass fail indeterminate).each do |name|
      %th=name
  - @data.each do |build, build_list|
    %tr
      %td=build
      %td{:colspan=4}
        %table
          - build_list.each do |build, platform_list|
            %tr
              %td=build
              %td{:colspan=3}
                %table
                  - platform_list.each do |row|
                    %tr
                      - row.each do |attr|
                        %td=attr

If you use an AR model here is what you do:

class  Build < ActiveRecord::Base

  def self.builds_by_platform
    reply = Hash.new{|h, k| h[k] = Hash.new{|h, k| h[k] = []}}
    Build.order("build ASC, platform_type ASC").find_each do |row|
      reply[row.build][row.platform_type] << row
    end
    reply.map{|b, bh| [b, bh.sort_by(&:first)}.sort_by(&:first)
  end

end

In your controller you can access the normalized variable as:

@report _list = Build.builds_by_platform

You can use the @report _list variable for rendering the table.

Upvotes: 2

Related Questions