<commit_before>'use strict';

/*
 * Country view Controller : api call and data manipulation to serve four
 * visualisations (dualtimeline, brushing, partner histogram & linechart)
 */

angular.module('ricardo.controllers.country', [])

  .controller('country', ["$scope", "$location", "cfSource", "cfTarget",
    "cfSourceLine", "apiService", "lineChartService", "dataTableService", "utils",
    "countryService", "countryLines", "reportingEntities", "DEFAULT_REPORTING", "TABLE_HEADERS",
    function ($scope, $location, cfSource, cfTarget,
    cfSourceLine, apiService, lineChartService, dataTableService, utils, countryService,
    countryLines, reportingEntities, DEFAULT_REPORTING, TABLE_HEADERS) {
    /*
     * Message error if no data
     */
    $scope.ok = function () {
      $scope.missing = "0";
    };

    $scope.okPartner = function () {
      $scope.missingPartner = 0;
    };

    /*
     * Partners Histo filter
     */
    $scope.filtered = {};
    $scope.filters = [
      {type: {value: "all", writable: true },
       name: {value: "All",writable: true}},
      {type: {value: "city/part_of",writable: true},
       name: {value: "City",writable: true}},
      {type: {value: "colonial_area",writable: true},
       name: {value: "Colonial",writable: true}},
      {type: {value: "country",writable: true},
       name: {value: "Country",writable: true}},
      {type: {value: "geographical_area",writable: true},
       name: {value: "Geo",writable: true}},
      {type: {value: "group",writable: true},
       name: {value: "Group",writable: true}
    }];

    $scope.ordered = {
      type: {value: "tot",writable: true},
      name: {value: "Average share on Total",writable: true}
    };
    $scope.orders = [
      {type: {value: "tot",writable: true},
       name: {value: "Average share on Total",writable: true}},
      {type: {value: "imp",writable: true},
       name: {value: "Average share on Imports",writable: true}},
      {type: {value: "exp",writable: true},
       name: {value: "Average share on Exports",writable: true}
      }];

    $scope.grouped = {};
    $scope.groups = [
      {type: {value: 0,writable: true},
       name: {value: "None",writable: true}},
      {type: {value: 1,writable: true},
       name: {value: "Continent",writable: true}
    }];

    $scope.sorted = {};
    $scope.sorts = [
      {type: {value: "name",writable: true},
       name: {value: "Name",writable: true}},
      {type: {value: "average",writable: true},
       name: {value: "Average share",writable: true}
    }];

    $scope.linechartCurrency = {}
    $scope.linechartCurrencyChoices = [
      {type: {value: "sterling",writable: true},
       name: {value: "Sterling",writable: true}},
      {type: {value: "value",writable: true},
       name: {value: "Percent",writable: true}
    }];

    $scope.linechartFlow = {}
    $scope.linechartFlowChoices = [
      {type: {value: "total",writable: true},
       name: {value: "Total",writable: true}},
      {type: {value: "exp",writable: true},
       name: {value: "Exports",writable: true}},
      {type: {value: "imp",writable: true},
       name: {value: "Imports",writable: true}
    }];

    /*
     * All var declarations
     */
    var data
    $scope.reportingEntities = reportingEntities;
    $scope.messagePercent = 0;
    $scope.entities = {
      sourceEntity : {},
      sourceCountryEntity : {},
      sourceColonialEntity : {},
      sourceGeoEntity : {},
      sourceContinentEntity : {},
      sourceWorldEntity : {}
    }

    $scope.RICentities = {}
    $scope.RICentitiesDD = d3.values($scope.RICentities).sort(function(a,b){
        if(a.RICname < b.RICname) return -1;
        if(a.RICname > b.RICname) return 1;
        return 0;
    })

    /*
     * Arrays of entities for linechart
     */

    $scope.reporting = []
    $scope.reportingCountryEntities = [];
    $scope.reportingColonialEntities = [];
    $scope.reportingGeoEntities = [];
    $scope.reportingContinentEntities = [];
    $scope.reportingWorldEntities = [];

    /*
     * Linecharts default config
     */

    $scope.lineColors = ['#1f77b4','#aec7e8','#ff7f0e','#ffbb78','#2ca02c']
    $scope.linechartCurrency = {type: {value : "sterling", writable: true},
                                name: {value: "Sterling", writable: true}};
    $scope.linechartFlow = {type: {value: "total", writable: true},
                            name: {value: "Total", writable: true}};

    $scope.missingData = [];
    /*
     * Trigger to show or hide data table
     */
    $scope.viewTable = 0;

    $scope.yValue = "total";
    $scope.conversion = "sterling";
    $scope.filter = "all"
    $scope.order = "tot"
    $scope.currency = 0
    $scope.actualCurrency = "sterling pound"


    /*
     * First init with local storage
     */
     if (localStorage.sourceEntitySelected) {
      try {
        $scope.entities.sourceEntity.selected = JSON.parse(localStorage.getItem('sourceEntitySelected'));
        init($scope.entities.sourceEntity.selected.RICid, $scope.currency);
        }
      catch (e) {
        $scope.entities.sourceEntity.selected=$scope.reportingEntities.filter(function (e){
          return e.RICid===DEFAULT_REPORTING})[0]
        init(DEFAULT_REPORTING, $scope.currency);
      }
     }
     else {
      $scope.entities.sourceEntity.selected=$scope.reportingEntities.filter(function (e){
        return e.RICid===DEFAULT_REPORTING})[0]
      init(DEFAULT_REPORTING, $scope.currency);
     }

    /*
     * Calling the API to init country selection
     */
    function init(sourceID, currency) {
      apiService
        .getFlows({
          reporting_ids: sourceID
        })
        .then(function (data) {

          var dates = data.flows.map(function (d) { return d.year})

          $scope.selectedMinDate = d3.min(dates);
          $scope.selectedMaxDate = d3.max(dates);

          $scope.tableData = data.flows;

          if (cfSource.size() > 0) {
            cfSource.year().filterAll();
            cfSource.clear();
          }

          $scope.actualCurrency = data.flows[0].currency;
          $scope.RICentities = {};

          data.RICentities.partners.forEach(function(d){
            $scope.RICentities[""+d.RICid] = {
              RICname : d.RICname,
              type: d.type,
              RICid: d.RICid,
              continent: d.continent }
            })

          $scope.RICentitiesDD = d3.values($scope.RICentities).sort(function(a,b){
              if(a.RICname < b.RICname) return -1;
              if(a.RICname > b.RICname) return 1;
              return 0;
          })

          /*
           *  Init all entities by types filters for linechart viz
           */

          $scope.reportingCountryEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "country" || d.type === "group"})
          $scope.reportingColonialEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "colonial_area"})
          $scope.reportingGeoEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "geographical_area" && d.RICname.indexOf("World ") !== 0})
          $scope.reportingWorldEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "geographical_area" && d.RICname.indexOf("World ") === 0})

          /*
           * Special methods for continent
           */

          var continents = d3.nest()
            .key(function(d){return d.continent})
            .entries($scope.RICentitiesDD.filter(function(d){return d.continent}))
            .map(function(d){return d.key})

          $scope.reportingContinentEntities = []

          continents.forEach(function(d){
            var elm = {RICname : d, type: "continent", RICid: d }
            $scope.reportingContinentEntities.push(elm)
          })

          /*
           * Line chart world
           */

          $scope.reporting = []
          $scope.entities.sourceCountryEntity = {}
          $scope.entities.sourceColonialEntity = {}
          $scope.entities.sourceGeoEntity = {}
          $scope.entities.sourceContinentEntity = {}
          $scope.entities.sourceWorldEntity = {}

          $scope.rawMinDate = d3.min( data.flows, function(d) {return d.year; })
          $scope.rawMaxDate = d3.max( data.flows, function(d) {return d.year; })

          // $scope.selectedMinDate = parseInt(localStorage.getItem('selectedMinDate'));
          // $scope.selectedMaxDate = parseInt(localStorage.getItem('selectedMaxDate'));

          /*
           * Check if dates were in localstorage
           */
          var minDate = parseInt(localStorage.getItem('selectedMinDate'));
          var maxDate = parseInt(localStorage.getItem('selectedMaxDate'));
          $scope.selectedMinDate = minDate ?
            minDate : Math.max( $scope.selectedMinDate, $scope.rawMinDate );
          $scope.selectedMaxDate = maxDate ?
            maxDate : Math.min( $scope.selectedMaxDate, $scope.rawMaxDate );


          /*
           * Build data for timeline
           */
          data.flows.forEach(function(d){
            d.type = $scope.RICentities[""+d.partner_id].type
            d.continent = $scope.RICentities[d.partner_id+""].continent
          })
          cfSource.add(data.flows)

          // delete world flows, maybe api action ?
          cfSource.partner().filter(function(p){return !/^World/.test(p)});
          var flowsPerYear = cfSource.years().top(Infinity)
          // arrrrrg CFSource kill me ! we need to do a hard copy.
          flowsPerYear = JSON.parse(JSON.stringify(flowsPerYear))
          cfSource.partner().filterAll();

          var timelineData = [];

          flowsPerYear.sort(function (a, b){ return d3.ascending(a.key, b.key); })
          flowsPerYear.forEach(function (d){
              var td = $.extend(d.value, {year: (new Date(d.key)).getFullYear()});
              if (!td.exp)
                td.exp = null;
              if (!td.imp)
                td.imp = null;
              if (!td.tot)
                td.tot = null;
              timelineData.push(td);
           });

          $scope.timelineData=timelineData;

          /*
           * Display filters selection and init partner histogram
           */
          $scope.ordered = {type: {value :"tot",writable: true},
                            name: {value:"Average share on Total",writable: true}};
          $scope.grouped = {type: {value :0,writable: true},
                            name: {value:"None",writable: true}};
          $scope.filtered = {type: {value :"all",writable: true},
                            name: {value:"All",writable: true}};
          $scope.sorted = { type: {value :"average",writable: true},
                            name: {value:"Average share",writable: true}};

          initPartnerHisto($scope.tableData)

          /*
           * Save all object in localStorage
           */
          localStorage.removeItem('selectedMinDate');
          localStorage.removeItem('selectedMaxDate');
          localStorage.removeItem('sourceEntitySelected');
          localStorage.selectedMinDate = JSON.stringify($scope.selectedMinDate);
          localStorage.selectedMaxDate = JSON.stringify($scope.selectedMaxDate);
          localStorage.sourceEntitySelected = JSON.stringify($scope.entities.sourceEntity.selected);
      });
    }

    /*
     * Triggers entity selected and dates
     */
    $scope.$watchCollection('[selectedMinDate, selectedMaxDate]', function (newValue, oldValue) {
      if (newValue !== oldValue && newValue[0] != newValue[1]) {
        // update date selected
        $scope.selectedMinDate = newValue[0];
        $scope.selectedMaxDate = newValue[1];

        // update local storage
        localStorage.removeItem('selectedMinDate');
        localStorage.removeItem('selectedMaxDate');
        localStorage.selectedMinDate = newValue[0];
        localStorage.selectedMaxDate = newValue[1];

        updateDateRange();
        initLinechart($scope.reporting, $scope.linechartFlow.type.value,
          $scope.linechartCurrency.type.value);
        initPartnerHisto($scope.tableData);
      }
    })

    $scope.$watch("entities.sourceEntity.selected", function (newValue, oldValue){
      console.log("watch entity");
      if(newValue !== oldValue && newValue){<commit_msg>update country controllers<commit_after>'use strict';

/*
 * Country view Controller : api call and data manipulation to serve four
 * visualisations (dualtimeline, brushing, partner histogram & linechart)
 */

angular.module('ricardo.controllers.country', [])

  .controller('country', ["$scope", "$location", "cfSource", "cfTarget",
    "cfSourceLine", "apiService", "lineChartService", "dataTableService", "utils",
    "countryService", "countryLines", "reportingEntities", "DEFAULT_REPORTING", "TABLE_HEADERS",
    function ($scope, $location, cfSource, cfTarget,
    cfSourceLine, apiService, lineChartService, dataTableService, utils, countryService,
    countryLines, reportingEntities, DEFAULT_REPORTING, TABLE_HEADERS) {
    /*
     * Message error if no data
     */
    $scope.ok = function () {
      $scope.missing = "0";
    };

    $scope.okPartner = function () {
      $scope.missingPartner = 0;
    };

    /*
     * Partners Histo filter
     */
    $scope.filtered = {};
    $scope.filters = [
      {type: {value: "all", writable: true },
       name: {value: "All",writable: true}},
      {type: {value: "city/part_of",writable: true},
       name: {value: "City",writable: true}},
      {type: {value: "colonial_area",writable: true},
       name: {value: "Colonial",writable: true}},
      {type: {value: "country",writable: true},
       name: {value: "Country",writable: true}},
      {type: {value: "geographical_area",writable: true},
       name: {value: "Geo",writable: true}},
      {type: {value: "group",writable: true},
       name: {value: "Group",writable: true}
    }];

    $scope.ordered = {
      type: {value: "tot",writable: true},
      name: {value: "Average share on Total",writable: true}
    };
    $scope.orders = [
      {type: {value: "tot",writable: true},
       name: {value: "Average share on Total",writable: true}},
      {type: {value: "imp",writable: true},
       name: {value: "Average share on Imports",writable: true}},
      {type: {value: "exp",writable: true},
       name: {value: "Average share on Exports",writable: true}
      }];

    $scope.grouped = {};
    $scope.groups = [
      {type: {value: 0,writable: true},
       name: {value: "None",writable: true}},
      {type: {value: 1,writable: true},
       name: {value: "Continent",writable: true}
    }];

    $scope.sorted = {};
    $scope.sorts = [
      {type: {value: "name",writable: true},
       name: {value: "Name",writable: true}},
      {type: {value: "average",writable: true},
       name: {value: "Average share",writable: true}
    }];

    $scope.linechartCurrency = {}
    $scope.linechartCurrencyChoices = [
      {type: {value: "sterling",writable: true},
       name: {value: "Sterling",writable: true}},
      {type: {value: "value",writable: true},
       name: {value: "Percent",writable: true}
    }];

    $scope.linechartFlow = {}
    $scope.linechartFlowChoices = [
      {type: {value: "total",writable: true},
       name: {value: "Total",writable: true}},
      {type: {value: "exp",writable: true},
       name: {value: "Exports",writable: true}},
      {type: {value: "imp",writable: true},
       name: {value: "Imports",writable: true}
    }];

    /*
     * All var declarations
     */
    var data
    $scope.reportingEntities = reportingEntities;
    $scope.messagePercent = 0;
    $scope.entities = {
      sourceEntity : {},
      sourceCountryEntity : {},
      sourceColonialEntity : {},
      sourceGeoEntity : {},
      sourceContinentEntity : {},
      sourceWorldEntity : {}
    }

    $scope.RICentities = {}
    $scope.RICentitiesDD = d3.values($scope.RICentities).sort(function(a,b){
        if(a.RICname < b.RICname) return -1;
        if(a.RICname > b.RICname) return 1;
        return 0;
    })

    /*
     * Arrays of entities for linechart
     */

    $scope.reporting = []
    $scope.reportingCountryEntities = [];
    $scope.reportingColonialEntities = [];
    $scope.reportingGeoEntities = [];
    $scope.reportingContinentEntities = [];
    $scope.reportingWorldEntities = [];

    /*
     * Linecharts default config
     */

    $scope.lineColors = ['#1f77b4','#aec7e8','#ff7f0e','#ffbb78','#2ca02c']
    $scope.linechartCurrency = {type: {value : "sterling", writable: true},
                                name: {value: "Sterling", writable: true}};
    $scope.linechartFlow = {type: {value: "total", writable: true},
                            name: {value: "Total", writable: true}};

    $scope.missingData = [];
    /*
     * Trigger to show or hide data table
     */
    $scope.viewTable = 0;

    $scope.yValue = "total";
    $scope.conversion = "sterling";
    $scope.filter = "all"
    $scope.order = "tot"
    $scope.currency = 0
    $scope.actualCurrency = "sterling pound"


    /*
     * First init with local storage
     */
     if (localStorage.sourceEntitySelected) {
      try {
        $scope.entities.sourceEntity.selected = JSON.parse(localStorage.getItem('sourceEntitySelected'));
        init($scope.entities.sourceEntity.selected.RICid, $scope.currency);
        }
      catch (e) {
        $scope.entities.sourceEntity.selected=$scope.reportingEntities.filter(function (e){
          return e.RICid===DEFAULT_REPORTING})[0]
        init(DEFAULT_REPORTING, $scope.currency);
      }
     }
     else {
      $scope.entities.sourceEntity.selected=$scope.reportingEntities.filter(function (e){
        return e.RICid===DEFAULT_REPORTING})[0]
      init(DEFAULT_REPORTING, $scope.currency);
     }

    /*
     * Calling the API to init country selection
     */
    function init(sourceID, currency) {
      apiService
        .getFlows({
          reporting_ids: sourceID
        })
        .then(function (data) {

          var dates = data.flows.map(function (d) { return d.year})

          $scope.selectedMinDate = d3.min(dates);
          $scope.selectedMaxDate = d3.max(dates);

          $scope.tableData = data.flows;

          if (cfSource.size() > 0) {
            cfSource.year().filterAll();
            cfSource.clear();
          }

          $scope.actualCurrency = data.flows[0].currency;
          $scope.RICentities = {};

          data.RICentities.partners.forEach(function(d){
            $scope.RICentities[""+d.RICid] = {
              RICname : d.RICname,
              type: d.type,
              RICid: d.RICid,
              continent: d.continent }
            })

          $scope.RICentitiesDD = d3.values($scope.RICentities).sort(function(a,b){
              if(a.RICname < b.RICname) return -1;
              if(a.RICname > b.RICname) return 1;
              return 0;
          })

          /*
           *  Init all entities by types filters for linechart viz
           */

          $scope.reportingCountryEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "country" || d.type === "group"})
          $scope.reportingColonialEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "colonial_area"})
          $scope.reportingGeoEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "geographical_area" && d.RICname.indexOf("World ") !== 0})
          $scope.reportingWorldEntities = $scope.RICentitiesDD.filter(function(d){
            return d.type === "geographical_area" && d.RICname.indexOf("World ") === 0})

          /*
           * Special methods for continent
           */

          var continents = d3.nest()
            .key(function(d){return d.continent})
            .entries($scope.RICentitiesDD.filter(function(d){return d.continent}))
            .map(function(d){return d.key})

          $scope.reportingContinentEntities = []

          continents.forEach(function(d){
            var elm = {RICname : d, type: "continent", RICid: d }
            $scope.reportingContinentEntities.push(elm)
          })

          /*
           * Line chart world
           */

          $scope.reporting = []
          $scope.entities.sourceCountryEntity = {}
          $scope.entities.sourceColonialEntity = {}
          $scope.entities.sourceGeoEntity = {}
          $scope.entities.sourceContinentEntity = {}
          $scope.entities.sourceWorldEntity = {}

          $scope.rawMinDate = d3.min( data.flows, function(d) {return d.year; })
          $scope.rawMaxDate = d3.max( data.flows, function(d) {return d.year; })

          // $scope.selectedMinDate = parseInt(localStorage.getItem('selectedMinDate'));
          // $scope.selectedMaxDate = parseInt(localStorage.getItem('selectedMaxDate'));

          /*
           * Check if dates were in localstorage
           */
          var minDate = parseInt(localStorage.getItem('selectedMinDate'));
          var maxDate = parseInt(localStorage.getItem('selectedMaxDate'));
          $scope.selectedMinDate = minDate ?
            minDate : Math.max( $scope.selectedMinDate, $scope.rawMinDate );
          $scope.selectedMaxDate = maxDate ?
            maxDate : Math.min( $scope.selectedMaxDate, $scope.rawMaxDate );


          /*
           * Build data for timeline
           */
          data.flows.forEach(function(d){
            d.type = $scope.RICentities[""+d.partner_id].type
            d.continent = $scope.RICentities[d.partner_id+""].continent
          })
          cfSource.add(data.flows)

          // delete world flows, maybe api action ?
          cfSource.partner().filter(function(p){return !/^World/.test(p)});
          var flowsPerYear = cfSource.years().top(Infinity)
          // arrrrrg CFSource kill me ! we need to do a hard copy.
          flowsPerYear = JSON.parse(JSON.stringify(flowsPerYear))
          cfSource.partner().filterAll();

          var timelineData = [];

          flowsPerYear.sort(function (a, b){ return d3.ascending(a.key, b.key); })
          flowsPerYear.forEach(function (d){
              var td = $.extend(d.value, {year: (new Date(d.key)).getFullYear()});
              if (!td.exp)
                td.exp = null;
              if (!td.imp)
                td.imp = null;
              if (!td.tot)
                td.tot = null;
              timelineData.push(td);
           });

          $scope.timelineData=timelineData;

          /*
           * Display filters selection and init partner histogram
           */
          $scope.ordered = {type: {value :"tot",writable: true},
                            name: {value:"Average share on Total",writable: true}};
          $scope.grouped = {type: {value :0,writable: true},
                            name: {value:"None",writable: true}};
          $scope.filtered = {type: {value :"all",writable: true},
                            name: {value:"All",writable: true}};
          $scope.sorted = { type: {value :"average",writable: true},
                            name: {value:"Average share",writable: true}};

          initPartnerHisto($scope.tableData)

          /*
           * Save all object in localStorage
           */
          localStorage.removeItem('selectedMinDate');
          localStorage.removeItem('selectedMaxDate');
          localStorage.removeItem('sourceEntitySelected');
          localStorage.selectedMinDate = JSON.stringify($scope.selectedMinDate);
          localStorage.selectedMaxDate = JSON.stringify($scope.selectedMaxDate);
          localStorage.sourceEntitySelected = JSON.stringify($scope.entities.sourceEntity.selected);
      });
    }


    /*
     * Triggers entity selected and dates
     */
    $scope.$watchCollection('[selectedMinDate, selectedMaxDate]', function (newValue, oldValue) {
      if (newValue !== oldValue && newValue[0] != newValue[1]) {
        // update date selected
        $scope.selectedMinDate = newValue[0];
        $scope.selectedMaxDate = newValue[1];

        // update local storage
        localStorage.removeItem('selectedMinDate');
        localStorage.removeItem('selectedMaxDate');
        localStorage.selectedMinDate = newValue[0];
        localStorage.selectedMaxDate = newValue[1];

        updateDateRange();
        initLinechart($scope.reporting, $scope.linechartFlow.type.value,
          $scope.linechartCurrency.type.value);
        initPartnerHisto($scope.tableData);
      }
    })

    $scope.$watch("entities.sourceEntity.selected", function (newValue, oldValue){
      console.log("watch entity");
      if(newValue !== oldValue && newValue){
{'fraction_non_alphanumeric': 0.14553990610328638, 'fraction_numerical': 0.003804435810263882, 'mean_word_length': 2.3544664675536247, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4998412', 'n_tokens_mistral': 7473, 'n_tokens_neox': 7370, 'n_words': 1391}