Friday, October 2, 2015

Here are a few caveats to using Aria Templates in your web application

Abstract

This post discusses my experience as a first-timer with Aria Template,  what type of challenges I faced, and the lessons I learned that the reader could benefit from.


You may have already known about the robust and awesome way of having templates using Aria Template. If not, check out this short guide here. It indeed looks fancy, sounds like it can help you a great deal but it comes with some warnings.

MIME Types

Most web servers nowadays don't just serve all types of files when requested through a URL. Only the ones that they are configured to serve. Aria template files are saved with .tpl extension, and it also loads one of its internal file with .cml extension in the bootstrap code so you will have to configure your web server software (IIS, apache, nginx etc) to allow those extensions as text/html.


Template Path confusion

Aria turns what you provide in classpath into a relative path to your .tpl file. In my experience, I had thought the following classpath would represent /home/userarea/Profilesnapshot.tpl

{{Template classpath:'home.userarea.Profilesnapshot' }}

But it was actually looking for the said tpl file in /js/home/userarea/Profilesnapshot.tpl where 'js' is same directory where area templates JS file was included from.

Also, the last part of the classpath (in this case Profilesnapshot) should have the first letter upper case (same goes for the template file name).


Conclusion

Be prepared to spend at least 5 hours scratching your head, and trying to figure out why something is not working. Also, if your team has many red-tapes or is one of the ones that are "too busy to improve", it is not easy to sell Aria to them. Not to mention the grouchy admins who will give you all sorts of trouble when you ask them to add new MIME types to the web server.

Sunday, September 6, 2015

CalcRx: The Einstein Equation Fix

Abstract

This is about a bug in Project CalcRx which was causing it to compute the wrong results for Einstein equations or expressions with similar properties. The resolution, and future improvement possibility are also discussed. You can suggest edits to this article at this page.

The issue

There is nothing peculiar about Einstein equations in this scenario that causes this, it is just how CalcRx was coded was throwing it off. Anything with Base expression (or your parameter variable) involved twice across one or more binary operations would fit the scenario. But in my case, I was writing tests for Einstein Equations and failed it at the beginning. To understand the issue better, consider this scenario. You have an application that is monitoring the velocities of an object moving relative to your probe, you want to apply Lorentz Transformation. The following equation is not true Lorentz Factor because of the missing square root in the denominator, we will see later why.
_/(1 - (_ / 299792458) ^ 2)
That huge number is speed of light in a vacuum in meters per seconds. Before the issue was fixed, it was generating an Expression like this
__main.CombineLatest(__main, __main.Select(a0 => a0/299792458).Select(a1 => Math.Pow(a1, 2)))
Pretty ugly right? not only above expression looks ugly it will produce undefined results because of the way simultaneous operations are timed. To understand this better, we need to look into CombineLatest. CombineLatest does not care how the data is coming in, as soon as one of the observables in the combination yields an output, CombineLatest recomputes the expression. So if an expression has to wait for all variables involved to be updated before it could be recomputed will fail. The other option is to use Zip, but CombineLatest is thrown in there for a purpose, there are cases where you need not to wait for all sides to post an update to recompute the formula. So what is the option? The answer is to wrap everything cohesive around in one Select.
The above example would look pretty if CalcRx generated the following expression
__main.Select(a0 => a0 / (1 - Math.Pow(a0 / 299792458)));
But the way expression tree was being built was, every time an IObservable<T> was combined in a binary operation with a number, it would do Select which turns the expression into IObservable, then all subsequent binary operations will do Select on Select. And if you try to combine two Select-s that would try to join them with CombineLatest.

The fix

To fix the issue, I implemented the following.
  1. Everytime you run into a binary computation between a Select-or and a number, re-write that Select-or to include that select inside.
  2. Everytime two observables that are results of Select-s are combined in a binary operation, discard one Select and combine the selectors of both in one Select.

New issues

The fix helped me generate a very clean and acceptable Expression. I have ended my fix there, but there are still two issues left.
  1. The fix is a burden on Parsing because it regenerates the whole Select by prying the existing one open, excavating the selectors out of it and combining it with the other operand into another Select.
  2. Einstein Equation will still fail
