Sunday, November 26, 2017

Javascript closures - Angular approach

One big monolithic app 


Let's create a module in angular

// Below code is a setter which instantiate an angular module
angular.module('app',                           ['ui.router','toaster','ngAnimate','chart.js']);


The above approach is used best when you are creating a monolithic angular application where your application consists only a one big module and all the controllers, configurations, the routes, directives, factories, and the services are linked together.

but if you wish to create an angular app where there's separation of concerns. You'll definitely have to breakdown your application into reusable modules.

The modular way of doing things

(function () {                                          
       'use strict'; 
       angular.module('app', [
                     'ui.router','toaster','ngAnimate', 'chart.js'
           ])
})();

but wait. when you do it like this your app can't be accessed from the global scope. Then you'll have to turn the whole app into a collection of modules.

(function () { 
       'use strict';
        var app = angular.module('justforfunApp', ["ui.router","toaster","ngAnimate","chart.js" ]) 
})() 
//Below code won't execute since the app can't be accessible from the global scope
app.run(function ($window) {
        // some operations here
})


But why ?

If you declare the app variable as a  global then your variable maybe conflict with other javascript variable with the similar name. So if you are making the application as reusable components. Probably your components should't be conflicting with the rest of the code. That's why we use this kinda approach

There are few good examples here

The better way

To show how its done, I've created a sample angular application. The modules are categorized based on the features/functionality (customer, administrator) rather than the type (models, views and controllers)

I followed the LIFT principle

