Pages

Wednesday, December 26, 2018

Microsoft Q# is my choice

Today I am thinking to dig into the real quantum programming after learning the foundation part. There are three big players I was looking at on quantum programming framework/language. I hate to waste my time learning the framework or language itself. I want to focus on so I can focus on solving the problem. I have three choices:
  • IBM, IBM Q
  • Google Cirq
  • Microsoft Q#
It seems IBM Q and Cirq are reusing the Python to provide a framework while Q# is a brand new language. It makes sense IBM and Google use Python as base as it has a good math library. However, I doubt this choice is the best choice. Like the GPU, quantum is special hardware. Cuda is a like C language but it has its own features. For quantum computing, it has a totally different way to compute. Therefore, I think the best way to use a new language, maybe it is just a DSL. C++ can be used to write a functional program, but the best way is still to use a functional programming language. 

Also, the examples from those three companies show a huge difference. Q# provides the best sample pack so far. It has a quantum foundation, matrix computation, and programming language. It will help me a lot to adopt this new stuff. 

The setup and debugging is the last thought. I had tried to step away from Microsoft stack; however, the debugging and the setup needs more time I expected. I was spoiled by double click and just working. 

Anyway, the current Q# shows good progress and the watch, star, and pull request number is bigger than other players. I will continue to focus on the quantum basics, like matrix and math part, anyway. 

Sunday, December 23, 2018

SelfNotes: Quantum Algorithms

I believe the quantum computing is the key to enable real AI. No matter how complicated the AI algorithm can be, the fundamental computing power will make a real difference.

The huge difference between a classical computer and a quantum computer is so intriguing! It is the first time that math can come back to computer science and guide its evolution. Also, the quantum algorithm is the first time it introduces the internal parallelism. Those existing algorithms barely considered parallelism when it is designed. Now the quantum can change this situation!

The following are some notes about quantum algorithms. They are not related to any language implementation, it is just pure math. The only tool I want to use is "that piece of meat between my ears". :)








Saturday, December 8, 2018

Automation with NodeJS-Puppeteer & Visual Studio Code

I saw many attempts to automate the processing using commercial software. However, I do not see this is necessary if this task is not that huge. When more and more application is moving to the web-based. Automation of the Chrome browser will automate most of the scenarios. Therefore, I feel 90% of those cases won't need to pay and still get the job done.

First, we need to set up IDE. There are three files needs to be updated and placed in the place. Please make sure you put launch.json and tasks.json are under .vscode folder. 

  • launch.json: it is used to launch the application with predefined tasks such as Typescript build. 
  • tsconfig.json is to used to build the application using Puppeteer
  • tasks.json is to define the Typescript build process

The content of these three files
  • launch.json

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "sourceMaps": true,
            "preLaunchTask": "TSC",
            "program": "${workspaceFolder}/app.ts",
            "outFiles": [
                "${workspaceFolder}/**/*.js"
            ]
        }
    ]
}'


  • tasks.json

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "TSC",
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}


  • tsconfig.json


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",        
        "lib": ["es2015", "dom"],
        "sourceMap": true
    },
    "include": [
        "src",
        "node_modules/@types/puppeteer/index.d.ts"
      ]
}


The node_modules folder is created from the npm command provided by the NodeJS framework. The command to run is listed the following. The first line is to install TypeScript compiler which is not installed by default with VS Code. The second line is to install Puppeteer. The last line is to install the library for Typescript to work with Puppeteer.


1
2
3
npm i -g typescript
npm i --save puppeteer
npm i --save-dev @types/puppeteer

The last file is the Puppeteer file:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
import * as puppeteer from 'puppeteer'
 
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('http://www.google.com');
  await page.screenshot({path: 'example.png'});
 
  await browser.close();
})();


Sunday, November 11, 2018

Move NLP IKVM to F# .NET-Friendly

