bool CAnomalyJobConfig::CAnalysisConfig::CDetectorConfig::determineFunction()

in lib/api/CAnomalyJobConfig.cc [909:1147]


bool CAnomalyJobConfig::CAnalysisConfig::CDetectorConfig::determineFunction(bool haveSummaryCountField) {

    bool isPopulation{m_OverFieldName.empty() == false};
    bool hasByField{m_ByFieldName.empty() == false};

    // Some functions must take a field, some mustn't and for the rest it's
    // optional.  Validate this based on the contents of these flags after
    // determining the function.  Similarly for by fields.
    bool fieldRequired{false};
    bool fieldInvalid{false};
    bool byFieldRequired{false};
    bool byFieldInvalid{false};

    if (m_FunctionName.empty()) {
        LOG_ERROR(<< "No function specified");
        return false;
    }

    if (m_FunctionName == FUNCTION_COUNT || m_FunctionName == FUNCTION_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationCount
                                  : model::function_t::E_IndividualRareCount;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_DISTINCT_COUNT ||
               m_FunctionName == FUNCTION_DISTINCT_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationDistinctCount
                                  : model::function_t::E_IndividualDistinctCount;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_DISTINCT_COUNT ||
               m_FunctionName == FUNCTION_LOW_DISTINCT_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationLowDistinctCount
                                  : model::function_t::E_IndividualLowDistinctCount;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_DISTINCT_COUNT ||
               m_FunctionName == FUNCTION_HIGH_DISTINCT_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationHighDistinctCount
                                  : model::function_t::E_IndividualHighDistinctCount;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_NON_ZERO_COUNT ||
               m_FunctionName == FUNCTION_NON_ZERO_COUNT_ABBREV) {
        m_Function = model::function_t::E_IndividualNonZeroCount;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_RARE_NON_ZERO_COUNT ||
               m_FunctionName == FUNCTION_RARE_NON_ZERO_COUNT_ABBREV) {
        m_Function = model::function_t::E_IndividualRareNonZeroCount;
        fieldInvalid = true;
        byFieldRequired = true;
    } else if (m_FunctionName == FUNCTION_RARE) {
        m_Function = isPopulation ? model::function_t::E_PopulationRare
                                  : model::function_t::E_IndividualRare;
        fieldInvalid = true;
        byFieldRequired = true;
    } else if (m_FunctionName == FUNCTION_RARE_COUNT) {
        m_Function = model::function_t::E_PopulationRareCount;
        fieldInvalid = true;
        byFieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_COUNT || m_FunctionName == FUNCTION_LOW_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationLowCounts
                                  : model::function_t::E_IndividualLowCounts;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_HIGH_COUNT ||
               m_FunctionName == FUNCTION_HIGH_COUNT_ABBREV) {
        m_Function = isPopulation ? model::function_t::E_PopulationHighCounts
                                  : model::function_t::E_IndividualHighCounts;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_LOW_NON_ZERO_COUNT ||
               m_FunctionName == FUNCTION_LOW_NON_ZERO_COUNT_ABBREV) {
        m_Function = model::function_t::E_IndividualLowNonZeroCount;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_HIGH_NON_ZERO_COUNT ||
               m_FunctionName == FUNCTION_HIGH_NON_ZERO_COUNT_ABBREV) {
        m_Function = model::function_t::E_IndividualHighNonZeroCount;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_FREQ_RARE || m_FunctionName == FUNCTION_FREQ_RARE_ABBREV) {
        m_Function = model::function_t::E_PopulationFreqRare;
        fieldInvalid = true;
        byFieldRequired = true;
    } else if (m_FunctionName == FUNCTION_FREQ_RARE_COUNT ||
               m_FunctionName == FUNCTION_FREQ_RARE_COUNT_ABBREV) {
        m_Function = model::function_t::E_PopulationFreqRareCount;
        fieldInvalid = true;
        byFieldRequired = true;
    } else if (m_FunctionName == FUNCTION_INFO_CONTENT) {
        m_Function = isPopulation ? model::function_t::E_PopulationInfoContent
                                  : model::function_t::E_IndividualInfoContent;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_INFO_CONTENT) {
        m_Function = isPopulation ? model::function_t::E_PopulationLowInfoContent
                                  : model::function_t::E_IndividualLowInfoContent;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_INFO_CONTENT) {
        m_Function = isPopulation ? model::function_t::E_PopulationHighInfoContent
                                  : model::function_t::E_IndividualHighInfoContent;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_METRIC) {
        if (haveSummaryCountField) {
            LOG_ERROR(<< "Function " << m_FunctionName
                      << "() cannot be used with a summary count field");
            return false;
        }

        m_Function = isPopulation ? model::function_t::E_PopulationMetric
                                  : model::function_t::E_IndividualMetric;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_AVERAGE || m_FunctionName == FUNCTION_MEAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricMean
                                  : model::function_t::E_IndividualMetricMean;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_AVERAGE || m_FunctionName == FUNCTION_LOW_MEAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricLowMean
                                  : model::function_t::E_IndividualMetricLowMean;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_AVERAGE || m_FunctionName == FUNCTION_HIGH_MEAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricHighMean
                                  : model::function_t::E_IndividualMetricHighMean;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MEDIAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricMedian
                                  : model::function_t::E_IndividualMetricMedian;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_MEDIAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricLowMedian
                                  : model::function_t::E_IndividualMetricLowMedian;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_MEDIAN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricHighMedian
                                  : model::function_t::E_IndividualMetricHighMedian;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MIN) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricMin
                                  : model::function_t::E_IndividualMetricMin;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MAX) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricMax
                                  : model::function_t::E_IndividualMetricMax;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_VARIANCE) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricVariance
                                  : model::function_t::E_IndividualMetricVariance;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_VARIANCE) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricLowVariance
                                  : model::function_t::E_IndividualMetricLowVariance;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_VARIANCE) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricHighVariance
                                  : model::function_t::E_IndividualMetricHighVariance;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_SUM) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricSum
                                  : model::function_t::E_IndividualMetricSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_SUM) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricLowSum
                                  : model::function_t::E_IndividualMetricLowSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_SUM) {
        m_Function = isPopulation ? model::function_t::E_PopulationMetricHighSum
                                  : model::function_t::E_IndividualMetricHighSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_NON_NULL_SUM ||
               m_FunctionName == FUNCTION_NON_NULL_SUM_ABBREV) {
        m_Function = model::function_t::E_IndividualMetricNonNullSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_LOW_NON_NULL_SUM ||
               m_FunctionName == FUNCTION_LOW_NON_NULL_SUM_ABBREV) {
        m_Function = model::function_t::E_IndividualMetricLowNonNullSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_HIGH_NON_NULL_SUM ||
               m_FunctionName == FUNCTION_HIGH_NON_NULL_SUM_ABBREV) {
        m_Function = model::function_t::E_IndividualMetricHighNonNullSum;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_TIME_OF_DAY) {
        m_Function = isPopulation ? model::function_t::E_PopulationTimeOfDay
                                  : model::function_t::E_IndividualTimeOfDay;
        fieldRequired = false;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_TIME_OF_WEEK) {
        m_Function = isPopulation ? model::function_t::E_PopulationTimeOfWeek
                                  : model::function_t::E_IndividualTimeOfWeek;
        fieldRequired = false;
        fieldInvalid = true;
    } else if (m_FunctionName == FUNCTION_LAT_LONG) {
        m_Function = isPopulation ? model::function_t::E_PopulationLatLong
                                  : model::function_t::E_IndividualLatLong;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MAX_VELOCITY) {
        m_Function = isPopulation ? model::function_t::E_PopulationMaxVelocity
                                  : model::function_t::E_IndividualMaxVelocity;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MIN_VELOCITY) {
        m_Function = isPopulation ? model::function_t::E_PopulationMinVelocity
                                  : model::function_t::E_IndividualMinVelocity;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_MEAN_VELOCITY) {
        m_Function = isPopulation ? model::function_t::E_PopulationMeanVelocity
                                  : model::function_t::E_IndividualMeanVelocity;
        fieldRequired = true;
    } else if (m_FunctionName == FUNCTION_SUM_VELOCITY) {
        m_Function = isPopulation ? model::function_t::E_PopulationSumVelocity
                                  : model::function_t::E_IndividualSumVelocity;
        fieldRequired = true;
    } else {
        LOG_ERROR(<< "Invalid function " << m_FunctionName << " specified");
        return false;
    }

    // Validate
    if (model::function_t::isPopulation(m_Function) && isPopulation == false) {
        LOG_ERROR(<< "Function " << m_FunctionName << " requires an 'over' field");
        return false;
    }

    if (isPopulation && model::function_t::isPopulation(m_Function) == false) {
        LOG_ERROR(<< "Function " << m_FunctionName << " cannot be used with an 'over' field");
        return false;
    }

    if (byFieldRequired && hasByField == false) {
        LOG_ERROR(<< "Function " << m_FunctionName << " requires a 'by' field");
        return false;
    }

    if (byFieldInvalid && hasByField) {
        LOG_ERROR(<< "Function " << m_FunctionName << " cannot be used with a 'by' field");
        return false;
    }

    if (fieldRequired && m_FieldName.empty()) {
        LOG_ERROR(<< "Function " << m_FunctionName << " requires a field");
        return false;
    }

    if (fieldInvalid && m_FieldName.empty() == false) {
        LOG_ERROR(<< "Function " << m_FunctionName << " does not work on a field");
        return false;
    }

    return true;
}