<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 'dart:async';

import 'package:shelf/shelf.dart' as shelf;

import '../../package/name_tracker.dart';
import '../../package/search_adapter.dart';
import '../../search/search_form.dart';
import '../../search/search_service.dart';
import '../../shared/handlers.dart';
import '../../shared/tags.dart';
import '../../shared/urls.dart' as urls;
import '../../shared/utils.dart' show DurationTracker;

import '../templates/listing.dart';

final _searchOverallLatencyTracker = DurationTracker();

Map searchDebugStats() {
  return {
    'overall_latency': _searchOverallLatencyTracker.toShortStat(),
  };
}

/// Handles /packages - package listing
Future<shelf.Response> packagesHandlerHtml(shelf.Request request) =>
    _packagesHandlerHtmlCore(request, context: SearchContext.regular());

/// Handles /dart/packages
Future<shelf.Response> dartPackagesHandlerHtml(shelf.Request request) async {
  return await _packagesHandlerHtmlCore(request, context: SearchContext.dart());
}

/// Handles /flutter/packages
Future<shelf.Response> flutterPackagesHandlerHtml(shelf.Request request) {
  return _packagesHandlerHtmlCore(
    request,
    context: SearchContext.flutter(),
  );
}

/// Handles /flutter/favorites
Future<shelf.Response> flutterFavoritesPackagesHandlerHtml(
  shelf.Request request,
) {
  return _packagesHandlerHtmlCore(
    request,
    context: SearchContext.flutterFavorites(),
    title: 'Flutter Favorite packages',
    searchPlaceholder: 'Search Flutter favorite packages',
  );
}

/// Handles /web/packages
Future<shelf.Response> webPackagesHandlerHtml(shelf.Request request) async {
  return redirectResponse(
    urls.searchUrl(
      context: SearchContext.dart(),
      runtimes: [DartSdkRuntime.web],
      q: request.requestedUri.queryParameters['q'],
    ),
  );
}

/// Handles:
/// - /packages - package listing
/// - /dart/packages
/// - /flutter/packages
Future<shelf.Response> _packagesHandlerHtmlCore(
  shelf.Request request, {
  required SearchContext context,
  String? title,
  String? searchPlaceholder,
}) async {
  final openSections =
      request.requestedUri.queryParameters['open-sections']?.split(' ').toSet();
  final searchForm =
      SearchForm.parse(context, request.requestedUri.queryParameters);<commit_msg>Redirect SDK search contexts to new search page. (#5310)<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 'dart:async';

import 'package:shelf/shelf.dart' as shelf;

import '../../package/name_tracker.dart';
import '../../package/search_adapter.dart';
import '../../search/search_form.dart';
import '../../search/search_service.dart';
import '../../shared/handlers.dart';
import '../../shared/tags.dart';
import '../../shared/urls.dart' as urls;
import '../../shared/utils.dart' show DurationTracker;

import '../request_context.dart';
import '../templates/listing.dart';

final _searchOverallLatencyTracker = DurationTracker();

Map searchDebugStats() {
  return {
    'overall_latency': _searchOverallLatencyTracker.toShortStat(),
  };
}

/// Handles /packages - package listing
Future<shelf.Response> packagesHandlerHtml(shelf.Request request) =>
    _packagesHandlerHtmlCore(request, context: SearchContext.regular());

/// Handles /dart/packages
Future<shelf.Response> dartPackagesHandlerHtml(shelf.Request request) async {
  if (requestContext.showNewSearchUI) {
    final newUrl = SearchForm(query: request.requestedUri.queryParameters['q'])
        .toggleRequiredTag(SdkTag.sdkDart)
        .toSearchLink();
    return redirectResponse(newUrl);
  }
  return await _packagesHandlerHtmlCore(request, context: SearchContext.dart());
}

/// Handles /flutter/packages
Future<shelf.Response> flutterPackagesHandlerHtml(shelf.Request request) async {
  if (requestContext.showNewSearchUI) {
    final newUrl = SearchForm(query: request.requestedUri.queryParameters['q'])
        .toggleRequiredTag(SdkTag.sdkFlutter)
        .toSearchLink();
    return redirectResponse(newUrl);
  }
  return await _packagesHandlerHtmlCore(
    request,
    context: SearchContext.flutter(),
  );
}

/// Handles /flutter/favorites
Future<shelf.Response> flutterFavoritesPackagesHandlerHtml(
  shelf.Request request,
) {
  return _packagesHandlerHtmlCore(
    request,
    context: SearchContext.flutterFavorites(),
    title: 'Flutter Favorite packages',
    searchPlaceholder: 'Search Flutter favorite packages',
  );
}

/// Handles /web/packages
Future<shelf.Response> webPackagesHandlerHtml(shelf.Request request) async {
  if (requestContext.showNewSearchUI) {
    final newUrl = SearchForm(query: request.requestedUri.queryParameters['q'])
        .toggleRequiredTag(FlutterSdkTag.platformWeb)
        .toSearchLink();
    return redirectResponse(newUrl);
  }
  return redirectResponse(
    urls.searchUrl(
      context: SearchContext.dart(),
      runtimes: [DartSdkRuntime.web],
      q: request.requestedUri.queryParameters['q'],
    ),
  );
}

/// Handles:
/// - /packages - package listing
/// - /dart/packages
/// - /flutter/packages
Future<shelf.Response> _packagesHandlerHtmlCore(
  shelf.Request request, {
  required SearchContext context,
  String? title,
  String? searchPlaceholder,
}) async {
  final openSections =
      request.requestedUri.queryParameters['open-sections']?.split(' ').toSet();
  final searchForm =
      SearchForm.parse(context, request.requestedUri.queryParameters);
{'fraction_non_alphanumeric': 0.1321898821898822, 'fraction_numerical': 0.002079002079002079, 'mean_word_length': 4.186882300089847, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '1543883', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1705, 'n_words': 371}