Improve BodyMeasures - CSV Import

... allow "date" in ISO8601 as an alternative to "ts" timestamp from Epoch
... update documentation / provide sample files
This commit is contained in:
Joern
2017-04-08 17:40:18 +02:00
parent 514f24535d
commit f1a4f82552
7 changed files with 55 additions and 9 deletions

View File

@@ -0,0 +1,4 @@
date,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
2017-01-01T12:00:00Z,2,3,4,5,6,7,Useless data - but valid format
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,11.57,Test Value
2017-01-03T12:00:00Z,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
1 date weightkg fatkg boneskg musclekg leankg fatpercent comment
2 2017-01-01T12:00:00Z 2 3 4 5 6 7 Useless data - but valid format
3 2017-01-02T12:00:00Z 80.15 12.10 5.4 0 32.5 11.57 Test Value
4 2017-01-03T12:00:00Z 81.15 12.10 5.4 0 32.5 12.57 Here the comment text contain commas itself, so it needs quotes

View File

@@ -0,0 +1,4 @@
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
1,2,3,4,5,6,7,Is a technically valid entry but the timestamp is 1 second of Epoch so it will never be in the possible time range
1479632565,80.15,12.10,5.4,0,32.5,11.57,Test Value
1479632865,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
1 ts weightkg fatkg boneskg musclekg leankg fatpercent comment
2 1 2 3 4 5 6 7 Is a technically valid entry but the timestamp is 1 second of Epoch so it will never be in the possible time range
3 1479632565 80.15 12.10 5.4 0 32.5 11.57 Test Value
4 1479632865 81.15 12.10 5.4 0 32.5 12.57 Here the comment text contain commas itself, so it needs quotes

View File

@@ -1,3 +0,0 @@
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
1,2,3,4,5,6,7,Is a technically valid entry, but the timestamp is 1 second of Epoch
1479632565,80.15,12.10,5.4,0,32.5,1157,Test Value
1 ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
2 1,2,3,4,5,6,7,Is a technically valid entry, but the timestamp is 1 second of Epoch
3 1479632565,80.15,12.10,5.4,0,32.5,1157,Test Value

View File

@@ -1,21 +1,34 @@
Description of File-Format for "Body Measures - .CSV Import" function Description of File-Format for "Body Measures - .CSV Import" function
--------------------------------------------------------------------- ---------------------------------------------------------------------
Example is provided here: Example for Timestamp is provided here:
---- ----
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
1,2,3,4,5,6,7,Is a technically valid entry, but the timestamp is 1 second of Epoch 1,2,3,4,5,6,7,Is a technically valid entry, but the timestamp is 1 second of Epoch
1479632565,80.15,12.10,5.4,0,32.5,1157,Test Value 1479632565,80.15,12.10,5.4,0,32.5,1157,Test Value
1479632865,80.15,12.10,5.4,0,32.5,1157,"Here the comment text contain commas itself, so it needs quotes"
---- ----
Example for Date/Time is provided here:
date,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
2017-01-01T12:00:00Z,2,3,4,5,6,7,Useless data - but valid format
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,1157,Test Value
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,1157,"Here the comment text contain commas itself, so it needs quotes"
a) Header Line a) Header Line
First Line of a valid file has to contain header information. This has to be a comma-separated First Line of a valid file has to contain header information. This has to be a comma-separated
list of the measures provided in the data lines. You do not need to provide all possible measures, list of the measures provided in the data lines. You do not need to provide all possible measures,
but at least "ts" and "weightkg". but at least "ts" and "weightkg".
The mandatory measures are: As mandatory field you need time/date of the measures. There are 2 options:
- "ts" - Unix timestamp (in seconds) since Epoch. The value can be calcuated in XLS - just Google how-to. - "ts" - Unix timestamp (in seconds) since Epoch. The value can be calcuated in XLS - just Google how-to.
OR
- "date" - ISO8601 formatted Date/Time String
The mandatory measure is:
- "weightkg" - Athlete weight in kilograms - "weightkg" - Athlete weight in kilograms
The optional measures are: The optional measures are:
@@ -29,7 +42,8 @@ The optional measures are:
Data formats: Data formats:
- "ts" is a 64Bit Integer - any other time will be rejected - "ts" is a 64Bit Integer - any other time will be rejected
- "comment" is a String - "date" is a ISO8601 formated date/time
- "comment" is a String - if it contains "commas" itself the string must be enclosed by "string with comma, and more comment"
- all other measures are float/double in format <xxxxxxx.yyy> - all other measures are float/double in format <xxxxxxx.yyy>
There are very strict format checks when importing. Only if the whole There are very strict format checks when importing. Only if the whole

View File

