Skip to content

Commit

Permalink
Added percentile autocalculation on stat modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightara committed Sep 17, 2019
1 parent 5de742f commit 49f1a71
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
36 changes: 36 additions & 0 deletions raidar/migrations/0049_stats_add_percentile_autocalculation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.2.3 on 2019-09-17 09:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('raidar', '0048_new_era_store'),
]

operations = [
migrations.AddField(
model_name='squadstats',
name='avg_val',
field=models.FloatField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='squadstats',
name='max_val',
field=models.FloatField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='squadstats',
name='min_val',
field=models.FloatField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='squadstats',
name='perc_data',
field=models.TextField(default='', editable=False),
),
]
38 changes: 34 additions & 4 deletions raidar/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import base64
import os
import re
import copy
import random
from math import floor
from time import time
from hashlib import md5
from functools import lru_cache
from datetime import datetime, timedelta, timezone
from json import loads as json_loads, dumps as json_dumps

import numpy
import pytz
from django.db import models
from django.contrib.auth.models import User
Expand Down Expand Up @@ -712,10 +716,36 @@ class Meta:
area = models.ForeignKey(Area, on_delete=models.CASCADE)
stat = models.TextField
out = models.BooleanField
min_val = models.FloatField
max_val = models.FloatField
avg_val = models.FloatField
perc_data = models.TextField
min_val = models.FloatField(editable=False)
max_val = models.FloatField(editable=False)
avg_val = models.FloatField(editable=False)
perc_data = models.TextField(default="", editable=False)

# Internal storage
data = []
percentiles = None

def add_data_point(self, data_point):
self.data.append(data_point)

def get_percentile(self, percentile):
if not self.percentiles:
self.percentiles = numpy.frombuffer(base64.b64decode(self.perc_data.encode("utf-8")), dtype=numpy.float32)
# Clamp percentile to range 0-100
percentile = max(0, min(percentile, len(self.percentiles)))
return self.percentiles[percentile] if percentile < len(self.percentiles) else self.max_val

def save(self, *args, **kwargs):
if self.data:
self.data.sort()
length = len(self.data)
percent_length = length / 100
min_val = self.data[0]
max_val = self.data[-1]
avg_val = sum(self.data) / length
perc_data = base64.b64encode(numpy.array([self.data[floor(i * percent_length)] for i in range(0, 100)],
dtype=numpy.float32).tobytes()).decode("utf-8")
super(SquadStats, self).save(*args, **kwargs)


class BuildStats(SquadStats):
Expand Down

0 comments on commit 49f1a71

Please sign in to comment.