Commit 5fc045ca authored by Claes Sjofors's avatar Claes Sjofors

Sev mysql, policy when selecting values with deadband changed

parent 6618e0c6
...@@ -1482,6 +1482,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo ...@@ -1482,6 +1482,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
char orderby_part[80]; char orderby_part[80];
char jumpstr[40]; char jumpstr[40];
char where_part[200]; char where_part[200];
int rows;
if ( starttime && starttime->tv_sec == 0 && starttime->tv_nsec == 0) if ( starttime && starttime->tv_sec == 0 && starttime->tv_nsec == 0)
starttime = 0; starttime = 0;
...@@ -1620,7 +1621,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo ...@@ -1620,7 +1621,7 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
strcpy( column_part, "time,value"); strcpy( column_part, "time,value");
if ( options & pwr_mSevOptionsMask_UseDeadBand) if ( options & pwr_mSevOptionsMask_UseDeadBand)
strcpy( jumpstr, "or jump = 1"); strcpy( jumpstr, ""); // "or jump = 1" removed
else else
strcpy( jumpstr, ""); strcpy( jumpstr, "");
...@@ -1634,105 +1635,120 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo ...@@ -1634,105 +1635,120 @@ int sev_dbms::get_values( pwr_tStatus *sts, pwr_tOid oid, pwr_tMask options, flo
strcpy( orderby_part, "time"); strcpy( orderby_part, "time");
} }
// 'where' part for ( int k = 0; k < 2; k++) {
if ( options & pwr_mSevOptionsMask_ReadOptimized) { // 'where' part
if ( starttime && endtime) { if ( options & pwr_mSevOptionsMask_ReadOptimized) {
if ( div == 1) { if ( starttime && endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime) if ( div == 1) {
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec); if ( options & pwr_mSevOptionsMask_PosixTime)
else sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr); else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
}
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld and time <= %ld",
div, jumpstr, (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s' and time <= '%s'",
div, jumpstr, starttimstr, endtimstr);
}
} }
else { else if ( starttime) {
if ( options & pwr_mSevOptionsMask_PosixTime) if ( div == 1) {
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld and time <= %ld", if ( options & pwr_mSevOptionsMask_PosixTime)
div, jumpstr, (long int)starttime->tv_sec, (long int)endtime->tv_sec); sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s' and time <= '%s'", sprintf( where_part, "where time >= '%s'", starttimstr);
div, jumpstr, starttimstr, endtimstr); }
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld", div, jumpstr, (long int)starttime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s'", div, jumpstr, starttimstr);
}
} }
} else if ( endtime) {
else if ( starttime) { if ( div == 1) {
if ( div == 1) { if ( options & pwr_mSevOptionsMask_PosixTime)
if ( options & pwr_mSevOptionsMask_PosixTime) sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec); else
else sprintf( where_part, "where time <= '%s'", endtimstr);
sprintf( where_part, "where time >= '%s'", starttimstr); }
else {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time <= %ld", div, jumpstr, (long int)endtime->tv_sec);
else
sprintf( where_part, "where (id %% %d = 0 %s) and time <= '%s'", div, jumpstr, endtimstr);
}
} }
else { else {
if ( options & pwr_mSevOptionsMask_PosixTime) if ( div == 1)
sprintf( where_part, "where (id %% %d = 0 %s) and time >= %ld", div, jumpstr, (long int)starttime->tv_sec); strcpy( where_part, "");
else else
sprintf( where_part, "where (id %% %d = 0 %s) and time >= '%s'", div, jumpstr, starttimstr); sprintf( where_part, "where id %% %d = 0 %s", div, jumpstr);
} }
} }
else if ( endtime) { else {
if ( div == 1) { // Not read optimized
if ( options & pwr_mSevOptionsMask_PosixTime) if ( starttime && endtime) {
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec); if ( options & pwr_mSevOptionsMask_PosixTime)
else sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
sprintf( where_part, "where time <= '%s'", endtimstr); else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr);
} }
else { else if ( starttime) {
if ( options & pwr_mSevOptionsMask_PosixTime) if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where (id %% %d = 0 %s) and time <= %ld", div, jumpstr, (long int)endtime->tv_sec); sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec);
else else
sprintf( where_part, "where (id %% %d = 0 %s) and time <= '%s'", div, jumpstr, endtimstr); sprintf( where_part, "where time >= '%s'", starttimstr);
}
else if ( endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec);
else
sprintf( where_part, "where time <= '%s'", endtimstr);
} }
}
else {
if ( div == 1)
strcpy( where_part, "");
else
sprintf( where_part, "where id %% %d = 0 %s", div, jumpstr);
}
}
else {
// Not read optimized
if ( starttime && endtime) {
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time >= %ld and time <= %ld", (long int)starttime->tv_sec, (long int)endtime->tv_sec);
else else
sprintf( where_part, "where time >= '%s' and time <= '%s'", starttimstr, endtimstr); strcpy( where_part, "");
}
sprintf( query, "select %s from %s %s order by %s limit %d",
column_part, item.tablename, where_part, orderby_part, maxsize*2);
rc = mysql_query( m_env->con(), query);
if (rc) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "Get Values: %s\n", mysql_error(m_env->con()));
*sts = SEV__DBERROR;
return 0;
} }
else if ( starttime) {
if ( options & pwr_mSevOptionsMask_PosixTime) result = mysql_store_result( m_env->con());
sprintf( where_part, "where time >= %ld", (long int)starttime->tv_sec); if ( !result) {
else printf("In %s row %d:\n", __FILE__, __LINE__);
sprintf( where_part, "where time >= '%s'", starttimstr); printf( "GetValues Result Error\n");
*sts = SEV__DBERROR;
return 0;
} }
else if ( endtime) { rows = mysql_num_rows( result);
if ( options & pwr_mSevOptionsMask_PosixTime)
sprintf( where_part, "where time <= %ld", (long int)endtime->tv_sec); if ( k == 0 &&
else options & pwr_mSevOptionsMask_UseDeadBand &&
sprintf( where_part, "where time <= '%s'", endtimstr); rows < maxsize / 3 &&
rows != 0 &&
div != 1) {
// Try another read with smaller div
printf( "Retry: %d old dev: %d, new div %d\n", rows, div, div * rows / maxsize + 1);
if ( div == div * rows / maxsize + 1)
break;
div = div * rows / maxsize + 1;
mysql_free_result( result);
} }
else else break;
strcpy( where_part, "");
}
sprintf( query, "select %s from %s %s order by %s",
column_part, item.tablename, where_part, orderby_part);
rc = mysql_query( m_env->con(), query);
if (rc) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "Get Values: %s\n", mysql_error(m_env->con()));
*sts = SEV__DBERROR;
return 0;
}
result = mysql_store_result( m_env->con());
if ( !result) {
printf("In %s row %d:\n", __FILE__, __LINE__);
printf( "GetValues Result Error\n");
*sts = SEV__DBERROR;
return 0;
} }
int rows = mysql_num_rows( result);
int bufrows = rows; int bufrows = rows;
if ( options & pwr_mSevOptionsMask_ReadOptimized) { if ( options & pwr_mSevOptionsMask_ReadOptimized) {
*tbuf = (pwr_tTime *) calloc( bufrows, sizeof(pwr_tTime)); *tbuf = (pwr_tTime *) calloc( bufrows, sizeof(pwr_tTime));
...@@ -4062,10 +4078,10 @@ void sev_dbms::add_cache( int item_idx) ...@@ -4062,10 +4078,10 @@ void sev_dbms::add_cache( int item_idx)
case pwr_eType_Int32: case pwr_eType_Int32:
if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandMeanValue) if ( m_items[item_idx].options & pwr_mSevOptionsMask_DeadBandMeanValue)
m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Mean, m_items[item_idx].deadband, m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Mean, m_items[item_idx].deadband,
m_items[item_idx].scantime); m_items[item_idx].scantime/2);
else else
m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Point, m_items[item_idx].deadband, m_items[item_idx].cache = new sev_valuecache_double( sev_eCvType_Point, m_items[item_idx].deadband,
m_items[item_idx].scantime); m_items[item_idx].scantime/2);
m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx); m_items[item_idx].cache->set_write_cb( write_db_cb, this, item_idx);
break; break;
case pwr_eType_Boolean: case pwr_eType_Boolean:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment