Friday, 7 August 2020

add a graphana visualization

1) Create a visualization


2) Choose visualization type


3) Change Query destination to graphite


4) add a Query

consolidateBy(stats_counts.kafkaConsumerRecordCount.application.my-microservice-name.environment.prod.instanceId.*.region.*.statistic.count, 'sum')


The panel will look like:

{
  "cacheTimeout": "",
  "datasource": "Graphite",
  "gridPos": {
    "h": 9,
    "w": 12,
    "x": 0,
    "y": 0
  },
  "id": 2,
  "links": [],
  "pluginVersion": "6.1.6",
  "targets": [
    {
      "refId": "A",
      "target": "consolidateBy(stats_counts.kafkaConsumerRecordCount.application.my-microservice.environment.prod.instanceId.*.region.*.statistic.count, 'sum')",
      "textEditor": true
    }
  ],
  "timeFrom": null,
  "timeShift": null,
  "title": "Processed Events",
  "type": "graph",
  "renderer": "flot",
  "yaxes": [
    {
      "label": null,
      "show": true,
      "logBase": 1,
      "min": null,
      "max": null,
      "format": "short"
    },
    {
      "label": null,
      "show": true,
      "logBase": 1,
      "min": null,
      "max": null,
      "format": "short"
    }
  ],
  "xaxis": {
    "show": true,
    "mode": "time",
    "name": null,
    "values": [],
    "buckets": null
  },
  "yaxis": {
    "align": false,
    "alignLevel": null
  },
  "lines": true,
  "fill": 1,
  "linewidth": 1,
  "dashes": false,
  "dashLength": 10,
  "spaceLength": 10,
  "points": false,
  "pointradius": 2,
  "bars": false,
  "stack": false,
  "percentage": false,
  "legend": {
    "show": true,
    "values": false,
    "min": false,
    "max": false,
    "current": false,
    "total": false,
    "avg": false
  },
  "nullPointMode": "null",
  "steppedLine": false,
  "tooltip": {
    "value_type": "individual",
    "shared": true,
    "sort": 0
  },
  "aliasColors": {},
  "seriesOverrides": [],
  "thresholds": [],
  "timeRegions": []
}


5) The graph visualization looks like


Saturday, 30 November 2019

WiFi 101


wifi 101

  • The WiFi radio waves are very similar to the radios used for cell phones and other devices.
    They can convert 1s and 0s into radio waves and convert the radio waves back into 1s and 0s.
  • Wifi can transmit from 2.4 GHz or 5 GHz
  • 5G wifi (802.11ac) is the newest standard as of early 2013.
wifi radio waves RSSI
/System/Library/PrivateFrameworks/Apple*.framework/Versions/Current/Resources/airport -I
     agrCtlRSSI: -44
     agrExtRSSI: 0
    agrCtlNoise: -92
    agrExtNoise: 0
          state: running
        op mode: station 
     lastTxRate: 1170
        maxRate: 1300
lastAssocStatus: 0
    802.11 auth: open
      link auth: wpa2-psk
          BSSID: _:_:_:_:_:_
           SSID: progessive_energy_5g
            MCS: 9
        channel: 153,80
Closer I move to router better signal strength I receive.
jshell> var signalToNoiseRatio = -44 - (-92)
signalToNoiseRatio ==> 48
Higher SNR margin values mean clearer signals.

RSSI table

RSSIdesc
-30 dBmMaximum signal strength (NEAR)
-50 dBmexcellent signal strength
-60 dBmreliable signal strength
-67 dBmreliable signal strength
-70 dBmNot a strong signal. Light browsing and email.
-80 dBmUnreliable signal strength, will not suffice for most services. Connecting to the network.
-90 dBmThe chances of even connecting are very low at this level.


Saturday, 17 August 2019

hashmap datastructure with php REPL





php > $user_cache=array("uid1"  => "1", "uid2" => "2");

php > echo $user_cache["uid1"];
1

php > if(!$user_cache["uid3"]) { echo "cache miss";}
PHP Notice:  Undefined index: uid3 in php shell code on line 1

Notice: Undefined index: uid3 in php shell code on line 1
cache miss


Thursday, 25 July 2019

java async task



.map

jshell> var t1 = CompletableFuture.completedFuture(100)
t1 ==> java.util.concurrent.CompletableFuture@25f38edc[Completed normally]

jshell> var t2 = t1.thenApply(x -> x * 2)
t2 ==> java.util.concurrent.CompletableFuture@1a86f2f1[Completed normally]

jshell> t2.get()
$14 ==> 200

.fmap

jshell> var t1 = CompletableFuture.completedFuture(100)
t1 ==> java.util.concurrent.CompletableFuture@506c589e[Completed normally]

jshell> var t2 = CompletableFuture.completedFuture(200)
t2 ==> java.util.concurrent.CompletableFuture@69d0a921[Completed normally]

jshell> var result = t1.thenCompose($ -> t2)
result ==> java.util.concurrent.CompletableFuture@7aec35a[Completed normally]

jshell> result.get()
$19 ==> 200

javascript equivalent

> var t1 = Promise.resolve(100)
undefined
> var t2 = t1.then(x => x * 2)
undefined
> t2
Promise {
  200,
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }


Monday, 31 December 2018

Extracting URL components in Java

URL, Uniform Resource Locator is composed of
1) protocol
2) domain
3) path
4) query string
4) reference/ anchor

example:

[https]://[www.introverse.com]/[abc/def]?[id=1&id=2]#[index1]


scala> import java.net.URL
scala> val url = new URL("https://www.introverse.com/abc/def?id=1&id=2#index1")
url: java.net.URL = https://www.introverse.com/abc/def?id=1&id=2#index1

scala> url.getProtocol
res1: String = https

scala> url.getHost
res2: String = www.introverse.com

scala> url.getPath
res3: String = /abc/def

scala> url.getQuery
res4: String = id=1&id=2

scala> url.getRef
res5: String = index1

Sunday, 16 December 2018

oracle create Entity Relation Diagram(ERD) from existing SQL DDL


I have a existing DDL for my application but I wanted to see the ERD for that. Oracle SQL developer provides Data modeler feature to generate ERD from existing DDL as shown in following screenshot
(import as Data Dictionary)



Sunday, 9 December 2018

nodejs - call REST api with url encoded parameters

I have a REST API that expects url encoded request params as POST request body. So, API schema looks like
POST /login HTTP/1.1
Host: intro-api.com
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: xxx
And, If you use curl request would look like,
curl -v --request POST -H "Content-Type: application/x-www-form-urlencoded" intro-api.com/login -d 'username=admin' -d 'password=admin'
Also, since content-type is urlencoded type, request params are in query string format. So -d "username=admin&password=admin" is valid as well.

Now,

I have a frontend(html/js) served by nodejs server which would have to validate user login by calling above REST api. This is what I'm talking in this article.
So, first of all I tried standard nodejs http library as a http client. But could not make it work for url encoded params. I was getting 408 error, which means it never sent the params?
function getUserSession(username, password) {

  const postData = qs.stringify({
    'username': username,
    'password': password
  });

  var options = {
    protocol: 'https:',
    host: 'intro.com',
    port: 443,
    path: '/login',
    method: 'POST',
    headers: {
      'Content-Type'  : 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(postData)
    }
  };

  https.request(options, function(resp){
    var response = '';
    resp.on('data', function(chunk){
      response += chunk;
      console.log(response)
    });
  }).on("error", function(e){
    console.log("Got error: " + e.message);
  });

}
So I ended up using request library. which needs to added to package.json (can also be done with npm install request, for more see official documentation).
Next, I will create auth.js with with a function that expects username and password and call the REST api, and respond the cookie value.
var request = require('request');

function getUserSessionId(username, password) {
  console.log("making login request")

  request.post({
    url: 'https://intro.com/login',
    form: {
      username: username,
      password: password
    }
  }).on("response", function(httpResponse) {
        var session = httpResponse.headers['set-cookie']
        console.log("session: " + session)
        return session
  });

}

var token = getUserSessionId('admin', 'admin')
Now, lets run it on terminal using node runtime (without having run it on http)
node auth.js
That is it to call "a REST api with url encoded params" using nodejs.

Thursday, 22 November 2018

haskell 101 - hello world with cabal

I am in love with fp these days. In my opinion the cool thing about fp is about being deterministic. Just like in mathematics where a function f(x) = x * x always returns a same output for a given input.
I have been in situtations where I had to debug in exising applications where reference is passed around all over the place and mutated in multiple places. It is very dificult to trace mutations. I'm still learning fp but this week decided to do some haskell as if I'm building a professional application so that I can also learn the tooling in haskell.
So, the first step is to install cabal package manager which is equivalent to sbt, gradle or maven in JVM world.
$ cabal --version
cabal-install version 2.0.0.0
compiled using version 2.0.0.2 of the Cabal library 
With cabal installed in a machine next step is to create a create a haskell project with base structures which is what cabal init does.
mkdir infp
cd infp
cabal init
Otherwise there is always an option to create those file by yourself.
The structure of a project looks like
$ tree .
.
├── ChangeLog.md
├── LICENSE
├── Setup.hs
├── infp-world.cabal
└── src
    └── Main.hs

1 directory, 5 files
*.cabal equivalent to pom.xml of JVM world for dependency management and application artifact creation etc which will look like
name:                infp-world
version:             0.1.0.0
license:             BSD3
license-file:        LICENSE
author:              prayagupd
maintainer:          upd@upd.com
build-type:          Simple
extra-source-files:  ChangeLog.md
cabal-version:       >=1.10

executable infp-world
  main-is:             Main.hs
  hs-source-dirs:      src
  build-depends:       base >=4.10 && <4.11
  default-language:    Haskell98
build-depends section is where I have to add external dependencies which are available in http://hackage.haskell.org/packages/
For example if I need mongodb driver http://hackage.haskell.org/package/mongoDB could be the one which I would add as
build-depends:       base >=4.10 && <4.11,
                     mongodb == 2.4.0.0
For this application we simply want to print the system time so lets use the time package,
build-depends:       base >=4.10 && <4.11,
                     time == 1.9.2
Followed by cabal install to download the dependencies which will be installed to ~/.cabal/packages/ pretty much same as ~/.m2/repository in JVM world.
Now, now the next step is to add some functionality to get system time in main function.
To do that let's create following src/Main.hs. Note to edit the file, you can use vim or emacs with autocomplete (I use spacemacs - https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Blang/haskell).
module Main where

import Data.Time
import Control.Applicative

main :: IO ()
main = do
  time <- getZonedTime
  putStrLn ("current time: " ++ show time)
lets build the project now which will create an executable in dist.
$ cabal build
Preprocessing executable 'infp-world' for infp-world-0.1.0.0..
Building executable 'infp-world' for infp-world-0.1.0.0..
It's good to see no compilation error, so it is good to run now with cabal run,
$ cabal run
Preprocessing executable 'infp-world' for infp-world-0.1.0.0..
Building executable 'infp-world' for infp-world-0.1.0.0..
Running infp-world...
current time: 2018-11-22 22:54:06.355117 PST
There we go, it prints current time: 2018-11-22 22:54:06.355117 PST which is the what we wanted.
So, there I end the hello world in haskell using cabal package manager. I am doing REST API on haskell as well so at some point plan to write a post on that.