<commit_before>Promise = require 'bluebird'
mysql = require 'mysql'

class DB
  constructor: (params) ->
    @_connection = mysql.createConnection params

  connect: ->
    new Promise (resolve, reject) =>
      @_connection.connect (err) ->
        if err
          reject err
        else
          resolve @_connection

  getConnection: ->
    @_connection

  setConnection: (connection) ->
    @_connection = connection
    @

  isConnected: ->
    @_connection.state is 'connected'

  closeConnection: ->
    @_connection.end()
    @

  records_for_week_by_dish_type: (dish_type, begin_date, end_date, {half} = {}) ->
    half ?= false
    sql = '
      SELECT
        date,
        COUNT(*) AS count,
        title
      FROM
        record r INNER JOIN
        dish d ON r.dish_id = d.id
      WHERE
        type = ? AND
        date >= ? AND
        date <= ? AND
        half = ?
      GROUP BY
        date
    '
    @_run_query sql, [dish_type, begin_date, end_date, half]

  _run_query: (sql, data) ->
    new Promise (resolve, reject) =>
      @_connection.query sql, data, (err, rows) =>
        if err
          reject err
        else
          resolve rows

module.exports = DB<commit_msg>[DB] Added method for retrieving total paying servings grouped by dish type<commit_after>Promise = require 'bluebird'
mysql = require 'mysql'

class DB
  constructor: (params) ->
    @_connection = mysql.createConnection params

  connect: ->
    new Promise (resolve, reject) =>
      @_connection.connect (err) ->
        if err
          reject err
        else
          resolve @_connection

  getConnection: ->
    @_connection

  setConnection: (connection) ->
    @_connection = connection
    @

  isConnected: ->
    @_connection.state is 'connected'

  closeConnection: ->
    @_connection.end()
    @

  records_for_week_by_dish_type: (dish_type, begin_date, end_date, {half} = {}) ->
    half ?= false
    sql = '
      SELECT
        date,
        COUNT(*) AS count,
        title
      FROM
        record r INNER JOIN
        dish d ON r.dish_id = d.id
      WHERE
        type = ? AND
        date >= ? AND
        date <= ? AND
        half = ?
      GROUP BY
        date
    '
    @_run_query sql, [dish_type, begin_date, end_date, half]

  total_paying_by_dish_type: (begin_date, end_date) ->
    sql = '
      SELECT
        COUNT(*) count,
        type
      FROM
        record r INNER JOIN
        dish d ON r.dish_id = d.id
      WHERE
        prime_cost = 0 AND
        date >= ? AND
        date <= ?
      GROUP BY
        type
    '
    @_run_query sql, [begin_date, end_date]

  _run_query: (sql, data) ->
    new Promise (resolve, reject) =>
      @_connection.query sql, data, (err, rows) =>
        if err
          reject err
        else
          resolve rows

module.exports = DB
{'fraction_non_alphanumeric': 0.09847679773290825, 'fraction_numerical': 0.00035423308537017357, 'mean_word_length': 1.846774193548387, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 31, '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': '709455', 'n_tokens_mistral': 896, 'n_tokens_neox': 865, 'n_words': 244}