예제 #1
0
 def test_get_average_view(self):
     older = Stats(created_at=datetime.datetime(2015, 1, 1, 1, 1, 1),
                   view_count=100)
     newer = Stats(created_at=datetime.datetime(2015, 1, 1, 1, 2, 40),
                   view_count=199)
     average_view = services._get_average_view_per_seconds(newer_stat=newer, older_stat=older)
     self.assertEqual(average_view, 1)
예제 #2
0
def poll_refresh():
    try:
        response = requests.get(
            f'{app.config["STORIES_ENDPOINT"]}/stories/stats/refresh')
    except Timeout:
        return

    to_update = response.json()
    for author in to_update:
        auth_stats = Stats.query.get(int(author))

        add = auth_stats is None
        if auth_stats is None:
            auth_stats = Stats()

        auth_stats.n_dice = 0
        auth_stats.stories_written = 0
        auth_stats.likes = 0
        auth_stats.dislikes = 0

        for story in to_update[author]:
            auth_stats.n_dice += story['dice']
            auth_stats.stories_written += 1
            auth_stats.likes += story['likes']
            auth_stats.dislikes += story['dislikes']

        if add:
            db.session.add(auth_stats)

        db.session.commit()
예제 #3
0
    def save_stat(self, video_detail):
        stats = video_detail['statistics']

        Stats(video_id=helpers.get_video_id(video_detail),
              view_count=stats.get('viewCount', None),
              like_count=stats.get('likeCount', None),
              dislike_count=stats.get('dislikeCount', None),
              favorite_count=stats.get('favoriteCount', None),
              comment_count=stats.get('commentCount', None)).save()
예제 #4
0
def init_database(database):
    example = Stats()
    example.author_id = 1
    example.likes = 5
    example.dislikes = 3
    example.stories_written = 15
    example.n_dice = 4
    database.session.add(example)

    database.session.commit()
예제 #5
0
def save_stats(dictionary):
    obj = Stats()
    # set regular fields
    for field, value in dictionary.items():
        if not isinstance(value, list):
            setattr(obj, field, value)
    try:
        obj.save()
    except IntegrityError as e:
        print("Error saving obj {}".format(e))
        return False
    return True
예제 #6
0
def calculate_view_diff(video, start_day, end_day):
    older_stat = video.stats_set.filter(
        created_at__gte=start_day).order_by('created_at')[0]
    newer_stat = video.stats_set.filter(
        created_at__lte=end_day).order_by('-created_at')[0]

    if newer_stat == older_stat:
        older_stat = Stats(view_count=0, created_at=video.published_at)

    total_seconds = (end_day - start_day).total_seconds()
    return _get_average_view_per_seconds(older_stat,
                                         newer_stat) * total_seconds
예제 #7
0
def create(date):
    existing = Stats.all().filter("date = ", date)
    try:
        stats = existing[0]
    except IndexError:
        stats = Stats()
    stats.date = date
    data = dict([(key, None) for key in registered.keys()])
    stats.set_stats(data)
    stats.save()

    for key in registered.keys():
        taskqueue.add(url=reverse("stats-action", kwargs={"action":key, "pk":stats.id}))
예제 #8
0
project_dir = "/home/django/mmashnik/mmashnik"

sys.path.append(project_dir)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django

django.setup()

from stats.models import Stats

data = csv.reader(open("/home/django/mmashnik/fighters.csv"), delimiter=',')

for row in data:
    post = Stats()
    post.id = row[0]
    post.image_stats = row[1]
    post.first_name = row[2]
    post.last_name = row[3]
    post.nick_name = row[4]
    post.date_of_birth = row[5]
    post.place_of_birth = row[6]
    post.height = row[7]
    post.weight = row[8]
    post.weight_class = row[9]
    post.win = row[10]
    post.ko_win = row[11]
    post.sub_win = row[12]
    post.dec_win = row[13]
    post.other_win = row[14]