def fallback(): """ fallback method to use, if reading from cache data is not possible """ tsa = self.load_tsa(datestring=datestring, filterkeys=None, timedelta=timedelta) # load full tsa, and generate statistics tsastats = TimeseriesArrayStats(tsa) # generate full Stats tsastats.dump(cachedir) # save tsastats = TimeseriesArrayStats.load(cachedir, self.__index_keynames, filterkeys=filterkeys) # read specific return tsastats
def load_tsastats(self, datestring, filterkeys=None, timedelta=0, cleancache=False): """ caching version to load_tsa_raw if never called, get ts from load_tsa_raw, and afterwards dump_tsa on every consecutive call read from cached version use cleancache to remove caches parameters: datestring <str> timedelta <int> cleancache <bool> returns <TimeseriesArrayLazy> object read from cachefile or from raw data """ try: assert not_today(datestring) except AssertionError: raise DataLoggerLiveDataError("Reading from live data is not allowed") cachedir = self.__get_cachedir(datestring) cachefilename = os.path.join(cachedir, TimeseriesArrayStats.get_dumpfilename(self.__index_keynames)) def fallback(): """ fallback method to use, if reading from cache data is not possible """ tsa = self.load_tsa(datestring=datestring, filterkeys=None, timedelta=timedelta) # load full tsa, and generate statistics tsastats = TimeseriesArrayStats(tsa) # generate full Stats tsastats.dump(cachedir) # save tsastats = TimeseriesArrayStats.load(cachedir, self.__index_keynames, filterkeys=filterkeys) # read specific return tsastats if not os.path.isfile(cachefilename): logging.info("cachefile %s does not exist, fallback read from tsa archive", cachefilename) return fallback() if (os.path.isfile(cachefilename)) and (cleancache == True): logging.info("deleting cachefile %s and read from raw", cachefilename) os.unlink(cachefilename) return fallback() logging.debug("loading stored TimeseriesArrayLazy object file %s", cachefilename) try: tsastats = TimeseriesArrayStats.load(cachedir, self.__index_keynames, filterkeys=filterkeys) return tsastats except IOError: logging.error("IOError while reading from %s, using fallback", cachefilename) os.unlink(cachefilename) return fallback() except EOFError: logging.error("EOFError while reading from %s, using fallback", cachefilename) os.unlink(cachefilename) return fallback()