@@ -25,7 +25,7 @@
BodyMeasuresCsvImport::BodyMeasuresCsvImport(Context *context) : context(context) { BodyMeasuresCsvImport::BodyMeasuresCsvImport(Context *context) : context(context) {
allowedHeaders << "ts" << "weightkg" << "fatkg" << "boneskg" << "musclekg" << "leankg" allowedHeaders << "ts" << "date" << "weightkg" << "fatkg" << "boneskg" << "musclekg" << "leankg"
<< "fatpercent" << "comment"; << "fatpercent" << "comment";
} }
@@ -38,6 +38,7 @@ BodyMeasuresCsvImport::getBodyMeasures(QString &error, QDateTime from, QDateTime
// all variables to be defined here (to allow :goto - for simplified error handling/less redundant code) // all variables to be defined here (to allow :goto - for simplified error handling/less redundant code)
bool tsExists = false; bool tsExists = false;
bool dateExists = false;
bool weightkgExists = false; bool weightkgExists = false;
int lineNo = 0; int lineNo = 0;
@@ -72,6 +73,7 @@ BodyMeasuresCsvImport::getBodyMeasures(QString &error, QDateTime from, QDateTime
foreach(QString h, headers) { foreach(QString h, headers) {
if (h == "ts") tsExists = true; if (h == "ts") tsExists = true;
if (h == "date") dateExists = true;
if (h == "weightkg") weightkgExists = true; if (h == "weightkg") weightkgExists = true;
if (!allowedHeaders.contains(h)) { if (!allowedHeaders.contains(h)) {
if (error.isEmpty()) error = tr("Unknown column header: "); if (error.isEmpty()) error = tr("Unknown column header: ");
@@ -82,8 +84,12 @@ BodyMeasuresCsvImport::getBodyMeasures(QString &error, QDateTime from, QDateTime
if (error.length() > 0) { if (error.length() > 0) {
goto error; goto error;
} }
if (!tsExists) { if (!(tsExists || dateExists)) {
error = tr("Column 'ts' - Timestamp - is missing"); error = tr("Date and Timestamp are missing - Column 'ts' for timestamp - Colum 'date' for Date/Time.");
goto error;
}
if (tsExists && dateExists) {
error = tr("Both column 'ts' - Timestamp and 'date' - Date/Time are defined.");
goto error; goto error;
} }
if (!weightkgExists) { if (!weightkgExists) {
@@ -124,6 +130,19 @@ BodyMeasuresCsvImport::getBodyMeasures(QString &error, QDateTime from, QDateTime
error = tr("Invalid 'ts' - Timestamp - in line %1").arg(lineNo); error = tr("Invalid 'ts' - Timestamp - in line %1").arg(lineNo);
goto error; goto error;
} }
} else if (h == "date") {
// parse date ISO 8601
m.when = QDateTime::fromString(i, Qt::ISODate);
if (m.when.isValid()) {
// skip line if not in date range
if (m.when < from || m.when > to) {
m.when = QDateTime::fromMSecsSinceEpoch(0);
break; // stop analysing the data items of the line
}
} else {
error = tr("Invalid 'date' - Date/Time not ISO 8601 format - in line %1").arg(lineNo);
goto error;
}
} else if (h == "weightkg") { } else if (h == "weightkg") {
m.weightkg = i.toDouble(&ok); m.weightkg = i.toDouble(&ok);
if (!ok) { if (!ok) {

View File

@@ -0,0 +1,4 @@
date,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
2017-01-01T12:00:00Z,2,3,4,5,6,7,Useless data - but valid format
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,11.57,Test Value
2017-01-03T12:00:00Z,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
1 date weightkg fatkg boneskg musclekg leankg fatpercent comment
2 2017-01-01T12:00:00Z 2 3 4 5 6 7 Useless data - but valid format
3 2017-01-02T12:00:00Z 80.15 12.10 5.4 0 32.5 11.57 Test Value
4 2017-01-03T12:00:00Z 81.15 12.10 5.4 0 32.5 12.57 Here the comment text contain commas itself, so it needs quotes

View File

@@ -0,0 +1,4 @@
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
1,2,3,4,5,6,7,Is a technically valid entry but the timestamp is 1 second of Epoch so it will never be in the possible time range
1479632565,80.15,12.10,5.4,0,32.5,11.57,Test Value
1479632865,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
1 ts weightkg fatkg boneskg musclekg leankg fatpercent comment
2 1 2 3 4 5 6 7 Is a technically valid entry but the timestamp is 1 second of Epoch so it will never be in the possible time range
3 1479632565 80.15 12.10 5.4 0 32.5 11.57 Test Value
4 1479632865 81.15 12.10 5.4 0 32.5 12.57 Here the comment text contain commas itself, so it needs quotes