Tuesday, June 27, 2017

Serverless JoymonOnline.in - Monitoring up and downtime of web sites

Side effects of Serverless

One of the side effect of Serverless is that is has so many moving parts and we need to monitor the health of all the services consumed by our application. In the case of JoymonOnline.in, there is no money involved but may be slight reputation loss when people sees the site down. How can this guy solve our problems in distributed systems if he can't make his site up all the time?

But that is not the case with real time web apps which brings revenue to the organization. Such scenarios demand the need for monitoring the health of third party services. Ideally we should design the system to switch to another alternative if possible. But at minimum we should know what is going wrong in the system.

JoymonOnline.in uses GitHub pages to host static content as it is HTML app developed using AngularJS framework. It is good to monitor the health of site as is.

Choosing monitoring service - Freemium

Freemium is a relatively new term in SaaS. When vendors sell Software as a Service, they often include a free tier to do all the basic things. For higher usage they start billing. There are many freemium services for web site/service monitoring too. One of such service is UptimeRobot. It checks the website in 5 min interval and informs us about downtime. It is very easy to setup. Only downside is it accepts only custom credentials. Not integrated with Google or Facebook logins.


There are many other services like this. Of course this is best otherwise I would not have used it :) . A list is given below.

https://github.com/lexoyo/Monitoshi - Includes the source code and a free hosted instance.
https://visualping.io/pricing - Only 2 checks /day
http://mashable.com/2010/04/09/free-uptime-monitoring/#FBROURLfQkqR
http://www.hongkiat.com/blog/monitor-website-up-downtime-30-free-web-services-and-tools/

Tuesday, June 20, 2017

C program to encode and decode string

Recently I get introduced to too many technology WhatsApp groups which talks about software development, basic electronics, robotics, AI and many more. It really helps me to know what is going in other areas instead of browsing for news. Also it is happy to be in places where people help each other.

Below is one question someone threw to a groups and I could do something after a week.

Write C program for self defined codec

1.Get a string of chars from user.
2. Convert string to array of chars (each char has value 0~255)
3.Define a lookup table (LUT) and an algorithm to encode the char values.
4.Use the LUT and algoithm from step 3 to encode the array of chars.
5.Output the encoded array of chars.
6.Decode the encoded array of chars.
7.Output the decoded array of integers to prove that your algorithm can decode.

I seriously programmed in C when I was in college and during my job seeking. After I landed in .Net world, there were less than 5 scenarios where I had to look or code in C/C++. Yes the big trouble was with pointers. Also comparing C#, I felt driving 80's model Ambassador car when programmed in C. Enough talks. Below is the code

#include < stdio.h >
char LUT[26] = "QRSTUVWXYZABCDEFGHIJKLMNOP";

int getLocationFromLUT(char c) {
  int i = 0;
  while (i < 26) {
    if (LUT[i] == c) return i;
    else i++;
  }
  return -1;
}

char * encode(char * str) {
  int i = 0;
  while (str[i] != '\0') {
    int locationInAlphabet = str[i] - 65;
    str[i++] = LUT[locationInAlphabet];
  }
  return (str);
}
char * decode(char * str) {
  int i = 0;
  while (str[i] != '\0') {
    int location = getLocationFromLUT(str[i]);
    str[i++] = location + 65;
  }
  return (str);
}
int main() {
  printf("Enter string to encode (caps & max len 64)\n");
  char word[64];
  scanf("%s", word);

  char * encoded = encode( & word[0]);
  printf("Encoded string %s\n", encoded);

  char * decoded = decode(encoded);
  printf("Decoded string %s\n", decoded);
  return 0;
}

So simple isn't it. Took me around 2 hrs to get the code completed.

It can be run in browser using tutorialspoint online environment. Technically it not in browser, browser just acts as console. I used this environment to write this code.
http://www.tutorialspoint.com/compile_c_online.php?PID=0Bw_CjBb95KQMU0tIWi1qcEJUUUk

