<commit_before>// @flow
import {
  api,
  loadLoginCredentials,
  isLoggedIn,
} from '../lib/login'
import { AsyncStorage, NetInfo } from 'react-native'

const ERROR_URL = 'https://www.stolaf.edu/sis/landing-page.cfm'
import {SisAuthenticationError} from './errors'
import buildFormData from './formdata'
import {parseHtml, cssSelect, getText} from './html'
import moment from 'moment'
import startsWith from 'lodash/startsWith'

// const FINANCIALS__CACHE__MEALS = 'financials:cache-time:meals'
const FINANCIALS__CACHE__BALANCES = 'financials:cache-time:balances'
const FINANCIALS__FLEX = 'financials:flex'
const FINANCIALS__OLE = 'financials:ole'
const FINANCIALS__PRINT = 'financials:print'

type FinancialDataShapeType = {flex: null|number, ole: null|number, print: null|number};

export async function getFinancialData(forceFromServer?: bool): Promise<FinancialDataShapeType> {
  let timeLastFetched = JSON.parse(await AsyncStorage.getItem(FINANCIALS__CACHE__BALANCES))
  let needsUpdate = moment(timeLastFetched).isBefore(moment().subtract(1, 'minute'))

  let isConnected = await NetInfo.isConnected.fetch()
  if (isConnected && (!timeLastFetched || needsUpdate || forceFromServer)) {
    return await getFinancialDataFromServer()
  }
  return await getFinancialDataFromStorage()
}


async function getFinancialDataFromStorage(): Promise<FinancialDataShapeType> {
  let vals = await Promise.all([
    AsyncStorage.getItem('financials:flex'),
    AsyncStorage.getItem('financials:ole'),
    AsyncStorage.getItem('financials:print'),
  ])

  let [flex, ole, print] = vals.map(v => JSON.parse(v))

  return {flex, ole, print}
}

async function getSisFinancialsPage(): string|null|SisAuthenticationError {
  try {
    let resp = await api.get('/sis/st-financials.cfm')
    if (startsWith(resp.url, ERROR_URL)) {<commit_msg>use constants for data caching keys in financials<commit_after>// @flow
import {
  api,
  loadLoginCredentials,
  isLoggedIn,
} from '../lib/login'
import { AsyncStorage, NetInfo } from 'react-native'

const ERROR_URL = 'https://www.stolaf.edu/sis/landing-page.cfm'
import {SisAuthenticationError} from './errors'
import buildFormData from './formdata'
import {parseHtml, cssSelect, getText} from './html'
import moment from 'moment'
import startsWith from 'lodash/startsWith'

// const FINANCIALS__CACHE__MEALS = 'financials:cache-time:meals'
const FINANCIALS__CACHE__BALANCES = 'financials:cache-time:balances'
const FINANCIALS__FLEX = 'financials:flex'
const FINANCIALS__OLE = 'financials:ole'
const FINANCIALS__PRINT = 'financials:print'

type FinancialDataShapeType = {flex: null|number, ole: null|number, print: null|number};

export async function getFinancialData(forceFromServer?: bool): Promise<FinancialDataShapeType> {
  let timeLastFetched = JSON.parse(await AsyncStorage.getItem(FINANCIALS__CACHE__BALANCES))
  let needsUpdate = moment(timeLastFetched).isBefore(moment().subtract(1, 'minute'))

  let isConnected = await NetInfo.isConnected.fetch()
  if (isConnected && (!timeLastFetched || needsUpdate || forceFromServer)) {
    return await getFinancialDataFromServer()
  }
  return await getFinancialDataFromStorage()
}


async function getFinancialDataFromStorage(): Promise<FinancialDataShapeType> {
  let vals = await Promise.all([
    AsyncStorage.getItem(FINANCIALS__FLEX),
    AsyncStorage.getItem(FINANCIALS__OLE),
    AsyncStorage.getItem(FINANCIALS__PRINT),
  ])

  let [flex, ole, print] = vals.map(v => JSON.parse(v))

  return {flex, ole, print}
}

async function getSisFinancialsPage(): string|null|SisAuthenticationError {
  try {
    let resp = await api.get('/sis/st-financials.cfm')
    if (startsWith(resp.url, ERROR_URL)) {
{'fraction_non_alphanumeric': 0.1185888738127544, 'fraction_numerical': 0.000542740841248304, 'mean_word_length': 5.981060606060606, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6566230', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1191, 'n_words': 253}