#from epidata.context import ec
from datetime import datetime, timedelta
import pandas as pd

"""
Explanation: <h1 style="text-align:center;text-decoration: underline">Data Ingestion Tutorial</h1>
<h1>Overview</h1>
<p>Welcome to the data ingestion tutorial. In this tutorial, we'll go over steps required to ingest data into EpiData. We'll also query the data and verify that our ingestion process was successful.</p>

<h2>Generate Access Token</h2>

<p>The first step in ingesting data into EpiData is obtaining an access token for session authentication. At present, EpiData supports GitHub's Personal Access Token.<p>
<p>You can go ahead and create a Personal Access Token by visiting https://github.com/settings/tokens</p>

<h2>Modify Ingestion Example</h2>
<p>Once an access token is available, you can use it within the data ingestion program by following these steps:<p>
<ul><li><p>Download the Python ingestion example <i>sensor_data_ingest.py</i> available in your Notebook tree view.</p></li>
<li><p>Update the ACCESS_TOKEN variable (in <i>sensor_data_ingest.py</i>) to the Personal Access Token you created on GitHub's website.
<ul><li>ACCESS_TOKEN = '&#60;Personal Access Token&#62;'</li></ul></li></p>
<li><p>Modify the default values of the following variables (optional):
<ul><li>COMPANY</li>
<li>SITE</li>
<li>STATION</li>
</ul></li>
</ul></p>

<h2>Run Ingestion Example</h2>
<p>The next step is to run the updated example <i>'sensor_data_ingest.py'</i> by using a Python 2.x interpreter. The example sends data to EpiData server using REST interface. You should see status of each ingestion steps in your standard output.</p>
<p>You can let the example run and ingest data for a short period of time, and interrupt it by using Ctrl-C command.</p>

<h2>Query and Display Data</h2>
<p>We'll now query the database for the data that was ingested in the previous step. Let's start by running the cell below that imports the required modules.</p>
End of explanation
"""


COMPANY = 'EpiData'
SITE = 'San_Jose'
STATION = 'WSN-1'
start_time = datetime.strptime('8/1/2017 00:00:00', '%m/%d/%Y %H:%M:%S')
stop_time = datetime.strptime('8/31/2017 00:00:00', '%m/%d/%Y %H:%M:%S')

primary_key={"company": COMPANY, "site": SITE, "station": STATION, "sensor": ["Temperature_Probe","Anemometer","RH_Probe"]}
df = ec.query_measurements_original(primary_key, start_time, stop_time)

"""
Explanation: <p>In the cell below, let's modify the variables COMPANY, SITE and SENSOR to match the data recently ingested, and run the cell to query the data.</p>
End of explanation
"""


df = df.select("company", "site", "station", "ts", "meas_name", "meas_value", "meas_unit")
df.show(5)

"""
Explanation: <p>Next we'll display the initial few records using <i>df.show()</i> function, and visually verify that the data matches the ingested data.</p>
End of explanation
"""


