<commit_before>import DataLoader from 'dataloader'
import LRUCache from 'lru-cache'

const debug = require('debug')('graphbrainz:extensions/fanart-tv')

export default function createLoader(options) {
  const { client } = options
  const cache = LRUCache({
    max: options.cacheSize,
    maxAge: options.cacheTTL,
    dispose(key) {
      debug(`Removed from cache. key=${key}`)
    }
  })
  // Make the cache Map-like.
  cache.delete = cache.del
  cache.clear = cache.reset

  const loader = new DataLoader(
    keys => {
      return Promise.all(
        keys.map(key => {
          const [entityType, id] = key
          return client
            .musicEntity(entityType, id)
            .catch(err => {
              if (err.statusCode === 404) {
                // 404s are OK, just return empty data.
                return {
                  artistbackground: [],
                  artistthumb: [],
                  musiclogo: [],
                  hdmusiclogo: [],
                  musicbanner: [],
                  musiclabel: [],
                  albums: {}
                }
              }
              throw err
            })
            .then(body => {
              if (entityType === 'artist') {
                const releaseGroupIDs = Object.keys(body.albums)
                debug(
                  `Priming album cache with ${releaseGroupIDs.length} album(s).`
                )
                releaseGroupIDs.forEach(key =>
                  loader.prime(['release-group', key], body)
                )
              }
              return body
            })
        })
      )
    },
    {
      batch: false,
      cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`,
      cacheMap: cache
    }<commit_msg>Fix for missing albums property on fanart.tv artists<commit_after>import DataLoader from 'dataloader'
import LRUCache from 'lru-cache'

const debug = require('debug')('graphbrainz:extensions/fanart-tv')

export default function createLoader(options) {
  const { client } = options
  const cache = LRUCache({
    max: options.cacheSize,
    maxAge: options.cacheTTL,
    dispose(key) {
      debug(`Removed from cache. key=${key}`)
    }
  })
  // Make the cache Map-like.
  cache.delete = cache.del
  cache.clear = cache.reset

  const loader = new DataLoader(
    keys => {
      return Promise.all(
        keys.map(key => {
          const [entityType, id] = key
          return client
            .musicEntity(entityType, id)
            .catch(err => {
              if (err.statusCode === 404) {
                // 404s are OK, just return empty data.
                return {
                  artistbackground: [],
                  artistthumb: [],
                  musiclogo: [],
                  hdmusiclogo: [],
                  musicbanner: [],
                  musiclabel: [],
                  albums: {}
                }
              }
              throw err
            })
            .then(body => {
              if (entityType === 'artist') {
                const releaseGroupIDs = Object.keys(body.albums || {})
                debug(
                  `Priming album cache with ${releaseGroupIDs.length} album(s).`
                )
                releaseGroupIDs.forEach(key =>
                  loader.prime(['release-group', key], body)
                )
              }
              return body
            })
        })
      )
    },
    {
      batch: false,
      cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`,
      cacheMap: cache
    }
{'fraction_non_alphanumeric': 0.11633172940843475, 'fraction_numerical': 0.003396546844041891, 'mean_word_length': 1.3765971755211837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '346318', 'n_tokens_mistral': 980, 'n_tokens_neox': 923, 'n_words': 213}