<commit_before>import { settings } from 'app/settings/settings';
import { setSetting } from 'app/settings/actions';
import { toWishList } from './wishlist-file';
import { t } from 'app/i18next-t';
import _ from 'lodash';
import { showNotification } from 'app/notifications/notifications';
import { loadWishLists } from './actions';

export class WishListMonitor {
  private wishListIntervalId?: number;

  start = () => {
    if (this.wishListIntervalId) {
      return;
    }

    fetchWishList();

    window.setInterval(fetchWishList, 1000 * 60 * 60);
  };

  stop = () => {
    if (!this.wishListIntervalId) {
      return;
    }
    window.clearInterval(this.wishListIntervalId);
  };
}

// https://stackoverflow.com/a/43735902/66109
function daysAgo(startDate?: Date): number {
  if (!startDate) {
    return 0;
  }

  const differenceInMilliseconds = Math.abs(new Date().getTime() - startDate.getTime());

  return Math.ceil(differenceInMilliseconds / (1000 * 3600 * 24));
}

export function fetchWishList(showAlert?: boolean) {
  if (!settings.wishListSource) {
    return;
  }

  if (settings.wishListLastChecked && daysAgo(settings.wishListLastChecked) < 0.5) {
    return;
  }

  fetch(settings.wishListSource)
    .then((result) => result.text())
    .then((resultText) => transformAndStoreWishList(resultText, 'Fetch Wish List', showAlert))
    .then(() => setSetting('wishListLastChecked', new Date()));
}

export function transformAndStoreWishList(
  wishListResult: string,
  eventName: string,
  showAlert?: boolean
) {
  const wishListAndInfo = toWishList(wishListResult);
  ga('send', 'event', 'Rating Options', eventName);

  if (wishListAndInfo.wishListRolls.length > 0) {
    loadWishLists(wishListAndInfo);

    const titleAndDescription = _.compact([<commit_msg>Mark as checked after we transform.<commit_after>import { settings } from 'app/settings/settings';
import { setSetting } from 'app/settings/actions';
import { toWishList } from './wishlist-file';
import { t } from 'app/i18next-t';
import _ from 'lodash';
import { showNotification } from 'app/notifications/notifications';
import { loadWishLists } from './actions';

export class WishListMonitor {
  private wishListIntervalId?: number;

  start = () => {
    if (this.wishListIntervalId) {
      return;
    }

    fetchWishList();

    window.setInterval(fetchWishList, 1000 * 60 * 60);
  };

  stop = () => {
    if (!this.wishListIntervalId) {
      return;
    }
    window.clearInterval(this.wishListIntervalId);
  };
}

// https://stackoverflow.com/a/43735902/66109
function daysAgo(startDate?: Date): number {
  if (!startDate) {
    return 0;
  }

  const differenceInMilliseconds = Math.abs(new Date().getTime() - startDate.getTime());

  return Math.ceil(differenceInMilliseconds / (1000 * 3600 * 24));
}

export function fetchWishList(showAlert?: boolean) {
  if (!settings.wishListSource) {
    return;
  }

  if (settings.wishListLastChecked && daysAgo(settings.wishListLastChecked) < 0.5) {
    return;
  }

  fetch(settings.wishListSource)
    .then((result) => result.text())
    .then((resultText) => {
      transformAndStoreWishList(resultText, 'Fetch Wish List', showAlert);
      setSetting('wishListLastChecked', new Date());
    });
}

export function transformAndStoreWishList(
  wishListResult: string,
  eventName: string,
  showAlert?: boolean
) {
  const wishListAndInfo = toWishList(wishListResult);
  ga('send', 'event', 'Rating Options', eventName);

  if (wishListAndInfo.wishListRolls.length > 0) {
    loadWishLists(wishListAndInfo);

    const titleAndDescription = _.compact([
{'fraction_non_alphanumeric': 0.13726035009724924, 'fraction_numerical': 0.020561267018616283, 'mean_word_length': 4.128205128205129, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 2, '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': '4422495', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1147, 'n_words': 233}