def getIpAddresses(self, limit=0, start=0, sort='ipAddressAsInt', dir='DESC', params=None, uid=None, criteria=()): infos = [] cat = ICatalogTool(self._getObject(uid)) reverse = dir=='DESC' brains = cat.search("Products.ZenModel.IpAddress.IpAddress", start=start, limit=limit, orderby=sort, reverse=reverse) for brain in brains: infos.append(IInfo(unbrain(brain))) devuuids = set(info.device.uuid for info in infos if info.device) # get ping severities zep = getFacade('zep') pingSeverities = zep.getEventSeverities(devuuids, severities=(), status=(), eventClass=Status_Ping) self._assignPingStatuses(infos, pingSeverities) # get snmp severities snmpSeverities = zep.getEventSeverities(devuuids, severities=(), status=(), eventClass=Status_Snmp) self._assignSnmpStatuses(infos, snmpSeverities) return SearchResults(infos, brains.total, brains.hash_)
def getList(self, limit=None, start=None, sort='name', dir='DESC', params=None, uid=None, criteria=()): searchResults = self._serviceSearch(limit, start, sort, dir, params, uid, criteria) if searchResults.areBrains: serviceClasses = [unbrain(brain) for brain in searchResults] else: serviceClasses = searchResults # the info decorator changes the returned serviceClasses to serviceInfos return {'serviceInfos': serviceClasses, 'total': searchResults.total, 'hash': searchResults.hash_, }
def _sortQueryResults(self, queryResults, orderby, reverse): # save the values during sorting in case getting the value is slow savedValues = {} def getValue(obj): key = obj.getPrimaryPath() if key in savedValues: value = savedValues[key] else: value = getattr(IInfo(obj), orderby) if callable(value): value = value() # if an info object is returned then sort by the name if IInfo.providedBy(value): value = value.name.lower() savedValues[key] = value return value return sorted((unbrain(brain) for brain in queryResults), key=getValue, reverse=reverse, cmp=natural_compare)
def _sortQueryResults(self, queryResults, orderby, reverse): # save the values during sorting in case getting the value is slow savedValues = {} def getValue(obj): key = obj.getPrimaryPath() if key in savedValues: value = savedValues[key] else: value = getattr(IInfo(obj), orderby) if callable(value): value = value() # if an info object is returned then sort by the name if IInfo.providedBy(value): value = value.name.lower() savedValues[key] = value return value return sorted((unbrain(brain) for brain in queryResults), key=getValue, reverse=reverse)
def test_deviceSearchAndSortByProdState(self): devMaintenance = self.dmd.Devices.createInstance('devMaintenance') devMaintenance.setPerformanceMonitor('localhost') devMaintenance.setProdState(400) devProduction = self.dmd.Devices.createInstance('devProduction') devProduction.setPerformanceMonitor('localhost') devProduction.setProdState(1000) # Search by prod state results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", params=dict(productionState=[400])) self.assertEquals(1, results.total) self.assertEquals(iter(results).next().getObject().getProductionState(), 400) results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", params=dict(productionState=[1000])) self.assertEquals(1, results.total) self.assertEquals(iter(results).next().getObject().getProductionState(), 1000) # Sort by prod state results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState') resultIter = iter(results) self.assertEquals(2, results.total) self.assertEquals(resultIter.next().getObject().getProductionState(), 400) self.assertEquals(resultIter.next().getObject().getProductionState(), 1000) # Sort by prod state, descending order results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState', dir='DESC') resultIter = iter(results) self.assertEquals(2, results.total) self.assertEquals(resultIter.next().getObject().getProductionState(), 1000) self.assertEquals(resultIter.next().getObject().getProductionState(), 400) # This test specifically verifies the fix for ZEN-26901 sorting by a non-indexed # field while filtering on productionState caused a ProdStateNotSetError. # sort by status (non-indexed) with productionState filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='status', params=dict(productionState=[400, 1000])) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getProductionState(), 400) self.assertEquals(device.getPerformanceServer().id, 'localhost') device = resultIter.next() self.assertEquals(device.getProductionState(), 1000) self.assertEquals(device.getPerformanceServer().id, 'localhost') # sort by name (indexed) with productionState filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='name', params=dict(productionState=[400, 1000])) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 400) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 1000) # This test specifically verifies the fix for ZEN-26901 sorting productionState # while filtering on an indexed field caused a ProdStateNotSetError. # sort by productionState with name (indexed) filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort="productionState", params=dict(name="dev")) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 400) self.assertEquals(device.getObject().getDeviceName(), 'devMaintenance') device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 1000) self.assertEquals(device.getObject().getDeviceName(), 'devProduction') # sort by productionState with collector (non-indexed) filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState', params=dict(collector="local")) resultIter = iter(results) self.assertEquals(2, results.total) device = unbrain(resultIter.next()) self.assertEquals(device.getProductionState(), 400) self.assertEquals(device.getPerformanceServer().id, 'localhost') device = unbrain(resultIter.next()) self.assertEquals(device.getProductionState(), 1000) self.assertEquals(device.getPerformanceServer().id, 'localhost')
def _componentSearch(self, uid=None, types=(), meta_type=(), start=0, limit=None, sort='name', dir='ASC', name=None, keys=()): reverse = dir == 'DESC' if isinstance(meta_type, basestring) and get_component_field_spec( meta_type) is not None: brains, total = self._typecatComponentBrains( uid, types, meta_type, start, limit, sort, dir, name, keys) if brains is not None: return self._typecatComponentPostProcess( brains, total, sort, reverse) if isinstance(meta_type, basestring): meta_type = (meta_type, ) if isinstance(types, basestring): types = (types, ) querySet = [] if meta_type: querySet.append(Or(*(Eq('meta_type', t) for t in meta_type))) querySet.append(Generic('getAllPaths', uid)) query = And(*querySet) obj = self._getObject(uid) cat = obj.device().componentSearch if 'getAllPaths' not in cat.indexes(): obj.device()._createComponentSearchPathIndex() brains = cat.evalAdvancedQuery(query) # unbrain the results comps = [] for brain in brains: try: comps.append(IInfo(unbrain(brain))) except: log.warn( 'There is broken component "{}" in componentSearch catalog on {} device.' .format(brain.id, obj.device().id)) # filter the components if name is not None: comps = self._filterComponents(comps, keys, name) total = len(comps) hash_ = str(total) def componentSortKey(parent): val = getattr(parent, sort) if val: if isinstance(val, list): val = val[0] if callable(val): val = val() if IInfo.providedBy(val): val = val.name return pad_numeric_values_for_indexing(val) # sort the components sortedResults = list( sorted(comps, key=componentSortKey, reverse=reverse)) # limit the search results to the specified range if limit is None: pagedResult = sortedResults[start:] else: pagedResult = sortedResults[start:start + limit] # fetch any rrd data necessary self.bulkLoadMetricData(pagedResult) # Do one big lookup of component events and add to the result objects showSeverityIcon = self.context.dmd.UserInterfaceSettings.getInterfaceSettings( ).get('showEventSeverityIcons') if showSeverityIcon: uuids = [r.uuid for r in pagedResult] zep = getFacade('zep') severities = zep.getWorstSeverity(uuids) for r in pagedResult: r.setWorstEventSeverity(severities[r.uuid]) return SearchResults(iter(pagedResult), total, hash_, False)
def test_deviceSearchAndSortByProdState(self): devMaintenance = self.dmd.Devices.createInstance('devMaintenance') devMaintenance.setPerformanceMonitor('localhost') devMaintenance.setProdState(400) devProduction = self.dmd.Devices.createInstance('devProduction') devProduction.setPerformanceMonitor('localhost') devProduction.setProdState(1000) # Search by prod state results = self.facade.getDeviceBrains( uid="/zport/dmd/Devices", params=dict(productionState=[400])) self.assertEquals(1, results.total) self.assertEquals( iter(results).next().getObject().getProductionState(), 400) results = self.facade.getDeviceBrains( uid="/zport/dmd/Devices", params=dict(productionState=[1000])) self.assertEquals(1, results.total) self.assertEquals( iter(results).next().getObject().getProductionState(), 1000) # Sort by prod state results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState') resultIter = iter(results) self.assertEquals(2, results.total) self.assertEquals(resultIter.next().getObject().getProductionState(), 400) self.assertEquals(resultIter.next().getObject().getProductionState(), 1000) # Sort by prod state, descending order results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState', dir='DESC') resultIter = iter(results) self.assertEquals(2, results.total) self.assertEquals(resultIter.next().getObject().getProductionState(), 1000) self.assertEquals(resultIter.next().getObject().getProductionState(), 400) # This test specifically verifies the fix for ZEN-26901 sorting by a non-indexed # field while filtering on productionState caused a ProdStateNotSetError. # sort by status (non-indexed) with productionState filter results = self.facade.getDeviceBrains( uid="/zport/dmd/Devices", sort='status', params=dict(productionState=[400, 1000])) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getProductionState(), 400) self.assertEquals(device.getPerformanceServer().id, 'localhost') device = resultIter.next() self.assertEquals(device.getProductionState(), 1000) self.assertEquals(device.getPerformanceServer().id, 'localhost') # sort by name (indexed) with productionState filter results = self.facade.getDeviceBrains( uid="/zport/dmd/Devices", sort='name', params=dict(productionState=[400, 1000])) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 400) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 1000) # This test specifically verifies the fix for ZEN-26901 sorting productionState # while filtering on an indexed field caused a ProdStateNotSetError. # sort by productionState with name (indexed) filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort="productionState", params=dict(name="dev")) resultIter = iter(results) self.assertEquals(2, results.total) device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 400) self.assertEquals(device.getObject().getDeviceName(), 'devMaintenance') device = resultIter.next() self.assertEquals(device.getObject().getProductionState(), 1000) self.assertEquals(device.getObject().getDeviceName(), 'devProduction') # sort by productionState with collector (non-indexed) filter results = self.facade.getDeviceBrains(uid="/zport/dmd/Devices", sort='productionState', params=dict(collector="local")) resultIter = iter(results) self.assertEquals(2, results.total) device = unbrain(resultIter.next()) self.assertEquals(device.getProductionState(), 400) self.assertEquals(device.getPerformanceServer().id, 'localhost') device = unbrain(resultIter.next()) self.assertEquals(device.getProductionState(), 1000) self.assertEquals(device.getPerformanceServer().id, 'localhost')