Class: McpServer::StatusTimeAnalysisTool

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/jirametrics/mcp_server.rb

Class Method Summary collapse

Class Method Details

.call(server_context:, project: nil, project_name: nil, issue_state: 'all', group_by: 'status', column: nil) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/jirametrics/mcp_server.rb', line 480

def self.call(server_context:, project: nil, project_name: nil, issue_state: 'all', group_by: 'status',
              column: nil, **)
  project ||= project_name
  group_by = 'column' if column

  totals = Hash.new { |h, k| h[k] = { total_seconds: 0.0, visit_count: 0 } }
  allowed_projects = McpServer.resolve_projects(server_context, project)

  server_context[:projects].each do |project_name, project_data|
    next if allowed_projects && !allowed_projects.include?(project_name)

    project_data[:issues].each do |issue|
      next unless select_issues(issue, issue_state)

      time_map = if group_by == 'column'
                   McpServer.time_per_column(issue, project_data[:end_time])
                 else
                   McpServer.time_per_status(issue, project_data[:end_time])
                 end

      time_map.each do |name, seconds|
        totals[name][:total_seconds] += seconds
        totals[name][:visit_count] += 1
      end
    end
  end

  return MCP::Tool::Response.new([{ type: 'text', text: 'No data found.' }]) if totals.empty?

  rows = totals.map do |name, data|
    total_days = (data[:total_seconds] / 86_400.0).round(1)
    avg_days = (data[:total_seconds] / data[:visit_count] / 86_400.0).round(1)
    { name: name, total_days: total_days, avg_days: avg_days, visit_count: data[:visit_count] }
  end
  rows.sort_by! { |r| -r[:avg_days] }

  label = group_by == 'column' ? 'Column' : 'Status'
  lines = rows.map do |r|
    "#{label}: #{r[:name]} | Avg: #{r[:avg_days]}d | Total: #{r[:total_days]}d | Issues: #{r[:visit_count]}"
  end
  MCP::Tool::Response.new([{ type: 'text', text: lines.join("\n") }])
end

.select_issues(issue, issue_state) ⇒ Object



470
471
472
473
474
475
476
477
478
# File 'lib/jirametrics/mcp_server.rb', line 470

def self.select_issues issue, issue_state
  started, stopped = issue.started_stopped_times
  case issue_state
  when 'aging' then started && !stopped
  when 'completed' then !!stopped
  when 'not_started' then !started && !stopped
  else true
  end
end