diff --git a/apps/monitor/calc.py b/apps/monitor/calc.py index f996ada..364a8bf 100644 --- a/apps/monitor/calc.py +++ b/apps/monitor/calc.py @@ -19,6 +19,43 @@ class Calc: "diffPercentage": (CurrentRate.rate-Decimal(rateMinusXDay[0]))/Decimal(rateMinusXDay[0])*100, "rateUSD": None} return None + def CalcHistory(self, Hours, Crypto, Fiat): + with connection.cursor() as cursor: + startDate = datetime.datetime.today() - datetime.timedelta(hours=Hours) + endDate = datetime.datetime.today() + sql = """SELECT datetime_valid, rate FROM exchangerate + WHERE crypto_currency = %s AND fiat_currency = %s + AND datetime_valid BETWEEN %s AND %s + ORDER BY datetime_valid ASC LIMIT 1""" + cursor.execute(sql, [Crypto, Fiat, startDate, endDate]) + startRate = cursor.fetchone() + if not startRate: + return {} + sql = """SELECT datetime_valid, rate FROM exchangerate + WHERE crypto_currency = %s AND fiat_currency = %s + AND datetime_valid BETWEEN %s AND %s + ORDER BY datetime_valid DESC LIMIT 1""" + cursor.execute(sql, [Crypto, Fiat, startDate, endDate]) + endRate = cursor.fetchone() + sql = """SELECT datetime_valid, rate FROM exchangerate + WHERE crypto_currency = %s AND fiat_currency = %s + AND datetime_valid BETWEEN %s AND %s + ORDER BY rate DESC LIMIT 1""" + cursor.execute(sql, [Crypto, Fiat, startDate, endDate]) + maxRate = cursor.fetchone() + sql = """SELECT datetime_valid, rate FROM exchangerate + WHERE crypto_currency = %s AND fiat_currency = %s + AND datetime_valid BETWEEN %s AND %s + ORDER BY rate ASC LIMIT 1""" + cursor.execute(sql, [Crypto, Fiat, startDate, endDate]) + minRate = cursor.fetchone() + return {"startdate": startRate[0], "startvalue": startRate[1], + "curdate": endRate[0], "curvalue": endRate[1], + "mindate": minRate[0], "minvalue": minRate[1], + "maxdate": maxRate[0], "maxvalue": maxRate[1]} + return None + + def GetCurrentValue(self, userid, crypto, total_investment, current_value, total_tax_free): amount_kept = 0 out = {} @@ -70,6 +107,15 @@ class Calc: if diffrate: out["rates"].append(diffrate) + out["graphs"] = [] + out["graphs"].append({"id": "h48", "active": "active", "label": "48 hours", "period": "number_of_hours=48", **self.CalcHistory(24*2, crypto, 'EUR')}) + out["graphs"].append({"id": "d3", "label": "3 days", "period": "number_of_hours=72", **self.CalcHistory(24*3, crypto, 'EUR')}) + out["graphs"].append({"id": "w1", "label": "1 week", "period": "number_of_hours=168", **self.CalcHistory(24*7, crypto, 'EUR')}) + out["graphs"].append({"id": "m1", "label": "1 month", "period": "number_of_days=30", **self.CalcHistory(24*30, crypto, 'EUR')}) + out["graphs"].append({"id": "m6", "label": "6 months", "period": "number_of_days=180", **self.CalcHistory(24*180, crypto, 'EUR')}) + out["graphs"].append({"id": "y1", "label": "1 year", "period": "number_of_days=360", **self.CalcHistory(24*360, crypto, 'EUR')}) + out["graphs"].append({"id": "y10", "label": "10 years", "period": "number_of_days=3600", **self.CalcHistory(24*3600, crypto, 'EUR')}) + amount_within_past_year = 0 with connection.cursor() as cursor: sql = """SELECT SUM(amount_after_fee) as amount FROM `transaction` WHERE amount > 0 and crypto_currency=%s and owner_id=%s and date_valid>%s""" diff --git a/apps/monitor/templates/monitor.html b/apps/monitor/templates/monitor.html index 8e8856f..c7d5c9f 100644 --- a/apps/monitor/templates/monitor.html +++ b/apps/monitor/templates/monitor.html @@ -97,34 +97,16 @@ -
-

48 hours

- -
-
-

3 days

- -
-
-

1 week

- -
-
-

1 month

- -
-
-

6 months

- -
-
-

1 year

- -
-
-

10 years

- + {% for g in c.graphs %} +
+

{{ g.label }}

+
+ Start: {{ g.startdate }} {{ g.startvalue|floatformat:2 }} EUR
+ Min: {{ g.mindate }} {{ g.minvalue|floatformat:2 }} EUR
+ Max: {{ g.maxdate }} {{ g.maxvalue|floatformat:2 }} EUR
+ Now: {{ g.curdate }} {{ g.curvalue|floatformat:2 }} EUR
+ {% endfor %}
{% endif %}