def command_stats(self, ctx, limit: int=10):
"""Show most used commands."""
if limit > 20 or limit < 1:
await ctx.send('no')
return
cur = self.cstats_coll.find().sort('uses',
direction=pymongo.DESCENDING)\
.limit(limit)
res = []
async for single in cur:
if single.get('t'):
continue
name, uses = single['name'], single['uses']
res.append(f'{name}: used {uses} times')
_res = '\n'.join(res)
await ctx.send(f'```\n{_res}\n```')
python类DESCENDING的实例源码
def load_current_state(self):
self.known = {}
blocks = store.config_blocks.find({'botnet': self.label})
# First, we have to get each config block 'type' known
for type in blocks.distinct('type'):
self.known[type] = {}
# This could probably be done in just one (complex) query
targets = store.config_blocks.find({
'botnet': self.label,
'type': type
}).distinct('target')
for target in targets:
last_known = store.config_blocks.find({
'botnet': self.label,
'type': type,
'target': target,
}).sort('updated', DESCENDING).limit(1)[0]
if last_known['action'] != ACTION_REMOVED:
self.known[type][target] = ConfigBlock(last_known)
def post():
title = "?????"
if request.method == 'POST':
co = conect_db()
name = request.form['name']
name = re.compile('.*{0}.*'.format(name))
result = []
if not(request.form['name']):
return render_template('index.html')
for i,data in enumerate(co.find({'$or':[{'name':name},{'area':name},{'aliases.name':name},{'tags.value':name}]}, sort = [('rating.count',pymongo.DESCENDING),('rating.value',pymongo.DESCENDING)])):
if i <100:
result.append(data)
else:
i = 99
break
return render_template('index.html', result=result, title=title, name=request.form['name'],num=i+1)
else:
return redirect(url_for('index'))
def order_by_recorded_msg_count(self, limit=None):
"""Sort by COUNT OF RECEIVED MESSAGES"""
RECEIVED_MESSAGES = 'rcv_sum'
aggr_pipeline = [
{ '$addFields': { RECEIVED_MESSAGES: { '$sum': [ '$' + group_data.MESSAGE_RECORDS + '.' + msg_stats_data.RECEIVE + '.' + str(type_enum) + '.' + k for k in (msg_stats_pair.TRIGGERED, msg_stats_pair.NOT_TRIGGERED) for type_enum in list(msg_type) ] } } },
{ '$sort': { RECEIVED_MESSAGES: pymongo.DESCENDING } }
]
if limit is not None and isinstance(limit, (int, long)):
aggr_pipeline.append({ '$limit': limit })
aggr_result = list(self.aggregate(aggr_pipeline))
if len(aggr_result) > 0:
return [group_data(data) for data in aggr_result]
else:
return []
# private
def find_one_and_update(self, filter, update, projection=None, sort=None, upsert=False, return_document=pymongo.ReturnDocument.BEFORE, **kwargs):
if self._available_range == group_dict_manager_range.GLOBAL:
pass
elif self._available_range == group_dict_manager_range.GROUP_AND_PUBLIC:
or_list = [{ pair_data.AFFILIATED_GROUP: self._group_id }, { pair_data.AFFILIATED_GROUP: PUBLIC_GROUP_ID }]
if '$or' in filter:
filter['$and'] = [{'$or': or_list}, {'$or': filter['$or']}]
del filter['$or']
else:
filter['$or'] = or_list
elif self._available_range == group_dict_manager_range.GROUP_ONLY:
filter[pair_data.AFFILIATED_GROUP] = self._group_id
else:
raise UnknownRangeError()
sort_tuple = (pair_data.AFFILIATED_GROUP, pymongo.DESCENDING)
if sort is None:
sort = [sort_tuple]
else:
sort.append(sort_tuple)
return super(group_dict_manager, self).find_one_and_update(filter, update, projection, sort, upsert, return_document, **kwargs)
def get_reply_data(self, keyword, kw_type=word_type.TEXT):
"""Return none if nothing found, else return result in pair_data class"""
data_result = self.find_one({
pair_data.KEYWORD: keyword,
pair_data.PROPERTIES + '.' + pair_data.DISABLED: False,
pair_data.PROPERTIES + '.' + pair_data.KEYWORD_TYPE: int(kw_type)
}, sort=[(pair_data.PROPERTIES + '.' + pair_data.PINNED, pymongo.DESCENDING),
(pair_data.SEQUENCE, pymongo.DESCENDING)])
if data_result is not None:
data_result = pair_data(data_result)
if data_result.last_call is None or data_result.last_call < datetime.now() - timedelta(seconds=self._repeat_call_cd_secs):
self.update({ pair_data.SEQUENCE: data_result.seq_id }, {
'$inc': { pair_data.STATISTICS + '.' + pair_data.CALLED_COUNT: 1 },
'$set': { pair_data.STATISTICS + '.' + pair_data.LAST_CALL: datetime.now() }
})
return pair_data(data_result)
def query(self, q_kw=None, fields=None, sort_by=None, use_iterators=True, *args, **kwargs):
""" Find a set of document with condition
Sometimes because of memory, cause `MemoryError` exception
"""
if not isinstance(sort_by, (tuple, types.NoneType)):
raise TypeError('meth: query, `sort_by` keyword type error')
if not isinstance(fields, dict):
raise TypeError('meth: query, `fields` keyword type error')
self.__connect()
skip = kwargs.pop('skip', 0)
limit = kwargs.pop('limit', 0) # 0 hint to get overall document
args = (q_kw, ) + args
kwargs['projection'] = fields
sort_by = sort_by or [('_id', pymongo.DESCENDING)]
cursor = self.__collection.find(*args, **kwargs).sort(sort_by)
if use_iterators:
return cursor.skip(skip).limit(limit)
return [doc for doc in cursor]
def bulk_insert_cves(self, cve_list):
products = []
for product in cve_list:
splitted_product = product.split("#")
data = {}
data['cve_id'] = splitted_product[0]
data['vendor'] = splitted_product[1]
data['product'] = splitted_product[2]
data['version'] = splitted_product[3]
data['year'] = int(splitted_product[4])
products.append(data)
# Bulk insert
self.db.cve.create_index([('product', pymongo.DESCENDING)])
self.db.cve.insert_many(products)
# Bulk insert the cve info dict format
def get_api_exceptions(result_limit=50, sort_direction=pymongo.DESCENDING):
"""
Retrieve api exceptions.
Args:
result_limit: the maximum number of exceptions to return.
sort_direction: pymongo.ASCENDING or pymongo.DESCENDING
"""
db = api.common.get_conn()
results = db.exceptions.find({"visible": True}).sort([("time", sort_direction)]).limit(result_limit)
return list(results)
def insert_proxy(self, table_name, proxy):
data = proxy.get_dict()
data['save_time'] = str(time.time())
data['create_time'] = str(datetime.datetime.now())
self.db[table_name].create_index([("ip", pymongo.DESCENDING)], unique=True)
try:
self.db[table_name].insert(data)
except BaseException,e:
if "E11000 duplicate key error collection" in e.message:
utils.log(str(e.message.split(' ')[12])+"????????")
def get_oplog_tail_ts(self):
logging.debug("Gathering oldest 'ts' in %s oplog" % self.uri)
return self.get_oplog_rs().find_one(sort=[('$natural', DESCENDING)])['ts']
def get_api_exceptions(result_limit=50, sort_direction=pymongo.DESCENDING):
"""
Retrieve api exceptions.
Args:
result_limit: the maximum number of exceptions to return.
sort_direction: pymongo.ASCENDING or pymongo.DESCENDING
"""
db = api.common.get_conn()
results = db.exceptions.find({"visible": True}).sort([("time", sort_direction)]).limit(result_limit)
return list(results)
def last_updated(self):
doc = next(self.query(properties=[self.lu_field]).sort(
[(self.lu_field, pymongo.DESCENDING)]).limit(1), None)
# Handle when collection has docs but `NoneType` lu_field.
return (self.lu_func[0](doc[self.lu_field]) if (doc and doc[self.lu_field])
else datetime.min)
def get_all(self):
return self.user_meta.find().sort([('version', pymongo.DESCENDING)])
def get_all(self):
return self.rating_meta.find().sort([('version', pymongo.DESCENDING)])
def get_all(self):
return self.item_meta.find().sort([('version', pymongo.DESCENDING)])
def search_one(self, query, split_regexp, key):
query = re.split(split_regexp, query)
#print("Query: ", query)
if len(query)>7:
return None
dbs = []
if "cpp" in query:
dbs = [db_cpp]
query.remove("cpp")
elif "c++" in query:
dbs = [db_cpp]
query.remove("c++")
elif "python" in query:
dbs = [db_python3]
query.remove("python")
elif "python3" in query:
dbs = [db_python3]
query.remove("python3")
else:
dbs = [db_cpp, db_python3]
#print("Query: '", query, "'")
query = filter(lambda x: x, query)
if key == "name":
query = sorted(query)
query = " ".join(query)
#print("Query: '", query, "'")
#print("dbs: ", str(dbs))
found = False
for db in dbs:
#print("db: ", str(db))
cursor = db.index.find({key : query}, sort=[("relevance", pymongo.DESCENDING)])
for doc in cursor:
found = True
need_continue = self.found_reference(db, doc)
if not need_continue:
break
return found
def get_all_team_scores():
"""
Gets the score for every team in the database.
Returns:
A list of dictionaries with name and score
"""
teams = api.team.get_all_teams()
db = api.api.common.get_conn()
result = []
for team in teams:
team_query = db.submissions.find({'tid': team['tid'], 'eligible': True, 'correct': True})
if team_query.count() > 0:
lastsubmit = team_query.sort('timestamp', direction=pymongo.DESCENDING)[0]['timestamp']
else:
lastsubmit = datetime.datetime.now()
score = get_score(tid=team['tid'])
if score > 0:
result.append({
"name": team['team_name'],
"tid": team['tid'],
"school": team["school"],
"score": score,
"lastsubmit": lastsubmit
})
time_ordered = sorted(result, key=lambda entry: entry['lastsubmit'])
time_ordered_time_removed = [{'name': x['name'], 'tid': x['tid'], 'school': x['school'], 'score': x['score']} for x in time_ordered]
return sorted(time_ordered_time_removed, key=lambda entry: entry['score'], reverse=True)
def ensure_indexes(self):
#Ensure index existence
db = self.client.bitcoin
collection = db.transactions
collection.create_index([("source_n_id", ASCENDING)])
collection.create_index([("destination_n_id", ASCENDING)])
collection.create_index([("source", ASCENDING)])
collection.create_index([("destination", ASCENDING)])
collection.create_index([("block_id",DESCENDING)])
def last_notifications(self, last_n=5):
return self.collection.find().sort(
'offset', pymongo.DESCENDING)[:last_n]