<commit_before>'use strict'
var express = require('express');
var router = express.Router();

var db = require('../db')
var ServerActions = require('../ServerActions')

var curSong = null;
var songStartedAt = -1
var userIDs = [1];
var nextSongTimeout = null

function getNextSong() {
  return db.query('SELECT * FROM "playlist" WHERE user_id IN (' + userIDs.join(',') + ')')
    .then(function(result) {
      return result.rows.reduce(function(prev, next) {
        prev.push(next.id)
        return prev;
      }, [])

    }).catch(function(error) {
      console.log(error)
    })
    .then(function(ids) {
      return db.query('SELECT song_id FROM playlist_has_song WHERE playlist_id IN (' + ids.join(',') + ')')
    })
    .then(function(result) {
      return result.rows
    })
    .then(function(songIds) {
      return songIds[Math.floor(Math.random() * songIds.length)].song_id
    }).then(function(songId) {
      return db.query('SELECT * FROM "song" WHERE id=$1', [songId])
    }).then(function(result) {
      var song = result.rows[0]
      var validStatuses = ['valid', 'converted']
      if (validStatuses.indexOf(song.status) === -1) {
        process.nextTick(getNextSong)
        return
      }
      curSong = song;
      console.log(curSong)
      songStartedAt = Date.now()
      clearTimeout(nextSongTimeout)
      nextSongTimeout = setTimeout(getNextSong, song.duration * 1000)
      return curSong
    })
}

function skip() {
  clearTimeout(nextSongTimeout)
  getNextSong()
    .then((song) => {
      ServerActions.forceClientNewSong()
    })
}
router.get('/currentSong', function(request, result) {
  let seek = 0
  if (request.query.seek) {
    seek = Date.now() - songStartedAt
  }
  result.json({
    song: curSong,
    seek: seek,
  })
})

ServerActions.forceSkip.listen(skip)

module.exports = {
  routes: router,
  getNextSong: getNextSong,
};<commit_msg>curSong => currentSong<commit_after>'use strict'
var express = require('express');
var router = express.Router();

var db = require('../db')
var ServerActions = require('../ServerActions')

var currentSong = null;
var songStartedAt = -1
var userIDs = [1];
var nextSongTimeout = null

function getNextSong() {
  return db.query('SELECT * FROM "playlist" WHERE user_id IN (' + userIDs.join(',') + ')')
    .then(function(result) {
      return result.rows.reduce(function(prev, next) {
        prev.push(next.id)
        return prev;
      }, [])

    }).catch(function(error) {
      console.log(error)
    })
    .then(function(ids) {
      return db.query('SELECT song_id FROM playlist_has_song WHERE playlist_id IN (' + ids.join(',') + ')')
    })
    .then(function(result) {
      return result.rows
    })
    .then(function(songIds) {
      return songIds[Math.floor(Math.random() * songIds.length)].song_id
    }).then(function(songId) {
      return db.query('SELECT * FROM "song" WHERE id=$1', [songId])
    }).then(function(result) {
      var song = result.rows[0]
      var validStatuses = ['valid', 'converted']
      if (validStatuses.indexOf(song.status) === -1) {
        process.nextTick(getNextSong)
        return
      }
      currentSong = song;
      console.log(currentSong)
      songStartedAt = Date.now()
      clearTimeout(nextSongTimeout)
      nextSongTimeout = setTimeout(getNextSong, song.duration * 1000)
      return currentSong
    })
}

function skip() {
  clearTimeout(nextSongTimeout)
  getNextSong()
    .then((song) => {
      ServerActions.forceClientNewSong()
    })
}
router.get('/currentSong', function(request, result) {
  let seek = 0
  if (request.query.seek) {
    seek = Date.now() - songStartedAt
  }
  result.json({
    song: currentSong,
    seek: seek,
  })
})

ServerActions.forceSkip.listen(skip)

module.exports = {
  routes: router,
  getNextSong: getNextSong,
};
{'fraction_non_alphanumeric': 0.15069572066159098, 'fraction_numerical': 0.005250721974271462, 'mean_word_length': 3.101184068891281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '773369', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1226, 'n_words': 247}