Exemplo n.º 1
0
    def new_stations(self, name, station_keys):
        if len(station_keys) == 0:
            return 0

        # assume all stations are unknown
        unknown_keys = set(station_keys)

        if name == 'wifi':
            # there is only one combined table structure
            shards = defaultdict(list)
            for mac in unknown_keys:
                shards[WifiShard.shard_model(mac)].append(mac)
            for shard, macs in shards.items():
                query = (self.session.query(shard.mac)
                                     .filter(shard.mac.in_(macs)))
                unknown_keys -= set([r.mac for r in query.all()])
        elif name == 'cell':
            # first check the station table, which is more likely to contain
            # stations
            station_iter = Cell.iterkeys(
                self.session,
                list(unknown_keys),
                # only load the columns required for the hashkey
                extra=lambda query: query.options(
                    load_only(*tuple(Cell._hashkey_cls._fields))))
            # subtract all stations which are found in the station table
            unknown_keys -= set([sta.hashkey() for sta in station_iter])
            if len(unknown_keys) == 0:  # pragma: no cover
                return 0

            # Only check the blocklist table for the still unknown keys.
            # There is no need to check for the already found keys again.
            block_iter = CellBlocklist.iterkeys(
                self.session,
                list(unknown_keys),
                # only load the columns required for the hashkey
                extra=lambda query: query.options(
                    load_only(*tuple(CellBlocklist._hashkey_cls._fields))))
            # subtract all stations which are found in the blocklist table
            unknown_keys -= set([block.hashkey() for block in block_iter])

        return len(unknown_keys)
Exemplo n.º 2
0
 def blocklisted_stations(self, station_keys):
     blocklist = {}
     for block in CellBlocklist.iterkeys(
             self.session, list(station_keys)):
         blocklist[block.hashkey()] = self.blocklisted_station(block)
     return blocklist