Since the last post, I read Sergey's code. Then I decided to work on refactoring the code to store the data into the .NET and F# format. Stanford NLP does provide a server. I still want to make it .Net friendly and also get myself familiar with the NLP core.

I prefer the project-based solution than the interactive solution is because it can have Visual Studio's watch, immediate windows, and debug visualizer. Once I got the information into a comfortable environment, it will be easy to move forward. 

The 200-line code is to build up a structure like the following:

I call multiple sentences a story. A story contains (1) sentences and (2) cross-references. The sentence structure shows NLP info about a sentence. The sentence includes a token list, tree, and dependency graph. The cross-references maintain the relationship among elements from different sentences. The first file is the main file. It shows how to invoke the underlying functions and show the structures. 


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.

open System
open System.IO
open java.util
open java.io
open edu.stanford.nlp.pipeline
open edu.stanford.nlp.ling
open Utils.NLPUtils
open Utils.NLPExtensions
open edu.stanford.nlp.util
open edu.stanford.nlp.trees
open edu.stanford.nlp.semgraph
open Utils.NLPStructures
open edu.stanford.nlp.coref

[<EntryPoint>]
let main argv = 
    let text = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply email.";

    // Annotation pipeline configuration
    let props = Properties()
    props.setProperty("annotators","tokenize, ssplit, pos, lemma, ner, parse, dcoref") |> ignore
    props.setProperty("ner.useSUTime","0") |> ignore

    let pipeline = StanfordCoreNLP(props)

    // Annotation
    let annotation = Annotation(text)
    pipeline.annotate(annotation)

    //get annotation info
    let keys = annotation.GetToken<HashMap>(typeof<CorefCoreAnnotations.CorefChainAnnotation>)
    let mentions = keys |> Seq.exactlyOne |> getMentions        

    let sentences = 
        [
            let sentences = annotation.GetToken<CoreMap>(typeof<CoreAnnotations.SentencesAnnotation>)

            for s in sentences do
                let tokens = s.GetToken<CoreLabel>(typeof<CoreAnnotations.TokensAnnotation>)
                let words = getWords tokens

                let t = s.GetToken<Tree>(typeof<TreeCoreAnnotations.TreeAnnotation>)  
                let tree = t |> Seq.exactlyOne |> buildTree words
       
                let deps = s.GetToken<SemanticGraph>(typeof<SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation>)
                let relationships = deps |> Seq.exactlyOne |> getDependencyGraph words
                let sentence = { Words = words; Dependency = relationships; Tree = tree; }
                yield sentence
            ]

    let story = 
        {
            CrossLinks = mentions;
            Sentences = sentences;
        }

    printfn "%O" story

    0 // return an integer exit code


The second file is the library file. I do not think the structure will stay same after two weeks. I might decide to add more fields. But currently the foundation is there.



1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace Utils

module NLPStructures = 

    type Word = string
    type Ner = string
    type POS = string
    type Index = int
    type Relationship = string
    type Span = int * int
    type Head = int * string
    type SentenceIndex = int
    
    type WordType = 
        {
          Word : Word
          Ner: Ner
          POS: POS
          Index: Index
        }

        member this.IsSame word = 
            match this with
            | { Word = w; } -> w = word

        member this.IsSame index = 
            match this with
            | { Index = i } -> i = index

    type MentionEntity =
            {
                Index: Index
                Relationship: Relationship
                Span : Span
                Head : Head
                SentenceIndex : SentenceIndex
            }
    
    type RepresentiveMention = MentionEntity

    type CrossLinkType =
        {
            Index: Index
            RepresentiveMention : RepresentiveMention
            Mentions : MentionEntity list
        }
    
    type DependencyGraph = 
        | Link of Relationship * WordType * WordType
        | CrossLink of CrossLinkType
        
    type TreeNode = 
        | Node of string * WordType
        | SubNodes of POS * TreeNode list
    
    type SentenceType =
        {
            Words: WordType list
            Dependency: DependencyGraph list
            Tree: TreeNode
        }

    type StoryType = 
        {
            CrossLinks : CrossLinkType list
            Sentences : SentenceType list
        }

module NLPUtils =
    open edu.stanford.nlp.trees
    open edu.stanford.nlp.ling
    open edu.stanford.nlp.semgraph

    open NLPStructures

    let toEnumerable<'T> (obj:obj) = 
        let l = obj :?> java.util.ArrayList
        l |> Seq.cast<'T>    
            
    let toJavaClass (t:System.Type) = java.lang.Class.op_Implicit(t)

    let findWord (words:WordType list) (word:Word) = 
        words |> Seq.find (fun n -> n.IsSame(word))
    let findIndex (words:WordType list) (i:Index) = 
        words |> Seq.find (fun n -> n.IsSame(i))

    let inline getObjFromMap (x:^T) t = 
        let key = t |> toJavaClass
        (^T : (member get : java.lang.Class -> obj) (x, key) )

    let getWords (tokens:seq<CoreLabel>) = 
        [ 
            for token in tokens do
                let word = typeof<CoreAnnotations.TextAnnotation> |> getObjFromMap token :?> Word
                let pos  = typeof<CoreAnnotations.PartOfSpeechAnnotation> |> getObjFromMap token :?> POS
                let ner  = typeof<CoreAnnotations.NamedEntityTagAnnotation> |> getObjFromMap token :?> Ner
                let index = token.index()
                let word = { Word = word; Ner = ner; POS = pos; Index = index }
                yield word
        ]

    let getDependencyGraph words (deps:SemanticGraph)  = 
        [
            for edge in deps.edgeListSorted().toArray() |> Seq.cast<SemanticGraphEdge> do
                let gov = edge.getGovernor()
                let dep = edge.getDependent()

                let govEntity = findIndex words (gov.index())
                let depEntity = findIndex words (dep.index())

                let e = Link(edge.getRelation().getLongName(), govEntity, depEntity)
                yield e
        ]
    
    let rec buildTree words (tree:Tree)  = 
        let label = tree.value()
        let children = tree.children()
        if children.Length = 0 then
            let x = tree.label() :?> CoreLabel
            let i = x.index()

            let entity = findIndex words i
            Node(label, entity)
        else
            let nodes = children |> Seq.map (fun tree -> buildTree words tree) |> Seq.toList
            SubNodes(label, nodes)

    let getMention (mention:edu.stanford.nlp.coref.data.CorefChain.CorefMention) = 
        let mentionId = mention.mentionID
        let span = (mention.startIndex, mention.endIndex)
        let relation = mention.animacy.name()
        let head = (mention.headIndex, mention.mentionSpan)
        let sentenceIndex = mention.sentNum
        let m = { Index = mentionId; Relationship = relation; Span = span; Head = head; SentenceIndex = sentenceIndex; }
        m

    let getMentions (keys:java.util.HashMap) = 
        [
            for key in keys.keySet().toArray() do
                let v = keys.get(key) :?> edu.stanford.nlp.coref.data.CorefChain
                let representiveMention = v.getRepresentativeMention()
                let m = getMention(representiveMention)

                let index = v.getChainID()
                let mentions = v.getMentionsInTextualOrder().toArray()
                let ms = mentions 
                         |> Seq.cast<edu.stanford.nlp.coref.data.CorefChain.CorefMention> 
                         |> Seq.map getMention
                         |> Seq.toList
                let r = { Index = index; RepresentiveMention = m; Mentions = ms; }
                yield r
        ]
    
    let returnSeq<'T> (x:obj) = 
        if x :? java.util.ArrayList then
            toEnumerable<'T> x
        else
            Seq.singleton (x :?> 'T)
    
module NLPExtensions = 
    open NLPUtils
    open edu.stanford.nlp.util

    type CoreMap with
        member this.GetToken<'T> (t:System.Type) = 
            t |> getObjFromMap this |> returnSeq<'T> 

The execution result shows below:




Saturday, November 3, 2018

SelfNote: Stanford NLP

I create this page as the master page for using F# on Stanford NLP.

Monday, October 29, 2018

F# Stanford NLP is running

After some configuration, I can successfully run the first NLP project with F#. Special thanks to Sergey's post! The post is very informative. His solution is based on the F# interactive while I prefer to use the project-based solution.

Sergey points out that one of the common problems to setup is the path problem. His claim is so true. I had stuck in this problem for days. Here is the process I followed.


  • Open Visual Studio 2017 and create an F# console application. 
    • I tried .net core app; it does not work as the IKVM has the dependency on the .NET framework
  • compile the F# console application and remember the debug folder location
  • Open NuGet and retrieve Stanford NLP CoreNLP. The current version is 3.9.1
    • Current Stanford NLP is 3.9.2. I suggest you download 3.9.1 version
  • download the Standard NLP 3.9.1 zip file
  • unzip the 3.9.1 file to the F# console app debug folder
  • go the unzipped folder and find the model JAR file

  • download WINRAR to unzip the JAR file to a folder, this folder should contain a folder called "EDU"
  • copy the "EDU" folder up to debug folder, so the structure in the "DEBUG" folder is like the following.
  

The F# file I was using is listed below. Set the "EDU" folder to the debug folder can save you from setting the CurrentDirectory. 


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.

open System
open System.IO
open java.util
open java.io
open edu.stanford.nlp.pipeline

[<EntryPoint>]
let main argv = 
    let text = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply.";

    // Annotation pipeline configuration
    let props = Properties()
    props.setProperty("annotators","tokenize, ssplit, pos, lemma, ner, parse, dcoref") |> ignore
    props.setProperty("ner.useSUTime","0") |> ignore

    let pipeline = StanfordCoreNLP(props)

    // Annotation
    let annotation = Annotation(text)
    pipeline.annotate(annotation)

    // Result - Pretty Print
    let stream = new ByteArrayOutputStream()
    pipeline.prettyPrint(annotation, new PrintWriter(stream))
    printfn "%O" <| stream.toString()
    stream.close()

    printfn "%A" argv
    0 // return an integer exit code

Executing the NLP program seems taking a lot of memory. My program uses 2G memory and takes a while to show the result. Hopefully, your computer is faster enough. :)

