<commit_before>import _ from 'lodash';
import moment from 'moment';
import Promise from 'bluebird';
import debugLib from 'debug';
import models, { sequelize, Op } from '../server/models';
import emailLib from '../server/lib/email';
import config from 'config';
import { exportToPDF } from '../server/lib/utils';
import { getTransactions } from '../server/lib/transactions';
import { getHostedCollectives, getBackersStats, sumTransactions } from '../server/lib/hostlib';

const debug = debugLib('hostreport');

if (process.env.SKIP_PLATFORM_STATS) {
  console.log('Skipping computing platform stats');
}

const summary = {
  totalHosts: 0,
  totalActiveHosts: 0,
  totalCollectives: 0,
  totalActiveCollectives: 0,
  numberTransactions: 0,
  numberDonations: 0,
  numberPaidExpenses: 0,
  hosts: [],
};

async function HostReport(year, month, hostId) {
  let startDate, endDate;

  const d = new Date();
  d.setFullYear(year);
  let yearlyReport = false;
  if (typeof month === 'number') {
    d.setMonth(month);
    startDate = new Date(d.getFullYear(), d.getMonth(), 1);
    endDate = new Date(d.getFullYear(), d.getMonth() + 1, 1);
  } else {
    // yearly report
    yearlyReport = true;
    startDate = new Date(d.getFullYear(), 0, 1);
    endDate = new Date(d.getFullYear() + 1, 0, 1);
  }

  const endDateIncluded = moment(endDate)
    .subtract(1, 'days')
    .toDate();

  const dateRange = {
    createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
  };

  const emailTemplate = yearlyReport ? 'host.yearlyreport' : 'host.monthlyreport';
  const reportName = yearlyReport ? `${year} Yearly Host Report` : `${year}/${month + 1} Monthly Host Report`;
  const dateFormat = yearlyReport ? 'YYYY' : 'YYYYMM';
  const csv_filename = `${moment(d).format(dateFormat)}-transactions.csv`;
  const pdf_filename = `${moment(d).format(dateFormat)}-expenses.pdf`;
  console.log('startDate', startDate, 'endDate', endDate);

  year = year || startDate.getFullYear();

  let previewCondition = '';
  if (process.env.DEBUG && process.env.DEBUG.match(/preview/))
    previewCondition = 'AND c.id IN (11004, 9804, 9802, 9801)'; // open source collective host, wwcode host, brusselstogether, changex
  // previewCondition = "AND c.id IN (9802)"; // brusselstogether

  if (hostId) {
    previewCondition = `AND c.id = ${hostId}`;
  }

  if (process.env.SLUGS) {
    const slugs = process.env.SLUGS.split(',');
    previewCondition = `AND c.slug IN ('${slugs.join("','")}')`;
    process.env.SKIP_PLATFORM_STATS = true;
  }

  const getHostStats = (host, collectiveids) => {
    const where = { CollectiveId: { [Op.in]: collectiveids } };
    const whereWithDateRange = { ...where, ...dateRange };

    const payoutProcessorFeesQuery = service => {
      // When using Sequelize query.include[].where it performs the condition on the join and it doesn't work
      // that's why have to do the query this this way.
      if (service === 'paypal') {
        where['$PaymentMethod.service$'] = 'paypal';
      } else {
        where['$PaymentMethod.service$'] = {<commit_msg>fix(hostReport): take into account host at the time of transaction<commit_after>import _ from 'lodash';
import moment from 'moment';
import Promise from 'bluebird';
import debugLib from 'debug';
import models, { sequelize, Op } from '../server/models';
import emailLib from '../server/lib/email';
import config from 'config';
import { exportToPDF } from '../server/lib/utils';
import { getTransactions } from '../server/lib/transactions';
import { getHostedCollectives, getBackersStats, sumTransactions } from '../server/lib/hostlib';

const debug = debugLib('hostreport');

if (process.env.SKIP_PLATFORM_STATS) {
  console.log('Skipping computing platform stats');
}

const summary = {
  totalHosts: 0,
  totalActiveHosts: 0,
  totalCollectives: 0,
  totalActiveCollectives: 0,
  numberTransactions: 0,
  numberDonations: 0,
  numberPaidExpenses: 0,
  hosts: [],
};

async function HostReport(year, month, hostId) {
  let startDate, endDate;

  const d = new Date();
  d.setFullYear(year);
  let yearlyReport = false;
  if (typeof month === 'number') {
    d.setMonth(month);
    startDate = new Date(d.getFullYear(), d.getMonth(), 1);
    endDate = new Date(d.getFullYear(), d.getMonth() + 1, 1);
  } else {
    // yearly report
    yearlyReport = true;
    startDate = new Date(d.getFullYear(), 0, 1);
    endDate = new Date(d.getFullYear() + 1, 0, 1);
  }

  const endDateIncluded = moment(endDate)
    .subtract(1, 'days')
    .toDate();

  const dateRange = {
    createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
  };

  const emailTemplate = yearlyReport ? 'host.yearlyreport' : 'host.monthlyreport';
  const reportName = yearlyReport ? `${year} Yearly Host Report` : `${year}/${month + 1} Monthly Host Report`;
  const dateFormat = yearlyReport ? 'YYYY' : 'YYYYMM';
  const csv_filename = `${moment(d).format(dateFormat)}-transactions.csv`;
  const pdf_filename = `${moment(d).format(dateFormat)}-expenses.pdf`;
  console.log('startDate', startDate, 'endDate', endDate);

  year = year || startDate.getFullYear();

  let previewCondition = '';
  if (process.env.DEBUG && process.env.DEBUG.match(/preview/))
    previewCondition = 'AND c.id IN (11004, 9804, 9802, 9801)'; // open source collective host, wwcode host, brusselstogether, changex
  // previewCondition = "AND c.id IN (9802)"; // brusselstogether

  if (hostId) {
    previewCondition = `AND c.id = ${hostId}`;
  }

  if (process.env.SLUGS) {
    const slugs = process.env.SLUGS.split(',');
    previewCondition = `AND c.slug IN ('${slugs.join("','")}')`;
    process.env.SKIP_PLATFORM_STATS = true;
  }

  const getHostStats = (host, collectiveids) => {
    // Since collectives can change host,
    // we don't fetch transactions based on the CollectiveId but based on the HostCollectiveId
    // at the time of the transaction
    const where = { HostCollectiveId: host.id };
    const whereWithDateRange = { ...where, ...dateRange };

    const payoutProcessorFeesQuery = service => {
      // When using Sequelize query.include[].where it performs the condition on the join and it doesn't work
      // that's why have to do the query this this way.
      if (service === 'paypal') {
        where['$PaymentMethod.service$'] = 'paypal';
      } else {
        where['$PaymentMethod.service$'] = {
{'fraction_non_alphanumeric': 0.14991320814265424, 'fraction_numerical': 0.011993056651412341, 'mean_word_length': 3.787009063444109, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5683534', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2039, 'n_words': 548}