To understand 1, consider the scenario where parsing is underway for the following expression
_ + 3 * _
Now the first step would be to generate the Expression for 3 * _ which would generate
__main.Select(a0 => 3 * a0);
Perfect so far right? Now the system has to combine a _ +, the new fix would rip the Expression, remove 3 * a0 which is the selector out, then generate new expression and a new Select call Expression. Imagine how crazy would it be if your expression involve 20 binary operations? The troubles don't end there consider the following expression
(_ + 1) * (_ - 1)
In the above case, you will end up with having to combine two __main.Select Expressions with binary multiply operation. Before the fix, it would combine them using CombineLatest, but thanks to the fix, now it will convert both of them into one Select. But to do that, it would excavate the selectors of both, get the Parameter expression of one, and recursively walk the other selector tree to replace its own parameter expression with the one of the other side. To understand it better you are having to combine the following two
__main.Select(a0 => a0 + 1) * __main.Select(a1 => a1 - 1)
Notice that the first selector is referencing a0, and second is a1. In the new Select, there would be only one parameter, either a0 or a1. In order to do that, I have done something I am not too happy about. I am walking the whole tree on the other side, regenerating every bit of it because Expression trees are non-mutable and replacing a1 with a0.
Now about 2, The Einstein Equation failure. Consider the easier equation that would fail this scenario. How about we divide the number by its square root?
_ / sqrt ( _ )
Now in order to accomplish the above, the square root function or sqrt has to come from either the Function Packs or has to be included in the call to Evaluate, when underscore (_) is an argument to a function, the current architecture expects to find a function that takes observable for that argument and will pass __main to that parameter. A sane implementation however should find a proper overload and do something like following
__main.Select(a0 => a0 / sqrt( a0 ))
That is indeed not expecting too much. For the above I have opened the issue#3 and the fix to that will finish the Einstein Equation fix.

Conclusion

So in a nutshell, the issue was the crazy Expressions being generated, I implemented a fix which although one-time but does add an overhead on the parsing and Expression generation part and Einstein Equation or similar would still fail because of the way function calling is implemented.

Tuesday, August 25, 2015

Allowing end-users to perform custom computations on real-time data

Absract

This post is an introduction to a new project CalcRx that allows you to write applications to give users ability to run custom expressions on Observables. A couple of example scenarios are also discussed. To learn technical aspects about CalcRx in detail, visit the project page. This post does not assume you know about .NET and Reactive Extensions already. But in order to understand the technical aspects of CalcRx, those two are required.

You may have learned the about importance of usability, abstraction, and inference of real-time data already. In this post, I am going to jump straight in to one of the ways that we can accomplish this in .NET Applications.

Scenario 1

First consider this scenario, you have an application that displays the real-time chart of radiation levels monitored in Earth's atmosphere. The satellite is pounding your application with tons of data. The user of your application wants to see only so much, suppose your user wants to see the data where radiation levels are higher than 2 microsieverts, but your application is getting the 100 readings per seconds and your chart is animating too fast to be effectively read by the users. Now the users are telling you to show only the levels higher than 2 microsieverts, or one user wants to see only those spots where the radiation level is 2+ for at least 1 minute other wise he or she wants to ignore the reading.

Scenario 2

Another scenario is, you are the developer of the application that is showing the live chart of stock prices. Every time there is a tick, the chart has a point added. Suppose your user is monitoring Apple's stock which is changing every 2 seconds, your user does not like to be overwhelmed with a lot of unrelated information, he asks you to change your application to show you the Moving Average of last 5 ticks only. While your code change might work for the user watching Apple's stock, it will not do much good for the person who is watching a stock that does not change frequently.

CalcRx can help you in both of the scenarios described above. As long as your application is a .NET Application and the radiation levels or stock prices you are getting are received in IObservable<T>, CalcRx can make the job so much easier.

IObservable<T> is an interface for an implementation of a Publish-Subscribe model. You can convert anything coming into your .NET Application into IObservable<T>. For instance, if you have an event that is fired up every time you receive a new reading of radiation level, you could easily create an observable out of it. For more information, Checkout www.introtorx.com.

Once you are receiving your data through an Observable. You don't have to change your code anymore. You just add a Textbox to your application and ask your users to write their own custom formulas into that. CalcRx uses the expression language which is very close to what Excel uses for formulas.

For a detailed into on CalcRx, visit the Project Page

Tuesday, June 16, 2015

Lets be honest, Java is a piece of crap. Here is why

Many people including the fanbois or the ones who have put all their eggs in the Java basket and think that there is no way out would get angry at my post, and will get very very defensive and I understand that. But have a little open mind and think about it for a moment, isn't it time we kill Java? Here is why



It was cool

Sure it was the next big thing when it first rolled out, it came up with some new ideas (and overly exaggerated claims). The first and foremost was the Platform independence. Which made many feel warm and fuzzy, it also did a great job in promoting the rapid development. It put many people who would otherwise not code into coding.


It was crazy

Lets first talk about this crazy idea of Platform Independence. If there is one plain simple way to describe it, it is described in this bash.org quote (CAUTION: Its not PG). But really the point is about the usefulness of this feature. Sure, it will work for hobbyist coders who are writing cool command line utilities and they just want to give out binaries. But in today's programming world of writing portable code and compiling from the source (which has been that way in *nix environments for a very long time); this idea does not fly anymore. I mean, how many enterprise applications do you see who target their servlets to run on Linux and Windows machines at the same time? They choose one or the other and they stick with it.

Mistakes and fallacies in Java and Sun's failure to address those caused Microsoft to create C# (ok may be not "create" but you get my point). And C# caught on very good, many fanbois argue that C# copied the semantics of Java, I would say, Rub it in!! because Java copied the semantics of C++. Just moving those little access modifiers to the function signatures doesn't really make you creative. C# not only addressed the issues the rapid-development world was having, it added many great features afterwards to make people's lives easier and nowadays its Java trying to catch up by "copying" C# features.


Fanbois are dying for attention

Sure Java is dying slowly, Sun's acquisition by Oracle is a sign. Oracle possibly bought Sun because Java has been Oracle's lovechild for quite a long time and they had quite a few of their eggs in Java's basket. So what are the fanbois doing? They are dying for attention, while the developers of languages like Python, Haskell, and C# etc are busy making this world a better place, the hustlers of Javaland are busy using allegedly "cool" ways to create some PR. Here is one example. And of course the "3 Billion Devices Run Java" banner whenever you install Java. I say this to that: 3 Billion devices Run Java, rest of them work fine.


The Update-Diarrhea

And lastly, the everyday Java updates that are annoying as hell. You go to one website where their crap is written in Java and you are required to install Java to see their content. You make that mistake and now you are facing the Update-Diarrhea of Java. You would never possibly be going to any website again in six months that require Java to run, but Java will keep Java Update Scheduler (jusched.exe) running in your PC all the time taking up CPU time, memory, and sucking your internet. And the real fun begins when a new update is out, which is out like every freaking day, and the updater will bug you over and over. I have to be honest, I have a pet peeve with this Update-Diarrhea thing; what the hell is so important about you that you are spitting out updates too often and you are showing those pesky popups begging the users to click on that 'Install' button? I tell you why, Java is struggling, it is dying to get your attention.

Oh no they're here, hide Java immediately!

Lastly, I would finish my rant with this. Whenever we have aliens visiting us, we will be under pressure to show them the human ingenuity. We will show them the Great Wall of China, we will show them the mixed-fraction of Pi (3.14...), there is quite a lot to show off to them to impress them. BUT there has to be a list of things that we should hide so we don't sound unenlightened halfwit idiots and not worth exploring, and Java tops that list.

Feel free to disagree with me and let me know your thoughts in the comments below

Sunday, May 10, 2015

SignalR-based Live Stock Ticker with Ticker.FSharp

Introduction

It was an awesome weekend, finally got my hands into SignalR and updated the Ticker.FSharp to add a SignalR web sample ticker. In this post I have talked about how easy it was to throw a simple ticker together using the Ticker.FSharp repository and SignalR. The sample can be downloaded from the Github repository page.

Screenshot





Implementation

I added an empty web project to the solution, then added a SignalR Hub, a Javascript file for event handling, an HTML page for the UI. Here is a quick overview of each of those.