Thursday, October 25, 2018

F# Enum usage II

I want to take a quick note on the F# enum usage again. The supporting of the space and unique character in F# language and editor is a great feature can make your development work much more comfortable. I am in the Web API front these days.

One of the requirement is to provide options to end users. If an application only takes "excellent choice", "good option", "ok choice", and "bad and never go there" as options, I'd like to offload these values check to the type system instead of handling the error in my code.

If I can declare the enum like the following

type enum MyEnum =
    ``excellent choice`` = 0
    | ``good option`` = 1
    ....

After declaring the enum-as-string attribute on the attribute [JsonConverter(typeof(StringEnumConverter))], the output and input validation is solved in one shoot.

Monday, September 24, 2018

F# Enum's Usage

I have a web service project and I found my team constantly needs to convert enum to a string. The only conversion is adding space. It wastes lots of time and involves in reflection which slows down the run time performance.

Enum type is one of the favorite types in F#. C#'s enum type does not have the ability to define a value with space. Having an enum value with space is very important because this feature can save me lots of time to output enum value as string.

For example, if the enum value can be "Post Release", the ToString function can output a nice string and won't have to use attribute and reflection to do the job. C# does have the ability to have space in enum but it will need to use reflect and emit to generate the type. If you can use F#, the problem can be easily solved. Please check the following F# code:

namespace ClassLibrary1

type public EnumEng =
    Registration = 0
    | ``Under Review``=1
    | Approval = 2
    | Release = 3
    | ``Post Release`` = 4

type public EnumChn =
    注册 = 0
    | 审批 = 1
    | 批准 = 2
    | 发布 = 3
    | 发布后 = 4

type public EnumIndex =
    Reg = 0
    | Review = 1
    | Approval = 2
    | Release = 3
    | PostRelase = 4


module Test =
    let a = EnumEng.``Under Review``
    let b = EnumChn.审批

In the C# side, the intellisense won't display the enum value if it contains space. However, it will be displayed in debug mode. You can execute the following code and stop at the end of the function.

        static void Main(string[] args)
        {
            //get all string from enum0
            var strs = Enumerable.Range(0, 5)
                                 .Select(n => (ClassLibrary1.EnumEng)n)
                                 .Select(n => n.ToString())
                                 .ToList();

            // get all string from enum2
            var i = Enumerable.Range(0, 5)
                              .Select(n => (ClassLibrary1.EnumChn)n)
                              .Select(n => n.ToString())
                              .ToList();

            // parse string to enum
            var v = strs.Select(n => Enum.Parse(n))
                        .ToList();

            var x = ClassLibrary1.EnumIndex.PostRelase;

            var str = (ClassLibrary1.EnumChn)x;
        }

Both # and C# support non-English variable name, it will provide a way to localize the output as well. From the sample above, the EnumIndex is the type can used in C#/F# code. Once the value needs to be output as string, it can be convert to EnumEnglish (English string) or EnumChinese(Chinese string).



Is that convenient?

Thursday, September 13, 2018

SelfNote: WebAssembly With Blazor & .NET Core Upgrade

After set up the HTML5/Typescript roadmap for my group, I started to move my interest to other UI/visualization technology. With the Blazor is in the starting phase, I feel this is an opportunity to get real-time web rendering and .net core in one shoot. The get start part in Blazor is very helpful. Only one small bug for the project creation if you upgrade .NET core.

From PowerShell window, you can find the .Net core version by using "dotnet --info". The Blazor service generates the global.json file, which sits beside the solution file. This file is required to load the .Net core. The default value in the file is 2.1.300. 

{
  "sdk": {
    "version": "2.1.300"
  }
}
My computer is new and I directly installed 2.1.402 version. Now I got the error complains about "cannot import package". After change that version to 402, I can now manually add those created projects (Blazor Server and Client) to the solution.  

Sunday, September 9, 2018

A Tech Manager's View on Full Stack Developer

As a hiring manager, I was asked by agent about full stack developer. I am not a big fan of so-called full stack developers. 

No one can have unlimited memory and time to be expert in all area. I am more interested in knowing what the depth one can go. Those average stuff can be searched from internet. Those experience does not demonstrate the critical thinking skills. Those solution, with high probability, introduces high maintenance cost down the road.

Being deep in one area requires perseverance and talent, those attributes will provide a foundation to long lasting solution. 

Being a technology manager, maintaining different technology stack has a huge cost factor and segment team into small silos. The team effort will be lost. The tech plan and road map is missing. 

I have read some job description that requires tens of unrelated technology. This kind of position really makes me thinking how effective the tech management is and what the team looks like.

Friday, August 31, 2018

Technology from a Tech Manager's View - Measure Productivity

It is always an intriguing topic about software development productivity. There are some attempts to measure productivity. From those numbers, there are some conclusions and team chaos and management challenge are knocking at the door.


First of all, I do not think this productivity is measurable. If a computer program can understand how much effort to solve a problem, this program will be smart enough to solve the problem. Then we don’t need software developers at all.

The worst thing about this measurement is let team members know about it. The assumption is measurement will give insight into the problem and eventually leads to a solution. Not mention the trust between employee and management is broken, the assumption itself has a fatal error. You can measure stone's size and weight and those numbers won’t change. However, the subject of the measurement is human who will change the behavior if s/he knows someone is watching.

