Google Apps Script, How to import gs functions across other Google docs

Go to .gs file/project(Tools>Script editor..) which you want to use as a library.

Save file with the version at File>>Manager versions.

Copy project key from this menu location File>>Project properties>>Project key

Go to your second .gs file/project and add previous project key as a library at menul Resources>>Libraries…

“Add a library” -> click on Add button -> click on Save

Add identifier for this library once library is added with Resources>>Libraries… menu.

Now you can use functions from previous project/library with this identifier inside your local function.

Identifier.functionFromImportedLibrary();

REST Mock API Server Example

Integration projects require APIs contract and implementation to carry out integration work. Once contract is defined you need API end point from other project/service to finish your work.
If you dont get it on time, you are kind of stuck, what can you do? Create your own API end point and finish your side of implementation by changing your url end point to something local. That local service should be your mock service which gives you desire results same as like actual APIs.

MockServer is one of such library which can allow you to mock your thirdparty or dependent APIs.

Take a look of below easy example.

import org.mockserver.client.server.*;
import org.mockserver.model.*;
import static org.mockserver.integration.ClientAndServer.*;
import static org.mockserver.model.HttpRequest.*;
import static org.mockserver.model.HttpResponse.*;
import static java.util.concurrent.TimeUnit.*;

public class DummyPOSTService {
	public static void main(String[] args) {
		MockServerClient mockServer = startClientAndServer(1100);

		mockServer
	        .when(
	        		request()
                    .withMethod("POST")
                    .withPath("/test")
                    .withHeader("authorization", "dummy")
	        )
	        .respond(
	        		response()
	        		.withStatusCode(200)
                    .withHeaders(
                            new Header("Content-Type", "application/json; charset=utf-8"),
                            new Header("Cache-Control", "public, max-age=86400")
                    )
                    .withBody("{ 'test': 'abc' }")
                    .withDelay(new Delay(SECONDS, 1))
	        );
	}
}

You need this jar in order to run above class.

<a href="http://search.maven.org/remotecontent?filepath=org/mock-server/mockserver-netty/3.10.4/mockserver-netty-3.10.4-jar-with-dependencies.jar">mockserver-netty-3.10.4-jar-with-dependencies</a>

mock-api-server

Angularjs code to display dynamic fields binding in html

  	<div ng-repeat="field in questionElements">    
  	  	<ng-form name="form">
  			<label>{{field.question}}</label>
    			<!-- TEXT FIELDS -->
    			<div ng-if="field.type=='textfield'" class="form-group" >
        			<input type="{{field.type}}" dynamic-name="field.id" id="{{field.id}}" data-ng-model="field.answer" class="form-control"/>
      			</div>
      			
      			<!-- SELECT FIELDS -->
			    <div ng-if="field.type=='dropdown'" class="form-group" >
			        <select data-ng-model="field.answer" dynamic-name="field.id" class="form-control" />
			        	<option ng-repeat="option in field.options" value="{{option.key}}">{{option.value}}</option>
			        </select>
			    </div>
      			
      			<!-- RADIO FIELDS -->
			    <div ng-if="field.type=='radio'" class="form-group">
			    	<div class="form-control" ng-repeat="option in field.options" >
			            <input type="radio" data-ng-model="field.answer" id="{{option.key}}" name="{{option.key}}" value="{{option.key}}" >{{option.value}}<br/>
			      	</div>
			    </div>
			    
			    <!-- CHECKBOX FIELDS -->
			    <div ng-if="field.type=='checkbox'" class="form-group">
			    	<div class="form-control" ng-repeat="option in field.options" >
			            <input type="checkbox" data-ng-model="option.answer" id="{{option.key}}" name="{{option.key}}" value="{{option.value}}" >{{option.value}}<br/>
			      	</div>
			    </div>
      			
      		</ng-form>
      	</div>

A Raspberry PI – DC motor, stepper motor, ultra sonic sensor project

I was in Cloudera Hadoop Developer training recently and there was interesting discussion on how low cost computer like raspberry PI can be used to setup Hadoop cluster for developer testing and proof of concepts. I started digging into that but in parallel i’ve decided to use such low configured CPU to do some robotics projects.

Here is the first project i recently finished with Raspberry PI.
A self-trajectory motor vehicle. It has its own brain(not remote controlled) coded in python language to make all decisions.
It runs on two external power sources, 12V and 5V. 12V to rotate main DC motors while 5V for CPU processing, stepper motor movements and for obstacle sensing(ultrasonic sensor). PI is operating those motors through two L298N circuit board.

Scroll for code below.

Raspberry_PI_motor1

Raspberry_PI_motor2

Raspberry_PI_motor3

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

#GPIO.cleanup()

wheel1A=27
wheel1B=10
wheel2A=17
wheel2B=22

eye1A=7
eye1B=8

eyeWA=6
eyeWB=13
eyeWC=19
eyeWD=26

GPIO.setup(wheel1A, GPIO.OUT)
GPIO.setup(wheel1B, GPIO.OUT)
GPIO.setup(wheel2A, GPIO.OUT)
GPIO.setup(wheel2B, GPIO.OUT)
GPIO.setup(eye1A, GPIO.IN)
GPIO.setup(eye1B, GPIO.OUT)

#eyes
GPIO.setup(eyeWA,GPIO.OUT)
GPIO.setup(eyeWB,GPIO.OUT)
GPIO.setup(eyeWC,GPIO.OUT)
GPIO.setup(eyeWD,GPIO.OUT)

def spinRight():
        print('SR')
        GPIO.output(wheel1A,1)
        GPIO.output(wheel1B,1)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,0)
def spinLeft():
        print('SL')
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,1)
        GPIO.output(wheel2B,1)
def goForward():
        GPIO.output(wheel1A,1)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,1)
def goBackward():
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,1)
        GPIO.output(wheel2A,1)
        GPIO.output(wheel2B,0)
def stop():
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,0)

##stepper_motor_start
delay = 0.015
cSteps=0
def setStep(w1, w2, w3, w4):
        GPIO.output(eyeWA, w1)
        GPIO.output(eyeWB, w2)
        GPIO.output(eyeWC, w3)
        GPIO.output(eyeWD, w4)
def rotateLeft(steps,check):
        global cSteps
        for i in range(0, steps):
                print('L'+str(cSteps))
                setStep(1,0,1,0)
                time.sleep(delay)
                setStep(0,1,1,0)
                time.sleep(delay)
                setStep(0,1,0,1)
                time.sleep(delay)
                setStep(1,0,0,1)
                time.sleep(delay)
                cSteps=cSteps-1
                if(check==True and checkDistance()!=-99):
                        return 1
        return 0

def rotateRight(steps,check):
        global cSteps
        for i in range(0, steps):
                print('R'+str(cSteps))
                setStep(1,0,0,1)
                time.sleep(delay)
                setStep(0,1,0,1)
                time.sleep(delay)
                setStep(0,1,1,0)
                time.sleep(delay)
                setStep(1,0,1,0)
                time.sleep(delay)
                cSteps=cSteps+1
                if(check==True and checkDistance()!=-99):
                        return 1
        return 0
##stepper_motor_end

##eye_start
def checkDistance():
        GPIO.output(eye1B,1)
        time.sleep(0.00001)
        GPIO.output(eye1B,0)
        while GPIO.input(eye1A)==0:
                pass
        start = time.time()
        while GPIO.input(eye1A) == 1:
                pass

        stop=time.time()
        distanceCM = (stop - start) * 170 * 100
        #print distanceCM
        #if distanceCM < 50 and (distanceCM <5 or distanceCM > 8):
        if distanceCM < 50:
                return -99

arrow = 'F'
findPath ='F'
skip='F'
maxRotate=6 #must be even number
while(1==1):
        #print(arrow)
        if(arrow=='SL'):
                spinLeft()
        if(arrow=='SR'):
                spinRight()
        if(arrow=='B'):
                goBackward()
        if(arrow=='F'):
                goForward()
        if(arrow=='X'):
                stop()
                if(findPath=='T'):
                        skip='T'
                        cSteps=0
                        found=rotateLeft(maxRotate, True)
                        if(found==0):
                                print('No left')
                                found=rotateRight((maxRotate*2) + 1, True)
                                if(found==1):
                                        rotateLeft(cSteps, False) #return eye to the front
                                        spinRight()


                        else:
                                print('Found something in left')
                                if(cSteps<0):
                                        cSteps = cSteps * -1
                                rotateRight(cSteps, False) #return eye to the front
                                spinLeft()
                        print('done........')
                        arrow='F'
                        findPath=''
                        setStep(0,0,0,0)
                        time.sleep(0.1)
                time.sleep(0.5)
        else:
                time.sleep(0.125)
        #arrow=raw_input("")

        if skip=='F' and checkDistance()==-99:
                arrow='X'
                findPath='T'
        else:
                skip='F'
        #if distanceCM > 200:
        #       arrow='F'

Configuring tomcat jdbc connection pool for high concurrency

There will be case when you will require to tune your existing connection pool configuration to sustain more user load with your old product or you want to identify very first time that how many concurrent user load your new product can handle.

Being architect or application designer, high probability will be that you will target tuning of connection pool configurations first at your end.

Below are some crucial configurations with tomcat jdbc and apache dbcp one can use to get high concurrency with less to no database connection issues.

//I used this in my application to hit database server with 20 running threads to update 14 tables(average 10 fields per table).
//It was 238 insertion of 0.5MB of data(encrypted XML string) in a second.
//Encryption time is also considered in this otherwise database operation number can be higher than this.

poolProperties.setRemoveAbandonedTimeout(30); 
poolProperties.setMinEvictableIdleTimeMillis(5000); 
poolProperties.setTimeBetweenEvictionRunsMillis(1500); 

RemoveAbandonedTimeout – This is a timeout value. This should be the longest running query of your application however if your single connection object is used to fire multiple queries, value of timeout should be sum all those query execution. Keep this wide open to avoid ‘connection is already closed’ issue.
Above will not apply if you use ResetAbandonedTimer JDBC interceptor. In case of this, use timeout number as longest running single query.

poolProperties.setJdbcInterceptors(
	      "org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer");

MinEvictableIdleTimeMillis – Minimum time a connection can stay idle before it gets evacuated by evacuation thread to free up the resources.

TimeBetweenEvictionRunsMillis – Evacuation thread will kick in every x milliseconds to evacuate idle or abandoned connection objects.

Please note: Performance is subject of tuning of multiple elements. It also depends on hardware where database files will get stored, network adapters and database server configuration itself. To get maximum throughput of any application it is necessary that all these elements are tuned/configured properly.

Synchronizing java threads on a string value

We had a situation where we wanted to synchronize all threads to get a new value from database. This was purely a database design issue but a month effort to fix whole thing so we decided to write a code which can wait for database operation to get completed. This required a synchronization mechanism on value instead of an object. Below is the code.

I wrote a mechanism to clean up synchSemaphores object but it is advisable to use LRUMap or similar API which does it automatically.

One can also remove lockAcquiredCounter object, this was only used for cleaning up process with my custom code.

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;

public final class SemaphoreLocker {
	
	private ConcurrentMap<String, Semaphore> synchSemaphores = new ConcurrentHashMap<String, Semaphore>();
	private long timeToClear = 5;
    private static SemaphoreLocker locker = new SemaphoreLocker();
    
    public static SemaphoreLocker getInstance(){
    	return locker;
    }
    
    /**
     * Constructor will initialize the thread which will do the cleanup when nobody is making any request
     */
    private SemaphoreLocker(){
        // Write a code which can clean	synchSemaphores objects periodically. 
    }
    
    /*
     * Indicator if any thread under lock mode
     */
    volatile int lockAcquiredCounter = 0;
    
    public void acquireLock(String key){
    	synchronized (locker) {
    		lockAcquiredCounter++;
		}
        Semaphore tempS = new Semaphore(1, true);
        Semaphore s = synchSemaphores.putIfAbsent(key, tempS);
        if(s == null){
            s = tempS;
        }
        s.acquireUninterruptibly();
    }

    public void releaseLock(String key){
        Semaphore s = synchSemaphores.get(key);
        if(s != null){
            s.release();
        }
        synchronized (locker) {
    		lockAcquiredCounter--;
		}
    }
}

Value of PI

Programming is always a fun.

Ever thought of what can be the possible value of PI? Well there is no fix value because it is irrational. However i’ve tried to get max possible number. My eclipse gone mad(use console 🙂 ) and at one point of time I was not able to see the result. What i could capture is in below screen.

The sequence does not repeat itself atleast for number I captured.PI_ValueCode

java.util.regex.PatternSyntaxException: Dangling meta character

When you try to split any string with ? or * as below code.

String sqlParts[] = sql.split("?");

You will end up with unchecked PatternSyntaxException as given below.

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0

Solution:
Avoid using dangling metacharacters like ‘?’, ‘+’ and ‘*’. Instead use it with escape sequences as like below.

String sqlParts[] = sql.split("\\?");

Double Checked Locking With Thread

While implementing a circular non-blocking queue, we observed a pattern which could boost a performance.

When you have condition based thread safe block, it is always advisable to check same condition before and after lock.

Before lock will avoid threads to get locked unnecessarily and after lock will ensure that any state change within synchronized block does not violate protocol.

In detail, for example, there are ‘n’ threads entering into this piece of code and say queue(ConcurrentLinkedQueue) is full, If queue is not full we should not lock threads, hence check at Line 1, All thread will wait at line 2. First thread (whichever will get CPU based on OS priority/roundrobin mechanism for processes) will enter and modify queue at line 4. Because of this, queue may not satisfy condition for other waiting threads, so thread entering second should not run code if condition is not satisfied, hence check at line 3. We could omit line 1 actually but adding it will have performance boost especially when condition is important to pass before entering to synchronized block.

   if (this. linkedThreadSafeQueue.size() == this.maxLimit) {
      synchronized(this.linkedThreadSafeQueue) {
          if (this. linkedThreadSafeQueue.size() == this. maxLimit) {
                        //Modify queue
          }
      }
   }

This helps when developer is dealing with singleton (condition based) and multithread implementation where he does not want to initialize same object again and also not to lock thread unnecessarily.