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

Batch events #8

Merged
merged 12 commits into from
Aug 27, 2024
Merged

Batch events #8

merged 12 commits into from
Aug 27, 2024

Conversation

GabrielRozendo
Copy link
Contributor

Refactoring:

  • Unify to get both system info and OS Name once and changed to not null.
  • InitOptions in a separated file with more options.
  • Normalized ' and ".
  • Private fields.
  • Not null when possible.
  • Handle logs.

Add:

  • Storage (as abstract/interface) to persist events for offline and to accumulate before send in batch.
  • Check lifecycle, if the app goes to inactive or paused states, send events.
  • Send events in batch (customized).
  • If an error happens, check to try again later (internet issues or 5xx status code) or discard events (4xx status code).

In this moment, using Hive but could be implement using anything.

- InitOptions in separated file with more options
- Normalized ' and "
- Private fields
- Not null when possible
- Handle logs

- Add storage to persist events (offline and acumulate before send in batch)
- Add lifecycle to check if the app went to inactive or paused states
- Send events in batch to endpoint events
- If an error happened, check to try again or discard events
@GabrielRozendo
Copy link
Contributor Author

Hi @goenning
Does are PR welcome?

I've been hosting the Aptabase in a small VPS and it is handling huge requests.
I know about PR #6 and as it has a new approach and seems no more active, I'm trying a simpler approach to help (including for my VPS' health 😅).
Did everything thinking in your comments there and approaches used in another SDKs too.

Please check it out and give your feedback.
It is in WIP (work in progress) yet, need to update readme/instructions about them and review some unecessary comments.
Let me know about your thoughts.

Thank you.

@GabrielRozendo GabrielRozendo marked this pull request as draft February 14, 2024 12:18
@GabrielRozendo GabrielRozendo changed the title [WIP] Batch events Draft: Batch events Feb 14, 2024
@goenning
Copy link
Member

Hi @GabrielRozendo thanks for the PR, just letting you know that I'll have a look at it this week :)

@goenning
Copy link
Member

Hi @GabrielRozendo seems pretty good to me!

  1. Will it work with flutter on web?
  2. Have you tried using it on your app?
  3. It's currently in Draft, are you looking to add more things before publishing?

@GabrielRozendo
Copy link
Contributor Author

Hi @goenning
1: Yes, as Hive and SharedPrefs work for web, it'll be too!
2: Yes, I'm trying in beta right now (2 users) and so far so good. Not released to public yet.
3: I want to check and test more, decide between Hive and SharedPrefs (1), plus, release to my public app and follow how it would be going (2)

It was only a preview to get your firstly impressions and check if it is going good for you.
Good to know!

@goenning
Copy link
Member

@GabrielRozendo just a heads up that there might be some minor conflicts due to package updates on pubspec.yaml

@GabrielRozendo
Copy link
Contributor Author

@goenning no problem so far and I've found a case.
My server was rebooting (intentionally) and as I was debugging the Flutter app, I validated another scenario.

Logs

[Aptabase] Checking events, reason: init
[Aptabase] Sent 30 events successfully
[Aptabase] Aptabase initilized!
[Aptabase] TrackEvent failed with status code 502. Response:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

[Aptabase] Checking events, reason: timer
[Aptabase] Sent 30 events successfully
[Aptabase] TrackEvent failed with status code 502. Response:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

[Aptabase] Checking events, reason: timer
[Aptabase] TrackEvent Exception: SocketException: Connection refused (OS Error: Connection refused, errno = 61), address = aptabase.myserver.com, port = 51030
[Aptabase] SocketException (SocketException: Connection refused (OS Error: Connection refused, errno = 61), address = aptabase.myserver.com, port = 51030)
[Aptabase]

           #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:721:35)
           #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1914:26)
           #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
           #3      RawSecureSocket.startConnect (dart:io/secure_socket.dart:299:22)
           #4      SecureSocket.startConnect (dart:io/secure_socket.dart:77:28)
           #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2483:26)
           #6      _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2926:12)
           #7      _HttpClient._getConnection (dart:_http/http_impl.dart:2931:12)
           #8      _HttpClient._openUrl (dart:_http/http_impl.dart:2786:12)
           #9      _HttpClient.postUrl (dart:_http/http_impl.dart:2633:49)
           #10     Aptabase._send (package:aptabase_flutter/aptabase_flutter.dart:223:35)
           #11     Aptabase._tick (package:aptabase_flutter/aptabase_flutter.dart:144:28)
           <asynchronous suspension>

[Aptabase] Checking events, reason: timer
[Aptabase] Sent 30 events successfully
[Aptabase] TrackEvent failed with status code 502. Response:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

[Aptabase] Checking events, reason: timer
[Aptabase] Sent 30 events successfully
[Aptabase] TrackEvent failed with status code 400. Response: Too many events (30) in a single request. Maximum is 25.
[Aptabase] Checking events, reason: timer
[Aptabase] Sent 22 events successfully
[Aptabase] Checking events, reason: timer


I'll change this sentence to remove the word successfully. At this point we don't know yet.
Sent 30 events successfully

But my real question here is about that 400 response.
The server has a max limit for 25? If so, we should handle this case. I can change that as well.

[Aptabase] TrackEvent failed with status code 400. Response: Too many events (30) in a single request. Maximum is 25.

@goenning
Copy link
Member

goenning commented Apr 2, 2024

Yes, there’s a limit of 25 events per request, so the SDK has to create chunks of up to 25 events before sending the request.

@GabrielRozendo GabrielRozendo marked this pull request as ready for review April 3, 2024 00:19
@GabrielRozendo GabrielRozendo changed the title Draft: Batch events Batch events Apr 3, 2024
@GabrielRozendo
Copy link
Contributor Author

Hey @goenning
Just updated the PR with main

Not in progress anymore.
No problem so far and feel free to review/approve it

Copy link
Member

@cristipufu cristipufu left a comment

Choose a reason for hiding this comment

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

Thanks @GabrielRozendo!

Let's also update the version of the sdk and add a new entry in the CHANGELOG

Copy link
Member

Choose a reason for hiding this comment

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

We need to update the version of the sdk to 0.4.0:
version: 0.4.0

static const Duration _sessionTimeout = Duration(hours: 1);
Aptabase._();

static const _sdkVersion = "[email protected]";
Copy link
Member

Choose a reason for hiding this comment

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

static const _sdkVersion = "[email protected]";

@cristipufu cristipufu merged commit 54e827e into aptabase:main Aug 27, 2024
@cristipufu cristipufu mentioned this pull request Aug 27, 2024
@flutter-painter
Copy link

Hi, @GabrielRozendo ,

Thank you again, I gave it a look, but fail to grasp it.
Would it please be possible to include a short example on how to handle aptabase with persistence ?
My main concern is that it seems the storage solution (e.g. Hive) needs to be instantiated in the aptabase.init in the main.dart
In my case this is not possible as storage solution is instantiated much further down the widget tree

@GabrielRozendo
Copy link
Contributor Author

Hi @flutter-painter

indeed, the aptabase.init initialize one storage if you don't provide it, the default one is Hive.

I'm not sure about your specific case but even if you init our storage solution later, as the aptabase init it anyway, I'm not sure if it is a real problem. Maybe you'd have two diff storage solution (one for Aptabase and other for you app).

  • Also, you could init the aptabase after init your storage solution, not exactly on main.dart (in my apps I use this approach).
  • You can use even other diff than Hive.

In example/main.dart in PR #13 I've included one using SharedPreferences.

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