influxdata / influxdb-client-js

InfluxDB 2.0 JavaScript client
https://influxdata.github.io/influxdb-client-js/
MIT License
326 stars 70 forks source link

there is no way to set the timout limit #441

Closed mirend2002 closed 2 years ago

mirend2002 commented 2 years ago

since we can not provide the timeout limit "RequestTimedOutError: Request timed out" return from the system. this happens wen there is massive data load to process. In my case i try to get 15days power consumption data query out from the system.

public getCurrentStatInfoDAO( _nodeRegistryId: any, _startTimeStamp: any, _endTimeStamp: any, _timeGap: number ): IPromise<any[]> { const defer = Q.defer(); const start = now(); const _logger = Logger.getLogger('[CurrentStatDAO] '); const end = now(); _logger.debug( 'Get influx current stat info execution [Start] ', start.toFixed(3), 'ms, [End] ', end.toFixed(3), 'ms, [Duration] ', (end - start).toFixed(3), 'ms' ); const queryApi = new InfluxDB({url, token}).getQueryApi(org);

const node_reg_ids = _nodeRegistryId.split(',');
let index = 0;
let node = '';
while (index < node_reg_ids.length) {
  if (index === node_reg_ids.length - 1) {
    node = node + '"' + node_reg_ids[index] + '"';
  } else {
    node = node + '"' + node_reg_ids[index] + '",';
  }
  index++;
}

const current_agg =
  'from(bucket: "stat_db")' +
  '|> range(start:' +
  _startTimeStamp +
  ', stop:' +
  _endTimeStamp +
  ')' +
  '|> filter(fn: (r) => contains(value: r.node_reg_id, set: [' +
  node +
  ']) and' +
  ' r._measurement == "currentstats" and r._field == "value_unit") ' +
  '|> group(columns: ["phase", "node_reg_id"]) ' +
  '|> aggregateWindow(every: ' +
  _timeGap +
  'm, fn: sum) ' +
  '|> fill(value: 0.0)' +
  '|> yield(name: "current_agg1")';

const power_rate =
  current_agg +
  '|> map(fn: (r) => ({r with value_unit: r.value_unit})) ' +
  '|> rename(columns: {_value: "power_rate"}) ' +
  '|> yield(name: "power rate")';

const power_consumption =
  power_rate +
  '|> integral(column: "power_rate", unit: 1m) ' +
  '|> rename(columns: {power_rate: "power_consumption"})' +
  '|> yield(name: "power consumption")';

queryApi
  .collectRows(power_consumption)
  .then((data: any) => {
    _logger.debug(
      'getCurrentStatInfoDAO() result: ' + JSON.stringify(data)
    );
    const result: any = [];
    node_reg_ids.forEach((node_reg_id: any, index: any) =>
      result.push({
        node_reg_id: node_reg_id,
        time: jsonpath.query(
          data,
          '$[?(@.result=="current_agg1" && @.node_reg_id=="' +
            node_reg_id +
            '")]._time'
        ),
        power_rate: jsonpath.query(
          data,
          '$[?(@.result=="current_agg1" && @.node_reg_id=="' +
            node_reg_id +
            '")]._value'
        ),
        power_consumption: jsonpath.query(
          data,
          '$[?(@.result=="power consumption" && @.node_reg_id=="' +
            node_reg_id +
            '")].power_consumption'
        )[0],
      })
    );
    defer.resolve(result);
  })
  .catch((error: any) => {
    this.logger.error(
      'Error while executing getCurrentStatInfoDAO() query: ',
      error
    );
    defer.reject(error);
  });
return defer.promise as IPromise<any[]>;

}

Code works fine until its under 5 days.

sranka commented 2 years ago

Thank you @mirend2002 for the details, I assume that you are using node.js, where the default timeout is 10 seconds. You can change the timeout when creating the InfluxDB instance with:

const timeout = 30_000 // 30 seconds
const influxDB = new InfluxDB({url, token, timeout})
mirend2002 commented 2 years ago

thanks for the quick response. it worked.