influxdata / influxdb-php

influxdb-php: A PHP Client for InfluxDB, a time series database
http://influxdb.com
MIT License
432 stars 125 forks source link

Exception using getPoints on empty metric #141

Open andig opened 4 years ago

andig commented 4 years ago

PHP Warning: Invalid argument supplied for foreach() in src/InfluxDB/ResultSet.php on line 68

Getting this warning when writing against Telegraf v1 listener

andig commented 4 years ago

I think root cause is getSeries which has meanwhile been modified:

    public function getSeries()
    {
        $series = array_map(
            function ($object) {
                if (isset($object['error'])) {
                    throw new ClientException($object['error']);
                }

                return isset($object['series']) ? $object['series'] : [];
            },
            $this->parsedResults['results']
        );

        return array_shift($series);
    }

array_shift($series); on empty array will yield NULL and hence yield the notice.

andig commented 4 years ago

Seems this was changed in https://github.com/influxdata/influxdb-php/commit/ba96a93c0cbe74c0c3ca49f525de5b389c07e718. However, upgrading to 1.15, I'm getting:

In ResultSet.php line 107: Invalid statement index provided  
andig commented 4 years ago

I feel the reason is that in https://github.com/influxdata/influxdb-php/blob/4a1efb43656a4f2b390201865cfe7051c895dff7/src/InfluxDB/ResultSet.php#L74 getSeries() is called without parameter which means default value of 0.

In getSeries(), in https://github.com/influxdata/influxdb-php/blob/4a1efb43656a4f2b390201865cfe7051c895dff7/src/InfluxDB/ResultSet.php#L106 this will throw when the result does not contain any and hence no 0 series.

Is that the desired behaviour?