samsara_sdk 0.5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +201 -0
- data/README.md +43 -0
- data/lib/samsara_sdk.rb +6 -0
- data/lib/samsara_sdk/client.rb +57 -0
- data/lib/samsara_sdk/config.rb +114 -0
- data/lib/samsara_sdk/errors.rb +8 -0
- data/lib/samsara_sdk/event.rb +55 -0
- data/lib/samsara_sdk/publisher.rb +70 -0
- data/lib/samsara_sdk/ring_buffer.rb +129 -0
- data/samsara_sdk.gemspec +22 -0
- metadata +124 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c262c3b124bd4903dac56a5418ae0a1c99fab824
|
|
4
|
+
data.tar.gz: 090ef013b88e2e169729c6891ba7d8f4752a6ecb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2a40ee0f8dc8e6095e36f33d6d8d268eb5270cc1b7a55bad63aa3830a5b3419516d7206b1b4e4a4ae3a0d31080d9e300739fd03171a34597f35bed14d2f6a53a
|
|
7
|
+
data.tar.gz: 8cce55a07e5355be151f2ac4d260b8fab985e869e345dd2d00dfd7b796624234a43a942cd8729d19140fc6d8c28ff08aaf21bfb7bc392443486bd598e22203e3
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# samara_sdk
|
|
2
|
+
|
|
3
|
+
Ruby client SDK for Samsara.
|
|
4
|
+
|
|
5
|
+
Works with Ruby >= 1.9.3
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
To see how to use this client please [read the documentation](/docs/clients/ruby-client.md)
|
|
10
|
+
|
|
11
|
+
## Contributing
|
|
12
|
+
|
|
13
|
+
Follow the [Quick-Start guide](/docs/quick-start.md) to get Samsara & friends set up.
|
|
14
|
+
|
|
15
|
+
To hack client locally:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
$ gem install bundler # (if not installed)
|
|
19
|
+
$ gem install rake # (if not installed)
|
|
20
|
+
$ git clone https://github.com/samsara/samsara.git
|
|
21
|
+
$ cd samsara/clients/ruby
|
|
22
|
+
$ rake rubocop # to check code-standards
|
|
23
|
+
$ rake test # to run tests
|
|
24
|
+
$ rake coverage # to generate code coverage report
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
To install client locally:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
$ cd samsara/clients/ruby
|
|
31
|
+
$ rake build
|
|
32
|
+
$ rake install
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### TODO
|
|
36
|
+
|
|
37
|
+
- [ ] support `"send_client_stats"` option
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
Copyright © 2016 Samsara's authors.
|
|
42
|
+
|
|
43
|
+
Distributed under the Apache License v 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
data/lib/samsara_sdk.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'samsara_sdk/config'
|
|
2
|
+
require 'samsara_sdk/publisher'
|
|
3
|
+
require 'samsara_sdk/ring_buffer'
|
|
4
|
+
require 'samsara_sdk/event'
|
|
5
|
+
|
|
6
|
+
# Samsara SDK
|
|
7
|
+
module SamsaraSDK
|
|
8
|
+
# A client for ingesting events into Samsara.
|
|
9
|
+
# It is the main interface to communicate with Samsara API.
|
|
10
|
+
class Client
|
|
11
|
+
# initialize
|
|
12
|
+
#
|
|
13
|
+
# @param config [Hash] Configuration overrides.
|
|
14
|
+
# @raise [SamsaraSDK::ConfigValidationError] if any config option is incorrect.
|
|
15
|
+
def initialize(config)
|
|
16
|
+
Config.setup! config
|
|
17
|
+
@publisher = Publisher.new
|
|
18
|
+
@queue = RingBuffer.new(Config.get[:max_buffer_size])
|
|
19
|
+
@activity = Thread.new { publishing_activity } if Config.get[:start_publishing_thread]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Publishes given events list to Ingestion API immediately.
|
|
23
|
+
#
|
|
24
|
+
# @param events [Array<Hash>] List of events.
|
|
25
|
+
# @return [Boolean] The result of publish operation.
|
|
26
|
+
# @raise [SamsaraSDK::EventValidationError] if any option of the given event is invalid.
|
|
27
|
+
# @see http://samsara-analytics.io/docs/design/events-spec Event specification
|
|
28
|
+
def publish_events(events)
|
|
29
|
+
events = events.map { |event| Event.validate(Event.enrich(event)) }
|
|
30
|
+
@publisher.post events
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Pushes event to internal events' queue.
|
|
34
|
+
#
|
|
35
|
+
# @param event [Hash] Event data.
|
|
36
|
+
# @option data [String] :eventName Name of the event.
|
|
37
|
+
# @option data [String] :sourceId Source ID of the event.
|
|
38
|
+
# @option data [Integer] :timestamp Timestamp in milliseconds.
|
|
39
|
+
# @see http://samsara-analytics.io/docs/design/events-spec Event specification
|
|
40
|
+
def record_event(event)
|
|
41
|
+
event = Event.validate(Event.enrich(event))
|
|
42
|
+
@queue << event
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# Publishing activity.
|
|
48
|
+
# Represents an infinite loop that periodically posts queued events to Ingestion API.
|
|
49
|
+
# Used in a background thread.
|
|
50
|
+
def publishing_activity
|
|
51
|
+
loop do
|
|
52
|
+
@queue.flush { |data| @publisher.post data } if @queue.count >= Config.get[:min_buffer_size]
|
|
53
|
+
sleep Config.get[:publish_interval_ms] / 1000
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require 'samsara_sdk/errors'
|
|
2
|
+
|
|
3
|
+
# Samsara SDK
|
|
4
|
+
module SamsaraSDK
|
|
5
|
+
# Samsara SDK default configuration and request constants.
|
|
6
|
+
class Config
|
|
7
|
+
# Samsara specific HTTP Header
|
|
8
|
+
PUBLISHED_TIMESTAMP_HEADER = 'X-Samsara-publishedTimestamp'.freeze
|
|
9
|
+
|
|
10
|
+
# Samsara Ingestion API endpoint
|
|
11
|
+
API_PATH = '/v1/events'.freeze
|
|
12
|
+
|
|
13
|
+
# Default configuration values.
|
|
14
|
+
@defaults = {
|
|
15
|
+
# Samsara ingestion api endpoint "http://samsara-ingestion.local/"
|
|
16
|
+
url: '',
|
|
17
|
+
|
|
18
|
+
# Identifier of the source of these events
|
|
19
|
+
# OPTIONAL used only for record-event
|
|
20
|
+
sourceId: '',
|
|
21
|
+
|
|
22
|
+
# Start the publishing thread?
|
|
23
|
+
start_publishing_thread: TRUE,
|
|
24
|
+
|
|
25
|
+
# How often should the events being sent to Samsara
|
|
26
|
+
# in milliseconds.
|
|
27
|
+
# default = 30s
|
|
28
|
+
publish_interval_ms: 30_000,
|
|
29
|
+
|
|
30
|
+
# Max size of the buffer.
|
|
31
|
+
# When buffer is full older events are dropped.
|
|
32
|
+
max_buffer_size: 10_000,
|
|
33
|
+
|
|
34
|
+
# Minimum number of events that must be in the buffer
|
|
35
|
+
# before attempting to publish them.
|
|
36
|
+
min_buffer_size: 100,
|
|
37
|
+
|
|
38
|
+
# Network timeout for send operations
|
|
39
|
+
# in milliseconds.
|
|
40
|
+
# default 30s
|
|
41
|
+
send_timeout_ms: 30_000,
|
|
42
|
+
|
|
43
|
+
# Should the payload be compressed?
|
|
44
|
+
# allowed values :gzip, :none
|
|
45
|
+
compression: :gzip,
|
|
46
|
+
|
|
47
|
+
# NOT CURRENTLY SUPPORTED
|
|
48
|
+
# Add Samsara client statistics events
|
|
49
|
+
# this helps you to understand whether the
|
|
50
|
+
# buffer size and publish-intervals are
|
|
51
|
+
# adequately configured.
|
|
52
|
+
# send_client_stats: TRUE
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
# Actual configuration values.
|
|
56
|
+
@values = {}
|
|
57
|
+
|
|
58
|
+
class << self
|
|
59
|
+
# Set up configuration.
|
|
60
|
+
# Merges given config with defaults and validates the result.
|
|
61
|
+
#
|
|
62
|
+
# @param input_config [Hash] Input configuration options.
|
|
63
|
+
# @raise [SamsaraSDK::ConfigValidationError] if any config option is incorrect.
|
|
64
|
+
def setup!(input_config)
|
|
65
|
+
@values = @defaults.merge input_config
|
|
66
|
+
validate @values
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Get configuration values.
|
|
70
|
+
#
|
|
71
|
+
# @return [Hash] Configuration values.
|
|
72
|
+
def get
|
|
73
|
+
@values
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Generates current timestamp.
|
|
77
|
+
#
|
|
78
|
+
# @return [Integer] timestamp in milliseconds.
|
|
79
|
+
def timestamp
|
|
80
|
+
(Time.now.to_f.round(3) * 1000).to_i
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
# Validates given configuration values.
|
|
86
|
+
#
|
|
87
|
+
# @param config [Hash] Configuration values.
|
|
88
|
+
# @raise [SamsaraSDK::ConfigValidationError] if any config option is incorrect.
|
|
89
|
+
def validate(config)
|
|
90
|
+
config.each do |k, v|
|
|
91
|
+
raise ConfigValidationError, "#{k} should be of #{@defaults[k].class}" unless valid_type?(k, v)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
raise ConfigValidationError, 'URL for Ingestion API should be specified.' if config[:url] == ''
|
|
95
|
+
raise ConfigValidationError, 'Incorrect compression option.' unless [:gzip, :none].include? config[:compression]
|
|
96
|
+
raise ConfigValidationError, 'Invalid interval time for Samsara client.' if config[:publish_interval_ms] <= 0
|
|
97
|
+
raise ConfigValidationError, 'max_buffer_size can not be less than min_buffer_size.' if
|
|
98
|
+
config[:max_buffer_size] < config[:min_buffer_size]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Check value type corresponds to allowed type for particular config key.
|
|
102
|
+
#
|
|
103
|
+
# @param key [Symbol] Configuration key.
|
|
104
|
+
# @param value [Object] Configuration value.
|
|
105
|
+
# @return [Boolean]
|
|
106
|
+
def valid_type?(key, value)
|
|
107
|
+
if [TrueClass, FalseClass].include? @defaults[key].class
|
|
108
|
+
return [TrueClass, FalseClass].include? value.class
|
|
109
|
+
end
|
|
110
|
+
value.is_a? @defaults[key].class
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require 'samsara_sdk/config'
|
|
2
|
+
require 'samsara_sdk/errors'
|
|
3
|
+
|
|
4
|
+
# Samsara SDK
|
|
5
|
+
module SamsaraSDK
|
|
6
|
+
# Contains methods for event manipulation.
|
|
7
|
+
class Event
|
|
8
|
+
# Validation schema for Ingestion API event.
|
|
9
|
+
@schema = {
|
|
10
|
+
sourceId: { required: TRUE, not_blank: TRUE, type: String },
|
|
11
|
+
timestamp: { required: TRUE, type: Integer, min: 0 },
|
|
12
|
+
eventName: { required: TRUE, not_blank: TRUE, type: String }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
# Validation rules for Ingestion API event.
|
|
16
|
+
@rules = {
|
|
17
|
+
required: lambda do |key, value, required|
|
|
18
|
+
raise EventValidationError, "Event.#{key}: Argument is required" if required && value.nil?
|
|
19
|
+
end,
|
|
20
|
+
not_blank: lambda do |key, value, on|
|
|
21
|
+
value.strip! if value.respond_to?(:strip!)
|
|
22
|
+
raise EventValidationError, "Event.#{key}: can't be blank" if
|
|
23
|
+
on && value.respond_to?(:empty?) ? value.empty? : !value
|
|
24
|
+
end,
|
|
25
|
+
type: ->(key, value, type) { raise EventValidationError, "Event.#{key}: Wrong type" unless value.is_a? type },
|
|
26
|
+
min: ->(key, value, min) { raise EventValidationError, "Event.#{key}: Can't be less than #{min}" if value < min }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
# Enriches missing event properties with ones from config.
|
|
31
|
+
#
|
|
32
|
+
# @param event [Hash] Event for Ingestion API.
|
|
33
|
+
# @return [Hash] Event for Ingestion API filled with missing properties.
|
|
34
|
+
def enrich(event)
|
|
35
|
+
event[:sourceId] = event.fetch(:sourceId, Config.get[:sourceId])
|
|
36
|
+
event[:timestamp] = event.fetch(:timestamp, Config.timestamp)
|
|
37
|
+
event
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Validates event to conform Ingestion API requirements.
|
|
41
|
+
#
|
|
42
|
+
# @param event [Hash] Event.
|
|
43
|
+
# @return [Hash] Passed back event.
|
|
44
|
+
# @raise [SamsaraSDK::EventValidationError] if any option of the given event is invalid.
|
|
45
|
+
def validate(event)
|
|
46
|
+
@schema.each do |schema_param, schema_value|
|
|
47
|
+
schema_value.each do |rule_key, rule_value|
|
|
48
|
+
@rules[rule_key].call(schema_param, event[schema_param], rule_value)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
event
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'zlib'
|
|
3
|
+
require 'stringio'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'samsara_sdk/config'
|
|
6
|
+
|
|
7
|
+
# Samsara SDK
|
|
8
|
+
module SamsaraSDK
|
|
9
|
+
# Physical connector that Publishes messages to Samsara Ingestion API.
|
|
10
|
+
class Publisher
|
|
11
|
+
# Sends message to Ingestion API.
|
|
12
|
+
#
|
|
13
|
+
# @param data [Array<Hash>] List of events.
|
|
14
|
+
# @return [Boolean] Success or failure of HTTP POST call.
|
|
15
|
+
def post(data)
|
|
16
|
+
url = URI.parse(Config.get[:url].chomp('/') + Config::API_PATH)
|
|
17
|
+
http = Net::HTTP.new(url.host, url.port)
|
|
18
|
+
http.use_ssl = url.scheme == 'https'
|
|
19
|
+
http.read_timeout = Config.get[:send_timeout_ms] / 1000
|
|
20
|
+
request = Net::HTTP::Post.new(url, headers)
|
|
21
|
+
request.body = prepare data
|
|
22
|
+
http.request(request).instance_of? Net::HTTPAccepted
|
|
23
|
+
rescue RuntimeError
|
|
24
|
+
FALSE
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# Wraps data in JSON and optionally compresses it.
|
|
30
|
+
#
|
|
31
|
+
# @param data [Array<Hash>] Data to prepare.
|
|
32
|
+
# @return [String] Prepared data.
|
|
33
|
+
def prepare(data)
|
|
34
|
+
data = JSON.generate data
|
|
35
|
+
send(Config.get[:compression], data)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Gzip wrapper for data.
|
|
39
|
+
#
|
|
40
|
+
# @param data [String] Data to wrap.
|
|
41
|
+
# @return [String] Gzipped data.
|
|
42
|
+
def gzip(data)
|
|
43
|
+
wio = StringIO.new('w')
|
|
44
|
+
w_gz = Zlib::GzipWriter.new(wio)
|
|
45
|
+
w_gz.write(data)
|
|
46
|
+
w_gz.close
|
|
47
|
+
wio.string
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# None wrapper for data.
|
|
51
|
+
#
|
|
52
|
+
# @param data [String] Data to wrap.
|
|
53
|
+
# @return [String] Original data.
|
|
54
|
+
def none(data)
|
|
55
|
+
data
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Helper method to generate HTTP request headers for Ingestion API.
|
|
59
|
+
#
|
|
60
|
+
# @return [Hash] headers
|
|
61
|
+
def headers
|
|
62
|
+
{
|
|
63
|
+
'Accept' => 'application/json',
|
|
64
|
+
'Content-Type' => 'application/json',
|
|
65
|
+
'Content-Encoding' => Config.get[:compression] == :gzip ? 'gzip' : 'identity',
|
|
66
|
+
Config::PUBLISHED_TIMESTAMP_HEADER => Config.timestamp.to_s
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Samsara SDK
|
|
2
|
+
module SamsaraSDK
|
|
3
|
+
# Thread-safe ring-buffer data queue tailored for Samsara Client.
|
|
4
|
+
class RingBuffer
|
|
5
|
+
# initialize.
|
|
6
|
+
#
|
|
7
|
+
# @param size [Integer] Storage size.
|
|
8
|
+
def initialize(size)
|
|
9
|
+
@size = size
|
|
10
|
+
@low = -1
|
|
11
|
+
@high = -1
|
|
12
|
+
@buffer = Array.new(size)
|
|
13
|
+
@mutex = Mutex.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Get current number of items in buffer.
|
|
17
|
+
#
|
|
18
|
+
# @return [Integer] number of pushed elements.
|
|
19
|
+
def count
|
|
20
|
+
@high - @low
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Is buffer full?
|
|
24
|
+
#
|
|
25
|
+
# @return [Boolean] Full or not.
|
|
26
|
+
def full?
|
|
27
|
+
count == @size
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Is buffer empty?
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean] Empty or not.
|
|
33
|
+
def empty?
|
|
34
|
+
count.zero?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Puts element into buffer.
|
|
38
|
+
#
|
|
39
|
+
# @return [Object] Element that has been put into buffer.
|
|
40
|
+
def push(value)
|
|
41
|
+
@mutex.synchronize do
|
|
42
|
+
return if @size.zero?
|
|
43
|
+
@high += 1
|
|
44
|
+
@low += 1 if count > @size
|
|
45
|
+
@buffer[high_position] = value
|
|
46
|
+
value
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
alias << :push
|
|
50
|
+
|
|
51
|
+
# Extract all existing elements out of buffer.
|
|
52
|
+
#
|
|
53
|
+
# @yield [data] Block that processes data and returns success of the processing.
|
|
54
|
+
# @yieldparam [Array<Object>] data Buffer data.
|
|
55
|
+
# @yieldreturn [Boolean] Result of data processing. True if success, false otherwise.
|
|
56
|
+
#
|
|
57
|
+
# @return [Array<Object>] All buffer's elements in FIFO order.
|
|
58
|
+
def flush
|
|
59
|
+
data, at_mark = take_snapshot
|
|
60
|
+
success = block_given? ? yield(data) : TRUE
|
|
61
|
+
delete at_mark if success
|
|
62
|
+
data
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# Helper-method for calculating position in a circle.
|
|
68
|
+
#
|
|
69
|
+
# @return [Integer] position of a given pointer.
|
|
70
|
+
def calculate_position(pointer)
|
|
71
|
+
@size.nonzero? ? pointer.remainder(@size) : -1
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Get array-position of the element that was last added.
|
|
75
|
+
# Must be used within a synced mutex.
|
|
76
|
+
#
|
|
77
|
+
# @return [Integer] position.
|
|
78
|
+
def high_position
|
|
79
|
+
calculate_position @high
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Get array-position of the element that was last consumed.
|
|
83
|
+
# Must be used within a synced mutex.
|
|
84
|
+
#
|
|
85
|
+
# @return [Integer] position.
|
|
86
|
+
def low_position
|
|
87
|
+
calculate_position @low
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Make a snapshot of the current buffer at a given moment in FIFO order.
|
|
91
|
+
# Notes:
|
|
92
|
+
# It could be implemented just by iterating through @buffer and taking
|
|
93
|
+
# elements between @low and @high;
|
|
94
|
+
# but it's not as efficient as Ruby native array manipulations.
|
|
95
|
+
# So we stick to manipulating indices and handling different cases to get FIFO.
|
|
96
|
+
#
|
|
97
|
+
# @return [Array<Object>] actual elements in FIFO order,
|
|
98
|
+
# @return [Integer] High-water-mark that snapshot was taken at.
|
|
99
|
+
def take_snapshot
|
|
100
|
+
@mutex.synchronize do
|
|
101
|
+
i = high_position
|
|
102
|
+
j = low_position + 1
|
|
103
|
+
|
|
104
|
+
data = if empty?
|
|
105
|
+
[]
|
|
106
|
+
elsif @low == -1
|
|
107
|
+
@buffer.compact
|
|
108
|
+
elsif @high < @size # array is not filled completely
|
|
109
|
+
@buffer[j..i]
|
|
110
|
+
else
|
|
111
|
+
@buffer[j..-1] + @buffer[0..i]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
return data, @high
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Removes chunk of elements that present in a snapshot out of buffer.
|
|
119
|
+
# Detects if the last consumed element has been overridden by new pushes.
|
|
120
|
+
#
|
|
121
|
+
# @param mark [Integer] high attribute that represents high-water-mark of a snapshot.
|
|
122
|
+
def delete(mark)
|
|
123
|
+
@mutex.synchronize do
|
|
124
|
+
@low = [@low, mark].max
|
|
125
|
+
@high = [@high, mark].max
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
data/samsara_sdk.gemspec
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |spec|
|
|
5
|
+
spec.authors = ['Egor Kolotaev']
|
|
6
|
+
spec.summary = 'Ruby client library for Samsara Analytics platform.'
|
|
7
|
+
spec.email = %w(samsara.systems+info@gmail.com)
|
|
8
|
+
spec.homepage = 'https://github.com/samsara/samsara/tree/master/clients/ruby'
|
|
9
|
+
spec.license = 'Apache License 2.0'
|
|
10
|
+
spec.name = 'samsara_sdk'
|
|
11
|
+
spec.require_paths = %w(lib)
|
|
12
|
+
spec.required_ruby_version = '>= 1.9.3'
|
|
13
|
+
spec.description = spec.summary
|
|
14
|
+
spec.files = %w(LICENSE.txt README.md samsara_sdk.gemspec) + Dir['lib/**/*.rb']
|
|
15
|
+
spec.version = File.read('../../samsara.version').strip.sub(/[A-za-z-]*$/, '')
|
|
16
|
+
|
|
17
|
+
spec.add_development_dependency 'bundler'
|
|
18
|
+
spec.add_development_dependency 'rspec'
|
|
19
|
+
spec.add_development_dependency 'rubocop'
|
|
20
|
+
spec.add_development_dependency 'simplecov'
|
|
21
|
+
spec.add_development_dependency 'webmock'
|
|
22
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: samsara_sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.5.5.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Egor Kolotaev
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-02-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: simplecov
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: webmock
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '>='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
description: Ruby client library for Samsara Analytics platform.
|
|
84
|
+
email:
|
|
85
|
+
- samsara.systems+info@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- LICENSE.txt
|
|
91
|
+
- README.md
|
|
92
|
+
- samsara_sdk.gemspec
|
|
93
|
+
- lib/samsara_sdk/client.rb
|
|
94
|
+
- lib/samsara_sdk/config.rb
|
|
95
|
+
- lib/samsara_sdk/errors.rb
|
|
96
|
+
- lib/samsara_sdk/event.rb
|
|
97
|
+
- lib/samsara_sdk/publisher.rb
|
|
98
|
+
- lib/samsara_sdk/ring_buffer.rb
|
|
99
|
+
- lib/samsara_sdk.rb
|
|
100
|
+
homepage: https://github.com/samsara/samsara/tree/master/clients/ruby
|
|
101
|
+
licenses:
|
|
102
|
+
- Apache License 2.0
|
|
103
|
+
metadata: {}
|
|
104
|
+
post_install_message:
|
|
105
|
+
rdoc_options: []
|
|
106
|
+
require_paths:
|
|
107
|
+
- lib
|
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - '>='
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: 1.9.3
|
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - '>='
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
requirements: []
|
|
119
|
+
rubyforge_project:
|
|
120
|
+
rubygems_version: 2.0.14.1
|
|
121
|
+
signing_key:
|
|
122
|
+
specification_version: 4
|
|
123
|
+
summary: Ruby client library for Samsara Analytics platform.
|
|
124
|
+
test_files: []
|