<commit_before>logger = new Logger("project")

getFileSize = (numBytes) ->
  if numBytes < 1024
    sizeStr = "#{numBytes} B"
  else if numBytes < 1024*1024
    sizeStr = "#{Math.ceil(numBytes / 1024.0)} KB"
  else if numBytes < 1024*1024*1024
    sizeStr = "#{Math.ceil(numBytes / (1024*1024.0))} MB"
  else if numBytes < 1024*1024*1024*1024
    sizeStr = "#{Math.ceil(numBytes / (1024*1024*1024.0))} GB"
  else
    throw new Error("File size too large: #{numBytes}")
  sizeStr

extendFile = (file) ->
  sizeStr = getFileSize(file.size)
  R.merge(file, {sizeStr: sizeStr})

@ProjectController = RouteController.extend({
  action: ->
    logger.debug('Getting active tab')
    tabNameMatch = /^.+#([^#]+)$/.exec(@url)
    tabName = if tabNameMatch? then tabNameMatch[1] else 'description'
    if tabName not in ['description', 'instructions', 'files']
      tabName = 'description'
    logger.debug("Current tab name: '#{tabName}'")
    @state.set('activeTab', tabName)
    @render()
  waitOn: -> R.map(Meteor.subscribe, ["projects", "users",])
  data: ->
    owner = Meteor.users.findOne(username: @params.owner)
    project = Projects.findOne(
      owner: @params.owner,
      projectId: @params.project,
    )
    if project?
      if owner?
        project.ownerName = owner.profile.name
        project.ownerUsername = owner.username
        logger.debug("Project owner's name: #{project.ownerName}")
      else
        logger.warn("Owner of project '#{@params.project}', '#{@params.owner}', is not in database")
      project.zipFileSize = if project.zipFile? then getFileSize(project.zipFile.size) else 0
      project.files = R.map(extendFile, project.files || [])
      if R.isEmpty(project.pictures || [])
        logger.debug("Project has no pictures, setting default")
        project.pictures = [{url: '/images/revox-reel-to-reel.jpg'}]
      licenseId = project.licenseId
      license = licenses[licenseId]
      logger.debug("Project has license '#{licenseId}':", license)
      project.license = license
      Session.set("mainPicture", project.pictures[0].url)
      logger.debug("Project has files: #{project.hasFiles}")
      logger.debug("Project has pictures: #{!R.isEmpty(project.pictures)}")
    project
  onBeforeAction: ->
    data = @data()
    if data?
      @next()
    else
      logger.debug("@data is not defined, rendering not found page")
      @render('projectNotFound', {
        data: {owner: @params.owner, projectId: @params.project},
      })
  onAfterAction: ->
    data = @data()
    if data?
      title = "#{data.owner}/#{data.projectId}"
      logger.debug("Setting SEO properties")
      SEO.set({
        title: title
        meta: {
          description: data.description<commit_msg>Fix logging<commit_after>logger = new Logger("project")

getFileSize = (numBytes) ->
  if numBytes < 1024
    sizeStr = "#{numBytes} B"
  else if numBytes < 1024*1024
    sizeStr = "#{Math.ceil(numBytes / 1024.0)} KB"
  else if numBytes < 1024*1024*1024
    sizeStr = "#{Math.ceil(numBytes / (1024*1024.0))} MB"
  else if numBytes < 1024*1024*1024*1024
    sizeStr = "#{Math.ceil(numBytes / (1024*1024*1024.0))} GB"
  else
    throw new Error("File size too large: #{numBytes}")
  sizeStr

extendFile = (file) ->
  sizeStr = getFileSize(file.size)
  R.merge(file, {sizeStr: sizeStr})

@ProjectController = RouteController.extend({
  action: ->
    logger.debug('Getting active tab')
    tabNameMatch = /^.+#([^#]+)$/.exec(@url)
    tabName = if tabNameMatch? then tabNameMatch[1] else 'description'
    if tabName not in ['description', 'instructions', 'files']
      tabName = 'description'
    logger.debug("Current tab name: '#{tabName}'")
    @state.set('activeTab', tabName)
    @render()
  waitOn: -> R.map(Meteor.subscribe, ["projects", "users",])
  data: ->
    owner = Meteor.users.findOne(username: @params.owner)
    project = Projects.findOne(
      owner: @params.owner,
      projectId: @params.project,
    )
    if project?
      if owner?
        project.ownerName = owner.profile.name
        project.ownerUsername = owner.username
        logger.debug("Project owner's name: #{project.ownerName}")
      else
        logger.warn("Owner of project '#{@params.project}', '#{@params.owner}', is not in database")
      project.zipFileSize = if project.zipFile? then getFileSize(project.zipFile.size) else 0
      project.files = R.map(extendFile, project.files || [])
      if R.isEmpty(project.pictures || [])
        logger.debug("Project has no pictures, setting default")
        project.pictures = [{url: '/images/revox-reel-to-reel.jpg'}]
      licenseId = project.licenseId
      license = licenses[licenseId]
      logger.debug("Project has license '#{licenseId}':", license)
      project.license = license
      Session.set("mainPicture", project.pictures[0].url)
      logger.debug("Project has files: #{!R.isEmpty(project.files)}")
      logger.debug("Project has pictures: #{!R.isEmpty(project.pictures)}")
    project
  onBeforeAction: ->
    data = @data()
    if data?
      @next()
    else
      logger.debug("@data is not defined, rendering not found page")
      @render('projectNotFound', {
        data: {owner: @params.owner, projectId: @params.project},
      })
  onAfterAction: ->
    data = @data()
    if data?
      title = "#{data.owner}/#{data.projectId}"
      logger.debug("Setting SEO properties")
      SEO.set({
        title: title
        meta: {
          description: data.description
{'fraction_non_alphanumeric': 0.15274986296363968, 'fraction_numerical': 0.025580120591997078, 'mean_word_length': 3.1627376425855513, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '4768560', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1701, 'n_words': 396}