<commit_before>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

define([
    'underscore',
    'backbone',
    'knockout',
    'contrail-model',
    'query-or-model',
    'query-and-model',
    'core-basedir/reports/qe/ui/js/common/qe.utils',
    'contrail-list-model',
], function (_, Backbone, Knockout, ContrailModel, QueryOrModel, QueryAndModel, qeUtils, ContrailListModel) {
    var QueryFormModel = ContrailModel.extend({
        defaultSelectFields: [],
        disableSelectFields: [],
        disableSubstringInSelectFields: ['CLASS('],
        disableWhereFields: [],

        constructor: function (modelData, queryReqConfig) {
            var self = this, modelRemoteDataConfig,
                defaultQueryReqConfig = {chunk: 1, autoSort: true, chunkSize: cowc.QE_RESULT_CHUNK_SIZE_10K, async: true};

            var defaultSelectFields = this.defaultSelectFields,
                disableFieldArray = [].concat(defaultSelectFields).concat(this.disableSelectFields),
                disableSubstringArray = this.disableSubstringInSelectFields;

            if (contrail.checkIfExist(modelData.table_name)) {
                modelRemoteDataConfig = getTableSchemaConfig(self, modelData.table_name, disableFieldArray, disableSubstringArray, this.disableWhereFields);
            }

            if(contrail.checkIfExist(queryReqConfig)) {
                defaultQueryReqConfig = $.extend(true, defaultQueryReqConfig, queryReqConfig);
            }

            this.defaultQueryReqConfig = defaultQueryReqConfig;

            ContrailModel.prototype.constructor.call(this, modelData, modelRemoteDataConfig);

            this.model().on("change:table_name", this.onChangeTable, this);
            this.model().on('change:select change:table_name change:time_range change:where change:filter change:time_granularity change:time_granularity_unit', function () {
                // TODO ContrailListModel should have reload function instead of whole model recreation just to get new data
                self.refresh()
            })

            //TODO - Needs to be tested for Flow Pages
            this.model().on("change:time_range change:from_time change:to_time change:table_type", this.onChangeTime, this);

            return this;
        },

        onChangeTime: function() {
            var self = this,
                table_type = self.model().get('table_type')
            if (table_type === cowc.QE_STAT_TABLE_TYPE
                || table_type === cowc.QE_OBJECT_TABLE_TYPE
                || table_type === cowc.QE_FLOW_TABLE_TYPE) {
                var setTableValuesCallbackFn = function (self, resultArr){
                    var currentSelectedTable = self.model().attributes.table_name;
                    if (currentSelectedTable != null)
                    {
                        // If time_range is changed then Fetch active tables and check if selected table
                        // is present in the response; if not then reset, else don't reset
                        if (_.indexOf(resultArr, currentSelectedTable) == -1) {
                            // reset everything except time range
                            self.reset(self, null, false, true);
                        }
                    }
                }
                this.setTableValues(setTableValuesCallbackFn, table_type)
            }
            this.setTableFieldValues()
        },

        setTableValues: function(setTableValuesCallbackFn, tabletype) {
            var self = this,
                contrailViewModel = this.model(),
                timeRange = contrailViewModel.attributes.time_range;

            function fetchTableValues(fromTimeUTC, toTimeUTC) {
                var data = {
                    fromTimeUTC: fromTimeUTC,
                    toTimeUTC  : toTimeUTC,
                    table_name : 'StatTable.FieldNames.fields',
                    select     : ['name', 'fields.value'],
                    where      : [[{"name": "name", "value": tabletype, "op": 7}]]
                };
                self.table_name_data_object({
                    status: cowc.DATA_REQUEST_STATE_FETCHING,
                    data: []
                });
                $.ajax({
                    url: '/api/qe/table/column/values',<commit_msg>Where Clause Values Not Coming for LOG Table Type<commit_after>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

define([
    'underscore',
    'backbone',
    'knockout',
    'contrail-model',
    'query-or-model',
    'query-and-model',
    'core-basedir/reports/qe/ui/js/common/qe.utils',
    'contrail-list-model',
], function (_, Backbone, Knockout, ContrailModel, QueryOrModel, QueryAndModel, qeUtils, ContrailListModel) {
    var QueryFormModel = ContrailModel.extend({
        defaultSelectFields: [],
        disableSelectFields: [],
        disableSubstringInSelectFields: ['CLASS('],
        disableWhereFields: [],

        constructor: function (modelData, queryReqConfig) {
            var self = this, modelRemoteDataConfig,
                defaultQueryReqConfig = {chunk: 1, autoSort: true, chunkSize: cowc.QE_RESULT_CHUNK_SIZE_10K, async: true};

            var defaultSelectFields = this.defaultSelectFields,
                disableFieldArray = [].concat(defaultSelectFields).concat(this.disableSelectFields),
                disableSubstringArray = this.disableSubstringInSelectFields;

            if (contrail.checkIfExist(modelData.table_name)) {
                modelRemoteDataConfig = getTableSchemaConfig(self, modelData.table_name, disableFieldArray, disableSubstringArray, this.disableWhereFields);
            }

            if(contrail.checkIfExist(queryReqConfig)) {
                defaultQueryReqConfig = $.extend(true, defaultQueryReqConfig, queryReqConfig);
            }

            this.defaultQueryReqConfig = defaultQueryReqConfig;

            ContrailModel.prototype.constructor.call(this, modelData, modelRemoteDataConfig);

            this.model().on("change:table_name", this.onChangeTable, this);
            this.model().on('change:select change:table_name change:time_range change:where change:filter change:time_granularity change:time_granularity_unit', function () {
                // TODO ContrailListModel should have reload function instead of whole model recreation just to get new data
                self.refresh()
            })

            //TODO - Needs to be tested for Flow Pages
            this.model().on("change:time_range change:from_time change:to_time change:table_type", this.onChangeTime, this);

            return this;
        },

        onChangeTime: function() {
            var self = this,
                table_type = self.model().get('table_type')

            if (table_type === cowc.QE_STAT_TABLE_TYPE
                || table_type === cowc.QE_OBJECT_TABLE_TYPE
                || table_type === cowc.QE_FLOW_TABLE_TYPE) {
                var setTableValuesCallbackFn = function (self, resultArr){
                    var currentSelectedTable = self.model().attributes.table_name;
                    if (currentSelectedTable != null)
                    {
                        // If time_range is changed then Fetch active tables and check if selected table
                        // is present in the response; if not then reset, else don't reset
                        if (_.indexOf(resultArr, currentSelectedTable) == -1) {
                            // reset everything except time range
                            self.reset(self, null, false, true);
                        }
                    }
                }
                this.setTableValues(setTableValuesCallbackFn, table_type)
            }
            // use the timer trick to overcome a event firing sequence issue.
            setTimeout(function() {
                this.setTableFieldValues();
            }.bind(this), 0);
        },

        setTableValues: function(setTableValuesCallbackFn, tabletype) {
            var self = this,
                contrailViewModel = this.model(),
                timeRange = contrailViewModel.attributes.time_range;

            function fetchTableValues(fromTimeUTC, toTimeUTC) {
                var data = {
                    fromTimeUTC: fromTimeUTC,
                    toTimeUTC  : toTimeUTC,
                    table_name : 'StatTable.FieldNames.fields',
                    select     : ['name', 'fields.value'],
                    where      : [[{"name": "name", "value": tabletype, "op": 7}]]
                };
                self.table_name_data_object({
                    status: cowc.DATA_REQUEST_STATE_FETCHING,
                    data: []
                });
                $.ajax({
                    url: '/api/qe/table/column/values',
{'fraction_non_alphanumeric': 0.09461608359836438, 'fraction_numerical': 0.002158109950022717, 'mean_word_length': 1.9507372654155497, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '3813679', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2174, 'n_words': 471}