<commit_before>### define
backbone : Backbone
moment : moment
utils: Utils
../team_time_collection : TeamTimeCollection
###

# #############
# Project ViewModel
#
# Handles the transformation of the general TeamTimeCollection data for the
# 'project' view and serves common view models attributes for this view.
# #############

class ProjectViewModel extends Backbone.Model

  defaults :
    date : moment()
    rows : new Backbone.Collection()
    monthlyTotalHours : 0
    dailyTotalHours : 0
    urlRoot : "project"


  initialize : (options = {}) ->

    if options.date
      @set("date", moment(options.date).startOf("month"))

    @teamTimeCollection = new TeamTimeCollection(date : @get("date"))
    @listenTo(@teamTimeCollection, "sync", @synced)


  fetch : =>

    @teamTimeCollection.date = @get("date")
    return @teamTimeCollection.fetch().done(
      =>
        @trigger("sync", @)
    )


  synced : ->

    # Make sure we save the server data under a meaningful attribute
    @set {
      monthlyTotalHours: @teamTimeCollection.getMonthlyTotalHours()
      dailyTotalHours: @teamTimeCollection.getDailyTotalHours()
    }

    @transformData()


  transformData : ->

    # reset
    @get("rows").reset([])

    allTimeEntries= [].concat(@teamTimeCollection.pluck("times")...)
    allTimeEntries = _.groupBy(allTimeEntries, (timeEntry) ->
      return timeEntry.issue.project
    )

    _.each(allTimeEntries, (timings, repositoryName) =>

      dailyEntriesPerUser = @teamTimeCollection.map((user) =>

        # and group his issue by his repositories (aka projects)
        userIssues = _.filter(user.get("times"), (timeEntry) ->
          timeEntry.issue.project == repositoryName
        )
        return Utils.range(1, @get("date").daysInMonth()).map(
          (day) -> return Utils.sum(
            _.map(userIssues, (issue) ->
              if moment(issue.timestamp).date() == day
                return issue.duration
              else
                return 0
            )
          )
        )
      )


      # Sum up the total amount of hours per day for every issue
      sumDaily = Utils.range(1, @get("date").daysInMonth()).map((i) -> return Utils.sum(dailyEntriesPerUser, i - 1)) #-1 because days start with 1 and arrays with 0
      sumTotal = Utils.sum(sumDaily)

      #Add that shit to the collection as a table 'header' for every user
      @get("rows").add(
        isUserHeader : true
        userName : repositoryName
        sumTotal : sumTotal
        sumDaily : sumDaily
      )

      # Add the daily individual time logs to the collection
      @teamTimeCollection.forEach((user, i) =>
        @get("rows").add(
          isUserHeader : false
          projectName : user.get("name")
          sumCompleteProject : Utils.sum(dailyEntriesPerUser[i])
          timeEntry : dailyEntriesPerUser[i]
        )
      )<commit_msg>fixed project table<commit_after>### define
backbone : Backbone
moment : moment
utils: Utils
../team_time_collection : TeamTimeCollection
###

# #############
# Project ViewModel
#
# Handles the transformation of the general TeamTimeCollection data for the
# 'project' view and serves common view models attributes for this view.
# #############

class ProjectViewModel extends Backbone.Model

  defaults :
    date : moment()
    rows : new Backbone.Collection()
    monthlyTotalHours : 0
    dailyTotalHours : 0
    urlRoot : "project"


  initialize : (options = {}) ->

    if options.date
      @set("date", moment(options.date).startOf("month"))

    @teamTimeCollection = new TeamTimeCollection(date : @get("date"))
    @listenTo(@teamTimeCollection, "sync", @synced)


  fetch : =>

    @teamTimeCollection.date = @get("date")
    return @teamTimeCollection.fetch().done(
      =>
        @trigger("sync", @)
    )


  synced : ->

    # Make sure we save the server data under a meaningful attribute
    @set {
      monthlyTotalHours: @teamTimeCollection.getMonthlyTotalHours()
      dailyTotalHours: @teamTimeCollection.getDailyTotalHours()
    }

    @transformData()


  transformData : ->

    # reset
    @get("rows").reset([])

    projectIssues = @teamTimeCollection.groupBy((timeEntry) ->
      return timeEntry.get("issue").project
    )

    _.each(projectIssues, (timings, repositoryName) =>

      userProjects = _.groupBy(timings, (timing) ->
        return timing.get("userGID")
      )

      # Finally add the days of the month to every project...
      userProjects = _.transform(userProjects,
        (result, project, key) =>
          result[key] = Utils.range(1, @get("date").daysInMonth()).map(
            (day) -> return Utils.sum(
              _.filter(project,
                (project) -> return moment(project.get("timestamp")).date() == day
              ).map(
                (projectFilterdByDay) -> return projectFilterdByDay.get("duration")
              )
            )
          )
      )

      # Sum up the total amount of hours per day for every issue
      # sumDaily = Utils.range(1, @get("date").daysInMonth()).map((i) ->
      #   return _.foldl(userProjects, (sum, project) ->
      #     sum + project[i]
      #   , i )
      # ) #-1 because days start with 1 and arrays with 0
      sumDaily = Utils.range(1, @get("date").daysInMonth()).map((i) -> return Utils.sum(_.values(userProjects), i - 1)) #-1 because days start with 1 and arrays with 0

      sumTotal = Utils.sum(sumDaily)

      #Add that shit to the collection as a table 'header' for every user
      @get("rows").add(
        isUserHeader : true
        userName : repositoryName
        sumTotal : sumTotal
        sumDaily : sumDaily
      )

      # Add the daily individual time logs to the collection
      _.each(userProjects, (dailyEntries, userID) =>
        @get("rows").add(
          isUserHeader : false
          projectName : userID
          sumCompleteProject : Utils.sum(dailyEntries)
          timeEntry : dailyEntries
        )
      )
{'fraction_non_alphanumeric': 0.1250420168067227, 'fraction_numerical': 0.0035294117647058825, 'mean_word_length': 2.5592105263157894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7486328', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1666, 'n_words': 431}