Windows Azure Mobile Services : Scripting a Scheduler to dump Live Forex data for future analytics
Mobile Services on azure allows you to add structured storage, user authentication and push notifications to your Android, iOS, HTML, Windows Store, or Windows Phone 8 apps. Have a look at the below list of features. You can visit mobile services website if you are a beginner.
What are we going to achieve?
As a proof of concept, let us create a mobile services scheduler job on azure to get the live Forex data from an API and then load the data into a table recursively. (It simply means, every x minutes, our scheduler gets the fresh Forex currency data and stores it in a structured storage). The data we store can be used in a variety of ways later. You can push the data to end user's mobile phones through push notifications or you may want to draw a currency chart on your users mobile phone screens. It is up-to you. Here we go.Creating a Mobile Service
Login to Azure management portal, navigate to the mobile services tab. First, let us create a Mobile Service.Click on the "create a new mobile service" link (see above screen) and provide the details of your new mobile service (like in the screen below).
go Next. Specify the database settings like I have done below.
Finish (Click on the check mark above). Wait for a few seconds. Your mobile service will be created and ready to use. You will the below screen.
Adding a table to store data
Click on the Forex-Loader mobile service (The arrow pointing towards right) in the above screen. Mobile Service screen will get displayed. Click on the DATA tab to see the below screen to start creating a table.Now, Add a table with details below. (Well, you can give your own set of permissions to be safe. I gave 'Everyone' just to make it quick)
Once you click Finish (check mark) in the above screen, table will be created with a index and with no records automatically. See the below screen on how it looks. (Now, don't worry that you did not provide details of fields, datatypes etc. Azure will do the magic!. Fields are created automatically at the time of loading the data. You don't have to define it well in advance).
Creating a Scheduler Job
Let us create a scheduler. Click on the Scheduler tab. You will see the below screen.Click on the "Create a scheduled Job" link to create a new job. Provide the details as mentioned below and Finish.
While creating a job, you may notice that you can schedule to run the jobs in different time frames in a variety of available combinations. You can select the option 'On demand', if you want to manually run the job.
Now, your Forex job is created. (screen below)
Let us now write some code to do the actual task. Click on the right arrow to open the ForexJob you just created. When the Forex job opens, by default it will take you to Configure tab. Now, you should navigate to the SCRIPT tab. You will see something like the below screen.
Delete the default javascript function you already see (screen above) and add the below code instead. It is a simple script. Scheduler allows you to write your own script for doing the job.
var updatesTable = tables.getTable('ForexLive'); // Table Name to store data var request = require('request'); var url='http://api.apirates.com/api/update'; // This api gets the fresh forex data function ForexJob() { //Notice that the function name is same as the Job Name request(url, function ForexDataLoaded (error,response, body) { console.log("------BEGIN------"); //Log it var list =JSON.parse(body).ticks; //Read the JSON data and store it as object var now = new Date(); //Get current date time for (var currency in list) { var data = { Currency_Name: currency, Present_Value: list[currency], Date_Time: now }; //Prepare the object updatesTable.insert(data); //Insert data to the updatesTable console.log(currency + " ::: " + list[currency]); } console.log("------End------"); }); }
Code is self explanatory. Read the comments. To summarize, we already have an API from APIRates which gets fresh Forex currency data when you call it. The output returned by the API will be in JSON format. (Just try hitting the API URL in browser. You should see the latest Forex ticks. An API call always gets you the currency pair values of that moment changes every now and then.)
Below screen shows how it looks after you add the above code.
Click on the save button (above screen) to save the Job.
Before running the Job, It is very important to Enable it first. Click on the Enable button in the above screen.
Running the job once
You are almost done with it. Now, if you do not want to wait for few minutes to see the job running, just click on the "RUN ONCE" button to forcefully run/test the job once.
Congratulations. You just ran a new Job. Waiting to see the result?. Just click on the Back button as you see in the screen (below).
You will see the below screen. Now, open the DATA tab
Open the ForexLive table (Click on the Arrow pointing towards the right) and you will realize the Magic that has happened !!
The scheduler has done its job and loaded fresh data to the table. Remember, this will happen every x minutes you selected earlier. Observe the Column Names of the table and the way it got created :). To your surprise, an Auto Ident column got created too!
Use this data and do whatever you want, show it to users at run time, push it to phones, do analytics, come up with fresh charts or what not. Hopefully this post helped you to learn a thing about Windows Azure Mobile Services.
For windows azure mobile services scripting reference, click here
Happy Learning :)
NOTE : Delete the Mobile Service after doing this exercise. You will end up paying HUGE Bills otherwise as it silently runs in the background every now and then costing you.