Skip to content

Instantly share code, notes, and snippets.

@tankist
Last active August 29, 2015 14:04
Show Gist options
  • Save tankist/4ece69a60d768287343f to your computer and use it in GitHub Desktop.
Save tankist/4ece69a60d768287343f to your computer and use it in GitHub Desktop.
PP1 algorithm description

PP1 API main algorithm

Proportions

  1. Reorder APIs by country order (position per country)
  2. Calculate Sum of all weights per PP (or per country) s = w1 + w2 + ... + wn;
  3. Calculate proportion for each source: p1 = w1 / s, p2 = w2 / s, ...., pn = wn / s (save as original APIs proportions)
  4. Normalize proportions to desired items per page (default is 48): nP1 = p1 * 48, nP2 = p2 * 48, ...., nPn = pn * 48
  5. For each API:
    • If normalized proportion < 0.5, then multiply ipp by 2 (for instance if ebay wants 12 items to be displayed, then request 24 items)
  6. Send requests to APIs in parallel
  7. For each response filter duplicates (per source)
  8. Parse and merge responses
    • Filter response by:
      • availability for country
      • minPrice/maxPrice
      • sale
      • free shipping
      • whitelist
      • reviews availability (only check if there productId defined)
    • Sort response
    • Sort response by relevance
  9. Slice items using original APIs proportions from p.3 (p1, p2, ..., pn)
    • Calculate ipp for each source by rounding -> round(p1 * 48). If the results = 0, then result should be set to 1
    • Sum all ipps (prev. step) -> weightSummary
    • If weightSummary < original ipp (default is 48):
      • Calculate ipp for each source by ceiling -> ceil(p1 * 48).
      • Sum all ipps (prev. step) -> weightSummary
    • If weightSummary > 48
      • Sort all APIs by ipp in reverse order: 24, 15, 6, 3
      • While weightSummary > 48 (diff = weightSummary - 48 <- how many items should we remove) remove some amount of elements from each source (on each step we are removing round(diff / count of APIs) or at least 1 item)
    • Slice each source items according to calculated IPP
    • If some API got less results then it should have, then compensate its desired amount from other sources (which got more then it needed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment