<commit_before>import getMockCodefuelSearchResults from 'js/components/Search/getMockCodefuelSearchResults'

const formatSearchResults = rawSearchResults => {
  return {
    resultsCount: 12,
    results: {
      pole: [],
      mainline: [],
      sidebar: [],
    },
  }
}

const fetchCodefuelSearchResults = async () => {
  console.log('Fetch CodeFuel search results.')
  const rawSearchResults = await getMockCodefuelSearchResults()
  const formattedSearchResults = formatSearchResults(rawSearchResults)
  return formattedSearchResults
}

export default fetchCodefuelSearchResults
<commit_msg>Format and display organic web page results<commit_after>import { get } from 'lodash/object'
import getMockCodefuelSearchResults from 'js/components/Search/getMockCodefuelSearchResults'

const ITEMS = 'Items'
const PLACEMENT_POLE = 'Pole'
const PLACEMENT_MAINLINE = 'Mainline'
const PLACEMENT_SIDEBAR = 'Sidebar'

// CodeFuel's top-level keys for each result type.
const CODEFUEL_RESULT_TYPE_KEY_WEBPAGE = 'OrganicResults'

// Result types used internally in components.
const RESULT_TYPE_WEBPAGES = 'WebPages'

const createWebpageResult = webpageData => {
  return {
    type: RESULT_TYPE_WEBPAGES,
    key: webpageData.TargetedUrl, // assume unique
    value: {
      displayUrl: webpageData.DisplayUrl,
      deepLinks: get(webpageData, 'DeepLinks', []).map(deepLink => ({
        name: deepLink.Title,
        url: deepLink.TargetedUrl,
        snippet: deepLink.Description,
      })),
      id: webpageData.TargetedUrl, // assume unique
      name: webpageData.Title,
      pixelUrl: webpageData.PixelUrl, // TODO: utilize this in the result component
      snippet: webpageData.Description,
      url: webpageData.TargetedUrl,
    },
    placement: webpageData.PlacementHint,
    rank: webpageData.Rank,
  }
}

const formatSearchResults = rawSearchResults => {
  console.log('raw search results:', rawSearchResults)

  const poleResults = []
  const mainlineResults = []
  const sidebarResults = []

  // Iterate through result data, formatting each result's data
  // and assigning them to the appropriate position (mainline,
  // sidebar, etc).
  const webpageResults = get(
    rawSearchResults,
    [CODEFUEL_RESULT_TYPE_KEY_WEBPAGE, ITEMS],
    []
  ).map(item => createWebpageResult(item))

  const allResults = [].concat(webpageResults)

  allResults.forEach(item => {
    switch (item.placement) {
      case PLACEMENT_POLE: {
        poleResults.push(item)
        break
      }
      case PLACEMENT_MAINLINE: {
        mainlineResults.push(item)
        break
      }
      case PLACEMENT_SIDEBAR: {
        sidebarResults.push(item)
        break
      }
      default:
        break
    }
  })
  return {
    resultsCount: get(rawSearchResults, [
      CODEFUEL_RESULT_TYPE_KEY_WEBPAGE,
      'NumResults',
    ]),
    results: {
      pole: poleResults,
      mainline: mainlineResults,
      sidebar: sidebarResults,
    },
  }
}

const fetchCodefuelSearchResults = async () => {
  console.log('Fetch CodeFuel search results.')
  const rawSearchResults = await getMockCodefuelSearchResults()
  const formattedSearchResults = formatSearchResults(rawSearchResults)
  return formattedSearchResults
}

export default fetchCodefuelSearchResults

{'fraction_non_alphanumeric': 0.09085328422344997, 'fraction_numerical': 0.0006138735420503376, 'mean_word_length': 3.6161473087818696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '978496', 'n_tokens_mistral': 991, 'n_tokens_neox': 960, 'n_words': 214}