def _usagecapacity(service): """calculate the current usage of the service.""" usage_xpu = Capacity() capacity_xpus = Capacity() busy = 0 detail = {} for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity_xpus += r_capacity reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved count_map_gpu = Counter() count_map_cpu = Counter() task_type = {} count_used_xpus = Capacity() r_usage_gpu = redis.hgetall("gpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_gpu: if t not in task_type: task_type[t] = redis.hget("task:%s" % t, "type") count_map_gpu[t] += 1 count_used_xpus.incr_ngpus(1) r_usage_cpu = redis.hgetall("cpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_cpu: if t not in task_type: task_type[t] = redis.hget("task:%s" % t, "type") count_map_cpu[t] += 1 count_used_xpus.incr_ncpus(1) detail[resource]['usage'] = [ "%s %s: %d (%d)" % (task_type[t], t, count_map_gpu[t], count_map_cpu[t]) for t in task_type ] detail[resource][ 'avail_gpus'] = r_capacity.ngpus - count_used_xpus.ngpus detail[resource][ 'avail_cpus'] = r_capacity.ncpus - count_used_xpus.ncpus err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 usage_xpu += count_used_xpus queued = redis.llen("queued:" + service.name) return ("%d (%d)" % (usage_xpu.ngpus, usage_xpu.ncpus), queued, "%d (%d)" % (capacity_xpus.ngpus, capacity_xpus.ncpus), busy, detail)
def _usagecapacity(service): """calculate the current usage of the service.""" usage = 0 capacity = 0 busy = 0 detail = {} for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity += r_capacity reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved r_usage = redis.hgetall("resource:%s:%s" % (service.name, resource)).values() count_usage = {} for r in r_usage: if r in count_usage: count_usage[r] += 1 else: count_usage[r] = 1 detail[resource]['usage'] = [ "%s: %d" % (k, count_usage[k]) for k in count_usage ] usage += len(r_usage) err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 queued = redis.llen("queued:" + service.name) return usage, queued, capacity, busy, detail
def _set_user_info(self): """ Fetch and set current user infos based on the informations stored in the database. """ sha = sha1(self.email).hexdigest() user_info = redis.hgetall("sl:account:{}".format(sha)) if (type(user_info) != dict or user_info.get("password") != self.password): user_info = {} try: self.plan = Plan.from_id(user_info.get("plan")) except SleekException: self.plan = None self.customer_token = str_to_none( user_info.get("customer_token") ) self.subscription_token = str_to_none( user_info.get("subscription_token") ) self.subscription_end = str_to_none( user_info.get("subscription_end") )
def get_previews(threadid): redis_previews = redis.hgetall('dlg_%s' % threadid) for key in redis_previews: prw = redis_previews.get(key) redis_previews[key] = eval(prw) return redis_previews
def confirmed(openid): """ 确认已收到id为'media_id'的信息推送 """ user = WechatUser.query.filter_by(openid=openid).first() user_id = user.id last_push_time = redis.get("wechat:last_push_time") # last_push_time = redis.get("wechat:last_pushtext_time") # timeout = int(last_push_time.decode()) - int(get_user_last_interact_time(openid)) # current_app.logger.warning('超时%s' % timeout) last_push_cache = redis.hgetall("wechat:last_push") media_id = last_push_cache[b'media_id'].decode() pushtype = last_push_cache[b'pushtype'].decode() if pushtype == 'text': pushtext = Pushtext.query.filter_by(media_id=media_id).first() to_confirmed_before = pushtext.to_confirmed content = pushtext.content # 这里我是把list转为json存到数据库,所以去出来的时候要转回来 to_confirmed = json.loads(to_confirmed_before) try: if user_id in to_confirmed: to_confirmed.remove(user_id) pushtext.to_confirmed = json.dumps(to_confirmed) pushtext.update() content = "你已确认获悉此条及在其之前发布的通知:\n【%s】"% content return content else: content = "【你已回复'收到', 无需重复回复!】" return content except Exception as e: current_app.logger.warning('用户重复回复了收到,错误信息:%s') % e return '出错, 请稍后在试试' elif pushtype == 'news': pushnews=Pushnews.query.filter_by(media_id=media_id).first() to_confirmed_before = pushnews.to_confirmed title=pushnews.title to_confirmed = json.loads(to_confirmed_before) current_app.logger.warning('to_confirmed_应该是个list %s' % to_confirmed) try: if user_id in to_confirmed: to_confirmed.remove(user_id) pushnews.to_confirmed = json.dumps(to_confirmed) pushnews.update() content = "你已确认获悉此条及在其之前发布的通知:\n【%s】" % title return content else: content = "【你已回复'收到', 无需重复回复!】" return content except Exception as e: current_app.logger.warning('用户重复回复了收到,错误信息:%s') % e cntent="出错, 请稍后在试试" return content
def get(cls, id): """ get hash object by id """ key = rkey(cls, id) obj = redis.hgetall(key) if obj: obj['id'] = str(id) return AttrDict(obj) return None
def logs(id): log = redis.get('%s_l' % id) if log is None: return "Nothing found" info = redis.hgetall(id) logs = log.split('\n') return render_template('logs.html', logs=logs, info=info, log_id=id)
def get_dict_from_list(name, count=-1): items = redis.lrange(name, 0, count) ret = [] for id in items: item = redis.hgetall(id) item["job_id"] = id ret.append(item) return ret
def tag_index(tag_str): posts = [] post_ids = redis.smembers("post:post-tags:%s" % tag_str) post_ids = [int(post_id) for post_id in post_ids] post_ids.sort() post_ids.reverse() for post_id in post_ids: post = redis.hgetall('post:%s' % post_id) posts.append(post) return render_template('blog/index.html', posts=posts)
def _usagecapacity(service): """calculate the current usage of the service.""" usage_gpu = 0 usage_cpu = 0 capacity_gpus = 0 capacity_cpus = 0 busy = 0 detail = {} servers = service.list_servers() for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity_gpus += r_capacity detail[resource]['ncpus'] = servers[resource]['ncpus'] capacity_cpus += servers[resource]['ncpus'] reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved count_map_gpu = Counter() task_type = {} count_map_cpu = {} count_used_gpus = 0 count_used_cpus = 0 r_usage_gpu = redis.hgetall("gpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_gpu: task_type[t] = redis.hget("task:%s" % t, "type") count_map_gpu[t] += 1 count_used_gpus += 1 if t not in count_map_cpu: count_map_cpu[t] = int(redis.hget("task:%s" % t, "ncpus")) count_used_cpus += count_map_cpu[t] r_usage_cpu = redis.lrange("cpu_resource:%s:%s" % (service.name, resource), 0, -1) for t in r_usage_cpu: task_type[t] = redis.hget("task:%s" % t, "type") if t not in count_map_cpu: count_map_cpu[t] = int(redis.hget("task:%s" % t, "ncpus")) count_map_gpu[t] = 0 count_used_cpus += count_map_cpu[t] detail[resource]['usage'] = ["%s %s: %d (%d)" % (task_type[k], k, count_map_gpu[k], count_map_cpu[k]) for k in count_map_gpu] detail[resource]['avail_cpus'] = int(redis.get("ncpus:%s:%s" % (service.name, resource))) detail[resource]['avail_gpus'] = r_capacity-count_used_gpus err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 usage_cpu += count_used_cpus usage_gpu += count_used_gpus queued = redis.llen("queued:"+service.name) return ("%d (%d)" % (usage_gpu, usage_cpu), queued, "%d (%d)" % (capacity_gpus, capacity_cpus), busy, detail)
def delete(alert_sha): """ Delete the given alert. """ alert = redis.hgetall("sl:alert:{}".format(alert_sha)) sha = sha1(alert["email"]).hexdigest() removed = bool( alert and redis.srem("sl:alert:ids", alert_sha) and redis.srem("sl:account:{}:alerts".format(sha), alert_sha) ) if not removed: raise SleekException("Could not delete alert.")
def valid_auth(email, password): """ Return True if user exists and the credentials are right. Return False otherwise. """ if not email or not password: return False sha = sha1(email).hexdigest() user_info = redis.hgetall("sl:account:{}".format(sha)) return bool( type(user_info) == dict and user_info.get("password") == password )
def received_msg(msg_event): sender_id = msg_event['sender']['id'] recipient_id = msg_event['recipient']['id'] timestamp = msg_event['timestamp'] message = msg_event['message'] app.logger.info('received message from user {}: {}'.format(sender_id, message)) message_id = message['mid'] message_text = message.get('text') message_attachments = message.get('attachments') session = redis.hgetall(sender_id) if session: continue_session(sender_id, session, message_text) else: start_session(sender_id, message_text)
def test_save(self): """ Alert.save should add the `alert_id` to 'alert:ids' and 'account:{account_id}:alerts'. It should also save the attributes in 'alert:{alert_id}' as a dict. """ self.alert.save() assert redis.sismember("sl:alert:ids", self.alert.sha) is True assert redis.sismember( "sl:account:{}:alerts".format(self.sha), self.alert.sha ) is True assert redis.hgetall( "sl:alert:{}".format(self.alert.sha) ) == self.alert.to_dict()
def get_room_members(): """Get user_id list of the pointed room Args: room_id: the room that pointed Returns: room_id: the room that pointed user_list: the list of user_id """ room_id = request.values['room_id'] uid_list = list(redis.smembers(room_id)) user_list = [] for uid in uid_list: user_list.append(redis.hgetall(uid)) return jsonify(api_format(status.HTTP_200_OK, "ok", {'room_id': room_id, 'user_list': user_list}))
def set_user_realname_and_classname(openid, realname, classname): """ 查询成绩(绑定)的时候可以写入用户的真实姓名和班级 """ redis_prefix = "wechat:user:"******"realname": realname, "classname": classname })
def get_room_members(): """Get user_id list of the pointed room Args: room_id: the room that pointed Returns: room_id: the room that pointed user_list: the list of user_id """ room_id = request.values['room_id'] uid_list = list(redis.smembers(room_id)) user_list = [] for uid in uid_list: user_list.append(redis.hgetall(uid)) return jsonify( api_format(status.HTTP_200_OK, "ok", { 'room_id': room_id, 'user_list': user_list }))
def get_user_library_info(openid): """ 获取绑定的图书馆帐号 """ redis_prefix = "wechat:user:"******"libraryid": auth_info.libraryid, "librarypwd": auth_info.librarypwd, }) user_info_cache['libraryid'] = auth_info.libraryid user_info_cache['librarypwd'] = auth_info.librarypwd return user_info_cache else: return False
def get_user_info(openid): """ 获取绑定的教务系统帐号 """ redis_prefix = "wechat:user:"******"studentid": auth_info.studentid, "studentpwd": auth_info.studentpwd, }) user_info_cache['studentid'] = auth_info.studentid user_info_cache['studentpwd'] = auth_info.studentpwd return user_info_cache else: return False
def get(self, id): """ Get a session by id from redis, populate data members or return None if not found """ sessionSettings = redis.hgetall(f"session::{id}") if len(sessionSettings) == 0: return None tmp = {"id": id} for k, v in sessionSettings.items(): tmp[k.decode('utf-8')] = v.decode('utf-8') emailList = redis.smembers(f"session::{id}::emails") tmp['emails'] = {email.decode('utf-8') for email in emailList} ipAddressList = redis.smembers(f"session::{id}::ip_addresses") tmp['ip_addresses'] = {ip.decode('utf-8') for ip in ipAddressList} postList = redis.lrange(f"session::{id}::posts", 0, -1) tmp['posts'] = [post.decode('utf-8') for post in postList] self.fromDict(tmp) return self
def check(guid): if not is_valid_guid(guid): abort(400) # request ongoing? status = redis.get(redis_key('%s.%s' % (guid, 'status'))) if status is None: abort(500) # this should not happen, unless load average is too high # error occured? if status == 'error': return jsonify(status='error') elif status == 'processing': percent = redis.get(redis_key('%s.%s' % (guid, 'percent'))) return jsonify(status='processing', percent=percent) elif status == 'done': results = {} for key in redis.keys('%s.*' % (redis_key(guid),)): if key not in ['%s.status' % (redis_key(guid),), '%s.percent' % (redis_key(guid),)]: results[key] = redis.hgetall(key) return jsonify(status='done', results=render_template('table.html', results=results)) abort(500) # is this even possible?
def school_report_card(openid=None): """ 学生成绩单 """ if is_user_exists(openid): jsapi = get_jsapi_signature_data(request.url) jsapi['jsApiList'] = [ 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] score_cache = redis.hgetall('wechat:user:scoreforweb:' + openid) if score_cache: score_info = ast.literal_eval(score_cache[b'score_info'].decode()) real_name = score_cache[b'real_name'].decode('utf-8') return render_template( 'wechat/score.html', real_name=real_name, school_term=score_cache[b'school_term'].decode('utf-8'), score_info=score_info, update_time=score_cache[b'update_time'].decode('utf-8'), baidu_analyics=current_app.config['BAIDU_ANALYTICS'], jsapi=Markup(jsapi)) else: abort(404) else: abort(404)
def index(): posts = [] for post_id in redis.lrange('post:post-list', 0, -1): post = redis.hgetall('post:%s' % post_id) posts.append(post) return render_template('blog/index.html', posts=posts)
def get_alert(alert_id): """ Fetch a given alert informations from database. """ return redis.hgetall("sl:alert:{}".format(alert_id))
def is_cache_available_for_ten_mins(): rate_ten_mins = redis.hgetall('rate_ten_mins') if rate_ten_mins: return rate_ten_mins return False
def add_order(): orders = request.get_json() shopToStocks = Neo.get_all(orders['id_shop']) new_order = [] shopToStocks.sort(key=lambda x: x[1]) for goods in orders['goods']: redisdata = redis.hgetall(goods) if redisdata == {}: update_redis() all_stocks = {k: int(v) for k, v in redisdata.items()} for i in range(len(shopToStocks)): id_stock = shopToStocks[i][0] if id_stock in all_stocks.values(): try: cur_order = next(item for item in new_order if item['id_stock'] == id_stock) except StopIteration: cur_order = {} cur_quantity = Postgres.return_quantity(id_stock, goods) new_quantity = cur_quantity[0] - orders['goods'][goods] if new_quantity >= 0: if cur_order == {}: cur_order.update({'id_shop': orders['id_shop']}) cur_order.update({'id_stock': id_stock}) cur_order.update({'goods': {}}) cur_order['goods'].update( {goods: orders['goods'][goods]}) new_order.append(cur_order) else: new_order.remove(cur_order) cur_order['goods'].update( {goods: orders['goods'][goods]}) new_order.append(cur_order) if new_quantity == 0: Postgres.delete_state_by_id(id_stock, goods) redis.delete(goods) address_id = Postgres.return_address_and_id(goods) redis.hmset(goods, address_id) else: Postgres.update_quantity(goods, id_stock, new_quantity) break else: continue for order in new_order: id_order = Mongo.insert('orders', order, returned=True) body = { "date": f"{datetime.now():%Y-%m-%d}", "id_order": str(id_order) } es.index(index='orders', body=body) return '.'
def redis_get(): requestTitle = request.get_json() k = requestTitle['title'] return str(redis.hgetall(k))
def detail(post_id): post = redis.hgetall('post:%s' % post_id) return render_template('blog/detail.html', post=post)
def start_session(sender_id, message_text): redis.hmset(sender_id, {'started_at': int(time.time())}) update_session(sender_id, next_filter=0) return continue_session(sender_id, redis.hgetall(sender_id), message_text)
def initialize(self): ''' 初始化参数 ''' token = self.request.headers.get("token") self.token_info = redis.hgetall(f'token:{token}')
def from_sha(cls, sha): """ Return an Alert from a sha. """ return cls(**redis.hgetall("sl:alert:{}".format(sha)))
def get_all_user_id(): ids = [] for i in redis.hgetall(user_key()): ids.append(str(i, encoding='utf-8')) return ids
def get_unread_pms(s_user): return redis.hgetall('pm_%s' % s_user)