Exemplo n.º 1
0
    def record_usage(self):
        client = Box(self.logger).client()
        if client is None:
            self.logger.warn(
                "Client was not created. Users will not be fetched.")
            return

        starting = datetime.datetime.now(datetime.timezone.utc).replace(
            minute=0, second=0, microsecond=0)
        ending = starting + datetime.timedelta(days=1)
        users = self.get_users(client)
        active = len([elem for elem in users if elem['status'] == 'active'])
        inactive = len(
            [elem for elem in users if elem['status'] == 'inactive'])
        storage_used = sum(map(lambda user: user['space_used'], users))
        self.logger.debug(
            "User stats: {0} active; {1} inactive; {2} GB used".format(
                active, inactive, storage_used / (1024 * 1024 * 1024)))
        db.session.add(Stat('ACTIVE_USERS', active, starting, ending))
        db.session.add(Stat('INACTIVE_USERS', inactive, starting, ending))
        db.session.add(
            Stat('STORAGE_USED_GB', storage_used / (1024 * 1024 * 1024),
                 starting, ending))
        try:
            db.session.commit()
        except Exception as e:
            self.logger.debug(
                'Caught exception when adding user stats: {}'.format(e))
            db.session.rollback()
Exemplo n.º 2
0
def site_stats(request):
    
    ss = Stat()
    ss.openid()
    
    from constants import STATS_TITLES as t
    
    from models import StatDB
    cs = StatDB.objects.latest()
    
    tails = cs.most_common_tail.split("\n")
    linked_tail = link_tails(tails)
        
    idents = cs.auv.split("\n")
    linked_airports = link_airports(idents)
    
    models = cs.most_common_type.split("\n")
    linked_mct = link_models(models)
    
    traveled_tails = cs.most_traveled_tail.split("\n")
    linked_travel_tail = link_tails(traveled_tails)
    
    days_million = cs.days_until_million()
    date_million = datetime.date.today() + datetime.timedelta(days=days_million)
    
    from django.utils.dateformat import format
    date_million = format(date_million, 'd M, Y')
    
    return locals()
Exemplo n.º 3
0
 def test_get_stats_shortcode_not_found_failure(self):
     with pytest.raises(
             Exception
     ) as exc:  # Wide catch, scope narrowed for preventing nested Exception override
         Stat.get_stats(shortcode='0b0b0b')
         assert isinstance(exc.type,
                           ShortcodeNotFound.__class__)  # Sanity check
Exemplo n.º 4
0
def create_new_user(user_info):
    new_user = User(username=user_info['username'], bio=user_info['bio'],
                    website=user_info['website'], profile_picture=user_info['profile_picture'],
                    user_id=user_info['id'], full_name=user_info['full_name'])
    user_stat = Stat(followed_by=user_info['counts']['followed_by'],
                     follows=user_info['counts']['follows'],
                     media=user_info['counts']['media'])
    new_user.stat = user_stat

    user_stat.save()
    new_user.save()
    return new_user
Exemplo n.º 5
0
def setStats():
    result = {'type': ProtocolTypes.SetStats}

    if request.method == 'POST' and request.form['data']:
        got_data = json.loads(request.form['data'])
        from_keys = ['session_id', 'stats']
        if checkContainKeys(from_keys, got_data):
            result['result'], got_user = checkSessionId(got_data['session_id'])

            if got_user:
                got_stat = got_data['stats']
                find_stat = Stat.query.filter_by(user_id=got_user.id).first()
                if find_stat:
                    # find_stat.__dict__.update(dict(
                    #     exp=got_stat['exp'],
                    #     level=got_stat['level'],
                    #     hp=got_stat['hp'],
                    #     weapon_level=got_stat['weapon_level'],
                    #     weapon_exp=got_stat['weapon_exp'],
                    #     visited_zone_no=got_stat['visited_zone_no'],
                    #     updated_date=datetime.datetime.now()))
                    find_stat.exp = got_stat['exp']
                    find_stat.level = got_stat['level']
                    find_stat.hp = got_stat['hp']
                    find_stat.weapon_level = got_stat['weapon_level']
                    find_stat.weapon_exp = got_stat['weapon_exp']
                    find_stat.visited_zone_no = got_stat['visited_zone_no']
                    find_stat.updated_date = datetime.datetime.now()

                    db_session.add(find_stat)
                else:
                    made_stat = Stat(got_user.id)
                    # made_stat.__dict__.update(dict(
                    #     exp=got_stat['exp'],
                    #     level=got_stat['level'],
                    #     hp=got_stat['hp'],
                    #     weapon_level=got_stat['weapon_level'],
                    #     weapon_exp=got_stat['weapon_exp'],
                    #     visited_zone_no=got_stat['visited_zone_no'],
                    #     updated_date=datetime.datetime.now()))
                    made_stat.exp = got_stat['exp']
                    made_stat.level = got_stat['level']
                    made_stat.hp = got_stat['hp']
                    made_stat.weapon_level = got_stat['weapon_level']
                    made_stat.weapon_exp = got_stat['weapon_exp']
                    made_stat.visited_zone_no = got_stat['visited_zone_no']
                    made_stat.updated_date = datetime.datetime.now()

                    db_session.add(made_stat)

                try:
                    db_session.commit()
                except exc.SQLAlchemyError:
                    result['result'] = ResultCodes.DBInputError
        else:
            result['result'] = ResultCodes.InputParamError
    else:
        result['result'] = ResultCodes.AccessError

    return str(json.dumps(result))
Exemplo n.º 6
0
def get_stats(shortcode):
    """
    This endpoint method handles the shortcode stats requests,
    routed to the shortcode specific routing url concatenated with
    the stats endpoint and being a GET request.

    The Stat database model specific methods will handle the logic.

    :param shortcode: The provided shortcode as url parameter.
    :type shortcode: str

    :return: The response with the corresponding stats details for the
        provided shortcode.
    :rtype: flask.Response

    .. note::
        The Stat database model specific methods handle the logic
        and exception handling for this endpoint, as the endpoint relies
        heavily on database specific logic.
    .. seealso::
        See for database model related methods: src/models.py
        See for exception related exceptions: src/exceptions.py
    """
    stats = Stat.get_stats(shortcode=shortcode)
    response = jsonify(stats)
    response.status_code = 200
    return response
Exemplo n.º 7
0
def crawl(remote, caller, *args):
    s = Stat.get_or_create(caller)
    try:
        getattr(remote, caller)(*args)
    except zerorpc.RemoteError, e:
        import traceback
        s.error(traceback.format_exc())
Exemplo n.º 8
0
    def test_parse_stat(self):

        response = '''
            {"response":[[
                {"month":"2011-02","impressions":0,"clicks":0,"money":"0.00"},
                {"month":"2011-03","impressions":123456,"clicks":789,"money":"123.45"}
            ]]}
            '''
        account = Account.objects.create(remote_id=1)

        instance = Stat(account=account, period=0, data={})
        instance.parse(json.loads(response)['response'][0][1])
        instance.save()

        self.assertEqual(instance.clicks, 789)
        self.assertEqual(instance.impressions, 123456)
        self.assertEqual(instance.month, '2011-03')
        self.assertEqual(instance.money, '123.45')
Exemplo n.º 9
0
def add(user, stat):
    """Add the users and the stats in the session."""
    for el in user:
        U = User(el, user[el])
        db_session.add(U)

    for el in stat:
        S = Stat(el, stat[el])
        db_session.add(S)
Exemplo n.º 10
0
def go(request):
	user = request.user
				
	stats = Stat.objects()

	template_values = {
		'stats': stats,
		'user' : user,
	}

	path = os.path.join(os.path.dirname(__file__), 'templates/ideas/managestats.html')
	return render_to_response(path, template_values)
Exemplo n.º 11
0
def incrementStat(statname,value):
	stats = Stat.objects(name=statname)
	stat = None
	if not stats or len(stats)<=0:
		stat = Stat()
		stat.name = statname
		stat.total = 0
	else:
		stat = stats[0]
	stat.total = stat.total + value
	stat.save()
Exemplo n.º 12
0
    def record_velocity(self):
        created_before = datetime.datetime.now(datetime.timezone.utc).replace(
            second=0, microsecond=0)
        created_after = created_before + datetime.timedelta(minutes=-1)
        client = Box(self.logger).client()
        if client is None:
            self.logger.warn(
                "Client was not created. Events will not be fetched.")
            return

        events = self.get_velocity_events(client,
                                          BackgroundTasks.velocity_event_types,
                                          created_after, created_before)

        for event_type in BackgroundTasks.velocity_event_types:
            count = len(
                [elem for elem in events if elem['event_type'] == event_type])
            stat = Stat(event_type, count, created_after, created_before)
            db.session.add(stat)
            try:
                db.session.commit()
            except Exception as e:
                self.logger.debug('Caught exception: {}'.format(e))
                db.session.rollback()

        unique_user_count = len(
            set([elem['created_by']['login'] for elem in events]))
        stat = Stat('UNIQUE_USERS', unique_user_count, created_after,
                    created_before)
        db.session.add(stat)
        try:
            db.session.commit()
        except:
            self.logger.debug(
                'Caught exception when adding event stats: {}'.format(e))
            db.session.rollback()
        else:
            self.logger.info("inserted %s unqiue users" % unique_user_count)
Exemplo n.º 13
0
 def get(self):
     """
     If region_id is in the query string, then we want to see statistics
     by cities, and we do not need links
     """
     if 'rid' in self.req_get:
         context = {
             'rows': [
                 s.as_dic()
                 for s in Stat().get_list(region_id=self.req_get['rid'][0])
             ],
             'city':
             1,
         }
     else:
         context = {
             'rows': [],
             'city': 0,
         }
         rows = Stat().get_list()
         if rows:
             context['rows'] = [stat.as_dic() for stat in rows]
     return render('templates/stat.html', context)
Exemplo n.º 14
0
def search(country_name):
    try:
        stat = Stat.get(Stat.country_name == country_name)
    except:
        return False

    l = []
    l.append(stat.country_name)
    l.append(stat.cases)
    l.append(stat.deaths)
    l.append(stat.total_recovered)
    l.append(stat.new_cases)
    l.append(stat.new_deaths)
    l.append(str(stat.updated))
    return l
Exemplo n.º 15
0
def top_10():
    top10_country = []
    top10_cases = []
    i = 0
    try:
        Stats = Stat.select().order_by(-Stat.cases)
        for stat in Stats:
            i += 1
            if i > 1:
                top10_country.append(stat.country_name)
                top10_cases.append(stat.cases)
            if i == 11:
                break
    except:
        return False

    return top10_country, top10_cases
Exemplo n.º 16
0
    def create_stat(token):
        body = request.get_json()

        athlete_id = body.get('athlete_id', None)
        avg_miles_per_week = body.get('avg_miles_per_week', None)
        avg_vertical_per_week = body.get('avg_vertical_per_week', None)
        longest_run = body.get('longest_run', None)
        longest_run_2_weeks = body.get('longest_run_2_weeks', None)
        race_distance = body.get('race_distance', None)
        race_veritcal = body.get('race_veritcal', None)
        race_date = body.get('race_date', None)

        if not ('athlete_id' in body):
            abort(404)

        try:
            stat = Stat(athlete_id=athlete_id,
                        avg_miles_per_week=avg_miles_per_week,
                        avg_vertical_per_week=avg_vertical_per_week,
                        longest_run=longest_run,
                        longest_run_2_weeks=longest_run_2_weeks,
                        race_distance=race_distance,
                        race_veritcal=race_veritcal,
                        race_date=race_date)
            db.session.add(stat)
            db.session.commit()

            return jsonify({
                'success': True,
                'created stat_id': stat.id,
                'total_stats': len(Stat.query.all())
            })
        except:
            abort(422)

        finally:
            db.session.close()
