<commit_before>// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:meta/meta.dart';
import 'package:pana/models.dart' show Report, ReportSection, SuggestionLevel;

import '../../analyzer/analyzer_client.dart';
import '../../scorecard/models.dart';
import '../../shared/markdown.dart';
import '../../shared/urls.dart' as urls;
import '../../shared/utils.dart';

import '../color.dart';
import '../request_context.dart';

import '_cache.dart';
import '_consts.dart';
import 'misc.dart';

/// Renders the `views/pkg/analysis/tab.mustache` template.
String renderAnalysisTab(
  String package,
  String sdkConstraint,
  ScoreCardData card,
  AnalysisView analysis, {
  @required int likeCount,
}) {
  if (card == null || analysis == null || !analysis.hasAnalysisData) {
    return '<i>Awaiting analysis to complete.</i>';
  }

  String statusText;
  switch (analysis.panaReportStatus) {
    case ReportStatus.aborted:
      statusText = 'aborted';
      break;
    case ReportStatus.failed:
      statusText = 'tool failures';
      break;
    case ReportStatus.success:
      statusText = 'completed';
      break;
    default:
      break;
  }

  // TODO: use only `analysis.report` after we've migrated to the new UI.
  final report =
      requestContext.isExperimental ? _extendedReport(analysis) : null;
  final hasReport = report != null;

  final Map<String, dynamic> data = {
    'package': package,
    'show_discontinued': card.isDiscontinued,
    'show_outdated': card.isObsolete,
    'show_legacy': card.isLegacy,
    'show_analysis': !card.isSkipped,
    'analysis_tab_url': urls.pkgScoreUrl(package),
    'date_completed': analysis.timestamp == null
        ? null
        : shortDateFormat.format(analysis.timestamp),
    'analysis_status': statusText,
    'dart_sdk_version': analysis.dartSdkVersion,
    'pana_version': analysis.panaVersion,
    'flutter_version': analysis.flutterVersion,
    'analysis_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml('Analysis', analysis.panaSuggestions),
    'health_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml('Health', analysis.healthSuggestions),
    'maintenance_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml(
            'Maintenance', analysis.maintenanceSuggestions),
    'dep_table_html': _renderDepTable(sdkConstraint, card, analysis),
    'report_html': _renderReport(report),
    'like_key_figure_html': _renderLikeKeyFigure(likeCount),
    'popularity_key_figure_html':
        _renderPopularityKeyFigure(card.popularityScore),
    'pubpoints_key_figure_html': _renderPubPointsKeyFigure(report),
    // TODO: remove after we've migrated to the new UI
    'score_table_html': _renderScoreTable(card),
  };

  return templateCache.renderTemplate('pkg/analysis/tab', data);
}

Report _extendedReport(AnalysisView analysis) {
  if (analysis == null || !analysis.hasPanaSummary) return null;
  final healthScore = (analysis.health * 100).round();
  final maintenanceScore = (analysis.maintenanceScore * 100).round();

  String summaryMarkdown(List<Suggestion> suggestions) {
    return (suggestions ?? [])
        .map((s) => '**${s.title}** (${s.score})\n\n${s.description}')
        .join('\n\n');
  }

  final storedReport = analysis.report;

  return Report(sections: [
    if (storedReport?.sections != null) ...storedReport.sections,
    if (analysis.panaSuggestions != null && analysis.panaSuggestions.isNotEmpty)
      ReportSection(
        title: 'Analysis (pana suggestions)',
        grantedPoints: 0,
        maxPoints: 0,
        summary: summaryMarkdown(analysis.panaSuggestions),
      ),
    ReportSection(
      title: 'Health (old)',
      grantedPoints: healthScore,
      maxPoints: 100,
      summary: summaryMarkdown(analysis.healthSuggestions),
    ),
    ReportSection(
      title: 'Maintenance (old)',
      grantedPoints: maintenanceScore,
      maxPoints: 100,
      summary: summaryMarkdown(analysis.maintenanceSuggestions),
    ),
  ]);
}

/// Renders the `views/pkg/analysis/report.mustache` template.
String _renderReport(Report report) {
  if (report?.sections == null) return null;
  return templateCache.renderTemplate('pkg/analysis/report', {
    'sections': report.sections.map((s) => {
          'title': s.title,<commit_msg>Remove old suggestions from the new report display (and scores). (#3759)<commit_after>// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:meta/meta.dart';
import 'package:pana/models.dart' show Report, SuggestionLevel;

import '../../analyzer/analyzer_client.dart';
import '../../scorecard/models.dart';
import '../../shared/markdown.dart';
import '../../shared/urls.dart' as urls;
import '../../shared/utils.dart';

import '../color.dart';
import '../request_context.dart';

import '_cache.dart';
import '_consts.dart';
import 'misc.dart';

/// Renders the `views/pkg/analysis/tab.mustache` template.
String renderAnalysisTab(
  String package,
  String sdkConstraint,
  ScoreCardData card,
  AnalysisView analysis, {
  @required int likeCount,
}) {
  if (card == null || analysis == null || !analysis.hasAnalysisData) {
    return '<i>Awaiting analysis to complete.</i>';
  }

  String statusText;
  switch (analysis.panaReportStatus) {
    case ReportStatus.aborted:
      statusText = 'aborted';
      break;
    case ReportStatus.failed:
      statusText = 'tool failures';
      break;
    case ReportStatus.success:
      statusText = 'completed';
      break;
    default:
      break;
  }

  // TODO: use only `analysis.report` after we've migrated to the new UI.
  final report = requestContext.isExperimental ? analysis.report : null;
  final hasReport = report != null;

  final Map<String, dynamic> data = {
    'package': package,
    'show_discontinued': card.isDiscontinued,
    'show_outdated': card.isObsolete,
    'show_legacy': card.isLegacy,
    'show_analysis': !card.isSkipped,
    'analysis_tab_url': urls.pkgScoreUrl(package),
    'date_completed': analysis.timestamp == null
        ? null
        : shortDateFormat.format(analysis.timestamp),
    'analysis_status': statusText,
    'dart_sdk_version': analysis.dartSdkVersion,
    'pana_version': analysis.panaVersion,
    'flutter_version': analysis.flutterVersion,
    'analysis_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml('Analysis', analysis.panaSuggestions),
    'health_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml('Health', analysis.healthSuggestions),
    'maintenance_suggestions_html': hasReport
        ? null
        : _renderSuggestionBlockHtml(
            'Maintenance', analysis.maintenanceSuggestions),
    'dep_table_html': _renderDepTable(sdkConstraint, card, analysis),
    'report_html': _renderReport(report),
    'like_key_figure_html': _renderLikeKeyFigure(likeCount),
    'popularity_key_figure_html':
        _renderPopularityKeyFigure(card.popularityScore),
    'pubpoints_key_figure_html': _renderPubPointsKeyFigure(report),
    // TODO: remove after we've migrated to the new UI
    'score_table_html': _renderScoreTable(card),
  };

  return templateCache.renderTemplate('pkg/analysis/tab', data);
}

/// Renders the `views/pkg/analysis/report.mustache` template.
String _renderReport(Report report) {
  if (report?.sections == null) return null;
  return templateCache.renderTemplate('pkg/analysis/report', {
    'sections': report.sections.map((s) => {
          'title': s.title,
{'fraction_non_alphanumeric': 0.12522292993630574, 'fraction_numerical': 0.0033121019108280254, 'mean_word_length': 3.752421307506053, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5037113', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2304, 'n_words': 530}