Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] API Traffic.listing date range #878

Closed
andlinger opened this issue Aug 24, 2020 · 5 comments
Closed

[Feature request] API Traffic.listing date range #878

andlinger opened this issue Aug 24, 2020 · 5 comments
Assignees
Milestone

Comments

@andlinger
Copy link
Contributor

The Traffic.listing interface would be much more efficient to use if you could select the time period you want to have returned using two dates.

For example: If I want to see the last 30 days of traffic consumption and it is January 02, 2020. Then I have to either make two requests against Traffic.listing with the year 2020 and 2019 or a request without date. So I get the whole traffic history back.

@d00p d00p self-assigned this Aug 24, 2020
@d00p
Copy link
Member

d00p commented Aug 24, 2020

I see your point. Makes sense.

@d00p
Copy link
Member

d00p commented Nov 2, 2020

As you seem to be having a requirement / testcase, would you mind testing the following patch? I'm currently a bit short regarding time :P

diff --git a/lib/Froxlor/Api/Commands/Traffic.php b/lib/Froxlor/Api/Commands/Traffic.php
index 7212e3c1..caa5c670 100644
--- a/lib/Froxlor/Api/Commands/Traffic.php
+++ b/lib/Froxlor/Api/Commands/Traffic.php
@@ -60,6 +60,10 @@ class Traffic extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
         *              optional, default empty
         * @param int $day
         *              optional, default empty
+        * @param int $date_from
+        *              optional timestamp, default empty, if specified, $year, $month and $day will be ignored
+        * @param int $date_until
+        *              optional timestamp, default empty, if specified, $year, $month and $day will be ignored
         * @param bool $customer_traffic
         *              optional, admin-only, whether to output ones own traffic or all of ones customers, default is 0 (false)
         * @param int $customerid
@@ -76,10 +80,29 @@ class Traffic extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
                $year = $this->getParam('year', true, "");
                $month = $this->getParam('month', true, "");
                $day = $this->getParam('day', true, "");
+               $date_from = $this->getParam('date_from', true, - 1);
+               $date_until = $this->getParam('date_until', true, - 1);
                $customer_traffic = $this->getBoolParam('customer_traffic', true, 0);
                $customer_ids = $this->getAllowedCustomerIds();
                $result = array();
                $params = array();
+
+               // validate parameters
+               if ($date_from >= 0 || $date_until >= 0) {
+                       $year = "";
+                       $month = "";
+                       $day = "";
+                       if ($date_from == $date_until) {
+                               $date_until = -1;
+                       }
+                       if ($date_from >= 0 && $date_until >= 0 && $date_until < $date_from) {
+                               // switch
+                               $temp_ts = $date_from;
+                               $date_from = $date_until;
+                               $date_until = $temp_ts;
+                       }
+               }
+
                // check for year/month/day
                $where_str = "";
                if (! empty($year) && is_numeric($year)) {
@@ -94,6 +117,17 @@ class Traffic extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
                        $where_str .= " AND `day` = :day";
                        $params['day'] = $day;
                }
+               if ($date_from >= 0 && $date_until >= 0) {
+                       $where_str .= " AND `stamp` BETWEEN :df AND :du";
+                       $params['df'] = $date_from;
+                       $params['du'] = $date_until;
+               } elseif ($date_from >= 0 && $date_until < 0) {
+                       $where_str .= " AND `stamp` > :df";
+                       $params['df'] = $date_from;
+               } elseif ($date_from < 0 && $date_until >= 0) {
+                       $where_str .= " AND `stamp` < :du";
+                       $params['du'] = $date_until;
+               }
 
                if (! $this->isAdmin() || ($this->isAdmin() && $customer_traffic)) {
                        $result_stmt = Database::prepare("

@andlinger
Copy link
Contributor Author

I just tried it. Seems to work perfectly. Even if you leave out one of the two parameters.

However, I noticed that the day stored in the Traffic object might be wrong. As far as I understand it, the traffic is always generated shortly after midnight. And then the current day is saved to the Traffic object. But this Traffic Object describes the previous day.

@d00p
Copy link
Member

d00p commented Nov 3, 2020

				'year' => date('Y', time()),
				'month' => date('m', time()),
				'day' => date('d', time()),
				'stamp' => time(),

It should reflect the same date as far as I can see that. Maybe it would make sense to store these value before the (long running) loop to have a sane date

@andlinger
Copy link
Contributor Author

I guess so. On our Froxlor instances the last generated traffic object is from 03.11.2020. And that can't really be because, as far as I know, the day is not over yet :D

@d00p d00p added this to the 0.10.23 milestone Dec 10, 2020
@d00p d00p closed this as completed in aba97df Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants