Skip to content

Commit

Permalink
chore: did some improvements to make dynamic query
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Sep 24, 2024
1 parent 33485c9 commit 102567e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
15 changes: 7 additions & 8 deletions course_discovery/apps/course_metadata/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ class PathwayType(Enum):
prod.enterprise.course_reviews
'''

COURSE_TYPES = {
'ocm_course' : ['audit', 'verified-audit', 'verified', 'credit-verified-audit', 'spoc-verified-audit', 'professional'],
'executive_education' : ['executive-education-2u'],
'bootcamp' : ['bootcamp-2u'],
}

SNOWFLAKE_POPULATE_PRODUCT_CATALOG_QUERY = """
WITH course_data AS (
SELECT
Expand Down Expand Up @@ -229,14 +235,7 @@ class PathwayType(Enum):
discovery.course_metadata_courseurlslug AS cslug ON c.id = cslug.course_id
WHERE
c.draft != 1 AND cr.hidden != 1 AND cr.status = 'published'
AND (
coursetype.slug LIKE 'audit'
OR coursetype.slug LIKE 'verified-audit'
OR coursetype.slug LIKE 'verified'
OR coursetype.slug LIKE 'credit-verified-audit'
OR coursetype.slug LIKE 'spoc-verified-audit'
OR coursetype.slug LIKE 'professional'
)
AND coursetype.slug IN ({course_types})
AND cslug.is_active = 1
GROUP BY
c.uuid, c.id, c.key, cr.key, c.title, coursetype.name, p.marketing_site_url_root, cslug.url_Slug, c.image, c.card_image_url, cr.RUN_START, cr.enrollment_end, cr.enrollment_start, cr.RUN_END, crt.is_marketable, cr.draft, cr.status, s.id, cr.slug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.db.models import Count, Prefetch, Q

from course_discovery.apps.course_metadata.constants import (
SNOWFLAKE_POPULATE_COURSE_LENGTH_QUERY, SNOWFLAKE_POPULATE_PRODUCT_CATALOG_QUERY
SNOWFLAKE_POPULATE_COURSE_LENGTH_QUERY, SNOWFLAKE_POPULATE_PRODUCT_CATALOG_QUERY, COURSE_TYPES
)
from course_discovery.apps.course_metadata.gspread_client import GspreadClient
from course_discovery.apps.course_metadata.models import Course, CourseType, Program, SubjectTranslation
Expand Down Expand Up @@ -86,11 +86,13 @@ def get_products_via_snowflake(self, product_type='ocm_course', product_source=N
database='prod'
)
cs = snowflake_client.cursor()
course_types = ', '.join([f"'{ct}'" for ct in COURSE_TYPES.get(product_type, [])])
rows = []
try:
cs.execute(SNOWFLAKE_POPULATE_PRODUCT_CATALOG_QUERY)
cs.execute(SNOWFLAKE_POPULATE_COURSE_LENGTH_QUERY.format(course_types=course_types))
rows = cs.fetchall()
except Exception as e:
logger.error('Error while fetching products from Snowflake')
logger.error('Error while fetching products from Snowflake for product catalog: %s', str(e))
finally:
cs.close()
snowflake_client.close()
Expand Down

0 comments on commit 102567e

Please sign in to comment.