OK, Sooo… here’s what I have so far:
The first task is to calculate the time you should have been running, in this shift, up til now.
My idea was to set up 2 tables, pretty much identical:
Shifts
id - integer
starttime - time
endtime - time
Breaks
id - integer
starttime - time
endtime - time
NOTE: Let me say this now- everything I’m posting is built around MySQL. If you’re not using mysql… the data types, functions, etc probably won’t be the same.
Ok, so the query I came up with to get the current amount of run time is:
SELECT
(time_to_sec(current_time-s.starttime)-time_to_sec(
(
SELECT CASE WHEN count(*)>0 THEN b.endtime-b.starttime ELSE 0 END FROM breaks b WHERE b.starttime>s.starttime and b.starttime<current_time and b.endtime<current_time
)+(
SELECT CASE WHEN count(*)>0 THEN current_time-b.starttime ELSE 0 END from breaks b WHERE b.starttime<current_time and b.endtime>current_time
)
))/60 FROM shifts s WHERE current_time>s.starttime and current_time<s.endtime;
I’ll explain a bit:
To get the current run time, you need 1) NOW-Shift.Startime. Then you need to remove 2) the breaks that have completed, and 3) the point up til now in a break that you might be in. My numbering 1, 2, & 3 corrospond more or less to each SELECT you see up there (though the first select actually does a bit more, adding the subqueries and all).
So, if you study the query carefully, hopefully you can see how this is happening. The query is a little “noisy”, cause I threw in the CASE statements. Their function is to provide a fallback value. That is, should you be in a position where there were no breaks already complete, the first sub query would normally return no rows. This won’t work in the calculation, so we have to qualify it by saying “if there rows returned (count=0), return ‘0’”. Also, you can see at the end that I’m dividing by 60 to get it into minutes… you could also choose to store it however you want and convert it later.
OK, so now that we have the current run time, you can calculate the efficency on the fly. There’s a few ways to log the history… the “realtime” group could update the last row of a table (and you could select that last row in PMI for status), and then another group would simply insert a row every hour, or you could update one row in a table and then use sql to store it to a history table every hour (INSERT INTO history SELECT * FROM status).
Anyhow, let me know what you think of all this, and we can go from there.
Regards,