Tuesday, June 13, 2017

Interception in Microsoft Unity framework for .Net

Introduction

This article is to demonstrate how we can combine dependency injection framework with logging og low level execution flow of application.

Prerequisites


  • Knowledge about dependency injection.
  • Knowledge about unity from Microsoft for dependency injection

Background

If we develop any application, logging is an essential thing to troubleshoot issues which comes in  later stages. Ideally there should be no bugs in production, if we test well. But it is not practical 99.99% of times. Sometimes even if there are no defects we may need to understand how the system is getting used. We call it as instrumentation, telemetry etc...Whatever we call, we have to make sure our application is capable of telling us what it did. Writing log lines inside our business logic has more efforts/costly, makes it longer and very difficult to read. 

Instrumentation technology considerations

Below are the factors to be considered when we select any logging and instrumentation technology for software.
  • Performance - Due to the logging operations our application should not be performing slow. Logging is for application not the other way.
  • Controlling behavior - We should be able to turn the logging on and off without restarting the application. Also change the level of logging without restarting.
  • Compatibility - If our system spans across different technologies better choose one which works everywhere.

Options

Traditional options include inserting log lines at the beginning and end of every methods and additional lines if the logic inside complicated. Another option is to inject the logging code at runtime. Both has their own pros and cons.

Few words on ideal composed application

If we look at SOLID principles we could see that all it tells us is to develop apps in a modular way where modules can be composed easily. In other words single responsible, closed components connected together to become an application. When we say modules, components in general they all translate to classes or functions in code.

How we compose the application by connecting the components is via dependency injection framework.

Combining DI framework and logging

Now most of the people might have got the click of idea. If the application follows SOLID principles religiously, why can't our DI framework log the method calls as it knows how application is composed. When we say how the application is composed it is same as the application flow which we need to log.

This is supported in Microsoft unity framework which provides DI capabilities for .Net. There are other DI frameworks available, one other called MEF which is native to .Net itself. This post is not to discuss what is the best DI framework but on the possibilities of combining DI with function level logging.

Steps in general

  • Have a interceptor mechanism which can tell us what is happening in the DI framework
  • Let the interceptor log the details into appropriate medium using best technology.
  • Register the interceptor into the DI framework.

Understanding sample

Coming directly to the sample which is available in the below location.
https://github.com/joymon/dotnet-demos/tree/master/patterns-practices/DI/unity/Interception

class LogCallHandler : ICallHandler

This is the interceptor we have written. It has Invoke() implemented which came via ICallHandler. Invoke() will be called by the unity DI framework when things happen there.

This is one mechanism unity supports. There are other ways to hook interceptors.

UnityConfiguration.xml

This is the configuration file which tells the unity framework about the components and how they composed. In simple words when someone request object of x interface give object of y class which implemented x. This file also has options to register interceptor.

This configuration can be specified via code too.

class Logger

This is a simple logger class which logs via .Net trace. .Net tracing is configured in the sample to write to file.Refer app.config file. We have to consider intercepting the method calls and writing the logs separate. If our log writing takes more time the performance of application may get affected. Choosing log writing mechanism is not in the scope of this article. In short, recommended mechanisms are ETW for on premise and Application insights for Azure environments.

References

https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx
http://mohtasebi.com/patterns/2013/10/24/using-unity-for-aspect-oriented-programming-and-interception-part-1.html

Tuesday, June 6, 2017

Puzzle - Sales tax

Below is famous problem which could be asked in coding interviews. I got this problem when I attended ThoughtWorks long back and could pass to the next round of pair programming.  The solution what I sent to them is uploaded to Github and URL is given at the end of this post. The mail came up when I was cleaning my mail box.

PROBLEM SALES TAXES 

Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.

When I purchase items I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping baskets...
INPUT:

Input 1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85

Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50

Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99

1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25

OUTPUT

Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83

Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15

Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68

Solution

The major pattern here applicable is decorator to structure the calculation rules. Full code is present in GitHub repo.