<commit_before>import R from 'ramda';
import moment from 'moment';

import { getBookings } from './endpoints';
import { getDateKey, getUniqueRoomKey } from '../utils/keys';

import todayBookings from '../data/bookings.json';

const CACHE_TODAY_MINUTES = 2;    // cache in minutes for today's bookings
const CACHE_DEFAULT_MINUTES = 10; // cache in minutes for any other bookings
// the reason for this: bookings made on the day can be automatic and can go
// through immediately

const cache = {};

function expired(time) {
  if (time == null) return true;
  const minutes = getDateKey(new Date()) === getDateKey(time) ?
                  CACHE_TODAY_MINUTES :
                  CACHE_DEFAULT_MINUTES;
  return (new Date() - time) / 1000 / 60 > minutes;
}

function requestBookingsForDay(date) {
  const start = moment(date).startOf("day").toDate();
  const end = moment(date).endOf("day").toDate();
  return getBookings({ start, end });
}

function structureBookings(bookings) {
  // TODO remove duplication of roomname, roomid?
  return R.groupBy(getUniqueRoomKey, bookings);
}

export function getBookingsForDay(date) {

  // temporarily resolve from local bookings.json
  return new Promise((resolve) => resolve(todayBookings));

  // TODO uncomment

  // const dateKey = getDateKey(date);
  //
  // if (cache[dateKey] && !cache[dateKey].invalidated && !expired(cache[dateKey].requested)) {
  //   return pending ? cache[dateKey].promise : new Promise((resolve) => resolve(cache[dateKey].bookingsByRoom));
  // } else {
  //
  //   const requested = new Date();
  //
  //   const promise = requestBookingsForDay(date)
  //     .then(structureBookings)
  //     .then((bookingsByRoom) => {
  //       // do not update cache if a newer request has updated it
  //       // this can happen when a request is invalidated while it's pending
  //       if (requested >= cache[dateKey].requested) {
  //         cache[dateKey] = {
  //           pending: false,
  //           invalidated: false,<commit_msg>Update cache expiration logic (and fix some bugs)<commit_after>import R from 'ramda';
import moment from 'moment';

import { getBookings } from './endpoints';
import { getDateKey, getUniqueRoomKey } from '../utils/keys';

import todayBookings from '../data/bookings.json';

const CACHE_TODAY_MINUTES = 2;    // cache in minutes for today's bookings
const CACHE_DEFAULT_MINUTES = 10; // cache in minutes for any other bookings
// the reason for this: bookings made on the day can be automatic and can go
// through immediately

const cache = {};

function expired(dateKey) {
  if (cache[dateKey] == null ||
      cache[dateKey].invalidated ||
      !cache[dateKey].pending && !cache[dateKey].received) {
    return true;
  } else if (pending) {
    return false;
  }
  const today = new Date();
  const received = cache[dateKey].received; // time received
  const minutes = getDateKey(today) === dateKey ?
                  CACHE_TODAY_MINUTES :
                  CACHE_DEFAULT_MINUTES;
  return (new Date() - received) / 1000 / 60 > minutes;
}

function requestBookingsForDay(date) {
  const start = moment(date).startOf("day").toDate();
  const end = moment(date).endOf("day").toDate();
  return getBookings({ start, end });
}

function structureBookings(bookings) {
  // TODO remove duplication of roomname, roomid?
  return R.groupBy(getUniqueRoomKey, bookings);
}

export function getBookingsForDay(date) {

  // temporarily resolve from local bookings.json
  return new Promise((resolve) => resolve(todayBookings));

  // TODO uncomment

  // const dateKey = getDateKey(date);
  //
  // if (cache[dateKey] && !cache[dateKey].invalidated && !expired(dateKey)) {
  //   return cache[dateKey].pending ?
  //          cache[dateKey].promise :
  //          new Promise((resolve) => resolve(cache[dateKey].bookingsByRoom));
  // } else {
  //
  //   const requested = new Date();
  //
  //   const promise = requestBookingsForDay(date)
  //     .then(structureBookings)
  //     .then((bookingsByRoom) => {
  //       // do not update cache if a newer request has updated it
  //       // this can happen when a request is invalidated while it's pending
  //       if (requested >= cache[dateKey].requested) {
  //         cache[dateKey] = {
  //           pending: false,
  //           invalidated: false,
  //           received: new Date(),
{'fraction_non_alphanumeric': 0.13040478380864765, 'fraction_numerical': 0.004139834406623735, 'mean_word_length': 3.011992619926199, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2080584', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1282, 'n_words': 365}