Skip to content

Instantly share code, notes, and snippets.

@souri
Last active September 10, 2020 06:29
Show Gist options
  • Save souri/e55f27bf75b8ecf47df79943f45a6328 to your computer and use it in GitHub Desktop.
Save souri/e55f27bf75b8ecf47df79943f45a6328 to your computer and use it in GitHub Desktop.

Collections

Sells - Appointments (amount, qty, type of products, agent name, agent id)

Commissions - we are storing commissions for each appointment, each agent (agentid, agent name, appoint details, item details) Payouts - sparse collection - monthly payout records


Each agent - Sum of commissions - sum of payouts

Advantage of CRON - We can deal with long exec times!


Approach 1

How this collection is created:

  • (For past records) We created this on a cron BUT on an appointment basis
  • (For future records) trigger on sell completion

I/P Date, Page Size

  1. Find all distinct agents in commissions collection
  2. Limit
  3. Gram first n 4 Unwind
  4. Group on the collection by item 6, Sum
  5. Get all commissions

Problem: Num of document is same as number of documents in sell Net result: No advantages for this collection

Ex: If an agent has 20,000 appointments - we are dealing with 20,000 records Ex: x 50 num of agents 1,000,000


Approach 2

I/P Date, Page Size

How this collection is created:

  • (For past records) We created this on a cron on a date basis
  • (For future records) We will create this on a cron on a date basis

Date, Page Size (no change)

  1. Find all distinct agents in commissions collection
  2. Limit
  3. Gram first n 4 Unwind
  4. Group on the collection by item (This data will be manifold smaller) 6, Sum
  5. Get all commissions

Ex. If an agent has 20,000 appointments over 365- we are dealing with 365 records Ex. x 50 num of agents = 18250 records

Advantage: 1000000/18250 = 54x


Approach 3

Same as approach 2, with some optimizations

I/P Date, Page Size

  • Store all tier pricings in the commissions collection (t1, t2, t3)
  • Calculate the tier while doing a payout, and then choose from t1, t2 or t3

How this collection is created (more logic):

  • (For past records) We created this on a cron on a weekly basis
  • (For future records) We will create this on a cron on a weekly basis
  • CRON will run daily
  • Based on the current week - It will either
    • Create a new document, or,
    • Update the existing week document

Date, Page Size (no change)

  1. Find all distinct agents in commissions collection
  2. Limit
  3. Gram first n
  4. Unwind
  5. Group on the collection by item (This data will be manifold smaller) 6, Sum
  6. Get all commissions

Ex. If an agent has 20,000 appointments over 365- we are dealing with 365/7 records Ex. x 50 num of agents = 2600 records

Advantage: 1000000/18250/7 = 54x*7 = 378x improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment