<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Leszek Walszewski</title>
    <description>The latest articles on Forem by Leszek Walszewski (@walesgator).</description>
    <link>https://forem.com/walesgator</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F888596%2F7031a64f-7b9f-4926-99aa-071275e3f06a.jpg</url>
      <title>Forem: Leszek Walszewski</title>
      <link>https://forem.com/walesgator</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/walesgator"/>
    <language>en</language>
    <item>
      <title>Dynamic destructuring JavaScript</title>
      <dc:creator>Leszek Walszewski</dc:creator>
      <pubDate>Wed, 15 Nov 2023 19:05:44 +0000</pubDate>
      <link>https://forem.com/walesgator/dynamic-destructuring-javascript-1b14</link>
      <guid>https://forem.com/walesgator/dynamic-destructuring-javascript-1b14</guid>
      <description>&lt;p&gt;Hi!&lt;/p&gt;

&lt;p&gt;Today I want to share with you a trick about dynamic destructuring in JavaScript.&lt;/p&gt;

&lt;p&gt;It is not always clear what exactly property we want to desctruct from an object.&lt;/p&gt;

&lt;p&gt;This is a vice versa situation when you want to access object property dynamically.&lt;/p&gt;

&lt;p&gt;For example, let's assume this object with balances of three cryptocurrency exchanges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;binance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someBinanceData&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="na"&gt;kucoin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someKucoinData&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="na"&gt;bybit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;someByBitData&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we have a component in which we pass a source_key as a variable. Somewhere in the component, we need to get particular data. We don't know that "source_key" is binance, kucoin or bybit and we want component to be reusable. So we can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;binance&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--- we can receive this from the outside as variable&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;source_key&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;balanceSource&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balanceSource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//someBinanceData&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Use ChatGPT to convert function to SQL query</title>
      <dc:creator>Leszek Walszewski</dc:creator>
      <pubDate>Fri, 17 Feb 2023 15:02:02 +0000</pubDate>
      <link>https://forem.com/walesgator/use-chatgpt-to-convert-function-to-sql-query-51i5</link>
      <guid>https://forem.com/walesgator/use-chatgpt-to-convert-function-to-sql-query-51i5</guid>
      <description>&lt;p&gt;I've been struggling with tasks that require me to write SQL queries for testing purposes. I have to create queries that return the same results as PHP functions. I thought this was a good moment to test the capabilities of ChatGPT. I prepared a simpler version of the function that I want to convert into plain SQL and asked it for help. Let's take a look at the results.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 1
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumQuantity(array $offers): int
{
    $groupsCount = [];

    foreach ($offers as $offer) {

        if ($offer-&amp;gt;getStatus() !== OfferStatuses::STATUS_ACTIVE) {
            continue;
        }

        if (isset($groupsCount[$offer-&amp;gt;getGroup()-&amp;gt;getId()])) {
            continue;
        }

        if ($offer-&amp;gt;getGroup()-&amp;gt;getOldestOffer()) {
            $offer = $offer-&amp;gt;getGroup()-&amp;gt;getOldestOffer();
        }

        $quantity = $offer-&amp;gt;getGroup()-&amp;gt;getQuantity();

        $groupsCount[$offer-&amp;gt;getGroup()-&amp;gt;getId()] = $quantity;
    }

    return array_sum($groupsCount);
}

PostgreSQL tables:
- "offers", columns: id, group_id, company_status
- "groups", columns: id, oldest_offer_id, quantity


I need a sql query that returns the same result 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was surprised by the answer. It gave me a solution with a fancy explanation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fry8zz6tetz591835brr6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fry8zz6tetz591835brr6.png" alt="Image description" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I thought - OK Chat, that was easy. Show me how you handle something harder, maybe with subquery.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 2
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getSumOfMaximalCounts($matches)
{
    $matchesCount = 0;

    foreach ($matches as $match) {
        if (!$match-&amp;gt;getMatches()) {
            continue;
        }

        $maxCountFromGroups = 0;
        foreach ($match-&amp;gt;getMatches() as $matchItem) {
            if ($matchItem['count'] &amp;gt; $maxCountFromGroups) {
                $maxCountFromGroups = $matchItem['count'];
            }
        }

        $matchesCount += $maxCountFromGroups;
    }

    return $matchesCount;
}

postreSQL table:
offer_matches:
- offer_id int
- matches json

I need a sql query that returns the same result 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, I was also amazed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrc9l4q1dvu3yegpys2n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrc9l4q1dvu3yegpys2n.png" alt="Image description" width="790" height="583"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have handled all queries with this cooperation. Not all cases were generated without errors, but even with small corrections from myself, I saved a lot of time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Even if not all answers were perfect, I'm very impressed with the capabilities of the OpenAI product. It should be noted that this is the previous version of model (3), but version 4 will be available soon. Another fact is that it is learning all the time, so the number of errors will diminish.&lt;br&gt;
I will further experiment with this tool. I feel that the new era is here :)&lt;/p&gt;

</description>
      <category>pihole</category>
      <category>selfhost</category>
      <category>networking</category>
      <category>devops</category>
    </item>
    <item>
      <title>Year summary &amp; plans</title>
      <dc:creator>Leszek Walszewski</dc:creator>
      <pubDate>Sun, 12 Feb 2023 11:00:47 +0000</pubDate>
      <link>https://forem.com/walesgator/year-summary-plans-2ihk</link>
      <guid>https://forem.com/walesgator/year-summary-plans-2ihk</guid>
      <description>&lt;p&gt;January is an ideal time to summarize the past year and to set plans and goals for the coming year. In my experience, it's helpful to divide this into two categories: personal and career.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Personal
&lt;/h3&gt;

&lt;p&gt;Last year was a big change for me. The most important thing is that I have purchased a home with my fiancee. &lt;/p&gt;

&lt;p&gt;The war in Ukraine began after less than a month of living there. We felt compelled to offer assistance to the Ukrainian people. As a result, we decided to share our home with a Ukrainian family. They lived with us for about three months, and then we helped them find their own flat. I will never forget the moment when they were first reunited with their father and husband. That was a huge lowliness lesson.&lt;/p&gt;

&lt;p&gt;Another significant realization for me last year was visiting a new, exotic country. My girlfriend and I spent two wonderful weeks on one of the most well-known paradise islands: the Dominican Republic. It was an amazing experience.&lt;/p&gt;

&lt;p&gt;Plans for 2023&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wedding&lt;/li&gt;
&lt;li&gt;asian / south america travel&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Career
&lt;/h3&gt;

&lt;p&gt;Last year was very intensive, and I learned a lot. I was able to keep up with my self-development routine system that I started at the beginning of the year. At work, I begun to work more independently with more complex projects, e.g., the progress measuring module for the financial controller.This experience has allowed me to see the value I bring to the company and it's incredibly fulfilling.&lt;/p&gt;

&lt;p&gt;Plans for 2023&lt;br&gt;
The main goal is crystal clear here. I want to increase my salary. There are two options to achieve this. First, get a raise in my current company. This is a better option according to the fact that I like this project - tech stack, and I'm learning a lot there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I'm excited about 2023 as well. This year, I've made a commitment to improving my skills and competencies as a software engineer. I'm working hard and am confident that the results will be positive. Here's to a great year ahead!&lt;/p&gt;

</description>
      <category>newbie</category>
      <category>firstpost</category>
    </item>
    <item>
      <title>Debug php-fpm with strace</title>
      <dc:creator>Leszek Walszewski</dc:creator>
      <pubDate>Mon, 09 Jan 2023 19:09:55 +0000</pubDate>
      <link>https://forem.com/walesgator/debug-php-fpm-with-strace-2ha1</link>
      <guid>https://forem.com/walesgator/debug-php-fpm-with-strace-2ha1</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;I've been dealing with a problem in one of my projects for quite some time. I think this is the worst kind of problem: it only happens in production, it occurs randomly, and it doesn't leave any traces in either PHP logs or server logs. When the problem occurs, there is a terribly long page load that ends in a timeout.&lt;/p&gt;

&lt;p&gt;The project is quite old with a large technology debt. No framework was used, so it's very difficult to find common problems or get community advice.&lt;/p&gt;

&lt;p&gt;The client won't invest a lot of money in this project, but it's still profitable, so he is trying to get every task done at a low cost.&lt;br&gt;
Therefore, there is no opportunity to purchase a New Relic license or a similar paid profiling service. So, how can we trace low-level problems with the tools that we already have?&lt;/p&gt;
&lt;h2&gt;
  
  
  Strace to the rescue
&lt;/h2&gt;

&lt;p&gt;Strace is the Linux tool for tracing system processes. It can be used to debug low-level interactions between them.&lt;/p&gt;

&lt;p&gt;The basic usage command is:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ strace -p 26380
strace: Process 26380 attached
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When the failure occurs again, the only thing left to do is execute a command that will connect the Strace to every php-fpm sub-process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo strace -f $(pgrep php-fpm | sed 's/\([0-9]*\)/\-p \1/g') -o ~/stracelogs/log5 -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-f trace child processes 
-o save result to the file
-t add timestamps to each line
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we use a combination of phrep and sed to find all child php-fpm processes to attach to. &lt;/p&gt;

