def me(bot, update): msg_id = update.message.message_id chat_id = update.message.chat_id user_id = update.message.from_user.id username = update.message.from_user.username fullname = " ".join([ update.message.from_user.first_name, update.message.from_user.last_name ]) chat_type = update.message.chat.type if chat_type == 'private': info = Stats.get_user(user_id) token = User.generate_token(user_id) msg = Stats.me_private_format(user_id, info['groups'], info['msg_count'], token) cache.set('user_token_{}'.format(user_id), token, 600) bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN) if chat_type == 'group' or chat_type == 'supergroup': info = Stats.get_user(user_id, chat_id) msg = Stats.me_format(user_id, fullname, username, info['group_msg_count'], info['percent'], info['msg_count']) bot.sendMessage(chat_id, msg, reply_to_message_id=msg_id, parse_mode=ParseMode.MARKDOWN) logger.info('User {} requested stats'.format(user_id))
def insert_url(url:str, code:str) -> int: """Inserts a new URL""" q = URL.insert(url=url, code=code) url_id = q.execute() Stats.insert(url=url_id, usage_count=0).execute() return url_id
def stat(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id chat_type = update.message.chat.type chat_title = update.message.chat.title last_call = cache.get('last_{}'.format(chat_id)) # First request if not last_call: cache.set('last_{}'.format(chat_id), int(time.time()) - 5) last_call = int(time.time()) - 5 # If last request was over 5 seconds ago if (int(time.time()) - last_call) >= 5: if chat_type == 'group' or chat_type == 'supergroup': # Get stats for group info = Stats.get_chat(chat_id) # Get msg text for /stat msg = Stats.stat_format(chat_id, info['msg_count'], info['current_users'], info['top_users'], chat_title) bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN) # Update last call cache.set('last_{}'.format(chat_id), int(time.time())) logger.info('Group {} requested stats'.format(chat_id))
def process_clients(self, file_path): """This method is used to parse the clients.xml and provides the IP, PORT, USERNAME, PASSWORD and EMAIL to upload_collector Attributes: file_path (str): path for clients.xml """ logger.info(f"Clients file path: {file_path}") logger.info("Started parsing clients file") try: tree = ET.parse(file_path) except (ET.ParseError, FileNotFoundError) as exe: logger.exception(exe) return False try: root = tree.getroot() for client in root.findall("client"): data = self.parse_client(client) stats = Stats.get_or_create(**data) Stats.save(stats) upload_collector.delay( data["ip"], data["port"], data["username"], data["password"] ) except socket.error as e: logger.exception(f"Rabbitmq is not accessible. {e}") return False
def upload(): failed = False # Need 'force=True' for file upload data = request.get_json(force=True) # So very, very insecure username = data.get('username') password = data.get('password') user = query_user(username, password) if user is None: return redirect(url_for('page_unauthorized'), 'No such user') action = data.get('action') user_key = user.key if action == 'workout': photo_url, blob_key = get_photo_url_and_blob_key() try: workout_dict = data.get('workout') new_workout = Workout( user_key = user_key, name = workout_dict.get('name'), blob_key = blob_key, photo_url = photo_url, text = workout_dict.get('text'), exercises = workout_dict.get('exercises') ) new_workout.put() except: print("Failed to create new workout: {0}".format(sys.exc_info()[0])) failed = True elif action == 'stats': try: stats_dict = data.get('stats') new_stats = Stats( user_key = user_key, weight = stats_dict.get('weight'), height_inches = stats_dict.get('height_inches'), height_feet = stats_dict.get('height_feet'), body_fat = stats_dict.get('body_fat'), ) new_stats.put() except: print("Failed to create new workout: {0}".format(sys.exc_info()[0])) failed = True else: return redirect(url_for('page_unauthorized'), 'No such operation') if failed: return "failed" return "success"
def save(self, user): photo = super(PhotoForm, self).save(commit=False) stats = Stats() stats.save() info = PhotoInfo() info.save() photo.stats = stats photo.info = info photo.user = user photo.save()
def upload(): failed = False # Need 'force=True' for file upload data = request.get_json(force=True) # So very, very insecure username = data.get('username') password = data.get('password') user = query_user(username, password) if user is None: return redirect(url_for('page_unauthorized'), 'No such user') action = data.get('action') user_key = user.key if action == 'workout': photo_url, blob_key = get_photo_url_and_blob_key() try: workout_dict = data.get('workout') new_workout = Workout(user_key=user_key, name=workout_dict.get('name'), blob_key=blob_key, photo_url=photo_url, text=workout_dict.get('text'), exercises=workout_dict.get('exercises')) new_workout.put() except: print("Failed to create new workout: {0}".format( sys.exc_info()[0])) failed = True elif action == 'stats': try: stats_dict = data.get('stats') new_stats = Stats( user_key=user_key, weight=stats_dict.get('weight'), height_inches=stats_dict.get('height_inches'), height_feet=stats_dict.get('height_feet'), body_fat=stats_dict.get('body_fat'), ) new_stats.put() except: print("Failed to create new workout: {0}".format( sys.exc_info()[0])) failed = True else: return redirect(url_for('page_unauthorized'), 'No such operation') if failed: return "failed" return "success"
def get_normalized_stats(self, player_name): player = Player.query.filter_by(name=player_name).first() if player: stats = Stats.query.filter_by(player_id=player.id).first() return stats.to_dict() else: fake_game_id = 0 fake_player_id = 0 stats = Stats(fake_game_id, fake_player_id) return stats.to_dict()
async def get_stats(after='1y', vote_type=None): stats = Stats() epoch = get_epoch(after) db = DB(DB_FILE) await db.connect() votes_stats = VotesStats() votes_stats.latest = await db.get_latest_vote(vote_type) votes_stats.count = await db.get_vote_count(epoch, vote_type) stats.votes = votes_stats bots_stats = BotsStats() bots_stats.count = await db.get_bot_count(epoch) stats.bots = bots_stats await db.close() return stats
def create_stats(form, user_key=None): try: new_stats = Stats( user_key=user_key, weight=form.weight.data, height_inches=form.height_inches.data, height_feet=form.height_feet.data, body_fat=form.body_fat.data, ) new_stats.put() except: print("Failed to update user stats: {0}".format(sys.exc_info()[0])) return None return new_stats
def create_stats(form, user_key=None): try: new_stats = Stats( user_key = user_key, weight = form.weight.data, height_inches = form.height_inches.data, height_feet = form.height_feet.data, body_fat = form.body_fat.data, ) new_stats.put() except: print("Failed to update user stats: {0}".format(sys.exc_info()[0])) return None return new_stats
def add_stat(self, player_name, stat): player = self.get_or_create_player(player_name) if player.name not in self.stats: self.stats[player.name] = Stats(self.game_id, player.id) self.stats[player.name].count_stat(stat)
def get_stats(self, stats_id): cur = self.con.cursor() q = """ SELECT stats_id, player_id, attack_player_id, defense_player_id, team_id, wins, draws, losses, goals_pro, goals_against, elo_rating, timestamp FROM STATS WHERE stats_id = :stats_id """ cur.execute(q, dict(stats_id=stats_id)) stats_exists = cur.fetchone() if stats_exists: _, player_id, attack_player_id, defense_player_id, team_id, wins, draws, losses, goals_pro, goals_against, elo_rating, timestamp = stats_exists return Stats(stats_id=stats_id, player_id=player_id, attack_player_id=attack_player_id, defense_player_id=defense_player_id, team_id=team_id, wins=wins, draws=draws, losses=losses, goals_pro=goals_pro, goals_against=goals_against, elo_rating=elo_rating, timestamp=timestamp) else: return None
def stats_add(): values = request.get_json() if not values: return json.dumps({"error": "please provide data with json headers"}) name = values.get('server_name', None) stats = {} if not name: return json.dumps({"error": "invalid name"}) session = Session() machine = None try: machine = session.query(Machine).filter_by(name=name).one() except Exception as ex: stats = { "excpetion": "None or More than one servers exist with the name %s" % (name, ) } if machine: count = session.query(Stats).filter_by(machine=machine).count() if count >= stats_count_limit: extra_stats = session.query(Stats).filter_by( machine=machine).order_by( Stats.id.asc()).limit(count - stats_count_limit + 1) for each in extra_stats: session.delete(each) try: if enable_print: print(json.dumps(values, indent=4)) cpu = float(values.get( "cpu")) if "cpu" in values and values.get("cpu") else None disk = float(values.get( "disk")) if "disk" in values and values.get("disk") else None memory = float( values.get("memory") ) if "memory" in values and values.get("memory") else None gpu = float(values.get( "gpu")) if "gpu" in values and values.get("gpu") else None network = float( values.get("network") ) if "network" in values and values.get("network") else None processes = float( values.get("processes") ) if "processes" in values and values.get("processes") else None new_stat = Stats(machine=machine, cpu=cpu, memory=memory, disk=disk, gpu=gpu, network=network, processes=processes) session.add(new_stat) session.commit() stats = {"status": "Success"} except Exception as ex: stats = {"exception": str(ex)} session.close() return json.dumps(stats)
def new_company(): form = StatsForm(request.form) if request.method == "POST" and form.validate(): stats = Stats() save_changes(stats, form, new=True) flash("Company created!") return redirect("/") return render_template("new_company.html", form=form)
async def top(search_id: int = Path(..., gt=0)) -> List[StatsModel]: """This function provides the get method to route /stats/top/{search_id} :param search_id: primary key for finding necessary record :return: list of all Stats records for specified Searches record :rtype: List[StatsModel] """ stats_all = Stats.filter( search_id=search_id).order_by('-ads_amount').limit(5) return await StatsModel.from_queryset(stats_all)
def spider_closed(self, spider): self.session = self.Session() try: stats = Stats(self.crawler.stats.get_stats()) self.session.add(stats) self.session.commit() except Exception as exception: logging.error("Could not save crawler statistics %s cause %s", stats, exception) self.session.rollback() finally: self.session.close()
def write_stats(request): stats = Stats() agent = request.user_agent stats.agent_platform = agent.platform stats.agent_browser = agent.browser stats.agent_browser_version = agent.version stats.agent_lang = agent.language trusted_proxies = {'127.0.0.1', '10.9.180.95'} # define your own set route = request.access_route + [request.remote_addr] remote_addr = next( (addr for addr in reversed(route) if addr not in trusted_proxies), request.remote_addr) stats.ip = remote_addr stats.referrer = request.referrer stats.time = datetime.utcnow().time() stats.date = datetime.utcnow().date() db.session.add(stats) db.session.commit()
def get_today_stats(): now = datetime.utcnow() # get all video in database videos = tqdm(videoCollection.find({})) for v in videos: data = yt.get_video_stats(v['vid']) try: stats = Stats(v['vid'], now, data['viewCount'], data['likeCount'], data['dislikeCount'], data['commentCount']) add_stats(stats) except: print(f'Failed: {v["vid"]}')
def make_character(stats, profession, background, professional_skills, background_skills, base_skills): prof_skills = make_professional_skills( professional_skills[profession]["Skills"]) char_background = make_background_skills(background_skills[background], base_skills.keys()) char_stats = Stats(*stats) char_skills = Skills(base_skills, prof_skills, char_background) bonds = professional_skills[profession]["Bonds"] character = Character(char_stats, char_skills, profession, bonds, background) return character
def get_pins_added(): pins_added = 0 if not Stats.query.filter_by(user_id=current_user.id).first(): stats = Stats(user_id=current_user.id) db.session.add(stats) db.session.commit() else: stats = Stats.query.filter_by(user_id=current_user.id).first() pins_added = stats.pins_added return pins_added
def update_stats(pin_added, current_user_id): if not Stats.query.filter_by(user_id=current_user_id).first(): stats = Stats(user_id=current_user_id, total_pins=pin_added, last_pin_at=datetime.utcnow()) db.session.add(stats) else: stats_instance = Stats.query.filter_by(user_id=current_user_id).first() stats_instance.total_pins += pin_added stats_instance.last_pin_at = datetime.utcnow() db.session.commit() return True
def write_stats(request): stats = Stats() agent = request.user_agent stats.agent_platform = agent.platform stats.agent_browser = agent.browser stats.agent_browser_version = agent.version stats.agent_lang = agent.language trusted_proxies = {'127.0.0.1', '10.9.180.95'} # define your own set route = request.access_route + [request.remote_addr] remote_addr = next((addr for addr in reversed(route) if addr not in trusted_proxies), request.remote_addr) stats.ip = remote_addr stats.referrer = request.referrer stats.time = datetime.utcnow().time() stats.date = datetime.utcnow().date() db.session.add(stats) db.session.commit()
def init_db(): db.drop_all() db.create_all() bal = get_bal() db.session.add( Stats(distance=0, distance1=0, distance2=0, cash=0, venmo=0, card=0, misc=0, start_venmo_bal=bal)) db.session.commit()
def post(self): args = self.reqparse.parse_args() data = json.loads(args['data']) # Prevent duplicate entrires if Stats.query.filter_by(date=data['date'], start_time=data['start_time'], end_time=data['end_time'], water_source=data['water_source']).first() is None: # Add entry if not duplicate stats = Stats(date=data['date'], start_time=data['start_time'], end_time=data['end_time'], water_source=data['water_source']) db.session.add(stats) db.session.commit() return { 'data': data, }, 200
def get_video(video_id: str): try: print(video_id) video = Video.get(Video.id == video_id) video_stats = Stats.get(Stats.video == video) video_dict = model_to_dict(video) video_stats_ = model_to_dict(video_stats) video_dict['statistic'] = video_stats_ video_dict['status'] = 'success' return video_dict except BaseException as e: # print(e) return { 'status': 'not found', }
def show_calculator_form(): """Use a total daily energy expenditure to find and add the users current stats to their user stats. Information for the calculator itself can be seen in tdee_calculator.py, has more than tdee and we pass in all that information to the database for a user """ form = TDEEForm() # once the user fills out and completes this form they will have access to our dashboard if form.validate_on_submit(): # process the form data gender = request.form['gender'] height = request.form['height'] weight = request.form['weight'] age = request.form['age'] activity_level = request.form['activity_level'] # run the calculations for the various health metrics tdee = calculate_tdee(gender, height, weight, age, activity_level) bmi = calculate_bmi(height, weight) ideal_weight = calculate_ideal_weight(height) pounds_to_lose = calculate_pounds_to_lose(weight, height) ideal_time_frame = calculate_ideal_time_frame(tdee, pounds_to_lose) # check if the user currently has stats, if they dont then we make a new stats entry to our database and give access to the database if ((Stats.query.filter_by(user_id=current_user.id).first()) == None): new_stats = Stats(user_id=current_user.id, height=height, weight=weight, tdee=tdee, bmi=bmi, ideal_weight=ideal_weight, pounds_to_lose=pounds_to_lose, ideal_time_frame=ideal_time_frame) db.session.add(new_stats) db.session.commit() return redirect('/dashboard') # else we will be just updating the current user stats and send them back to the dashboard and load in the updated info else: current_stats = Stats.query.filter_by( user_id=current_user.id).first() current_stats.height = height current_stats.weight = weight current_stats.tdee = tdee current_stats.bmi = bmi current_stats.ideal_weight = ideal_weight current_stats.pounds_to_lose = pounds_to_lose current_stats.ideal_time_frame = ideal_time_frame db.session.commit() return redirect('/dashboard') # Ideally the user will only have to visit this form once since it now lives on the dashbaord for easier access return render_template('metrics.html', form=form, user=current_user)
def update_stats(current_user_id, pinterest_requests_left=None, pins_added=None): # If Stats does not exist, it will be created on the first call to Home URL if not Stats.query.filter_by(user_id=current_user_id).first(): pinterest_requests_left = pinterest_requests_left if pinterest_requests_left else 0 stats_data = Stats(user_id=current_user_id, pinterest_requests_left=pinterest_requests_left) db.session.add(stats_data) else: stats_instance = Stats.query.filter_by(user_id=current_user_id).first() stats_instance.pins_added = stats_instance.pins_added + pins_added if pins_added else stats_instance.pins_added stats_instance.pinterest_requests_left = pinterest_requests_left if pinterest_requests_left else stats_instance.pinterest_requests_left stats_instance.last_pin_at = datetime.utcnow() db.session.commit() return True
async def stats(search_id: int = Query(..., gt=0), from_datetime: datetime = Query(datetime.fromtimestamp(0)), to_datetime: datetime = Query( None, description='Format: YYYY-mm-DDTHH:MM:SS')): """This method provides the get method to route /stats/ :param search_id: primary key for finding necessary Searches record :param from_datetime: first field of datetime interval :param to_datetime: second filed of datetime interval :return: list of all Stats records for specified Searches record """ filters = { 'search_id': search_id, 'created_at__gte': from_datetime, } if to_datetime: filters['created_at__lte'] = to_datetime stats_queryset = Stats.filter(**filters) return await StatsModel.from_queryset(stats_queryset)
def init_db(): db.drop_all() db.create_all() bal = fetch_venmo_balance() if bal is None: print("fetch_venmo_balance returned None. Please reauthorize to fetch " "initial venmo balance.") print("Database manipulation failed.") return db.session.add( Stats(distance=0, distance1=0, distance2=0, cash=0, venmo=0, card=0, misc=0, start_venmo_bal=bal)) db.session.commit()
def build_stats_response(games): present_players = [] stats = {} week = 0 for game in games: week = max(week, game.week) present_players = present_players + game.players for player_stats in Stats.query.filter_by(game_id=game.id): player = Player.query.get(player_stats.player_id) data = player_stats.to_dict() # sum all stats for the player if player.name in stats: existing_data = stats[player.name] stats_to_sum = data.keys() - [ 'pay', 'salary', 'salary_per_point', 'o_efficiency', 'd_efficiency', 'total_efficiency' ] summed_stats = { s: data.get(s, 0) + existing_data.get(s, 0) for s in stats_to_sum } stats[player.name].update(summed_stats) else: stats.update({player.name: data}) # set the current team for the player if player.name in json.loads(game.home_roster): team = game.home_team elif player.name in json.loads(game.away_roster): team = game.away_team if "(S)" in player.name: team = "Substitute" data.update({'team': team}) # handle absent players players = Player.query.filter(Player.team_id != None) absent_players = [ player for player in players if player.name not in present_players ] for player in absent_players: game_id = -1 player_stats = Stats(game_id, player.id).to_dict() player_stats.update({'team': player.team.name}) if player.salary > 0: player_stats.update({'salary': player.salary}) else: team_players = Player.query.filter_by(team_id=player.team_id) same_gender_salaries = [ p.salary for p in team_players if p.is_male == player.is_male and p.salary > 0 ] avg_salary = sum(same_gender_salaries) / len( same_gender_salaries) player_stats.update({'salary': avg_salary}) stats[player.name] = player_stats # pro rate for the current week for player in stats: pro_rated_salary = stats[player]['salary'] * float(week) stats[player]['salary'] = int(pro_rated_salary) return stats
def app_create(request): #Dropbox auth client = db_client(request) if not client: return redirect(dropbox_auth) #Make sure tokens are valid try: client.account_info() except: try: del request.session["dropbox_access_token"] del request.session["dropbox_access_token_secret"] return redirect(dropbox_auth) except: return redirect('home') #If form submitted if request.method == 'POST': form = CreateAppForm(request.POST) # A form bound to the POST data if form.is_valid(): # Process the data in form.cleaned_data app_form_vars = {} app_registration_vars = {} app_probe_vars = {} for field_name in form.cleaned_data.keys(): #Registration info if field_name.endswith('REG_INFO'): app_registration_vars[field_name] = form.cleaned_data[field_name] #Configuration update/Upload data info if field_name == 'configUpdate_freq' or field_name == 'dataUpload_freq': if form.cleaned_data[field_name] != None: app_form_vars[field_name] = int(form.cleaned_data[field_name]) #General app info elif not field_name.endswith('Probe') and not field_name.endswith('freq') and not field_name.endswith('duration'): #Clean if app name if field_name == 'app_name': app_form_vars[field_name] = re.sub(r'([^\s\w]|_)+', '', form.cleaned_data[field_name]) else: app_form_vars[field_name] = form.cleaned_data[field_name] #Probe info elif not field_name.endswith('freq') and not field_name.endswith('duration') and not form.cleaned_data[field_name] == False: try: app_probe_vars[field_name] = {} #Extra parameters for UserStudyNotificationProbe if field_name == 'UserStudyNotificationProbe': app_probe_vars[field_name]['URL'] = form.cleaned_data[field_name + '_url'] app_probe_vars[field_name]['TITLE'] = form.cleaned_data[field_name + '_notifyTitle'] app_probe_vars[field_name]['MESSAGE'] = form.cleaned_data[field_name + '_notifyMessage'] app_probe_vars[field_name]['PERIOD'] = int(form.cleaned_data[field_name + '_freq']) app_probe_vars[field_name]['DURATION'] = int(form.cleaned_data[field_name + '_duration']) except: pass #Create json config for app creation config_dict = create_app_config(app_form_vars, app_probe_vars) config_json = json.dumps(config_dict) #Save stats app_creation = Stats(create_time=datetime.datetime.now(), app_name=app_form_vars['app_name'], description=app_form_vars['description'], contact_email=app_form_vars['contact_email'], creator_name=app_registration_vars['creator_name_REG_INFO'], creator_email=app_registration_vars['creator_email_REG_INFO'], org_name=app_registration_vars['org_name_REG_INFO'], location=app_registration_vars['location_REG_INFO'], probe_config=str(config_dict)) app_creation.save() dropbox_account_info = client.account_info() access_token = request.session.get("dropbox_access_token") access_token_secret = request.session.get("dropbox_access_token_secret") AuthHTTP = ComputeEngine.Authorize() ComputeEngine.NewInstance(AuthHTTP, dropbox_account_info["uid"], access_token, access_token_secret, app_form_vars["app_name"], app_form_vars["description"], app_form_vars["contact_email"], config_json) return redirect(app_thanks) # Redirect after POST else: form = CreateAppForm() # An unbound form return render(request, 'app_create.html', {'form': form})
def stats(): stats = Stats.get_all() return render_template("stats.html", data=stats)
def profile(name): stats = Stats.get_by_name(name) if stats is None: abort(404) print(stats) return render_template('profile.html', data=stats)