<commit_before>'use strict';

var angular = require('angular');

var fs = require('fs');

var template = fs.readFileSync(__dirname + '/incidents-tab.html', 'utf8');

var Directive = [
  '$http', '$q', '$modal', 'search', 'Uri', 'Views', '$translate', 'localConf',
  function($http, $q, $modal, search, Uri, Views, $translate, localConf) {

    var Link = function linkFunction(scope) {

      var availableColumns = [
        { class: 'state',      request: 'incidentState',     sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_STATE')},
        { class: 'create-time',request: 'createTime',        sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CREATE_TIME')},
        { class: 'end-time',   request: 'endTime',           sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_END_TIME')},
        { class: 'timestamp',  request: 'incidentTimestamp', sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_TIMESTAMP')},
        { class: 'message',    request: '',                  sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_MESSAGE')},
        { class: 'activity',   request: 'activityId',        sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_ACTIVITY')},
        { class: 'cause instance-id uuid',      request: 'causeIncidentProcessInstanceId',     sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CAUSE_INSTANCE_ID')},
        { class: 'cause-root instance-id uuid', request: 'rootCauseIncidentProcessInstanceId', sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CAUSE_ROOT_INSTANCE_ID')},
        { class: 'type',       request: 'incidentType',      sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_TYPE')},
        { class: 'action',     request: '',                  sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_ACTION')}
      ];

      scope.onSortChange = updateView;
      setHeader();

      function setHeader() {
        var defaultSort = {sortBy: 'incidentType', sortOrder: 'asc'};

        var commonClasses = [
          'activity',
          'cause instance-id uuid',
          'cause-root instance-id uuid',
          'type',
          'action'
        ];

        if (scope.incidentsContext === 'history') {
          var histColClasses = [
            'state',
            'message',
            'create-time',
            'end-time'
          ];
          scope.headColumns = setColumns(availableColumns,histColClasses.concat(commonClasses));
          scope.localConfKey = 'sortHistInci';

        } else {
          var runtimeColClasses = [
            'message',
            'timestamp'
          ];
          scope.headColumns = setColumns(availableColumns, runtimeColClasses.concat(commonClasses));
          scope.localConfKey = 'sortInci';
        }

        scope.sortObj = loadLocal(defaultSort);
      }

      function setColumns(availableColObjs, selectedColClasses) {
        var Cols = [];

        selectedColClasses.forEach(function(selectedColumnClass) {
          var classObj = availableColObjs.filter(function(columnObj) {
            return columnObj.class === selectedColumnClass;
          })[0];
          Cols.push(classObj);
        });
        return Cols;
      }

      var incidentData = scope.processData.newChild(scope);

      var pages = scope.pages = angular.copy({size: 50, total: 0, current: 1});

      var baseRuntimeUrl = 'plugin://base/:engine/incident/';
      var baseHistoricUrl = 'plugin://history/:engine/incident/';

      scope.incidents = null;

      scope.onPaginationChange = function onPaginationChange(pages) {
        scope.pages.current = pages.current;
        updateView();
      };

      scope.getIncidentState = function(incident) {
        var result = 'open';

        if (incident.resolved) {
          result = 'resolved';
        } else if (incident.deleted) {
          result = 'deleted';
        }

        return result;
      };

      incidentData.observe(
        ['filter', 'bpmnElements', 'activityIdToInstancesMap'],
        function(newFilter, bpmnElements, activityIdToInstancesMap) {
          pages.current = newFilter.page || 1;
          scope.filter = newFilter;
          scope.bpmnElements = bpmnElements;
          scope.activityIdToInstancesMap = activityIdToInstancesMap;
          updateView();
        });

      function updateView(query, pages, sortObj) {
        scope.sortObj = sortObj || scope.sortObj;

        if (!scope.filter || !scope.bpmnElements || !scope.activityIdToInstancesMap) {
          // return empty promise
          return $q.when(null);
        }

        if(pages) {
          scope.pages = pages;
        }

        var newFilter = scope.filter;
        var bpmnElements = scope.bpmnElements;
        var activityIdToInstancesMap = scope.activityIdToInstancesMap;

        var filter = angular.copy(newFilter);

        delete filter.page;
        delete filter.activityInstanceIds;
        delete filter.scrollToBpmnElement;

        var page =  scope.pages.current,
            count =  scope.pages.size,
            firstResult = (page - 1) * count,
            queryParams = query || {};

        var defaultParams;

        if (scope.processDefinition) {
          defaultParams = {processDefinitionIdIn: [scope.processDefinition.id]};
        } else {
          defaultParams = {processInstanceIdIn: [scope.processInstance.id]};
        }

        // Add default sorting param
        if (sortObj) {
          defaultParams.sortBy    = scope.sortObj.sortBy;
          defaultParams.sortOrder = scope.sortObj.sortOrder;
          saveLocal(sortObj);
        }

        var pagingParams = {
          firstResult: firstResult,
          maxResults: count
        };

        var params = angular.extend({}, filter, defaultParams,queryParams);

        params.activityIdIn = params.activityIds;
        delete params.activityIds;

        var baseUrl = scope.incidentsContext === 'history' ? baseHistoricUrl : baseRuntimeUrl;

        // get the 'count' of incidents
        $http.post(Uri.appUri(baseUrl + 'count'), params).success(function(data) {
          scope.pages.total = data.count;
        });

        // get the incidents
        scope.loadingState = 'LOADING';
        return $http.post(Uri.appUri(baseUrl), params, {params: pagingParams})
          .then(function(res) {
            var data = res.data;
            angular.forEach(data, function(incident) {
              var activityId = incident.activityId;
              var bpmnElement = bpmnElements[activityId];
              incident.activityName = (bpmnElement && (bpmnElement.name || bpmnElement.id)) || activityId;
              incident.linkable = bpmnElements[activityId] && activityIdToInstancesMap[activityId] && activityIdToInstancesMap[activityId].length > 0;
            });

            scope.incidents = data;
            scope.loadingState = data.length ? 'LOADED' : 'EMPTY';
          });
      }

      scope.getIncidentType = function(incident) {
        if (incident.incidentType === 'failedJob') {<commit_msg>chore(cockpit): tiny changes concerning code style<commit_after>'use strict';

var angular = require('angular');

var fs = require('fs');

var template = fs.readFileSync(__dirname + '/incidents-tab.html', 'utf8');

var Directive = [
  '$http', '$q', '$modal', 'search', 'Uri', 'Views', '$translate', 'localConf',
  function($http, $q, $modal, search, Uri, Views, $translate, localConf) {

    var Link = function linkFunction(scope) {

      var availableColumns = [
        { class: 'state',      request: 'incidentState',     sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_STATE')},
        { class: 'create-time',request: 'createTime',        sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CREATE_TIME')},
        { class: 'end-time',   request: 'endTime',           sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_END_TIME')},
        { class: 'timestamp',  request: 'incidentTimestamp', sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_TIMESTAMP')},
        { class: 'message',    request: '',                  sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_MESSAGE')},
        { class: 'activity',   request: 'activityId',        sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_ACTIVITY')},
        { class: 'cause instance-id uuid',      request: 'causeIncidentProcessInstanceId',     sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CAUSE_INSTANCE_ID')},
        { class: 'cause-root instance-id uuid', request: 'rootCauseIncidentProcessInstanceId', sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_CAUSE_ROOT_INSTANCE_ID')},
        { class: 'type',       request: 'incidentType',      sortable: true, content: $translate.instant('PLUGIN_INCIDENTS_TAB_TYPE')},
        { class: 'action',     request: '',                  sortable: false, content: $translate.instant('PLUGIN_INCIDENTS_TAB_ACTION')}
      ];

      scope.onSortChange = updateView;
      setHeader();

      function setHeader() {
        var commonClasses = [
          'activity',
          'cause instance-id uuid',
          'cause-root instance-id uuid',
          'type',
          'action'
        ];

        if (scope.incidentsContext === 'history') {
          var histColClasses = [
            'state',
            'message',
            'create-time',
            'end-time'
          ];
          scope.headColumns = setColumns(availableColumns, histColClasses.concat(commonClasses));
          scope.localConfKey = 'sortHistInci';

        } else {
          var runtimeColClasses = [
            'message',
            'timestamp'
          ];
          scope.headColumns = setColumns(availableColumns, runtimeColClasses.concat(commonClasses));
          scope.localConfKey = 'sortInci';
        }

        var defaultSort = {sortBy: 'incidentType', sortOrder: 'asc'};
        scope.sortObj = loadLocal(defaultSort);
      }

      function setColumns(availableColumns, selectedColClasses) {
        var columns = [];

        selectedColClasses.forEach(function(selectedColumnClass) {
          var matchedColumn = availableColumns.filter(function(column) {
            return column.class === selectedColumnClass;
          })[0];
          columns.push(matchedColumn);
        });
        return columns;
      }

      var incidentData = scope.processData.newChild(scope);

      var pages = scope.pages = angular.copy({size: 50, total: 0, current: 1});

      var baseRuntimeUrl = 'plugin://base/:engine/incident/';
      var baseHistoricUrl = 'plugin://history/:engine/incident/';

      scope.incidents = null;

      scope.onPaginationChange = function onPaginationChange(pages) {
        scope.pages.current = pages.current;
        updateView();
      };

      scope.getIncidentState = function(incident) {
        var result = 'open';

        if (incident.resolved) {
          result = 'resolved';
        } else if (incident.deleted) {
          result = 'deleted';
        }

        return result;
      };

      incidentData.observe(
        ['filter', 'bpmnElements', 'activityIdToInstancesMap'],
        function(newFilter, bpmnElements, activityIdToInstancesMap) {
          pages.current = newFilter.page || 1;
          scope.filter = newFilter;
          scope.bpmnElements = bpmnElements;
          scope.activityIdToInstancesMap = activityIdToInstancesMap;
          updateView();
        });

      function updateView(query, pages, sortObj) {
        scope.sortObj = sortObj || scope.sortObj;

        if (!scope.filter || !scope.bpmnElements || !scope.activityIdToInstancesMap) {
          // return empty promise
          return $q.when(null);
        }

        if (pages) {
          scope.pages = pages;
        }

        var newFilter = scope.filter;
        var bpmnElements = scope.bpmnElements;
        var activityIdToInstancesMap = scope.activityIdToInstancesMap;

        var filter = angular.copy(newFilter);

        delete filter.page;
        delete filter.activityInstanceIds;
        delete filter.scrollToBpmnElement;

        var page =  scope.pages.current,
            count =  scope.pages.size,
            firstResult = (page - 1) * count,
            queryParams = query || {};

        var defaultParams;

        if (scope.processDefinition) {
          defaultParams = {processDefinitionIdIn: [scope.processDefinition.id]};
        } else {
          defaultParams = {processInstanceIdIn: [scope.processInstance.id]};
        }

        // Add default sorting param
        if (sortObj) {
          defaultParams.sortBy = scope.sortObj.sortBy;
          defaultParams.sortOrder = scope.sortObj.sortOrder;
          saveLocal(sortObj);
        }

        var pagingParams = {
          firstResult: firstResult,
          maxResults: count
        };

        var params = angular.extend({}, filter, defaultParams, queryParams);

        params.activityIdIn = params.activityIds;
        delete params.activityIds;

        var baseUrl = scope.incidentsContext === 'history' ? baseHistoricUrl : baseRuntimeUrl;

        // get the 'count' of incidents
        $http.post(Uri.appUri(baseUrl + 'count'), params).success(function(data) {
          scope.pages.total = data.count;
        });

        // get the incidents
        scope.loadingState = 'LOADING';
        return $http.post(Uri.appUri(baseUrl), params, {params: pagingParams})
          .then(function(res) {
            var data = res.data;
            angular.forEach(data, function(incident) {
              var activityId = incident.activityId;
              var bpmnElement = bpmnElements[activityId];
              incident.activityName = (bpmnElement && (bpmnElement.name || bpmnElement.id)) || activityId;
              incident.linkable = bpmnElements[activityId] && activityIdToInstancesMap[activityId] && activityIdToInstancesMap[activityId].length > 0;
            });

            scope.incidents = data;
            scope.loadingState = data.length ? 'LOADED' : 'EMPTY';
          });
      }

      scope.getIncidentType = function(incident) {
        if (incident.incidentType === 'failedJob') {
{'fraction_non_alphanumeric': 0.12017918387345139, 'fraction_numerical': 0.0012598866102050816, 'mean_word_length': 2.467119631157486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4439843', 'n_tokens_mistral': 4110, 'n_tokens_neox': 3966, 'n_words': 776}