Exemplo n.º 17
0
Arquivo: app.py Projeto: bachard/idp
def add_stats_to_db(data):
    """Add data statistics to the database"""
    # Expected parameters
    session_uuid = data['session_id']
    world = data['world']
    round = data['round']
    player_id = data['player']
    stats = json.loads(data['stats'])
    try:
        position_over_time = data['position_over_time']
    except Exception as e:
        print(e)
        position_over_time = None
    try:
        solution = data['solution']
    except Exception as e:
        solution = "Not implemented"

    checkpoints = data['checkpoints']
    score = int(data['score'])

    # try to find if the session has already been created
    # if not: create a new one
    try:
        session = db_session.query(Session).filter_by(session_uuid=session_uuid).one()
        try:
            test = db_session.query(Test).filter_by(session_id=session.id, world=world, round=round).one()
        except:
            test = Test(session.id, world, round)
            session.tests.append(test)

    except:
        session = Session(session_uuid)
        test = Test(session.id, world, round)
        session.tests.append(test)

    # try to find an existing stat for the player
    # if yes: update it
    # if not: create it
    try:
        stat = db_session.query(Stat).filter_by(test_id=test.id, player_id=player_id).one()
    except:
        stat = Stat(test.id, player_id)
        test.stats.append(stat)

    items = []

    try:
        stat.position_over_time = json.dumps(position_over_time)
    except Exception as e:
        print(e)

    stat.checkpoints = json.dumps(checkpoints)
    stat.solution = solution
    stat.score = score

    items = {}

    for (k,v) in stats.items():
        k = k.split('.')
        attr = k[0]
        if attr == 'stat':
            # changes columnName into column_name
            column = re.sub("([A-Z])","_\g<1>", k[1]).lower()
            if len(k) == 3:
                update = False
                item_id = k[2]
                # try to find an already existing item in db
                # if exists: update it
                # else: create it
                if not item_id in items:
                    items[item_id] = {}
                items[item_id][column] = v
            else:
                # only adds statistics defined in db
                try:
                    setattr(stat, column, v)
                except:
                    pass
        elif attr == 'achievement':
            achiev = k[1]
            if achiev == 'exploreAllBiomes':
                try:
                    setattr(stat, 'number_biomes', len(v['progress']))
                except:
                    pass
        else:
            pass

    for (item_id, item_values) in items.items():
        try:
            item = db_session.query(Item).filter_by(test_id=test.id, player_id=player_id, item_item=item_id).one()

        except:
            item = Item(test.id, player_id)
            test.items.append(item)
            setattr(item, 'item_item', item_id)

        for (action, n) in item_values.items():
            setattr(item, action, n)

    db_session.add(session)
    db_session.commit()
Exemplo n.º 18
0
 def test_stat_redirect_is_not_zero(self):
     stats = Stat.get_stats(shortcode='090909')
     assert stats['redirectCount'] == 2
Exemplo n.º 19
0
 def test_get_stats_success(self):
     stats = Stat.get_stats(shortcode='aqaqaq')
     assert 'created' in stats
     assert 'lastRedirect' in stats
     assert 'redirectCount' in stats
Exemplo n.º 20
0
 s = Stat(plid=player_id[i],
          player=player_stats[i][0],
          pos=player_stats[i][1],
          age=int(player_stats[i][2]),
          team=player_stats[i][3],
          g=int(player_stats[i][4]),
          gs=int(player_stats[i][5]),
          mp=int(player_stats[i][6]),
          fg=int(player_stats[i][7]),
          fga=int(player_stats[i][8]),
          fgp=float(player_stats[i][9]),
          threep=int(player_stats[i][10]),
          threepa=int(player_stats[i][11]),
          threepper=float(player_stats[i][12]),
          two=int(player_stats[i][13]),
          twoa=int(player_stats[i][14]),
          twoper=float(player_stats[i][15]),
          efg=float(player_stats[i][16]),
          ft=int(player_stats[i][17]),
          fta=int(player_stats[i][18]),
          ftper=float(player_stats[i][19]),
          orb=int(player_stats[i][20]),
          drb=int(player_stats[i][21]),
          trb=int(player_stats[i][22]),
          ast=int(player_stats[i][23]),
          stl=int(player_stats[i][24]),
          blk=int(player_stats[i][25]),
          tov=int(player_stats[i][26]),
          pf=int(player_stats[i][27]),
          pts=int(player_stats[i][28]),
          sal=int(player_sal[i][1]))
Exemplo n.º 21
0
s1 = Stat(plid='pelleno01',
          player='Norvel Pelle',
          pos='C',
          age=21,
          team='ORL',
          g=1,
          gs=0,
          mp=2,
          fg=0,
          fga=0,
          fgp=float(0),
          threep=0,
          threepa=0,
          threepper=float(0),
          two=0,
          twoa=0,
          twoper=float(0),
          efg=float(0),
          ft=0,
          fta=0,
          ftper=float(0),
          orb=0,
          drb=0,
          trb=0,
          ast=0,
          stl=0,
          blk=0,
          tov=1,
          pf=0,
          pts=0,
          sal=1079322)