Skip to content

Instantly share code, notes, and snippets.

@galarant
Last active September 12, 2017 22:33
Show Gist options
  • Save galarant/ee11c9c564cc64222597f751485bf05a to your computer and use it in GitHub Desktop.
Save galarant/ee11c9c564cc64222597f751485bf05a to your computer and use it in GitHub Desktop.
Reserve Inventory Conflicts

We have written a query on our pro_soda order, product, and line_item tables to find potential scheduling conflicts for booked line items. This first pass should be considered a proof of concept for the purposes of validating the result set. To keep this initial result set small, we have temporarily introduced the following limitations:

  • conflicts were defined as two booked line items with identical product_ids on the same day
  • only products 676, 677 and 1403 were used at the moment
  • we only looked at media plans with 100% probability
  • we only looked at line_items created after 8/1/2017

With those limitations in mind, we wrote the following query:

WITH line_item_run_dates AS (
  SELECT AS STRUCT
    ordr.name AS order_name,
    ordr.operative_id AS order_id,
    ordr.probability,
    line_item.operative_id AS line_item_id,
    line_item.started_at,
    line_item.finished_at,
    product.operative_id AS product_id,
    product.name AS product_name,
    dates.date AS run_date,
    1 AS order_count
  FROM
    prod.pro_soda_sales_lineitem AS line_item
  INNER JOIN
    prod.pro_soda_ad_ops_order AS ordr
  ON
    line_item.order_id = ordr.id
  INNER JOIN
    prod.pro_soda_ad_ops_product AS product
  ON
    line_item.product_id = product.id
  INNER JOIN
    z_rico.dates AS dates
  ON
    line_item.started_at <= DATETIME(dates.date) AND line_item.finished_at >= DATETIME(dates.date)
  WHERE
    line_item.created_at >= DATETIME("2017-08-01")
)
SELECT
  a.run_date,
  a.order_name AS order_name_1,
  b.order_name AS order_name_2,
  a.probability AS order_1_probability,
  b.probability AS order_2_probability,
  a.product_name,
  a.product_id,
  a.line_item_id AS line_item_id_1,
  b.line_item_id AS line_item_id_2
FROM
  line_item_run_dates AS a
INNER JOIN
  line_item_run_dates AS b
ON
  a.product_id = b.product_id
AND
  a.run_date = b.run_date
AND
  a.line_item_id != b.line_item_id
WHERE
  a.probability = 100
AND
  b.probability = 100
AND
  (a.product_id = "676" OR a.product_id = "677" OR a.product_id = "1403");

Which produced the following set of conflicts. Please investigate these individually and verify that the data is accurate. Once we have verified, we an expand this solution by removing some of the constraints listed above and talk about how to productize this with alerts.

[
  {
    "run_date": "2017-10-11",
    "order_name_1": "CW: Dynasty_Fall",
    "order_name_2": "Campbell's: Campbells_NBC_Tasty_Q3",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Fixed BSU_CPD",
    "product_id": "676",
    "line_item_id_1": "203263",
    "line_item_id_2": "203897"
  },
  {
    "run_date": "2017-09-01",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "Internal Ad Promo_Unsolved_Content Adjacency",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Fixed BSU_CPD",
    "product_id": "676",
    "line_item_id_1": "204858",
    "line_item_id_2": "206466"
  },
  {
    "run_date": "2017-09-02",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "Internal Ad Promo_Unsolved_Content Adjacency",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Fixed BSU_CPD",
    "product_id": "676",
    "line_item_id_1": "204858",
    "line_item_id_2": "206466"
  },
  {
    "run_date": "2017-08-31",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "Internal Ad Promo_Unsolved_Content Adjacency",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Fixed BSU_CPD",
    "product_id": "676",
    "line_item_id_1": "204858",
    "line_item_id_2": "206466"
  },
  {
    "run_date": "2017-09-01",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "The Weinstein Company: Tulip Fever (9/1)",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Awareness Card_CPD",
    "product_id": "677",
    "line_item_id_1": "204857",
    "line_item_id_2": "206467"
  },
  {
    "run_date": "2017-09-01",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "The Weinstein Company: Tulip Fever (9/1)",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Awareness Card_CPD",
    "product_id": "677",
    "line_item_id_1": "204857",
    "line_item_id_2": "206467"
  },
  {
    "run_date": "2017-09-02",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "The Weinstein Company: Tulip Fever (9/1)",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Awareness Card_CPD",
    "product_id": "677",
    "line_item_id_1": "204857",
    "line_item_id_2": "206467"
  },
  {
    "run_date": "2017-08-31",
    "order_name_1": "Perfetti Van Melle: Mentos Pure Fresh Gum",
    "order_name_2": "The Weinstein Company: Tulip Fever (9/1)",
    "order_1_probability": "100",
    "order_2_probability": "100",
    "product_name": "US_On-Site_Homepage Awareness Card_CPD",
    "product_id": "677",
    "line_item_id_1": "204857",
    "line_item_id_2": "206467"
  },
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment