From 5cbffcf3ac1092a138ce5a4fee91b3d85ac5d110 Mon Sep 17 00:00:00 2001 From: blitzman Date: Tue, 3 Jan 2017 01:06:56 -0500 Subject: [PATCH] Handle counting number of fits per ship category more efficiently (see #819) --- eos/db/saveddata/queries.py | 31 +++++++++++++++++++------------ gui/shipBrowser.py | 4 +++- service/fit.py | 13 ++----------- service/market.py | 1 + 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/eos/db/saveddata/queries.py b/eos/db/saveddata/queries.py index bcecb3b69..f615aa913 100644 --- a/eos/db/saveddata/queries.py +++ b/eos/db/saveddata/queries.py @@ -306,24 +306,31 @@ def countAllFits(): return count -def countFitsWithShip(shipID, ownerID=None, where=None, eager=None): +def countFitsWithShip(lookfor, ownerID=None, where=None, eager=None): """ Get all the fits using a certain ship. If no user is passed, do this for all users. """ - if isinstance(shipID, int): - if ownerID is not None and not isinstance(ownerID, int): - raise TypeError("OwnerID must be integer") - filter = Fit.shipID == shipID - if ownerID is not None: - filter = and_(filter, Fit.ownerID == ownerID) + if ownerID is not None and not isinstance(ownerID, int): + raise TypeError("OwnerID must be integer") - filter = processWhere(filter, where) - eager = processEager(eager) - with sd_lock: - count = saveddata_session.query(Fit).options(*eager).filter(filter).count() + if isinstance(lookfor, int): + filter = Fit.shipID == lookfor + elif isinstance(lookfor, list): + if len(lookfor) == 0: + return 0 + filter = Fit.shipID.in_(lookfor) else: - raise TypeError("ShipID must be integer") + raise TypeError("You must supply either an integer or ShipID must be integer") + + if ownerID is not None: + filter = and_(filter, Fit.ownerID == ownerID) + + filter = processWhere(filter, where) + eager = processEager(eager) + with sd_lock: + count = saveddata_session.query(Fit).options(*eager).filter(filter).count() + return count diff --git a/gui/shipBrowser.py b/gui/shipBrowser.py index e1271b616..96bfb7ad5 100644 --- a/gui/shipBrowser.py +++ b/gui/shipBrowser.py @@ -34,6 +34,7 @@ Stage3Selected, EVT_SB_STAGE3_SEL = wx.lib.newevent.NewEvent() SearchSelected, EVT_SB_SEARCH_SEL = wx.lib.newevent.NewEvent() ImportSelected, EVT_SB_IMPORT_SEL = wx.lib.newevent.NewEvent() + class PFWidgetsContainer(PFListPane): def __init__(self,parent): PFListPane.__init__(self,parent) @@ -685,7 +686,8 @@ class ShipBrowser(wx.Panel): # set map & cache of fittings per category for cat in self.categoryList: - self.categoryFitCache[cat.ID] = sFit.groupHasFits(cat.ID) + itemIDs = [x.ID for x in cat.items] + self.categoryFitCache[cat.ID] = sFit.countFitsWithShip(itemIDs) > 1 for ship in self.categoryList: if self.filterShipsWithNoFits and not self.categoryFitCache[ship.ID]: diff --git a/service/fit.py b/service/fit.py index b2bb691ff..70bc020fc 100644 --- a/service/fit.py +++ b/service/fit.py @@ -142,19 +142,10 @@ class Fit(object): def countAllFits(self): return eos.db.countAllFits() - def countFitsWithShip(self, shipID): - count = eos.db.countFitsWithShip(shipID) + def countFitsWithShip(self, stuff): + count = eos.db.countFitsWithShip(stuff) return count - def groupHasFits(self, groupID): - sMkt = Market.getInstance() - grp = sMkt.getGroup(groupID) - items = sMkt.getItemsByGroup(grp) - for item in items: - if self.countFitsWithShip(item.ID) > 0: - return True - return False - def getModule(self, fitID, pos): fit = eos.db.getFit(fitID) return fit.modules[pos] diff --git a/service/market.py b/service/market.py index 7d1cdea19..1a2022854 100644 --- a/service/market.py +++ b/service/market.py @@ -543,6 +543,7 @@ class Market(): def getGroupsByCategory(self, cat): """Get groups from given category""" groups = set(filter(lambda grp: self.getPublicityByGroup(grp), cat.groups)) + return groups def getMarketGroupChildren(self, mg):