Thursday, 15 August 2013

Subtract one Javascript Arry from another

var array1 = [1,2,3,5,7,9];
var array2 = [3,5];

var arrray1MinusArray2= $(array1).not(array2).get();

// result is arrray1MinusArray2 = [1,2,5,7,9]


var arrray2MinusArray1= $(array2).not(array1).get();

//result is arrray2MinusArray1=[]

It works with both primitive and object type. 

Sunday, 7 July 2013

Durandal event publication

Lets say a page is composed of 3 child pages with their own view model. Some user action on one page needs to send some data to other child or parent page(s). This article shows how this can be done easily in Durandal.

Durandal view model are not shared or visible to each other by default. This is to keep the structure clean.

There is a view model tree propagation mechanism (preserveContext:true). But it's turned off by default. This is to be used on very specific scenario. More details about Context are available in the reference document in http://durandaljs.com/documentation/Composition/.

To handle generic interactions among view models Durandal exposes nice set of API. This is done using event publication and subscribe mechanism.
Event source publishes an event with an object argument. All interested parties registers for that event. When an event is raised by its source the framework passes it to all registered clients. As simple as that. There is a nice video on this in you tube by Ryan Keeter (DurandalJS and Pub-Sub with Events http://www.youtube.com/watch?v=wuCpf-Tvff4).

Following code example walks through the mechanism:
This image shows a page ( created on the Durandal basic MVC template) containing two child pages.

Container page published a event and it's subscribed by both child pages. Similarly child page 2 also generates event's that is being subscribed by both child 1 and 2. 
This is the code of the container page. Nothing really special here. One button to generate event and two composition. 
 <section><div class="container">  
        <div class="row" style="background-color: powderblue; height: 400px">  
          <div class="span4" >  
            <h2 data-bind="html: displayName"></h2>  
            <button data-bind='click: registerClick'>Click me</button>  
            <!--Sidebar content-->  
          </div>  
          <div class="span4" data-bind="compose: { model: 'viewmodels/sub', activate: true }" style="background-color:mistyrose; height: 300px"></div>  
          <div class="span4" data-bind="compose: { model: 'viewmodels/sub2', activate: true }" style="background-color:navajowhite; height: 300px">  
          </div>  
        </div>  
      </div>  
 </section>  

Here is the view model:
define(function(require) {

    var self = this;
    self.app = require('durandal/app');
    self.registerClick = function() {
        self.app.trigger('myevent.name', { someValue: 'parent' });
    };

    return {
        displayName: 'Parent Page',
        registerClick: self.registerClick
    };
});

The magic starts from the highlighted line. Durandal App module provides a trigger method to publish an event. It takes a name and object. Subscriber subscribe using this event name and the object is passed to the subscriber as an event argument. 

Child view 1
 <section>  

   <h3 data-bind="html: displayName"></h3>
   <h3 data-bind="html: eventMessage"></h3>
   <h3 data-bind="html: eventArgument"></h3>
 </section>


Child model 1:

define(function(require) {
    var self = this;
    self.app = require('durandal/app');
    self.eventCount = ko.observable(0);
    self.eventArgument = ko.observable("no event.");
    self.eventMessage = ko.computed(function() {
        debugger;
        if (self.eventCount() == 0)
            return "No event received Yet.";
        return "Number of event received: " + self.eventCount();
    });

    self.myEventHandler = function(eventArg) {
        self.eventCount(self.eventCount() + 1);
        self.eventArgument("Event Received from :"+ eventArg.someValue);
    };
   
    return {
        displayName: 'Child Page 1.',
        eventMessage: self.eventMessage,
        eventArgument:self.eventArgument,
        activate: function() {
            self.app.on('myevent.name', self.myEventHandler);
            self.app.on('myevent.child', self.myEventHandler);
        }
    };
});

The highlighted line above is the subscriber. The on method of Durandal app module registers a function for an event. First argument is the event name and second argument is a method.


Following image shows when parent published the image.

Image below shows when child 2 publish an image. 

This shows event can be published from anywhere and subscribed by anyone.


Just for completeness source code of Child View 2:
 <section>  

   <h3 data-bind="html: displayName2"></h3>
   <h3 data-bind="html: eventMessage2"></h3>
   <h3 data-bind="html: eventArgument2"></h3>
   <button data-bind='click: registerClick2'>Click me</button>
 </section>


and Child model 2:


define(function(require) {

    var self = this;
    self.app = require('durandal/app');
    self.registerClick2 = function() {
        self.app.trigger('myevent.child', { someValue: 'Child2' });
    };
    self.eventCount2 = ko.observable(0);
    self.eventArgument2 = ko.observable("no event.");
    self.eventMessage2 = ko.computed(function() {
        debugger;
        if (self.eventCount2() == 0)
            return "No event received Yet.";
        return "Number of event received: " + self.eventCount2();
    });

    self.myEventHandler2 = function(eventArg) {
        self.eventCount2(self.eventCount2() + 1);
        self.eventArgument2("Event Received from :" + eventArg.someValue);
    };
    return {
        displayName2: 'Child Page 2',
        registerClick2: self.registerClick2,
        eventMessage2: self.eventMessage2,
        eventArgument2: self.eventArgument2,
        activate: function() {
            self.app.on('myevent.name', self.myEventHandler2);
            self.app.on('myevent.child', self.myEventHandler2);
        }
    };
});

Tuesday, 12 March 2013

A different take on sprint velocity

Sprint velocity is an useful stats for any project or team. It can provide many useful information including:

  1.  Teams throughput at the present
  2.  How much team is likely to accomplish in next sprint and thereafter. Therefore it allows a planner to prepare a roadmap.
  3.  What’s the trend:
  4.          Past à is the team progressively doing better?
  5.          Future à what’s the likely throughput over next quarter or half year and so on?

Following chart shows velocity of my current project sprint over about 9 months’ time period.
Figure 1

But simply using the velocity chart above has some inherent drawbacks. The output varied largely in every sprint. If  I just take simple average of all sprint that does not predict next sprint velocity. Its in fact 30% less than what my current sprint's output is. The result get's bit better if I take a moving average.
The reason behind this discrepancies are not random. There are some valid explanation for this. Simply taking average over a period of time and using that for future prediction is equivalent to working with ideal working condition. And as it said reality is always far from the ideal.
One of the most common of this variable is change in team. Over a period of time in evidently team member will change. Some people will leave. Few more will join. Leave could be temporary, where one of the team members may be loaned to another team for few months and will come back later. Whatever is the nature of the change, it will affect the velocity.
Second is the absence or holiday. All absence will affect sprint velocity.
Third is the length of the sprint. Scrum team normally works with an fixed sprint length  of 2, 4, 6 etc. weeks. This too changes for various reasons. Holidays, emergencies and  whatever unforeseen reason sprint length could change and do change.
Now that we know there are variables. How do we do to overcome this variable and get better result out of sprint velocity? Read on..

Effective Resource

First we need to calculate effective resource for a sprint. Following spreadsheet shows an example of such calculation.
Figure 2
For every sprint( possibly before or in sprint planning) note down all the available resources to the project. Then check
     Is  any resources are shared? to update commitment percentage accordingly.
     Have you got a new resource joined to the team? He will need to set up time or catchup time. Therefore to reflect that you need to change his or her commitment percentage.
     Is any one has planned holiday? Add that to non working days.
     Is there a holiday day in the sprint? deduct that from ideal sprint length to calculate current sprint length.

Once you have all of this data, you can calculate how much effective resource is committed to a ideal length of sprint.

Single Resource Velocity

Once you have effective resource of a sprint, you can calculate what the throughput/achievement of that resource on a given sprint. Tracking this throughput over multiple sprint will give single resource velocity. following chart shows single resource velocity of my project.
Figure 3

If we compare Figure 3 with Figure 1 we can see the variation has calmed down over later part of the chart. The trend line quite leaner. Therefore we can use this figures to calculate some of the stats that we are interested in regards to sprint velocity.

How do we use this data

Now the single resource velocity is change proof. The above calculation normalizes all the variance we have talked about. Therefore we can calculate/predict future sprint output much more correctly.

Simply calculate your effective resource availability.
Then multiply that with single resource average velocity ( better if you use moving average ).

To predict further future throughput ,we only have to know on an average how many resource will be available to he team.  

Thursday, 7 March 2013

Durandal SPA template for MVC

i was trying with all the SPA template available with ASP.NET MVC. finally decided to go with Durandal template. It seems to be full filling all my selection criteria:

  • html and javascript are in separate file
  • html parts, and related JavaScripts are loaded automatically by the framework
  • and easy way to load relevant data though activation
  • good routing
Durandal tick all the box perfectly. I installed the template and create my first application. Sample works like charm. Shows some images from flicker. 
Then To test a sample webservice call to my code, I've added my own WebApi controller and tried same way as flicker call.
But it's not working.  I can see in fiddler that it's making the ajax call and a json response is coming back from the server, but for some reason framework is not invoking the promise( response call back). No error is shown. Tried debugging and it's not easy either.
After long try with this and that finally decided to gove durandal http module a ditch and try jquery ajax call directly. i provided the error call back as well and BANG! 
Error!

Parsing error.
Because I was simply following the template. I was using the same jsonp data type as used in the flicker call. But my MVC Web API is not returning jsonp. It's json only! :) 
So changed the call to http.get and Hurrah!!! all is working.


 var that = this;
            //debugger;
            return http.get('http://localhost:63795/api/projects').then(function (response) {
                that.projects(response);
                });


Tuesday, 4 December 2012

Cycle through open applications


I was trying to setup team display. It supposed to display JIRA, build monitoring etc. And rotate this windows one after another after a certain interval. Finally one of my colleague give me this little script and it worked like charm. Here it is:
Its a VBS script. works in Microsoft windows.

1. Save following five line of script in a text file with .vbs file extension.
set objShell = CreateObject("Wscript.Shell")
While true
    objShell.SendKeys "%{TAB 11}"
     Wscript.sleep 10000
Wend


2. Change the number 11 (after TAB) to the number of open application that you want to cycle through

3. Save the file. 

4. Open all your applications that you want to cycle through

5. Run the script, simply by double clicking it.

It going to change screen every ten seconds. To stop the script you have to kill the Wscript shell. For that, open task manager, go the process tab and find wscript.exe and kill it. There may be multiple wscript instance running. And you may not have access to kill them all. Don't worry. 


The above loop run in a infinite loop, to make it finite you can use something like this :

set objShell = CreateObject("Wscript.Shell")
for i = 1 to 10000000
    objShell.SendKeys "%{TAB 11}"
    i = i + 1
    Wscript.sleep 10000
Next