<?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 Napas Vinitnantharat on Medium]]></title>
        <description><![CDATA[Stories by Napas Vinitnantharat on Medium]]></description>
        <link>https://medium.com/@napas.vin?source=rss-80bc867526bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vxev1vd1HPrnbyfz8JS2sQ.jpeg</url>
            <title>Stories by Napas Vinitnantharat on Medium</title>
            <link>https://medium.com/@napas.vin?source=rss-80bc867526bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:26:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@napas.vin/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[Manage Database Concurrency With Pessimistic and Optimistic Locking]]></title>
            <link>https://medium.com/ascend-developers/manage-database-concurrency-with-pessimistic-and-optimistic-locking-c5913b9b72a6?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5913b9b72a6</guid>
            <category><![CDATA[pessimistic-locking]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[optimistic-locking]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Mon, 11 May 2026 03:21:54 GMT</pubDate>
            <atom:updated>2026-05-11T03:21:54.616Z</atom:updated>
            <content:encoded><![CDATA[<p>In high-concurrency applications that interact with a database, it is essential to ensure that simultaneous transactions do not result in data loss or duplication. Proper concurrency control mechanisms must be implemented to maintain data consistency and integrity when multiple transactions attempt to update the same data concurrently. To ensure data consistency and integrity, an appropriate locking mechanism is required. This article explores the concepts of pessimistic locking and optimistic locking, how they work, how to use them, and also gives you some Java code to explore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*MqHbv6ZuCTgr_9Q9RYtnJA.png" /></figure><p>Consider the situation when transaction 1 (T1) is trying to update the account balance from the database, while transaction 2 (T2) is also trying to update the balance at the same time. Without a locking mechanism after T1 and T2 updates, the account balance might be incorrect. This is because both Transactions have queried the same account balance. So both transactions are updated based on the same initial balance, which will result in the incorrect update of the account balance</p><p>Let’s write some code to see the behavior of the account balance without locking</p><p>Account Entity</p><pre>@Entity<br>@Table(name = &quot;accounts&quot;)<br>@Getter<br>@Setter<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class Account {<br><br>  @Id<br>  @GeneratedValue(strategy = GenerationType.IDENTITY)<br>  private Long id;<br><br>  @Column(nullable = false)<br>  private BigDecimal balance;<br>  <br>}</pre><p>Service layer code implementation of no lock deductBalance</p><pre>@Transactional<br>public Account deductBalance(Long accountId, BigDecimal amount) {<br>  String ThreadName = Thread.currentThread().getName();<br><br>  log.info(&quot;Thread={} | Fetching account id ={}&quot;, ThreadName, accountId));<br>  Account account = accountRepository.findById(accountId)<br>                    .orElseThrow(() -&gt; new IllegalArguementException(<br>                            &quot;Acount not found: &quot; + accountId));<br><br>  log.info(&quot;Thread={} | Read Balance ={}&quot;, ThreadName, account.getBalance()));<br><br>  // Simulate processing delay - makes the race window wide enogh to observe<br>  simulateDelay(100);<br><br>  if (account.getBalance().compareTo(amount) &lt; 0) {<br>    throw new IllegalStateException(&quot;Insufficient balance: &quot;, account.getBalance());<br>  }<br><br>  BigDecimal newBalance = account.getBalance().subtract(amount);<br>  account.setBalance(newBalance)<br>  <br>  Account saved = accountRepository.save(account);<br><br>  log.info(&quot;Thread={} | Updated balance ={}&quot;, ThreadName, saved.getBalance()));<br><br>}<br><br>private void simulateDelay(long millis) {<br>  try {<br>    Thread.sleep(millis);<br>  } catch(InterruptedException e) {<br>    Thread.currentThread().interrupt();<br>  }<br>}</pre><p>To properly test for a race condition in this code, we need to simulate concurrent execution by using a thread pool and submitting multiple tasks that operate on shared resources simultaneously, with the following code</p><pre>private List&lt;String&gt; runConcurrently(int threadCount, Callable&lt;String&gt; task) {<br>  ExecutorService executor = Executors.newFixedThreadPool(threadCount);<br>  List&lt;future&lt;String&gt;&gt; futures = new ArrayList&lt;&gt;();<br><br>  for (int i = 0; i &lt; threadCount; i++) {<br>    futures.add(executor.submit(task));<br>  }<br>  <br>  executor.shutdown();<br>  <br>  List&lt;String&gt; results = new ArrayList&lt;&gt;();<br>  for (Future&lt;String&gt; f: futures) {<br>    try{<br>      results.add(f.get());<br>    } catch(Exception e) {<br>      results.add(&quot;Error: &quot; + e.getMessage());<br>    }<br>  }<br>  <br>  return results;<br>}</pre><p>Let’s run with 5 threads, trying to deduct balance from the account with ID equal to 1, and each thread deducts $100 from the account. With the initial account balance of $1,000</p><pre>List&lt;String&gt; results = runConcurrently(5, () -&gt; {<br>  Account a = noLockService.deductBalance(1, 100);<br>  return &quot;OK -&gt; balance=&quot; + a.getBalance();<br>})</pre><p>After running the following code, let&#39;s see the result</p><pre>{<br>  &quot;results&quot; : [<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;<br>  ]<br>}<br><br>// SELECT id, balance FROM accounts<br>-------------------<br>|  id  |  balance |<br>-------------------<br>|  1   |   900    |<br>-------------------</pre><p>Even though five threads attempted to deduct $100 simultaneously, the final persisted balance is $900 instead of the expected $500. This happens because each thread fetches the account state concurrently and observes the same balance value of $1000. Without any locking strategy, every thread computes the updated balance as $900 and writes it back to the database, causing later writes to overwrite earlier ones. This demonstrates the <strong>lost update problem</strong></p><h3>Pessimistic Locking</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*0eOY7XAwma8u4mYe9IKwIA.png" /><figcaption>Two Transaction try to update account balance with pessimistic lock mechanism</figcaption></figure><p>When using pessimistic locking, we prevent another transaction from updating data by locking the related rows or table. This operation will be done at the database level. So, in case another transaction tries to update the data, it must wait until the lock is released.</p><h4><strong>Pessimistic Locking Flow</strong></h4><ol><li>T1 select data from the database to update the new balance</li><li>T1 acquires the database lock to prevent other transactions from updating the account balance data</li><li>T2 selects data from the database to update the new balance</li><li>T1 commits the updated balance to the database and releases the lock</li><li>T2, continue to update the new balance after T1 releases the lock</li></ol><p>Let’s write some code to see the behavior of the account balance with pessimistic locking</p><p>Repository layer code implementation with pessimistic locking</p><pre>@Repository<br>public interface AccountRepository extends JpaRepository&lt;Account, Long&gt; {<br><br>  // Generates: SELECT ... FOR UPDATE <br>  // The DB places an exclusive row lock; all other writers <br>  // on the same row block<br>  @Lock(LockModeType.PESSIMISTIC_WRITE)<br>  @Query(&quot;SELECT a FROM Account a WHERE a.id = :id&quot;)<br>  Optional&lt;Account&gt; findByIdWithPessimisticLock(@Param(&quot;id&quot;) Long id);<br>}</pre><p>Service layer code implementation of no lock deductBalance</p><pre>@Transactional<br>public Account deductBalance(long accountId, BigDecimal amount) {<br>  log.info(&quot;Thread={} | Acquiring FOR UPDATE lock on account id={}&quot;,<br>    Thread.currentThread().getName(), accountId);<br><br>  // Acquire the Lock, Block another thread to execute the code<br>  Account account = accountRepository.findByIdWithPessimisticLock(accountId)<br>      .orElseThrow(() -&gt; <br>          new IllegalArguementException(&quot;Account not found: &quot;, accountId));<br>  <br>  log.info(&quot;Thread ={} | Lock acquired. balance={}&quot;,<br>        Thread.currentThread().getName(), account.getBalance());<br><br>  // Simulate processing delay - other threads are BLOCKED at the SELECT above<br>  simulateDelay(100);<br>  <br>  if (account.getBalance().compareTo(amount) &lt; 0) {<br>    throw new IllegalStateException(&quot;Insufficient balance: &quot;, account.getBalance());<br>  }<br><br>  BigDecimal newBalance = account.getBalance().subtract(amount);<br>  account.setBalance(newBalance)<br>  <br>  Account saved = accountRepository.save(account);<br><br>  log.info(&quot;Thread={} | Updated balance ={}&quot;, ThreadName, saved.getBalance()));<br>}<br><br>private void simulateDelay(long millis) {<br>  try {<br>    Thread.sleep(millis);<br>  } catch(InterruptedException e) {<br>    Thread.currentThread().interrupt();<br>  }<br>}<br></pre><p>Let’s run with 5 threads, trying to deduct balance from the account with ID equal to 1, and each thread deducts $100 from the account. With the initial account balance of $1,000</p><pre>List&lt;String&gt; results = runConcurrently(5, () -&gt; {<br>  Account a = pessimisticLockService.deductBalance(1, 100);<br>  return &quot;OK -&gt; balance=&quot; + a.getBalance();<br>})</pre><p>After running the following code, let’s see the result</p><pre>{<br>  &quot;results&quot; : [<br>    &quot;OK -&gt; balance=800.00&quot;,<br>    &quot;OK -&gt; balance=600.00&quot;,<br>    &quot;OK -&gt; balance=700.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=500.00&quot;<br>  ]<br>}<br>// SELECT id, balance FROM accounts<br>-------------------<br>|  id  |  balance |<br>-------------------<br>|  1   |   500    |<br>-------------------</pre><p>As you can see, we have an initial account balance of $1,000 with a pessimistic locking strategy, which allows only one thread to access the resources at a time; these threads are deducting the balance sequentially. So no race condition occurs in this case. The balance is deducted correctly with the final balance of 500$.</p><h4>Type of Locks</h4><ul><li><strong>Shared Lock</strong></li></ul><p>With a shared lock, we allow multiple transactions to read the same data at the same time, but do not allow transactions to update, modify, or delete it until the shared lock is released.</p><pre>BEGIN;<br>-- lock row update, delete operation <br>SELECT * FROM account WHERE account_id = 1 FOR SHARE;</pre><ul><li><strong>Exclusive lock</strong></li></ul><p>On the other hand, an exclusive lock prevents other transactions from reading or modifying the data; if another transaction tries to acquire a shared lock or exclusive lock, it will wait until the lock is released.</p><pre>BEGIN;<br><br>SELECT * FROM account WHERE id = 1 <br>FOR UPDATE;  -- locks row not allow read, update, delete<br><br>-- do business logic<br>UPDATE account <br>SET balance = balance - 100 <br>WHERE id = 1;<br><br>COMMIT;</pre><h4>When to use Pessimistic Locking</h4><ul><li>Critical financial operations</li><li>High chance of data conflict during update, write data to the database</li><li>Data consistency is more important than performance</li></ul><h3>Optimistic Locking</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*HuOuURBhAQe8Ex4fMBf3pg.png" /><figcaption>Two Transaction try to update account balance with optimistic lock mechanism — No retry</figcaption></figure><p>Optimistic locking uses a technique that allows multiple transactions to read and modify the same data concurrently. Instead of locking transactions via the database, the Optimistic Locking approach detects conflicts at commit time by checking a <strong>version column</strong>. If the version has changed since it was read, the update will affect zero rows, indicating a conflict.</p><pre>UPDATE account <br>SET balance = balance - 70, version = version + 1 <br>WHERE id = 1 AND version = 1</pre><h4>Optimistic Locking Flow</h4><ol><li>T1 selects data from the database with version V1</li><li>T2 also selects data from that database with version V1</li><li>T1 updates the new account balance and updates the account version from V1 to V2</li><li>T2 updated the new account balance with version V1 and got no row affected because T1 has updated the version V1 to V2</li></ol><p>Let’s write some code to see the behavior of the account balance with optimistic locking</p><p>Repository Layer Code</p><pre>@Entity<br>@Table(name = &quot;accounts&quot;)<br>@Getter<br>@Setter<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class Account {<br><br>  @Id<br>  @GeneratedValue(strategy = GenerationType.IDENTITY)<br>  private Long id;<br><br>  @Column(nullable = false)<br>  private BigDecimal balance;<br><br>  /**<br>   * Used by JPA optimistic lockig.<br>   * Hibernate reads this value before UPDATE and adds a<br>   * {@code WHERE version = ?} clause - causing a <br>   * {@link jararta.persistence.OptimisticLockException} when two<br>   * concurrent transactions try update the same row.<br>   */<br>  @Version<br>  @Column(nullable = false)<br>  private Long version;<br>  <br>}</pre><p>Service Layer Code</p><pre><br>@Transactional<br>public Account deductBalance(Long accountId, BigDecimal amount) {<br>  String ThreadName = Thread.currentThread().getName();<br><br>  log.info(&quot;Thread={} | Fetching account id ={}&quot;, ThreadName, accountId));<br>  Account account = accountRepository.findById(accountId)<br>                    .orElseThrow(() -&gt; new IllegalArguementException(<br>                            &quot;Acount not found: &quot; + accountId));<br><br>  log.info(&quot;Thread={} | Read Balance ={}&quot;, ThreadName, account.getBalance()));<br><br>  // Simulate processing delay - makes the race window wide enogh to observe<br>  simulateDelay(100);<br><br>  if (account.getBalance().compareTo(amount) &lt; 0) {<br>    throw new IllegalStateException(&quot;Insufficient balance: &quot;, account.getBalance());<br>  }<br><br>  BigDecimal newBalance = account.getBalance().subtract(amount);<br>  account.setBalance(newBalance)<br>  <br>  Account saved = accountRepository.save(account);<br><br>  log.info(&quot;Thread={} | Updated balance ={}&quot;, ThreadName, saved.getBalance()));<br><br>}<br><br>private void simulateDelay(long millis) {<br>  try {<br>    Thread.sleep(millis);<br>  } catch(InterruptedException e) {<br>    Thread.currentThread().interrupt();<br>  }<br>}</pre><p>Adding the version annotation to the account entity will generate a where clause `<strong>WHERE version = ?` </strong>So when two concurrent transactions try to update the same row, it will cause an <strong>OptimisticLockException</strong>.</p><p>Let’s run with 5 threads, trying to deduct balance from the account with ID equal to 1, and each thread deducts $100 from the account. With the initial account balance of $1,000</p><pre>List&lt;String&gt; results = runConcurrently(5, () -&gt; {<br>  Account a = optimisticLockService.deductBalance(1, 100);<br>  return &quot;OK -&gt; balance=&quot; + a.getBalance();<br>})</pre><p>After running the following code, let’s see the result</p><pre><br>{<br>  &quot;results&quot;: [<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;ERROR: org.springframework.orm.ObjectOptimisticLockingFailureException: Unexpected row count&quot;,<br>    &quot;ERROR: org.springframework.orm.ObjectOptimisticLockingFailureException: Unexpected row count&quot;,<br>    &quot;ERROR: org.springframework.orm.ObjectOptimisticLockingFailureException: Unexpected row count&quot;,<br>    &quot;ERROR: org.springframework.orm.ObjectOptimisticLockingFailureException: Unexpected row count&quot;<br>  ]<br>}<br><br>// SELECT id, balance, version FROM accounts<br>-----------------------------<br>|  id  |  balance | version |<br>-----------------------------<br>|  1   |   900    |    2    |<br>-----------------------------</pre><p>The result shows that with an optimistic locking mechanism, only one thread can update the account balance. While the other 4 threads are failing to update the account balance.</p><h4><strong>Retry Strategy with Optimistic Locking</strong></h4><p>With Optimistic Locking, the conflict is checked at commit time, causing the concurrent transaction to fail. In a real-life scenario, we might not want the transaction to fail during a concurrent update. So, to solve this problem, we need a retry strategy to retry updating the transaction, as shown in the following figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ufikmrbK62IkwxTwzLFsZQ.png" /><figcaption>Two Transaction try to update account balance with optimistic lock mechanism — with retry</figcaption></figure><p>Instead of letting T2 fail when T1 has already updated the account, we add <strong>Step 5: Retry the account balance update multiple times</strong> to ensure the transaction completes.</p><p>In a high-contention scenario, multiple transactions attempt to update the data concurrently, which can lead to many failed updates and retries. This can cause a database performance problem. So, Using Optimistic locking might not be a good idea in a high contention environment</p><pre>private static final int MAX_RETRIES = 5;<br><br>public Account deductBalance(Long accountId, BigDecimal amount) {<br>  int attempt = 0;<br>  <br>  while (attempt &lt; MAX_RETRIES) {<br>    attempt++;<br>    log.info(&quot;Thread={} | Attempt={} for account id ={}&quot;, <br>        Thread.currentThread().getName(), attempt, accountId);<br>    try {<br>      return tryDeductBalance(accountId, amount);<br>    } catch(ObjectOptimisticLockingFailureException ex) {<br>      log.warning(&quot;Thread={} | Version conflict on attempt {}. Retrying...&quot;,<br>        Thread.currentThread().getName(), attempt)<br>      if (attempt &gt;= MAX_RETRIES) {<br>        throw new IllegalStateException(&quot;Could not deduct account balance for&quot; <br>          + MAX_RETRIES + &quot;attempts due to concurrent update&quot;)<br>      }<br>    }<br>  // unreachable, but satisfies the complier<br>  throw new IllegalStateException(&quot;Unexpected state in deducting balance&quot;);<br>  } <br>}<br><br>@Transactional<br>public Account tryDeductBalance(Long accountId, BigDecimal amount) {<br>  String ThreadName = Thread.currentThread().getName();<br><br>  log.info(&quot;Thread={} | Fetching account id ={}&quot;, ThreadName, accountId));<br>  Account account = accountRepository.findById(accountId)<br>                    .orElseThrow(() -&gt; new IllegalArguementException(<br>                            &quot;Acount not found: &quot; + accountId));<br><br>  log.info(&quot;Thread={} | Read Balance ={}&quot;, ThreadName, account.getBalance()));<br><br>  // Simulate processing delay - makes the race window wide enogh to observe<br>  simulateDelay(100);<br><br>  if (account.getBalance().compareTo(amount) &lt; 0) {<br>    throw new IllegalStateException(&quot;Insufficient balance: &quot;, account.getBalance());<br>  }<br><br>  BigDecimal newBalance = account.getBalance().subtract(amount);<br>  account.setBalance(newBalance)<br>  <br>  Account saved = accountRepository.save(account);<br><br>  log.info(&quot;Thread={} | Updated balance ={}&quot;, ThreadName, saved.getBalance()));<br><br>}<br><br>private void simulateDelay(long millis) {<br>  try {<br>    Thread.sleep(millis);<br>  } catch(InterruptedException e) {<br>    Thread.currentThread().interrupt();<br>  }<br>}</pre><p>Let’s try to run it again with a retry strategy, so we will test with MAX_RETRIES equal to 5, and try to run 5 concurrent threads to see the result</p><pre>{<br>  &quot;results&quot;: [<br>    &quot;OK -&gt; balance=700.00&quot;,<br>    &quot;OK -&gt; balance=900.00&quot;,<br>    &quot;OK -&gt; balance=500.00&quot;,<br>    &quot;OK -&gt; balance=800.00&quot;,<br>    &quot;OK -&gt; balance=600.00&quot;,<br>  ]<br>}<br><br>// SELECT id, balance, version FROM accounts<br>-----------------------------<br>|  id  |  balance | version |<br>-----------------------------<br>|  1   |   500    |    6   |<br>-----------------------------</pre><h4>When to use Optimistic Locking</h4><ul><li>Low chance of data conflict during the update of data</li><li>The application has more read operations than write operations</li><li>Read latency is important</li><li>Transactions take a long time to complete, and holding locks during the entire process isn’t feasible.</li></ul><h4><strong>Reference</strong></h4><ul><li><a href="https://dev.to/jszutkowski/pessimistic-vs-optimistic-locking-in-mysql-3hgc">https://dev.to/jszutkowski/pessimistic-vs-optimistic-locking-in-mysql-3hgc</a></li><li><a href="https://newsletter.systemdesigncodex.com/p/pessimistic-vs-optimistic-locking">https://newsletter.systemdesigncodex.com/p/pessimistic-vs-optimistic-locking</a></li><li><a href="https://www.abstractalgorithms.dev/types-of-locks-explained-tips-for-maintaining-consistent-systems-and-avoiding-write-conflicts">https://www.abstractalgorithms.dev/types-of-locks-explained-tips-for-maintaining-consistent-systems-and-avoiding-write-conflicts</a></li><li><a href="https://raigardastautkus.medium.com/optimistic-vs-pessimistic-locking-basics-of-concurrency-in-databases-2f3adb290b2f">https://raigardastautkus.medium.com/optimistic-vs-pessimistic-locking-basics-of-concurrency-in-databases-2f3adb290b2f</a></li><li><a href="https://www.youtube.com/watch?v=lR6y-0RC5cc&amp;t=2075s">https://www.youtube.com/watch?v=lR6y-0RC5cc</a></li><li><a href="https://medium.com/@felipe.ascari_49171/concurrent-transactions-in-go-pessimistic-vs-optimistic-locks-part-1-2-33f866561feb">https://medium.com/@felipe.ascari_49171/concurrent-transactions-in-go-pessimistic-vs-optimistic-locks-part-1-2-33f866561feb</a></li><li><a href="https://medium.com/engineering-housing/optimistic-locking-overview-6b30315e759b">https://medium.com/engineering-housing/optimistic-locking-overview-6b30315e759b</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5913b9b72a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ascend-developers/manage-database-concurrency-with-pessimistic-and-optimistic-locking-c5913b9b72a6">Manage Database Concurrency With Pessimistic and Optimistic Locking</a> was originally published in <a href="https://medium.com/ascend-developers">Ascend Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Terminal tools for boosting productivity]]></title>
            <link>https://medium.com/ascend-developers/terminal-tools-for-boosting-productivity-894bd272fccd?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/894bd272fccd</guid>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[terminal-commands]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Mon, 02 Mar 2026 04:42:05 GMT</pubDate>
            <atom:updated>2026-03-02T04:42:05.135Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-b9k4lYuZJLY0Xy-Ole-IQ.png" /></figure><p>As a software developer, we use the terminal a lot of the time, whether it is about running a backend service on a local machine, connecting to a remote server, or even finding the right file in some directory. So having a good terminal setup helps improve a lot of productivity. This article will walk through the tools and recommend setups that will help your software development productivity</p><h3><strong>Select the terminal you like</strong></h3><p>Nowadays, there are a lot of terminals tool in the market such as <a href="https://terminaltrove.com/terminals/iterm2/">iTerm2</a>, <a href="https://terminaltrove.com/terminals/alacritty/">Alacritty</a>, <a href="https://terminaltrove.com/terminals/wezterm/">Wezterm</a>, <a href="https://www.warp.dev/">Warp</a>, etc. So the question is “ Which terminal should we use?” Based on my experience, I found that you have to try it to find what terminal is suitable for you. If you like a terminal that enriches AI agents&#39; tools, you might try <strong>Warp</strong>, But If you focus on simplicity and speed, you might want to try <strong>Alacritty</strong>. I recommend not using the default terminal app that comes with macOS because it offers little functionality.</p><h3>Decorate your terminal</h3><p>Decorating your terminal is not just about having a fancy terminal look, but need to give us information about the context of the directory you are working in. For example, when you are developing software, you want to know what git branch you are working with. So it will be great if you have a terminal plugin that tells you what branch you are on. There are a lot of ways to decorate your terminal; you can use tools like Oh my zsh, Oh my posh, and Starship.</p><h3>Add an alias to your terminal</h3><p>Sometimes when you has long command to type on the terminal, and you have to use it a lot of the time, it might be great if you have a way to make a shortcut of this long command using an alias</p><p>To add an alias to the terminal, open the file .bashrc ( if you use zsh, open.zshrc file instead) add alias by adding the following command</p><pre>alias la=&#39;ls -a&#39;</pre><p>By adding the following command, this creates a shortcut <strong>la, </strong>for the shell command ls -a, which lists all files in the directory</p><p>To list alias that you have created, use the command alias</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7w1SpVmQO1ezqehmQrIGAg.png" /></figure><h3><strong>Recommend CLI tools</strong></h3><h4><strong>zsh-autosuggestion</strong></h4><p><a href="https://github.com/zsh-users/zsh-autosuggestions">zsh autosuggestion</a> is one of the CLI tools I really like. This plugin auto- suggestion command you type in gray text, and if you press → on the keyboard, it means you accept the suggestion. This improves the speed a lot when you have to type a long command. By using <strong>zsh-autosuggestion,</strong> you just type a few letters on the terminal, then press →</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*xGpA3qFXxAC6GZulQCHMyg.png" /></figure><p>To install zsh-autosuggestion in your terminal, you can use Homebrew with the following command,</p><pre>brew install zsh-autosuggestions</pre><p>then activate autosuggestion by adding the following at the end of the file <strong>.zshrc</strong></p><pre>source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh</pre><h4>fd</h4><p><a href="https://github.com/sharkdp/fd">fd</a> is a program used for finding file, directory in your filesystem. It is similar to the command <a href="https://linux.die.net/man/1/find">find</a>, but much faster and simpler than the <a href="https://linux.die.net/man/1/find">find</a> command that comes up by default on Mac. In case you want to find a file in your system with fd, you can use the following command</p><pre>fd &quot;auth&quot; -t f # finding a file that contain auth </pre><p>compared to the find command</p><pre>find . -type f -name &quot;*auth*&quot;</pre><p>If you are using the find command in a large project, this might take 2 minutes to list all of the files, while using fd might take less than a second</p><h4><strong>fzf</strong></h4><p><a href="https://github.com/junegunn/fzf">fzf (fuzzy finder)</a> is a command line tool for filtering search list such as finding files in a directory, looking for git commits, and finding history commands that you been using. I use fzf for finding files and finding history commands. We can install Fzf using Homebrew with the following command</p><pre>brew install fzf</pre><p>To find a directory, I create a custom command, so when I use this command, it will change the directory automatically after completing the find from fzf. To achieve this functionality, we add the following command in file <strong>.zshrc</strong></p><pre>cdf() {<br>  local dir<br>  dir=$(fd --type d . | fzf) &amp;&amp; cd &quot;$dir&quot;<br>}</pre><h4>ripgrep</h4><p><a href="https://github.com/BurntSushi/ripgrep">ripgrep</a> is a tool used for searching text similar to grep, but the difference is that it recursively searches in the current directory by default. To install ripgrep, use the following command</p><pre>brew install ripgrep</pre><p>One of my use cases is to identify the configuration in the project. For Example If you want to find which microservice is using this API. You can use it ripgrep with the following command.</p><pre>rg --hidden /api/v1/user  # finding word /api/v1/user include hidden file</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*SnJHi_loG443T3vufH4XRA.png" /><figcaption>example of using riggrep (rg command) for search /api/v1/user</figcaption></figure><p>After running the ripgrep command, It show that 3 places contain the word /api/v1/user</p><h4>jq</h4><p><a href="https://github.com/jqlang/jq">jq</a> is a tool used for processing JSON in the terminal. One of the common usecase to use jq is to prettify the JSON payload. For example,</p><pre>echo &#39;{&quot;fruit&quot;:{&quot;name&quot;:&quot;apple&quot;,&quot;color&quot;:&quot;green&quot;,&quot;price&quot;:1.20}}&#39; | jq &#39;.&#39;</pre><p>with this command, the output will be the prettification of the JSON payload</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*sJ_GsyC2ne34FcnmmuVDyw.png" /></figure><h4><strong>bat</strong></h4><p><a href="https://github.com/sharkdp/bat">bat</a> is a command-line tool that is enhanced from the cat command. It adds feature syntax highlight to the cat command. Install bat using the following command</p><pre>brew install bat</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/0*1MOXMLNWmN7FQ5z8" /><figcaption>Syntax highlight from using bat reference: <a href="https://github.com/sharkdp/bat">https://github.com/sharkdp/bat</a></figcaption></figure><h4>lsd</h4><p><a href="https://github.com/lsd-rs/lsd">lsd</a> is similar to the lscommand, but lsd with more colorful and file, directory icons for a better experience. To Install lsd using the following command</p><pre>brew install lsd</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*10-iY9AMV7MDMvXOBrRpLg.png" /><figcaption>reference: <a href="https://github.com/lsd-rs/lsd?tab=readme-ov-file">https://github.com/lsd-rs/lsd?tab=readme-ov-file</a></figcaption></figure><p>When installing lsd, I typically configure the following alias to facilitate more convenient usage.</p><pre>alias ls=&#39;lsd&#39; # use ls command as lsd <br><br>alias l=&#39;ls -l&#39;<br>alias la=&#39;ls -a&#39;<br>alias lla=&#39;ls -la&#39;<br>alias lt=&#39;ls --tree&#39;</pre><h4><strong>tmux</strong></h4><p>tmux is a program that runs on a terminal. It allow user to create multiple terminal session with in single window. This tool is very handy when we open a lot of terminals. For example In the software development process, you might have one terminal for running Docker, one terminal for running remote to the server, and one terminal for running the application. This might be chaos when we have a lot of terminals opening. So using tmux, we can combine those terminals in a single window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*66mF3jfdXpavbve0dY33fQ.png" /><figcaption>reference: <a href="https://www.tecmint.com/tmux-to-access-multiple-linux-terminals-inside-a-single-console/">https://www.tecmint.com/tmux-to-access-multiple-linux-terminals-inside-a-single-console/</a></figcaption></figure><p>To install tmux, we can use the following command</p><pre>brew install tmux</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=894bd272fccd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ascend-developers/terminal-tools-for-boosting-productivity-894bd272fccd">Terminal tools for boosting productivity</a> was originally published in <a href="https://medium.com/ascend-developers">Ascend Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[แนะนำ Neovim Plugin ที่ผมใช้]]></title>
            <link>https://medium.com/@napas.vin/%E0%B9%81%E0%B8%99%E0%B8%B0%E0%B8%99%E0%B8%B3-neovim-plugin-%E0%B8%97%E0%B8%B5%E0%B9%88%E0%B8%9C%E0%B8%A1%E0%B9%83%E0%B8%8A%E0%B9%89-c24a6040cd5f?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/c24a6040cd5f</guid>
            <category><![CDATA[nvim]]></category>
            <category><![CDATA[vim]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Sat, 16 Nov 2024 06:25:17 GMT</pubDate>
            <atom:updated>2024-11-16T06:25:17.773Z</atom:updated>
            <content:encoded><![CDATA[<p>ส่วนตัวเลยผมเป็นคนที่ใช้ vscode เป็นเครื่องมือหลักในการทำงาน เขียนโค๊ด ถ้าเขียน program ภาษา python, javascript หรือ golang ผมมักจะเลือก vscode เป็นหลัก แต่ถ้าเป็น java ผมมักจะเลือก Intellij เป็นหลัก แต่ไม่นานมากนี่ ผมจะมักจะมีปัญหาเรื่อง คอมพิวเตอร์ของผมที่ช้าและเก่า มีหลายๆครั้งที่ผมเขียน code อยู่ดีๆ ต้อง restart คอมพิวเตอร์ ทั้งๆที่ผมเปิดแค่ vscode กับ google ไม่กี่ tab เรื่องนี้ทำให้ผมเลือกที่จะหา IDE ตัวใหม่มาใช้ในการทำงานเป็นหลัก ผมเลยได้ลอง IDE ตัวใหม่ ชื่อ neovim ซึ่ง Neovim นั้นก็จะมี Plugin หลากหลายให้เราได้ custom วันนี้ผมจะมาเล่า plugin ที่ผมใช้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aPQ3ypHWT8aEWg4oogPp6g.png" /></figure><h4><strong>flash.nvim</strong></h4><p><a href="https://github.com/folke/flash.nvim">flash.nvim</a> เป็น neovim plugin ตัวหนึ่งที่ส่วนตัวผมให้ในการ เปลี่ยนตำแหน่งของ cursor plugin ตัวนี้ทำให้เราไม่ต้อง เลื่อน ขึ้นลงด้วย <strong>key hjkl</strong> หรือ<strong> key w b </strong>บ่อยๆ เป็น plugin ที่ทำให้เราสามารถ กระโดดcursor ไปที่ๆเราต้องการได้เร็ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HX_bdwe-377tz8MA" /><figcaption><a href="https://github.com/folke/flash.nvim">https://github.com/folke/flash.nvim</a></figcaption></figure><h4><strong>telescope.nvim</strong></h4><p><a href="https://github.com/nvim-telescope/telescope.nvim">telescope.nvim</a> เป็น plugin อันหนึ่งที่ส่วนตัวผมใช้อยู่บ่อยๆ telescope เป็นเรื่องมือที่ใช้ในการหาไฟล์(find files) หรือ หาคำใน directory (live-grep) Telescope ยังมี integrate กับ Plugin <a href="https://github.com/folke/todo-comments.nvim">todo-comments.nvim</a> ทำให้เราหา Todo comment ที่เราทำไว้ด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FtWilOASJCF_kkj6" /><figcaption><a href="https://github.com/nvim-telescope/telescope.nvim?tab=readme-ov-file">https://github.com/nvim-telescope/telescope.nvim?tab=readme-ov-file</a></figcaption></figure><h4><strong>surround.nvim</strong></h4><p>surround.nvim เป็น Plugin อันหนึ่งที่ใช้ในการ เพิ่ม ลด surrounding ของ คำหรือ ประโยค เช่น ผมมี code ดังนี้</p><pre>const data = {&quot;name&quot;: &quot;Napas&quot;}</pre><p>แล้วผมต้องการ เปลี่ยน จาก <strong>“name”</strong> เป็น <strong>name </strong>ที่ไม่ “ ผมสามารถใช้คำส่ง ds” (ย่อมาจาก delete surround “)</p><h4><strong>vim-visual-multi</strong></h4><p>ผมเริ่มมาสนใจ <a href="https://github.com/mg979/vim-visual-multi?tab=readme-ov-file">vim-visual-multi</a> เพราะว่าปัญหาที่ว่า visual-block ไม่เพียงพอต่อการใช้งาน vim-visual-multi ช่วยให้เราสามารถเพิ่ม cursor และใช้งาน normal mode หลาย cursor พร้อมๆกันได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/0*90PViBtGGQXYy9C4" /><figcaption><a href="https://github.com/mg979/vim-visual-multi?tab=readme-ov-file">https://github.com/mg979/vim-visual-multi?tab=readme-ov-file</a></figcaption></figure><h4><strong>toggleterm.nvim</strong></h4><p>ในการเปิด terminal ใน neovim ผมจะใช้ plugin <a href="https://github.com/akinsho/toggleterm.nvim">toggleterm</a> ซึ่งตัว toggleterm มีให้เราสามารถ config ได้ว่า จะเปิด terminal ในรูปแบบไหน ไม่ว่าจะเป็น terminal ลอยขึ้นมา (float), แบบ vertical, horzional หรือ เป็น tab ใหม่เลยก็ได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v73vU5GkqwDTkTpT.gif" /><figcaption><a href="https://github.com/akinsho/toggleterm.nvim">https://github.com/akinsho/toggleterm.nvim</a></figcaption></figure><h4><strong>project.nvim</strong></h4><p>ในการทำงานที่เกี่ยวข้องกับ microservice หลายๆ ครั้งจำเป็นต้องเราจำเป็นที่จะต้องเปลี่ยน repo ที่เราเขียน code ไปมาหรือสับเปลี่ยน project ไปมา <a href="https://github.com/ahmedkhalf/project.nvim">project.nvim</a> ตัวนี้จะตอบโจทย์มากๆครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ufOqKPBAj0DoPU7R.png" /><figcaption><a href="https://github.com/ahmedkhalf/project.nvim">https://github.com/ahmedkhalf/project.nvim</a></figcaption></figure><h3><strong>Git Workflow in Neovim</strong></h3><h4><strong>lazygit.nvim</strong></h4><p><a href="https://github.com/kdheepak/lazygit.nvim">lazygit.nvim</a> เป็น plugin ในการจัดการ git ต่างๆ ไม่ว่าจะ ทำ add, commit, push, pull, merge หรือ ทำ interactive rebase หรือ ทำ cherry-pick commit ก็ทำได้ ข้อดีของ <a href="https://github.com/jesseduffield/lazygit">lazygit</a> คือเป็น terminal GUI support ทุกระบบ OS ไม่ว่าจะใช้ Mac, Window หรือ Linux ก็ใช้งานได้ ที่สำคัญคือใช้งานง่าย ไม่ซับซ้อน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xty257gE0poYSAXj.gif" /><figcaption><a href="https://github.com/kdheepak/lazygit.nvim">https://github.com/kdheepak/lazygit.nvim</a></figcaption></figure><h4><strong>diffview.nvim</strong></h4><p>Plugin นี้ ส่วนตัวผมใช้ในการ ดู change ที่เกิดขึ้นในแต่ละ commit ซึ่ง feature ที่ผมใช้อยู่บ่อยๆคือ การ Diffview History File คือการดู change ที่เกิดขึ้นในแต่ละ file เป็นอย่างไรบ้างและใครเป็นคนแก้ เพิ่มหรือลบ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*73DkXrltTAaWLC0X.png" /><figcaption><a href="https://github.com/sindrets/diffview.nvim">https://github.com/sindrets/diffview.nvim</a></figcaption></figure><h4><strong>gitsign.nvim</strong></h4><p>plugin นี้หลักๆ ผมจะใช้ในการดู line blame ว่าใครในทีมเป็นคนเขียน บรรทัดนี้ และ ผมก็ใช้ gitsign ในการดูว่าเรามีการ เพิ่ม code ของเราไว้ตรงไหนบ้าง gitsign ยังมีประโยชน์ในการจัดการ hunk ไม่ว่าจะเป็นการ preview hunk, การ reset hunk และอื่นๆ อีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/0*OvWqxpJyjqcyer5g.gif" /><figcaption>credit: <a href="https://github.com/lewis6991/gitsigns.nvim?tab=readme-ov-file">https://github.com/lewis6991/gitsigns.nvim?tab=readme-ov-file</a></figcaption></figure><h4><strong>gitlab.nvim</strong></h4><p>เป็น plugin ที่ผมใช้ในการทำการสร้าง Merge Request เป็นหลักเลยครับ plugin นี้ แต่ก่อนเวลาไม่ตอนที่ผมไม่ใช้ plugin นี้ ผมต้องไปเปิด gitlab ใน web แล้วหาว่า repo ที่ผมจะทำการสร้าง Merge Request อยู่ที่ไหน และต้อง click อีก หลายๆ ปุ้ม กว่าจะทำการ สร้าง Merge Request ได้ แต่พอมาใช้ Plugin ตัวนี้ ทำให้ผมสามารถเปิด Merge Request ใน neovim โดยไม่ต้องออกจาก terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SRBjp-bbDYti_uJ2" /><figcaption><a href="https://github.com/harrisoncramer/gitlab.nvim?tab=readme-ov-file">https://github.com/harrisoncramer/gitlab.nvim?tab=readme-ov-file</a></figcaption></figure><p>gitlab.nvim ยังมี feature อื่นๆ ไม่ว่าจะเป็น การ View Merge Request, การ Approve Merge Request, view Pipeline Status และอื่นๆ อีกมากมาย</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c24a6040cd5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ฝึกงาน ที่ ExxonMobil IT ทำอะไรบ้าง ?]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%9D%E0%B8%B6%E0%B8%81%E0%B8%87%E0%B8%B2%E0%B8%99-%E0%B8%97%E0%B8%B5%E0%B9%88-exxonmobil-it-%E0%B8%97%E0%B8%B3%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3%E0%B8%9A%E0%B9%89%E0%B8%B2%E0%B8%87-72b93afe4d9c?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/72b93afe4d9c</guid>
            <category><![CDATA[internships]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 23:36:33 GMT</pubDate>
            <atom:updated>2024-09-24T23:36:33.927Z</atom:updated>
            <content:encoded><![CDATA[<h3>ฝึกงาน ที่ ExxonMobil IT ทำอะไรบ้าง ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W08yWE3xNTdBrDEfsVwTew.png" /><figcaption>Intern Life @ EMIT</figcaption></figure><p>สวัสดีครับ ผมชื่อฟางครับ เป็นนักศึกษาชั้นปี 4 คณะวิศวกรรมคอมพิวเตอร์ มหาวิทยาลัยเทคโนโลยีพระจอมเกล้าธนบุรี วันนี้ก็จะมาเล่าประสบการณ์การฝึกงานสหกิจศึกษาเป็นระยะเวลา 10 เดือนที่ ExxonMobil IT หรือเรียกสั้นๆว่า EMIT ในตำแหน่ง Software Developer ว่าวันๆหนึ่งพวกเราทำอะไรบ้าง</p><h4><strong>EMIT คืออะไร?</strong></h4><p>ก่อนจะไปเล่าประสบการณ์การฝึกงาน ผมขอมาเล่าก่อนว่า <strong>EMIT คืออะไร?</strong> เชื่อว่าหลายๆคนที่เข้ามาอ่านอาจจะยังไม่รู้จักหรือเคยได้ยิน EMIT มาก่อน</p><p><strong>EMIT</strong> ย่อมาจาก <strong>ExxonMobil IT </strong>ซึ่งเป็นหน่วยงาน IT ของบริษัท ExxonMobil งานที่พวกเราทำหลักๆคือ การทำ Application เพื่อ support แผนกต่างๆ ใน บริษัทน้ำมัน ไม่ว่าจะเป็นแผนก Sale, Marketing, Finance และอื่นๆ อีกมากมาย</p><h4>One Day Life @ EMIT</h4><p>การทำงานที่นี้รูปแบบการทำงานแบบ Agile ใช้เฟรมเวิร์ค <a href="https://medium.com/fastwork-engineering/scrum-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3-%E0%B9%80%E0%B8%A3%E0%B8%B4%E0%B9%88%E0%B8%A1%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B9%84%E0%B8%A3-2483e761a47e">Scrum</a> ใน 1 sprint จะใช้เวลา 2 อาทิตย์ เริ่ม sprint เราก็จะทำ sprint planning คือ การ plan ว่าใน sprint นี้เราจะทำอะไรบ้างแล้วใครจะรับผิดชอบ card ไหน แล้วแต่ละงานต้องใช้กี่ point ซึ่งในตอนจบ sprint ทีมของเราก็กิจกรรม sprint retrospective คือการ review งานที่ทำไปใน sprint นี้มีอะไรที่จบได้ดีบ้าง (What went well), มีอะไรที่เราต้องปรับปรุง(What to improve) และมี action item อะไรบ้างที่ต้องทำเพื่อไม่ให้เกิดปัญหาขึ้นอีกในอนาคต</p><p>ทีมของผมจะเริ่มงานประมาณ 10 โมงเช้า เริ่มด้วยการทำ stand up meeting ซึ่งทุกคนในทีมก็จะเข้ามาอัปเดทงานของตัวเองที่ทำเมื่อวานว่าเป็นอย่างไรบ้างแล้วมีปัญหาติดอะไรไหม ถ้ามีปัญหาติดอะไรเราก็สามารถใช้เวลาตรงนี้คุยกับทีมเพื่อหาทางออกร่วมกัน หลังจาก stand up meeting ทุกคนก็จะแยกย้ายกันไปทำงานของตัวเอง เวลาประมาณ 11.30 โมง ผมก็ไปกินข้าวกับทีม ใกล้ออฟฟิศบริษัทมีของกินมากมาย หลังจากทำกินข้าวเสร็จ ผมก็จะกลับมาทำงานในช่วงบ่าย พอถึง 16.00 โมง ทีมผมเราก็จะมีการทำ Code Review ซึ่งสมาชิกในทีมก็จะมารวมกันออกความเห็น เกี่ยวกับ code ที่เราได้เขียนไปว่ามีตรงไหนที่ควรจะต้องทำการ improve เพื่อให้ code ที่เราเขียนอ่านง่ายๆ และมีประสิทธิภาพก่อนที่จะ release ลง production</p><h4><strong>My Project Intern</strong></h4><p>ตลอดการฝึกงาน 10 เดือน ผมได้ทำหลาย project แต่ที่จะเล่าหลักๆ จะมีสองโปรเจ็ค</p><ol><li><strong>DataDog Dashboard and Alert System</strong></li></ol><p>โปรเจ็คแรกของผมเป็นเกี่ยวกับการทำ dashboard ในการ monitor การทำงานของ application ต่างๆ ว่า application ต่างๆที่ทีมรับผิดชอบอยู่ยังทำงานได้ปกติดีหรือไม่ ถ้ามีบ้างอย่างที่ผิดปกติเกิดขึ้นมาใน application เรามีการทำระบบแจ้งเตือนเพื่อให้ ทีมก็สามารถแก้ปัญหาได้อย่างทันท่วงที ด้วยความที่ application มีขนาดที่ใหญ่มากๆ หลายๆ ครั้ง การทำงานของ application ก็ทำงานไม่เป็นไปอย่างที่เราคาด ซึ่งการที่เราไม่มีเครื่องมือที่ใช้ในการ monitor application ของเราที่ดี ก็อาจจะทำให้เรา investigate issue ได้ยาก หรือใช้เวลานานในการหาที่มาของปัญหาที่เกิดจากที่ไหน เครื่องมือหลักๆ ที่ผมใช้ในการ monitoring ก็คือ Datadog</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kGpNhe7qeSPMt5hQ.png" /><figcaption>Reference: <a href="https://www.devopsschool.com/blog/top-50-datadog-interview-questions-and-answers/">https://www.devopsschool.com/blog/top-50-datadog-interview-questions-and-answers/</a></figcaption></figure><p><strong>2. Auto Pod Scaling using KEDA</strong></p><p>โปรเจ็คสอง จะเป็นเรื่องของการทำ scaling application ซึ่งปัญหาคือว่ามี service ตัวหนึ่งที่ทีมดูแลหนึ่งซึ่ง consume data จาก Kafka Topic ช้า ทำให้ข้อมูลติดอยู่ใน Kafka Topic นาน ที่นี้ทีมก็อยากที่ทำให้ service พวกนี้ consume data จาก Kafka Topic เร็วขึ้น ผมก็ศึกษาไปศึกษามาก็เจอเทคโนโลยีตัวหนึ่งชื่อ <strong>Kubernete Event Driven Autoscaling </strong>หรือ <a href="https://keda.sh/"><strong>KEDA</strong></a> ซึ่งหลักการทำงานของมันคือ KEDA จะทำการ check ว่ามี data ที่ติดอยู่ใน Kafka Topic มากน้อยแค่ไหน ถ้ามีมาก KEDA ก็จะทำการ scale application ขึ้นมาเพื่อช่วยในการ consume data จาก Kafka Topic แล้วพอ consume data ใน Kafka Topic หมดแล้ว KEDA ก็จะทำการ scale application ลงเพื่อทำให้เราใช้ทรัพยากรที่มีอยู่อย่างคุ้มค่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/0*KOoD5Gx6qwP_LBxa" /><figcaption>Reference:<a href="https://piotrminkowski.com/2022/01/18/autoscaling-on-kubernetes-with-keda-and-kafka/">https://piotrminkowski.com/2022/01/18/autoscaling-on-kubernetes-with-keda-and-kafka/</a></figcaption></figure><h4>Tech Stack and Tool</h4><p>ความรู้และประสบการณ์ที่ผมได้รับจากที่นี้ ส่วนมากจะเป็นเรื่องของการทำ software ที่มีขนาดใหญ่มากๆ และ DevOps practice ต่างๆ เพราะตอนที่ทำ project ที่มหาวิทยาลัย เราก็ไม่ได้ทำ software ที่มีขนาดใหญ่มาก ส่วนมากทำ monolith application ขนาดเล็ก เน้นว่ามีส่งอาจารย์ แอปทำงานได้ก็ถือว่าผ่านแล้ว แต่พอมาฝึกงานที่บริษัท ExxonMobil ผมก็ได้ลองเล่น Tool ต่างๆ มากมาย ซึ่ง Tool บางอย่างก็ไม่มีให้ได้ลองใน มหาวิทยาลัย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hHN_iUMfWcZLqEgQwCeVzw.png" /><figcaption>Tech Stack</figcaption></figure><ul><li><a href="https://www.redhat.com/en/technologies/cloud-computing/openshift/container-platform"><strong>Openshift Container Platform</strong></a><strong> </strong>เป็น platform application container ซึ่งถูกพัฒนาโดย Red Hat (เป็น tool ที่ถูกพัฒนามาจาก Kubernetes)</li><li><a href="https://micrometer.io/"><strong>Micrometer</strong></a><strong> </strong>เป็นเครื่องมือหนึ่งที่เราใช้ในการทำ custom metric</li><li><a href="https://azure.microsoft.com/en-us"><strong>Microsoft Azure</strong></a><strong> </strong>เป็น Cloud<strong> </strong>Computing Platform</li><li><a href="https://k6.io/"><strong>Grafana K6</strong></a><strong> </strong>เป็นเครื่องมือที่ใช้ในทำ load testing เพื่อ test ว่า feature ที่เรา ทำเพิ่มไปสามารถ รองรับ load ได้หรือไหม เมื่อมีการใช้งานเป็น</li><li><a href="https://helm.sh/"><strong>Helm</strong></a><strong> </strong>เป็น package management Kubernetes ใช้ในการจัดการตัวแปรผ่านไฟล์ Kubernetes manifest YAML</li><li><a href="https://www.datadoghq.com/"><strong>Datadog</strong></a><strong> </strong>เป็นเครื่องมือที่ใช้ในการทำ dashboard เพื่อ monitor application ต่างๆ และทำ alert system เพื่อแจ้งเตือน</li><li><a href="https://kafka.apache.org/"><strong>Kafka</strong></a><strong> </strong>เป็นเครื่องมือที่เป็นตัวกลางในการรับส่งข้อมูล ระหว่างผู้ส่งข้อมูล (Producer) ไปยังผู้รับ (Consumer) ในทำงานเหมือนระบบต่อ Queue ของข้อมูล</li></ul><h4>Intern Activity @ EMIT</h4><p>นอกจากเรื่องงานแล้ว ที่ EMIT เรายังมีกิจกรรมมากมายที่ให้โอกาส intern ตัวน้อยๆ อย่างเราได้ร่วม</p><ul><li><strong>EMIT Sports Day</strong></li></ul><p>EMIT Sports Day จัดขึ้นที่ Beat active BITEC BangNa เป็นกิจกรรมกีฬาสีของชาว ExxonMobil IT จัดเมื่อปลายปี 2023</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*86KOJemKTry2UFPHdnIPOw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3FEI_oyPSdPktR8-u_Dk4g.jpeg" /></figure><ul><li><strong>EMIT Intern Get Together</strong></li></ul><p>EMIT Intern Get Together เป็นกิจกรรมที่ให้โอกาส Intern จากทีมต่างๆ ได้เข้ามารวมทำความรู้จักกันผ่านการทำกิจกรรมรวมกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wo5DVwm8ebijA3GOjwMBxA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NlT3kemTPdNHip4GUbW9Ig.jpeg" /></figure><ul><li><strong>Team Building</strong></li></ul><p><strong>Team Building</strong> เป็นกิจกรรมสังสรรค์ภายในทีม ปกติจะจัด 3 เดือนครั้งครับ กิจกรรมก็มีหลากหลาย ไม่ว่าจะเล่นแบตมินตัน, board game ไปกินอาหารบุฟเฟ่ต์และอื่นๆอีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sErpxLA8pCkW2Fl8FJRqmw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ivvVS0b91SWTCyqgnZuLQ.jpeg" /></figure><h4>บรรยากาศการทำงาน</h4><ul><li><strong>Ergonomic Chair</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AgvTLvDm9zBs8WqrhMCbHg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ML253aYVuaKmZtgkpdhGNA.jpeg" /></figure><ul><li><strong>Xommon</strong></li></ul><p>ที่ ExxonMobil เราก็จะมีห้อง Xommon เป็นห้องที่ให้พนักงานพักผ่อนจากการทำงานในเวลาว่าง ไม่ว่าจะเป็น โต๊ะพูล, โต๊ะปิงปอง, Board game, Nintendo Switch ในช่วงเวลากลางวันห้องนี้ก็จะมีคนใช้เป็นเต็มเลยครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eK9HaNHMv7kgvwovVvlv4A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nqSF0bkVVNR6Jcip11NclQ.jpeg" /><figcaption>Xommon</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72b93afe4d9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[รีวิว Incubator program ที่ Arise by INFINITAS 2024]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%A3%E0%B8%B5%E0%B8%A7%E0%B8%B4%E0%B8%A7-incubator-program-%E0%B8%97%E0%B8%B5%E0%B9%88-arise-by-infinitas-2024-524b8920a4cb?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/524b8920a4cb</guid>
            <category><![CDATA[arise-by-infinitas]]></category>
            <category><![CDATA[incubator-program]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Tue, 30 Jul 2024 17:39:45 GMT</pubDate>
            <atom:updated>2024-09-14T08:26:25.646Z</atom:updated>
            <content:encoded><![CDATA[<p>สวัสดีครับ ผมชื่อฟางครับ เป็น Backend Engineer ที่ Arise by INFINITAS ผมวันนี้จะมารีวิวประสบการณ์ การเข้าร่วม Incubator Program ของบริษัท Arise by INFINITAS ฉบับจบใหม่</p><p>จุดเริ่มต้นของการเข้ามาเป็น Backend Engineer ที่ Arise by INFINITAS เกิดจากการที่ผมได้ไปเห็น Post in Linkedin ว่าจะมีกิจกรรม <strong><em>คุย สอบ สัม</em></strong> ซึ่งหลังหลังจากที่ผมส่งเอกสารไป ก็มีพี่จาก Team People Experience(PX) โทรมาบอกว่าให้ไปสอบ coding และสัมภาษณ์ที่ The Parq ชั้น 5 ซึ่งเป็น Office ของ Arise ในงาน <strong><em>คุย สอบ สัม</em></strong> ก็จะมีพี่ๆมาบรรยายบริษัทให้ฟังว่า บริษัททำอะไร มีที่ไปที่มาเป็นอย่างไร หลังจากบรรยายและแนะนำบริษัทไปแล้วก็เริ่ม สอบ coding เป็น โจทย์ Algorithm ไม่ยากมาก basic ถ้าเขียน code อยู่เป็นประจำก็น่าจะพอจะทำได้ครับ ซึ่งถ้าสอบผ่านก็จะสัมต่อในรอบบ่ายเลย ซึ่งถ้าผ่านสัมภาษณ์ ก็จะมีพี่จากทีม PX ติดต่อมาให้ทำสัญญาจ้างงานครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P7e_eb1DrBL8TmQytoZnzg.jpeg" /></figure><h3>บริษัท <strong>Arise by INFINITAS ทำอะไร ?</strong></h3><p><a href="https://www.arise.tech/"><strong>Arise by INFINITAS</strong></a> เป็นบริษัทร่วมทุนระหว่างบริษัท <strong>INFINITAS by Krungthai</strong> กับ บริษัท <strong>Accenture</strong> งานหลักๆคือการสร้างเทคโนโลยีและนวัตกรรมทางการเงินต่างๆให้กับประเทศไทย ไม่จะเป็น App เช่น <strong>เป๋าตัง, Krungthai NEXT, ถุงเงิน </strong>และอื่นๆ อีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xNZm56urBRlQws9ak8Mcbw.png" /></figure><h3><strong>Orientation</strong></h3><p>ในวันแรกที่มา Office บริษัทก็จะมี Orientation แนะนำเกี่ยวกับ องค์กรว่าที่มาที่ไปของบริษัทเป็นอย่างไร บริษัท Arise และ Infinitas ทำ product อะไรอยู่บ้าง แล้วมี checklistอะไรบ้างที่จะต้องทำในช่วง probation รวมไปถึงการ Setup Notebook ในการทำงาน ซึ่งที่นี่เราจะใช้ Mac Book M3 ในการทำงาน</p><p>หลังจากจบ Orientation ในช่วงเช้า ในรอบบ่าย เรามีพี่ๆจาก Team Aster มาอธิบายการใช้งาน Web Aster ซึ่งเป็น Web การประชาสัมพันธ์เกี่ยวกับกิจกรรมภายในของชาว Ariser ซึ่งเวลาเราไปทำกิจกรรมต่างๆในบริษัทเราก็จะได้ Arise Token เอาไปแลกเป็นของรางวัลต่างๆ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aGlqL3DW4OdTvl5N1CWo5g.png" /></figure><h3>Agile Section</h3><p>Agile Section เป็น Section ที่จัดหลังจาก orientation ซึ่งใช้เวลาทั้งหมดสองวัน ซึ่งจัดให้กับ New Join Ariser ทุกคน ซึ่งใน section นี้จะมีพี่ๆ Agile coach มาสอน Agile Fundamental ว่า Agile คืออะไร แล้ว เราสามารถทำอย่างไร ให้ team ของเราสามารถส่งงานได้อย่างรวดเร็วและมีคุณภาพ นอกจากเรื่อง Agile Fundamental แล้ว section นี่ก็จะมีสอนการใช้งาน Jira และ Conflunce ซึ่งเป็น tool หลักที่ใช้ในบริษัท</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ugktC3ofCacQf0ktdu6yA.jpeg" /></figure><h3><strong>What is Incubator Program ?</strong></h3><p>Incubator Program เป็นโปรแกรมที่จัดให้กับ เด็กจบใหม่หรืออาจยังไม่มีประสบการณ์มาก มาเรียนปรับพื้นฐาน ก่อนที่จะเริ่มเข้าไปทำงาน โดยโปรแกรมนี้จะใช้เวลาทั้งหมด 1 เดือน ซึ่งจะมีพี่ๆ Technical Coach เก่งๆ ไม่ว่าจะเป็นพี่ยอด (Pallat Anchaleechamaikorn) พี่หน่อง(Anuchit Prasertsang) พี่บอนด์(Surasuk Oakkharaamonphong) มาสอน skill ที่จำเป็นก่อนลงทำงานจริงๆ</p><h4>week 1</h4><p>เริ่มต้น Incubator วันแรก เราเรียนเรื่อง Git and Version Control System ซึ่งใน class นี่ จะเป็นการสอน Deep ลงไปให้เข้าใจหลักการทำงานของ Git ว่า Git มีการจัดการข้อมูลยังไงแล้ว เวลาที่เรา add หรือ commit code ที่เราเขียนไป Git มีการจัดการในการ track change ที่เปลี่ยนไปอย่างไรบ้าง</p><p>วันต่อมา เริ่มเรียน Software Pricinple ต่างๆ ไม่ว่าจะเป็น SDLC (Software Development Life Cycle), KISS principle (Keep it simple and stupid) , YAGNI(You Ain’t Gonna Need It) หลังจากเรียน principle แล้ว เรามาเรียนการเขียน Basic Program และ Practice ในภาษาต่างๆ ไม่ว่าจะเป็น Go, JavaScript, Kotlin, Java และ ภาษา haskell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*d0TSirfq1ZV78-qW.png" /><figcaption>Credit: monkeyuser.com</figcaption></figure><h4>week 2</h4><p>สัปดาห์สองนี่เป็นสัปดาห์ที่เรามาเรียนเรื่อง การทำ API โดยใช้ Golang Gin Framework การทำ Gracefully Shutdown สัปดาห์นี่ยังเป็น สัปดาห์ที่เราเริ่มเรียนเรื่องการทำให้ Software ของเรามี Quality เช่นการทำ Test-Driven Deployment (TDD) ซึ่งคือการให้ Test ที่เราเขียนเป็นตัวที่ driven ให้เกิด code นอกจากนี้ยังมีเรียนเรื่องการทำ Software Testing ในรูปแบบต่างๆ ไม่ว่าจะเป็น การทำ Unit Test, API Testing, E2E(end-to-end) Testing รวมไปถึง Five test of doubles ไม่ว่าจะเป็น Dummy, Fake, Stub, Spy และ Mock</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3YO23ca2x6erlWE1Cp7KEg.jpeg" /><figcaption>Credit: monkeyuser.com</figcaption></figure><p>ซึ่งในสัปดาห์ เรามี workshop ให้ลองทำ API ขึ้นมาตาม Requirement ที่พี่ให้มา แล้วทำ Unit testing เพื่อ Test ว่าการทำงานของ Code เราเป็นไปตามที่เรา expected ไหม รวมไปถึงการทำ API Testing โดยใช้ Playwright เพื่อเช็คว่า API ที่เราสร้าง เป็นไปตาม requirement ที่เราได้มาหรือไหม</p><h4><strong>week 3</strong></h4><p>สัปดาห์ที่สาม เริ่มเรื่อง Redis ซึ่งเป็น Cache ที่นิยมตัวหนึ่ง ที่ใช้ในการเพิ่มสิทธิภาพการทำงานของแอป ซึ่งตัว Redis นี้ทำให้แอปลดการเอาข้อมูล(Query) จาก Database (เอาจาก Cache แทน) หลังจากเรียนเรื่อง Redis เสร็จก็ได้มาเรียนเรื่องการใช้งาน Kafka ซึ่งเป็น Message Queue รูปแบบหนึ่งที่ใช้ในบริษัท ซึ่งส่วนตัวเคยมีประสบการณ์กับ Kafka มาจากที่ฝึกงานที่เก่ามาแต่เป็น Kafka เขียนกับ Java Spring Boot แต่พอมาเรียนเขียน Kafka โดย Golang กับพี่ยอด ก็ได้รู้ว่าปัญหามันเยอะกว่า เขียน Java เยอะเลย เพราะ Kafka สร้างมาจาก Java มันเลยคุยกันง่ายแต่ Go นั้นต้อง handle หลายอย่างเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*qAZVoUxovH34WkDdLbu01w.png" /></figure><p>ซึ่งหลังจากเรื่อง Kafka เสร็จ พี่ๆก็มี Lab Kafka ใช้ลองไปทำซึ่งใน Lab ก็จะใช้ Library ชื่อ <a href="https://github.com/IBM/sarama">Sarama</a> รูปด้านบนก็ไปตัวอย่าง Lab ที่เราได้ลองทำ API ที่เกี่ยวกับ Skill ซึ่งถ้าเป็น method GET ให้ไปเอาจาก Database เลยแต่ถ้า Create / Update / Delete ให้ส่งไปที่ Kafka แล้วให้ skill-consumer (ในรูป) ไปทำการ Update Database ให้กับเรา</p><h4>week 4</h4><p>ขึ้น สัปดาห์ที่ 4 ผมได้เริ่มทำการ containize ตัว app ที่เราได้ทำไปในสัปดาห์ที่สาม รวมไปถึงการทำ Continuous Integration โดยใช้ GitLab ซึ่งพอเราทำการ Push โค๊ดของเราไป Gitlab CI ก็จะทำการ run ตัว unit test และ build image ไปเก็บไว้ที่ Gitlab Container Registry</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/0*WG65Zx6wSBw1pVtS" /><figcaption>reference: <a href="https://flexagon.com/blog/why-the-world-needs-ci-cd/">https://flexagon.com/blog/why-the-world-needs-ci-cd/</a></figcaption></figure><h4><strong>last week</strong></h4><p>สัปดาห์สุดท้ายนี่เป็นสัปดาห์ พี่หน่องมา review code ที่ได้ทำไปใน week ที่ 3 กับ weekที่ 4 เพื่อที่จะได้เรียนรู้ว่าตรงไหนต้องแก้ไข ตรงไหนไม่ควรทำ รวมไปถึง best practice ในการเขียนของภาษา Go เป็นต้น ในสัปดาห์นี้ ผมยังได้เรียนรู้การทำ Load Testing โดยใช้ K6 Grafana ซึ่งเป็น Tool ที่ใช้ในการ Test ว่าตัว Application ที่เราสร้างสามารถรับ load ที่มี traffic ได้มากน้อย ซึ่งพี่บอนด์ได้สอน การทำ k6 การ Save ข้อมูลจากการทำ load test ไว้ที่ database InfluxDB และนำผลที่ได้ไปแสดงที่ Grafana Dashboard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j-NXYLJu6FY_Rwvq.png" /><figcaption>Reference:<a href="https://medium.com/@dhanukadulanjana184/performance-load-testing-with-k6-influxdb-grafana-on-windows-d0d138bbf326">https://medium.com/@dhanukadulanjana184/performance-load-testing-with-k6-influxdb-grafana-on-windows-d0d138bbf326</a></figcaption></figure><h3><strong>บรรยากาศการทำงาน Arise by INFINITAS</strong></h3><h3><strong>Coffee 101</strong></h3><p>ใน Incubator Program ทุกคนก็จะได้เข้าฟังบรรยายกับพี่บอล เรื่อง Coffee 101 ซึ่งพี่เค้าเล่าทุกอย่างที่เกี่ยวกับกาแฟ ตั้งแต่ เมล็ดพันธุ์และการเพาะปลูก การเลือกเม็ดกาแฟที่เราเลือกมาทำการดิป นอกจากนี้ยังสอนการทำกาแฟดิปด้วย ที่ตรงนี้ใครจะทำกินก็ได้ฟรี</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PHJxcjEFuUUqzkMZUEtP0w.jpeg" /></figure><h3>Tabble Tennis</h3><p>ในบริษัท Arise กับ Infinitas ตอนนี้มีจัดการแข่งขันกีฬาสีภายใน รูปด้านล่างเป็นการคัดนักกีฬาปิงปองของสี โต๊ะปิงปองจะอยู่ที่ Office ของ Infinitas ซึ่งอยู่ตึก The Parq East wing ชั้น 17 ปัจจุบันโต๊ะปิงปองยังไม่มีที่ Office ของ Arise ครับแต่พนักงานของ Arise ก็สามารถไปที่ Office ของ Infinitas ได้ และพนักงานของ Infinitas ก็สามารถมาที่ Office ของ Arise ได้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AcPmPDoD0jPlnyP91ERFMA.jpeg" /></figure><h3><strong>กิจกรรม Arise Connext</strong></h3><p>ในช่วงที่ผมอยู่ Incubator Program ก็มีกิจกรรม Arise Connext ครั้งที่ 12 ในธีม One Spirit — Sports Day 2024 ซึ่งจัดในวันที่ 26 กรกฎาคม 2567 ซึ่งวัตถุประสงค์ของการจัดกิจกรรม Arise Connext คือให้คนจากทีมต่างๆ ได้มาพูดคุย พบปะกัน</p><p>ในกิจกรรม Arise Connext ครั้งที่ 12 นี่ก็จะมีกิจกรรมย่อยๆ อยู่หลายกิจกรรม ไม่ว่าจะเป็น กิจกรรม เล่น Nintendo Switch Sport (Tennis), กิจกรรม Board Game, กิจกรรมกินกาแฟ by พี่ บอนด์ และอื่นๆอีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q066OEhrZECi0T3sNmMWDg.jpeg" /><figcaption>Arise Connext Coffee Processing Unit by พี่บอนด์</figcaption></figure><p>และแน่นอนว่าซึ่งที่ขาดไม่ได้ใน Arise Context คือ อาหารที่ทีม Connext ได้จัดให้ในรอบนี้อาหารที่กินกันคือ Pizza กับ ไก่ทอด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cc5h0jfcEf0XpCyY35BGKQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=524b8920a4cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาทำความรู้จักกับ Kubernetes Event Driven Autoscaling (KEDA)]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%81%E0%B8%B1%E0%B8%9A-kuberenetes-event-driven-autoscaling-keda-fc3c2b52a4b1?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc3c2b52a4b1</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Fri, 01 Mar 2024 15:07:51 GMT</pubDate>
            <atom:updated>2024-05-14T06:38:56.142Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*uPYFyOep3o_6o_15" /><figcaption>Kuberenetes Event Driven Autoscaling (KEDA)</figcaption></figure><p>ในโลกของ Kuberenetes การทำ scaling workload เป็นสิ่งสำคัญ ซึ่ง Kubernetes นั้นได้สร้าง feature built-in ขึ้นมาชื่อ <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/">Kubernetes HPA</a> (Horizontal Pod Autoscaler) ซึ่งสามารถทำให้เรา auto scale pod base on การใช้งานของ CPU และ Memory แต่หลายครั้งๆ เราไม่ได้อยากจะใช้ Kuberenetes scaling pod เพราะ การใช้งาน ของ CPU สูงขึ้น ตัวอย่างเช่น สมมุติเรามี application ง่ายๆ อันหนึ่งที่ consume จาก kafka topic หนึ่ง ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XCVKu-NU0yH1gPmPPr63Iw.png" /><figcaption>Application consume message from Kafka topic</figcaption></figure><p>แล้วเราต้องการที่จะทำการ scaling application ของเราขึ้นเพื่อให้ช่วยกัน consumer message จาก kafka topic ให้เร็วขึ้นในตอนที่มี message ที่ยังเหลืออยู่ใน Kafka topic และ scaling application ของเราลงตอนที่ไม่มี message เหลืออยู่ใน Kafka topic คำถามคือเราจะทำยังไง ? เห็นได้ว่าใน case นี้เราต้องการ scale application จาก event ที่ว่า มี message ที่ยังไม่ถูก consume (Kafka lag) มากน้อยแก้ไหน ซึ่งการใช้ Kubernetes HPA อาจจะไม่ได้ตอบโจทย์เรา เพราะ เราไม่ได้อยากที่จะ scale application จากการใช้งานของ CPU หรือ memory ครับ หรืออีกตัวอย่างหนึ่งที่น่าจะเห็นได้ชัดๆ เช่น สมมุติถ้าเราต้องการ scale application ของเราในช่วงเวลา 11.00–13.00 ของทุกๆวันเพราะเรารู้ว่าในช่วงเวลานั้นมีคนใช้งานตัว application มากกว่าช่วงปกติ เราจะเห็นได้ว่า Kubernete HPA ไม่ได้มีความสามารถตรงนี้ ซึ่งหนึ่งในวิธีการแก้ปัญหาคือ Kubernetes Events Driven Autoscaling (KEDA)</p><p><strong>KEDA คืออะไร ?</strong></p><p>Kubernetes Events Driven Autoscaling (KEDA) เป็น open-source project ถูกนำมาใช้ในการทำ autoscaling Kubernetes workload base on event ที่ต่างๆเกิดขึ้นเช่น kafka, จำนวน http traffic ที่เข้ามาที่ application เป็นต้น ซึ่ง KEDA ทำหน้าที่เหมือนกับ metrics server ในการมอนิเตอร์ metric จากแหล่งต่างๆ แล้วทำการส่งข้อมูลให้กับ Horizontal Pod Autoscaler (HPA) ในการ scale workload ตามที่เราต้องการ โดยตัว KEDA นั้น support จากหลากหลาย metric resource ไม่ว่าจะเป็น metric จาก rabbitmq, kafka, prometheus metric, azure service bus และอื่นๆอีกมากมาย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yqPVUZr4_jpux62L.png" /><figcaption>Kubernetes Event-driven Autoscaling(KEDA) <a href="https://keda.sh/blog/2021-04-06-why-alibaba-cloud-uses-keda-for-app-autoscaling/">https://keda.sh/blog/2021-04-06-why-alibaba-cloud-uses-keda-for-app-autoscaling/</a></figcaption></figure><h3>KEDA Architecture</h3><p>KEDA นั้นมีสิ่งที่เรียกว่า<strong> ScaledObject </strong>ซึ่งเป็น Custom Resource ของ KEDA ใช้ในการในอธิบาย scale ของ application เราว่าจะ scale ยังไง ตัวอย่างเช่น ถ้าเรามี application ที่ consume message จาก topic เป็นมากกว่า 10,000 ให้ทำการ scale up application เป็น 3 และ ถ้าไม่มี message เหลืออยู่ใน topic ให้ทำการ scale down application เหลือ 1 เป็นต้น พอเราทำการ apply ตัว <strong>scaledobject</strong> ลงไปใน cluster KEDA จะทำการเช็ค metrics จาก source ต่างๆ ที่เราเขียนอธิบายไว้ใน <strong>scaledobject </strong>ว่า event ที่ต้องทำในการ scale up/down workload หรือไหม ถ้ามี event KEDA จะส่งข้อมูลไปให้ HPA ทำการ scale pod</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Pmq64SDLsHXGFodo.png" /></figure><blockquote><strong>มาลอง scale application โดย KEDA กัน</strong></blockquote><p>ในการ scale application ในครั้งผมจะ scale application อย่างง่ายๆ โดยใช้ Deployment จาก ตัวอย่าง <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">Document Deployment ของ Kuberenete</a> และทำการ scale application โดย trigger ด้วย event ช่วงเวลา (cron)</p><h4>Tool</h4><p>เครื่องมือที่ใช้ในการทดลอง scale application มีดังนี้</p><ul><li><a href="https://minikube.sigs.k8s.io/docs/start/"><strong>Minikube</strong></a> — เป็นเครื่องมือในการจำลอง Kuberenete Cluster ในเครื่อง local</li><li><a href="https://kubernetes.io/docs/tasks/tools/#kubectl"><strong>Kubectl</strong></a><strong> </strong>— เป็น command line interface (CLI) ใช้ในส่ง command คุยกับ kuberenete master</li><li><a href="https://helm.sh/"><strong>Helm</strong></a> (optional) — เป็น package manager ของ kubernetes</li></ul><ol><li><strong>Install KEDA</strong></li></ol><p>ในการลง KEDA ใน cluster ทำได้หลายวิธี เช่น ลง KEDA โดยใช้ helm หรือ kubectl ก็ได้ ผมแนะนำว่าถ้าจะลง KEDA ทดลองในเครื่องคอมพิวเตอร์ ผมแนะนำใช้ install <a href="https://minikube.sigs.k8s.io/docs/start/">minikuke</a> ในการสร้าง cluster มาทดลองแล้วอาจจะใช้ kubectl ในการลง KEDA ก็ได้ครับ</p><p><strong>Install KEDA โดย Helm</strong></p><ol><li>Add Helm repo</li></ol><pre>helm repo add kedacore https://kedacore.github.io/charts</pre><p>2. Update Helm repo</p><pre>helm repo update</pre><p>3. Install Keda โดย helm</p><pre>helm install keda kedacore/keda --namespace keda --create-namespace</pre><p><strong>Install โดยใช้ </strong><a href="https://keda.sh/docs/2.13/deploy/#yaml"><strong>Kubectl yaml file</strong></a></p><p>ในการลง KEDA โดยใช้ kubectl ทำได้สองวิธี คือ มี admission webhooks กับแบบที่ไม่มี admission webhooks ซึ่งใน Documention ของ KEDA แนะนำให้ลงแบบที่มี admission webhook ซึ่งตัว admission webhook นั้นจะเป็นตัวที่ใช้ในการเช็คว่า resource ถูกต้องไหม ไม่มีการ configure อะไรผิดไป</p><pre># Including admission webhooks<br>kubectl apply --server-side -f https://github.com/kedacore/keda/releases/download/v2.13.0/keda-2.13.0.yaml<br># Without admission webhooks<br>kubectl apply --server-side -f https://github.com/kedacore/keda/releases/download/v2.13.0/keda-2.13.0-core.yaml</pre><p>ในการ check ว่า เราได้ install KEDA ลงใน cluster สำเร็จแล้วเราใช้ command ดังนี้</p><pre>kubectl get all -n keda</pre><p>ผลลัพธ์ที่ได้จะเป็นแบบนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*vmqZ1Ckj5jqS7ybWkYMU7Q.png" /></figure><p>สังเกตได้ว่าเราจะมี pod อันที่ run อยู่ใน cluster คือ <strong>keda-metric-api-server </strong>กับ<strong> keda-operator </strong>ตัว <strong>keda-metric-api-server</strong> นั้นจะทำเป็นเหมือน metric-server ของ Kuberenete ทำหน้าที่ในการเช็คดู metrics จาก event source ต่างๆ แล้วส่งค่าไปให้กับ Horizontal Pod Autoscaler ในการทำ scale workload ส่วนตัว <strong>keda-operator </strong>ทำหน้าที่เป็น agent การกำหนดว่าสถานะของ Kuberenete Deployment ว่าจะ activate หรือ deactive ในการทำ scale ซึ่งขึ้นกับ event ที่เรา config ไว้</p><p><strong>2. Deploy application</strong></p><p>ในการทดลอง scale application ในครั้งนี้ ผมจะใช้ image nginx ในการ deploy โดย เราเริ่มจากการสร้าง Deployment โดยการเขียน <strong>deployment.yaml</strong> ดังโค๊ดด้านล่าง (code นี้จาก <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">Document Deployment Kubernete</a>)</p><pre># deployment.yaml<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx-deployment<br>  labels:<br>    app: nginx<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      containers:<br>      - name: nginx<br>        image: nginx:1.14.2<br>        ports:<br>        - containerPort: 80</pre><p>สร้าง Deployment โดยใช้คำสั่งดังนี้</p><pre>kubectl apply -f deployment.yaml</pre><p>เช็คว่า Deployment ของเราสำเร็จไหมโดยใช้คำสั่งดังนี้</p><pre>kubectl get deployments</pre><p>หลังจาก run คำสั่งนี้ ก็จะได้หน้าตาประมาณนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*OUx-wAN7Q_McJCL_nkHqrA.png" /></figure><p>ลองมาเช็คดูว่า replicaset กับ pod กันครับว่า ยังอยู่ดีไหมโดยคำสั่งนี้</p><pre>kubectl get pod,rs</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*w3V9UFeZW6Pspwr6FydJkQ.png" /></figure><p><strong>3. สร้าง scaledobject กัน</strong></p><pre># scaledobject.yaml<br>apiVersion: keda.sh/v1alpha1<br>kind: ScaledObject<br>metadata:<br>  name: cron-scaledobject<br>spec:<br>  scaleTargetRef:<br>    apiVersion: apps/v1 <br>    kind: Deployment<br>    name: nginx-deployment<br>  minReplicaCount:  1<br>  maxReplicaCount:  3 <br>  triggers:<br>  - type: cron<br>    metadata:<br>      timezone: Asia/Bangkok  <br>      start: 30 * * * *       <br>      end: 45 * * * *         <br>      desiredReplicas: &quot;3&quot;</pre><p>อันนี้คือ scaledobject ซึ่งเป็นส่วนที่ใช้ในอธิบายการ scale Deployment ที่ชื่อ “nginx-deployment” ซึ่งในที่นี้เรา set minReplicaCount ที่ 1 หมายความว่า pod จะ run ต่ำสุดที่ 1 และ maxReplicaCount ที่ 3 หมายความว่า pod เราสามารถ scale ได้สูงสุดที่ 3 ในส่วนอยู่ใน triggers จะเป็นส่วนที่ใช้ในการบอกว่า เราจะ scale pod เมื่อมีเหตุการณ์อะไร ซึ่งในตัวอย่างนี้ เราใช้ trigger ประเภท cron ซึ่งเราทำการ scale pod ขึ้นเป็น 3 pod ทุกๆ นาทีที่ 30 ของทุกๆ ชั่วโมงและทำการ scale pod ลงเหลือ 1 pod ทุกนาทีที่ 45 ของทุกๆชั่วโมง</p><p>apply scaledobject ด้วยคำสั่ง</p><pre>kubectl apply -f scaledobject.yaml</pre><p>เช็ค สถานะของ scaledobject กัน</p><pre>kubectl get scaledobjects</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B9MmkYpvnuu_kVyaZ5f2oA.png" /></figure><p><strong>4. Test การ scale pod กัน</strong></p><p>ในการ Test การ scale pod ครั้งนี้ สิ่งที่ผมต้องทำคือการรอให้มีการ trigger เกิดขึ้น สิ่งที่เราจะเห็นคือ มี pod run เพิ่มขึ้นจาก 1 เป็น 3 pod ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*rq_ZfLD8wT72GwTMjUDpyw.png" /><figcaption>Deployment บอกว่า มี 3 pod run</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*kZqJYLIqlJWkpqHepFi9Kw.png" /><figcaption>pod เพิ่มขึ้นเป็น 3 pod</figcaption></figure><p>ในรูปนี้บอกว่า Deployment ของเรามี 3 pods ที่ run อยู่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H9T3PHroslCWgCCgSzcN2w.png" /></figure><p>ส่วนในรูปนี้ เราจะเห็นได้ว่า Status Active (หน้าขวาสุด) เป็น False พอมีการ scale เกิดขึ้น Status Active เปลี่ยนเป็น True</p><p><strong>Reference</strong></p><ul><li><a href="https://keda.sh/docs/2.13/deploy/">https://keda.sh/docs/2.13/deploy/</a></li><li><a href="https://keda.sh/docs/2.13/concepts/">https://keda.sh/docs/2.13/concepts/</a></li><li><a href="https://medium.com/cuddle-ai/auto-scaling-microservices-with-kubernetes-event-driven-autoscaler-keda-8db6c301b18">https://medium.com/cuddle-ai/auto-scaling-microservices-with-kubernetes-event-driven-autoscaler-keda-8db6c301b18</a></li><li><a href="https://medium.com/@carlocolumna/eda-in-amazon-eks-install-keda-82849cf31f01">https://medium.com/@carlocolumna/eda-in-amazon-eks-install-keda-82849cf31f01</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc3c2b52a4b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาลองเขียน Golang กัน Let Go!!]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%A1%E0%B8%B2%E0%B8%A5%E0%B8%AD%E0%B8%87%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99-golang-%E0%B8%81%E0%B8%B1%E0%B8%99-let-go-92f1d9f0183e?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/92f1d9f0183e</guid>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Sat, 30 Sep 2023 14:09:05 GMT</pubDate>
            <atom:updated>2023-09-30T14:09:05.369Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*AawJYAdidSDntQ2z6MrDnw.png" /></figure><p>สวัสดีครับวันนี้ ผมก็จะมาสรุปการเขียน Golang เบื้องต้นนะครับ ผมเองเป็นคนที่ขี้ลืม เลยต้องมาเขียนทวนความจำ วัตถุประสงค์ของการเขียนบทความคือ ให้เข้าใจ syntax ของ Golang เบื้องต้น เช่น for loop,if else เขียนยังไง และประเด็นที่น่าสนใจของ Golang ต้องบอกก่อนว่าผมไม่ได้เก่ง Golang นะครับ พึ่งศึกษามาได้ไม่นาน ถ้ามีผิดผลาดตรงไหน ก็ช่วย comment มาบอกได้นะครับ 555</p><p><strong>คำถามคือทำไมผมมาศึกษา Golang</strong></p><p>จริงๆ เกิดจากการอยาก ลองเขียน ภาษาใหม่ๆ ดูบาง เพราะ แต่ก่อน เขียนเป็นอยู่หลักแค่ 2 ภาษา คือ javascript กับ python เป็น main หลักๆ อีกเหตุผลหนึ่ง คือ ผมเห็นว่า เป็นภาษา เริ่มเป็นที่น่าสนใจมากขึ้น หลายๆ บริษัทเริ่มมาใช้งานมากขึ้น แล้ว Golang ก็เป็นภาษาที่มี performance สูง ทำงานได้เร็ว มีน้ำหนักที่เบา สิ่งหนึ่งหลังจากได้ลองเขียน Golang คือ มันให้ความรู้สึก เหมือนเขียน c บวก python คือ เหมือน c ในแง่ของ functional programming and การใช้ struct กับ pointer และ เหมือน python ในแง่ของความเรียบง่าย</p><p><strong>ข้อดี (Pros)</strong></p><ul><li><strong>Concurrent and Parallel Execution</strong> คือ สามารถเขียน concurrently programing ได้โดยใช้ goroutine</li><li><strong>Fast and Lightwight</strong> คือ<strong> </strong>Golang เป็นภาษามีน้ำหนักที่เบา และเร็วกว่า พวก Python และ Javascript framework เช่น Node.js</li><li><strong>Simplicity </strong>คือ Golang เป็นภาษาที่เรียบง่าย ในความเห็นส่วนตัว ถ้าใครเขียนพวก Javascript หรือ Java จะรู้สึกว่า {} เยอะ แต่ Golang เรียน syntax ง่าย แล้ว ถ้าประกาศตัวแปรแล้วไม่ใช้ก็จะ compile ไม่ผ่านเลย หรือ import library แล้วไม่ใช่ก ็compile ไม่ผ่านเลย</li></ul><p><strong>ข้อเสีย (Cons)</strong></p><ul><li><strong>Lack of libraries </strong>โดยความที่เป็นภาษาใหม่ทำให้ library ไม่ได้มีมากนัก เหมือน ภาษา Java หรือ Python</li><li><strong>Small community </strong>ปัจจุบัน การ Golang ยังไม่ได้เป็นภาษาที่นิยมมาก เมื่อเทียบกับ ภาษาอื่นๆ แต่ community ก็ยังเพิ่มขึ้นอยู่เรื่อยๆ การที่ภาษาเป็นที่นิยมน้อย อาจจะทำให้ดวลาเรามีปัญหา หรือ Error อาจจะหาคนมาช่วย support ยาก</li></ul><p><strong>เริ่มสร้าง program “Hello world ” กัน</strong></p><ol><li>เริ่มจาก สร้าง file ขึ้นมาก่อน ชื่อ main.go</li><li>import library ชื่อ fmt เป็น library ใช้ในการรับข้อมูลและแสดงผลข้อมูลครับ</li><li>จากนั้นสร้าง function main ขึ้นมา และให้ print โชว์ hello world</li></ol><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    fmt.Println(&quot;hello world&quot;)<br>}</pre><p>4.run ไฟล์ Go โดยใช้คำสั่ง</p><pre>$ go run main.go<br>hello world</pre><p>แค่นี้เราก็จะได้ hello world ใน terminal กันแล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*2CA6rFLPvcEt4JALZkrhHw.png" /></figure><p>ใน case ที่เรา ต้องการ build binary file ขึ้นมา เรา run</p><pre>$ go build main.go<br>$ ./main<br>hello world</pre><p><strong>Declare Variable</strong></p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br><br>    var a = &quot;initial&quot;<br>    fmt.Println(a)<br><br>    var b, c int = 1, 2<br>    fmt.Println(b, c)<br><br>    var d = true<br>    fmt.Println(d)<br><br>    e := &quot;apple&quot;<br>    fmt.Println(e)<br>}</pre><p>ในการประกาศ ตัวแปร ทำได้ คือ Explicit Declaration คือ บอกชนิดของตัวแปร กับ Implicit Initialization หรือ short Declaration คือ ไม่บอกชนิดของตัวแปร โดยส่วนตัว ผมชอบ short Declaration (เป็นคนขี้เกียจเขียนเยอะ)</p><p><strong>If condition in Golang</strong></p><pre>point := 50<br> if point &gt;= 50 &amp;&amp; point &lt;= 100 {<br>  fmt.Printf(&quot;point is more than 50 &lt;= 100\n&quot;)<br> } else if point &gt;= 20 {<br>  fmt.Printf(&quot;point is more than 20\n&quot;)<br> }</pre><p>ใน Golang If condition จะเขียน คล้ายๆ ภาษา C ต่างที่ไม่มี “( )”</p><p><strong>Array and Slice in Golang</strong></p><pre> // array datatype<br> var xarray = [3]int{}<br> // or delcare like this<br> yarray := [3]int{1, 2, 3}<br> fmt.Printf(&quot;xarray = %v\n&quot;, xarray) // return [0,0,0]<br> fmt.Printf(&quot;yarray = %v\n&quot;, yarray) // return [1,2,3]<br><br> // assign array<br> xarray[0] = 10<br> fmt.Printf(&quot;xarray = %v\n&quot;, xarray) // return [10,0,0]</pre><p>Array ใน ภาษา Go จะเป็น แบบ fix size โดย จะต้องประกาศ โดยบอก ขนาด ของ array ไม่สามารถ เปลี่ยนได้ครับ ส่วนมากจะใช้ slice แทนมากกว่า</p><pre>//list (slice) in golang<br> xlist := []int{1, 2, 3}<br> //append 4 to xlist<br> xlist = append(xlist, 4)<br> xlist = append(xlist, 4)<br> fmt.Printf(&quot;xlist = %v\n&quot;, xlist) // [1,2,3,4,4]<br> //length of list or array<br> fmt.Printf(&quot;length of xlist is %v\n&quot;, len(xlist))</pre><p>Slice ใน Golang จะเป็น คล้าย array ที่ ปรับ size ได้ โดยเราสามารถ เพิ่ม โดยการ append ค่าที่เราต้องการได้</p><p><strong>Map in Golang</strong></p><pre> countries := map[string]string{}<br><br> countries[&quot;th&quot;] = &quot;Thailand&quot;<br> countries[&quot;en&quot;] = &quot;United State&quot;<br> <br><br> country, ok := countries[&quot;jp&quot;]<br><br> if !ok {<br>  println(&quot;no value&quot;)<br> } else {<br>  println(country)<br> }</pre><p>ใน Golang จะมี Built-in tool ที่เป็นลักษณะการเก็บข้อมูลเป็น key-value เรียกว่า Map ถ้าใน Python มันคือ dictionary นั้นละครับ สังเกตจาก ตัวอย่างนะครับว่า countries[“jp”] มีการ return สองค่า ไว้ check ว่า key นี้มีอยู่จริงไหม ใน case นี้ เราไม่ได้มีการกำหนด key jp ไว้ ดังนั้น โปรแกรม จะ print “no value” ใน case นี้</p><p><strong>Loop in Golang</strong></p><pre>package main<br>import &quot;fmt&quot;<br><br>func main() {<br><br>  // for loop<br>  for i := 1; i &lt;= 5; i++ {<br>    fmt.Println(i)<br>  }<br><br><br>  // while loop<br>  j := 0<br><br>  for j&lt;= 5{<br>    fmt.Println(j)<br>    j++<br>  }<br><br>  // infinte loop<br>  for{<br>    // some action <br>  }<br>}</pre><p>For loop ในภาษา Go จะคล้ายๆ กับ ภาษา C แตกต่างที่ ไม่มี “( )” ส่วน while loop จะต่างกับ ภาษาอื่นๆ ภาษาส่วนมากจะใช้ while แต่ ของ Golang จะใช้ for ได้เลย</p><p><strong>Structs in Golang</strong></p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>)<br><br>type Person struct {<br>    FirstName string<br>    LastName  string<br>    Age       int<br>}<br><br>// Method for the Person struct<br>func (Person p) printHello() string {<br>  return &#39;Hello&#39; + p.FirstName + <br>}<br><br>func main(){<br><br>  person1 := Person{&quot;Nas&quot;, &quot;Virat&quot;, 22}<br><br>  person2 := Person{&quot;Alice&quot;, &quot;Smith&quot;, 25}<br><br>  fmt.Printf(person1.printHello())<br><br>}</pre><p>ใน Golang ก็มี struct เหมือนกับภาษา C เลยครับ อันนี้ เป็นตัวอย่างครับ ทำเป็น struct ชื่อ Person มี attritube คือ FirstNam</p><p><strong>Reference</strong></p><p><a href="https://www.linkedin.com/pulse/exploring-pros-cons-go-golang-programming-language-indra-nand-jha/">https://www.linkedin.com/pulse/exploring-pros-cons-go-golang-programming-language-indra-nand-jha/</a></p><p><a href="https://gobyexample.com/">https://gobyexample.com/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92f1d9f0183e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Genetic Algorithm(GA) เบื้องต้น]]></title>
            <link>https://medium.com/@napas.vin/genetic-algorithm-ga-%E0%B9%80%E0%B8%9A%E0%B8%B7%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%95%E0%B9%89%E0%B8%99-58617558f463?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/58617558f463</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[genetic-algorithm]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Sat, 24 Jun 2023 12:42:22 GMT</pubDate>
            <atom:updated>2023-06-24T12:44:45.564Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*W1UlQGkLpT18-62a_oXt8w.png" /></figure><p>สวัสดีครับ วันนี้จะมาคุยเรื่อง Genetic Algorithm คืออะไร และสามารถทำอะไรได้บ้าง Genetic Algorithm(GA) เป็น algorithm optimization หาชุดพารามิเตอร์ที่ดีที่สุดสำหรับ objectives function ฟังดูแรกๆ อาจจะงงๆว่ามันคืออะไร แต่ไม่เป็นไร อ่านไปเรื่อยพอจะเห็นภาพมากขึ้น ผมจะเล่าพื้นฐานตั้งแต่ Optimization คืออะไร และไปถึง Genetic Algorithm</p><p>การที่จะไปเข้าใจว่า Genetic Algorithm คืออะไร ผมจะเล่าเรื่อง Optimization คืออะไรก่อน จะได้ไม่งงว่าเราทำอะไรอยู่ Optimization ในภาษาไทย แปลว่า กระบวนการหาจุดที่ดีที่สุด หรือ การหาค่าที่เหมาะสมที่สุด ในที่นี้จะเป็นการหาค่าสูงที่สุด หรือต่ำที่สุด ก็ได้ขึ้นอยู่กับโจทย์ปัญหาของเราว่าจะเป็นแบบไหน ตัวอย่างโจทย์ปัญหาการ optimization เช่น การจัดการตารางงาน ตารางการสอนให้มีประสิทธิภาพมากที่สุด เช่น บริษัทมีพนักงานอยู่ 10 คนแต่ละคนมี ช่วงเวลาที่ว่างไม่เหมือนกัน จัดตารางการทำงานยังไงให้ใช้คนได้มีประสิทธิภาพ ทุกคนทำงานเท่าๆกัน ไม่มีใครได้งานมากหรือน้อยกว่าใคร หรือโจทย์ปัญหา การหาเส้นทางที่สั้นที่สุด เร็วที่สุดจะต้องทำยังไง (Route Optimization) หรือ โจทย์ปัญหาเช่น knapsack problem เป็นต้น</p><h3>คำศัพท์เฉพาะทาง Optimization</h3><ul><li><strong>Objective Function </strong>ภาษาไทยคือ ฟังก์ชั่นวัตถุประสงค์ (ฟังแล้วจะรู้สึกแปลกๆ 5555) หมายถึงฟั่งชั่นที่เราต้องการหาค่าต่ำสุดหรือสูงสุด ในการแก้ปัญหา Optimization สิ่งที่เป็นฐานที่สำคัญที่สุดของการแก้ไข้ปัญหา Optimization คือการกำหนด Objective function พูดง่ายคือ เราอยากจะหาค่าที่ดีที่สุดของอะไร? ตัวอย่างเช่น โจทย์ปัญหา การหาเส้นทางที่สั้นที่สุด Objective function ของเราอาจจะเป็น การ minimizes ระยะทางระหว่างจุดถึงจุดทั้งหมด เป็นต้น</li><li><strong>Decision Variable </strong>คือคำตอบของคำถาม ในตัวอย่าง Route Optimization คือ เส้นทางที่ใช้เวลาน้อยที่สุดในการเดินทาง หรือ ในทาง Optimization เราสามารถพูดว่า เส้นทางที่ Minimize Objective function ของเรา</li><li><strong>Constraints</strong> คือ ข้อกำจัดว่าที่เราจะกำหนด ในแต่ละโจทย์ที่เราได้รับ เช่น ในโจทย์ปัญหา Route Optimization เช่น เราไม่สามารถเส้นทางนี้ได้เพราะ ทางถนนกำลังก่อสร้างอยู่เป็นต้น</li></ul><h3>แล้ว Genetic Algorithm เกี่ยวอะไรกับ Optimization</h3><p>Genetic Algorithm เป็น Algorithm อันหนึ่งที่ใช้ในการแก้ปัญหา โจทย์ Optimization โดยได้รับแรงบันดาลใจมาจากแนวคิดการวิวัฒนาการของธรรมชาติ key idea สำคัญ คือ การสุ่มคำตอบที่น่าจะเป็นไปได้ base on คำตอบก่อนหน้า แล้วทำการเปรียบเทียบ ว่าคำตอบใหม่ดีกว่าเดิมไหม จากนั้น ทำการเลือก คำตอบใหม่ ซึ่ง Genetic Algorithm มีขั้นตอนจะเป็นดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*Q1kBB2BuuMWzM5inF4urqA.png" /><figcaption>Flow chart Genetic Algorithm</figcaption></figure><p><strong>1.Initial population</strong></p><p>Initial Population คือการกำหนด ประชากรขึ้นมา ประชากรในที่นี้หมายถึง กลุ่มของ solution หรือคำตอบที่เป็นไปได้ โดยในประชากร(<strong>Population</strong>) ก็จะมี<strong> Chromosome </strong>อยู่ n ตัว (คำตอบที่เป็นไปได้) และใน 1 Chromosome มีจะมี <strong>Gene</strong> m ตัว ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*W1UlQGkLpT18-62a_oXt8w.png" /></figure><p><strong>2. Fitness function</strong></p><p>fitness function คือฟังชั่นที่ใช้ในการการประเมินเพื่อใช้ในการเปรียบเทียบว่า <strong>Chromosome </strong>คือ คำตอบไหนดีกว่ากัน โดยเป็นเหมือนการให้คะแนนว่าอันไหนดีกว่ากัน ประมาณนั้น โดยค่าที่ได้จากการประเมินเราจะเรียกว่า <strong>fitness value</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nu9o2Wwkyr7pY6IV-NxggA.png" /></figure><p><strong>3. Selection</strong></p><p>Selection คือการเลือก Chromosome ว่า Chromosome ไหน ควรที่จะอยู่และ Chromosome ไหนควรที่จะเอาออก โดยเราจะให้โอกาสกับ Chromosome ที่มี fitness value ที่ดีกว่า ผ่านเป็นรุ่นต่อไปมากกว่า Chromosome ที่มี fitness value ที่แย่กว่า โดยการคัดเลือกจะมีหลายวิธี เช่น <a href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection">Roulette Wheel Selection</a> , Ranking Selection หรือ <a href="https://www.geeksforgeeks.org/tournament-selection-ga/">Tournament Selection </a>เป็นต้น</p><p><strong>4. Crossover</strong></p><p>Crossover คือการสร้าง chromosome หรืออีกความหมายถึงคือการสร้าง คำตอบที่เป็นไปได้ใหม่ขึ้น โดย เราจะสุ่ม chromosome จาก population ของเรามา 2 ตัวและเลือกตำแหน่งที่เราจะทำการเปลี่ยน gene ซึ่งเราจะเรียกตัวแหน่งนั้นว่า <strong>Crossover point</strong> โดย Crossover point จะถูกกำหนดจากการสุ่มจุดขึ้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*dsfeKjKS7gggE3wP3k3tDQ.png" /></figure><p><strong>5. Mutation</strong></p><p>Mutation คือการกลายพันธุ์ คือการเปลี่ยน Gene ของ Chromosome ที่เรามี แต่เราจะไม่เปลี่ยนทุกตำแหน่งแต่จะใช้การสุ่ม ในขั้นตอนการทำ Mutation เราจะต้องกำหนดตัวแปร ชื่อว่า Mutation rate ซึ่งจะบอกว่า จะทำการ Mutation มากน้อยแค่ไหน การกำหนด Mutation rate เราจะไม่กำหนด สูงมาก อาจจะอยู่ในช่วง 0.1–0.3 หรือ 10–30% ของ gene ใน Chromosome เป็นต้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*CUbHYYgwKLWgKMUTzmbOZw.png" /></figure><p><strong>6. Termination</strong></p><p>Termination คือ เงื่อนไขในการหยุด ทำงานของ Genetic Algorithm โดยการหยุดการทำงานของ Genetic Algorithm จะขึ้นอยู่ที่ว่า คำตอบ หรือ Chromosome ที่เราได้จากการ Crossover และ Mutation เริ่มไม่มีการเปลี่ยนแปลง หลาย Generation แล้ว หรือ ทำไปแล้ว 1000 Generations เป็นต้น</p><h3>ตัวอย่าง การใช้ Genetic Algorithm</h3><p>อันนี้คือตัวอย่างการใช้ Genetic Algorithm ผมลองให้ ChatGPT เขียน Genetic Algorithm Basic ให้นะครับ โดยโจทย์ปัญหานี้คือ maximize function นะครับ</p><ul><li><strong>Objective function : </strong>maximize f(x) ,f(x) = x1+x2+x3+x4+….+x10, x มีค่าแต่ 0 กับ 1</li><li><strong>Constraint :</strong> ไม่มี</li></ul><pre>import random<br><br># Genetic Algorithm parameters<br>POPULATION_SIZE = 100<br>GENOME_LENGTH = 10<br>MUTATION_RATE = 0.01<br>GENERATIONS = 100<br><br># Define the fitness function (example: maximize a simple function)<br>def fitness_function(individual):<br>    return sum(individual)  # Maximizing the sum of genes<br><br># Create an initial population of random individuals<br>def create_population():<br>    population = []<br>    for _ in range(POPULATION_SIZE):<br>        individual = [random.randint(0, 1) for _ in range(GENOME_LENGTH)]<br>        population.append(individual)<br>    return population<br><br># Select parents for reproduction using tournament selection<br>def select_parents(population):<br>    tournament_size = 5<br>    parents = []<br>    for _ in range(2):<br>        tournament = random.sample(population, tournament_size)<br>        best_individual = max(tournament, key=fitness_function)<br>        parents.append(best_individual)<br>    return parents<br><br># Perform crossover to produce offspring<br>def crossover(parents):<br>    crossover_point = random.randint(1, GENOME_LENGTH - 1)<br>    child1 = parents[0][:crossover_point] + parents[1][crossover_point:]<br>    child2 = parents[1][:crossover_point] + parents[0][crossover_point:]<br>    return child1, child2<br><br># Perform mutation on an individual<br>def mutate(individual):<br>    for i in range(GENOME_LENGTH):<br>        if random.random() &lt; MUTATION_RATE:<br>            individual[i] = 1 - individual[i]<br><br># Create the next generation<br>def create_next_generation(population):<br>    new_population = []<br>    while len(new_population) &lt; POPULATION_SIZE:<br>        parents = select_parents(population)<br>        offspring1, offspring2 = crossover(parents)<br>        mutate(offspring1)<br>        mutate(offspring2)<br>        new_population.append(offspring1)<br>        new_population.append(offspring2)<br>    return new_population<br><br># Main genetic algorithm loop<br>def genetic_algorithm():<br>    population = create_population()<br>    best_individual = []<br>    best_fitness = 0<br><br>    for _ in range(GENERATIONS):<br>        population = create_next_generation(population)<br>        for individual in population:<br>            fitness = fitness_function(individual)<br>            if fitness &gt; best_fitness:<br>                best_fitness = fitness<br>                best_individual = individual<br><br>    print(&quot;Best individual:&quot;, best_individual)<br>    print(&quot;Best fitness:&quot;, best_fitness)<br><br># Run the genetic algorithm<br>genetic_algorithm()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58617558f463" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาทำ Dashboard หุ้นด้วย Streamlit กัน]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3-dashboard-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-streamlit-%E0%B8%81%E0%B8%B1%E0%B8%99-3549ef2ac1c3?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/3549ef2ac1c3</guid>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[stock-market]]></category>
            <category><![CDATA[streamlit]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Tue, 13 Jun 2023 13:52:44 GMT</pubDate>
            <atom:updated>2023-06-13T13:52:44.394Z</atom:updated>
            <content:encoded><![CDATA[<p>ใช้ streamlit มาทำ Dashboard การลงทุนเพื่อใช้ในการวิเคราะห์กัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4-tl0l_6nJwI-UT7sY6p0Q.png" /><figcaption>Example Dashboard CPALL stock</figcaption></figure><h4><strong>Streamlit คืออะไร</strong></h4><p>Streamlit เป็น open source library ให้คนที่อาจไม่สาย coding จัดๆ มา หรือ คนที่อาจมีประสบการณ์การเขียน code อยู่แล้วต้องการทำ interactive web application หรือ data dashboard โดยใช้ python ขอดีของ streamlit คือ ไม่ซับซ้อนมาก เขียนง่าย หรือ บางทีเราทำ project data science หรือ machine learning อะไรบ้างอย่างเราก็ใช้ streamlit ในการโชว์ ผลงานก็ได้</p><h4><strong>เริ่มใช้งาน Streamlit กัน</strong></h4><p>ก่อนจะไปเขียน Streamlit เราก็ต้อง install library ที่เราต้องใช้ก่อน ซึ่งในทีนี้ เราจะเราจะใช้ 3 library หลัก คือ 1. streamlit 2. numpy 3. plotly</p><pre>pip install streamlit<br>pip install numpy<br>pip install plotly</pre><p>พอ install library เสร็จแล้วก็ลองเขียน web application ง่ายๆ กัน</p><ol><li>สร้างไฟล์ dashboard.py</li><li>เขียน code ใน dashboard.py กัน</li></ol><pre>import streamlit as st <br><br>st.header(&quot;Dashboard&quot;)<br><br>st.write(&quot;Hello world&quot;)</pre><p>3. ที่นี่ก็มา run app กัน เปิด terminal ขึ้นมา แล้วพิมพ์</p><pre>streamlit run dashboard.py</pre><p>พอพิมพ์เสร็จเราก็จะเห็นใน terminal ประมาณนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*48XGBITzlRc8o9keiyobPQ.png" /></figure><p>เปิด browser ดูที่ <a href="http://localhost:8501/">http://localhost:8501</a> ก็จะเห็น website Hello world ง่ายๆแล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*_rFZDAshwe6Xy63McOyzfA.png" /></figure><h3><strong>มาทำ Dashboard หุ้นไทยกัน</strong></h3><p>ในการทำ Dashboard ครั้งนี้ ผมเลือกที่จะใช้ plotly ในการทำ dashboard เป็นหลัก โดย <a href="https://github.com/Nas-virat/streamlit-TH-stock">project</a> ใน Github ที่ผมทำนี้จะมีโครงสร้าง file จะมี ประมาณนี้นะครับ</p><pre>root<br>├── alldata.csv<br>├── config<br>│   ├── color.py<br>├── dashboard.py<br>├── feature<br>│   ├── BS.py<br>│   ├── cashcycle.py<br>│   ├── cashflow.py<br>│   ├── eps.py<br>│   ├── margin.py<br>│   ├── metric.py<br>│   ├── revenueProfit.py<br>│   ├── roeroa.py<br>│   └── turnover.py<br>├── setupfile.py<br>├── style.css<br>├── TH-data<br>│   ├── 2547-q1.csv<br>│   ├── 2547-q2.csv<br>|   .......<br>│   └── 2566-q1.csv<br>└── utils<br>    ├── barplot.py<br>    ├── cardlist.py<br>    ├── lineplot.py<br>    └── utils.py</pre><ul><li><strong>dashboard.py</strong> จะเป็น file หลักของ web application</li><li><strong>setupfile.py</strong> จะเป็น file ทำหน้าที่ในการ clean ข้อมูล file csv ใน TH-data และ นำมารวมกันเป็น file alldata.csv</li><li><strong>alldata.csv</strong> จะเป็น file ที่รวม file csv in folder TH-data</li><li><strong>config folder</strong> จะมี file color.py ใช้ในการกำหนดสีของ graph แต่ละอัน โดยอ้างอิงจาก column ในตาราง alldata.csv</li><li><strong>feature folder</strong> จะเป็น folder ที่รวม file plot ของแต่ละส่วน เช่น BS.py ก็จะเป็น plot ที่เกี่ยวข้องกับ balance sheet , cashflow.py ก็จะเป็น plot ที่เกี่ยวข้องกับ cash cycle</li><li><strong>utils folder</strong> จะเป็น folder ที่รวม file ที่เกี่ยวข้องกับการ สร้าง plot ขึ้น</li></ul><p>ในที่นี้ผมไม่เล่าทุก file นะครับ ผมจะเล่าไฟล์ที่ดูน่าสนใจและสำคัญๆ เป็นหลัก</p><h3><strong>dashboard.py</strong></h3><p>file นี้เป็นไฟล์หลักในการ run website ขึ้นมา หลักๆจะประกอบไปด้วย การ load ไฟล์จาก allstock.csv ซึ่งเป็นไฟล์รวม record หุ้นทั้งหมด stock_options จะเป็น หุ้นทั้งหมดที่มีอยู่ใน record ทั้งหมด และก็มี slider ในการปรับช่วงหุ้นที่เราสนใจจะดู</p><pre><br># dashboard.py<br><br><br># Load the stock data<br>allstock = pd.read_csv(&#39;alldata.csv&#39;)<br><br># Get unique stock options<br>stock_options= allstock[&#39;Symbol&#39;].unique()<br><br># Create a selectbox for stock selection<br>selected_stock = st.sidebar.selectbox(&quot;Select a stock:&quot;,options=stock_options,index=6)<br>st.sidebar.subheader(&#39;Range of year&#39;)<br><br>min_year = allstock[&#39;Year&#39;].min()<br>max_year = allstock[&#39;Year&#39;].max()<br><br>range = st.sidebar.slider(<br>    &#39;Select a range of year&#39;,<br>    min_value=2547,<br>    max_value=int(max_year),<br>    value=[int(max_year-10),int(max_year)]<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/274/1*NpfE68d3l4ndZBARBTWVHg.png" /></figure><pre># dashboard.py<br><br><br># Filter data based in the selected stock and range<br>filtered_data = allstock[(allstock[&#39;Symbol&#39;] == selected_stock) &amp; (allstock[&#39;Year&#39;] &gt;= range[0]) &amp; (allstock[&#39;Year&#39;] &lt;= range[1])]<br><br># Sort the filtered data table<br>filtered_data = filtered_data.sort_values(by=[&#39;Year&#39;,&#39;Quarter&#39;])<br><br># Calculate current year and quarter from csv file<br>current_year = filtered_data[&#39;Year&#39;].max()<br><br>current_quarter = filtered_data[filtered_data[&#39;Year&#39;] == current_year][&#39;Quarter&#39;].max()</pre><h3>barplot.py</h3><p>file นี้จะเป็น file ที่อยู่ใน utils เป็น file ที่มี function ชื่อว่า barplot function นี้ ใช้ในการทำ barplot ขึ้นมา มี argument ดังนี้</p><ol><li><strong>fig</strong> เป็น figure ที่สร้างจาก plotly</li><li><strong>df </strong>เป็น dataframe หรือ ข้อมูลที่เราสนใจที่จะ plot</li><li><strong>columns</strong> จะเป็น list ชื่อ column ที่อยู่ใน file allstock.csv ที่เราสนใจจะนำมา plot</li><li><strong>markers</strong> จะเป็น list ของ สีในแต่ละ column ว่าเราอยากได้สีอะไร</li><li><strong>legendfontsize</strong> ตามชื่อเลยครับ ขนาด font legend</li><li><strong>xaxis_title </strong>เป็นชื่อแกน x</li><li><strong>yaxis_title </strong>เป็นชื่อแกน y</li><li><strong>tickangle </strong>เป็นองศา ของ แกน x ทำให้เอียงดูง่ายขึ้น</li></ol><pre># barplot.py<br><br>import pandas as pd<br><br>import plotly.graph_objects as go<br>from plotly.subplots import make_subplots<br><br>def barplot(<br>        fig:go.Figure,<br>        df:pd.DataFrame,<br>        columns:list[str],<br>        markers:list[str],<br>        legendfontsize:int,<br>        xaxis_title:str,<br>        yaxis_title:str,<br>        tickangle:int,<br>        ) -&gt; go.Figure | None:<br><br>    if(len(columns) != len(markers)):<br>        raise Exception(&#39;columns and marker must have the same length&#39;)<br>    <br>    else:<br>        for i in range(len(columns)):<br>            fig.add_trace(<br>                go.Bar(<br>                    name=columns[i],<br>                    x=df[&#39;Period&#39;],<br>                    y=df[columns[i]],<br>                    base=0,<br>                    textposition=&quot;outside&quot;,<br>                    textfont=dict(size=14),<br>                    marker=dict(color=markers[i]) <br>                ),<br>                secondary_y=False<br>            )<br>    <br>    fig.update_xaxes(tickangle=tickangle)<br><br>    fig.update_layout(<br>        showlegend=True,<br>        xaxis_title=xaxis_title,<br>        yaxis_title=yaxis_title,<br>        xaxis=dict(<br>            title_font=dict(size=20),<br>            tickfont=dict(size=18),<br>        ),<br>        yaxis=dict(<br>            title_font=dict(size=20),<br>            tickfont=dict(size=18),<br>        ),<br>        barmode=&#39;group&#39;,<br>        legend=dict(<br>            x=1,<br>            y=1,<br>            bgcolor=&#39;rgba(0,0,0,0)&#39;,<br>            font=dict(size=legendfontsize)<br>        ),<br>        hoverlabel=dict(font=dict(size=24))<br>    )<br><br>    return fig</pre><p>อันนี้เป็นตัวอย่างการใช้งาน barplot ใน eps.py file นี้ จะโชว์ trend ของ earning per share ของบริษัท</p><pre># eps.py<br><br>import pandas as pd<br>from plotly.subplots import make_subplots<br><br>from utils.barplot import *<br>from utils.utils import *<br><br>def epsFeature(st,df: pd.DataFrame) -&gt; None:<br><br>    figeps = make_subplots(specs=[[{&quot;secondary_y&quot;: True}]])<br><br>    barplot(<br>        fig=figeps,<br>        df=df,<br>        columns=[&#39;EPS&#39;],<br>        markers=setColor([&#39;EPS&#39;]),<br>        legendfontsize=20,<br>        xaxis_title=&#39;Year&#39;,<br>        yaxis_title=&#39;Amount&#39;,<br>        tickangle=-60<br>        )<br><br>    # Display the bar plot<br>    st.plotly_chart(figeps,use_container_width=True)</pre><p>หน้าตาก็จะประมาณนี้ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gEhaoOMfyNtsy0uwpwZ-Wg.png" /></figure><p>ลองนำ code ไป ลอง run เล่นดูก็ได้นะครับ</p><h4><strong>More Information</strong></h4><ul><li><a href="https://github.com/Nas-virat/streamlit-TH-stock">https://github.com/Nas-virat/streamlit-TH-stock</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3549ef2ac1c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ลองใช้ Datawrapper ทำ Data Visualization กัน]]></title>
            <link>https://medium.com/@napas.vin/%E0%B8%A5%E0%B8%AD%E0%B8%87%E0%B9%83%E0%B8%8A%E0%B9%89-datawrapper-%E0%B8%97%E0%B8%B3-data-visualization-%E0%B8%81%E0%B8%B1%E0%B8%99-ec7f14549135?source=rss-80bc867526bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec7f14549135</guid>
            <category><![CDATA[storytelling]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Napas Vinitnantharat]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 11:23:56 GMT</pubDate>
            <atom:updated>2023-04-25T11:23:56.407Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*qmlqD0aQXKmBQVF-dIBWvQ.png" /><figcaption>Number of Thai Tourism On February 2023</figcaption></figure><p>สวัสดีครับวันนี้ ผมก็จะมาเล่าการทำ Data Visualization กับเครื่องมือ D<a href="https://www.datawrapper.de/">atawrapper</a> นะครับ ก่อนอื่นผมขอเล่าที่มาของการเขียนอันนี้ก่อน คือพอดีได้การบ้านมาจากทางมหาวิทยาลัยให้ทำไปคิดโจทย์ทางธุรกิจที่เราสนใจมาหนึ่งอัน แล้วให้ทำ Data Visualization แล้วมาเล่าให้เพื่อนๆ ได้ฟังและเข้าใจ ธุรกิจที่ผมเลือกเป็นธุรกิจการท่องเที่ยวผมเลยอยากรู้ว่า จำนวน นักท่องเที่ยวในไทยเป็นอย่างไรบ้างในช่วงที่ผ่านมาแล้ว มาดูกันครับ</p><p><strong>Datawrapper</strong></p><p>เป็นเครื่องมือทำ Data Visualization ที่ใช้งานง่ายมากครับ ไม่ต้องเขียนโค๊ด แม้แต่บรรทัดเดียวเลยครับ มาเริ่มกันเลยดีกว่าครับ</p><ol><li>เลือกแผนที่ที่เราสนใจครับ ในที่นี้เราเลือกประเทศไทยนะครับในการทำการ plot</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q6MNsAhO9IQDlcP3J3X33A.png" /></figure><p>2. เพิ่มข้อมูลกันครับ ในที่นี้ผมใช้ข้อมูล จาก <a href="https://www.kaggle.com/datasets/thaweewatboy/thailand-domestic-tourism-statistics">Kaggle Thailand Domestic Tourism Statistics</a> และเอามาจัดการข้อมูลนิดหน่อย และก็ upload ไฟล์ csv ขึ้นไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VURfkun9mmkuFvq0SYrZZg.png" /></figure><p>3. ต่อมา Visualize ก็จะเป็นการแต่ง visualize ของเรา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*s7kE0_vzi7y11S3DYCSTlg.png" /></figure><p>4. Publish &amp; Embed หลังจากตกแต่ง เราก็จะทำการแชร์ public ผ่าน link</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jq_TfgKeIQ-Fnbir2Q_TqQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec7f14549135" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>