models.py: Code cleanup

Cleaned up the class Raw to have better readability.
Also added docstring to methods in this class.

Signed-off-by: Mariano Lopez <mariano.lopez@linux.intel.com>
This commit is contained in:
Mariano Lopez 2015-07-09 16:47:47 -05:00 committed by Aníbal Limón
parent e8a13de121
commit 0d295280da

View File

@ -371,63 +371,47 @@ class RecipeUpgrade(models.Model):
self.commit_date) self.commit_date)
class Raw(): class Raw():
""" Raw SQL call to improve performance
Table abbrevations:
re: Recipe
ma: Maintainer
reup: Recipe Upstream
reupg: Recipe Ugrade
rema: Recipe Maintainer
remahi: Recipe Maintainer History
"""
@staticmethod @staticmethod
def get_remahi_by_end_date(date): def get_re_all():
""" Get all Recipes """
cur = connection.cursor() cur = connection.cursor()
cur.execute("""SELECT id, pn, pv, summary
cur.execute("""SELECT id FROM layerindex_Recipe;
FROM rrs_RecipeMaintainerHistory """)
WHERE date <= %s return Raw.dictfetchall(cur)
ORDER BY date DESC
LIMIT 1;
""", [str(date)])
ret = cur.fetchone()
if not ret:
cur.execute("""SELECT id
FROM rrs_RecipeMaintainerHistory
ORDER BY date
LIMIT 1;""")
ret = cur.fetchone()
return ret
@staticmethod @staticmethod
def get_re_by_mantainer_and_date(maintainer, date_id): def get_re_by_mantainer_and_date(maintainer, date_id):
""" Get Recipes based on Maintainer and Recipe Maintainer History """
recipes = [] recipes = []
cur = connection.cursor() cur = connection.cursor()
cur.execute("""SELECT DISTINCT rema.recipe_id cur.execute("""SELECT DISTINCT rema.recipe_id
FROM rrs_RecipeMaintainer as rema FROM rrs_RecipeMaintainer AS rema
INNER JOIN rrs_maintainer AS ma INNER JOIN rrs_maintainer AS ma
ON rema.maintainer_id = ma.id ON rema.maintainer_id = ma.id
WHERE rema.history_id = %s AND ma.name = %s; WHERE rema.history_id = %s
""", [date_id, maintainer]) AND ma.name = %s;
""", [date_id, maintainer])
for re in cur.fetchall(): for re in cur.fetchall():
recipes.append(re[0]) recipes.append(re[0])
return recipes return recipes
@staticmethod
def get_reup_by_recipes_and_date(recipes_id, date_id=None):
stats = []
recipes = str(recipes_id).strip('[]')
if date_id:
qry = """SELECT recipe_id, status, no_update_reason, version
FROM rrs_RecipeUpstream"""
qry += "\nWHERE history_id = '%s' AND" % str(date_id)
qry += "\nrecipe_id IN (%s)\n" % recipes
cur = connection.cursor()
cur.execute(qry)
stats = Raw.dictfetchall(cur)
return stats
@staticmethod @staticmethod
def get_ma_by_recipes_and_date(recipes_id, date_id=None): def get_ma_by_recipes_and_date(recipes_id, date_id=None):
""" Get Maintainer based on Recipes and Recipe Upstream History """
stats = [] stats = []
recipes = str(recipes_id).strip('[]') recipes = str(recipes_id).strip('[]')
@ -436,8 +420,8 @@ class Raw():
FROM rrs_RecipeMaintainer AS rema FROM rrs_RecipeMaintainer AS rema
INNER JOIN rrs_Maintainer AS ma INNER JOIN rrs_Maintainer AS ma
ON rema.maintainer_id = ma.id""" ON rema.maintainer_id = ma.id"""
qry += "\nWHERE rema.history_id = '%s' AND" % str(date_id) qry += "\nWHERE rema.history_id = '%s'" % str(date_id)
qry += "\nrema.recipe_id IN (%s)\n" % recipes qry += "\nAND rema.recipe_id IN (%s);" % recipes
cur = connection.cursor() cur = connection.cursor()
cur.execute(qry) cur.execute(qry)
stats = Raw.dictfetchall(cur) stats = Raw.dictfetchall(cur)
@ -445,68 +429,82 @@ class Raw():
return stats return stats
@staticmethod @staticmethod
def get_re_all(): def get_reup_by_recipes_and_date(recipes_id, date_id=None):
cur = connection.cursor() """ Get Recipe Upstream based on Recipes and Recipe Upstream History """
cur.execute("""SELECT id, pn, pv, summary stats = []
FROM layerindex_recipe""") recipes = str(recipes_id).strip('[]')
return Raw.dictfetchall(cur)
@staticmethod if date_id:
def get_reupg_by_date(date): qry = """SELECT recipe_id, status, no_update_reason, version
cur = connection.cursor() FROM rrs_RecipeUpstream"""
cur.execute("""SELECT re.id, re.pn, re.summary, te.version, rownum FROM ( qry += "\nWHERE history_id = '%s'" % str(date_id)
SELECT recipe_id, version, commit_date, qry += "\nAND recipe_id IN (%s);" % recipes
ROW_NUMBER() OVER( cur = connection.cursor()
PARTITION BY recipe_id cur.execute(qry)
ORDER BY commit_date DESC stats = Raw.dictfetchall(cur)
) AS rownum
FROM rrs_RecipeUpgrade return stats
WHERE commit_date <= %s) AS te
INNER JOIN layerindex_Recipe AS re
ON te.recipe_id = re.id
WHERE rownum = 1
ORDER BY re.pn;
""", [date])
return Raw.dictfetchall(cur)
@staticmethod @staticmethod
def get_reup_by_last_updated(date): def get_reup_by_last_updated(date):
""" Get last time the Recipes were upgraded """
cur = connection.cursor() cur = connection.cursor()
cur.execute("""SELECT te.recipe_id, te.status, te.date, te.rownum FROM( cur.execute("""SELECT te.recipe_id, te.status, te.date, te.rownum FROM(
SELECT recipe_id, status, date, ROW_NUMBER() OVER( SELECT recipe_id, status, date, ROW_NUMBER() OVER(
PARTITION BY recipe_id PARTITION BY recipe_id
ORDER BY date DESC ORDER BY date DESC
) AS rownum ) AS rownum
FROM rrs_RecipeUpstream FROM rrs_RecipeUpstream
WHERE status = 'Y' WHERE status = 'Y'
AND date <= %s) AS te AND date <= %s) AS te
WHERE te.rownum = 1; WHERE te.rownum = 1;
""", [date]) """, [date])
return Raw.dictfetchall(cur) return Raw.dictfetchall(cur)
@staticmethod @staticmethod
def get_reup_by_date(date_id): def get_reup_by_date(date_id):
""" Get Recipes not up to date based on Recipe Upstream History """
cur = connection.cursor() cur = connection.cursor()
cur.execute("""SELECT DISTINCT recipe_id cur.execute("""SELECT DISTINCT recipe_id
FROM rrs_RecipeUpstream FROM rrs_RecipeUpstream
WHERE status = 'N' WHERE status = 'N'
AND history_id = %s AND history_id = %s
""", [date_id]) """, [date_id])
return [i[0] for i in cur.fetchall()] return [i[0] for i in cur.fetchall()]
@staticmethod
def get_reupg_by_date(date):
""" Get info for Recipes for the milestone """
cur = connection.cursor()
cur.execute("""SELECT re.id, re.pn, re.summary, te.version, rownum FROM (
SELECT recipe_id, version, commit_date, ROW_NUMBER() OVER(
PARTITION BY recipe_id
ORDER BY commit_date DESC
) AS rownum
FROM rrs_RecipeUpgrade
WHERE commit_date <= %s) AS te
INNER JOIN layerindex_Recipe AS re
ON te.recipe_id = re.id
WHERE rownum = 1
ORDER BY re.pn;
""", [date])
return Raw.dictfetchall(cur)
@staticmethod @staticmethod
def get_reupg_by_dates(start_date, end_date): def get_reupg_by_dates(start_date, end_date):
""" Get Recipe Upgrade for the milestone """
cur = connection.cursor() cur = connection.cursor()
cur.execute("""SELECT id, recipe_id, maintainer_id, author_date, commit_date cur.execute("""SELECT id, recipe_id, maintainer_id, author_date, commit_date
FROM rrs_recipeupgrade FROM rrs_RecipeUpgrade
WHERE commit_date >= %s WHERE commit_date >= %s
AND commit_date <= %s AND commit_date <= %s
ORDER BY commit_date DESC; ORDER BY commit_date DESC;
""", [start_date, end_date]) """, [start_date, end_date])
return Raw.dictfetchall(cur) return Raw.dictfetchall(cur)
@staticmethod @staticmethod
def get_reupg_by_dates_and_recipes(start_date, end_date, recipes_id): def get_reupg_by_dates_and_recipes(start_date, end_date, recipes_id):
""" Get Recipe Upgrade for the milestone based on Recipes """
recipes = str(recipes_id).strip('[]') recipes = str(recipes_id).strip('[]')
cur = connection.cursor() cur = connection.cursor()
@ -518,6 +516,30 @@ class Raw():
cur.execute(qry) cur.execute(qry)
return Raw.dictfetchall(cur) return Raw.dictfetchall(cur)
@staticmethod
def get_remahi_by_end_date(date):
""" Get the latest Recipe Maintainer History for the milestone """
cur = connection.cursor()
cur.execute("""SELECT id
FROM rrs_RecipeMaintainerHistory
WHERE date <= %s
ORDER BY date DESC
LIMIT 1;
""", [str(date)])
ret = cur.fetchone()
if not ret:
cur.execute("""SELECT id
FROM rrs_RecipeMaintainerHistory
ORDER BY date
LIMIT 1;
""")
ret = cur.fetchone()
return ret
@staticmethod @staticmethod
def dictfetchall(cursor): def dictfetchall(cursor):
"Returns all rows from a cursor as a dict" "Returns all rows from a cursor as a dict"