Search code examples
javamathprogressjprogressbar

Calculate a percentage progression based on events


I have to implement a progressbar that show the progress of a report generation, the problem is that i have to represent the percentage of the report progression based on event generated by the generation but without first knowing the total amount of event, so for example the report generation sends the following event(phase) :

 - Starting Report generation
 - Start Query execution
 - End Query execution
 - Start Report Rendering
 - End Report Rendering
 - End Report Generation

In this example there are in total 3 task and 6 events, i don't know the total number of task but i know that the total number of events is twice respect the number of tasks.

I don't know which events are present and how many they are , but i have to represent the progression with a progress bar so with a number between 0 and 100 .

How can it be calculated ? Whats the best way to represents the progression ?


Solution

  • Probably the best you can do is to abandon the idea of actual progress bar altogether, and sue some indeterminate progress indicator instead. A "marquee" or "bouncing" progress bar is typically used in this scenario on desktop systems.

    Another option - if you really want to show something progress-like - is to employ some heuristics and just (partially) fake it. You don't really have many information here so it would have to be pretty clever, and require a lot of testing on real cases. Here's some suggestions:

    • Increase the progress based on the nesting level of currently processed task. Basically, the idea is that the more fine grained the task is, the less it contributes to the overall progress. By task nesting I mean, of course, the hierarchy implied from bracketing of your Start and End messages.

    • Get a reasonable estimate of the maximum number of tasks you can expect, and adjust the speed of progress' increase based on how close you might be to finishing. For example, if you expect no more than 500 tasks, and you have processed 300 but are already displaying 80% progress, you must seriously slow down any further increase. Similarly, if you are lagging behind, you should appropriate scale up you progress increments.

    • Avoid big jumps. Even if you need to rapidly catch up, as described in previous point, do so gradually and over time. To put up a concrete example: rather than incrementing once by 10% and then stand still for 10 seconds, make 10 increments of 1% each over the course of 10 seconds.

    There are probably many more techniques you could try to use. Overally, it's quite an interesting UX- and math-related problem, but you should consider whether spending cognitive effort on it is worth the hassle. I expect that in most cases, the indeterminate progress would be absolutely fine.