<commit_before>require 'utils/expandable-record-array'
require 'models/model'

Model = Travis.Model
ExpandableRecordArray = Travis.ExpandableRecordArray
EnvVar = Travis.EnvVar
Build  = Travis.Build
SshKey = Travis.SshKey
Event = Travis.Event
durationFrom = Travis.Helpers.durationFrom
Ajax = Travis.ajax

Repo = Model.extend
  slug:                DS.attr()
  description:         DS.attr()
  private:             DS.attr('boolean')
  lastBuildNumber:     DS.attr('number')
  lastBuildState:      DS.attr()
  lastBuildStartedAt:  DS.attr()
  lastBuildFinishedAt: DS.attr()
  githubLanguage:      DS.attr()
  _lastBuildDuration:  DS.attr('number')
  lastBuildLanguage:   DS.attr()
  active:              DS.attr()
  lastBuildId:         DS.attr('number')
  lastBuildHash: (->
    {
      id: @get('lastBuildId')
      number: @get('lastBuildNumber')
      repo: this
    }
  ).property('lastBuildId', 'lastBuildNumber')

  lastBuild: (->
    id = @get('lastBuildId')
    @store.find('build', id)
    @store.recordForId('build', id)
  ).property('lastBuildId')

  withLastBuild: ->
    @filter( (repo) -> repo.get('lastBuildId') )

  sshKey: (->
    @store.find('ssh_key', @get('id'))
    @store.recordForId('ssh_key', @get('id'))
  )

  envVars: (->
    id = @get('id')
    @store.filter('env_var', { repository_id: id }, (v) ->
      v.get('repo.id') == id
    )
  ).property()

  builds: (->
    id = @get('id')
    builds = @store.find('build', event_type: 'push', repository_id: id)

    # TODO: move to controller
    array  = ExpandableRecordArray.create
      type: 'build'
      content: Ember.A([])

    array.load(builds)

    id = @get('id')
    array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && !build.get('isPullRequest') )

    array
  ).property()

  pullRequests: (->
    id = @get('id')
    builds = @store.find('build', event_type: 'pull_request', repository_id: id)

    # TODO: move to controller
    array  = ExpandableRecordArray.create
      type: 'build'
      content: Ember.A([])

    array.load(builds)

    id = @get('id')
    array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && build.get('isPullRequest') )

    array
  ).property()

  branches: (->
    builds = @store.find 'build', repository_id: @get('id'), branches: true

    builds.then ->
      builds.set 'isLoaded', true

    builds
  ).property()

  owner: (->
    (@get('slug') || '').split('/')[0]
  ).property('slug')

  name: (->
    (@get('slug') || '').split('/')[1]
  ).property('slug')

  lastBuildDuration: (->
    duration = @get('_lastBuildDuration')
    duration = durationFrom(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
    duration
  ).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt')

  sortOrder: (->
    # cuz sortAscending seems buggy when set to false
    if lastBuildFinishedAt = @get('lastBuildFinishedAt')
      - new Date(lastBuildFinishedAt).getTime()
    else
      - new Date('9999').getTime() - parseInt(@get('lastBuildId'))
  ).property('lastBuildFinishedAt', 'lastBuildId')

  stats: (->
    if @get('slug')
      @get('_stats') || $.get("https://api.github.com/repos/#{@get('slug')}", (data) =>
        @set('_stats', data)<commit_msg>Use durationFrom as durationFromHelper<commit_after>require 'utils/expandable-record-array'
require 'models/model'

Model = Travis.Model
ExpandableRecordArray = Travis.ExpandableRecordArray
EnvVar = Travis.EnvVar
Build  = Travis.Build
SshKey = Travis.SshKey
Event = Travis.Event
durationFromHelper = Travis.Helpers.durationFrom
Ajax = Travis.ajax

Repo = Model.extend
  slug:                DS.attr()
  description:         DS.attr()
  private:             DS.attr('boolean')
  lastBuildNumber:     DS.attr('number')
  lastBuildState:      DS.attr()
  lastBuildStartedAt:  DS.attr()
  lastBuildFinishedAt: DS.attr()
  githubLanguage:      DS.attr()
  _lastBuildDuration:  DS.attr('number')
  lastBuildLanguage:   DS.attr()
  active:              DS.attr()
  lastBuildId:         DS.attr('number')
  lastBuildHash: (->
    {
      id: @get('lastBuildId')
      number: @get('lastBuildNumber')
      repo: this
    }
  ).property('lastBuildId', 'lastBuildNumber')

  lastBuild: (->
    id = @get('lastBuildId')
    @store.find('build', id)
    @store.recordForId('build', id)
  ).property('lastBuildId')

  withLastBuild: ->
    @filter( (repo) -> repo.get('lastBuildId') )

  sshKey: (->
    @store.find('ssh_key', @get('id'))
    @store.recordForId('ssh_key', @get('id'))
  )

  envVars: (->
    id = @get('id')
    @store.filter('env_var', { repository_id: id }, (v) ->
      v.get('repo.id') == id
    )
  ).property()

  builds: (->
    id = @get('id')
    builds = @store.find('build', event_type: 'push', repository_id: id)

    # TODO: move to controller
    array  = ExpandableRecordArray.create
      type: 'build'
      content: Ember.A([])

    array.load(builds)

    id = @get('id')
    array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && !build.get('isPullRequest') )

    array
  ).property()

  pullRequests: (->
    id = @get('id')
    builds = @store.find('build', event_type: 'pull_request', repository_id: id)

    # TODO: move to controller
    array  = ExpandableRecordArray.create
      type: 'build'
      content: Ember.A([])

    array.load(builds)

    id = @get('id')
    array.observe(@store.all('build'), (build) -> build.get('isLoaded') && build.get('repo.id') == id && build.get('isPullRequest') )

    array
  ).property()

  branches: (->
    builds = @store.find 'build', repository_id: @get('id'), branches: true

    builds.then ->
      builds.set 'isLoaded', true

    builds
  ).property()

  owner: (->
    (@get('slug') || '').split('/')[0]
  ).property('slug')

  name: (->
    (@get('slug') || '').split('/')[1]
  ).property('slug')

  lastBuildDuration: (->
    duration = @get('_lastBuildDuration')
    duration = durationFromHelper(@get('lastBuildStartedAt'), @get('lastBuildFinishedAt')) unless duration
    duration
  ).property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt')

  sortOrder: (->
    # cuz sortAscending seems buggy when set to false
    if lastBuildFinishedAt = @get('lastBuildFinishedAt')
      - new Date(lastBuildFinishedAt).getTime()
    else
      - new Date('9999').getTime() - parseInt(@get('lastBuildId'))
  ).property('lastBuildFinishedAt', 'lastBuildId')

  stats: (->
    if @get('slug')
      @get('_stats') || $.get("https://api.github.com/repos/#{@get('slug')}", (data) =>
        @set('_stats', data)
{'fraction_non_alphanumeric': 0.18266486405287666, 'fraction_numerical': 0.0018026137899954935, 'mean_word_length': 3.2570332480818416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '7375013', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2251, 'n_words': 410}