Skip to content

Commit 89da827

Browse files
committed
fix: automatically adjusting timestamp (#168)
1 parent a0271a7 commit 89da827

File tree

9 files changed

+90
-10
lines changed

9 files changed

+90
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## Unreleased
3+
### Fixes
4+
- [193](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/193) - Automatically adjusting point timestamp according to the setting of write precision.
5+
26
## 3.12.0 [2022-03-21]
37
### Features
48
- [185](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/185) - Added diagnostic server connection state getter `bool InfluxDBClient::isConnected()`

src/InfluxDbClient.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,49 @@ void InfluxDBClient::reserveBuffer(int size) {
285285
}
286286
}
287287

288-
bool InfluxDBClient::writePoint(Point & point) {
289-
if (point.hasFields()) {
290-
if(_writeOptions._writePrecision != WritePrecision::NoTime && !point.hasTime()) {
288+
void InfluxDBClient::addZerosToTimestamp(Point &point, int zeroes) {
289+
char *ts = point._timestamp, *s;
290+
point._timestamp = new char[strlen(point._timestamp) + 1 + zeroes];
291+
strcpy(point._timestamp, ts);
292+
s = point._timestamp+strlen(ts);
293+
for(int i=0;i<zeroes;i++) {
294+
*s++ = '0';
295+
}
296+
*s = 0;
297+
delete [] ts;
298+
}
299+
300+
void InfluxDBClient::checkPrecisions(Point & point) {
301+
if(_writeOptions._writePrecision != WritePrecision::NoTime) {
302+
if(!point.hasTime()) {
291303
point.setTime(_writeOptions._writePrecision);
304+
// Check different write precisions
305+
} else if(point._tsWritePrecision != WritePrecision::NoTime && point._tsWritePrecision != _writeOptions._writePrecision) {
306+
int diff = int(point._tsWritePrecision) - int(_writeOptions._writePrecision);
307+
if(diff > 0) { //point has higher precision, cut
308+
point._timestamp[strlen(point._timestamp)-diff*3] = 0;
309+
} else { //point has lower precision, add zeroes
310+
addZerosToTimestamp(point, diff*-3);
311+
}
292312
}
313+
// check someone set WritePrecision on point and not on client. NS precision is ok, cause it is default on server
314+
} else if(point.hasTime() && point._tsWritePrecision != WritePrecision::NoTime && point._tsWritePrecision != WritePrecision::NS) {
315+
int diff = int(WritePrecision::NS) - int(point._tsWritePrecision);
316+
addZerosToTimestamp(point, diff*3);
317+
}
318+
}
319+
320+
bool InfluxDBClient::writePoint(Point & point) {
321+
if (point.hasFields()) {
322+
checkPrecisions(point);
293323
String line = pointToLineProtocol(point);
294324
return writeRecord(line);
295325
}
296326
return false;
297327
}
298328

329+
330+
299331
InfluxDBClient::Batch::Batch(uint16_t size):_size(size) {
300332
buffer = new char*[size];
301333
for(int i=0;i< _size; i++) {

src/InfluxDbClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ class InfluxDBClient {
263263
// flashOnlyFull - whether to flush only full batches
264264
// Returns true if successful, false in case of any error
265265
bool flushBufferInternal(bool flashOnlyFull);
266+
// Checks precision of point and mofifies if needed
267+
void checkPrecisions(Point & point);
268+
// helper which adds zeroes to timestamo of point to increase precision
269+
static void addZerosToTimestamp(Point &point, int zeroes);
266270
};
267271

268272

src/Point.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Point::Point(const String & measurement)
3232
{
3333
_measurement = escapeKey(measurement, false);
3434
_timestamp = nullptr;
35+
_tsWritePrecision = WritePrecision::NoTime;
3536
}
3637

3738
Point::~Point() {
@@ -171,6 +172,7 @@ void Point::setTime(WritePrecision precision) {
171172
setTime((char *)nullptr);
172173
break;
173174
}
175+
_tsWritePrecision = precision;
174176
}
175177

176178
void Point::setTime(unsigned long long timestamp) {
@@ -187,7 +189,7 @@ void Point::setTime(const char *timestamp) {
187189

188190
void Point::setTime(char *timestamp) {
189191
delete [] _timestamp;
190-
timestamp = timestamp;
192+
_timestamp = timestamp;
191193
}
192194

193195
void Point::clearFields() {

src/Point.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ friend class InfluxDBClient;
8484
String _tags;
8585
String _fields;
8686
char *_timestamp;
87+
WritePrecision _tsWritePrecision;
8788
protected:
8889
// method for formating field into line protocol
8990
void putField(const String &name, const String &value);

src/WritePrecision.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef _WRITE_PRECISION_H_
2828
#define _WRITE_PRECISION_H_
2929
// Enum WritePrecision defines constants for specifying InfluxDB write prcecision
30-
enum class WritePrecision {
30+
enum class WritePrecision:uint8_t {
3131
// Specifyies that points has no timestamp (default). Server will assign timestamp.
3232
NoTime = 0,
3333
// Seconds

test/Test.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void Test::run() {
7171
testLargeBatch();
7272
testFailedWrites();
7373
testTimestamp();
74+
testTimestampAdjustment();
7475
testRetryOnFailedConnection();
7576
testRetryOnFailedConnectionWithFlush();
7677
testNonRetry();
@@ -1217,10 +1218,11 @@ void Test::testTimestamp() {
12171218
delayMicroseconds(100);
12181219
}
12191220

1220-
1221+
12211222
serverLog(Test::apiUrl, "testTimestamp");
12221223
InfluxDBClient client(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
12231224
client.setWriteOptions(WritePrecision::S, 1, 5);
1225+
waitServer(Test::managementUrl, true);
12241226
//test with no batching
12251227
TEST_ASSERT(client.validateConnection());
12261228
uint32_t timestamp;
@@ -1283,6 +1285,44 @@ void Test::testTimestamp() {
12831285
serverLog(Test::apiUrl, "testTimestamp end");
12841286
}
12851287

1288+
void Test::testTimestampAdjustment() {
1289+
TEST_INIT("testTimestampAdjustment");
1290+
InfluxDBClient client;
1291+
// test no client precision, but on point
1292+
Point point("a");
1293+
point.setTime(WritePrecision::S);
1294+
client.checkPrecisions(point);
1295+
TEST_ASSERTM(point.getTime().endsWith("000000000"),point.getTime() );
1296+
1297+
point.setTime(WritePrecision::MS);
1298+
client.checkPrecisions(point);
1299+
TEST_ASSERTM(point.getTime().endsWith("000"),point.getTime() );
1300+
1301+
//test not modified ts
1302+
point.setTime(WritePrecision::NS);
1303+
String a = point.getTime();
1304+
client.checkPrecisions(point);
1305+
TEST_ASSERTM(a == point.getTime(), point.getTime() );
1306+
1307+
// test client precision and not point
1308+
client.setWriteOptions(WriteOptions().writePrecision(WritePrecision::S));
1309+
point.setTime(WritePrecision::NoTime);
1310+
TEST_ASSERTM(!point.hasTime(), point.getTime() );
1311+
client.checkPrecisions(point);
1312+
TEST_ASSERTM(point.getTime().length() == 10, point.getTime() );
1313+
// test cut
1314+
point.setTime(WritePrecision::US);
1315+
client.checkPrecisions(point);
1316+
TEST_ASSERTM(point.getTime().length() == 10, point.getTime() );
1317+
// test extending
1318+
client.setWriteOptions(WriteOptions().writePrecision(WritePrecision::US));
1319+
point.setTime(WritePrecision::S);
1320+
client.checkPrecisions(point);
1321+
TEST_ASSERTM(point.getTime().endsWith("000000"),point.getTime() );
1322+
1323+
TEST_END();
1324+
}
1325+
12861326
void Test::testV1() {
12871327
TEST_INIT("testV1");
12881328
InfluxDBClient client;

test/Test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Test : public TestBase {
6060
static void testUserAgent();
6161
static void testFailedWrites();
6262
static void testTimestamp();
63+
static void testTimestampAdjustment();
6364
static void testHTTPReadTimeout();
6465
static void testRetryOnFailedConnection();
6566
static void testRetryOnFailedConnectionWithFlush();

test/test.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@
3434
#include "E2ETest.h"
3535

3636
void setup() {
37-
#if defined(ESP8266)
38-
Serial.begin(74880);
39-
#else
4037
Serial.begin(115200);
41-
#endif
4238
delay(2000);
4339
Serial.println("Initializing tests");
4440
Serial.println(" Compiled on " __DATE__ " " __TIME__);

0 commit comments

Comments
 (0)