<commit_before>require('dotenv').config();

const pg = require('pg');
const winston = require('winston');

const pool = module.exports.pool = new pg.Pool({
  user: process.env.PG_USERNAME,
  host: process.env.PG_HOSTNAME,
  database: process.env.PG_DATABASE,
  password: <PASSWORD>.PG_PASSWORD,
});

const findUserQuery = 'SELECT id FROM users WHERE discord_id = $1 LIMIT 1';
const createUserQuery = 'INSERT INTO users (discord_id, display_name) VALUES' +
  ' ($1, $2) RETURNING id';
module.exports.findOrCreateUser = async function(discordUser) {
    const client = await pool.connect();
    try {
      const searchResult = await client.query(findUserQuery, [discordUser.id]);
      if (searchResult.rowCount == 1) {
        // user was found
        winston.log('debug', 'Found user ID %s for Discord ID %s',
          searchResult.rows[0].id, discordUser.id);
        return searchResult.rows[0].id;
      } else {
        // user was not found, create it
        const createResult = await client.query(createUserQuery,
          [discordUser.id, discordUser.tag]);
        winston.log('debug', 'Created new user ID %s for Discord ID %s',
          createResult.rows[0].id, discordUser.id);
        return createResult.rows[0].id;
      }
    } finally {
      client.release();
    }
  };

const findGameQuery = 'SELECT id FROM games WHERE name = $1 LIMIT 1';
const createGameQuery = 'INSERT INTO games (name) VALUES ($1) RETURNING id';
module.exports.findOrCreateGame = async function(gameName) {
    const client = await pool.connect();
    try {
      const searchResult = await client.query(findGameQuery, [gameName]);
      if (searchResult.rowCount == 1) {
        // game was found
        winston.log('debug', 'Found game ID %s for game %s',
          searchResult.rows[0].id, gameName);
        return searchResult.rows[0].id;
      } else {
        const createResult = await client.query(createGameQuery, [gameName]);
        winston.log('debug', 'Created new game ID %s for game %s',
          createResult.rows[0].id, gameName);
        return createResult.rows[0].id;
      }
    } finally {
      client.release();
    }
  };

const createNewSessionQuery = 'INSERT INTO game_sessions (user_id, game_id,' +
  ' session_start, state) VALUES ($1, $2, $3, $4) RETURNING id';
module.exports.createNewSession = async function(userId, gameId) {
  const client = await pool.connect();
  try {
    const values = [userId, gameId, new Date(), 'in_progress'];
    const createResult = await client.query(createNewSessionQuery, values);
    winston.log('debug', 'Created new session (%s) for user %s playing %s',
      createResult.rows[0].id, userId, gameId);
    return createResult.rows[0].id;
  } finally {
    client.release();
  }
};

const sessionDiscoverQuery = 'SELECT id FROM game_sessions WHERE ' +
  'user_id = $1 AND game_id = $2 AND state = \'in_progress\' ' +
  'ORDER BY session_start DESC LIMIT 1';
const sessionEndQuery = 'UPDATE game_sessions SET state = \'completed\', ' +
  'session_end = $1 WHERE id = $2 RETURNING id';
module.exports.endSession = async function(userId, gameId) {
  const client = await pool.connect();
  try {
    const discoverResult = await client.query(sessionDiscoverQuery,
      [userId, gameId]);
    if (discoverResult.rowCount == 0) {
      // silently do nothing
      return;
    }
    const endResult = await client.query(sessionEndQuery,
      [new Date(), discoverResult.rows[0].id]);
    winston.log('debug', 'Ended session (%s) for user %s playing %s',
      endResult.rows[0].id, userId, gameId);
    return;
  } finally {
    client.release();
  }
};

const gameStatisticsQuery = 'SELECT SUM(session_end - session_start) AS time,' +
  ' name FROM game_sessions INNER JOIN games ON' +
  ' game_sessions.game_id = games.id WHERE state = \'completed\'' +
  ' AND visible = true GROUP BY name ORDER BY time DESC';
module.exports.getGameStatistics = async function() {
  const selectResult = await pool.query(gameStatisticsQuery);
  return selectResult.rows;
};<commit_msg>Limit to 50 results by default<commit_after>require('dotenv').config();

const pg = require('pg');
const winston = require('winston');

