<commit_before>import { format } from 'date-fns';
import { sql, DatabaseTransactionConnectionType } from 'slonik';

import { getDateRangeUnion } from './date-union';

import config from '~api/config';
import { PageListStandard } from '~api/types';

export type PreviewRow = {
  date: string;
  value: number;
};

export async function selectPreviewRowsStandard(
  db: DatabaseTransactionConnectionType,
  uid: number,
  page: PageListStandard,
  startDate: Date,
  endDate: Date,
): Promise<readonly PreviewRow[]> {
  const result = await db.query<PreviewRow>(sql`
  SELECT date, SUM(cost) AS value
  FROM list_standard
  WHERE ${sql.join(
    [
      sql`uid = ${uid}`,
      sql`page = ${page}`,
      sql`date >= ${format(startDate, 'yyyy-MM-dd')}`,
      sql`date <= ${format(endDate, 'yyyy-MM-dd')}`,
    ],
    sql` AND `,
  )}
  ${
    page === PageListStandard.General
      ? sql`AND category != ALL(${sql.array(
          config.data.overview.investmentPurchaseCategories,
          'text',
        )})`
      : sql``
  }
  GROUP BY date
  ORDER BY date
  `);
  return result.rows;
}

export async function selectPreviewRowsStocks(
  db: DatabaseTransactionConnectionType,
  uid: number,
  monthDays: Date[],
): Promise<readonly PreviewRow[]> {
  const result = await db.query<PreviewRow>(sql`
  WITH dates AS (${getDateRangeUnion(monthDays)})<commit_msg>fix: correct query column name<commit_after>import { format } from 'date-fns';
import { sql, DatabaseTransactionConnectionType } from 'slonik';

import { getDateRangeUnion } from './date-union';

import config from '~api/config';
import { PageListStandard } from '~api/types';

export type PreviewRow = {
  date: string;
  value: number;
};

export async function selectPreviewRowsStandard(
  db: DatabaseTransactionConnectionType,
  uid: number,
  page: PageListStandard,
  startDate: Date,
  endDate: Date,
): Promise<readonly PreviewRow[]> {
  const result = await db.query<PreviewRow>(sql`
  SELECT date, SUM(value) AS value
  FROM list_standard
  WHERE ${sql.join(
    [
      sql`uid = ${uid}`,
      sql`page = ${page}`,
      sql`date >= ${format(startDate, 'yyyy-MM-dd')}`,
      sql`date <= ${format(endDate, 'yyyy-MM-dd')}`,
    ],
    sql` AND `,
  )}
  ${
    page === PageListStandard.General
      ? sql`AND category != ALL(${sql.array(
          config.data.overview.investmentPurchaseCategories,
          'text',
        )})`
      : sql``
  }
  GROUP BY date
  ORDER BY date
  `);
  return result.rows;
}

export async function selectPreviewRowsStocks(
  db: DatabaseTransactionConnectionType,
  uid: number,
  monthDays: Date[],
): Promise<readonly PreviewRow[]> {
  const result = await db.query<PreviewRow>(sql`
  WITH dates AS (${getDateRangeUnion(monthDays)})
{'fraction_non_alphanumeric': 0.1393740902474527, 'fraction_numerical': 0.0, 'mean_word_length': 3.7314974182444063, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3713796', 'n_tokens_mistral': 898, 'n_tokens_neox': 865, 'n_words': 213}