double SzbaseWrapper::get_avg( const std::string& param , time_t time , ProbeType type ) const throw( szbase_init_error, szbase_get_value_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); TParam* tparam = IPKContainer::GetObject()->GetParam( convert_string( base_name + ":" + param ) ); if( !tparam ) throw szbase_get_value_error( "Cannot get value from param " + param + ", param not found" ); sz4::weighted_sum<double, unsigned> sum; try { base->get_weighted_sum( tparam , unsigned( time ) , unsigned( next( time , type , 1 ) ) , type.get_szarp_pt() , sum ); } catch( sz4::exception& e ) { throw szbase_get_value_error( "Cannot get value from param " + param + ": " + e.what() ); } return sz4::scale_value(sum.avg(), tparam); }
time_t SzbaseWrapper::get_latest( const std::string& param , ProbeType type ) const throw( szbase_init_error, szbase_get_value_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); bool ok; std::wstring error; /** * It looks like szbase has different aruments for "search all" in * case of 10sec and other probles */ time_t start = type == ProbeType( ProbeType::Type::LIVE ) ? -1 : std::numeric_limits<time_t>::max(); auto t = Szbase::GetObject()->Search( convert_string( base_name + ":" + param ) , start , time_t(-1) , -1 , type.get_szarp_pt() , ok , error ); if( !ok ) throw szbase_get_value_error("Cannot get latest time of param " + param + ": " + SC::S2A(error) ); /** * Round by hand because Szbase::Search returns probes rounded to * either 10min or 10sec, not to exact pt */ return round( t , type ); }
time_t SzbaseWrapper::get_latest( const std::string& param , ProbeType type ) const throw( szbase_init_error, szbase_get_value_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); TParam* tparam = IPKContainer::GetObject()->GetParam( convert_string( base_name + ":" + param ) ); if( !tparam ) throw szbase_get_value_error( "Cannot get latest time of param " + param + ", param not found" ); unsigned t; try { base->get_last_time( tparam, t ); } catch( sz4::exception& e) { throw szbase_get_value_error( "Cannot get latest time of param " + param + ": " + e.what() ); } /** * Round by hand because search returns probes rounded to * either 10min or 10sec, not to exact pt */ return t == unsigned( -1 ) ? -1 : round( t , type ); }
std::string SzbaseWrapper::search_data( const std::string& param , const std::string& from , const std::string& to , TimeType time_type , SearchDir dir , ProbeType pt ) const throw( szbase_init_error, szbase_param_not_found_error, szbase_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); TParam* tparam = IPKContainer::GetObject()->GetParam( convert_string( base_name + ":" + param ) ); if( !tparam ) throw szbase_param_not_found_error( "Param " + param + ", does not exist." ); std::string result; switch (time_type) { case TimeType::NANOSECOND: result = search_data_helper<sz4::nanosecond_time_t>( base , tparam , from , to , dir , pt ); break; case TimeType::SECOND: result = search_data_helper<sz4::second_time_t> ( base , tparam , from , to , dir , pt ); break; } purge_cache(); return result; }
void SzbaseWrapper::remove_param(const std::string& base, const std::string& param) throw( szbase_param_not_found_error , szbase_init_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); auto tparam = IPKContainer::GetObject()->GetParam( convert_string( base + ":" + param ), false ); if( tparam ) this->base->remove_param( tparam ); IPKContainer::GetObject()->RemoveExtraParam( convert_string ( base ) , convert_string( param ) ); }
std::string SzbaseWrapper::add_param( const std::string& param , const std::string& base , const std::string& formula , const std::string& token , const std::string& type , int prec , unsigned start_time) throw( szbase_invalid_name , szbase_formula_invalid_syntax, szbase_init_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); std::wstring _param = convert_string( param ); std::wstring _token = convert_string( token ); std::wstring _formula = convert_string( formula ); std::wstring new_param_name; if ( !create_param_name( _param , _token , new_param_name ) ) throw szbase_invalid_name(param + " in not valid user defined param name"); std::vector<std::wstring> strings; if( !extract_strings_from_formula( _formula , strings ) ) throw szbase_formula_invalid_syntax("formula cannot be parsed"); for( auto& param : strings ) { std::wstring new_name; if ( !create_param_name_in_formula( param , _token , new_name ) ) continue; auto i = _formula.find( param ); assert( i != std::wstring::npos ); _formula.replace( i , param.size() , new_name ); } TParam::FormulaType formula_type; if( type == "av" ) formula_type = TParam::LUA_AV; else if( type == "va" ) formula_type = TParam::LUA_VA; auto tparam = new TParam(NULL, NULL, L"", formula_type, TParam::P_LUA); tparam->SetName(new_param_name); tparam->SetPrec(prec); tparam->SetTimeType(TParam::NANOSECOND); ///XXX: tparam->SetLuaScript(SC::S2U(_formula).c_str()); tparam->SetLuaStartDateTime(start_time); IPKContainer::GetObject()->AddExtraParam( convert_string ( base ) , tparam ); return reinterpret_cast<const char*>(SC::S2U(new_param_name).c_str()); }
SzbaseObserverToken SzbaseWrapper::register_observer( const std::string& param , std::function<void( void )> callback ) throw( szbase_init_error , szbase_param_not_found_error , szbase_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); TParam* tparam = IPKContainer::GetObject()->GetParam( convert_string( base_name + ":" + param ) ); if( !tparam ) throw szbase_param_not_found_error( "Param " + param + ", does not exist." ); return std::make_shared<SzbaseObserverImpl>( convert_string( base_name + ":" + param ) , IPKContainer::GetObject() , base , callback ); }
double SzbaseWrapper::get_avg_no_sync( const std::string& param , time_t time , ProbeType type ) const throw( szbase_init_error, szbase_get_value_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); bool is_fixed, ok; std::wstring error; double val = Szbase::GetObject()->GetValue( convert_string( base_name + ":" + param ) , time , type.get_szarp_pt() , type.get_len() , &is_fixed , ok , error ); if( !ok ) throw szbase_get_value_error("Cannot get value from param " + param + ": " + SC::S2A(error) ); return val; }
std::string SzbaseWrapper::get_data( const std::string& param , const std::string& from , const std::string& to , ValueType value_type , TimeType time_type , ProbeType pt ) const throw( szbase_init_error, szbase_param_not_found_error, szbase_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); std::ostringstream ss; TParam* tparam = IPKContainer::GetObject()->GetParam( convert_string( base_name + ":" + param ) ); if( !tparam ) throw szbase_param_not_found_error( "Param " + param + ", does not exist." ); try{ switch (time_type) { case TimeType::NANOSECOND: ::get_data<sz4::nanosecond_time_t>( base , tparam , from , to , value_type , pt.get_szarp_pt() , ss ); break; case TimeType::SECOND: ::get_data<sz4::second_time_t> ( base , tparam , from , to , value_type , pt.get_szarp_pt() , ss ); break; } } catch ( sz4::exception& e ) { throw szbase_error( "Cannot get data for param " + param + ": " + e.what() ); } purge_cache(); return ss.str(); }
double SzbaseWrapper:: get_avg_no_sync( const std::string& param , time_t start , time_t end ) const throw( szbase_init_error, szbase_get_value_error ) { if( !SzbaseWrapper::is_initialized() ) throw szbase_init_error("Szbase not initialized"); int len = end - start; bool is_fixed, ok; std::wstring error; double val = Szbase::GetObject()->GetValue( convert_string( base_name + ":" + param ) , start , PT_CUSTOM , len , &is_fixed , ok , error ); if( !ok ) throw szbase_get_value_error("Cannot get value from param " + param + ": " + SC::S2A(error) ); return val; }