import itertools


def _records_to_params(categories, ignored_count, request_id_url, report_url):
  """Generates and returns a dict to generate an error report.

  Arguments:
    categories: list of _ErrorCategory reports.
    ignored_count: number of errors not reported because ignored.
    request_id_url: base url to link to a specific request_id.
    report_url: base url to use to recreate this report.
  """
  categories = sorted(
      categories, key=lambda e: (e.signature, -e.events.total_count))
  return {
    'error_count': len(categories),
    'errors': categories,
    'ignored_count': ignored_count,
    'occurrence_count': sum(e.events.total_count for e in categories),
    'report_url': report_url,
    'request_id_url': request_id_url,
    'version_count':
        len(set(itertools.chain.from_iterable(e.versions for e in categories))),
  }