为非常大的哈希创建计数器(rails)

时间:2013-06-18 19:55:09

标签: ruby ruby-on-rails-3

所以我试图使用这个gem从Jira的rest API中解析出一些信息:https://github.com/sumoheavy/jira-ruby

我正在创建一个报告应用,它会计算特定“受让人”在特定项目中解决问题的次数。

哈希看起来像这样......

[#<JIRA::Resource::Issue:329487 @attrs={'key'=>'value','key'=>'value', "fields"=> {project=>{'key'=>'value'}, assignee=>{'key'=>'value'}}

我特别需要获得受让人的价值和项目的价值,然后以某种方式将它们列入一个表格,列出每个受让人,以及他们在每个项目中完成的问题。

由于我正在使用的查询,因此出现的问题只是已完成的问题,所以不要担心,只需解析它并使用模型中的某些方法对其进行计数。 / p>

我现在可以通过在视图中使用它来制作一个很好的解析表(不会得到我需要的信息)...

`<table>
  <thead>
    <th>Count</th>
    <th>Issue Key</th>
    <th>Summary</th>
    <th>Time Estimate</th>
    <th>Reporter</th>
    <th>Assignee</th>
  </thead>
<% @issues.each_with_index do |issue, index| %>
  <tr>
    <td><%= index + 1 %>  </td>
    <td><%= issue.key %> </td>
    <td><%= issue.fields['summary'] %>  </td>
    <td><%= issue.fields['aggregatetimeestimate'] %> </td>
    <td><%= issue.fields['reporter'].values[4] %> </td>
  <% if issue.fields['assignee'].present? %>
    <td><%= issue.fields['assignee'].values[4] %></td>
  <% else %>
    <td>N/A</td>
  <% end %>
  </tr>
<% end %>
</table>`

我更愿意看到的......是一个表,其中不同的受让人作为行,不同的项目作为列,然后是每个项目具有该特定受让人的问题的数量,然后是对。因此,我需要遍历所有受理人,组合所有相同的受理人,并计算列中每个项目的计数。

我不想使用activerecord,因为我没有保存到数据库。数据库是通过API检索的信息,因此它不断变化。

请记住,项目哈希,受理人哈希是他们自己的哈希,并且每个哈希在整个问题哈希中都有几个级别。 {问题哈希=&gt; {fields hash =&gt; {project hash} {assignee hash}}}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

试试这个:

# Controller
# {Assignee => {Project => Issues closed} }
@assignee_hash = Hash.new { |hash, key| hash[key] = Hash.new(0) }
@projects = Array.new
@issues.each do |issue|
    # I'm not familiar with the structure of the assignee/project
    # hashes here (and 'key'=>'value' isn't too descriptive),
    # but these next 2 lines should be modified to access whatever 
    # value you want to identify the assignee and project by
    assignee = issue.fields['assignee'].??
    project  = issue.fields['project'].??
    @projects.push project unless projects.include? project
    @assignee_hash[assignee][project] += 1
end

#View
<table>
  <thead>
    <th>Assignee</th>
    <% @projects.each do |project| %>
      <th><%= project %></th>
    <% end %>
    <th>Total</th>
  </thead>
  <% @assignee_hash.each do |assignee, project_hash| %>
    <tr>
      <td><%= assignee %></td>
      <% @projects.each do |project| %>
        <td><%= project_hash[project] %></td>
      <% end %>
      <td><%= project_hash.values.reduce(:+) %></td>
    </tr>
  <% end %>
</table>
相关问题