<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chuan-Yen Chiang on Medium]]></title>
        <description><![CDATA[Stories by Chuan-Yen Chiang on Medium]]></description>
        <link>https://medium.com/@cy.chiang?source=rss-b9959e27e72e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*R919JiZ3KB2_sJL3nu1uKQ.jpeg</url>
            <title>Stories by Chuan-Yen Chiang on Medium</title>
            <link>https://medium.com/@cy.chiang?source=rss-b9959e27e72e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 13:35:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cy.chiang/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Problems when trying to connect AWS Services with Istio]]></title>
            <link>https://medium.com/@cy.chiang/problems-when-trying-to-connect-aws-services-with-istio-a5140d2846ab?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5140d2846ab</guid>
            <category><![CDATA[istio]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Fri, 31 Aug 2018 08:14:34 GMT</pubDate>
            <atom:updated>2018-08-31T08:14:34.395Z</atom:updated>
            <content:encoded><![CDATA[<p>Have your services inside Istio service mesh are connect to AWS RDS with correct username and password but answer you back with incorrect username/password response? If your services need to connect multiple RDS with different username and password, here maybe the reason: <strong>ServiceEntry, </strong>your connections are different than you expected.</p><p>Few days ago, I am trying to deploy our services into service mesh (Istio) and faced some problems(maybe not) when tried to connect AWS RDS. Get a response like:</p><pre>mysqli_real_connect(): (HY000/1045): Access denied for user &#39;user&#39;@&#39;your.ip.address&#39; (using password: YES)</pre><p>Looks like the service is connected, but failed in authentication part. Why?</p><p>I did a little experiment with <strong>phpmyadmin inside service mesh </strong>to figure it out. Let’s assume we have 2 database, <strong>d1</strong> and <strong>d2</strong>. Each database have specific username/password:</p><ul><li><em>Connect to </em><strong><em>d1</em></strong><em> with </em><strong><em>username1/password1</em></strong><em>: It’s working</em></li><li><em>Connect to </em><strong><em>d2</em></strong><em> with </em><strong><em>username2/password2</em></strong><em>: Not working</em></li><li><em>Connect to </em><strong><em>d2</em></strong><em> with </em><strong><em>username1/password1</em></strong><em>: IT’S WORKING!</em></li></ul><p>Clearly, the service is connecting to the wrong database. But why?</p><p>As we know, if your traffic will go outside the mesh, you need to specify an ServiceEntry as following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258cd7138ce8deef2cc87bb83835b363/href">https://medium.com/media/258cd7138ce8deef2cc87bb83835b363/href</a></iframe><p>However, if services are connecting to multiple RDS or Redis, your ServiceEntry may look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/976dcf98af8cfc7ff2e1df9f88f9d1e7/href">https://medium.com/media/976dcf98af8cfc7ff2e1df9f88f9d1e7/href</a></iframe><p>Looks good right? But here is the problem, <strong>resolution: DNS</strong></p><p>Why? What happened to <strong>resolution: DNS</strong>? The istio provides 3 methods for the resolution: <strong>NONE, STATIC and DNS. </strong>The major difference between those three is, when will you know the exactly address of your request/traffic. After that, I realized one thing, we don’t need to get ip address from our proxy server. Just leave your DNS to AWS. So, the ServiceEntry will look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d824a2fa0f2d0533baf6bc0cda5b131/href">https://medium.com/media/9d824a2fa0f2d0533baf6bc0cda5b131/href</a></iframe><p>Problem solved.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5140d2846ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to integrate AWS ALB with istio v1.0]]></title>
            <link>https://medium.com/@cy.chiang/how-to-integrate-aws-alb-with-istio-v1-0-b17e07cae156?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b17e07cae156</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[application-load-balancer]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[istio]]></category>
            <category><![CDATA[elasticache]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Sat, 25 Aug 2018 00:56:19 GMT</pubDate>
            <atom:updated>2018-08-25T00:56:59.117Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s been awhile not writing any post here. Recently, I spent a lot of time to migrate existing backend services from Node.js to Golang, design and implement new architect from monolithic to micro-service and service-mesh. Also, spent sometime to take a look and on Dart and Flutter. Implemented some stuff with Dart and Flutter (<a href="https://github.com/cychiang">github</a>, if interesting).</p><p>Let’s go back to the main course. As mention before, I am in charge of backend architecture and infrastructure. All changes I have made can summarize into 3 major parts:</p><ul><li>Containerized all services and formed all services in Helm template</li><li>Kubernetes to manage all services</li><li>Istio, service mesh</li></ul><p>I am fascinated by <a href="https://istio.io/">Istio</a> before and always try to integrate <a href="https://istio.io/">Istio</a> into our production environment. Because it offers a lot of out-of-box benefits and make monitoring every single request, traffic flow much easier and transparent than before.</p><p>Here, I am going to share my experience on integrate AWS application load-balancer and Istio.</p><h3>Pre-requirement:</h3><ul><li><a href="https://github.com/kubernetes-sigs/aws-alb-ingress-controller">aws-alb-ingress-controller</a>: You need to install and familiar with this tool, it will help you to deploy AWS application load-balancer.</li><li>Download Istio v1.0</li></ul><p>In this post, we use helm to deploy all of our services.</p><h3>Step 1: Before you install Istio with helm chart</h3><ul><li>set <strong>global.k8sIngressSelector=ingressgateway</strong></li><li>set <strong>ingress.enable=false</strong></li></ul><p>There are many way to capture traffic flow from internet to Kubernetes. But, if we want to integrate with AWS alb than we need to start our own ingress control with <a href="https://github.com/kubernetes-sigs/aws-alb-ingress-controller">aws-alb-ingress-controller</a>.</p><ul><li>set <strong>gateways.enabled=true, gateways.istio-ingressgateway.type=NodePort</strong></li></ul><p>This is very important part to set <strong>gateways.istio-ingressgateway.type </strong>into <strong>NodePort</strong>, because AWS application load-balancer will route all traffic to specific port and not a ip address.</p><ul><li>than, install Istio into your cluster</li></ul><h3>Step 2: Create Your AWS application load-balancer</h3><p>Here is an example yaml file to create an ALB, make sure your <strong>aws-alb-ingress-controller</strong> is up and running.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdeca8eed2f0b36c41d1c0bcd9b9a179/href">https://medium.com/media/fdeca8eed2f0b36c41d1c0bcd9b9a179/href</a></iframe><p>Here are few things need to be careful:</p><ul><li>Make sure your istio-ingress and istio-ingressgateway are in the same namespace</li><li>Make sure your servicePort number is described in istio-ingressgateway service.</li><li>The istio-ingressgateway actually is like a istio-proxy(envoy proxy) and it will route all traffic to Istio service mesh. Just like a gatekeeper.</li></ul><h3>Step 3: Create Gateway to tell pilot You are here</h3><p>After you create a <strong>Ingress</strong>, the ALB will be created by <strong>aws-alb-ingress-controller</strong>. And will route traffic with hostname “your.hostname.com” to serviceBackend: istio-ingressgateway with port:80</p><p>Now, we need to create another gateway to tell pilot that please forward all hostname with “your.hostname.com” from istio-ingressgateway to me.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0f74c3276d02607dd11b72c6b4c1bc9/href">https://medium.com/media/d0f74c3276d02607dd11b72c6b4c1bc9/href</a></iframe><p>Here, you can put your gateway into your favorite namespace.</p><h3>Step 4: Create VirtualService</h3><p>After we created a gate to capture traffic flow from istio-ingressgateway. Now, we need to deal with request and route every request into their destination. Create a VirtualService and define what information we need to route a request into a right destination. In this <a href="https://gist.github.com/cychiang/c5b0de0ceb731af6701ea04ba6fd155a">istio-virtualservice.yaml</a>, we simply say, every request from <strong>gateways: magic-gateway(we have created on previous step)</strong> and <strong>hosts: “your.hostname.com”</strong> also uri is match <strong>“/”</strong>, please go to <strong>host: gordon-house at port: 8080</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d49ff0feaee4ad6ced1d4d54cb748dd/href">https://medium.com/media/4d49ff0feaee4ad6ced1d4d54cb748dd/href</a></iframe><h3><strong>Step 5: Create a Service and Deployment for your Service</strong></h3><p>Finally, a familiar part is raising up. There is nothing fancy here, just create a service for your deployment and make sure the name and port number of your service is exactly same as mentioned in VirtualService we just created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bd9b299a173444d5ba091279b6ffa4d/href">https://medium.com/media/5bd9b299a173444d5ba091279b6ffa4d/href</a></iframe><p>Here, I create a simple deployment with httpbin for the demo.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3a64fc9805aa528b4c63389308b30f4/href">https://medium.com/media/d3a64fc9805aa528b4c63389308b30f4/href</a></iframe><p>After finish step 1 to 5, your request should work from ALB to your service. Here is a big picture of step 1 to 5:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PrOERRoN8LkzCNIEPs5VAg.png" /></figure><h3>Step 6 (optional): If your service need to connect to external service like AWS ElastiCache?</h3><p>All egress from your service will be restricted which is good for security matter. So, if you want to connect external resource outside your service mesh (<a href="https://istio.io/">Istio</a>), kubernetes cluster. You need to create 2 more things to allow traffic from your service to other external service:</p><ul><li><strong>VirtualService</strong>: define which request that need to be take care.</li><li><strong>ServiceEntry</strong>: define a destination for a specific request.</li></ul><p>Here is a simple example to demonstrate how to connect AWS ElastiCache (Redis).</p><p>First, define a <strong>VirtualService</strong> of a destination for specific hosts or ip address, here we use hostname as example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d32d4756f093ffe19c4a7df2ebee65a/href">https://medium.com/media/7d32d4756f093ffe19c4a7df2ebee65a/href</a></iframe><p>Than, define ServiceEntry to declare a request with specific hostname and port number could go outside of service mesh.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258cd7138ce8deef2cc87bb83835b363/href">https://medium.com/media/258cd7138ce8deef2cc87bb83835b363/href</a></iframe><p>Now, your service should connect to AWS ElastiCache(Redis) and able to get data.</p><p>In conclusion, <a href="https://istio.io/">Istio</a> is a powerful tool, but require more work on initial stage. For example, we need to fully understand what traffic will go inside the mesh and what will go outside the mesh. Missing any single configuration of your service may result unexpected consequences. Before move your services to <a href="https://istio.io/">Istio</a>, this is good to make a checklist to document every managed hostname, api path and external(third party) service.</p><p>Thank you for reading, any comments are welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b17e07cae156" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: if/else statement vs dictionary]]></title>
            <link>https://medium.com/@cy.chiang/python-if-else-statement-vs-dictionary-c156b226c04d?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c156b226c04d</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[dictionary]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Thu, 05 Apr 2018 03:44:47 GMT</pubDate>
            <atom:updated>2018-04-05T03:44:47.992Z</atom:updated>
            <content:encoded><![CDATA[<p>One day, my friend posted a message on his Plurk about coding style of if/else statement. The story is, he wants to know how elegant to use “if/else” instead of use “if/else if/else” statement. For example, if we want to implement different operator based on specific element of JSON file.</p><pre>Here is the json file looks like.<br>{<br>    &quot;ops&quot;: &quot;add&quot;,<br>    &quot;value_1&quot;: 1,<br>    &quot;value_2&quot;: 2<br>},<br>{<br>    &quot;ops&quot;: &quot;sub&quot;,<br>    &quot;value_1&quot;: 1,<br>    &quot;value_2&quot;: 2<br>},<br>{<br>    &quot;ops&quot;: &quot;mul&quot;,<br>    &quot;value_1&quot;: 1,<br>    &quot;value_2&quot;: 2<br>},<br>{<br>    &quot;ops&quot;: &quot;div&quot;,<br>    &quot;value_1&quot;: 1,<br>    &quot;value_2&quot;: 2<br>}</pre><p>Here is the original code style of my friend.</p><pre>if data[&quot;ops&quot;] == &quot;add&quot;:<br>    ans = data[&quot;value_1&quot;] + data[&quot;value_2&quot;]<br>elif data[&quot;ops&quot;] == &quot;sub&quot;:<br>    ans = data[&quot;value_1&quot;] - data[&quot;value_2&quot;]<br>elif data[&quot;ops&quot;] == &quot;mul&quot;:<br>    ans = data[&quot;value_1&quot;] * data[&quot;value_2&quot;]<br>elif data[&quot;ops&quot;] == &quot;div&quot;:<br>    ans = data[&quot;value_1&quot;] / data[&quot;value_2&quot;]<br>else:<br>    # pass</pre><p>And I have different opinion of that code. I think the “elif” is a little bit “ugly” why don’t you just use “if” statement? Because we only need to deal with the operation we have provided. So, the code looks like this.</p><pre>if data[&quot;ops&quot;] == &quot;add&quot;:<br>    ans = data[&quot;value_1&quot;] + data[&quot;value_2&quot;]<br>if data[&quot;ops&quot;] == &quot;sub&quot;:<br>    ans = data[&quot;value_1&quot;] - data[&quot;value_2&quot;]<br>if data[&quot;ops&quot;] == &quot;mul&quot;:<br>    ans = data[&quot;value_1&quot;] * data[&quot;value_2&quot;]<br>if data[&quot;ops&quot;] == &quot;div&quot;:<br>    ans = data[&quot;value_1&quot;] / data[&quot;value_2&quot;]</pre><p>But, after a while, I came out with different idea, why do I just use lambda and dictionary to create a key-value map for the specific operator? So, the code looks like this.</p><pre>operators = {<br>    &quot;add&quot;: lambda x: x[&quot;value_1&quot;] + x[&quot;value_2&quot;],<br>    &quot;sub&quot;: lambda x: x[&quot;value_1&quot;] - x[&quot;value_2&quot;],<br>    &quot;mul&quot;: lambda x: x[&quot;value_1&quot;] * x[&quot;value_2&quot;],<br>    &quot;div&quot;: lambda x: x[&quot;value_1&quot;] / x[&quot;value_2&quot;]<br>}<br>ans = operators[data[&quot;ops&quot;]](data)</pre><p>After I provided my new idea to my friend and he says how about the else part? What if the data contains the operator that doesn’t existed in “ops” element? Yes, my code will failed. And I said, use try/except to catch exceptions and deal with that.</p><pre>try:<br>    ans = operators[data[&quot;ops&quot;]](data)<br>except KeyError:<br>    print(&quot;ops: {ops} not support&quot;.format(ops=data[&quot;ops&quot;]))</pre><p>But, how about we really want to add if/else statement into our code, said my friend. Well, let’s do it!</p><pre>if data[&quot;ops&quot;] in operators.keys():<br>    ans = operators[data[&quot;ops&quot;]](data)<br>else:<br>    print(&quot;ops: {ops} not support&quot;.format(ops=data[&quot;ops&quot;]))</pre><p>Let’s compare the performance of those implementation! Code is available in <a href="https://github.com/cychiang/python-for-fun/blob/master/dict_ifelse/app.py">github</a>.</p><pre>time_if_else_statement: 22.46713638305664 ms<br>time_if_elif_statement: 17.323017120361328 ms<br>time_try_except_dict_func_statement: 24.687767028808594 ms<br>time_if_else_dict_func_statement: 28.36775779724121 ms</pre><p>The results are kind of blow up my mind. The if/elif statement is much faster than other else when we set amount of test case into 50000. It’s kind of trade-off between performance and readable code. For me? I will choose <strong>try_except_dict_func_statement</strong> for my code of similar situation. Easy to understand and maintain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c156b226c04d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Istio: Why I choose to migrate?]]></title>
            <link>https://medium.com/@cy.chiang/istio-hands-on-experience-i-ad2f96fde024?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad2f96fde024</guid>
            <category><![CDATA[istio]]></category>
            <category><![CDATA[service-mesh]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Tue, 20 Mar 2018 00:16:28 GMT</pubDate>
            <atom:updated>2019-05-21T06:46:32.329Z</atom:updated>
            <content:encoded><![CDATA[<p>If you start to think about to manage your service from vm(virtual machine)to container. You will notice that almost everyone are talking about <a href="https://kubernetes.io/"><strong>Kubernetes</strong></a>, <a href="https://docs.docker.com/engine/swarm/"><strong>Docker swarm</strong></a> or <a href="http://mesos.apache.org/"><strong>MESOS</strong></a>. All of them are container orchestration and aim to make management of container much easier than before. How to scheduler your container to the specific node, how to specify resource usage for each container and so on. After that, you may start to focus on improve the performance of each container. How many RPS can be handled by each container? Are they still a room to improve? How to monitor or measure each container/service?</p><p>I am interesting in <a href="https://istio.io/"><strong>Istio</strong></a> since 0.2.0, now is 0.6.0 and will have a stable version on 0.7.0. The concept is simple, intercept requests, stamp a tag or modify routing path and so on for each request before they been sent into destination. Try to make management of your services much easier than before.</p><p>Currently, I am working for a startup company and in charge of improve backend architecture for a while and I have a really hard time to work on it. Because we only have limited resources for every single task. But, things are changed and now finally get a chance to really put my energy on it.</p><p>The legacy architecture of backend service is composed with AWS ALB/ELB, RDS and punch of EC2 instances, primitive but effective. But it also comes up with some problems. For instances, deployment, A/B testing, API migration, monitor performance of each API server, able to trace/inspect request header and so on. Those problems lead me to decide to migrate all services into container-based and use <a href="https://kubernetes.io/"><strong>Kubernetes</strong></a> to manage all containers.</p><p><a href="https://buoyant.io/2017/04/25/whats-a-service-mesh-and-why-do-i-need-one/">What&#39;s a service mesh? And why do I need one?</a></p><p>One day I saw the post about the concept of <strong>service mesh, </strong>it blows my mind and start to think about the future of our backend service. Everything can be breakdown into small pieces and assemble back together into original one and work exactly the same as original one and more transparency.</p><p>Base on my understand, the entire <a href="https://istio.io/"><strong>Istio</strong></a> is surrounded by <a href="https://www.envoyproxy.io/"><strong>envoy</strong></a> a service proxy, designed for cloud-native applications. I simply draw a diagram below to show how <a href="https://istio.io/"><strong>Istio</strong></a> handle a request. Basically, put a envoy proxy in front of every application to capture the traffic. There are many components in side the <a href="https://istio.io/"><strong>Istio</strong></a>, like Pilot, Mixer and Control Panel…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*yf5A3hzAl83I8c8i_80yMQ.png" /><figcaption>Traffic flow of Istio in Kubernetes</figcaption></figure><p>At the time of writing this article, I am also migrating our service into <a href="https://istio.io/"><strong>Istio</strong></a>-based architecture. Hopefully, I could share more about my experiences on <a href="https://istio.io/"><strong>Istio</strong></a> in the near future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad2f96fde024" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dart: hands-on experience (1)]]></title>
            <link>https://medium.com/@cy.chiang/dart-hands-on-experience-1-519b89bc3ba4?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/519b89bc3ba4</guid>
            <category><![CDATA[hands-on-learning]]></category>
            <category><![CDATA[dartlang]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Fri, 09 Mar 2018 10:04:38 GMT</pubDate>
            <atom:updated>2018-03-09T10:06:55.939Z</atom:updated>
            <content:encoded><![CDATA[<p>I have looked into a new programming language, <a href="https://www.dartlang.org/"><strong>Dart</strong></a>, for a while. But have no time to get my hands on it. Now, I am trying to build something fun and useful with <a href="https://www.dartlang.org/"><strong>Dart</strong></a>, <a href="https://flutter.io/"><strong>Flutter</strong></a> and <a href="https://webdev.dartlang.org/angular/"><strong>AngularDart</strong></a>. <a href="https://webdev.dartlang.org/angular/"><strong>AngularDart</strong></a> for website and <a href="https://flutter.io/"><strong>Flutter</strong></a> for mobile application.</p><p>Here is my idea: I am interesting in currency exchange rate and what to know the exchange rate from one currency to another with my smartphone. To do that, I need to know where I can access those data. <a href="https://openexchangerates.org/"><strong>Open Exchange Rates</strong></a> is my choice and it has easy-to-use API to fetch any data I need.</p><p><strong>First step:</strong> Create a wrapping library with <a href="https://www.dartlang.org/"><strong>Dart</strong></a> to access <a href="https://openexchangerates.org/"><strong>Open Exchange Rates</strong></a></p><p><strong>P.S.</strong> The source code is available on <a href="https://github.com/cychiang/open_exchange_rates"><strong>github</strong></a>. Any comments/suggestions are welcome.</p><p>Before start to code, I have to specify library/package information in pubspec.yaml.</p><p>I think the code below is minimum requirement to define a library/package. Declare the name, description, version, author and so on for the library/package you created.</p><p>Here is the <a href="https://www.dartlang.org/tools/pub/pubspec"><strong>link</strong></a> to the detail information about pubspec.yaml format.</p><pre><strong># pubspec.yaml</strong></pre><pre>name: open_exchange_rates<br>description: A dartlang wrapping for openexchangerates api requests.<br>version: 0.0.1<br>author: Casper CY Chiang &lt;cychiang0823@gmail.com&gt;<br>homepage: <a href="https://github.com/cychiang/open_exchange_rates">https://github.com/cychiang/open_exchange_rates</a></pre><pre>environment:<br>  sdk: &#39;&gt;=1.24.0 &lt;2.0.0&#39;<br>dependencies:<br>  http: &#39;^0.11.3+16&#39;</pre><p>An other reason to choose <a href="https://openexchangerates.org/"><strong>Open Exchange Rates</strong></a><strong> </strong>as the target to implement wrapping library, the API itself is easy to use and understand. Wrap it up is to practice my Dart programming skill.</p><p>To implement the wrapping library, I create a base class to fetch api key from environment by using only <strong>Platform</strong> from <strong>dart:io</strong>.</p><pre>import &#39;dart:io&#39; show Platform;</pre><pre>class OpenExchangeRatesBase {<br>  final String base_path = &#39;https://openexchangerates.org/api/&#39;;<br>  // init api_key from environment variable<br>  String api_key = null;<br>  OpenExchangeRatesBase() {<br>    this.api_key = Platform.environment[&#39;OER_API_KEY&#39;];<br>  }<br>}</pre><p>Next extends the base class and implement <strong>latest </strong>query from <a href="https://openexchangerates.org/"><strong>Open Exchange Rates</strong></a>. Source code is available in <a href="https://github.com/cychiang/open_exchange_rates"><strong>github</strong></a></p><pre>class OpenExchangeRates extends OpenExchangeRatesBase {<br>  String query = null;<br>  // query options<br>  String base = null;<br>  String symbols = null;<br>  bool show_alternative = null;<br>  bool prettyprint = null;<br>  // constructor<br>  OpenExchangeRates.initLatest({<br>    this.base, this.symbols, <br>    this.show_alternative, this.prettyprint}) {<br>    this.query = this.base_path + &#39;latest.json?app_id=${this.api_key}&#39;;<br>    // if options not null<br>    if (this.base != null) {<br>      this.query = this.query + &#39;&amp;base=${this.base}&#39;;<br>    }<br>    ...<br>  }<br>  // implement get request<br>  get() =&gt; http.get(this.query);<br>}</pre><p>After that, let’s implement our main function.</p><pre>main() async {<br>  // declare a class with initLatest constructor<br>  OpenExchangeRates queryLatest = new OpenExchangeRates.initLatest();<br>  // perform get();<br>  var response = queryLatest.get();<br>  // print result<br>  print(&#39;${response.body}&#39;);<br>}</pre><p>It comes up with some problem with my code. How to handle a user want to update the query string? I think there is a good pattern of it out there and wait me to explorer.</p><p>Write a code with Dart is kind of simple. Although I keep searching some information from the <a href="https://www.dartlang.org/"><strong>Dart</strong></a> documents. But, it’s fun!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=519b89bc3ba4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Painless with Kibana]]></title>
            <link>https://medium.com/@cy.chiang/using-painless-with-kibana-4b7342b610b0?source=rss-b9959e27e72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b7342b610b0</guid>
            <category><![CDATA[kibana]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[painless]]></category>
            <dc:creator><![CDATA[Chuan-Yen Chiang]]></dc:creator>
            <pubDate>Tue, 07 Mar 2017 04:47:39 GMT</pubDate>
            <atom:updated>2017-03-07T04:47:39.642Z</atom:updated>
            <content:encoded><![CDATA[<p>To be honest, it’s not actually “Painless” when using it with Kibana. The whole journey is full of “Pain” and fun.</p><p>Kibana can helps you to visualize your data in very short time and it’s super convenience. However, if you want to treat your data in different way, write a script is the only option.</p><p>You can write your script with Python or Javascript, but you need to install Plugin in your Kibana. You can find more information about supported script from <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html"><strong>here</strong></a>. In this case, I choose Painless to write the script even though I am familiar with Python.</p><p>There are two important elements need to know when using Kibana, <strong>metrics</strong> and <strong>buckets</strong>. All metrics are based on all records from specific index and type. In Kibana, it also call “doc”. Each “doc” has own attributes like “_index” and “_type” to declare the location of this record.</p><p>Back to the main story, the script is like the filter. Once you setup “Index Patterns” from management page. The Kibana will analyze all records under that patterns. So, the whole process of Kibana visualization is something like this: <strong>records/docs -&gt; metrics -&gt; value</strong>. You can use script to replace metrics and the process will becomes: <strong>records/docs -&gt; script -&gt; value</strong>.</p><p>Once we understood this principle of how Kibana process those data, it’s much easier to figure out “why my script is working” instead of “my script is working but I don’t know why”.</p><p>Now, let’s start to code. There are two place to put your script from Kibana. One is JSON input in “visualize” section and the other is “Scripted fields” in management -&gt; index patterns -&gt; Scripted fields tab</p><p>The example I shown in here is the example to deal with “Scripted fields”. You can find basic syntax in <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-painless-syntax.html"><strong>here</strong></a>. And <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/painless-api-reference.html"><strong>here</strong></a> is supported API. Now, I am going to use supported API to demonstrate check key is available or not.</p><pre># create dict for test<br>def key_map = [&#39;key1&#39;: &#39;this is key1&#39;, &#39;key2&#39;: &#39;this is key2&#39;]<br># use API &quot;containsKey&quot; that mentioned in Painless API Reference<br># now check the doc&#39;s type is in the key map or not<br>if (key_map.containsKey(doc[&#39;_type&#39;].value)) { <br>    # if doc[&#39;_type&#39;].value in key_map <br>    # Remember, the return type will affect behavior on &quot;bucket&quot;<br>    return True <br>} else { <br>    return False <br>}</pre><p>That’s it. But the timing of using script is depends on how you want to visualize your data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b7342b610b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>