/** * @file SpdbGenBasedThreshHandler.cc */ #include #include #include #include #include #include #include //------------------------------------------------------------------------ SpdbGenBasedThreshHandler:: SpdbGenBasedThreshHandler(const std::string &spdb) : _url(spdb), _chunkValidTime(0), _chunkTimeWritten(0) { } //------------------------------------------------------------------------ SpdbGenBasedThreshHandler:: SpdbGenBasedThreshHandler(const std::string &spdb, const SpdbGenBasedMetadata &md) : SpdbGenBasedMetadata(md), _url(spdb), _chunkValidTime(0), _chunkTimeWritten(0) { } //------------------------------------------------------------------------ SpdbGenBasedThreshHandler::~SpdbGenBasedThreshHandler(void) { } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::read(const time_t &genTime) { return _readExisting(genTime, "Exact"); } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::readBestOlder(const time_t &genTime, int maxSecondsBack) { std::vector times = timesInRange(genTime - maxSecondsBack, genTime-1); if (times.empty()) { LOG(DEBUG) << "No SPDB data found in data base " << _url << " between " << DateTime::strn(genTime-maxSecondsBack) << " and " << DateTime::strn(genTime-1); return false; } // filter to times with same hour DateTime dt(genTime); int hour = dt.getHour(); std::vector::iterator i; for (i=times.begin(); i!=times.end();) { dt = DateTime(*i); int ithHour = dt.getHour(); if (ithHour == hour) { ++i; } else { i = times.erase(i); } } int numTimes = (int)times.size(); for (int i=numTimes-1; i>=0; --i) { if (_readExisting(times[i], "Older")) { if (thresholdsSet()) { return true; } } } LOG(DEBUG) << "No database entries with thresholds set"; return false; } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::readNewestInRange(const time_t &t0, const time_t &t1) { std::vector times = timesInRange(t0, t1); if (times.empty()) { LOG(DEBUG) << "No SPDB data found in data base " << _url << " between " << DateTime::strn(t0) << " and " << DateTime::strn(t1); return false; } time_t tbest = *(times.rbegin()); return read(tbest); } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::readClosestToTargetInRange(const time_t &target, const time_t &t0, const time_t &t1) { std::vector times = timesInRange(t0, t1); if (times.empty()) { LOG(DEBUG) << "No SPDB data found in data base " << _url << " between " << DateTime::strn(t0) << " and " << DateTime::strn(t1); return false; } time_t bestT = times[0]; int bestDelta = times[0] - target; if (bestDelta < 0) { bestDelta = -bestDelta; } for (size_t i=1; i SpdbGenBasedThreshHandler::timesInRange(const time_t &t0, const time_t &t1) const { DsSpdb D; std::vector ret; if (D.compileTimeList(_url, t0, t1)) { LOG(ERROR) << "Compiling time list"; return ret; } ret = D.getTimeList(); return ret; } //------------------------------------------------------------------ std::vector SpdbGenBasedThreshHandler::timesInRangeWithMatchingHour(const time_t &t0, const time_t &t1) const { DsSpdb D; std::vector ret; if (D.compileTimeList(_url, t0, t1)) { return ret; } ret = D.getTimeList(); std::vector::iterator i; DateTime dt(t1); int wantedHour = dt.getHour(); for (i=ret.begin(); i!=ret.end(); ) { dt = DateTime(*i); if (dt.getHour() == wantedHour) { ++i; } else { i = ret.erase(i); } } return ret; } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::average(const time_t &recentTime, int daysBack, double coldstartThresh, SpdbGenBasedMetadata &out) { vector times = timesInRangeWithMatchingHour(recentTime - daysBack*24*3600, recentTime); out = *this; // the thresholds are all wrong, everything else is ok. int nTiles = getNumTiles(); vector ltimes = getLeadSeconds(); int numObarThresh = getNumObarThresh(); HistGenTime hist(recentTime, ltimes, numObarThresh, nTiles); if (times.empty()) { LOG(ERROR) << "No history found"; return false; } for (size_t i=0; i info; for (size_t l=0; l ave = hist.getAverages(ltSec, o); for (size_t i=0; i= 0) { out.setThresh(ltSec, o, i, ave[i]); } else { out.setThreshColdstart(ltSec, o, i); } } } } return true; } //------------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::_readExisting(const time_t &genTime, const std::string &description) { DsSpdb D; if (D.getExact(_url, genTime) != 0) { return false; } else { return _load(D, description); } } //------------------------------------------------------------------ bool SpdbGenBasedThreshHandler::_load(DsSpdb &s, const std::string &description) { const vector &chunks = s.getChunks(); int nChunks = static_cast(chunks.size()); if (nChunks <= 0) { return false; } if (nChunks > 1) { LOG(WARNING) << "Too many chunks " << nChunks << " expected 1"; return false; } vector lchunks; for (int i=0; i