<commit_before>angular.module('Software.controllers', ['Software.services']).
  controller('softwareController', function($scope, $q, BranchesApiService, softwareApiService) {

    BranchesApiService.getBranches().then(function(branches) {
      $scope.branches = branches;
    });

    $scope.showUpdatesOnly = true;
    $scope.showOnlyLatest = true;
    $scope.selectedBranch = undefined;
    $scope.installResult = '';
    $scope.latestVersions = [];

    $scope.loadInstalledSoftware = function() {
      $scope.loadingInstalled = softwareApiService.loadInstalledSoftware();
      $scope.loadingInstalled.then(function(items) {
        $scope.installedSoftware = items.data;
      });
    };

    $scope.loadVersions = function() {
      $scope.latestVersions = [];
      if ($scope.selectedBranch) {
        var getLatestSoftware = softwareApiService.getLatestVersion('openrov-*');
        var getCandidates = softwareApiService.getInstallCandidate('openrov-*');
        $scope.loadingPackages = $q.all([getLatestSoftware, getCandidates]);

        $scope.loadingPackages
          .then(
          function(results) {
            var versions = results[0].data;
            var candidates = results[1].data;

            if ($scope.showUpdatesOnly) {
              loadUpdatesOnlyPackages(candidates.result, versions);
            }
            else {
              loadAllPackages(versions);
            }
          })
      }
    };

    $scope.install = function(item) {
      softwareApiService.install(item.package, item.version, item.branch)
        .then(function(result) {
          $scope.installResult = JSON.stringify(result);
          $scope.loadInstalledSoftware();
          $scope.loadVersions();
        })
    };
    $scope.loadInstalledSoftware();

    function loadUpdatesOnlyPackages(candidates, versions) {
      candidates.forEach(function (candidate) {
        if (isPackageInstalled(candidate)
          && !isPackageVersionInstalled(candidate)) {
          var packagesToInstall =
            versions.filter(function (version) {
              return isSamePackage(candidate, version) && isSameVersion(candidate, version);
            });
          if (packagesToInstall && packagesToInstall.length == 1) {
            $scope.latestVersions.push(packagesToInstall[0]);
          }
        }
      });
    }

    function loadAllPackages(versions) {
      versions.forEach(function (version) {
        if (version.branch === $scope.selectedBranch) {
          if (!($scope.showOnlyLatest && newVersionsContainsPackage(version))) {
            if (!newVersionsContainsPackage(version) || !isPackageVersionInstalled(version)) {
              $scope.latestVersions.push(version);
            }
          }
        }
      })
    }

    function isSamePackage(installed, item) {
      return installed.package === item.package;
    }

    function isSameVersion(installed, item) {
      return installed.version === item.version;
    }
<commit_msg>Prevent dupplicates<commit_after>angular.module('Software.controllers', ['Software.services']).
  controller('softwareController', function($scope, $q, BranchesApiService, softwareApiService) {

    BranchesApiService.getBranches().then(function(branches) {
      $scope.branches = branches;
    });

    $scope.showUpdatesOnly = true;
    $scope.showOnlyLatest = true;
    $scope.selectedBranch = undefined;
    $scope.installResult = '';
    $scope.latestVersions = [];

    $scope.loadInstalledSoftware = function() {
      $scope.loadingInstalled = softwareApiService.loadInstalledSoftware();
      $scope.loadingInstalled.then(function(items) {
        $scope.installedSoftware = items.data;
      });
    };

    $scope.loadVersions = function() {
      $scope.latestVersions = [];
      if ($scope.selectedBranch) {
        var getLatestSoftware = softwareApiService.getLatestVersion('openrov-*');
        var getCandidates = softwareApiService.getInstallCandidate('openrov-*');
        $scope.loadingPackages = $q.all([getLatestSoftware, getCandidates]);

        $scope.loadingPackages
          .then(
          function(results) {
            var versions = results[0].data;
            var candidates = results[1].data;

            if ($scope.showUpdatesOnly) {
              loadUpdatesOnlyPackages(candidates.result, versions);
            }
            else {
              loadAllPackages(versions);
            }
          })
      }
    };

    $scope.install = function(item) {
      softwareApiService.install(item.package, item.version, item.branch)
        .then(function(result) {
          $scope.installResult = JSON.stringify(result);
          $scope.loadInstalledSoftware();
          $scope.loadVersions();
        })
    };
    $scope.loadInstalledSoftware();

    function loadUpdatesOnlyPackages(candidates, versions) {
      candidates.forEach(function (candidate) {
        if (isPackageInstalled(candidate)
          && !isPackageVersionInstalled(candidate)) {
          var packagesToInstall =
            versions.filter(function (version) {
              return isSamePackage(candidate, version) && isSameVersion(candidate, version);
            });
          if (packagesToInstall && packagesToInstall.length == 1) {
            if(!newVersionsContainsPackage(packagesToInstall[0])) {
              $scope.latestVersions.push(packagesToInstall[0]);
            }
          }
        }
      });
    }

    function loadAllPackages(versions) {
      versions.forEach(function (version) {
        if (version.branch === $scope.selectedBranch) {
          if (!($scope.showOnlyLatest && newVersionsContainsPackage(version))) {
            if (!newVersionsContainsPackage(version) || !isPackageVersionInstalled(version)) {
              $scope.latestVersions.push(version);
            }
          }
        }
      })
    }

    function isSamePackage(installed, item) {
      return installed.package === item.package;
    }

    function isSameVersion(installed, item) {
      return installed.version === item.version;
    }

{'fraction_non_alphanumeric': 0.12036423841059603, 'fraction_numerical': 0.001490066225165563, 'mean_word_length': 2.5724423418095803, 'pattern_counts': {'":': 0, '<': 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': '6015381', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1607, 'n_words': 228}