&lt;p&gt;Logs may appear to be complex, but we can sometimes find interesting information in them. I have found this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 6
connect(6, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("xxx.xxx.xx.xx")}, 16) = 0
poll([{fd=6, events=POLLOUT}], 1, 0)    = 1 ([{fd=6, revents=POLLOUT}])
sendmmsg(6, [{msg_hdr={msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="&amp;amp;\5\1\0\0\1\0\0\0\0\0\0\6portal\7interia\2pl\0\0"..., iov_len=35}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, msg_len=35}, {msg_hdr={msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\36\r\1\0\0\1\0\0\0\0\0\0\6portal\7interia\2pl\0\0"..., iov_len=35}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, msg_len=35}], 2, MSG_NOSIGNAL) = 2
poll([{fd=6, events=POLLIN}], 1, 5000)  = 1 ([{fd=6, revents=POLLIN}])
ioctl(6, FIONREAD, [156])               = 0
recvfrom(6, "&amp;amp;\5\201\200\0\1\0\1\0\3\0\3\6portal\7interia\2pl\0\0"..., 2048, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("xxx.xxx.xx.xx")}, [28-&amp;gt;16]) = 156
poll([{fd=6, events=POLLIN}], 1, 4999)  = 1 ([{fd=6, revents=POLLIN}])
ioctl(6, FIONREAD, [93])                = 0
recvfrom(6, "\36\r\201\200\0\1\0\0\0\1\0\0\6portal\7interia\2pl\0\0"..., 65536, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("xxx.xxx.xx.xx")}, [28-&amp;gt;16]) = 93
close(6)                                = 0
socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = 6
fcntl(6, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(6, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
connect(6, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("xxx.xx.xx.xxx")}, 16) = -1 EINPROGRESS (Die Operation ist jetzt in Bearbeitung)
poll([{fd=6, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 60000
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there, the process got stuck.&lt;br&gt;
Additional it was unable to connect to the ip xxx.xx.xx.xxx by CURL.&lt;/p&gt;

&lt;p&gt;The page went online again when this IP responded.&lt;/p&gt;

&lt;p&gt;After a little workaround, I found an issue in the code. The reason for the crush was the file_get_contents() function that tried to load the URL directly and put it in HTML's header.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Strace is a powerful tool. Sometimes, even basic knowledge of it is enough to find the cause of a problem. This is especially true when we don't have the budget for specialized, dedicated debugging software.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setup a user in Dockerfile</title>
      <dc:creator>Leszek Walszewski</dc:creator>
      <pubDate>Wed, 26 Oct 2022 18:20:22 +0000</pubDate>
      <link>https://forem.com/walesgator/setup-a-user-in-dockerfile-ke7</link>
      <guid>https://forem.com/walesgator/setup-a-user-in-dockerfile-ke7</guid>
      <description>&lt;p&gt;&lt;em&gt;Hi everyone!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Welcome to the first blog post of my life:) The main reason I decided to create a blog is to improve my writing skills in English. I decided to start just describing my everyday programming problems, so if anyone will find something interesting in the future, that would be great!&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I'm developing an application in Laravel and Vue using dockered php-fpm and nginx services.&lt;/p&gt;

&lt;p&gt;I have used out of the box sail solution previously, but I wanted to have simultaneus requests handling.&lt;/p&gt;

&lt;p&gt;I decided to use great boilerplate Dockerfile from this repo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Cyber-Duck/php-fpm-laravel/blob/8.1/Dockerfile"&gt;https://github.com/Cyber-Duck/php-fpm-laravel/blob/8.1/Dockerfile&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there was a little caveat.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;I was logged in as root when I launched Docker terminal. The main pitfall of this approach is that php artisan generated files have a root owner even on the host machine.&lt;/p&gt;

&lt;p&gt;When I checked the Dockerfile, I found this line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN usermod -u 1000 www-data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can't log in as this user because of the no-login directive in /etc/passwd file. Second thing, files in docker have group gid 1000 which doesn't exist...&lt;/p&gt;

&lt;p&gt;Probably if we exec artisan commands as the www-data user from the host machine everything would be ok, but I prefer to login into docker and operate from there.&lt;/p&gt;

&lt;p&gt;Group www-data had gid 33.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Solution is to change the www-data group gid, create a new user who has a login possibility, and assign it to the www-data group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN groupmod -g 1000 www-data
RUN useradd -u 1000 -ms /bin/bash -g www-data dockeruser
RUN chown -R dockeruser:www-data /var/www

USER dockeruser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila, everything works :)&lt;/p&gt;

</description>
      <category>docker</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
