Skip to content

Commit

Permalink
Prevent divide by zero error
Browse files Browse the repository at this point in the history
When there were no hits or misses against a node, the result being returned was NaN as a result of a divide by zero issue. This approach converts the NaN result to a zero.
  • Loading branch information
erikr-tempworks authored and kodiakhq[bot] committed Oct 4, 2021
1 parent 0a6d218 commit 37dec51
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private static void Main(string[] args)
channel = "Hit Rate",
unit = PRTGUnit.Percent,
Float=1,
value = (Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_hits")).Value) / (Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_hits")).Value) + Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_misses")).Value))).ToString()
value = SafeGetFloat(() => (Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_hits")).Value) / (Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_hits")).Value) + Convert.ToDouble(statsInfo.SingleOrDefault(i => i.Key.Equals("keyspace_misses")).Value))).ToString())
}
},
{
Expand Down Expand Up @@ -405,5 +405,20 @@ private static string SafeGetInt32(Func<string> func)
return "0";
}
}

private static string SafeGetFloat(Func<string> func)
{
try
{
var result = func();
return result.Equals("NaN", StringComparison.InvariantCultureIgnoreCase) ? "0" : result;

}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
return "0";
}
}
}
}

0 comments on commit 37dec51

Please sign in to comment.