L - Locating your code
I - Identifying the code at a glance
F - Flat file structure
T - Try to stay dry (Don't repeat yourself)


Finally, there are no rules

There are no hard and fast rules of coding. The best way is the way you feel more comfortable.

Saturday, April 29, 2017

Weather Monitoring System using java RMI and Sockets

[ Storm | StormServer | StormPI ]

Screen Shots

StormPI(Sends weather data via sockets)
Storm Server(Has a socket interface and a RMI interface)

StormServer(RMI/Sockets server) and StormPI( The sensor module)
Client Module


Introduction

“Storm” is a fully distributed weather monitoring system. It consists of mainly 3 main applications
  1. Strom Server - Server which provides services to monitoring stations and sensors
    1. Socket interface - Provides communication with the sensor devices.
    2. Java RMI interface - Exposes methods which can be used by Monitoring Stations.
  2. StormPI - A sensor emulator which contains
    1. Temperature sensor
    2. Pressure Sensor(Barometer)
    3. Rainfall Sensor(Rain Gauge)
    4. Humidity Sensor
3. StormApp - Client for weather monitoring,. Live weather data can be visualized in given time.
Features
  • StormApp Monitoring Stations receives live weather data and alerts on separated views.
  • StormApp Supports on demand weather.
    • Monitoring Stations receives an available list of sensor locations when connected to the server.
    • User can select a location and subscribe to the weather.

Non Functional Requirements

  1. Storm uses the Distributed Computing principles in the architecture and the design.
    1. Architecture - Client Server
    2. Server Communicates with the Monitoring stations through the RMI interface
    3. Sensors sends weather data to Storm server through sockets.
  2. Adoption of appropriate security/authentication mechanisms
    1. Sensors has to be registered with the server.
    2. Registered Data is stored in  a file which is present inside the server.
    3. Monitoring Stations can log into the server only using a secured key.
  3. Thread Safety
    1. Use of ConcurrentMaps to store data
    2. Use of CopyOnWriteArrayLists
    3. Object null checks before accessing the object.
    4. Synchronized file access methods
  4. Performance
    1. Use of multithreaded programming in order to utilize the maximum performance in sensors client and the server.

High Level Architectural Diagram (Physical Diagram[2]).


Requirements

Storm can be deployed on any hardware device which supports the following software requirements.

  1. Mac OSX
  2. Windows 10
  3. Java 1.8
  4. Ubuntu 16.4

Assumptions

  • StormPI  which used to simulate a real world sensor is nearly identical to a real system.
  • On demand weather data of the StormAPP client(monitor) outputs the latest weather update of that particular location, not the history of weather data.
  • Using a file to store registration won’t affect the efficiency of the server.
  • Garbage collector will automatically destroy all daemon threads, if exist any.

Known Issues.

  • Client throws null pointer exceptions when running on fedora linux and ubuntu 16.4

References

[1] ”Object Exportation”, http://www.fitc.unc.edu.ar/javadev/rmi/sequence_diagrams.html#callExec
[2] “Deployment Diagrams Overview”, “http://www.uml-diagrams.org/deployment-diagrams-overview.html

Tuesday, April 11, 2017

Prototype in detail [ taken from stackoverflow ]

General Idea : ------------------------------------------------------------------------------------------------------------

Instances use __ proto __ to access the prototype of its constructor function.


Prototype is a property of function object.Whenever a function is declared it comes with a prototype property.If we declare a function called Parent_func like the following (it is also called constructor function):
function Parent_func(){

       //..................

 }
Constructor functions ships with a prototype property called Parent_func.prototype.It is an object .So it means we can add new method and properties on it.It has a interface like the following:
Parent_func.prototype = {
       constructor:Parent_func,
       //other properties
}
If we create a new instance of that constructor function, newly created instance will have a method called __ proto __ which will be assigned the prototype of the constructor function.
var parent1 = new Parent_func();
here parent1 is an instance of Parent_func.So parent1 will have a __ proto __ and it will be assigned Parent_func.prototype.
parent1.__ proto __ = Parent_func.prototype
Thus we can say instances use __ proto __ to access the prototype of the constructor function.
Additional Information : ------------------------------------------------------------------------------------------------
(__ proto __) in inheritance (parent-child relationship) :

Say we have another constructor beside parent called Child().
function Child(){
       .................
}
As Child is also a constructor, it too has a prototype property.We want child to inherit from Parent_func.We can do this using Child.prototype 
Child.prototype = new Parent_func();
Now Child inherit from parent .If we make an instance of Child() constructor
var child1 = new Child();
So,
child1.__ proto __ === new Parent() 
child1.__ proto __ is now pointing to an object which happens to be an instance of Parent constructor.So now it will inherit all method and properties of Parent_func including it's prototype.
So 
child1.__ proto __.__ proto __ === Parent_func.prototype
console.log(child1) will result something like this !
Thus prototype chaining works in classical javascript
** _Now i will explain the image which op attached with the question:**
As we already know __ proto __ is used by instances to access the prototype of it's constructor :
enter image description here
1. First we created a constructor function Foo(){}
2. constructor function Foo has a prototype property which points to it's prototype which is Foo.prototype( see image).
3. Constructor functions are function which is instance of [[Function]] object.So we can say function Foo is constructed by [[Function]] object.So, __ proto __ of Foo indicates to Function.prototype.
4. [[Function]] objects inherit from [[Object]].So __ proto __ of Function.prototype points to Object.prototype.
5. Object.prototype is last man standing in prototype chain.So it's __ proto __ points to null.
6. Now come to instances of Foo.When we create an instance using new Foo(), it creates an instance.Foo it the constructor of these instance.Here we created two instances (x and y). __ proto __ of x and y thus points to Foo.prototype.

ref:[http://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript]

Friday, April 7, 2017

Getting started with MongoDB - Basics


Introduction


DML = Data manipulation language
DML keywords = Insert, update, delete, select

No hard and fast Rules
  • MongoDB is a No SQL database. 
  • It's written using C++.
  • It stores JSON data in the BSON format. 
  • Once we query the data, mongoldb will automatically convert the bson -> json when reading and json -> bson when storing. This process is handled by the mongodb driver.
  • MongoDB stores data in BSON but why?
    • Traversable
    • Efficient 
    • Lightweight
  • Advantages 
    • Performance - performance effecting queries such as joins are not needed. 
      • Example :- In relational databases, we use triggers(insert triggers(inserted table), update triggers) which makes a single CRUD operation run twice or more depending on the trigger. This will drastically reduce the database performance when the database gets bigger. suppose that your relational database contains about 5GB of data. Then it will take a long time to process a simple insert query with a trigger. That functionality is not needed in mongo db.
    • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
    • A clear path to horizontal scalability. 
    • No schema migrations. Since MongoDB is schema-free, your code defines your schema.
  • Disadvantages
    • MongoDB is not ACID compliant.( MongoDB is eventually persistence)
      • A - Atomicity
      • C - Consistency
      • I - Integrity - Since mongoDB doesn't assure data integrity,  it's not a good practice to use mongo db for transactions.
      • D - Durability  
A document-based data model. The basic unit of storage is analogous to JSON, Python dictionaries, Ruby hashes, etc. This is a rich data structure capable of holding arrays and other documents. This means you can often represent in a single entity a construct that would require several tables to properly represent in a relational db. This is especially useful if your data is immutable. [Quoted from stack overflow]


Terms

  • Collection - Similar to a table in RDBMS
  • Document - Similar to a tuple in RDBMS
  • Field - Similar to a Column in RDBMS


Setup

  • Install MongoDB - Complete guide is available on mongodb.com
  • Create a data folder in
    • Windows - C:\data\db
    • Linux - /data/db
  • Switch to the terminal and type the following commands
    • sudo mongod
  • open a new tab in the terminal and type
    • mongo #This will start the mongo console. You can execute any command here.

Basic Commands and Queries


Show the available databases 

show dbs

Show the current db

db

Switch to a particular database

use database_name #This will switch to the database only if it's available. Otherwise, mongodb will create a new database.

Drop the database

Switch to the database you want to drop and type the following command
db.dropDatabase();

Let's look into CRUD Operations


Inorder to test the crud operations, We need to have some data in the database. We'll use a json generator for this purpose. Go to http://www.json-generator.com/ and click on Generate button to generate a  random json data.Same as the databases, the collections will be automatically created upon the data insertion.

Insert a single document

db.collection_name_here.insert({});

Example : - 

db.collection_name_here.insert({
"name" : "your name",
"age" : 22
})

Insert multiple documents at the same time

db.collection_name_here.insert([{},{},{},{}])

Example : -

db.collection_name_here.insert([{"name" : "your name", "age" : 22},{"name" : "your name", "age" : 22},{"name" : "your name", "age" : 22},{"name" : "your name", "age" : 22}])

Note the syntax difference between inserting a single document and multiple documents

Finding all Document/Documents


db.collection_name_here.find().pretty()

pretty() command will format the json.

Finding a single Document

db.collection_name_here.findOne()

Delete

db.collection_name_here.remove(Deletion Criteria)

Example : -

db.collection_name_here.remove({"_id":"23343453"})
db.collection_name_here.remove({"gender":"make"})

Update

db.collection_name_here.update({unique document identifier},{updating json object})

Example : -

db.collection_name_here.update({"_id":"3435435e454355"}, {
"name" : "my name",
"age" : 23
})

More on Finding


Let's find a single document by id


db.collection_name_here.find({"_id":"39043890353434343"})


Find based on multiple criteria


db.collection_name_here.find({"age": 20 },{"gender":"male"},{"marks":"27"})

Using $or, $and


db.collection_name_here.find({$or: {"gender":"male"},{"age",24}})

db.collection_name_here.find({"University":"SLIIT"},{$or: {"gender":"male"},{"age", 20}})

db.collection_name_here.find({$and: {"gender":"male"},{"age",24}})

db.collection_name_here.find({"University":"SLIIT"},{$and: {"gender":"male"},{"age","20"}})

Using $ne, $gt , $gte , $lt , $lte


db.collection_name_here.find({$or: {"age": {$gte: 23}},{"isEnrolled": {$ne : true}}})

db.collection_name_here.find({"University":"SLIIT"},{$and: {"gender":"male"},{"age",{$gt : 20 }}})

Displaying only the required fields


Syntax :


db.collection_name_here.find({"Your query"},{"Specify the fields you want to display"})

Example : 


{"field_name": 1} <- Displays particular filed
{"filed_name" : 0} <- Doesn't display the specified field

db.collection_name_here.find({"University":"SLIIT"},{$and: {"gender":"male"},{"age",{$gt : 20 }}}, {"name":1,"_id": 0})
#the above code will only display the name fields


Indexing


A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.
An index is a copy of selected columns of data from a table that can be searched very efficiently that also includes a low-level disk block address or direct link to the complete row of data it was copied from.[wiki : search : Indexing databases]


  • Pros
    • Indexing speeds up the efficiency and performance of finding (Select / Find statements).
  • Cons
    • Indexing decreases the performance of inserts, updates and deletes.
      • What happens - When you index, an index table will be created on the index field and the primary key field. This index table is saved in the hard disk which takes up space. But when you are using a index field, the index table will be taken to the RAM.
      • Why performance ? When ever you are writing data(update, delete and inserting), the database has to automatically update the index table. So one insert operation does 2 inserts. So the performance decreases.

Adding an index


db.collection_name_here.ensureIndex({"age": 1}) 
db.collection_name_here.ensureIndex({"name": 1})


Getting all the indexed fields


db.collection_name_here.getIndexes()

Removing an Index


db.collection_name_here.dropIndex({"age": 1 })


Aggregation and Grouping


In the relational databases we use count, max, min and average keyword. But how do we do it using mongoldb  ?

db.collection_name_here.aggregate(
$group : {
"_id" : "$age",
"total" : {$sum : 1}
}
)

db.collection_name_here.aggregate(
$group : {
"_id" : "$marks",
"Average" : {$avg : 1}
}
)


db.collection_name_here.aggregate(
$group : {
"_id" : "$marks",
"Average" : {$max : 1}
}
)


Where to go from here

  • There are lot of  courses available at mongoDB university. you can follow one of these and get certified from the mongodb itself.
  • If you own a website which has sql, best way to learn mongo is to convert that website into mongo.