Skip to content

Commit

Permalink
Make getting (last)monthtraffic blazing fast
Browse files Browse the repository at this point in the history
  • Loading branch information
taoky committed Mar 28, 2024
1 parent 9339298 commit de4b1f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
21 changes: 12 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@ def month_traffic(self):
from
radius.radacct
where
((month(radius.radacct.acctstarttime) = month(now())) and
(year(radius.radacct.acctstarttime) = year(now()))) and
radius.radacct.username = %s;
radius.radacct.acctstarttime BETWEEN
DATE_FORMAT(NOW() ,'%Y-%m-01') AND
LAST_DAY(NOW())
and radius.radacct.username = %s;
""", self.email).first()
return sizeof_fmt(float(r[0]) if r and r[0] else 0)

Expand All @@ -270,9 +271,10 @@ def last_month_traffic(self):
from
radius.radacct
where
((month(radius.radacct.acctstarttime) = month(now() - interval 1 month)) and
(year(radius.radacct.acctstarttime) = year(now() - interval 1 month))) and
radius.radacct.username = %s;
radius.radacct.acctstarttime BETWEEN
DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') AND
LAST_DAY(NOW() - INTERVAL 1 MONTH)
and radius.radacct.username = %s;
""", self.email).first()
return sizeof_fmt(float(r[0]) if r and r[0] else 0)

Expand All @@ -295,9 +297,10 @@ def last_month_traffic_by_day(self):
from
radius.radacct
where
month(radius.radacct.acctstarttime) = month(date_sub(now(), interval 1 month)) and
year(radius.radacct.acctstarttime) = year(date_sub(now(), interval 1 month)) and
radius.radacct.username = %s
radius.radacct.acctstarttime BETWEEN
DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') AND
LAST_DAY(NOW() - INTERVAL 1 MONTH)
and radius.radacct.username = %s
group by
day(radius.radacct.acctstarttime);
""", self.email)
Expand Down
10 changes: 6 additions & 4 deletions scripts/create_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
from
radius.radacct
where
((month(radius.radacct.acctstarttime) = month(date_sub(now(),interval 1 month))) and
(year(radius.radacct.acctstarttime) = year(date_sub(now(),interval 1 month))))
radius.radacct.acctstarttime BETWEEN
DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') AND
LAST_DAY(NOW() - INTERVAL 1 MONTH)
group by
radius.radacct.username;
""")
Expand All @@ -33,8 +34,9 @@
from
radius.radacct
where
((month(radius.radacct.acctstarttime) = month(now())) and
(year(radius.radacct.acctstarttime) = year(now())))
radius.radacct.acctstarttime BETWEEN
DATE_FORMAT(NOW() ,'%Y-%m-01') AND
LAST_DAY(NOW())
group by
radius.radacct.username;
""")
Expand Down

0 comments on commit de4b1f6

Please sign in to comment.