def get_error_summary()

in treeherder/model/error_summary.py [0:0]


def get_error_summary(job, queryset=None):
    """
    Create a list of bug suggestions for a job.
    TextLogError queryset can be passed directly to avoid firing a DB query.

    Caches the results if there are any.
    """
    cache_key = f"error-summary-{job.id}"
    cached_error_summary = cache.get(cache_key)
    if cached_error_summary is not None:
        return cached_error_summary

    # add support for error line caching
    if job.repository == "comm-central":
        lcache = MemDBCache("cc_error_lines")
    else:
        lcache = MemDBCache("mc_error_lines")

    date = str(job.submit_time.date())
    line_cache = lcache.get_cache()
    if date not in line_cache.keys():
        lcache.write_cache(date, {})
    else:
        dates = lcache.get_cache_keys()
        dates.sort()
        for d in dates:
            date_time = datetime.datetime.strptime(d, "%Y-%m-%d")
            if date_time <= (job.submit_time - datetime.timedelta(days=LINE_CACHE_TIMEOUT_DAYS)):
                lcache.remove_cache_key(d)
            else:
                break

    if queryset is None:
        queryset = TextLogError.objects.filter(job=job).order_by("id")
    # don't cache or do anything if we have no text log errors to get results for
    if not queryset:
        return []

    # cache terms generated from error line to save excessive querying
    term_cache = {}

    error_summary = []
    # Future suggestion, set this to queryset[:10] to reduce calls to bug_suggestions_line
    for err in queryset:
        summary, line_cache = bug_suggestions_line(
            err,
            project=job.repository,
            logdate=job.submit_time,
            term_cache=term_cache,
            line_cache=line_cache,
            revision=job.push.revision,
        )
        error_summary.append(summary)

    try:
        cache.set(cache_key, error_summary, BUG_SUGGESTION_CACHE_TIMEOUT)
    except Exception as e:
        newrelic.agent.record_custom_event("error caching error_summary for job", job.id)
        logger.error("error caching error_summary for job %s: %s", job.id, e, exc_info=True)

    try:
        lcache.update_cache(date, line_cache[date])
        # TODO: consider reducing this, each date is ~5%, so it will be faster
        lcache.update_db_cache(date, line_cache[date])
    except Exception as e:
        newrelic.agent.record_custom_event("error caching error_lines for job", job.id)
        logger.error("error caching error_lines for job %s: %s", job.id, e, exc_info=True)

    return error_summary