/** * @file SpdbPbarHandler2.cc */ #include #include #include #include //------------------------------------------------------------------------ SpdbPbarHandler2:: SpdbPbarHandler2(const std::string &spdb) : _url(spdb), _chunkValidTime(0), _chunkTimeWritten(0) { } //------------------------------------------------------------------------ SpdbPbarHandler2:: SpdbPbarHandler2(const std::string &spdb, const SpdbPbarMetadata2 &md) : SpdbPbarMetadata2(md), _url(spdb), _chunkValidTime(0), _chunkTimeWritten(0) { } //------------------------------------------------------------------------ SpdbPbarHandler2::~SpdbPbarHandler2(void) { } //------------------------------------------------------------------------ bool SpdbPbarHandler2::read(const time_t &genTime) { return _readExisting(genTime, "Exact"); } //------------------------------------------------------------------------ bool SpdbPbarHandler2::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")) { return true; // if (pbarSet()) // { // return true; // } } } LOG(DEBUG) << "No database entries with thresholds set"; return false; } //------------------------------------------------------------------------ bool SpdbPbarHandler2::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 SpdbPbarHandler2::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 SpdbPbarHandler2::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 SpdbPbarHandler2::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 SpdbPbarHandler2::_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 SpdbPbarHandler2::_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