content: /*
   *  Copyright 2016-2024 Qameta Software Inc
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package io.qameta.allure.suites;
  
  import io.qameta.allure.CommonCsvExportAggregator2;
  import io.qameta.allure.CommonJsonAggregator2;
  import io.qameta.allure.CompositeAggregator2;
  import io.qameta.allure.Constants;
  import io.qameta.allure.core.LaunchResults;
  import io.qameta.allure.csv.CsvExportSuite;
  import io.qameta.allure.entity.TestResult;
  import io.qameta.allure.tree.TestResultTree;
  import io.qameta.allure.tree.TestResultTreeGroup;
  import io.qameta.allure.tree.Tree;
  import io.qameta.allure.tree.TreeWidgetData;
  import io.qameta.allure.tree.TreeWidgetItem;
  
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.Comparator;
  import java.util.List;
  import java.util.stream.Collectors;
  
  import static io.qameta.allure.entity.LabelName.PARENT_SUITE;
  import static io.qameta.allure.entity.LabelName.SUB_SUITE;
  import static io.qameta.allure.entity.LabelName.SUITE;
  import static io.qameta.allure.entity.Statistic.comparator;
  import static io.qameta.allure.entity.TestResult.comparingByTimeAsc;
  import static io.qameta.allure.tree.TreeUtils.calculateStatisticByLeafs;
  import static io.qameta.allure.tree.TreeUtils.groupByLabels;
  
  /**
   * Plugin that generates data for Suites tab.
   *
   * @since 2.0
   */
  public class SuitesPlugin extends CompositeAggregator2 {
  
      private static final String SUITES = "suites";
  
      /**
       * Name of the json file.
       */
      protected static final String JSON_FILE_NAME = "suites.json";
  
      /**
       * Name of the csv file.
       */
      protected static final String CSV_FILE_NAME = "suites.csv";
  
      public SuitesPlugin() {
          super(Arrays.asList(
                  new JsonAggregator(), new CsvExportAggregator(), new WidgetAggregator()
          ));
      }
  
      static /* default */ Tree<TestResult> getData(final List<LaunchResults> launchResults) {
  
          // @formatter:off
          final Tree<TestResult> xunit = new TestResultTree(
                  SUITES,
              testResult -> groupByLabels(testResult, PARENT_SUITE, SUITE, SUB_SUITE)
          );
          // @formatter:on
  
          launchResults.stream()
                  .map(LaunchResults::getResults)
                  .flatMap(Collection::stream)
                  .sorted(comparingByTimeAsc())
                  .forEach(xunit::add);
          return xunit;
      }
  
      /**
       * Generates tree data.
       */
      private static class JsonAggregator extends CommonJsonAggregator2 {
  
          JsonAggregator() {
              super(JSON_FILE_NAME);
          }
  
          @Override
          protected Tree<TestResult> getData(final List<LaunchResults> launches) {
              return SuitesPlugin.getData(launches);
          }
      }
  
      /**
       * Generates export data.
       */
      private static class CsvExportAggregator extends CommonCsvExportAggregator2<CsvExportSuite> {
  
          CsvExportAggregator() {
              super(CSV_FILE_NAME, CsvExportSuite.class);
          }
  
          @Override
          protected List<CsvExportSuite> getData(final List<LaunchResults> launchesResults) {
              return launchesResults.stream()
                      .flatMap(launch -> launch.getResults().stream())
                      .map(CsvExportSuite::new).collect(Collectors.toList());
          }
      }
  
      /**
       * Generates widget data.
       */
      private static class WidgetAggregator extends CommonJsonAggregator2 {
  
          WidgetAggregator() {
              super(Constants.WIDGETS_DIR, JSON_FILE_NAME);
          }
  
          @Override
          protected Object getData(final List<LaunchResults> launches) {
              final Tree<TestResult> data = SuitesPlugin.getData(launches);
              final List<TreeWidgetItem> items = data.getChildren().stream()
                      .filter(TestResultTreeGroup.class::isInstance)
                      .map(TestResultTreeGroup.class::cast)
                      .map(WidgetAggregator::toWidgetItem)
                      .sorted(Comparator.comparing(TreeWidgetItem::getStatistic, comparator()).reversed())
                      .limit(10)
                      .collect(Collectors.toList());
              return new TreeWidgetData().setItems(items).setTotal(data.getChildren().size());
          }
  
          private static TreeWidgetItem toWidgetItem(final TestResultTreeGroup group) {
              return new TreeWidgetItem()
                      .setUid(group.getUid())
                      .setName(group.getName())
                      .setStatistic(calculateStatisticByLeafs(group));
          }
      }
  }
  