Skip to content

Instantly share code, notes, and snippets.

@bewest
Created July 29, 2018 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bewest/dbe590ab131edfdddd4d988d58c25930 to your computer and use it in GitHub Desktop.
Save bewest/dbe590ab131edfdddd4d988d58c25930 to your computer and use it in GitHub Desktop.
Getting one day data out of Nightscout

Get one day data out of Nightscout

Here's how to get one day of data out of Nightscout. The trick is in realizing that the field used to define the date is different between different data types, and that different data types are stored in different places. In addition, the API itself has interesting behaviors, for example to switch between tsv, csv, and json outputs, to perform regex based searches, and in some cases bugs to work around.

Mechanics of the search syntax. The API endpoints support a query string syntax used to compose queries. These are always terms added after the question mark ? in the URL endpoint. The the way this works is similar to PHP's interpretation of query string parameters. The original idea was to support mongodb's special keyword syntax using $ as search operators. However, for a variety of issues, it does not work as expected on the SGV entries. We'll work through some of the advanced features of the API to work around those issues, finding ways to isolate data from a single day.



# Create regex for entries.
ns://api/v1/times/2017-/11-23.json?count=1000

ns://api/v1/times/2017-11-23.json?count=1000

# CSV format
ns://api/v1/times/2017-11-23.csv?count=1000

# Treatments for one day. 2017-11-23
ns://api/v1/treatments.json?count=1000&find[created_at][$gt]=2017-11-22Z&find[created_at][$lt]=2017-11-24

# Device status for one day
ns://api/v1/devicestatus.json?count=1000&find[created_at][$gt]=2017-11-22Z&find[created_at][$lt]=2017-11-24

There is a way to debug queries in order to figure out what kind of mongo search query is generated.

# Adding `/echo/` as a prefix can help debug and explain mongo queries.
# The object called find it passed as the mongo query.

ns://api/v1/echo/entries.json?count=1000&find[dateString][$lt]=2017-11-24Z&find[dateString][$gte]=2017-11-23Z
ns://api/v1/echo/treatments.json?count=1000&find[created_at][$gt]=2017-11-22Z&find[created_at][$lt]=2017-11-24


# It also works on the regex generator.
ns://api/v1/times/echo/2017-/11-23.csv?count=1000
# The regex generator allows slicing and dicing times in fairly powerful ways.
# It supports "globbing."
ns://api/v1/times/echo/2017-/11-{1-3}.csv?count=1000
ns://api/v1/times/echo/2017-/11-[1-3].csv?count=1000

# For example, look at evening hours in November only using regex + globbing
ns://api/v1/times/echo/2017-/11-*T{20,22,23,24,00,01,02,03,04,05,06,07,08}.csv?count=1000



HTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment