Thursday, 16 January 2020

Winston logger with file name and time stamp nodejs

logger.js

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} [${label}] ${level}: ${message}`;
});
const getLabel = function(callingModule) {
    const parts = callingModule.filename.split('/');
    return path.join(parts[parts.length - 2], parts.pop());
  };

const logger = (callingModule)=> createLogger({
  format: combine(
    label({ label: getLabel(callingModule) }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

module.exports = {
    logger:logger
}

use as below:

const logger = require('../util/logger').logger(module);
logger.info('logger file..');

//util/logger is my file path of logger.js

output:

2020-01-16T08:28:41.893Z [controller/shop.js] info: logger file..

Monday, 25 November 2019

Generic function for Post Data with some headers in spring

 public <T,K> K postApiCall(T t, HttpHeaders headers, String url, Class<K> resposne){
        try {
            RestTemplate restTemplate = new RestTemplate();
            headers.add("Content-Type","application/json");
            HttpEntity<T> entity = new HttpEntity<T>(t, headers);
            ResponseEntity<K> result = restTemplate.exchange(url, HttpMethod.POST, entity, resposne);
            System.out.println(result.getBody());
            return result.getBody();
        }catch (Exception ee){
            ee.printStackTrace();
        }
        return null;
    }

Thursday, 15 August 2019

Check linkedList palindrom


 Steps:
1. break list in 2 parts
2. reverse second list
3. compare list

void checkLinkedListpalindrom(Node node) {
        Node p = node;
        Node q = node;
        Node start = node;
        Node secondstart=null;
        while(true) {
            p = p.next.next;
            if(p==null) {
                secondstart = q.next;
                q.next = null;
                break;
            }
            if(p.next==null) {
                secondstart = q.next.next;
                q.next = null;
                break;
            }
           
            q = q.next;
       
        }
        secondstart =  reverse(secondstart);
        System.out.println(compareList(start, secondstart));
       
    }
   
    boolean compareList(Node n1, Node n2) {
        boolean flag = true;
        while(null!=n1 || null!=n2) {
            if(n1.value==n2.value) {
                n1 = n1.next;
                n2 = n2.next;
            }else {
                flag = false;
                break;
            }
        }
        return flag;
    }
   
    Node reverse(Node node) {
        Node pre = null;
        Node current =node;
        while(null!=current) {
            Node temp = current.next;
            current.next = pre;
            pre = current;
            current = temp;
           
        }
        return pre;
    }


Thursday, 20 June 2019

Send Multipart file in resttemplate Java Spring

import java.io.File;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String[] args) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource value = new FileSystemResource(new File("D://test.png"));
map.add("file", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost/api/v1/users/avatar", HttpMethod.POST, requestEntity, String.class);
}
}

ref: https://gist.github.com/ansidev/5816b8b3108c30b5279f5fec67506798

Sunday, 17 March 2019

Convert array into Zig-Zag fashion

public class ZigZag {
    public static void main(String[] args) {
          int arr[] = new int[]{4, 3, 7,7, 8, 6, 2, 1};
          zigzag(arr);
    }
   
    public static void zigzag(int[] arr) {
       
        boolean flag = true ;//<
       
        for(int i=0;i<arr.length-1;i++) {
            if(flag) {
                if(arr[i]>arr[i+1]) {
                    int temp = arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1] = temp;
                }
            }else {
                if(arr[i]< arr[i+1]) {
                    int temp = arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1] = temp;
                }
            }
            flag = !flag;
        }
       
        System.out.println(Arrays.toString(arr));
    }
}
output: [3, 7, 4, 8, 6, 7, 1, 2]

Sunday, 10 February 2019

Consistent Hashing

https://www.acodersjourney.com/system-design-interview-consistent-hashing/

Diameter Of a binary Tree

public class DiameterOfTree {
   
    public static void main(String[] args) {
        Node node  = new Node(1);
        node.left = new Node(2);
        node.right = new Node(3);
        node.left.left = new Node(4);
        node.left.left.left = new Node(5);
        node.left.left.left.left = new Node(6);
        node.left.left.left.right  = new Node(7);
        node.left.left.right = new Node(8);
       
        //System.out.println(findHeight(node));
         System.out.println(diameter(node));
    }
   
    static void printInOrder(Node node) {
        if(null!=node) {
            printInOrder(node.left);
            System.out.println(node.value);
            printInOrder(node.right);
        }
    }
   
    static int diameter(Node node) {
        if(null==node) {
            return 0;
        }
        int lengthLeft,lengthRight;
       
        lengthLeft = findHeight(node.left);
        lengthRight = findHeight(node.right);
       
        int leftDiameter = diameter(node.left);
        int rightDiameetr = diameter(node.right);
       
        return Math.max(1+lengthLeft+lengthRight, Math.max(leftDiameter, rightDiameetr));
       
    }
   
    static int findHeight(Node node) {
        if(null==node) {
            return 0;
        }
        if(null==node.left && null==node.right) {
            return 1;
        }
        int left ,right ;
        if(null!=node.left) {
            left = findHeight(node.left);
        }else {
            left  = Integer.MIN_VALUE;
        }
       
        if(null!=node.right) {
            right = findHeight(node.right);
        }else {
            right = Integer.MIN_VALUE;
        }

        return 1+ Math.max(left, right) ;
    }
   
    static class Node{
        Integer value;
        Node left;
        Node right;
        Node(Integer value){
            this.value = value;
            left = right = null;
        }
    }
}

output: 6

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...