Skip to content

Commit

Permalink
record timestamp hourly rounded
Browse files Browse the repository at this point in the history
  • Loading branch information
Kivou-2000607 committed Jul 25, 2019
1 parent 7ee4b26 commit 5dc58ab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
38 changes: 17 additions & 21 deletions bazaar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,9 @@ def create(cls, k, v):
def updateTendencies(self):
oneDay = 3600 * 24
priceHistory = json.loads(self.priceHistory)
priceHistoryCopy = dict({})
ts = 0
for t, p in priceHistory.items():
priceHistoryCopy[int(t) - int(t) % 3600] = p
ts = max(ts, int(t))
self.priceHistory = json.dumps(priceHistoryCopy)
priceHistory = json.loads(self.priceHistory)

# week Tendency
try:
Expand All @@ -107,7 +103,7 @@ def updateTendencies(self):
if ts - int(t) < oneDay * 6.5 and int(p):
x.append(int(t))
y.append(int(p))
print(len(x), x)
# print(len(x), x)
if(len(x) > 1):
a, b, _, _, _ = stats.linregress(x, y)
if math.isnan(a) or math.isnan(b):
Expand Down Expand Up @@ -186,22 +182,22 @@ def update(self, v):
self.tEffect = v['effect'],
self.tRequirement = v['requirement'],
self.tImage = v['image']
# priceHistory = json.loads(self.priceHistory)
# ts = int(v.get('timestamp', timezone.now().timestamp()))
# ts = int(ts) - int(ts) % 3600 # get the hour rounding
# to_del = []
# for t, p in priceHistory.items():
# if ts - int(t) > (oneMonth + 3600 * 23):
# to_del.append(t)
# if ts - int(t) < 3600 * 23: # delete entry the same day
# to_del.append(t)
#
# for t in to_del:
# print(f"[model.bazaar.item] remove history entry {t}: {priceHistory[t]}")
# del priceHistory[t]
#
# priceHistory[ts] = int(v["market_value"])
# self.priceHistory = json.dumps(priceHistory)
priceHistory = json.loads(self.priceHistory)
ts = int(v.get('timestamp', timezone.now().timestamp()))
ts = int(ts) - int(ts) % 3600 # get the hour rounding
to_del = []
for t, p in priceHistory.items():
if ts - int(t) > (3600 * 24 * 31 + 3600 * 22):
to_del.append(t)
if ts - int(t) < 3600 * 23: # delete entry the same day
to_del.append(t)

for t in to_del:
print(f"[model.bazaar.item] remove history entry {t}: {priceHistory[t]}")
del priceHistory[t]

priceHistory[ts] = int(v["market_value"])
self.priceHistory = json.dumps(priceHistory)
self.updateTendencies()
self.save()

Expand Down
10 changes: 5 additions & 5 deletions bazaar/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,20 @@ def prices(request, itemId):
priceHistory = sorted(json.loads(item.priceHistory).items(), key=lambda x: x[0])
# plot only last 8 points of the Tendency
graph = [[timestampToDate(int(t)), p, item.weekTendencyA * float(t) + item.weekTendencyB, item.monthTendencyA * float(t) + item.monthTendencyB] for t, p in priceHistory]
emptyGraph = True
graphLength = 0
for i, (_, p, wt, mt) in enumerate(graph):
if not int(p):
graph[i][1] = "null"
graph[i][2] = "null"
graph[i][3] = "null"
# graph[i][2] = "null"
# graph[i][3] = "null"
else:
emptyGraph = False
graphLength += 1
if i < len(graph) - 7 or wt < 0:
graph[i][2] = "null"
if i < len(graph) - 31 or mt < 0:
graph[i][3] = "null"

context = {'item': item, "graph": False if emptyGraph else graph}
context = {'item': item, "graph": graph, "graphLength": graphLength}
return render(request, 'bazaar/prices.html', context)

else:
Expand Down
1 change: 0 additions & 1 deletion templates/bazaar.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
$(document).on('click', '.prices-item', function(e){
e.preventDefault();
var tId = $(this).attr("href").split("/").pop();
console.log("cpicpu");
$( "#prices-item" ).load( "/bazaar/prices/"+tId, {
csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
});
Expand Down
24 changes: 18 additions & 6 deletions templates/bazaar/prices.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
{% load mathfilters %}
{% load app_filters %}

{% if graph %}
{% if graphLength > 1 %}
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var data = google.visualization.arrayToDataTable([
['Time', 'Market Price', 'Week Tendency: {{item.weekTendencyA|floatformat:0|mul:86400|format:"{:+,d}"}} $/day', 'Month Tendency: {{item.monthTendencyA|floatformat:0|mul:86400|format:"{:+,d}"}} $/day'],
['Time', 'Market Price', 'Week Tendency: {{item.weekTendencyA|mul:86400|format:"{:+,.0f}"}} $/day', 'Month Tendency: {{item.monthTendencyA|mul:86400|format:"{:+,.0f}"}} $/day'],
{% for a, b, c, d in graph %}
// [{{a}}, {{b}}],
[new Date({{a|date:"Y"}}, {{a|date:"n"|sub:"1"}}, {{a|date:"j"}}, {{a|date:"G"}}, {{a|date:"i"}}, {{a|date:"s"}}), {{b}}, {{c}}, {{d}}],
{% endfor %}
]);
Expand All @@ -49,11 +48,18 @@
};

var view = new google.visualization.DataView(data);
view.setColumns([0, 1]);
columns = [0, 1];
{% if item.weekTendencyA or item.weekTendencyB %}
columns.push(2);
{% endif %}
{% if item.monthTendencyA or item.monthTendencyB %}
columns.push(3);
{% endif %}
view.setColumns(columns);

var chart = new google.visualization.ComboChart(document.getElementById('price-history'));

chart.draw(data, options);
chart.draw(view, options);
}
</script>
{% endif %}
Expand All @@ -66,7 +72,13 @@ <h2 class="title">Last month market price fluctations of item: {{item.tName}} [{
<span style="position: absolute; top:40px; right: 10px;"><button>{{item.display_large}}</button></span>
<p>Tendency over last week: {{item.weekTendency|priceTendency|safe}}</p>
<p>Tendency over last month: {{item.monthTendency|priceTendency|safe}}</p>
<div id="price-history"></div>
{% if graphLength == 0 %}
<p class="warning">No relevent data to plot (most probably because the market price is $0)</p>
{% elif graphLength == 1 %}
<p class="warning">No relevent data to plot (most probably because the market price used to be $0), come back soon.</p>
{% else %}
<div id="price-history"></div>
{% endif %}
</div>
</div>
</div>

0 comments on commit 5dc58ab

Please sign in to comment.