<commit_before>angular.module('argus.services.dashboard', [])
.service('DashboardService', ['$filter', '$compile', '$resource', 'CONFIG', 'VIEWELEMENT', 'Metrics', '$sce', '$http', 'Annotations', 'growl',
    function ($filter, $compile, $resource, CONFIG, VIEWELEMENT, Metrics, $sce, $http,Annotations,growl) {

        this.getDashboardById = function(dashboardId){
            return $http.get(CONFIG.wsUrl + 'dashboards/' + dashboardId);
        };

        this.populateView = function(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope){

            if(metricList && metricList.length>0 && divId) {

                if (metricList && metricList.length > 0) {
                    if(elementType === VIEWELEMENT.chart){
                        populateChart(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope);
                    }else{
                        var metricExpressionList = getMetricExpressionList(metricList);
                        $http({
                            method: 'GET',
                            url: CONFIG.wsUrl + 'metrics',
                            params: {'expression': metricExpressionList}
                        }).success(function(data, status, headers, config){
                            if(data && data.length>0) {
                                $('#' + divId).show();
                                if(elementType === VIEWELEMENT.heatmap)
                                    updateHeatmap({}, data, divId, optionList, attributes);
                                else if(elementType === VIEWELEMENT.table)
                                    updateTable(data, scope, divId, optionList);
                            } else {
                                updateChart({}, data, divId, annotationExpressionList, optionList, attributes);
                                growl.info('No data found for the metric expressions: ' + JSON.stringify(metricExpressionList));

                            }
                        }).error(function(data, status, headers, config) {
                            growl.error(data.message);
                            $('#' + divId).hide();
                        });
                    }
                } else {
                    growl.error('Valid metric expressions are required to display the chart/table.');
                    $('#' + divId).hide();
                }
            }
        };

        // TODO: refactor this duplicate code also in: viewMetrics.js $scope function
        // 'populateSeries' below makes same API call, refactor both to separate factories
        this.getMetricData = function(metricExpression) {
            if (!metricExpression) return;

            var metricData =
                $http({
                    method: 'GET',
                    url: CONFIG.wsUrl + 'metrics',
                    params: {'expression': metricExpression}
                }).
                success(function(data, status, headers, config) {
                    if ( data && data.length > 0 ) {
                        return data[0];
                    } else{
                        growl.info('No data found for the metric expression: ' + JSON.stringify(metricExpression));
                        return;
                    }
                }).
                error(function(data, status, headers, config) {
                    growl.error(data.message);
                    return;
                });

            return metricData;
        };

        this.augmentExpressionWithControlsData = function(event, expression, controls) {
            var result = expression;

            for (var controlIndex in controls) {
                var controlName = '\\$' + controls[controlIndex].name + '\\$';
                var controlValue = controls[controlIndex].value;
                var controlType = controls[controlIndex].type;
                if ( controlType === "agDate" ) {
                    controlValue = isNaN(Date.parse(controlValue)) ? controlValue : Date.parse(controlValue);
                }
                controlValue = controlValue == undefined ? "" : controlValue;
                result = result.replace(new RegExp(controlName, "g"), controlValue);
            }

            result = result.replace(/(\r\n|\n|\r|\s+)/gm, "");
            return result;
        };

        this.updateIndicatorStatus = updateIndicatorStatus;

        this.buildViewElement = buildViewElement;

        function populateChart(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope){

            var objMetricCount = {};
            objMetricCount.totalCount = metricList.length;

            $('#' + divId).empty();
            $('#' + divId).show();

            // 'smallChart' currently viewed in the 'Services Dashboard'
            var smallChart = attributes.smallchart ? true : false;
            var chartType = attributes.type ? attributes.type : 'LINE';
            var highChartOptions = getOptionsByChartType(CONFIG, chartType, smallChart);

            setCustomOptions(highChartOptions, optionList);

            $('#' + divId).highcharts('StockChart', highChartOptions);

            var chart = $('#' + divId).highcharts('StockChart');

            // show loading spinner & hide 'no data message' during api request
            chart.showLoading();
            chart.hideNoData();

            // define series first; then build list for each metric expression
            var series = [];

            for (var i = 0; i < metricList.length; i++) {
                var metricExpression = metricList[i].expression;
                var metricOptions = metricList[i].metricSpecificOptions;

                // make api call to get data for each metric item
                populateSeries(metricList[i], highChartOptions, series, divId, attributes, annotationExpressionList, objMetricCount);
            }
            //populateAnnotations(annotationExpressionList, chart);
        };

        function updateIndicatorStatus(attributes, lastStatusVal) {
            if (lastStatusVal < attributes.lo) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('red');
            } else if (lastStatusVal > attributes.lo && lastStatusVal < attributes.hi) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('orange');
            } else if (lastStatusVal > attributes.hi) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('green');
            }
        };

        function buildViewElement(scope, element, attributes, dashboardCtrl, elementType, index, DashboardService, growl) {
            var elementId = 'element_' + elementType + index;
            var smallChartCss = ( attributes.smallchart ) ? 'class="smallChart"' : '';
            element.prepend('<div id=' + elementId + ' ' + smallChartCss +'></div>');

            scope.$on(dashboardCtrl.getSubmitBtnEventName(), function(event, controls){
                console.log(dashboardCtrl.getSubmitBtnEventName() + ' event received.');
                populateView(event, controls);
            });

            function populateView(event, controls) {
                var updatedMetricList = [];
                var updatedAnnotationList = [];
                var updatedOptionList = [];

                // TODO: move these 3 items to 'utils' folder
                for (var key in scope.metrics) {
                    if (scope.metrics.hasOwnProperty(key)) {

                        // get metricExpression, and name & color attributes from scope
                        var metrics = scope.metrics[key];
                        var metricExpression = metrics.expression;
                        var metricSpecificOptions = metrics.metricSpecificOptions;
                        var processedExpression = DashboardService.augmentExpressionWithControlsData(event, metricExpression, controls);

                        if (processedExpression.length > 0 /* && (/\$/.test(processedExpression)==false) */) {
                            var processedMetric = {};
                            processedMetric['expression'] = processedExpression;
                            processedMetric['name'] = metrics.name;
                            processedMetric['color'] = metrics.color;
                            processedMetric['metricSpecificOptions'] = getMetricSpecificOptionsInArray(metricSpecificOptions);

                            // update metric list with new processed metric object
                            updatedMetricList.push(processedMetric);
                        }
                    }
                }

                for (var key in scope.annotations) {
                    if (scope.annotations.hasOwnProperty(key)) {
                        var processedExpression = augmentExpressionWithControlsData(event, scope.annotations[key],controls);
                        if (processedExpression.length > 0 /* && (/\$/.test(processedExpression)==false) */) {
                            updatedAnnotationList.push(processedExpression);
                        }
                    }
                }

                for (var key in scope.options) {
                    if (scope.options.hasOwnProperty(key)) {
                        updatedOptionList.push({name: key, value: scope.options[key]});
                    }
                }

                if (updatedMetricList.length > 0) {
                    DashboardService.populateView(updatedMetricList, updatedAnnotationList, updatedOptionList, elementId, attributes, elementType, scope);
                } else {
                    growl.error('The valid metric expression(s) is required to display the chart.', {referenceId: 'growl-error'});
                    $('#' + elementId).hide();<commit_msg>Fix for ag-flags<commit_after>angular.module('argus.services.dashboard', [])
.service('DashboardService', ['$filter', '$compile', '$resource', 'CONFIG', 'VIEWELEMENT', 'Metrics', '$sce', '$http', 'Annotations', 'growl',
    function ($filter, $compile, $resource, CONFIG, VIEWELEMENT, Metrics, $sce, $http,Annotations,growl) {

        this.getDashboardById = function(dashboardId){
            return $http.get(CONFIG.wsUrl + 'dashboards/' + dashboardId);
        };

        this.populateView = function(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope){

            if(metricList && metricList.length>0 && divId) {

                if (metricList && metricList.length > 0) {
                    if(elementType === VIEWELEMENT.chart){
                        populateChart(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope);
                    }else{
                        var metricExpressionList = getMetricExpressionList(metricList);
                        $http({
                            method: 'GET',
                            url: CONFIG.wsUrl + 'metrics',
                            params: {'expression': metricExpressionList}
                        }).success(function(data, status, headers, config){
                            if(data && data.length>0) {
                                $('#' + divId).show();
                                if(elementType === VIEWELEMENT.heatmap)
                                    updateHeatmap({}, data, divId, optionList, attributes);
                                else if(elementType === VIEWELEMENT.table)
                                    updateTable(data, scope, divId, optionList);
                            } else {
                                updateChart({}, data, divId, annotationExpressionList, optionList, attributes);
                                growl.info('No data found for the metric expressions: ' + JSON.stringify(metricExpressionList));

                            }
                        }).error(function(data, status, headers, config) {
                            growl.error(data.message);
                            $('#' + divId).hide();
                        });
                    }
                } else {
                    growl.error('Valid metric expressions are required to display the chart/table.');
                    $('#' + divId).hide();
                }
            }
        };

        // TODO: refactor this duplicate code also in: viewMetrics.js $scope function
        // 'populateSeries' below makes same API call, refactor both to separate factories
        this.getMetricData = function(metricExpression) {
            if (!metricExpression) return;

            var metricData =
                $http({
                    method: 'GET',
                    url: CONFIG.wsUrl + 'metrics',
                    params: {'expression': metricExpression}
                }).
                success(function(data, status, headers, config) {
                    if ( data && data.length > 0 ) {
                        return data[0];
                    } else{
                        growl.info('No data found for the metric expression: ' + JSON.stringify(metricExpression));
                        return;
                    }
                }).
                error(function(data, status, headers, config) {
                    growl.error(data.message);
                    return;
                });

            return metricData;
        };

        this.augmentExpressionWithControlsData = function(event, expression, controls) {
            var result = expression;

            for (var controlIndex in controls) {
                var controlName = '\\$' + controls[controlIndex].name + '\\$';
                var controlValue = controls[controlIndex].value;
                var controlType = controls[controlIndex].type;
                if ( controlType === "agDate" ) {
                    controlValue = isNaN(Date.parse(controlValue)) ? controlValue : Date.parse(controlValue);
                }
                controlValue = controlValue == undefined ? "" : controlValue;
                result = result.replace(new RegExp(controlName, "g"), controlValue);
            }

            result = result.replace(/(\r\n|\n|\r|\s+)/gm, "");
            return result;
        };

        this.updateIndicatorStatus = updateIndicatorStatus;

        this.buildViewElement = buildViewElement;

        function populateChart(metricList, annotationExpressionList, optionList, divId, attributes, elementType, scope){

            var objMetricCount = {};
            objMetricCount.totalCount = metricList.length;

            $('#' + divId).empty();
            $('#' + divId).show();

            // 'smallChart' currently viewed in the 'Services Dashboard'
            var smallChart = attributes.smallchart ? true : false;
            var chartType = attributes.type ? attributes.type : 'LINE';
            var highChartOptions = getOptionsByChartType(CONFIG, chartType, smallChart);

            setCustomOptions(highChartOptions, optionList);

            $('#' + divId).highcharts('StockChart', highChartOptions);

            var chart = $('#' + divId).highcharts('StockChart');

            // show loading spinner & hide 'no data message' during api request
            chart.showLoading();
            chart.hideNoData();

            // define series first; then build list for each metric expression
            var series = [];

            for (var i = 0; i < metricList.length; i++) {
                var metricExpression = metricList[i].expression;
                var metricOptions = metricList[i].metricSpecificOptions;

                // make api call to get data for each metric item
                populateSeries(metricList[i], highChartOptions, series, divId, attributes, annotationExpressionList, objMetricCount);
            }
            //populateAnnotations(annotationExpressionList, chart);
        };

        function updateIndicatorStatus(attributes, lastStatusVal) {
            if (lastStatusVal < attributes.lo) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('red');
            } else if (lastStatusVal > attributes.lo && lastStatusVal < attributes.hi) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('orange');
            } else if (lastStatusVal > attributes.hi) {
                $('#' + attributes.name + '-status').removeClass('red orange green').addClass('green');
            }
        };

        function buildViewElement(scope, element, attributes, dashboardCtrl, elementType, index, DashboardService, growl) {
            var elementId = 'element_' + elementType + index;
            var smallChartCss = ( attributes.smallchart ) ? 'class="smallChart"' : '';
            element.prepend('<div id=' + elementId + ' ' + smallChartCss +'></div>');

            scope.$on(dashboardCtrl.getSubmitBtnEventName(), function(event, controls){
                console.log(dashboardCtrl.getSubmitBtnEventName() + ' event received.');
                populateView(event, controls);
            });

            function populateView(event, controls) {
                var updatedMetricList = [];
                var updatedAnnotationList = [];
                var updatedOptionList = [];

                // TODO: move these 3 items to 'utils' folder
                for (var key in scope.metrics) {
                    if (scope.metrics.hasOwnProperty(key)) {

                        // get metricExpression, and name & color attributes from scope
                        var metrics = scope.metrics[key];
                        var metricExpression = metrics.expression;
                        var metricSpecificOptions = metrics.metricSpecificOptions;
                        var processedExpression = DashboardService.augmentExpressionWithControlsData(event, metricExpression, controls);

                        if (processedExpression.length > 0 /* && (/\$/.test(processedExpression)==false) */) {
                            var processedMetric = {};
                            processedMetric['expression'] = processedExpression;
                            processedMetric['name'] = metrics.name;
                            processedMetric['color'] = metrics.color;
                            processedMetric['metricSpecificOptions'] = getMetricSpecificOptionsInArray(metricSpecificOptions);

                            // update metric list with new processed metric object
                            updatedMetricList.push(processedMetric);
                        }
                    }
                }

                for (var key in scope.annotations) {
                    if (scope.annotations.hasOwnProperty(key)) {
                        var processedExpression = DashboardService.augmentExpressionWithControlsData(event, scope.annotations[key],controls);
                        if (processedExpression.length > 0 /* && (/\$/.test(processedExpression)==false) */) {
                            updatedAnnotationList.push(processedExpression);
                        }
                    }
                }

                for (var key in scope.options) {
                    if (scope.options.hasOwnProperty(key)) {
                        updatedOptionList.push({name: key, value: scope.options[key]});
                    }
                }

                if (updatedMetricList.length > 0) {
                    DashboardService.populateView(updatedMetricList, updatedAnnotationList, updatedOptionList, elementId, attributes, elementType, scope);
                } else {
                    growl.error('The valid metric expression(s) is required to display the chart.', {referenceId: 'growl-error'});
                    $('#' + elementId).hide();
{'fraction_non_alphanumeric': 0.10745547841835194, 'fraction_numerical': 0.0010061374383740819, 'mean_word_length': 1.6530094755104765, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730495', 'n_tokens_mistral': 4620, 'n_tokens_neox': 4367, 'n_words': 1011}