I've got this method, and it's quite a big method. I look at it, and I feel... dirty. I feel that it's doing too much, and that there might be a better way to accomplish what I'm trying to.
/// <summary>
/// Calculates daily project drilling hours
/// </summary>
/// <param name="daily">Bool: True for daily, false for total project</param>
/// <param name="timeFrame">Bool: True for specific time frame, false for whole day </param>
/// <returns>Double: Drilling hours for time frame</returns>
public double CalculateDrillingHours(bool daily = false, bool timeFrame = false)
{
Logger.Info("Calculating Drilling Hours");
var query = "SELECT timestamp FROM myDatabase WHERE measured_dist = bit_loc AND rop > 0";
query = BuildQuery(query, daily, timeFrame);
var tempList = ExecuteQuery(query);
// The user needs to know if there was no data for the time frame chosen
var noData = new Exception("There was no data for the selected time frame. Please select another. (Drilling Hours)");
if (tempList.Count < 1)
{
throw noData;
}
if(!daily)
{
TotalProjectDrillingHours = Convert.ToDouble(Math.Round(TimeCalculations(ConvertStringListToDateTimeList(tempList)).TotalHours, 2, MidpointRounding.AwayFromZero));
Logger.Info("Total Drilling Hours calculated");
return TotalProjectDrillingHours;
}
DailyDrillingHours = Convert.ToDouble(Math.Round(TimeCalculations(ConvertStringListToDateTimeList(tempList)).TotalHours, 2, MidpointRounding.AwayFromZero));
Logger.Info("Daily Drilling Hours calculated");
return DailyDrillingHours;
}
This method returns 1 of 2 items: Either Daily Drilling Hours, or Total Project Drilling Hours. This was originally two methods, but I combined them because a lot of the code was being duplicated, and it made sense at the time (about 6 months ago). But now, looking at it, I'd like to improve this. Is there a better way?
Yes, I know about parameterized queries. I've already incorporated those in the final query, which would look something like this:
query = "SELECT timestamp FROM myDatabase WHERE measured_dist = bit_loc AND rop > 0 AND date = @Date AND timestamp BETWEEN @StartTime AND @EndTime";