<commit_before>// [WriteFile Name=StatisticalQueryGroupSort, Category=Analysis]
// [Legal]
// Copyright 2017 Esri.

// 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.
// [Legal]

#include "StatisticalQueryGroupSort.h"
#include "ServiceFeatureTable.h"
#include "StatisticDefinition.h"
#include "StatisticRecord.h"
#include "StatisticRecordIterator.h"
#include "StatisticsQueryParameters.h"
#include "StatisticsQueryResult.h"
#include "Field.h"

#include "StatisticResultListModel.h"

#include <QStringList>
#include <QVariantList>
#include <QList>
#include <QScopedPointer>

using namespace Esri::ArcGISRuntime;

StatisticalQueryGroupSort::StatisticalQueryGroupSort(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_resultsModel(new StatisticResultListModel(this))
{
}

void StatisticalQueryGroupSort::init()
{
  qmlRegisterType<StatisticalQueryGroupSort>("Esri.Samples", 1, 0, "StatisticalQueryGroupSortSample");
}

void StatisticalQueryGroupSort::componentComplete()
{
  QQuickItem::componentComplete();

  // Create the Service Feature Table
  m_featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3"), this);
  connectSignals();
  m_featureTable->load();

  // Setup default values for the Options page
  addStatisticDefinition("POP2007", "Sum");
  addStatisticDefinition("POP2007", "Average");
  addStatisticDefinition("AGE_5_17", "Minimum");
  addOrderBy("SUB_REGION", "Ascending");
  m_statisticTypes << "Average" << "Count" << "Maximum" << "Minimum"
                   << "Standard Deviation" << "Sum" << "Variance";
  emit statisticTypesChanged();
}

void StatisticalQueryGroupSort::connectSignals()
{
  connect(m_featureTable, &ServiceFeatureTable::doneLoading, this, [this](Error error)
  {
    if (!error.isEmpty())
      return;

    for (Field field : m_featureTable->fields())
    {
      m_fields << field.name();
    }
    emit fieldsChanged();
  });

  connect(m_featureTable, &ServiceFeatureTable::errorOccurred, this, [this](Error error)
  {
    if (error.isEmpty())
      return;

    m_resultsModel->clear();
    addResultToModel("", QString("Error. %1").arg(error.message()));
  });

  connect(m_featureTable, &ServiceFeatureTable::queryStatisticsCompleted, this, [this](QUuid, StatisticsQueryResult* rawResult)
  {
    // Delete rawResult when we leave local scope.
    QScopedPointer<StatisticsQueryResult> result(rawResult);

    if (!result)    
      return;    

    // clear previous results
    m_resultsModel->clear();

    // iterate the results and add to a model
    StatisticRecordIterator iter = result->iterator();
    while (iter.hasNext())
    {
      // get the statistic record
      StatisticRecord* record = iter.next(result.data());

      // get the group string
      QStringList sectionStrings;
      const QVariantMap& groupsMap = record->group();
      for (const auto& it : groupsMap.toStdMap() )
      {
          sectionStrings << QString("\"%1\":\"%2\"").arg(it.first, it.second.toString());
      }
      const QString sectionString = sectionStrings.join(',');

      // obtain the statistics
      const QVariantMap& statsMap = record->statistics();
      for (const auto& it : statsMap.toStdMap())
      {
        QString statString = QString("%1: %2").arg(it.first, it.second.toString());
        addResultToModel(sectionString, statString);
      }
    }
  });
}

void StatisticalQueryGroupSort::queryStatistics()
{
  // create the parameter object
  StatisticsQueryParameters params;

  // add the statistic definitions
  QList<StatisticDefinition> statisticDefinitionList;
  for (QVariant def : m_statisticDefinitions)
  {
    QVariantMap definitionMap = def.toMap();
    StatisticDefinition statisticDefinition;
    statisticDefinition.setOnFieldName(definitionMap["field"].toString());
    statisticDefinition.setStatisticType(statisticStringToEnum(definitionMap["statistic"].toString()));
    statisticDefinitionList.append(statisticDefinition);
  }
  params.setStatisticDefinitions(statisticDefinitionList);

  // set the grouping fields
  params.setGroupByFieldNames(groupingFields());

  // add the order by objects
  QList<OrderBy> orderByList;
  for (QVariant ob : m_orderBys)
  {<commit_msg>Reverting back to using an iterator type for QVariantMap. Added const to QString object.<commit_after>// [WriteFile Name=StatisticalQueryGroupSort, Category=Analysis]
// [Legal]
// Copyright 2017 Esri.

// 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.
// [Legal]

#include "StatisticalQueryGroupSort.h"
#include "ServiceFeatureTable.h"
#include "StatisticDefinition.h"
#include "StatisticRecord.h"
#include "StatisticRecordIterator.h"
#include "StatisticsQueryParameters.h"
#include "StatisticsQueryResult.h"
#include "Field.h"

#include "StatisticResultListModel.h"

#include <QStringList>
#include <QVariantList>
#include <QList>
#include <QScopedPointer>

using namespace Esri::ArcGISRuntime;

StatisticalQueryGroupSort::StatisticalQueryGroupSort(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_resultsModel(new StatisticResultListModel(this))
{
}

void StatisticalQueryGroupSort::init()
{
  qmlRegisterType<StatisticalQueryGroupSort>("Esri.Samples", 1, 0, "StatisticalQueryGroupSortSample");
}

void StatisticalQueryGroupSort::componentComplete()
{
  QQuickItem::componentComplete();

  // Create the Service Feature Table
  m_featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3"), this);
  connectSignals();
  m_featureTable->load();

  // Setup default values for the Options page
  addStatisticDefinition("POP2007", "Sum");
  addStatisticDefinition("POP2007", "Average");
  addStatisticDefinition("AGE_5_17", "Minimum");
  addOrderBy("SUB_REGION", "Ascending");
  m_statisticTypes << "Average" << "Count" << "Maximum" << "Minimum"
                   << "Standard Deviation" << "Sum" << "Variance";
  emit statisticTypesChanged();
}

void StatisticalQueryGroupSort::connectSignals()
{
  connect(m_featureTable, &ServiceFeatureTable::doneLoading, this, [this](Error error)
  {
    if (!error.isEmpty())
      return;

    for (Field field : m_featureTable->fields())
    {
      m_fields << field.name();
    }
    emit fieldsChanged();
  });

  connect(m_featureTable, &ServiceFeatureTable::errorOccurred, this, [this](Error error)
  {
    if (error.isEmpty())
      return;

    m_resultsModel->clear();
    addResultToModel("", QString("Error. %1").arg(error.message()));
  });

  connect(m_featureTable, &ServiceFeatureTable::queryStatisticsCompleted, this, [this](QUuid, StatisticsQueryResult* rawResult)
  {
    // Delete rawResult when we leave local scope.
    QScopedPointer<StatisticsQueryResult> result(rawResult);

    if (!result)    
      return;    

    // clear previous results
    m_resultsModel->clear();

    // iterate the results and add to a model
    StatisticRecordIterator iter = result->iterator();
    while (iter.hasNext())
    {
      // get the statistic record
      StatisticRecord* record = iter.next(result.data());

      // get the group string
      QStringList sectionStrings;
      const QVariantMap& groupsMap = record->group();
      for (auto it = groupsMap.cbegin(); it != groupsMap.cend(); ++it)
      {
          sectionStrings << QString("\"%1\":\"%2\"").arg(it.key(), it.value().toString());
      }
      const QString sectionString = sectionStrings.join(',');

      // obtain the statistics
      const QVariantMap& statsMap = record->statistics();
      for (auto it = statsMap.cbegin(); it != statsMap.cend(); ++it)
      {
        const QString statString = QString("%1: %2").arg(it.key(), it.value().toString());
        addResultToModel(sectionString, statString);
      }
    }
  });
}

void StatisticalQueryGroupSort::queryStatistics()
{
  // create the parameter object
  StatisticsQueryParameters params;

  // add the statistic definitions
  QList<StatisticDefinition> statisticDefinitionList;
  for (QVariant def : m_statisticDefinitions)
  {
    QVariantMap definitionMap = def.toMap();
    StatisticDefinition statisticDefinition;
    statisticDefinition.setOnFieldName(definitionMap["field"].toString());
    statisticDefinition.setStatisticType(statisticStringToEnum(definitionMap["statistic"].toString()));
    statisticDefinitionList.append(statisticDefinition);
  }
  params.setStatisticDefinitions(statisticDefinitionList);

  // set the grouping fields
  params.setGroupByFieldNames(groupingFields());

  // add the order by objects
  QList<OrderBy> orderByList;
  for (QVariant ob : m_orderBys)
  {
{'fraction_non_alphanumeric': 0.12334618189394729, 'fraction_numerical': 0.0058339410355245334, 'mean_word_length': 4.393258426966292, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4560416', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2677, 'n_words': 666}