18 #define PROGRESS_WINDOW (10)
30 TIMESTAMP stamps
[PROGRESS_WINDOW
];
34 static double getTimeDelta (void) {
35 static double res
= 10.0;
36 static int checked
= 0;
39 LIST
*var
= var_get("K8JAM-PROGRESS-TIME-DELTA"); /*FIXME: a perfectly idiotic name*/
41 TARGET
*t
= bindtarget(var
->string
);
42 pushsettings(t
->settings
);
43 t
->boundname
= search(t
->name
, &t
->time
);
44 popsettings(t
->settings
);
46 res
= atof(t
->boundname
);
47 if (res
< 0.0) res
= 0.0;
56 #define LAST_STAMP(p) ((p)->stamps[(p)->stampcount-1])
57 #define FIRST_STAMP(p) ((p)->stamps[0])
60 PROGRESS
*progress_start (int total
) {
61 PROGRESS
*p
= malloc(sizeof(PROGRESS
));
64 progress_update(p
, 0);
69 static double progress_estimate (PROGRESS
*progress
) {
75 if (progress
->stampcount
< 2) return 0.0;
76 count
= LAST_STAMP(progress
).completed
-FIRST_STAMP(progress
).completed
;
77 left
= progress
->total
-LAST_STAMP(progress
).completed
;
78 elapsed
= (double)(LAST_STAMP(progress
).when
-FIRST_STAMP(progress
).when
);
79 if (elapsed
<= 0.1) elapsed
= 0.1;
85 double progress_update (PROGRESS
*progress
, int completed
) {
87 double dd
= getTimeDelta();
90 /* only return progress every 10 seconds */
91 if (progress
->stampcount
> 0 && (dd
<= 0.00001 || difftime(now
, LAST_STAMP(progress
).when
) < dd
)) return 0.0;
92 if (progress
->stampcount
== PROGRESS_WINDOW
) {
93 memmove(progress
->stamps
, progress
->stamps
+1, sizeof(progress
->stamps
[0])*(PROGRESS_WINDOW
-1));
94 --progress
->stampcount
;
96 ++progress
->stampcount
;
97 LAST_STAMP(progress
).completed
= completed
;
98 LAST_STAMP(progress
).when
= now
;
99 return progress_estimate(progress
);