Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First version of a library that reads data from NS. #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tzachi-dar
Copy link
Collaborator

Any comments are welcomed.

);

@GET("/api/v1/entries.json?find[type][$eq]=cal")
Call<List<NightscoutBg>> getCal(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should a "cal" be mapped to a "NightscoutBg"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, should be probably called getBg.

@paolorotolo
Copy link

Also it seems you need to add compile 'com.squareup.okhttp:logging-interceptor:2.6.0' in build.gradle to compile. HttpLoggingInterceptor is in fact required by NsRestApiReader.

@tzachi-dar
Copy link
Collaborator Author

This is correct. I have done this in my private branch, will need to do it in master as well.
PaoloRotolo does the api look fine for you? after all it only allows one to get the latest readings.

@paolorotolo
Copy link

Yes, looks good! Can you actually make NsRestApiReader extend AsyncTask? In this way it will not block the main thread while reading from NS.

@AdrianLxM
Copy link
Collaborator

Can you actually make NsRestApiReader extend AsyncTask? In this way it will not block the main thread while reading from NS.

In the end, the library will start it's own service that will wake up every 5 minutes and will retrieve the latest data. It will then call back the app registered with the library.
If it just calls a notify event or actually pushes the data is yet to be specified.

@tzachi-dar
Copy link
Collaborator Author

It seems to me that the library should be reading the data from NS.
Assuming that one wants to read the data every 5 minutes is not a must. One might want to read it once a day and others might read it on other occasions.
You can see in WixelReader.timeForNextRead() that the logic for the next reading is not simple, and also differ from wifi readers to parrot and so on.

Does the approach of 5 functions that I have proposed looks fine?

@AdrianLxM
Copy link
Collaborator

The timing, when to trigger new Data should be part of the Library.
The Libre-Reader e.g. will not deliver the data every x minutes, but just when the User establishes an NFC connection.

I can write a proposal for an interface for that in the next couple of days.

@tzachi-dar
Copy link
Collaborator Author

Seems to me that if we want to have a java library that reads from
nightscout it should be passive and not triger calls.
The part that reads from hw sources or websites should be part of xdrip.
But fill free to create another proposal so that i can see what you mean.
בתאריך 9 בדצמ 2015 21:14,‏ "AdrianLxM" [email protected] כתב:

The timing, when to trigger new Data should be part of the Library.
The Libre-Reader e.g. will not deliver the data every x minutes, but just
when the User establishes an NFC connection.

I can write a proposal for an interface for that in the next couple of
days.


Reply to this email directly or view it on GitHub
#207 (comment)
.

@paolorotolo
Copy link

We can always implement both. Some passive methods and a service you can activate with an API.

// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient httpClient = new OkHttpClient();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be fed a cache to save data on mobile networks, and OkHttpClients are fairly expensive to create and will corrupt their state if multiple different OkHttpClients are using the same cache directory at the same time. We should probably create a singleton or store one already configured in the xdrip object.

For big requests this can save megabytes.

In another app I use combined gt/lte selectors and perform multiple queries in parallel using the .enqueue() method for day/hour chunks to maximize the chance that values are in the cache but still allow changes to make it through and it works very well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain:
Are you talking about caching connections? or are we talking about caching entries?

Using the same connection is very interesting. As for caching entries, this does not seem important since I read them by order, and I never read more than one entry more than once.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling gaps and backfills are where that approach fails. If you ever decide to add treatments for some reason it would also benefit from a modest cache.

Reusing an OkHttpClient will cause new requests to use a shared connection pool for any new requests. I believe it maintains 6 connections by default. There is a greater benefit when using HTTP/2.0 or SSL and doing multiple back-to-back requests. This code probably isn't likely to benefit much, but the REST uploader certainly would. Some things like redirects to HTTPS will benefit from reused clients and from a persistent cache.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the cache hold? Should it hold objects that are received from
the remote server?
If yes, how does it know if they were changed or not?

Thanks
Tzachi

On Wed, Jan 6, 2016 at 4:33 PM, Matthias Granberry <[email protected]

wrote:

In
app/src/main/java/com/eveningoutpost/dexdrip/Services/NsRestApiReader.java
#207 (comment)
:

  •   double xDrip_sensor_confidence;
    
  •   double xDrip_raw_timestamp;
    
  • }
  • private final static String TAG = NsRestApiReader.class.getName();
  • private INsRestApi CreateNsMethods(String baseUrl) {
  •    Retrofit retrofit;
    
  •    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    
  •    // set your desired log level
    
  •    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    
  •    OkHttpClient httpClient = new OkHttpClient();
    

Handling gaps and backfills are where that approach fails. If you ever
decide to add treatments for some reason it would also benefit from a
modest cache.

Reusing an OkHttpClient will cause new requests to use a shared connection
pool for any new requests. I believe it maintains 6 connections by default.
There is a greater benefit when using HTTP/2.0 or SSL and doing multiple
back-to-back requests. This code probably isn't likely to benefit much, but
the REST uploader certainly would. Some things like redirects to HTTPS will
benefit from reused clients and from a persistent cache.


Reply to this email directly or view it on GitHub
https://github.com/StephenBlackWasAlreadyTaken/xDrip-Experimental/pull/207/files#r48962549
.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more question, how will the uploader benefit from items cache? I can
see how he can benefit from caching connections, but for objects we only
upload each object once.

Thanks
Tzachi

On Wed, Jan 6, 2016 at 4:40 PM, Tzachi Dar [email protected] wrote:

What should the cache hold? Should it hold objects that are received from
the remote server?
If yes, how does it know if they were changed or not?

Thanks
Tzachi

On Wed, Jan 6, 2016 at 4:33 PM, Matthias Granberry <
[email protected]> wrote:

In
app/src/main/java/com/eveningoutpost/dexdrip/Services/NsRestApiReader.java
#207 (comment)
:

  •  double xDrip_sensor_confidence;
    
  •  double xDrip_raw_timestamp;
    
  • }
  • private final static String TAG = NsRestApiReader.class.getName();
  • private INsRestApi CreateNsMethods(String baseUrl) {
  •    Retrofit retrofit;
    
  •    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    
  •    // set your desired log level
    
  •    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    
  •    OkHttpClient httpClient = new OkHttpClient();
    

Handling gaps and backfills are where that approach fails. If you ever
decide to add treatments for some reason it would also benefit from a
modest cache.

Reusing an OkHttpClient will cause new requests to use a shared
connection pool for any new requests. I believe it maintains 6 connections
by default. There is a greater benefit when using HTTP/2.0 or SSL and doing
multiple back-to-back requests. This code probably isn't likely to benefit
much, but the REST uploader certainly would. Some things like redirects to
HTTPS will benefit from reused clients and from a persistent cache.


Reply to this email directly or view it on GitHub
https://github.com/StephenBlackWasAlreadyTaken/xDrip-Experimental/pull/207/files#r48962549
.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to an HTTP cache. The server generates an etag header containing a checksum of the data that it calculates that it will send. The client then sends a request to the server with an If-None-Match header containing a set of etags that it believes may match the response coming from the server. When the server receives a request it will generate the response and compare it to the etags in the request If-None-Match header. If any match, the server responds with a 304 not modified response instead of sending the actual data. The client presents the cached copy as though it came over the network.

The uploader will only benefit from cached redirects and reused connections. The response cache won't be a benefit for most upload requests.

An Android app can configure the OkHttp cache like this:
okHttpClient.setCache(new Cache(context.getCacheDir()), 1024 * 1024 * 50))

50m may be overkill. 2M contains about 6m of my personal records.

@mgranberry
Copy link
Collaborator

BTW I have a WIP Nightscout client here: https://github.com/mgranberry/YADLA/tree/master/alrightypump-cloud/src/main/kotlin/com/kludgenics/alrightypump/cloud/nightscout

Read/write of entries to/from nightscout works great. Writing treatments works, but reading treatments needs a little work to handle the fact that they aren't very well-structured and may map onto more than one record type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants