MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
Â
MongoDB Stitch Dashboard and API Builder
1. #MDBlocal
Todayâs Pre-Reqs
For todayâs workshop youâll need the following:
â Computer
â Preferably a Mac
â Or a working knowledge of Windows + NodeJS
â MongoDB Atlas Cluster running 3.4+ (can be free tier)
â NodeJS 6.0+/npm
â Important:
â Files: https://github.com/mongodb/stitch-examples
â Under âDashboardâ
â Documentation: https://docs.mongodb.com/stitch/getting-started/dashboard/
3. #MDBlocal
Agenda for this Workshop:
Intro to Stitch
+ Atlas
What weâll
work on today
Creating a
simple API
Building a D3
Dashboard
Adding
Authentication
Next steps
with Stitch
01 02 03 04 05 06
4. #MDBlocal
Todayâs Pre-Reqs
For todayâs workshop youâll need the following:
â Computer
â Preferably a Mac
â Or a working knowledge of Windows + NodeJS
â NodeJS/npm
â Important: Get the files/README (https://goo.gl/CU1JV1)
6. #MDBE17
MongoDB Query Language + Native DriversIntegrated Rules
Pipelines 3rd Party Services
Native SDKs (iOS, Android, JS)
Rest-like API
https://docs.mongodb.com/stitch/getting-started/dashboard/
7. #MDBE17
Stitch letâs you control the flow of data:
Integrated services and
Pipelines for complex,
multi-stage workflows
Native SDKs for Android,
JS, and iOS clients
Direct Database Access
https://docs.mongodb.com/stitch/getting-started/dashboard/
13. #MDBlocal
Our Data
*Point-of-sale data from a Pizza shop
(But Similar to Point-of-Sale, Log, Telemetry, etc. data you
would see elsewhere)
* Our Goal
https://docs.mongodb.com/stitch/getting-started/dashboard/
14. #MDBlocal
1. Data enters through Stitch, flows into Atlas
2. Stitch can augment data loading:
a) Grabbing extra data from an external API
b) Taking an action on load (e-mail receipt)
3. Can expose data through API, Web/mobile
app, or Atlas
(Remember: You can also use Stitch with
an existing database too)
Behind the scenes, Stitch connects Everything
Dash +
Auth
MongoDB
Stitch
MongoDB
Atlas
Sales
Data
API Access
https://docs.mongodb.com/stitch/getting-started/dashboard/
22. #MDBlocal
In Stitch, you opt-in each namespace in your
database separately for access.
1. Go to SalesReporting.Receipts
namespace
2. Delete the default Filter under âFiltersâ
3. Delete the default rules (including Owner
Id field rules)
4. Set the Top-level Document Read/Write
rules to {}
5. This enables Public read/write to this
namespace only
Next, set-up simple Access Rules:
https://docs.mongodb.com/stitch/getting-started/dashboard/
23. #MDBlocal
Now letâs load data:
1. Open the data_generator.js file
2. Add your App ID (line 10)
3. Run data_generator.js
4. Watch data load into Stitch!
https://docs.mongodb.com/stitch/getting-started/dashboard/
25. #MDBlocal
In Stitch, you can add a number of predefined services
to your app.
Services have predefined actions to ease integration.
Weâll use the HTTP Service to create an API to our data
1. Click âAdd Serviceâ
2. Click âHTTPâ and name your service âSalesAPIâ
Create a HTTP Service:
26. #MDBlocal
Create a Webhook:
For the HTTP service, you can create an
incoming webhook that will execute a
function in response to a request.
1. Within AggregateToppings, click âNew
Webhookâ
2. Name your new webhook âGetSalesâ
3. Switch âRespond with Resultâ ON
4. Click âRequire Secret as Query Paramâ
5. Set Secret to âSECRETâ
27. #MDBlocal
Get the distribution of toppings for the last 100
pizzas.
1. Function that uses aggregation to:
a) Sort the orders in descending time order
b) Limit the number of orders to 100
c) Group the orders by topping
Add a Function to the Webhook:
https://docs.mongodb.com/stitch/getting-started/dashboard/
28. #MDBlocal
Now you can access your data via HTTP!
curl -X POST -H [WEBHOOK_URL]?secret=[SECRET] | python -m json.tool
30. #MDBlocal
Add a Function:
SalesTimeline â Get the sales for a given time
interval
1. Find all the sales in a certain timeframe
a) Projects fields down to just timestamp/total
b) Sorts by timestamp
c) Limits to 100 documents
2. Make sure function is named âSalesTimelineâ
3. Set âCan Evaluateâ to {}
32. #MDBlocal
1. Open the index.html file
2. Add your App ID (line 8)
3. Go to your app directory
4. Run your app with âhttp-server &â
a) Note: You may need to install http-
server via npm (install with âg flag)
b) You can also use any other method of
opening the dashboard
Now letâs start the Dashboard:
https://docs.mongodb.com/stitch/getting-started/dashboard/
35. #MDBlocal
1. Go to âAuthenticationâ in the Stitch UI
2. Edit Email/Password configuration
a) Click the slider to enable
b) Set Email Confirmation URL and
Email Reset URL
c) Click âSaveâ
3. Add our first e-mail/password user
a) Click the âUsersâ tab
b) Click â+ Add Userâ
c) Enter an email address and
password of your choosing and
click âCreateâ
Add E-mail Auth:
36. #MDBlocal
1. Next go back to âProvidersâ, find âAPI
Keyâ authentication and click âeditâ
a) Click the slider to enable e-
mail/password authentication
b) Click âCreate API Key,â assign your
API Key a name, and click âSaveâ
2. Click âShow Key...â and then copy key
Add an API Key:
https://docs.mongodb.com/stitch/getting-started/dashboard/
37. #MDBlocal
1. Open the data_generator.js file
2. Comment out line 38, Uncomment line 41
3. Add your API Key (line 41)
4. Re-run data_generator.js
Add Auth to the Loader:
https://docs.mongodb.com/stitch/getting-started/dashboard/
38. #MDBlocal
CheckAuth â Check the authentication type of a
user for a security rule
1. Take an auth type and see if the given suer
matches it.
2. Name function âCheckAuthâ
3. Set âCan Evalutateâ to {}
Add a Function:
https://docs.mongodb.com/stitch/getting-started/dashboard/
39. #MDBlocal
1. Go to rules under the rules tab of
âmongodb-atlasâ in Stitch UI
2. In the âSalesReporting.Receiptsâ, edit the
Top-level Document Read and Write rules
(see README)
3. Add the timestamp/total fields into the
namespace
4. In both of these fields, add a read rule to
check a userâs authentication (see
README)
Set-up secure Access
Rules:
40. #MDBlocal
1. Open the index.html file
2. Comment out line 15, uncomment line 16
3. Re-run data_generator.js
Add Auth to the Dashboard:
https://docs.mongodb.com/stitch/getting-started/dashboard/
43. #MDBlocal
⢠Build out a more robust public API to access your data by adding
more pipelines and webhooks
⢠Add pipelines and using the data to create more graphs in your
realtime dashboard
⢠Use one of our integrations to build extra features like e-mail receipts
for customers or a slackbot for placing an order.
⢠Take the ideas here and building an API or dashboard with your own
data
Have more time?
45. #MDBlocal
⢠Get more info about Stitch at
⢠Try out Stitch â stitch.mongodb.com
⢠Check out SDKs and examples
⢠Code at github.com/MongodbStitch
⢠Docs at docs.mongodb.com/stitch
⢠Build the Dashboard or Weather IoT apps in our Tutorials section
⢠Ask Questions or let us know what youâre building through Intercom
What Now?