/** * Writes points into InfluxDB * * @param Point[] $points Array of points * @param string $precision The timestamp precision (defaults to nanoseconds) * * @return bool * @throws Exception */ public function writePoints(array $points, $precision = self::PRECISION_NANOSECONDS) { $payload = array(); foreach ($points as $point) { if (!$point instanceof Point) { throw new \InvalidArgumentException('An array of Point[] should be passed'); } $payload[] = (string) $point; } return $this->client->write($this->name, implode(PHP_EOL, $payload), $precision); }
/** * Writes points into InfluxDB * * @param Point[] $points Array of points * @param string $precision The timestamp precision (defaults to nanoseconds) * @return bool * @throws Exception */ public function writePoints(array $points, $precision = self::PRECISION_NANOSECONDS) { $payload = array_map(function (Point $point) { return (string) $point; }, $points); try { $parameters = ['url' => sprintf('write?db=%s&precision=%s', $this->name, $precision), 'database' => $this->name, 'method' => 'post']; return $this->client->write($parameters, $payload); } catch (\Exception $e) { throw new Exception($e->getMessage(), $e->getCode()); } }
/** * Write a payload into InfluxDB using the current driver. This method is similar to <tt>writePoints()</tt>, * except it takes a string payload instead of an array of Points. This is useful in the following situations: * * 1) Performing unique queries that may not conform to the current Point standard. * 2) Inserting very large set of points into a measurement where looping via array_map() actually * hurts performance as the payload may be calculated in advance by caller. * * @param string|array $payload InfluxDB payload (Or array of payloads) that conform to the Line syntax. * @param string $precision The timestamp precision (defaults to nanoseconds). * @param string|null $retentionPolicy Specifies an explicit retention policy to use when writing all points. If * not set, the default retention period will be used. This is only * applicable for the Guzzle driver. The UDP driver utilizes the endpoint * configuration defined in the server's influxdb configuration file. * @return bool * @throws \InfluxDB\Exception */ public function writePayload($payload, $precision = self::PRECISION_NANOSECONDS, $retentionPolicy = null) { try { $parameters = ['url' => sprintf('write?db=%s&precision=%s', $this->name, $precision), 'database' => $this->name, 'method' => 'post']; if ($retentionPolicy !== null) { $parameters['url'] .= sprintf('&rp=%s', $retentionPolicy); } return $this->client->write($parameters, $payload); } catch (Exception $e) { throw new InfluxDBException($e->getMessage(), $e->getCode()); } }