<commit_before>angular.module('InescApp').controller('AppController', ['$scope', 'openspending', ($scope, openspending) ->
    $scope.entities = []
    $scope.year = 2012

    openspending.query().then (entities) ->
      $scope.entities = entities
      parseUrl()
    openspending.getTotals().then (totals) -> $scope.totals = totals

    $scope.$watch 'entity.id + year', ->
      [entity, year] = [$scope.entity, $scope.year]
      if entity? and entity.id? and entity.type? and year?
        openspending.get(entity, year).then (entity) -> $scope.entity = entity
        updateRoute()

    updateRoute = ->
      newUrl = generateUrl($scope.entity, $scope.year)
      if window.location.pathname != newUrl
        if window.history
          window.history.pushState({}, '', newUrl)
        else
          window.location.pathname = newUrl

    parseUrl = ->
      urlParts = window.location.pathname.substr(1).split('/')
      id = if urlParts == 3
             slugToId(urlParts[1]) # É uma unidade orçamentária
           else
             slugToId(urlParts[0]) # É um órgão

      $scope.entity = entity for entity in $scope.entities when parseInt(entity.id) is id
      $scope.year = parseInt(urlParts[urlParts.length-1])

    generateUrl = (entity, year) -><commit_msg>Corrige bug na barra de busca na home<commit_after>angular.module('InescApp').controller('AppController', ['$scope', 'openspending', ($scope, openspending) ->
    $scope.entities = []
    $scope.year = 2012

    openspending.query().then (entities) ->
      $scope.entities = entities
      parseUrl()
    openspending.getTotals().then (totals) -> $scope.totals = totals

    $scope.$watch 'entity.id + year', ->
      [entity, year] = [$scope.entity, $scope.year]
      if entity? and entity.id? and entity.type? and year?
        openspending.get(entity, year).then (entity) -> $scope.entity = entity
        updateRoute()

    updateRoute = ->
      newUrl = generateUrl($scope.entity, $scope.year)
      if window.location.pathname != newUrl
        if window.history && !inRootPage()
          window.history.pushState({}, '', newUrl)
        else
          window.location.pathname = newUrl

    parseUrl = ->
      return if inRootPage()
      path = window.location.pathname.substr(1)
      parts = path.split('/')
      id = if parts == 3
             slugToId(parts[1]) # É uma unidade orçamentária
           else
             slugToId(parts[0]) # É um órgão

      $scope.entity = entity for entity in $scope.entities when parseInt(entity.id) is id
      $scope.year = parseInt(parts[parts.length-1])

    inRootPage = () ->
      window.location.pathname == '/'

    generateUrl = (entity, year) ->
{'fraction_non_alphanumeric': 0.1412639405204461, 'fraction_numerical': 0.006691449814126394, 'mean_word_length': 2.6169354838709675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '3110917', 'n_tokens_mistral': 826, 'n_tokens_neox': 810, 'n_words': 183}