Naturally the subject (human) changes behavior to make the number better. Will this change really improve the productivity? Most likely not. The first thing human subject can do is to game the system. Will this effort result in high quality software? No. When you know you are not trusted, are you willing to contribute more?

Einstein's noble prize paper is only a few pages. Essentially the attempt to measure is put equivalence between software development activities and operational labor work.


Saturday, August 25, 2018

Technology from a Tech Manager's view - Coding Standard

I have been in this industry for more than 10 years. Growing from a junior dev to a senior manager, I associated my name with many projects. I witnessed some interesting practices during the software development, especially in a team environment. This post is the first post I want to share my thoughts as a tech manager.

Coding standard or best practice? I have witnessed so many attempts to set up some coding standards in a team and failed. There are still coding standard proposed every day and I bet there are some emails flowing around  now. I can't say all of them, but most of them, will fail miserably. I have seen quant code, dev's code, system admin's code, etc. It seems that everyone has its own coding standard. Every industry has its own way to practice the software development. Is there a general coding standard, or should a coding standard exist at all? 

Now days, I feel those standard or effort to push such standard does not make much sense. Programming language is a way human tell a computer how s/he understand and solve the problem. It is like human language. Is there a standard way or "best practice" to say something? Do you have the "best" way to describe how solar system works for a 10-year-old boy? Is the "best way" still the best when facing a 20-year-old? 

For me, I think the best way to set up the "best practice" to understand your team. if my team has quant or physics background people. They want to use single-character variable name, it should be fine. Take a look at those mathematics paper they read every day, it is not difficult to understand why single-character variable name is the best choice for them. 

If I had a team whose members are all system admins, should I ask them to define interface or introduce complicated inheritance? Definitely not. Does this mean my project will fail? No. 

My job is to have my team to tell the computer how to solve a business problem. My team will use a programming language + framework to describe the solution. As a tech manager, my job is to choose the right technology for my team. That technology should be the most efficient and convenient way to 
  • understand and communicate among team members
  • communicate between a human and a computer
The diversity of technology is the outcome from innovative person and team to serve different people in different business environment. If you are pushing for innovation and still enforce a rigid coding standard, think twice.


Sunday, August 12, 2018

Solve Automation & Biz Flow Project Resource Problem with PowerShell

When I take over the DevOps project to do the automation work. My project is not juts to streamline the software development process, it involves in large number of internal systems. For a company with many legacy systems and different business process. It is literally re-engineer the business flow.

My challenge is to have the people who know the business flow and the technology and make them work together. I do not want to hire BA because I have enough people who know business knowledge. The pressing task to have more person to code the logic. Thanks to my language background, the solution I found is to use different languages to solve the problem.

The first decision I made is to have no-GUI at all. The fancy GUI is nice to a new user; however, it will block the future automation for good. When you have a meeting with your financial planner, you will notice how many times s/he do copy/paste from one system to the other.

To fast code the solution, I need a scripting language or a language support scripting feature. That language should be good for folks with less coding experience. I do not need them to understand what is singleton pattern or difference between NTLM and Basic Authentication. I want my dev team to focus on the technical challenges and provide API's. Biz-background people can use those API's to streamline those processes in his/her area.

The initial setup is like the diagram following. Part1 is finished by biz people by invoke API's provided by dev's working on Part2.



I landed on the PowerShell language for Part1. PowerShell (PS) is designed for support/op folks. It has large number of samples. The most compelling reason for PS is that it can move the interface line from top to very bottom in the above diagram. PS can go deeper. Almost everything C#/F#/VB.net can do, PS can do. At the project initial stage, Part2 is much bigger than part1. I want a language has the potential to go deeper to the binary code in case my dev does not have the bandwidth to provide API. Other .Net languages can invoke PowerShell as well. As a result, the coding effort has little waste. A perfect eco-system where every bit of coding effort can be used and reused.