SignalR Hub

As we know, the server-side hub is a primitive component of a SignalR application. My sample hub has only two methods, one is called by the client to start up the hub when it gets launched the first time, and other method is called from the server side that reports the ticks generated by Ticker.FSharp. The call to Tick then channels in to the browsers of each user connected.

The full source listing for the hub file can be found at this Github page. I am only discussing the important points.

Here is how the Hub is declared

[HubName("tickerFsharp")] public class TickerFSharpHub : Hub
As you can see, the hub declaration contains the HubNameAttribute which tells the SignalR engine how the hub is going to be located. There are two methods inside the hub, one is StartTicker which is responsible for calling the Ticker.FSharp library and obtaining the IObservable<Tick> object. Here I am fetching the ticks of three symbols together.

[HubMethodName("startTicker")]
public void StartTicker()
{
if (subscription == null)
{
var machine = new DataMachine();
var aapl = this.AddIndicator(machine.GetLiveGoogleApiTicks("AAPL"));
var goog = this.AddIndicator(machine.GetLiveGoogleApiTicks("GOOG"));
var msft = this.AddIndicator(machine.GetLiveGoogleApiTicks("MSFT"));

subscription = aapl.Merge(msft).Merge(goog).Subscribe(t => {

this.Tick(t.Tick.Symbol, t.Tick.Price, t.Tick.LastUpdated.ToString(), t.Up);

});.
}

In the above listing, I am calling the Ticker.FSharp 3 times to get the live feed for three symbols. I am also calling the AddIndicator helper method to garnish the object with an additional property that tells me whether the value went up or down since the last value. And finally, I am calling the Tick method which calls the tick method at the client's side.

public void Tick(string symbol, double price, string lastUpdated, bool up)
{
Clients.All.tick(symbol, price.ToString("###0.00"), lastUpdated, up);
}

The above Tick() method implementation is pretty self-explanatory.

Javascript Event Handling

The JS file has quite the code, I am only discussing the tick() handler that server-side will be calling.

hub.client.tick = function (symbol, price, lastUpdated, up) {
var row = $stockTableBody.find("tr[datasymbol=\"" + symbol + "\"]");
if (row.length == 0) {
var templatedRow = rowTemplate.replace("{Symbol}", symbol)
.replace("{Price}", price)
.replace("{lastUpdated}", lastUpdated);
$stockTableBody.append(templatedRow);
}
else {
var cells = row.first().find("td");
cells.eq(0).html(symbol);
cells.eq(1).html(price);
cells.eq(2).html(lastUpdated);
}
}

The above code is simple, as the tick arrives, it checks to see whether the table already has a row for that symbol, if so it updates the data otherwise it adds a new row for that symbol.


Thats all for this blog, if you want to check out the full source code, I encourage you to visit the Github project page.

Happy coding!

Saturday, May 9, 2015

SignalR - Damien Edwards' MoveShape sample . Getting it to work with latest SignalR + Sample Project

Introduction

I had a good learning experience with SignalR. I started by watching Damien Edwards Video on YouTube. Now I am officially the SignalR newbie and I am hoping to keep climbing this ladder in upward direction :)

In this post I would like to share my experience with the Move Shape example that Damien showed in his video. I initially followed all the instructions as he demonstrated, but I failed to get it to work. I then read through the SignalR documentation and found that the latest version has a few changes since that video was filmed. I corrected the example and this post contains all the things that are different and of course the sample project source code for you to download. So lets get started.

Screenshot


As you may have noticed in the video, this was how the example worked. You drag and move the box in one browser, the box in other browser will follow the movements.




Changes

First thing you would need to change is in the Hub definition file. Or the MoveShapeHub class. There are a couple of changes in the class code.

  1. Hub methods have to be attributed using HubMethodNameAttribute
  2. Instead of calling the client method on Clients object, you have to call it on Clients.All property.

So the new code looks like this (notice the highlights)

    [HubName("moveShape")]
    public class MoveShapeHub : Hub
    {

        [HubMethodName("moveShape")]
        public void MoveShape(int x, int y)
        {
            Clients.All.shapeMoved(Context.ConnectionId, x, y);
        }
    }


