Skip to content
This repository
Browse code

Implemented "ETA" and "download completed time" features

Inspired by BenBE's a033210 but doesn't need additional
node field.

Signed-off-by: Michał Pokrywka <michal.pokrywka@gmail.com>
  • Loading branch information...
commit 72ca5e572288e56aa055bb68174cd7df3f5c1746 1 parent f959e2a
authored February 28, 2011

Showing 1 changed file with 33 additions and 18 deletions. Show diff stats Hide diff stats

  1. 51  mod_upload_progress.c
51  mod_upload_progress.c
@@ -636,9 +636,8 @@ static int reportuploads_handler(request_rec *r)
636 636
     server_rec *server = r->server;
637 637
 /**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "reportuploads_handler()");
638 638
 
639  
-    apr_size_t length, received, speed;
640  
-    time_t started_at=0;
641  
-    int done=0, err_status, found=0, param_error;
  639
+    upload_progress_node_t upload, *found;
  640
+    int param_error;
642 641
     char *response;
643 642
     DirConfig *dir = get_dir_config(r);
644 643
 
@@ -670,17 +669,11 @@ static int reportuploads_handler(request_rec *r)
670 669
     ServerConfig *config = get_server_config(server);
671 670
 
672 671
     CACHE_LOCK();
673  
-    upload_progress_node_t *node = find_node(r, id);
674  
-    if (node != NULL) {
  672
+    found = find_node(r, id);
  673
+    if (found) {
675 674
         up_log(APLOG_MARK, APLOG_DEBUG, 0, server,
676 675
                      "Node with id=%s found for report", id);
677  
-        received = node->received;
678  
-        length = node->length;
679  
-        done = node->done;
680  
-        speed = node->speed;
681  
-        started_at = node->started_at;
682  
-        err_status = node->err_status;
683  
-        found = 1;
  676
+        memcpy(&upload, found, sizeof(upload));
684 677
     } else {
685 678
         up_log(APLOG_MARK, APLOG_DEBUG, 0, server,
686 679
                      "Node with id=%s not found for report", id);
@@ -702,14 +695,36 @@ static int reportuploads_handler(request_rec *r)
702 695
 
703 696
     if (!found) {
704 697
         response = apr_psprintf(r->pool, "{ \"state\" : \"starting\", \"uuid\" : \"%s\" }", id);
705  
-    } else if (err_status >= HTTP_BAD_REQUEST  ) {
706  
-        response = apr_psprintf(r->pool, "{ \"state\" : \"error\", \"status\" : %d, \"uuid\" : \"%s\" }", err_status, id);
707  
-    } else if (done) {
708  
-        response = apr_psprintf(r->pool, "{ \"state\" : \"done\", \"size\" : %d, \"speed\" : %d, \"started_at\": %d, \"uuid\" : \"%s\" }", length, speed, started_at, id);
709  
-    } else if ( length == 0 && received == 0 ) {
  698
+    } else if (upload.err_status >= HTTP_BAD_REQUEST  ) {
  699
+        response = apr_psprintf(r->pool, "{ "
  700
+            "\"state\": \"error\", "
  701
+            "\"status\": %d, "
  702
+            "\"uuid\": \"%s\" "
  703
+            "}", upload.err_status, id);
  704
+    } else if (upload.done) {
  705
+        response = apr_psprintf(r->pool, "{ "
  706
+            "\"state\": \"done\", "
  707
+            "\"size\": %" APR_SIZE_T_FMT ", "
  708
+            "\"speed\": %" APR_SIZE_T_FMT ", "
  709
+            "\"started_at\": %u, "
  710
+            "\"completed_at\": %u, "
  711
+            "\"uuid\": \"%s\" "
  712
+            "}", upload.length, upload.speed, upload.started_at, upload.updated_at, id);
  713
+    } else if (upload.received == 0) {
710 714
         response = apr_psprintf(r->pool, "{ \"state\" : \"starting\", \"uuid\" : \"%s\" }", id);
711 715
     } else {
712  
-        response = apr_psprintf(r->pool, "{ \"state\" : \"uploading\", \"received\" : %d, \"size\" : %d, \"speed\" : %d, \"started_at\": %d, \"uuid\" : \"%s\" }", received, length, speed, started_at, id);
  716
+        time_t eta = 0, t = time(NULL);
  717
+        if (upload.speed > 0) eta = upload.started_at + upload.length / upload.speed;
  718
+        if (eta <= t) eta = t + 1;
  719
+        response = apr_psprintf(r->pool, "{ "
  720
+            "\"state\": \"uploading\", "
  721
+            "\"received\": %" APR_SIZE_T_FMT ", "
  722
+            "\"size\": %" APR_SIZE_T_FMT ", "
  723
+            "\"speed\": %" APR_SIZE_T_FMT ", "
  724
+            "\"started_at\": %u, "
  725
+            "\"eta\": %u, "
  726
+            "\"uuid\": \"%s\" "
  727
+            "}", upload.received, upload.length, upload.speed, upload.started_at, eta, id);
713 728
     }
714 729
 
715 730
     char *completed_response;

0 notes on commit 72ca5e5

Please sign in to comment.
Something went wrong with that request. Please try again.