<commit_before>import { AllGoals } from 'common/goals';
import { getLocaleId } from './db';
import { getMySQLInstance } from './db/mysql';

const STREAK_THRESHOLDS = [1, 3, 5, 10, 15];

const THRESHOLDS = [5, 20, 50, 100, 150];

const db = getMySQLInstance();

const ONE_DAY = 24 * 60 * 60 * 1000;
const daysBetween = (date1: Date, date2: Date) =>
  Math.floor((date1.getTime() - date2.getTime()) / ONE_DAY);

export default async function getGoals(
  client_id: string,
  locale?: string
): Promise<AllGoals> {
  const [rows] = await db.query(
    `
      SELECT type, created_at
      FROM (
        SELECT client_id, created_at, 'clip' AS type
        FROM clips
        ${locale ? 'WHERE locale_id = :locale_id' : ''}
        UNION SELECT votes.client_id, votes.created_at, 'vote' AS type
        FROM votes
        LEFT JOIN clips on votes.clip_id = clips.id
        ${locale ? 'WHERE locale_id = :locale_id' : ''}
      ) dates
      WHERE client_id = :client_id
      ORDER BY created_at ASC
    `,
    {
      client_id,
      locale_id: locale ? await getLocaleId(locale) : null,
    }
  );

  let maxStreak = 0;
  let streakDates: string[] = [];
  for (let i = 0; i < rows.length - 1; i++) {
    const dateTime1 = new Date(rows[i].created_at);
    let lastDateTime;
    for (let j = i + 1; j < rows.length; j++) {
      const dateTime2 = new Date(rows[j].created_at);

      const streakDays = daysBetween(dateTime2, dateTime1);<commit_msg>fix #1614 wrong count for goals page<commit_after>import { AllGoals } from 'common/goals';
import { getLocaleId } from './db';
import { getMySQLInstance } from './db/mysql';

const STREAK_THRESHOLDS = [1, 3, 5, 10, 15];

const THRESHOLDS = [5, 20, 50, 100, 150];

const db = getMySQLInstance();

const ONE_DAY = 24 * 60 * 60 * 1000;
const daysBetween = (date1: Date, date2: Date) =>
  Math.floor((date1.getTime() - date2.getTime()) / ONE_DAY);

export default async function getGoals(
  client_id: string,
  locale?: string
): Promise<AllGoals> {
  const [rows] = await db.query(
    `
      SELECT type, created_at
      FROM (
        SELECT client_id, created_at, 'clip' AS type
        FROM clips
        ${locale ? 'WHERE locale_id = :locale_id' : ''}
        UNION ALL
        SELECT votes.client_id, votes.created_at, 'vote' AS type
        FROM votes
        LEFT JOIN clips on votes.clip_id = clips.id
        ${locale ? 'WHERE locale_id = :locale_id' : ''}
      ) dates
      WHERE client_id = :client_id
      ORDER BY created_at ASC
    `,
    {
      client_id,
      locale_id: locale ? await getLocaleId(locale) : null,
    }
  );

  let maxStreak = 0;
  let streakDates: string[] = [];
  for (let i = 0; i < rows.length - 1; i++) {
    const dateTime1 = new Date(rows[i].created_at);
    let lastDateTime;
    for (let j = i + 1; j < rows.length; j++) {
      const dateTime2 = new Date(rows[j].created_at);

      const streakDays = daysBetween(dateTime2, dateTime1);
{'fraction_non_alphanumeric': 0.13049403747870528, 'fraction_numerical': 0.028620102214650767, 'mean_word_length': 2.812987012987013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5190860', 'n_tokens_mistral': 1094, 'n_tokens_neox': 997, 'n_words': 292}