Purpose:
This project provides a script to be used with R-Studio for uploading CSV files into Dendra. It is customized for fog monitoring data from the Fog RNS group.
Prerequisites:
-
User must be an Administrator for the Organization that will receive the uploads.
-
At least one datastream must exist for each database/table that you want to load data into.
-
The datastream
datapoints_config
must contain an additional source field to permit upload. This must match the source that will be specified in the upload manifest.
{
"query": {
"api": "fogrns",
"db": "station_fog_csvs",
"fc": "source_fonr_st0",
"sc": "\"time\", \"Liters\"",
"utc_offset": -28800,
"coalesce": false,
"source": "/fogrns/station_fog_csvs/source_fonr_st0"
}
}
-
Install the R scripting language and R-Studio IDE.
-
Download this git repository using the "code" button on the website and select zip file. Unzip into your Documents folder.
-
Create a
.env
file in this directory using thesample.env
as an example. Put your Dendra credentials (login and password) in here. NEVER check this into source control (use a.gitignore
file). -
Launch R-Studio. Navigate to the upload.r file and open.
-
Make sure the dependency libraries get intalled. R-Studio may automatically detect and ask to install them. If not, copy the "install.packages" commands to the Console tab and run each one.
-
Switch to the Terminal tab. This is where you will run the upload.r script with arguments.
-
Run the script, providing the required arguments:
For example:
./upload.r --csv "FONR_STA_0_NN_20190710_20190913.csv" --comment "Production csv file import for fonr st0" --source "/fogrns/station_fog_csvs/source_fonr_st0" --station_id "672b9a9db1d7cdf52cecb5c3" --time_adjust 25200
--csv
: Data file. This is the comma separated value file holding datalogger measurements that will be uploaded.
--comment
: Metadata to give context to what this upload is. Will be stored with the CSV file.
--source
: posix-style filepath. This should match exactly the source listed in the datapoints_config section of the datastreams assiciated with this data.
--station_id
: Database ID of the station in Dendra this dataset belongs to. This can be found on the station metadata page as 'id' in grey. It also shows up in the URL.
--time
: timezone offset in seconds.
UTC: Greenwich meant time offset; 0 seconds.
PST: Pacific Standard Time offset: 28800 seconds.
PDT: Pacific Daylight Time offset: 25200 seconds.
-
Prepare an upload manifest as follows:
- Provide a meaningful
comment
- Set the
source
to match the source of a given datastream - Set
dry_run
tofalse
- Set
station_id
Sample:
{ "organization_id": "6710031571c87f0c4e4bd317", "spec": { "comment": "Production csv file import for ...", "method": "csv", "options": { "columns_name": "safe", "context": { "source": "/fogrns/station_fog_csvs/source_fonr" }, "date_column": "Date", "dry_run": false, "from_line": 11, "time_adjust": 0, "time_column": "Time", "time_format": "M/D/YYYY h:mm:ss A" } }, "spec_type": "file/import", "station_id": "REPLACE_WITH_A_VALID_STATION_ID", "storage": { "method": "minio" } }
- Provide a meaningful
-
POST the manifest to the API. You may either:
- POST the JSON to
/uploads
- Use the CLI, e.g.
den util create-upload --file="upload.json" --save
.
- POST the JSON to
-
GET the manifest after a few seconds, or until the
presigned_put_info
is available. -
PUT the CSV file to the URL at
result_pre.presigned_put_info.url
.CURL example:
curl -H "Content-Type: text/csv" --upload-file my.csv "THE_PRESIGNED_PUT_URL" ```
-
PATCH the manifest to activate it. Set
is_active
totrue
. -
Wait for processing. GET the manifest every few seconds until the upload
state
is eithercompleted
orerror
.
-
Prepare an upload manifest as follows:
- Provide a meaningful
comment
- Set the
source
to match the source of a given datastream - Set
dry_run
totrue
- Set
station_id
Sample:
{ "organization_id": "6710031571c87f0c4e4bd317", "spec": { "comment": "Production csv file import for ...", "method": "csv", "options": { "columns_name": "safe", "context": { "source": "/fogrns/station_fog_csvs/source_fonr" }, "date_column": "Date", "dry_run": true, "from_line": 11, "time_adjust": 0, "time_column": "Time", "time_format": "M/D/YYYY h:mm:ss A" } }, "spec_type": "file/import", "station_id": "REPLACE_WITH_A_VALID_STATION_ID", "storage": { "method": "minio" } }
- Provide a meaningful
-
POST the manifest to the API. You may either:
- POST the JSON to
/uploads
- Use the CLI, e.g.
den util create-upload --file="upload.json" --save
.
- POST the JSON to
-
GET the manifest after a few seconds, or until the
presigned_put_info
is available. -
PUT the CSV file to the URL at
result_pre.presigned_put_info.url
.CURL example:
curl -H "Content-Type: text/csv" --upload-file my.csv "THE_PRESIGNED_PUT_URL"
-
PATCH the manifest to activate it. Set
is_active
totrue
. -
Wait for processing. GET the manifest every few seconds until the upload
state
is eithercompleted
orerror
. -
Examine the
sampled_data
in the manifest to ensure it looks correct. -
PATCH the manifest again to submit for live. Patch operations can only be done on root level fields; hence in one PATCH operation you will:
- Set
is_active
totrue
- Set
spec
to the original spec withdry_run
set tofalse
- Set
-
Wait for processing. GET the manifest every few seconds until the upload
state
is eithercompleted
orerror
.