Link to home
Create AccountLog in
Avatar of amr-it
amr-it

asked on

Determine if a time span is within business hours

Hello you experts,

I am developing a small tool to calculate downtimes/accessibility for some nodes in our system.
The data i get are basically events like:

ID          | DateTime                    | Status
------------------------------------------------------
123456 | 2010-12-29 06:00:00 | Unavailable
123456 | 2010-12-29 09:30:12 | Available

In the given example above, the downtime for the node was 3 hours, 30 minutes and 12 seconds.

But when looking at a period of example 2 weeks, where there can be hundreds of events for a single day for one node, with just a few seconds of downtime, I need to ackumulate the downtime and if the node has been unavailable for more than 2 hours in any of the given days, I must check if it was available in the business hours (8-16).

The critieras are:
1. More than 22 hours availability per day. Node has passed.

Else

2. If downtime was more than 2 hours for one day, it should atleast have been available during the business hours. If not, the node has failed.

Do you have any interesting approches to suggest?

Kind regards,
AMR-IT
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Are the ID's related to each other (always one 'down' and one 'up' for instance)?
Avatar of amr-it
amr-it

ASKER

Yes, one node can only be in one status.
And I must group it per node and check how the availability was during the selected two week period.
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of amr-it

ASKER

Thank you for the reply.
This was the easier part, the major issue is how to check if the node fulfills the requirements.

Let's say selected time period (2w) is: 2010-12-13 to 2010-12-26
Could be that the node had these events:

123456 | 2010-12-13 06:00:00 | Unavailable |
123456 | 2010-12-13 07:30:00 | Available |
123456 | 2010-12-14 07:30:00 | Unavailable |
123456 | 2010-12-14 07:34:00 | Available |
123456 | 2010-12-14 20:11:00 | Unavailable |
123456 | 2010-12-15 02:33:00 | Available |

Meaning that the first day, 13th, the node had only a downtime of 1½ hours.
(Less than 2 hours so its ok).
The second day, the ackumulated downtime is ~6½ hours. Since the node failed the first criteria, it must be checked if it was atleast available in the business hours. And according to the events - no downtime was reported during business hours for that day.

Regards,
amr-it
The class I provided had two functions, one that gave you the duration of the downtime. The other is the duration that occurs within a certain timeframe (your working hours).

I opted to give those as an parameter, but you could also decide to have those times fixed.
So in your code, you should have something similar for this:

// for each day
TimeSpan TotalDownTime = new Timespan();
TimeSpan TotalWorkingDownTime = new Timespan();

foreach(NodeDown node in nodelist) {
  TotalDownTime.Add(node.Duration);
  TotalWorkingDownTime.Add(node.DurationWithinWorkingHours( ##8am##, ##5pm## );
}

if (TotalDownTime > new TimeSpan(2,0,0) && TotalWorkingDownTime > new TimeSpan(0,0,0)) {
  // fail
}

Open in new window

Avatar of amr-it

ASKER

The input data I have are for multiple nodes, multiple events and for several days.
Meaning there can be many events for one node for one day.

Using the class for the above example:
123456 | 2010-12-13 06:00:00 | Unavailable |
123456 | 2010-12-13 07:30:00 | Available |
123456 | 2010-12-14 07:30:00 | Unavailable |
123456 | 2010-12-14 07:34:00 | Available |
123456 | 2010-12-14 20:11:00 | Unavailable |
123456 | 2010-12-15 02:33:00 | Available |

Using the class, would give me the following result for the 14th:

ID: 123456
Start: 07:30
End: 07:34
Duration: 4 minutes
DurationWithinWorkingHours: 0

ID: 123456
Start: 20:11
End: Unknown
Duration: Unknown
DurationWithinWorkingHours: Unknown

Before requesting assistance here at experts exchange, I approached it by creating a class for the events, creating a list of all uniqueID's, then for each UniqueID - select all co-related events ordered by datetime.

Then foreach Event - determine the status of the event (available/unavailable) - if the first event is available - we are missing the unavailable event because its out of the two week selection - the start date time should then be the start of the selected period. For all the Unavailable/Available pair events I get, I ackumulated the downtime.

But then I got stuck, because I needed to match the downtime to a date and also figure out how much downtime it was during working hours.
I cannot write a complete working code for you (lacking time in the office), but with Linq, you should be able to make selections on node-ID's and/or specific timeframes.

You can use the result to calculate the downtime and proceed from there.
Avatar of amr-it

ASKER

Good solution that solved the problem with a few changes.