const pool = module.exports.pool = new pg.Pool({
  user: process.env.PG_USERNAME,
  host: process.env.PG_HOSTNAME,
  database: process.env.PG_DATABASE,
  password: <PASSWORD>,
});

const findUserQuery = 'SELECT id FROM users WHERE discord_id = $1 LIMIT 1';
const createUserQuery = 'INSERT INTO users (discord_id, display_name) VALUES' +
  ' ($1, $2) RETURNING id';
module.exports.findOrCreateUser = async function(discordUser) {
    const client = await pool.connect();
    try {
      const searchResult = await client.query(findUserQuery, [discordUser.id]);
      if (searchResult.rowCount == 1) {
        // user was found
        winston.log('debug', 'Found user ID %s for Discord ID %s',
          searchResult.rows[0].id, discordUser.id);
        return searchResult.rows[0].id;
      } else {
        // user was not found, create it
        const createResult = await client.query(createUserQuery,
          [discordUser.id, discordUser.tag]);
        winston.log('debug', 'Created new user ID %s for Discord ID %s',
          createResult.rows[0].id, discordUser.id);
        return createResult.rows[0].id;
      }
    } finally {
      client.release();
    }
  };

const findGameQuery = 'SELECT id FROM games WHERE name = $1 LIMIT 1';
const createGameQuery = 'INSERT INTO games (name) VALUES ($1) RETURNING id';
module.exports.findOrCreateGame = async function(gameName) {
    const client = await pool.connect();
    try {
      const searchResult = await client.query(findGameQuery, [gameName]);
      if (searchResult.rowCount == 1) {
        // game was found
        winston.log('debug', 'Found game ID %s for game %s',
          searchResult.rows[0].id, gameName);
        return searchResult.rows[0].id;
      } else {
        const createResult = await client.query(createGameQuery, [gameName]);
        winston.log('debug', 'Created new game ID %s for game %s',
          createResult.rows[0].id, gameName);
        return createResult.rows[0].id;
      }
    } finally {
      client.release();
    }
  };

const createNewSessionQuery = 'INSERT INTO game_sessions (user_id, game_id,' +
  ' session_start, state) VALUES ($1, $2, $3, $4) RETURNING id';
module.exports.createNewSession = async function(userId, gameId) {
  const client = await pool.connect();
  try {
    const values = [userId, gameId, new Date(), 'in_progress'];
    const createResult = await client.query(createNewSessionQuery, values);
    winston.log('debug', 'Created new session (%s) for user %s playing %s',
      createResult.rows[0].id, userId, gameId);
    return createResult.rows[0].id;
  } finally {
    client.release();
  }
};

const sessionDiscoverQuery = 'SELECT id FROM game_sessions WHERE ' +
  'user_id = $1 AND game_id = $2 AND state = \'in_progress\' ' +
  'ORDER BY session_start DESC LIMIT 1';
const sessionEndQuery = 'UPDATE game_sessions SET state = \'completed\', ' +
  'session_end = $1 WHERE id = $2 RETURNING id';
module.exports.endSession = async function(userId, gameId) {
  const client = await pool.connect();
  try {
    const discoverResult = await client.query(sessionDiscoverQuery,
      [userId, gameId]);
    if (discoverResult.rowCount == 0) {
      // silently do nothing
      return;
    }
    const endResult = await client.query(sessionEndQuery,
      [new Date(), discoverResult.rows[0].id]);
    winston.log('debug', 'Ended session (%s) for user %s playing %s',
      endResult.rows[0].id, userId, gameId);
    return;
  } finally {
    client.release();
  }
};

const gameStatisticsQuery = 'SELECT SUM(session_end - session_start) AS time,' +
  ' name FROM game_sessions INNER JOIN games ON' +
  ' game_sessions.game_id = games.id WHERE state = \'completed\'' +
  ' AND visible = true GROUP BY name ORDER BY time DESC LIMIT 50';
module.exports.getGameStatistics = async function() {
  const selectResult = await pool.query(gameStatisticsQuery);
  return selectResult.rows;
};
{'fraction_non_alphanumeric': 0.1285572878000495, 'fraction_numerical': 0.008166295471417966, 'mean_word_length': 3.3433637829124128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1814001', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2471, 'n_words': 703}