The next thing that is changed is the way hub was extended. If you remember the video, he used the $.connection.extend method. But now you just have to define it like a regular prototype

    hub.client.shapeMoved = function (cid, x, y) {
        if ($.connection.hub.id != cid) {
            console.log("Yes");
            $shape.css({ left: x, top: y });
        }
    };

Also notice that instead of hub.shapeMoved, it is now called hub.client.shapeMoved. Next change goes inside the drag handler. Instead of calling hub.moveShape, you have to call hub.server.moveShape.

so the Hub's start-done handler looks like this

    $.connection.hub.start().done(function() {
        $shape.draggable({
            drag: function () {
                hub.server.moveShape(this.offsetLeft, this.offsetTop || 0);
            }
        });
    });


And you're done. Happy coding!

Downloading the sample project

You can download the full-blown VS2013 Solution Here.



Sunday, April 5, 2015

Is the technology really making us better?


This blog post discusses my thoughts about technology that I use on daily basis, what I love about it, what I don't love, and how I feel about technology’s effect on our cognizance. I have given a few examples to support my claims


Technology today has advanced so much that we depend on it and it will not be unfair to say that we cannot survive without it. There have been emergencies declared in places where technology was out temporarily in the past. We use technology everyday while at home or driving on the road, this has become so transparent in many places that we use technology not knowing we are using it. For Instance, it was only very recently when I found out the traffic lights were controlled by a technology that is sensitive to the traffic activity and they were not using the timers anymore.
What I really love about technology that I use on daily basis is; first, it has made my life a whole lot easier, I can do things a lot faster than I used to do without technology, and it has helped with my productivity. The famous "do less achieve more" comes to my mind. For example, my earlier travels to New York City was very time and effort consuming and at the end of the day made me real tired leaving me with only so little sense of accomplishment. I had to keep a lot of maps for riding Subways and Buses, keep track of schedules and if something doesn't go the happy path, it would take a whole lot of extra time to correct what I missed. But now I just keep my smartphone in my pocket, I just tell Siri what I would like to do. She would suggest me places to choose from and once I have picked a place she would also tell me which way to walk towards to get what bus or train, where to make connections, and what time I should expect myself to arrive by.
Another Example of technological things I enjoy is my DVR at home, I don’t have to worry about missing my favorite TV show episodes anymore, I can watch it at my schedule. It also allows me to pause when I have to answer the door or take a bathroom break. I can also rewind to watch my favorite scenes again, and it also allows me to watch wherever I am over internet; not just at home.
Now coming to what I don't love so much about technology. There are many ways the technology does not make me happy. The biggest one is, when it stops working as it does a lot of times; it brings your world to a grinding halt. Take that New York City mass transit example, when you find out that the cellular data service is not available in the area you are in; you are on your own with no plan B. Another thing I hate about technology is that it is expensive. You have more bills to pay when depending on technology than when you live without it.

I don’t think technology has made us any smarter, it has actually the reverse effect in many cases. It is cutting our connection to the knowledge of how things used to be done without it and has convinced us up to some extent that we are not going to need to learn about the ways our ancestors lived their lives.
For example, according to Environmental Protection Agency (EPA) census survey of 2007, only 1% of the Americans reported farming as their occupation. Which concludes that 1% of the population is growing food for the whole population. How is it possible? Technology. Agriculture workers are using airplanes to spray fertilizers, or pesticides over the crop; they are using big harvesting machines that does the wonders most of us are completely oblivious to. But this also concludes that most of 99% of the us may not know how the food is grown and will not be able to if required for our own survival.
Technology is keeping people away from reading books and do research to invoke the healthy brain activity. People rely too much on technology to just grab the exact piece of information with little to no effort of searching through the books and magazines. This has gone so far out of the hands that the word Google is added to Oxford dictionary as a verb which means to search using Google.com.


Conclusion

Technology comes with pros and cons, on one side it makes our life easy but on the other side it comes with requiring you to plan the usage of it. It does amazing things for us and takes care of a lot of things for us. While using it for our own good, we should never stop the process of the human learning process that is being passed down to descendants for ages. This helps us determine how things have evolved and also help us invent new ideas of how to make technology even better.