The current result is my biz people can code PowerShell and slowly push the "interface line" down to the ultimate binary format. The dev side does not have to deal with those "boring" business work. And the dev workload is getting less. By using PowerShell scripting language,

  • Efficiency: one line PS code will do 10+ lines of .net language code. The overall progress will be faster when using a scripting language. 
  • cost saving, I have the potential to expand my team to accommodate more people with various programming level
  • Solid and quick solution: both parties can focus on the area in which s/he has deep expertise. The implementation speed is good.
  • Flexible solution: since the interface line can move up and down based on the resource / expertise. 
  • Vendor-independent: my solution is general enough so won't be locked in any vendor's customized solution. 
When the automation happens, one of the push back is the job lose. Having a person in the domain is the deciding factor for project success. The PowerShell provide a comfortable environment for biz people to start and grow, which makes my biz part team stable. 

To use a widely adopted language also stabilize the dev side. I witness many effort to have an in-house language which is only used in that team. Many vendor solution provides a programming interface which few people know how to use. The customized solution literally blocks the people from mainstream technology and few people wants their career to be in this situation. If you search for PowerShell, you can still find job openings requires that skill. This option really helps the adoption of the solution. With .net core moves to Linux platform, PowerShell 6 and all other .net languages will flood into Linux world. So the future is really bright. 


So far I am very happy about the result!

BTW: if you want to use F# to write PS module, here is the link.


Tuesday, June 12, 2018

My DevOp Project

I am into devOps for a week. The basic building block is to automate those systems. I have to say the PowerShell is a good tool to do this kind of work from a Win32 box. My first task is to call some services in a web site. I can find those services URL from the web page but need to go through some authentication processes.

The Invoke-WebRequest cmdlet + Chrome's network monitoring turns out to be the perfect solution to do this work.

First step is to understand how the web page performs.

I open the Chrome network monitoring tool by right click the web page and select "inspect". It shows the current DOM structure of displaying page. Click the "network" tab and select 'preserve log". The "preserve log" feature will keep all the logs even the page changes.



Now the target web page address can be pasted to Chrome and lots of log will be generated. I can see many page direction and form submission during this process (sorry, I cannot paste screenshot. 😉)

Step 2: Write down all the pages "network" tab shows after the initial web page is loaded. Please write down those web addresses. 

Now the simulation part starts. There are two variables needed for Invoke-WebRequest cmdlet:

  • SessionVariable: Browser maintains a session between client and server. So "-sessionVariable" parameter is a must. It will create a mutable variable to store session information between client and server, e.g. cookies. 
  • MaximumRedirection: PowerShell can automatically redirect to next page if you set the value to this variable > 0. I recommend to use 0 as initial test so you can know what is going on exactly.
Now let's the fun begin. 

Step 3: open PowerShell 
Step 4: run $r0 = invoke-webrequest -uri
-SessionVariable sessionVarialbe0 -MaximumRedirection 0

This will create the session variable "sessionVariable0". There is an error shows the maximum direction step is smaller. Please ignore this error. 

The cmdlet returns a $r0 variable, please check the $r0 content and you will find the web page address and redirection address from header field. Make sure the redirection address equals the one you get from Chrome network monitoring page (in step 2)

Step5: $r1 = inovke-webrequest -uri -webSession $sessionVariable0 -MaximumRedirection 0

Repeat the check performed on $r0.

if you cannot find the redirection address, please refer to "Form" variable in the $r1. You can invoke the address in the Form's action field and submit the fields in the HTTP request body part. For example:

$addressInForm = $r1.Forms[0].Action
$r2 = invoke-webrequest -uri $addressInForm  -SessionVariable sessionVarialbe0 -MaximumRedirection 0 -Method Post -UseDefaultCredentials -Body $r1.Forms[0].Fields

you might find the default browser opens, that's because PowerShell uses default browser to parse. The browser might show some error message, please ignore. 

You can repeat the above steps until you get the final destination. And the sessionVariable0 is the treasure from this journey, it contains all the information you need for future automation work. 

Happy hacking automation. ;)