Skip to content

Instantly share code, notes, and snippets.

@deniszh
Created September 22, 2021 21:27
Show Gist options
  • Save deniszh/048dc2aa195c6f2f116b6db8c8a539ba to your computer and use it in GitHub Desktop.
Save deniszh/048dc2aa195c6f2f116b6db8c8a539ba to your computer and use it in GitHub Desktop.
compressgaps.patch
--- functions.py.bak
+++ functions.py
@@ -2139,6 +2139,8 @@
"""
results = []
for series in seriesList:
+ # applying _compressGaps() first
+ series = _compressGaps(series)
newValues = []
prev = None
for val in series:
@@ -2183,6 +2185,8 @@
"""
results = []
for series in seriesList:
+ # applying _compressGaps() first
+ series = _compressGaps(series)
newValues = []
prev = None
step = series.step
@@ -2339,6 +2343,36 @@
]
+def _compressGaps(series):
+ """
+ Trying to intelligently remove None's from series,
+ recalculating start, stop and step
+ """
+ start = series.start
+ step = series.step
+ consolidate = series.consolidationFunc
+ tags = series.tags
+ xFilesFactor = series.xFilesFactor
+ pathExpression = series.pathExpression
+
+ newStart = start
+ newValues = []
+ seenValue = False
+ initNones = 0
+ lastSeen = 0
+ for i,value in enumerate(series):
+ if value:
+ newValues.append(value)
+ seenValue = True
+ lastSeen = i
+ elif not seenValue:
+ newStart = newStart + step
+ initNones = initNones + 1
+ newEnd = start + step * lastSeen
+ newStep = (newEnd - newStart) // (len(newValues) - 1)
+ return TimeSeries(series.name,newStart,newEnd,newStep,newValues,consolidate=consolidate,tags=tags,xFilesFactor=xFilesFactor,pathExpression=pathExpression)
+
+
def nonNegativeDerivative(requestContext, seriesList, maxValue=None, minValue=None):
"""
Same as the derivative function above, but ignores datapoints that trend
@@ -2361,6 +2395,8 @@
results = []
for series in seriesList:
+ # applying _compressGaps() first
+ series = _compressGaps(series)
newValues = []
prev = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment