aboutsummaryrefslogtreecommitdiff
path: root/_posts/posts
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-02-23 10:35:22 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-02-23 10:35:22 +0100
commit4abcce013c9ee3053badf2abda77190233066676 (patch)
tree450de7e8fed3c3c7501a9d2e2eb60a676bdfa09e /_posts/posts
parentcdf50cb2e3051200c6ea0628c318d66220b7d1a1 (diff)
downloadmitjafelicijan.com-4abcce013c9ee3053badf2abda77190233066676.tar.gz
Testing thoughts page
Diffstat (limited to '_posts/posts')
-rw-r--r--_posts/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md43
-rw-r--r--_posts/posts/2012-03-09-led-technology-not-so-eco.md34
-rw-r--r--_posts/posts/2013-10-24-wireless-sensor-networks.md55
-rw-r--r--_posts/posts/2015-11-10-software-development-pitfalls.md182
-rw-r--r--_posts/posts/2017-03-07-golang-profiling-simplified.md127
-rw-r--r--_posts/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md200
-rw-r--r--_posts/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md207
-rw-r--r--_posts/posts/2017-08-11-simple-iot-application.md608
-rw-r--r--_posts/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md332
-rw-r--r--_posts/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md416
-rw-r--r--_posts/posts/2019-10-14-simplifying-and-reducing-clutter.md60
-rw-r--r--_posts/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md109
-rw-r--r--_posts/posts/2020-03-22-simple-sse-based-pubsub-server.md455
-rw-r--r--_posts/posts/2020-03-27-create-placeholder-images-with-sharp.md103
-rw-r--r--_posts/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md109
-rw-r--r--_posts/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md112
-rw-r--r--_posts/posts/2020-05-05-remote-work.md73
-rw-r--r--_posts/posts/2020-08-15-systemd-disable-wake-onmouse.md74
-rw-r--r--_posts/posts/2020-09-06-esp-and-micropython.md226
-rw-r--r--_posts/posts/2020-09-08-bind-warning-on-login.md55
-rw-r--r--_posts/posts/2020-09-09-digitalocean-sync.md113
-rw-r--r--_posts/posts/2021-01-24-replacing-dropbox-with-s3.md115
-rw-r--r--_posts/posts/2021-01-25-goaccess.md205
-rw-r--r--_posts/posts/2021-06-26-simple-world-clock.md108
-rw-r--r--_posts/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md104
-rw-r--r--_posts/posts/2021-08-01-linux-cheatsheet.md288
-rw-r--r--_posts/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md277
-rw-r--r--_posts/posts/2021-12-25-running-golang-application-as-pid1.md348
-rw-r--r--_posts/posts/2021-12-30-wap-mobile-web-before-the-web.md203
-rw-r--r--_posts/posts/2022-06-30-trying-out-helix-editor.md55
-rw-r--r--_posts/posts/2022-07-05-what-would-dna-sound-if-synthesized.md365
-rw-r--r--_posts/posts/2022-10-06-state-of-web-technologies-in-year-2022.md297
-rw-r--r--_posts/posts/2022-10-16-that-sound-that-machine-makes-when-struggling.md67
-rw-r--r--_posts/posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md254
-rw-r--r--_posts/posts/2023-05-16-rekindling-my-love-for-programming.md75
-rw-r--r--_posts/posts/2023-05-23-i-was-wrong-about-git-workflows.md72
-rw-r--r--_posts/posts/2023-05-31-re-inventing-task-runner-that-i-actually-used-daily.md160
-rw-r--r--_posts/posts/2023-07-01-bringing-all-of-my-projects-together-under-one-umbrella.md282
-rw-r--r--_posts/posts/2023-07-08-who-knows-what-the-world-will-look-like-tomorrow.md101
-rw-r--r--_posts/posts/2023-11-05-elitist-attitudes-are-sapping-the-fun-from-programming.md97
-rw-r--r--_posts/posts/2024-02-11-k-mer.md141
41 files changed, 7307 insertions, 0 deletions
diff --git a/_posts/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md b/_posts/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md
new file mode 100644
index 0000000..de90494
--- /dev/null
+++ b/_posts/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md
@@ -0,0 +1,43 @@
1---
2title: Most likely to succeed in the year of 2011
3permalink: /most-likely-to-succeed-in-year-of-2011.html
4date: 2011-01-13T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10The year of 2010 was definitely the year of Geo-location. The market responded
11beautifully and lots of very cool services were launched. We all have to thank
12the mobile market for such extensive adoption. With new generations of mobile
13phones that are not only buffed with high-tech hardware but are also affordable.
14We can now manage tasks that were not so long time ago, almost Star Trek’ish.
15And all this had and has great influence on the destination to which we are
16going now.
17
18Reading all this articles about new innovation about new thriving technologies
19makes me wonder what’s the next step. The future is the mesh, like Lisa Gansky
20said in her book The Mesh.
21
22Many still have conservative views on distributed systems. The problems with
23security of information. Fear of not controlling every aspect of information
24flow. I am very opened to distributed systems and heterogeneous applications,
25and I think this is the correct and best way to proceed.
26
27This year will definitely be about communication platforms. Mobile to mobile.
28Machine to mobile and vice versa. All the tech is available and ready to put
29into action. Wireless is today’s new mantra. And the concept of semantic web is
30now ready for industry.
31
32Applications and developers now can gain access to new layers of systems and can
33prepare and build solutions to meet the high quality needs of market. The speed
34is everything now.
35
36My vote goes to “Machine to Machine” and “Embedded Systems”!
37
38- [Machine-to-Machine](http://en.wikipedia.org/wiki/Machine-to-Machine)
39- [The ultimate M2M communication protocol](http://www.bitxml.org/)
40- [COOS Project (connectivity initiative)](http://www.coosproject.org/maven-site/1.0.0/project-info.html)
41- [Community for machine-to-machine](http://m2m.com/index.jspa)
42- [Embedded system](http://en.wikipedia.org/wiki/Embedded_system)
43
diff --git a/_posts/posts/2012-03-09-led-technology-not-so-eco.md b/_posts/posts/2012-03-09-led-technology-not-so-eco.md
new file mode 100644
index 0000000..4c5fda3
--- /dev/null
+++ b/_posts/posts/2012-03-09-led-technology-not-so-eco.md
@@ -0,0 +1,34 @@
1---
2title: LED technology might not be as eco-friendly as you think
3permalink: /led-technology-not-so-eco.html
4date: 2012-03-09T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10There is a lot of talk about LED technology. It is beginning to infiltrate
11industry at a fast rate, and it’s a challenge for designers and also engineers.
12I wondered when a weakness will be revealed. Then I stomped on an article
13talking about harm in using LED technology. It looks like this magical
14technology is not so magical and eco-friendly.
15
16A new study from the University of California indicates that LED lights contain
17toxic metals, and should be produced, used and disposed of carefully. Besides
18the lead and nickel, the bulbs and their associated parts were also found to
19contain arsenic, copper, and other metals that have been linked to different
20cancers, neurological damage, kidney disease, hypertension, skin rashes and
21other illnesses in humans, and to ecological damage in waterways.
22
23Since then, I haven’t yet found any regulation for disposal of LED lights or any
24other regulation or standard. This might be a problem in the future. And it is a
25massive drawback. This might have quite an impact on consumer market.
26
27Nevertheless, there is a potential, and I am sure the market will adapt. I also
28hope I will be reading documents regarding solution for this concern soon.
29
30**Additional resources:**
31
32- [Recycling and Disposal of Light Bulbs](http://ezinearticles.com/?Recycling-and-Disposal-of-Light-Bulbs&id=1091304)
33- [How to Dispose of a Low-Energy Light Bulb](http://www.ehow.com/how_7483442_dispose-lowenergy-light-bulb.html)
34
diff --git a/_posts/posts/2013-10-24-wireless-sensor-networks.md b/_posts/posts/2013-10-24-wireless-sensor-networks.md
new file mode 100644
index 0000000..6eb3fe1
--- /dev/null
+++ b/_posts/posts/2013-10-24-wireless-sensor-networks.md
@@ -0,0 +1,55 @@
1---
2title: Wireless sensor networks
3permalink: /wireless-sensor-networks.html
4date: 2013-10-24T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Zigbee networks have this wonderful capability to self-heal, which means they
11can reorder connections between them if one of them is inoperable. This works
12our of the box when you deploy them. But you have to have in mind that achieving
13this is not as easy as you would think. None of it is plug&play. So to make
14your life a bit easier, here are some pointers which, I hope, will help you.
15
16- Be careful when you are ordering your equipment abroad. There are many rules
17 and regulations you need to comply before you get your Xbee radios. What they
18 do is they wait until you prove that you won’t use the technology for some
19 kind of evil take over control of the world project :). For this, they have
20 EAR (Export Administration Regulations) which basically means “This product
21 may require a license to export from the United States.”.
22- I don’t know if this applies for every country, but when we purchased our Xbee
23 radios from Mouser, this was mandatory! What we needed to do was to print out
24 a form and write information about our company and send them a copy via
25 email. With this document, we proved that we are a legitimate company.
26- When you complete your purchase and send all the documentation, you are not
27 clear yet. Then customs will take it from there :). There will be some
28 additional costs. Before purchasing, make sure you have as much information
29 about costs as possible. Because it can get costly in the end.
30- I suggest you use companies from your country. You can seriously cut your
31 costs. Here in Slovenia, the best option so far as I know is Farnell. And
32 based on my personal experience, they rock! All I need to say!
33- Make plans when ordering larger quantities. Do not, I say, do not make your
34 orders in December! :) Believe me! You will have problems with stock they can
35 provide for you. So, we were forced to buy some things from Mouser, which was
36 extremely painful because of all the regulations you need to obey when
37 importing goods from the USA.
38- Make sure that firmware version on your Xbee radios is exactly the same! Do
39 not get creative!!! I propose using templates. You can get template by
40 exporting settings/profile in X-CTU application. Make sure you have enabled
41 “Upgrade firmware” so you can be sure each radio has the same firmware.
42- And again: make plans! Plan everything! In months advanced! You will thank me
43 later :)
44- Test, test, test. Wireless networks can be tricky.
45
46If you are serious, I suggest you buy this book, Building Wireless Sensor
47Networks. You will get a glimpse of how networks work in lumens terms. It is a
48good starting point for everybody who wants to build wireless networks.
49
50**Additional resources:**
51
52- http://www.digi.com/aboutus/export/generalexportinfo
53- http://doresearch.stanford.edu/research-scholarship/export-controls/export-controlled-or-embargoed-countries-entities-and-persons
54- http://www.bis.doc.gov/licensing/exportingbasics.htm
55
diff --git a/_posts/posts/2015-11-10-software-development-pitfalls.md b/_posts/posts/2015-11-10-software-development-pitfalls.md
new file mode 100644
index 0000000..d7b9c1b
--- /dev/null
+++ b/_posts/posts/2015-11-10-software-development-pitfalls.md
@@ -0,0 +1,182 @@
1---
2title: Software development and my favorite pitfalls
3permalink: /software-development-pitfalls.html
4date: 2015-11-10T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Over the years I had the privilege to work on some very excited projects both in
11software development field and also in electronics field and every experience
12taught me some invaluable lessons about how NOT TO approach development. And
13through this post I will try to point out some absurd, outdated techniques I
14find the most annoying and damaging during a development cycle. There will be
15swearing because this topic really gets on my nerves and I never coherently
16tried to explain them in writing. So if I get heated up, please bear with me.
17
18As new methods of project management are emerging, underlying processes still
19stay old and outdated. This is mainly because we as people are unable to
20completely shift away from these approaches.
21
22I was always struggling with communication, and many times that cost me a
23relationship or two because I was not on the ball all the time. Through every
24experience, I became more convinced that I am the problem and never ever doubted
25that the problem may be that communication never evolved a single step from
26emails. And if you think for a second, not many things have changed around this
27topic. We just have different representations of email (message boards, chats,
28project management tools). And I believe this is the real issue we are facing
29now.
30
31There are many articles written about hyper connectivity and the effects that
32are a direct result of it. But mainstream does nothing towards it. We are just
33putting out fires, and we do nothing to prevent it. I am certain this will be a
34major source of grief in coming years. And what we all can do to avoid this is
35to change our mindset and experiment on our communication skills, development
36approaches. We need to maximize possible output that a person can give. And to
37achieve this we need to listen to them, encourage them. I know that not
38everybody is a naturally born leader, but with enough practice and encouragement
39they also can become active participants in leadership.
40
41There are many talks now about methodologies such as Scrum, Kanban, Cleanroom
42and they all fucking piss me of :). These are all boxes that imprison people and
43take away their freedom of thought. This is a straightforward mindfuck /
44amputation of creativity.
45
46Let me list a couple of things that I find really destructive and bad for a
47project and in a long run company.
48
49## Ping emails
50
51Ping emails are emails you have to write as soon as you receive an email. Its
52sole purpose is to inform the sender that you received their email, and you are
53working on it. Its result is only to calm down the sender that their task is
54being dealt with. It’s intent basically is, I did my job by sending you this
55email, so I am on clear grounds. I categorize this email as fuck you email.
56This is one of the most irritating types of emails I need to write. This is the
57ultimate control freak show you can experience, and it gives the sender a false
58feeling of control. Newsflash: We do not live in 1982 where there was a
59possibility that email never reached the destination. I really hate this from
60the bottom of my heart.
61
62They should be like: “Yes, I am fucking alive, and I am at your service my
63leash!”. I guess if I would reply like this, I wouldn’t have to write any more
64of this kind of messages.
65
66## Everybody is a project manager
67
68Well, this is a tough one. I noticed that as soon as you let people to give
69their suggestions, you are basically screwed. There is a truth in the saying:
70“Give low expectations and deliver little more than you promised.”.
71
72People tend to take a role of a manager as soon as they are presented with an
73opportunity. And by getting angry at them, you only provoke yourself. They are
74not at fault. You just need to tell them they are only giving suggestions and
75not tasks at the beginning and everything will be alright. But if you give them
76a feeling that they are in control, you will have immense problems explaining
77why their features are not in current release.
78
79Project mission must be always leading project requirements and any deviation
80from it will result in major project butchering. And by this, I mean that the
81project will get its own path, and you will be left with half done software that
82helps nobody. Clear mission goals and clean execution will allow you to develop
83software will clear intent.
84
85## We are never wrong
86
87I find this type of arrogance the worst. We must always conduct ourselves that
88we are infallible and cannot make mistakes. As soon as a procedure or process is
89established, there is no room for changes or improvements. This is the most
90idiotic thing someone can say of think. I think that processes need to involve
91and change over time. This is imperative and need to have in your organization
92if you want to improve and develop company. We all need to grow balls and change
93everything in order to adapt to current situations. Being a prisoner of
94predefined processes kills creativity.
95
96I am constantly trying new software for project managing and communication. I
97believe every team has its own dynamic, and it needs to be discovered
98organically and naturally through many experiments. By putting the team in a
99box, you are amputating their creativity and therefore minimizing their
100potential. But if you talk to an executive, you will mainly find archetypical
101thinking and a strong need to compartmentalize everything from business
102processes to resource management. And this type of management that often
103displays micromanagement techniques only works for short periods (couple of
104years) and then employees either leave the company or become basically retarded
105drones on autopilot.
106
107## Micromanaging
108
109This basically implies that everybody on the team is an idiot who needs to have
110a to-do list that they cannot write themselves. How about spoon-feeding the team
111at launch because besides the team leader, everybody must be a retarded idiot at
112best?
113
114I prefer milestones as they give developers much more freedom and creativity in
115developing and not waste their time checking some bizarre to-do list that was
116not even thought through. Projects constantly change throughout the development
117cycle, and all you are left at the end is a list of unchecked tasks and the
118wrath of management why they are not completed. Best WTF moment!
119
120## Human contact — no need for it!
121
122We are vigorously trying to eliminate physical contact by replacing short
123meetings with software, with no regards that we are not machines. Many times a
124simple 5-min meeting at morning can solve most of the problems. In rapid
125development, short bursts of man to man communication is possibly the best way
126to go.
127
128We now have all this software available, and all what we get out of it is a
129giant clusterfuck. An obstacle and not a solution. So, why we still use them?
130
131## MVP is killing innovation
132
133Many will disagree with me on this one, but I stand strong by this statement.
134What I noticed in my experience that all this buzz words around us only mislead
135and capture us in a circle of solving issues that already have a solution, but
136we are unable to see it without using some fancy word for it.
137
138The toughest thing to do for a developer is to minimize requirements. Well, this
139is though only for bad developers. Yes, I said it. There are many types of
140developers out there. And those unable to minimize feature scope are the ones
141you don’t need on your team. Their only goal is to solve problems that exist
142only in their heads. And then you have to argue with them, and waste energy on
143them, instead of developing your awesome product. They are a cancer and I
144suggest you cut them off.
145
146MVP as an idea is great, but sadly people don’t understand underlying
147philosophy, and they spent too much time focusing and fixating on something that
148every sane person with normal IQ will understand without some made up
149acronym. And the result is a lot of talking and barely no execution.
150
151Well, MVP is not directly killing innovation, but stupid people do when they try
152to understand it.
153
154## Pressure wasteland
155
156You must never allow to be pressured into confirming a deadline if you are not
157confident. We often feel a need that we are in service of others, which is true
158to some extent. But it is also true that others are in service to us to some
159extent. And we forget this all the time. We are all pressured all the time to
160make decisions just to calm other people down. And when they leave your office
161you experience WTF moment :) How the hell did they manage to fuck me up again?
162
163People need to realize that the more pressure you put on somebody, the less they
164will be able to do. So 5-min update email requests will only resolve in mental
165breakdown and inability to work that day. Constant poking is probably the only
166thing I lose my mind instantly. For all you that are doing this: “Stop bothering
167us with your insecurities and let us do our job. We will do it quicker and
168better without you breathing down our necks.”
169
170If this happens to me, I end up with no energy at the end. Don’t you get it?
171You will get much more from and out of me if you ask me like a human person and
172not your personal butler. On a long run, you are destroying your relationships
173and nobody would want to work with you. Your schizophrenic approach will damage
174only you in a long run. Nobody is anybody’s property.
175
176## Conclusion
177
178I am guilty of many things described in this post. And I find it hard sometimes
179to acknowledge this. And I lie to myself and try vigorously to find some
180explanation why I do these things. There is always space for growth. And maybe
181you will also find some of yourself in this post and realize what needs to
182change for you to evolve.
diff --git a/_posts/posts/2017-03-07-golang-profiling-simplified.md b/_posts/posts/2017-03-07-golang-profiling-simplified.md
new file mode 100644
index 0000000..aeea956
--- /dev/null
+++ b/_posts/posts/2017-03-07-golang-profiling-simplified.md
@@ -0,0 +1,127 @@
1---
2title: Golang profiling simplified
3permalink: /golang-profiling-simplified.html
4date: 2017-03-07T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Many posts have been written regarding profiling in Golang and I haven’t found
11proper tutorial regarding this. Almost all of them are missing some part of
12important information and it gets pretty frustrating when you have a deadline
13and are not finding simple distilled solution.
14
15Nevertheless, after searching and experimenting I have found a solution that
16works for me and probably should also for you.
17
18## Where are my pprof files?
19
20By default pprof files are generated in /tmp/ folder. You can override folder
21where this files are generated programmatically in your golang code as we will
22see below in example.
23
24## Why is my CPU profile empty?
25
26I have found out that sometimes CPU profile is empty because program was not
27executing long enough. Programs, that execute too quickly don’t produce pprof
28file in my cases. Well, file is generated but only contains 4KB of information.
29
30## Profiling
31
32As you can see from examples we are executing dummy_benchmark functions to
33ensure some sort of execution. Memory profiling can be done without such a
34“complex” function. But CPU profiling needs it.
35
36Both memory and CPU profiling examples are almost the same. Only parameters in
37main function when calling profile.Start are different. When we set
38profile.ProfilePath(“.”) we tell profiler to store pprof files in the same
39folder as our program.
40
41### Memory profiling
42
43```go
44package main
45
46import (
47 "fmt"
48 "time"
49 "github.com/pkg/profile"
50)
51
52func dummy_benchmark() {
53
54 fmt.Println("first set ...")
55 for i := 0; i < 918231333; i++ {
56 i *= 2
57 i /= 2
58 }
59
60 <-time.After(time.Second*3)
61
62 fmt.Println("sencond set ...")
63 for i := 0; i < 9182312232; i++ {
64 i *= 2
65 i /= 2
66 }
67}
68
69func main() {
70 defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
71 dummy_benchmark()
72}
73```
74
75### CPU profiling
76
77```go
78package main
79
80import (
81 "fmt"
82 "time"
83 "github.com/pkg/profile"
84)
85
86func dummy_benchmark() {
87
88 fmt.Println("first set ...")
89 for i := 0; i < 918231333; i++ {
90 i *= 2
91 i /= 2
92 }
93
94 <-time.After(time.Second*3)
95
96 fmt.Println("sencond set ...")
97 for i := 0; i < 9182312232; i++ {
98 i *= 2
99 i /= 2
100 }
101}
102
103func main() {
104 defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
105 dummy_benchmark()
106}
107```
108
109### Generating profiling reports
110
111```bash
112# memory profiling
113go build mem.go
114./mem
115go tool pprof -pdf ./mem mem.pprof > mem.pdf
116
117# cpu profiling
118go build cpu.go
119./cpu
120go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf
121```
122
123This will generate PDF document with visualized profile.
124
125- [Memory PDF profile example](/assets/posts/go-profiling/golang-profiling-mem.pdf)
126- [CPU PDF profile example](/assets/posts/go-profiling/golang-profiling-cpu.pdf)
127
diff --git a/_posts/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md b/_posts/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md
new file mode 100644
index 0000000..10aca0d
--- /dev/null
+++ b/_posts/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md
@@ -0,0 +1,200 @@
1---
2title: What I've learned developing ad server
3permalink: /what-i-ve-learned-developing-ad-server.html
4date: 2017-04-17T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10For the past year and half I have been developing native advertising server that
11contextually matches ads and displays them in different template forms on
12variety of websites. This project grew from serving thousands of ads per day to
13millions.
14
15The system is made from couple of core components:
16
17- API for serving ads,
18- Utils - cronjobs and queue management tools,
19- Dashboard UI.
20
21Initial release was using [MongoDB](https://www.mongodb.com/) for full-text
22search but was later replaced by [Elasticsearch](https://www.elastic.co/) for
23better CPU utilization and better search performance. This provided us with many
24amazing functionalities of [Elasticsearch](https://www.elastic.co/). You should
25check it out if you do any search related operations.
26
27Because the premise of the server is to provide native ad experience, they are
28rendered on the client side via simple templating engine. This ensures that ads
29can be displayed number of different ways based on the visual style of the
30page. And this makes JavaScript client library quite complex.
31
32So now that you know basic information about the product lets get into the
33lessons we learned.
34
35## Aggregate everything
36
37After beta version was released everything (impressions, clicks, etc) was
38written in nanosecond resolution in the database. At that time we were using
39[PostgreSQL](https://www.postgresql.org/) and database quickly grew way above
40200GB in disk space. And that was problematic. Statistics took disturbingly long
41time to aggregate. Also using indexes on stats table in database was no help
42after we reached 500 million datapoints.
43
44> There is a marketing product information and there is real life experience.
45And the tend to be quite the opposite.
46
47This was the reason that now everything is aggregated on daily basis and this
48data is then fed to Elastic in form of daily summary. With this we achieved we
49can now track many more dimensions such as zone, channel and platform
50information. And with this information we can now adapt occurrences of ads on
51specific places more precisely.
52
53We have also adapted [Redis](https://redis.io/) as a full-time citizen in our
54stack. Because Redis also stores information on a local disk we have some sort
55of backup if server would accidentally suffer some failure.
56
57All the real-time statistics for ad serving and redirecting is presented as
58counters in Redis instance and daily extracted and pushed to Elastic.
59
60## Measure everything
61
62The thing about software is that we really don't know how well it is performing
63under load until such load is presented. When testing locally everything is fine
64but when on production things tend to fall apart.
65
66As a solution for this we are measuring everything we can. Function execution
67time (by encapsulating functions with timers), server performance (cpu, memory,
68disk, etc), Nginx and [uWSGI](https://uwsgi-docs.readthedocs.io/) performance.
69We sacrifice a bit of performance for the sake of this information. And we store
70all this information for later analysis.
71
72**Example of function execution time**
73
74```json
75{
76 "get_final_filtered_ads": {
77 "counter": 1931250,
78 "avg": 0.0066143431,
79 "elapsed": 12773.9500310003
80 },
81 "store_keywords_statistics": {
82 "counter": 1931011,
83 "avg": 0.0004605267,
84 "elapsed": 889.2821669996
85 },
86 "match_by_context": {
87 "counter": 1931011,
88 "avg": 0.0055960716,
89 "elapsed": 10806.0758889999
90 },
91 "match_by_high_performance": {
92 "counter": 262,
93 "avg": 0.0152770229,
94 "elapsed": 4.00258
95 },
96 "store_impression_stats": {
97 "counter": 1931250,
98 "avg": 0.0006189991,
99 "elapsed": 1195.4419869999
100 }
101}
102```
103
104We have also started profiling with [cProfile](https://pymotw.com/2/profile/)
105and then visualizing with [KCachegrind](http://kcachegrind.sourceforge.net/).
106This provides much more detailed look into code execution.
107
108## Cache control is your friend
109
110Because we use Javascript library for rendering ads we rely on this script
111extensively and when in need we need to be able to change behavior of the script
112quickly.
113
114In our case we can not simply replace javascript url in html code. It usually
115takes a day or two for the guys who maintain sites to change code or add
116?ver=xxx attribute. And this makes rapid deployment and testing very difficult
117and time consuming. There is a limitation of how much you can test locally.
118
119We are now in the process of integrating [Google Tag
120Manager](https://www.google.com/analytics/tag-manager/) but couple of websites
121are developed on ASP.net platform that have some problems with tag manager. With
122a solution below we are certain that we are serving latest version of the
123script.
124
125And it only takes one mistake and users have the script cached and in case of
126caching it for 1 year you probably know where the problem is.
127
128```nginx
129# nginx ➜ /etc/nginx/sites-available/default
130location /static/ {
131 alias /path-to-static-content/;
132 autoindex off;
133 charset utf-8;
134 gzip on;
135 gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
136 location ~* \.(ico|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
137 expires 1y;
138 add_header Pragma public;
139 add_header Cache-Control "public";
140 }
141 location ~* \.(css|js|txt)$ {
142 expires 3600s;
143 add_header Pragma public;
144 add_header Cache-Control "public, must-revalidate";
145 }
146}
147```
148
149Also be careful when redirecting to url in your python code. We noticed that if
150we didn't precisely setup cache control and expire headers in response we didn't
151get the request on the server and therefore couldn't measure clicks. So when
152redirecting do as follows and there will be no problems.
153
154```python
155# python ➜ bottlepy web micro-framework
156response = bottle.HTTPResponse(status=302)
157response.set_header("Cache-Control", "no-store, no-cache, must-revalidate")
158response.set_header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
159response.set_header("Location", url)
160return response
161```
162
163> Cache control in browsers is quite aggressive and you need to be precise to
164avoid future problems. We learned that lesson the hard way.
165
166## Learn NGINX
167
168When deciding on a web server we went with Nginx as a reverse proxy for our
169applications. We adapted micro-service oriented architecture early in the
170project to ensure when we scale we can easily add additional servers to our
171cluster. And Nginx was crucial to perform load balancing and static content
172delivery.
173
174At first our config file was quite simple and later grew larger. After patching
175and adding new settings I sat down and learned more about the guts of Nginx.
176This proved to be very useful and we were able to squeeze much more out of our
177setup. So I advise you to take your time and read through the
178[documentation](https://nginx.org/en/docs/). This saved us a lot of headache.
179Googling for solutions only goes so far.
180
181## Use Redis/Memcached
182
183As explained above we are using caching basically for everything. It is the
184corner stone of our services. At first we were very careful about the quantity
185of things we stored in [Redis](https://redis.io/). But we later found out that
186the memory footprint is very low even when storing large amount of data in it.
187
188So we gradually increased our usage to caching whole HTML outputs of dashboard.
189This improved our performance in order of magnitude. And by using native TTL
190support this goes hand in hand with our needs.
191
192The reason why we choose [Redis](https://redis.io/) over
193[Memcached](https://memcached.org/) was the nature of scalability of Redis out
194of the box. But all this can be achieved with Memcached.
195
196## Conclusion
197
198There are a lot more details that could have been written and every single topic
199in here deserves it's own post but you probably got the idea about the problems
200we faced.
diff --git a/_posts/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md b/_posts/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
new file mode 100644
index 0000000..2e2ec70
--- /dev/null
+++ b/_posts/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
@@ -0,0 +1,207 @@
1---
2title: Profiling Python web applications with visual tools
3permalink: /profiling-python-web-applications-with-visual-tools.html
4date: 2017-04-21T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I have been profiling my software with KCachegrind for a long time now and I was
11missing this option when I am developing API's or other web services. I always
12knew that this is possible but never really took the time and dive into it.
13
14Before we begin there are some requirements. We will need to:
15
16- implement [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile) into our web app,
17- convert output to [callgrind](http://valgrind.org/docs/manual/cl-manual.html) format with [pyprof2calltree](https://pypi.python.org/pypi/pyprof2calltree/),
18- visualize data with [KCachegrind](http://kcachegrind.sourceforge.net/html/Home.html) or [Profiling Viewer](http://www.profilingviewer.com/).
19
20
21If you are using MacOS you should check out [Profiling
22Viewer](http://www.profilingviewer.com/) or
23[MacCallGrind](http://www.maccallgrind.com/).
24
25![KCachegrind](/assets/posts/python-profiling/kcachegrind.png){:loading="lazy"}
26
27We will be dividing this post into two main categories:
28
29- writing simple web-service,
30- visualize profile of this web-service.
31
32## Simple web-service
33
34Let's use virtualenv so we won't pollute our base system. If you don't have
35virtualenv installed on your system you can install it with pip command.
36
37```bash
38# let's install virtualenv globally
39$ sudo pip install virtualenv
40
41# let's also install pyprof2calltree globally
42$ sudo pip install pyprof2calltree
43
44# now we create project
45$ mkdir demo-project
46$ cd demo-project/
47
48# now let's create folder where we will store profiles
49$ mkdir prof
50
51# now we create empty virtualenv in venv/ folder
52$ virtualenv --no-site-packages venv
53
54# we now need to activate virtualenv
55$ source venv/bin/activate
56
57# you can check if virtualenv was correctly initialized by
58# checking where your python interpreter is located
59# if command bellow points to your created directory and not some
60# system dir like /usr/bin/python then everything is fine
61$ which python
62
63# we can check now if all is good ➜ if ok couple of
64# lines will be displayed
65$ pip freeze
66# appdirs==1.4.3
67# packaging==16.8
68# pyparsing==2.2.0
69# six==1.10.0
70
71# now we are ready to install bottlepy ➜ web micro-framework
72$ pip install bottle
73
74# you can deactivate virtualenv but you will then go
75# under system domain ➜ for now don't deactivate
76$ deactivate
77```
78
79We are now ready to write simple web service. Let's create file app.py and paste
80code bellow in this newly created file.
81
82```python
83# -*- coding: utf-8 -*-
84
85import bottle
86import random
87import cProfile
88
89app = bottle.Bottle()
90
91# this function is a decorator and encapsulates function
92# and performs profiling and then saves it to subfolder
93# prof/function-name.prof
94# in our example only awesome_random_number function will
95# be profiled because it has do_cprofile defined
96def do_cprofile(func):
97 def profiled_func(*args, **kwargs):
98 profile = cProfile.Profile()
99 try:
100 profile.enable()
101 result = func(*args, **kwargs)
102 profile.disable()
103 return result
104 finally:
105 profile.dump_stats("prof/" + str(func.__name__) + ".prof")
106 return profiled_func
107
108
109# we use profiling over specific function with including
110# @do_cprofile above function declaration
111@app.route("/")
112@do_cprofile
113def awesome_random_number():
114 awesome_random_number = random.randint(0, 100)
115 return "awesome random number is " + str(awesome_random_number)
116
117@app.route("/test")
118def test():
119 return "dummy test"
120
121if __name__ == '__main__':
122 bottle.run(
123 app = app,
124 host = "0.0.0.0",
125 port = 4000
126 )
127
128# run with 'python app.py'
129# open browser 'http://0.0.0.0:4000'
130```
131
132When browser hits awesome\_random\_number() function profile is created in prof/
133subfolder.
134
135## Visualize profile
136
137Now let's create callgrind format from this cProfile output.
138
139```bash
140$ cd prof/
141$ pyprof2calltree -i awesome_random_number.prof
142# this creates 'awesome_random_number.prof.log' file in the same folder
143```
144
145This file can be opened with visualizing tools listed above. In this case we
146will be using Profilling Viewer under MacOS. You can open image in new tab. As
147you can see from this example there is hierarchy of execution order of your
148code.
149
150![Profilling Viewer](/assets/posts/python-profiling/profiling-viewer.png){:loading="lazy"}
151
152> Make sure you convert output of the cProfile output every time you want to
153refresh and take a look at your possible optimizations because cProfile updates
154.prof file every time browser hits the function.
155
156This is just a simple example but when you are developing real-life applications
157this can be very illuminating, especially to see which parts of your code are
158bottlenecks and need to be optimized.
159
160## Update 2017-04-22
161
162Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this awesome
163web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/)
164that directly takes output from
165[cProfile](https://docs.python.org/2/library/profile.html#module-cProfile)
166module.
167
168<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="583880c1-002e-41ed-a373-020a0ef2cff9" data-embed-created="2017-04-22T19:46:54.810Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dgljhsb/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script>
169
170```bash
171# let's install it globally as well
172$ sudo pip install snakeviz
173
174# now let's visualize
175$ cd prof/
176$ snakeviz awesome_random_number.prof
177# this automatically opens browser window and
178# shows visualized profile
179```
180
181![SnakeViz](/assets/posts/python-profiling/snakeviz.png){:loading="lazy"}
182
183Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better
184way for installing pip software by targeting user level instead of using sudo.
185
186<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="f4f0459e-684d-441e-bebe-eb49b2f0a31d" data-embed-created="2017-04-22T19:46:10.874Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dglpzkx/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script>
187
188```bash
189# now we need to add this path to our $PATH variable
190# we do this my adding this line at the end of your
191# ~/.bashrc file
192PATH=$PATH:$HOME/.local/bin/
193
194# in order to use this new configuration you can close
195# and reopen terminal or reload .bashrc file
196$ source ~/.bashrc
197
198# now let's test if new directory is present in $PATH
199$ echo $PATH
200
201# now we can install on user level by adding --user
202# without use of sudo
203$ pip install snakeviz --user
204```
205
206Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can
207use [pipsi](https://github.com/mitsuhiko/pipsi).
diff --git a/_posts/posts/2017-08-11-simple-iot-application.md b/_posts/posts/2017-08-11-simple-iot-application.md
new file mode 100644
index 0000000..b552e8f
--- /dev/null
+++ b/_posts/posts/2017-08-11-simple-iot-application.md
@@ -0,0 +1,608 @@
1---
2title: Simple IOT application supported by real-time monitoring and data history
3permalink: /simple-iot-application.html
4date: 2017-08-11T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Initial thoughts
11
12I have been developing these kind of application for the better part of my last
135 years and people keep asking me how to approach developing such application
14and I will give a try explaining it here.
15
16IOT applications are really no different than any other kind of applications.
17We have data that needs to be collected and visualized in some form of tables or
18charts. The main difference here is that most of the times these data is
19collected by some kind of device foreign to developer that mainly operates in
20web domain. But fear not, it's not that different than writing some JavaScript.
21
22There are many devices able to transmit data via wireless or wired network by
23default but for the sake of example we will be using commonly known Arduino with
24wireless module already on the board → [Arduino
25MKR1000](https://store.arduino.cc/arduino-mkr1000).
26
27In order to make this little project as accessible to others as possible I will
28try to make it as inexpensive as possible. And by this I mean that I will avoid
29using hosted virtual servers and will be using my own laptop as a server. But
30you must buy Arduino MKR1000 to follow steps below. But if you would want to
31deploy this software I would suggest using
32[DigitalOcean](https://www.digitalocean.com) → smallest VPS is only per month
33making this one of the most affordable option out there. Please notice that this
34software will not run on stock web hosting that only supports LAMP (Linux,
35Apache, MySQL, and PHP).
36
37But before we begin please take notice that this is strictly experimental code
38and not well optimized and there are much better ways in handling some aspects
39of the application but that requires much deeper knowledge of technology that is
40not needed for an example like this.
41
42**Development steps**
43
441. Simple Python API that will receive and store incoming data.
452. Prototype C++ code that will read "sensor data" and transmit it to API.
463. Data visualization with charts → extends Python web application.
47
48Step 1. and 3. will share the same web application. One route will be dedicated
49to API and another to serving HTML with chart.
50
51Schema below represents what we will try to achieve and how different parts
52correlates to each other.
53
54![Overview](/assets/posts/iot-application/simple-iot-application-overview.svg){:loading="lazy"}
55
56## Simple Python API
57
58I have always been a fan of simplicity so we will be using [Bottle: Python Web
59Framework](https://bottlepy.org/docs/dev/). It is a single file web framework
60that seriously simplifies working with routes, templating and has built-in web
61server that satisfies our need in this case.
62
63First we need to install bottle package. This can be done by downloading
64```bottle.py``` and placing it in the root of your application or by using pip
65software ```pip install bottle --user```.
66
67If you are using Linux or MacOS then Python is already installed. If you will
68try to test this on Windows please install [Python for
69Windows](https://www.python.org/downloads/windows/). There may be some problems
70with path when you will try to launch ```python webapp.py``` so please take care
71of this before you continue.
72
73### Basic web application
74
75Most basic bottle application is quite simple. Paste code below in
76```webapp.py``` file and save.
77
78```python
79# -*- coding: utf-8 -*-
80
81import bottle
82
83# initializing bottle app
84app = bottle.Bottle()
85
86# triggered when / is accessed from browser
87# only accepts GET → no POST allowed
88@app.route("/", method=["GET"])
89def route_default():
90 return "howdy from python"
91
92# starting server on http://0.0.0.0:5000
93if __name__ == "__main__":
94 bottle.run(
95 app = app,
96 host = "0.0.0.0",
97 port = 5000,
98 debug = True,
99 reloader = True,
100 catchall = True,
101 )
102```
103
104To run this simple application you should open command prompt or terminal on
105your machine and go to the folder containing your file and type ```python
106webapp.py```. If everything goes ok then open your web browser and point it to
107```http://0.0.0.0:5000```.
108
109If you would like change the port of your application (like port 80) and not use
110root to run your app this will present a problem. The TCP/IP port numbers below
1111024 are privileged ports → this is a security feature. So in order of
112simplicity and security use a port number above 1024 like I have used port 5000.
113
114If this fails at any time please fix it before you continue, because nothing
115below will work otherwise.
116
117We use 0.0.0.0 as default host so that this app is available over your local
118network. If you find your local ip ```ifconfig``` and try accessing this site
119with your phone (if on same network/router as your machine) this should work as
120well (example of such ip ```http://192.168.1.15:5000```). This is a must have
121because Arduino will be accessing this application to send it's data.
122
123### Web application security
124
125There is a lot to be said about security and is a topic of many books. Of course
126all this can not be written here but to just establish some basic security → you
127should always use SSL with your application. Some fantastic free certificates
128are available by [Let's Encrypt - Free SSL/TLS
129Certificates](https://letsencrypt.org). With SSL certificate installed you
130should then make use of HTTP headers and send your "API key" via a header. If
131your key is send via header then this key is encrypted by SSL and send encrypted
132over the network. Never send your api keys by GET parameter like
133```http://example.com/?api_key=somekeyvalue```. The problem that this kind of
134sending presents is that this key is visible in logs and by network sniffers.
135
136There is a fantastic article describing some aspects about security: [11 Web
137Application Security Best
138Practices](https://www.keycdn.com/blog/web-application-security-best-practices/). Please
139check it out.
140
141### Simple API for writing data-points
142
143We will now be using boilerplate code from example above and extend it to be
144SQLite3 because it plays well with Python and can store quite large amount of
145able to write data received by API to local storage. For example use I will use
146data. I have been using it to collect gigabytes of data in a single database
147without any corruption or problems → your experience may vary.
148
149To avoid learning SQLite I will be using [Dataset: databases for lazy
150people](https://dataset.readthedocs.io/en/latest/index.html). This package
151abstracts SQL and simplifies writing and reading data from database. You should
152install this package with pip software ```pip install dataset --user```.
153
154Because API will use POST method I will be testing if code works correctly by
155using [Restlet Client for Google
156Chrome](https://chrome.google.com/webstore/detail/restlet-client-rest-api-t/aejoelaoggembcahagimdiliamlcdmfm).
157This software also allows you to set headers → for basic security with API_KEY.
158
159To quickly generate passwords or API keys I usually use this nifty website
160[RandomKeygen](https://randomkeygen.com/).
161
162Copy and paste code below over your previous code in file ```webapp.py```.
163
164```python
165# -*- coding: utf-8 -*-
166
167import time
168import bottle
169import random
170import dataset
171
172# initializing bottle app
173app = bottle.Bottle()
174
175# connects to sqlite database
176# check_same_thread=False allows using it in multi-threaded mode
177app.config["dsn"] = dataset.connect("sqlite:///data.db?check_same_thread=False")
178
179# api key that will be used in Arduino code
180app.config["api_key"] = "JtF2aUE5SGHfVJBCG5SH"
181
182# triggered when /api is accessed from browser
183# only accepts POST → no GET allowed
184@app.route("/api", method=["POST"])
185def route_default():
186 status = 400
187 ts = int(time.time()) # current timestamp
188 value = bottle.request.body.read() # data from device
189 api_key = bottle.request.get_header("Api_Key") # api key from header
190
191 # outputs to console received data for debug reason
192 print ">>> {} :: {}".format(value, api_key)
193
194 # if api_key is correct and value is present
195 # then writes attribute to point table
196 if api_key == app.config["api_key"] and value:
197 app.config["dsn"]["point"].insert(dict(ts=ts, value=value))
198 status = 200
199
200 # we only need to return status
201 return bottle.HTTPResponse(status=status, body="")
202
203# starting server on http://0.0.0.0:5000
204if __name__ == "__main__":
205 bottle.run(
206 app = app,
207 host = "0.0.0.0",
208 port = 5000,
209 debug = True,
210 reloader = True,
211 catchall = True,
212 )
213```
214
215To run this simply go to folder containing python file and run ```python
216webapp.py``` from terminal. If everything goes ok you should have simple API
217available via POST method on /api route.
218
219After testing the service with Restlet Client you should be able to view your
220data in a database file ```data.db```.
221
222![REST settings example](/assets/posts/iot-application/iot-rest-example.png){:loading="lazy"}
223
224You can also check the contents of new database file by using desktop client
225for SQLite → [DB Browser for SQLite](http://sqlitebrowser.org/).
226
227![SQLite database example](/assets/posts/iot-application/iot-sqlite-db.png){:loading="lazy"}
228
229Table structure is as simple as it can be. We have ts (timestamp) and value
230(value from Arduino). As you can see timestamp is generated on API side. If you
231would happen to have atomic clock on Arduino it would be then better to generate
232and send timestamp with the value. This would be particularity useful if we
233would be collecting sensor data at a higher frequency and then sending this data
234in bulk to API.
235
236If you will deploy this app with uWSGI and multi-threaded, use DSN (Data Source
237Name) url with ```?check_same_thread=False```.
238
239Ok, now that we have some sort of a working API with some basic security so
240unwanted people can not post data to your database can we proceed further and
241try to program Arduino to send data to API.
242
243## Sending data to API with Arduino MKR1000
244
245First of all you should have MKR1000 module and microUSB cable to proceed. If
246you have ever done any work with Arduino you should know that you also need
247[Arduino IDE](https://www.arduino.cc/en/Main/Software). On provided link you
248should be able to download and install IDE. Once that task is completed and you
249have successfully run blink example you should proceed to the next step.
250
251In order to use wireless capabilities of MKR1000 you need to first install
252[WiFi101 library](https://www.arduino.cc/en/Reference/WiFi101) in Arduino IDE.
253Please check before you install, you may already have it installed.
254
255Code below is a working example that sends data to API. Before you try to test
256your code make sure you have run Python web application. Then change settings
257for wifi, api endpoint and api_key. If by some reason code bellow doesn't work
258for you please leave a comment and I'll try to help.
259
260Once you have opened IDE and copied this code try to compile and upload it.
261Then open "Serial monitor" to see if any output is presented by Arduino.
262
263```c
264#include <WiFi101.h>
265
266// wifi settings
267char ssid[] = "ssid-name";
268char pass[] = "ssid-password";
269
270// api server enpoint
271char server[] = "192.168.6.22";
272int port = 5000;
273
274// api key that must be the same as the one in Python code
275String api_key = "JtF2aUE5SGHfVJBCG5SH";
276
277// frequency data is sent in ms - every 5 seconds
278int timeout = 1000 * 5;
279
280int status = WL_IDLE_STATUS;
281
282void setup() {
283
284 // initialize serial and wait for port to open:
285 Serial.begin(9600);
286 delay(1000);
287
288 // check for the presence of the shield
289 if (WiFi.status() == WL_NO_SHIELD) {
290 Serial.println("WiFi shield not present");
291 while (true);
292 }
293
294 // attempt to connect to wifi network
295 while (status != WL_CONNECTED) {
296 Serial.print("Attempting to connect to SSID: ");
297 Serial.println(ssid);
298 status = WiFi.begin(ssid, pass);
299 // wait 10 seconds for connection
300 delay(10000);
301 }
302
303 // output wifi status to serial monitor
304 Serial.print("SSID: ");
305 Serial.println(WiFi.SSID());
306
307 IPAddress ip = WiFi.localIP();
308 Serial.print("IP Address: ");
309 Serial.println(ip);
310
311 long rssi = WiFi.RSSI();
312 Serial.print("signal strength (RSSI):");
313 Serial.print(rssi);
314 Serial.println(" dBm");
315}
316
317void loop() {
318 WiFiClient client;
319
320 if (client.connect(server, port)) {
321
322 // I use random number generator for this example
323 // but you can use analog or digital inputs from arduino
324 String content = String(random(1000));
325
326 client.println("POST /api HTTP/1.1");
327 client.println("Connection: close");
328 client.println("Api-Key: " + api_key);
329 client.println("Content-Length: " + String(content.length()));
330 client.println();
331 client.println(content);
332
333 delay(100);
334 client.stop();
335 Serial.println("Data sent successfully ...");
336
337 } else {
338 Serial.println("Problem sending data ...");
339 }
340
341 // waits for x seconds and continue looping
342 delay(timeout);
343}
344```
345
346As seen from example you can notice that Arduino is generating random integer
347between [ 0 .. 1000 ]. You can easily replace this with a temperature sensor or
348any other kind of sensor.
349
350Now that we have API under the hood and Arduino is sending demo data we can now
351focus on data visualization.
352
353## Data visualization
354
355Before we continue we should examine our project folder structure. Currently we
356only have two files in our project:
357
358_simple-iot-app/_
359
360* _webapp.py_
361* _data.db_
362
363We will now add HTML template that will contain CSS and JavaScript code inline
364for the simplicity reason. And for the bottle framework to be able to scan root
365application folder for templates we will add ```bottle.TEMPLATE_PATH.insert(0,
366"./")``` in ```webapp.py```. By default bottle framework uses ```views/```
367subfolder to store templates. This is not the ideal situation and if you will
368use bottle to develop web applications you should use native behavior and store
369templates in it's predefined folder. But for the sake of example we will
370over-ride this. Be careful to fully replace your code with new code that is
371provided below. Avoid partially replacing code in file :) Also new code for
372reading data-points is provided in Python example below.
373
374First we add new route to our web application. It should be trigger when browser
375hits root of application ```http://0.0.0.0:5000/```. This route will do nothing
376more than render ```frontend.html``` template. This is done by ```return
377bottle.template("frontend.html")```. Check code below to further examine how
378exactly this is done.
379
380Now we will expand ```/api``` route and use different methods to write or read
381data-points. For writing data-point we will use POST method and for reading
382points we will use GET method. GET method will return JSON object with latest
383readings and historical data.
384
385There is a fantastic JavaScript library for plotting time-series charts called
386[MetricsGraphics.js](https://www.metricsgraphicsjs.org) that is based on
387[D3.js](https://d3js.org/) library for visualizing data.
388
389Data schema required by MetricsGraphics.js → to achieve this we need to
390transform data from database into this format:
391
392```json
393[
394 {
395 "date": "2017-08-11 01:07:20",
396 "value": 933
397 },
398 {
399 "date": "2017-08-11 01:07:30",
400 "value": 743
401 }
402]
403```
404
405Web application is now complete and we only need ```frontend.html``` that we
406will develop now. If you would try to start web app now and go to root app this
407will return error because we don't have frontend.html yet.
408
409```python
410# -*- coding: utf-8 -*-
411
412import time
413import bottle
414import json
415import datetime
416import random
417import dataset
418
419# initializing bottle app
420app = bottle.Bottle()
421
422# adds root directory as template folder
423bottle.TEMPLATE_PATH.insert(0, "./")
424
425# connects to sqlite database
426# check_same_thread=False allows using it in multi-threaded mode
427app.config["db"] = dataset.connect("sqlite:///data.db?check_same_thread=False")
428
429# api key that will be used in Arduino code
430app.config["api_key"] = "JtF2aUE5SGHfVJBCG5SH"
431
432# triggered when / is accessed from browser
433# only accepts GET → no POST allowed
434@app.route("/", method=["GET"])
435def route_default():
436 return bottle.template("frontend.html")
437
438# triggered when /api is accessed from browser
439# accepts POST and GET
440@app.route("/api", method=["GET", "POST"])
441def route_default():
442
443 # if method is POST then we write datapoint
444 if bottle.request.method == "POST":
445 status = 400
446 ts = int(time.time()) # current timestamp
447 value = bottle.request.body.read() # data from device
448 api_key = bottle.request.get_header("Api-Key") # api key from header
449
450 # outputs to console recieved data for debug reason
451 print ">>> {} :: {}".format(value, api_key)
452
453 # if api_key is correct and value is present
454 # then writes attribute to point table
455 if api_key == app.config["api_key"] and value:
456 app.config["db"]["point"].insert(dict(ts=ts, value=value))
457 status = 200
458
459 # we only need to return status
460 return bottle.HTTPResponse(status=status, body="")
461
462 # if method is GET then we read datapoint
463 else:
464 response = []
465 datapoints = app.config["db"]["point"].all()
466
467 for point in datapoints:
468 response.append({
469 "date": datetime.datetime.fromtimestamp(int(point["ts"])).strftime("%Y-%m-%d %H:%M:%S"),
470 "value": point["value"]
471 })
472
473 bottle.response.content_type = "application/json"
474 return json.dumps(response)
475
476# starting server on http://0.0.0.0:5000
477if __name__ == "__main__":
478 bottle.run(
479 app = app,
480 host = "0.0.0.0",
481 port = 5000,
482 debug = True,
483 reloader = True,
484 catchall = True,
485 )
486```
487
488And now finally we can implement ```frontend.html```. Create file with this name
489and copy code below. When you are done you can start web application. Steps for
490this part are listed below the code.
491
492```html
493<!DOCTYPE html>
494<html>
495
496 <head>
497 <meta charset="utf-8">
498 <title>Simple IOT application</title>
499 </head>
500
501 <body>
502
503 <h1>Simple IOT application</h1>
504
505 <div class="chart-placeholder">
506 <div id="chart"></div>
507 </div>
508
509 <!-- application main script -->
510 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
511 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
512 <script src="https://cdnjs.cloudflare.com/ajax/libs/metrics-graphics/2.11.0/metricsgraphics.min.js"></script>
513 <script>
514 function fetch_and_render() {
515 d3.json("/api", function(data) {
516 data = MG.convert.date(data, "date", "%Y-%m-%d %H:%M:%S");
517 MG.data_graphic({
518 data: data,
519 chart_type: "line",
520 full_width: true,
521 height: 270,
522 target: document.getElementById("chart"),
523 x_accessor: "date",
524 y_accessor: "value"
525 });
526 });
527 }
528 window.onload = function() {
529 // initial call for rendering
530 fetch_and_render();
531
532 // updates chart every 5 seconds
533 setInterval(function() {
534 fetch_and_render();
535 }, 5000);
536 }
537 </script>
538
539 <!-- application styles -->
540 <style>
541 body {
542 font: 13px sans-serif;
543 padding: 20px 50px;
544 }
545 .chart-placeholder {
546 border: 2px solid #ccc;
547 width: 100%;
548 user-select: none;
549 }
550 /* chart styles */
551 .mg-line1-color {
552 stroke: red;
553 stroke-width: 2;
554 }
555 .mg-main-area, .mg-main-line {
556 fill: #fff;
557 }
558 .mg-x-axis line, .mg-y-axis line {
559 stroke: #b3b2b2;
560 stroke-width: 1px;
561 }
562 </style>
563
564 </body>
565
566</html>
567```
568
569Now the folder structure should look like:
570
571_simple-iot-app/_
572
573* _webapp.py_
574* _data.db_
575* _frontend.html_
576
577Ok, lets now start application and start feeding it data.
578
5791. ```python webapp.py```
5802. connect Arduino MKR1000 to power source
5813. open browser and go to ```http://0.0.0.0:5000```
582
583If everything goes well you should be seeing new data-points rendered on chart
584every 5 seconds.
585
586If you navigate to ```http://0.0.0.0:5000``` you should see rendered chart as
587shown on picture below.
588
589![Application output](/assets/posts/iot-application/iot-app-output.png){:loading="lazy"}
590
591Complete application with all the code is available for
592[download](/assets/posts/iot-application/simple-iot-application.zip).
593
594## Conclusion
595
596I hope this clarifies some aspects of IOT application development. Of course
597this is a minimal example and is far from what can be done in real life with
598some further dive into other technologies.
599
600If you would like to continue exploring IOT world here are some interesting
601resources for you to examine:
602
603* [Reading Sensors with an Arduino](https://www.allaboutcircuits.com/projects/reading-sensors-with-an-arduino/)
604* [MQTT 101 – How to Get Started with the lightweight IoT Protocol](http://www.hivemq.com/blog/how-to-get-started-with-mqtt)
605* [Stream Updates with Server-Sent Events](https://www.html5rocks.com/en/tutorials/eventsource/basics/)
606* [Internet of Things (IoT) Tutorials](http://www.tutorialspoint.com/internet_of_things/)
607
608Any comment or additional ideas are welcomed in comments below.
diff --git a/_posts/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md b/_posts/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md
new file mode 100644
index 0000000..d29bd09
--- /dev/null
+++ b/_posts/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md
@@ -0,0 +1,332 @@
1---
2title: Using DigitalOcean Spaces Object Storage with FUSE
3permalink: /using-digitalocean-spaces-object-storage-with-fuse.html
4date: 2018-01-16T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Couple of months ago [DigitalOcean](https://www.digitalocean.com) introduced new
11product called
12[Spaces](https://blog.digitalocean.com/introducing-spaces-object-storage/) which
13is Object Storage very similar to Amazon's S3. This really peaked my interest,
14because this was something I was missing and even the thought of going over the
15internet for such functionality was in no interest to me. Also in fashion with
16their previous pricing this also is very cheap and pricing page is a no-brainer
17compared to AWS or GCE. [Prices are clearly and precisely defined and
18outlined](https://www.digitalocean.com/pricing/). You must love them for that
19:)
20
21## Initial requirements
22
23* Is it possible to use them as a mounted drive with FUSE? (tl;dr YES)
24* Will the performance degrade over time and over different sizes of objects?
25 (tl;dr NO&YES)
26* Can storage be mounted on multiple machines at the same time and be writable?
27 (tl;dr YES)
28
29> Let me be clear. This scripts I use are made just for benchmarking and are not
30> intended to be used in real-life situations. Besides that, I am looking into
31> using this approaches but adding caching service in front of it and then
32> dumping everything as an object to storage. This could potentially be some
33> interesting post of itself. But in case you would need real-time data without
34> eventual consistency please take this scripts as they are: not usable in such
35> situations.
36
37## Is it possible to use them as a mounted drive with FUSE?
38
39Well, actually they can be used in such manor. Because they are similar to [AWS
40S3](https://aws.amazon.com/s3/) many tools are available and you can find many
41articles and [Stackoverflow items](https://stackoverflow.com/search?q=s3+fuse).
42
43To make this work you will need DigitalOcean account. If you don't have one you
44will not be able to test this code. But if you have an account then you go and
45[create new
46Droplet](https://cloud.digitalocean.com/droplets/new?size=s-1vcpu-1gb&region=ams3&distro=debian&distroImage=debian-9-x64&options=private_networking,install_agent).
47If you click on this link you will already have preselected Debian 9 with
48smallest VM option.
49
50* Please be sure to add you SSH key, because we will login to this machine
51 remotely.
52* If you change your region please remember which one you choose because we will
53 need this information when we try to mount space to our machine.
54
55Instuctions on how to use SSH keys and how to setup them are available in
56article [How To Use SSH Keys with DigitalOcean
57Droplets](https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets).
58
59![DigitalOcean Droplets](/assets/posts/do-fuse/fuse-droplets.png){:loading="lazy"}
60
61After we created Droplet it's time to create new Space. This is done by clicking
62on a button [Create](https://cloud.digitalocean.com/spaces/new) (right top
63corner) and selecting Spaces. Choose pronounceable ```Unique name``` because we
64will use it in examples below. You can either choose Private or Public, it
65doesn't matter in our case. And you can always change that in the future.
66
67When you have created new Space we should [generate Access
68key](https://cloud.digitalocean.com/settings/api/tokens). This link will guide
69to the page when you can generate this key. After you create new one, please
70save provided Key and Secret because Secret will not be shown again.
71
72![DigitalOcean Spaces](/assets/posts/do-fuse/fuse-spaces.png){:loading="lazy"}
73
74Now that we have new Space and Access key we should SSH into our machine.
75
76```bash
77# replace IP with the ip of your newly created droplet
78ssh root@IP
79
80# this will install utilities for mounting storage objects as FUSE
81apt install s3fs
82
83# we now need to provide credentials (access key we created earlier)
84# replace KEY and SECRET with your own credentials but leave the colon between them
85# we also need to set proper permissions
86echo "KEY:SECRET" > .passwd-s3fs
87chmod 600 .passwd-s3fs
88
89# now we mount space to our machine
90# replace UNIQUE-NAME with the name you choose earlier
91# if you choose different region for your space be careful about -ourl option (ams3)
92s3fs UNIQUE-NAME /mnt/ -ourl=https://ams3.digitaloceanspaces.com -ouse_cache=/tmp
93
94# now we try to create a file
95# once you mount it may take a couple of seconds to retrieve data
96echo "Hello cruel world" > /mnt/hello.txt
97```
98
99After all this you can return to your browser and go to [DigitalOcean
100Spaces](https://cloud.digitalocean.com/spaces) and click on your created
101space. If file hello.txt is present you have successfully mounted space to your
102machine and wrote data to it.
103
104I choose the same region for my Droplet and my Space but you don't have to. You
105can have different regions. What this actually does to performance I don't know.
106
107Additional information on FUSE:
108
109* [Github project page for s3fs](https://github.com/s3fs-fuse/s3fs-fuse)
110* [FUSE - Filesystem in Userspace](https://en.wikipedia.org/wiki/Filesystem_in_Userspace)
111
112## Will the performance degrade over time and over different sizes of objects?
113
114For this task I didn't want to just read and write text files or uploading
115images. I actually wanted to figure out if using something like SQlite is viable
116in this case.
117
118### Measurement experiment 1: File copy
119
120```bash
121# first we create some dummy files at different sizes
122dd if=/dev/zero of=10KB.dat bs=1024 count=10 #10KB
123dd if=/dev/zero of=100KB.dat bs=1024 count=100 #100KB
124dd if=/dev/zero of=1MB.dat bs=1024 count=1024 #1MB
125dd if=/dev/zero of=10MB.dat bs=1024 count=10240 #10MB
126
127# now we set time command to only return real
128TIMEFORMAT=%R
129
130# now lets test it
131(time cp 10KB.dat /mnt/) |& tee -a 10KB.results.txt
132
133# and now we automate
134# this will perform the same operation 100 times
135# this will output results into separated files based on objecty size
136n=0; while (( n++ < 100 )); do (time cp 10KB.dat /mnt/10KB.$n.dat) |& tee -a 10KB.results.txt; done
137n=0; while (( n++ < 100 )); do (time cp 100KB.dat /mnt/100KB.$n.dat) |& tee -a 100KB.results.txt; done
138n=0; while (( n++ < 100 )); do (time cp 1MB.dat /mnt/1MB.$n.dat) |& tee -a 1MB.results.txt; done
139n=0; while (( n++ < 100 )); do (time cp 10MB.dat /mnt/10MB.$n.dat) |& tee -a 10MB.results.txt; done
140```
141
142Files of size 100MB were not successfully transferred and ended up displaying
143error (cp: failed to close '/mnt/100MB.1.dat': Operation not permitted).
144
145As I suspected, object size is not really that important. Sadly I don't have the
146time to test performance over periods of time. But if some of you would do it
147please send me your data. I would be interested in seeing results.
148
149**Here are plotted results**
150
151You can download [raw result here](/assets/posts/do-fuse/copy-benchmarks.tsv).
152Measurements are in seconds.
153
154<script src="//cdn.plot.ly/plotly-latest.min.js"></script>
155<div id="copy-benchmarks"></div>
156<script>
157(function(){
158 var request = new XMLHttpRequest();
159 request.open("GET", "/assets/posts/do-fuse/copy-benchmarks.tsv", true);
160 request.onload = function() {
161 if (request.status >= 200 && request.status < 400) {
162 var payload = request.responseText.trim();
163 var tsv = payload.split("\n");
164 for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); }
165 var traces = [];
166 var headers = tsv[0];
167 tsv.shift();
168 Array.prototype.forEach.call(headers, function(el, idx) {
169 var x = [];
170 var y = [];
171 for (var j=0; j<tsv.length; j++) {
172 x.push(j);
173 y.push(parseFloat(tsv[j][idx].replace(",", ".")));
174 }
175 traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } });
176 });
177 var copy = Plotly.newPlot("copy-benchmarks", traces, { legend: {"orientation": "h"}, height: 400, margin: { l: 40, r: 0, b: 20, t: 30, pad: 0 }, yaxis: { title: "execution time in seconds", titlefont: { size: 12 } }, xaxis: { title: "fn(i)", titlefont: { size: 12 } } });
178 } else { }
179 };
180 request.onerror = function() { };
181 request.send(null);
182})();
183</script>
184
185As far as these tests show, performance is quite stable and can be predicted
186which is fantastic. But this is a small test and spans only over couple of
187hours. So you should not completely trust them.
188
189### Measurement experiment 2: SQLite performanse
190
191I was unable to use database file directly from mounted drive so this is a no-go
192as I suspected. So I executed code below on a local disk just to get some
193benchmarks. I inserted 1000 records with DROPTABLE, CREATETABLE, INSERTMANY,
194FETCHALL, COMMIT for 1000 times to generate statistics. As you can see
195performance of SQLite is quite amazing. You could then potentially just copy
196file to mounted drive and be done with it.
197
198```python
199import time
200import sqlite3
201import sys
202
203if len(sys.argv) < 3:
204 print("usage: python sqlite-benchmark.py DB_PATH NUM_RECORDS REPEAT")
205 exit()
206
207def data_iter(x):
208 for i in range(x):
209 yield "m" + str(i), "f" + str(i*i)
210
211header_line = "%s\t%s\t%s\t%s\t%s\n" % ("DROPTABLE", "CREATETABLE", "INSERTMANY", "FETCHALL", "COMMIT")
212with open("sqlite-benchmarks.tsv", "w") as fp:
213 fp.write(header_line)
214
215start_time = time.time()
216conn = sqlite3.connect(sys.argv[1])
217c = conn.cursor()
218end_time = time.time()
219result_time = CONNECT = end_time - start_time
220print("CONNECT: %g seconds" % (result_time))
221
222start_time = time.time()
223c.execute("PRAGMA journal_mode=WAL")
224c.execute("PRAGMA temp_store=MEMORY")
225c.execute("PRAGMA synchronous=OFF")
226result_time = PRAGMA = end_time - start_time
227print("PRAGMA: %g seconds" % (result_time))
228
229for i in range(int(sys.argv[3])):
230 print("#%i" % (i))
231
232 start_time = time.time()
233 c.execute("drop table if exists test")
234 end_time = time.time()
235 result_time = DROPTABLE = end_time - start_time
236 print("DROPTABLE: %g seconds" % (result_time))
237
238 start_time = time.time()
239 c.execute("create table if not exists test(a,b)")
240 end_time = time.time()
241 result_time = CREATETABLE = end_time - start_time
242 print("CREATETABLE: %g seconds" % (result_time))
243
244 start_time = time.time()
245 c.executemany("INSERT INTO test VALUES (?, ?)", data_iter(int(sys.argv[2])))
246 end_time = time.time()
247 result_time = INSERTMANY = end_time - start_time
248 print("INSERTMANY: %g seconds" % (result_time))
249
250 start_time = time.time()
251 c.execute("select count(*) from test")
252 res = c.fetchall()
253 end_time = time.time()
254 result_time = FETCHALL = end_time - start_time
255 print("FETCHALL: %g seconds" % (result_time))
256
257 start_time = time.time()
258 conn.commit()
259 end_time = time.time()
260 result_time = COMMIT = end_time - start_time
261 print("COMMIT: %g seconds" % (result_time))
262
263 print
264 log_line = "%f\t%f\t%f\t%f\t%f\n" % (DROPTABLE, CREATETABLE, INSERTMANY, FETCHALL, COMMIT)
265 with open("sqlite-benchmarks.tsv", "a") as fp:
266 fp.write(log_line)
267
268start_time = time.time()
269conn.close()
270end_time = time.time()
271result_time = CLOSE = end_time - start_time
272print("CLOSE: %g seconds" % (result_time))
273```
274
275You can download [raw result here](/assets/posts/do-fuse/sqlite-benchmarks.tsv). And
276again, these results are done on a local block storage and do not represent
277capabilities of object storage. With my current approach and state of the test
278code these can not be done. I would need to make Python code much more robust
279and check locking etc.
280
281<div id="sqlite-benchmarks"></div>
282<script>
283(function(){
284 var request = new XMLHttpRequest();
285 request.open("GET", "/assets/posts/do-fuse/sqlite-benchmarks.tsv", true);
286 request.onload = function() {
287 if (request.status >= 200 && request.status < 400) {
288 var payload = request.responseText.trim();
289 var tsv = payload.split("\n");
290 for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); }
291 var traces = [];
292 var headers = tsv[0];
293 tsv.shift();
294 Array.prototype.forEach.call(headers, function(el, idx) {
295 var x = [];
296 var y = [];
297 for (var j=0; j<tsv.length; j++) {
298 x.push(j);
299 y.push(parseFloat(tsv[j][idx].replace(",", ".")));
300 }
301 traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } });
302 });
303 var sqlite = Plotly.newPlot("sqlite-benchmarks", traces, { legend: {"orientation": "h"}, height: 400, margin: { l: 50, r: 0, b: 20, t: 30, pad: 0 }, yaxis: { title: "execution time in seconds", titlefont: { size: 12 } } });
304 } else { }
305 };
306 request.onerror = function() { };
307 request.send(null);
308})();
309</script>
310
311## Can storage be mounted on multiple machines at the same time and be writable?
312
313Well, this one didn't take long to test. And the answer is **YES**. I mounted
314space on both machines and measured same performance on both machines. But
315because file is downloaded before write and then uploaded on complete there
316could potentially be problems is another process is trying to access the same
317file.
318
319## Observations and conslusion
320
321Using Spaces in this way makes it easier to access and manage files. But besides
322that you would need to write additional code to make this one play nice with you
323applications.
324
325Nevertheless, this was extremely simple to setup and use and this is just
326another excellent product in DigitalOcean product line. I found this exercise
327very valuable and am thinking about implementing some sort of mechanism for
328SQLite, so data can be stored on Spaces and accessed by many VM's. For a project
329where data doesn't need to be accessible in real-time and can have couple of
330minutes old data this would be very interesting. If any of you find this
331proposal interesting please write in a comment box below or shoot me an email
332and I will keep you posted.
diff --git a/_posts/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md b/_posts/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
new file mode 100644
index 0000000..6980ed1
--- /dev/null
+++ b/_posts/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
@@ -0,0 +1,416 @@
1---
2title: Encoding binary data into DNA sequence
3permalink: /encoding-binary-data-into-dna-sequence.html
4date: 2019-01-03T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Initial thoughts
11
12Imagine a world where you could go outside and take a leaf from a tree and put
13it through your personal DNA sequencer and get data like music, videos or
14computer programs from it. Well, this is all possible now. It was not done on a
15large scale because it is quite expensive to create DNA strands but it's
16possible.
17
18Encoding data into DNA sequence is relatively simple process once you understand
19the relationship between binary data and nucleotides and scientists have been
20making large leaps in this field in order to provide viable long-term storage
21solution for our data that would potentially survive our specie if case of
22global disaster. We could imprint all the world's knowledge into plants and
23ensure the survival of our knowledge.
24
25More optimistic usage for this technology would be easier storage of ever
26growing data we produce every day. Once machines for sequencing DNA become fast
27enough and cheaper this could mean the next evolution of storing data and
28abandoning classical hard and solid state drives in data warehouses.
29
30As we currently stand this is still not viable but it is quite an amazing and
31cool technology.
32
33My interests in this field are purely in encoding processes and experimental
34testing mainly because I don't have the access to this expensive machines. My
35initial goal was to create a toolkit that can be used by everybody to encode
36their data into a proper DNA sequence.
37
38## Glossary
39
40**deoxyribose** A five-carbon sugar molecule with a hydrogen atom rather than a
41hydroxyl group in the 2′ position; the sugar component of DNA nucleotides.
42
43**double helix** The molecular shape of DNA in which two strands of nucleotides
44wind around each other in a spiral shape.
45
46**nitrogenous base** A nitrogen-containing molecule that acts as a base; often
47referring to one of the purine or pyrimidine components of nucleic acids.
48
49**phosphate group** A molecular group consisting of a central phosphorus atom
50bound to four oxygen atoms.
51
52**RGB** The RGB color model is an additive color model in which red, green and
53blue light are added together in various ways to reproduce a broad array of
54colors.
55
56**GCC** The GNU Compiler Collection is a compiler system produced by the GNU
57Project supporting various programming languages.
58
59## Data encoding
60
61**TL;DR:** Encoding involves the use of a code to change original data into a
62form that can be used by an external process.
63
64Encoding is the process of converting data into a format required for a number
65of information processing needs, including:
66
67- Program compiling and execution
68- Data transmission, storage and compression/decompression
69- Application data processing, such as file conversion
70
71Encoding can have two meanings:
72
73- In computer technology, encoding is the process of applying a specific code,
74 such as letters, symbols and numbers, to data for conversion into an
75 equivalent cipher.
76- In electronics, encoding refers to analog to digital conversion.
77
78## Quick history of DNA
79
80- **1869** - Friedrich Miescher identifies "nuclein".
81- **1900s** - The Eugenics Movement.
82- **1900** – Mendel's theories are rediscovered by researchers.
83- **1944** - Oswald Avery identifies DNA as the 'transforming principle'.
84- **1952** - Rosalind Franklin photographs crystallized DNA fibres.
85- **1953** - James Watson and Francis Crick discover the double helix structure of DNA.
86- **1965** - Marshall Nirenberg is the first person to sequence the bases in each codon.
87- **1983** - Huntington's disease is the first mapped genetic disease.
88- **1990** - The Human Genome Project begins.
89- **1995** - Haemophilus Influenzae is the first bacterium genome sequenced.
90- **1996** - Dolly the sheep is cloned.
91- **1999** - First human chromosome is decoded.
92- **2000** – Genetic code of the fruit fly is decoded.
93- **2002** – Mouse is the first mammal to have its genome decoded.
94- **2003** – The Human Genome Project is completed.
95- **2013** – DNA Worldwide and Eurofins Forensic discover identical twins have differences in their genetic makeup.
96
97## What is DNA?
98
99Deoxyribonucleic acid, a self-replicating material which is **present in nearly
100all living organisms** as the main constituent of chromosomes. It is the
101**carrier of genetic information**.
102
103> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood,
104> the carbon in our apple pies were made in the interiors of collapsing stars.
105> We are made of starstuff.
106> **-- Carl Sagan, Cosmos**
107
108The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases
109(cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate.
110Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine
111bases. The sugar and the base together are called a nucleoside.
112
113![DNA](/assets/posts/dna-sequence/dna-basics.jpg){:loading="lazy"}
114
115*DNA (a) forms a double stranded helix, and (b) adenine pairs with thymine and
116cytosine pairs with guanine. (credit a: modification of work by Jerome Walker,
117Dennis Myts)*
118
119## Encode binary data into DNA sequence
120
121As an input file you can use any file you want:
122
123- ASCII files,
124- Compiled programs,
125- Multimedia files (MP3, MP4, MVK, etc),
126- Images,
127- Database files,
128- etc.
129
130Note: If you would copy all the bytes from RAM to file or pipe data to file you
131could encode also this data as long as you provide file pointer to the encoder.
132
133### Basic Encoding
134
135As already mentioned, the Basic Encoding is based on a simple mapping. Since DNA
136is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually
137referred using the first letter). Using this technique we can encode
138
139<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 -907.9672135000189 11313.37788460873 1185.0382429179317" style="width: 26.259ex; height: 2.721ex; vertical-align: -0.68ex; margin: 1px 0px;"><g stroke="black" fill="black" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-6C"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-6F" x="303" y="0"/><g transform="translate(793,0)"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-67"/><use transform="scale(0.7071067811865476)" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-32" x="681" y="-213"/></g><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-28" x="1732" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-34" x="2126" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-29" x="2631" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-3D" x="3302" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-6C" x="4363" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-6F" x="4666" y="0"/><g transform="translate(5156,0)"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-67"/><use transform="scale(0.7071067811865476)" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-32" x="681" y="-213"/></g><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-28" x="6095" y="0"/><g transform="translate(6489,0)"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-32"/><use transform="scale(0.7071067811865476)" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-32" x="714" y="583"/></g><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-29" x="7451" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-3D" x="8123" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMAIN-32" x="9184" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-62" x="9689" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-69" x="10123" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-74" x="10473" y="0"/><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#MJMATHI-73" x="10839" y="0"/></g><defs id="MathJax_SVG_glyphs"><path id="MJSZ2-2211" stroke-width="10" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z"/><path id="MJMATHI-69" stroke-width="10" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"/><path id="MJMAIN-3D" stroke-width="10" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"/><path id="MJMAIN-30" stroke-width="10" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"/><path id="MJMATHI-6E" stroke-width="10" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"/><path id="MJMAIN-28" stroke-width="10" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"/><path id="MJMAIN-2B" stroke-width="10" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"/><path id="MJMAIN-31" stroke-width="10" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"/><path id="MJMAIN-29" stroke-width="10" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"/><path id="MJMAIN-32" stroke-width="10" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"/><path id="MJMATHI-6C" stroke-width="10" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"/><path id="MJMATHI-6F" stroke-width="10" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"/><path id="MJMATHI-67" stroke-width="10" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"/><path id="MJMAIN-34" stroke-width="10" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"/><path id="MJMATHI-62" stroke-width="10" d="M73 647Q73 657 77 670T89 683Q90 683 161 688T234 694Q246 694 246 685T212 542Q204 508 195 472T180 418L176 399Q176 396 182 402Q231 442 283 442Q345 442 383 396T422 280Q422 169 343 79T173 -11Q123 -11 82 27T40 150V159Q40 180 48 217T97 414Q147 611 147 623T109 637Q104 637 101 637H96Q86 637 83 637T76 640T73 647ZM336 325V331Q336 405 275 405Q258 405 240 397T207 376T181 352T163 330L157 322L136 236Q114 150 114 114Q114 66 138 42Q154 26 178 26Q211 26 245 58Q270 81 285 114T318 219Q336 291 336 325Z"/><path id="MJMATHI-74" stroke-width="10" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"/><path id="MJMATHI-73" stroke-width="10" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"/></defs></svg>
140
141using a single nucleotide. In this way, we are able to use the 4 bases that
142compose the DNA strand to encode each byte of data.
143
144| Two bits | Nucleotides |
145| -------- | ---------------- |
146| 00 | **A** (Adenine) |
147| 10 | **G** (Guanine) |
148| 01 | **C** (Cytosine) |
149| 11 | **T** (Thymine) |
150
151With this in mind we can simply encode any data by using two-bit to Nucleotides
152conversion.
153
154```python
155{ Algorithm 1: Naive byte array to DNA encode }
156procedure EncodeToDNASequence(f) string
157begin
158 enc string
159 while not eof(f) do
160 c byte := buffer[0] { Read 1 byte from buffer }
161 bin integer := sprintf('08b', c) { Convert to string binary }
162 for e in range[0, 2, 4, 6] do
163 if e[0] == 48 and e[1] == 48 then { 0x00 - A (Adenine) }
164 enc += 'A'
165 else if e[0] == 48 and e[1] == 49 then { 0x01 - G (Guanine) }
166 enc += 'G'
167 else if e[0] == 49 and e[1] == 48 then { 0x10 - C (Cytosine) }
168 enc += 'C'
169 else if e[0] == 49 and e[1] == 49 then { 0x11 - T (Thymine) }
170 enc += 'T'
171 return enc { Return DNA sequence }
172end
173```
174
175Another encoding would be **Goldman encoding**. Using this encoding helps with
176Nonsense mutation (amino acids replaced by a stop codon) that occurs and is the
177most problematic during translation because it leads to truncated amino acid
178sequences, which in turn results in truncated proteins.
179
180[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU)
181
182### FASTA file format
183
184In bioinformatics, FASTA format is a text-based format for representing either
185nucleotide sequences or peptide sequences, in which nucleotides or amino acids
186are represented using single-letter codes. The format also allows for sequence
187names and comments to precede the sequences. The format originates from the
188FASTA software package, but has now become a standard in the field of
189bioinformatics.
190
191The first line in a FASTA file started either with a ">" (greater-than) symbol
192or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent lines
193starting with a semicolon would be ignored by software. Since the only comment
194used was the first, it quickly became used to hold a summary description of the
195sequence, often starting with a unique library accession number, and with time
196it has become commonplace to always use ">" for the first line and to not use
197";" comments (which would otherwise be ignored).
198
199```txt
200;LCBO - Prolactin precursor - Bovine
201; a sample sequence in FASTA format
202MDSKGSSQKGSRLLLLLVVSNLLLCQGVVSTPVCPNGPGNCQVSLRDLFDRAVMVSHYIHDLSS
203EMFNEFDKRYAQGKGFITMALNSCHTSSLPTPEDKEQAQQTHHEVLMSLILGLLRSWNDPLYHL
204VTEVRGMKGAPDAILSRAIEIEEENKRLLEGMEMIFGQVIPGAKETEPYPVWSGLPSLQTKDED
205ARYSAFYNLLHCLRRDSSKIDTYLKLLNCRIIYNNNC*
206
207>MCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
208ADQLTEEQIAEFKEAFSLFDKDGDGTITTKELGTVMRSLGQNPTEAELQDMINEVDADGNGTID
209FPEFLTMMARKMKDTDSEEEIREAFRVFDKDGNGYISAAELRHVMTNLGEKLTDEEVDEMIREA
210DIDGDGQVNYEEFVQMMTAK*
211
212>gi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]
213LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV
214EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG
215LLILILLLLLLALLSPDMLGDPDNHMPADPLNTPLHIKPEWYFLFAYAILRSVPNKLGGVLALFLSIVIL
216GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX
217IENY
218```
219
220FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format)
221format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge.
222
223### PNG encoded DNA sequence
224
225| Nucleotides | RGB | Color name |
226| ------------ | ----------- | ---------- |
227| A ➞ Adenine | (0,0,255) | Blue |
228| G ➞ Guanine | (0,100,0) | Green |
229| C ➞ Cytosine | (255,0,0) | Red |
230| T ➞ Thymine | (255,255,0) | Yellow |
231
232With this in mind we can create a simple algorithm to create PNG representation
233of a DNA sequence.
234
235```python
236{ Algorithm 2: Naive DNA to PNG encode from FASTA file }
237procedure EncodeDNASequenceToPNG(f)
238begin
239 i image
240 while not eof(f) do
241 c char := buffer[0] { Read 1 char from buffer }
242 case c of
243 'A': color := RGB(0, 0, 255) { Blue }
244 'G': color := RGB(0, 100, 0) { Green }
245 'C': color := RGB(255, 0, 0) { Red }
246 'T': color := RGB(255, 255, 0) { Yellow }
247 drawRect(i, [x, y], color)
248 save(i) { Save PNG image }
249end
250```
251
252## Encoding text file in practice
253
254In this example we will take a simple text file as our input stream for
255encoding. This file will have a quote from Niels Bohr and saved as txt file.
256
257> How wonderful that we have met with a paradox. Now we have some hope of
258> making progress.
259> ― Niels Bohr
260
261First we encode text file into FASTA file.
262
263```bash
264./dnae-encode -i quote.txt -o quote.fa
2652019/01/10 00:38:29 Gathering input file stats
2662019/01/10 00:38:29 Starting encoding ...
267 106 B / 106 B [==================================] 100.00% 0s
2682019/01/10 00:38:29 Saving to FASTA file ...
2692019/01/10 00:38:29 Output FASTA file length is 438 B
2702019/01/10 00:38:29 Process took 987.263µs
2712019/01/10 00:38:29 Done ...
272```
273
274Output of `quote.fa` file contains the encoded DNA sequence in ASCII format.
275
276```txt
277>SEQ1
278GACAGCTTGTGTACAAGTGTGCTTGCTCGCGAGCGGGTACGCGCGTGGGCTAACAAGTGA
279GCCAGCAGGTGAACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGCTGGCGGGTGA
280ACAAGTGTGCCGGTGAGCCAACAAGCAGACAAGTAAGCAGGTACGCAGGCGAGCTTGTCA
281ACTCACAAGATCGCTTGTGTACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGTAT
282GCTTGCTGGCGGACAAGCCAGCTTGTAAGCGGACAAGCTTGCGCACAAGCTGGCAGGCCT
283GCCGGCTCGCGTACAAATTCACAAGTAAGTACGCTTGCGTGTACGCGGGTATGTATACTC
284AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC
285AACC
286```
287
288Then we encode FASTA file from previous operation to encode this data into PNG.
289
290```bash
291./dnae-png -i quote.fa -o quote.png
2922019/01/10 00:40:09 Gathering input file stats ...
2932019/01/10 00:40:09 Deconstructing FASTA file ...
2942019/01/10 00:40:09 Compositing image file ...
295 424 / 424 [==================================] 100.00% 0s
2962019/01/10 00:40:09 Saving output file ...
2972019/01/10 00:40:09 Output image file length is 1.1 kB
2982019/01/10 00:40:09 Process took 19.036117ms
2992019/01/10 00:40:09 Done ...
300```
301
302After encoding into PNG format this file looks like this.
303
304![Encoded Quote in PNG format](/assets/posts/dna-sequence/quote.png){:loading="lazy"}
305
306The larger the input stream is the larger the PNG file would be.
307
308Compiled basic Hello World C program with
309[GCC](https://www.gnu.org/software/gcc/) would [look
310like](/assets/posts/dna-sequence/sample.png).
311
312```c
313// gcc -O3 -o sample sample.c
314#include <stdio.h>
315
316main() {
317 printf("Hello, world!\n");
318 return 0;
319}
320```
321
322## Toolkit for encoding data
323
324I have created a toolkit with two main programs:
325
326- dnae-encode (encodes file into FASTA file)
327- dnae-png (encodes FASTA file into PNG)
328
329Toolkit with full source code is available on
330[github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding).
331
332### dnae-encode
333
334```bash
335> ./dnae-encode --help
336usage: dnae-encode --input=INPUT [<flags>]
337
338A command-line application that encodes file into DNA sequence.
339
340Flags:
341 --help Show context-sensitive help (also try --help-long and --help-man).
342 -i, --input=INPUT Input file (ASCII or binary) which will be encoded into DNA sequence.
343 -o, --output="out.fa" Output file which stores DNA sequence in FASTA format.
344 -s, --sequence=SEQ1 The description line (defline) or header/identifier line, gives a name and/or a unique identifier for the sequence.
345 -c, --columns=60 Row characters length (no more than 120 characters). Devices preallocate fixed line sizes in software.
346 --version Show application version.
347```
348
349### dnae-png
350
351```bash
352> ./dnae-png --help
353usage: dnae-png --input=INPUT [<flags>]
354
355A command-line application that encodes FASTA file into PNG image.
356
357Flags:
358 --help Show context-sensitive help (also try --help-long and --help-man).
359 -i, --input=INPUT Input FASTA file which will be encoded into PNG image.
360 -o, --output="out.png" Output file in PNG format that represents DNA sequence in graphical way.
361 -s, --size=10 Size of pairings of DNA bases on image in pixels (lower resolution lower file size).
362 --version Show application version.
363```
364
365## Benchmarks
366
367First we generate some binary sample data with dd.
368
369```bash
370dd if=<(openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero) of=1KB.bin bs=1KB count=1 iflag=fullblock
371```
372
373![Sample binary file 1KB](/assets/posts/dna-sequence/sample-binary-file.png){:loading="lazy"}
374
375Our freshly generated 1KB file looks something like this (its full of
376garbage data as intended).
377
378We create following binary files:
379
380- 1KB.bin
381- 10KB.bin
382- 100KB.bin
383- 1MB.bin
384- 10MB.bin
385- 100MB.bin
386
387After this we create FASTA files for all the binary files by encoding them
388into DNA sequence.
389
390```bash
391./dnae-encode -i 100MB.bin -o 100MB.fa
392```
393
394Then we GZIP all the FASTA files to see how much the can be compressed.
395
396```bash
397gzip -9 < 10MB.fa > 10MB.fa.gz
398```
399
400![Encode to FASTA](/assets/posts/dna-sequence/chart-speed.svg){:loading="lazy"}
401
402The speed increase that occurs when encoding to FASTA format.
403
404![File sizes](/assets/posts/dna-sequence/chart-size.svg){:loading="lazy"}
405
406Size of the out file after encoding.
407
408[Download CSV file with benchmarks](/assets/posts/dna-sequence/benchmarks.csv).
409
410## References
411
412- https://www.techopedia.com/definition/948/encoding
413- https://www.dna-worldwide.com/resource/160/history-dna-timeline
414- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/
415- https://arxiv.org/abs/1801.04774
416- https://en.wikipedia.org/wiki/FASTA_format
diff --git a/_posts/posts/2019-10-14-simplifying-and-reducing-clutter.md b/_posts/posts/2019-10-14-simplifying-and-reducing-clutter.md
new file mode 100644
index 0000000..e804ecb
--- /dev/null
+++ b/_posts/posts/2019-10-14-simplifying-and-reducing-clutter.md
@@ -0,0 +1,60 @@
1---
2title: Simplifying and reducing clutter in my life and work
3permalink: /simplifying-and-reducing-clutter.html
4date: 2019-10-14T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I recently moved my main working machine back from Hachintosh to Linux. Well the
11experiment was interesting and I have done some great work on macOS but it was
12time to move back.
13
14I actually really missed Linux. The simplicity of `apt-get` or just the amount
15of software that exists for Linux should be a no-brainer. I spent most of my
16time on macOS finding solutions to make things work. Using
17[Brew](https://brew.sh/) was just a horrible experience and far from package
18managers of Linux. At least they managed to get that `sudo` debacle sorted.
19
20Not all was bad. macOS in general was a perfectly good environment. Things like
21Docker and tooling like this worked without any hiccups. My normal tools like
22coding IDE worked flawlessly and the whole look and feel is just superb. I have
23been using MacBook Air for couple of years so I was used to the system but never
24as a daily driver.
25
26One of the things I did after I installed Linux back on my machine was cleaning
27up my Dropbox folder. I have everything on Dropbox. Even projects folder. I
28write code for living so my whole life revolves around couple of megs of code
29(with assets). So it's not like I have huge files on my machine. I don't have
30movies or music or pictures on my PC. All of that stuff is in cloud. I use
31Google music and I have Netflix account which is more than enough for me.
32
33I also went and deleted some of the repositories on my Github account. I have
34deleted more code than deployed. People find this strange but for me deleting
35something feels so cathartic and also forces me to write better code next time
36around when I am faced with similar problem. That was a huge relief if I am
37being totally honest.
38
39Next step was to do something with my webpage. I have been using some scripts I
40wrote a while ago to generate static pages from markdown source posts. I kept on
41adding and adding stuff on top of it and it became a source of a
42frustration. And this is just a simple blog and I was using gulp and npm.
43Anyways after couple of hours of searching and testing static generators I found
44an interesting one
45[https://github.com/piranha/gostatic](https://github.com/piranha/gostatic) and I
46just decided to use this one. It was the only one that had a simple templating
47engine, not that I really need one. But others had this convoluted way of trying
48to solve everything and at the end just required quite bigger learning curve I
49was ready to go with. So I deleted couple of old posts, simplified HTML, trashed
50most of the CSS and went with
51[https://motherfuckingwebsite.com/](https://motherfuckingwebsite.com/)
52aesthetics. Yeah, the previous site was more visually stimulating but all I
53really care is the content at this point. And Times New Roman font is kind of
54awesome.
55
56I stopped working on most of the projects in the past couple of months because
57the overhead was just too insane. There comes a point when you stretch yourself
58too much and then you stop progressing and with that comes dissatisfaction.
59
60So that's about it. Moving forward minimal style.
diff --git a/_posts/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md b/_posts/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
new file mode 100644
index 0000000..a1b237b
--- /dev/null
+++ b/_posts/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
@@ -0,0 +1,109 @@
1---
2title: Using sentiment analysis for clickbait detection in RSS feeds
3permalink: /using-sentiment-analysis-for-clickbait-detection-in-rss-feeds.html
4date: 2019-10-19T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Initial thoughts
11
12One of the things that interested me for a while now is if major well
13established news sites use click bait titles to drive additional traffic to
14their sites and generate additional impressions.
15
16Goal is to see how article titles and actual content of article differ from each
17other and see if titles are clickbaited.
18
19## Preparing and cleaning data
20
21For this example I opted to just use RSS feed from a new website and decided to
22go with [The Guardian](https://www.theguardian.com) World news. While this gets
23us limited data (~40) articles and also description (actual content) is trimmed
24this really doesn't reflect the actual article contents.
25
26To get better content I could use web scraping and use RSS as link list and
27fetch contents directly from website, but for this simple example this will
28suffice.
29
30There are couple of requirements we need to install before we continue:
31
32- `pip3 install feedparser` (parses RSS feed from url)
33- `pip3 install vaderSentiment` (does sentiment polarity analysis)
34- `pip3 install matplotlib` (plots chart of results)
35
36So first we need to fetch RSS data and sanitize HTML content from description.
37
38```python
39import re
40import feedparser
41
42feed_url = "https://www.theguardian.com/world/rss"
43feed = feedparser.parse(feed_url)
44
45# sanitize html
46for item in feed.entries:
47 item.description = re.sub('<[^<]+?>', '', item.description)
48```
49
50## Perform sentiment analysis
51
52Since we now have cleaned up data in our `feed.entries` object we can start with
53performing sentiment analysis.
54
55There are many sentiment analysis libraries available that range from rule-based
56sentiment analysis up to machine learning supported analysis. To keep things
57simple I decided to use rule-based analysis library
58[vaderSentiment](https://github.com/cjhutto/vaderSentiment) from
59[C.J. Hutto](https://github.com/cjhutto). Really nice library and quite easy to
60use.
61
62```python
63from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
64analyser = SentimentIntensityAnalyzer()
65
66sentiment_results = []
67for item in feed.entries:
68 sentiment_title = analyser.polarity_scores(item.title)
69 sentiment_description = analyser.polarity_scores(item.description)
70 sentiment_results.append([sentiment_title['compound'], sentiment_description['compound']])
71```
72
73Now that we have this data in a shape that is compatible with matplotlib we can
74plot results to see the difference between title and description sentiment of an
75article.
76
77```python
78import matplotlib.pyplot as plt
79
80plt.rcParams['figure.figsize'] = (15, 3)
81plt.plot(sentiment_results, drawstyle='steps')
82plt.title('Sentiment analysis relationship between title and description (Guardian World News)')
83plt.legend(['title', 'description'])
84plt.show()
85```
86
87## Results and assets
88
891. Because of the small sample size further conclusions are impossible to make.
902. Rule-based approach may not be the best way of doing this. By using deep
91 learning we would be able to get better insights.
923. **Next step would be to** periodically fetch RSS items and store them over a
93 longer period of time and then perform analysis again and use either machine
94 learning or deep learning on top of it.
95
96![Relationship between title and description](/assets/posts/sentiment-analysis/guardian-sa-title-desc-relationship.png){:loading="lazy"}
97
98Figure above displays difference between title and description sentiment for
99specific RSS feed item. 1 means positive and -1 means negative sentiment.
100
101[» Download Jupyter Notebook](/assets/posts/sentiment-analysis/sentiment-analysis.ipynb)
102
103## Going further
104
105- [Twitter Sentiment Analysis by Bryan Schwierzke](https://github.com/bswiss/news_mood)
106- [AFINN-based sentiment analysis for Node.js by Andrew Sliwinski](https://github.com/thisandagain/sentiment)
107- [Sentiment Analysis with LSTMs in Tensorflow by Adit Deshpande](https://github.com/adeshpande3/LSTM-Sentiment-Analysis)
108- [Sentiment analysis on tweets using Naive Bayes, SVM, CNN, LSTM, etc. by Abdul Fatir](https://github.com/abdulfatir/twitter-sentiment-analysis)
109
diff --git a/_posts/posts/2020-03-22-simple-sse-based-pubsub-server.md b/_posts/posts/2020-03-22-simple-sse-based-pubsub-server.md
new file mode 100644
index 0000000..ffb7285
--- /dev/null
+++ b/_posts/posts/2020-03-22-simple-sse-based-pubsub-server.md
@@ -0,0 +1,455 @@
1---
2title: Simple Server-Sent Events based PubSub Server
3permalink: /simple-server-sent-events-based-pubsub-server.html
4date: 2020-03-22T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Before we continue ...
11
12Publisher Subscriber model is nothing new and there are many amazing solutions
13out there, so writing a new one would be a waste of time if other solutions
14wouldn't have quite complex install procedures and weren't so hard to maintain.
15But to be fair, comparing this simple server with something like
16[Kafka](https://kafka.apache.org/) or [RabbitMQ](https://www.rabbitmq.com/) is
17laughable at the least. Those solutions are enterprise grade and have many
18mechanisms there to ensure messages aren't lost and much more. Regardless of
19these drawbacks, this method has been tested on a large website and worked until
20now without any problems. So now, that we got that cleared up, let's continue.
21
22***Wiki definition:** Publish/subscribe messaging, or pub/sub messaging, is a
23form of asynchronous service-to-service communication used in serverless and
24microservices architectures. In a pub/sub model, any message published to a
25topic is immediately received by all the subscribers to the topic.*
26
27## General goals
28
29- provide a simple server that relays messages to all the connected clients,
30- messages can be posted on specific topics,
31- messages get sent via [Server-Sent
32 Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
33 to all the subscribers.
34
35## How exactly does the pub/sub model work?
36
37The easiest way to explain this is with diagram bellow. Basic function is
38simple. We have subscribers that receive messages, and we have publishers that
39create and post messages. Similar model is also well know pattern that works on
40a premise of consumers and producers, and they take similar roles.
41
42![How PubSub works](/assets/posts/simple-pubsub-server/pubsub-overview.png){:loading="lazy"}
43
44**These are some naive characteristics we want to achieve:**
45
46- producer is publishing messages to subscribe topic,
47- consumer is receiving messages from subscribed topic,
48- servers is also known as Broker,
49- broker does not store messages or tracks success,
50- broker uses
51 [FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)) method
52 for delivering messages,
53- if consumer wants to receive messages from a topic, producer and consumer
54 topics must match,
55- consumer can subscribe to multiple topics,
56- producer can publish to multiple topics,
57- each message has a messageId.
58
59**Known drawbacks:**
60
61- messages will not be stored in a persistent queue or unreceived messages like
62 [DeadLetterQueue](https://en.wikipedia.org/wiki/Dead_letter_queue) so old
63 messages could be lost on server restart,
64- [Server-Sent
65 Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
66 opens a long-running connection between the client and the server so make sure
67 if your setup is load balanced that the load balancer in this case can have
68 long opened connection,
69- no system moderation due to the dynamic nature of creating queues.
70
71## Server-Sent Events
72
73Read more about it on [official specification
74page](https://html.spec.whatwg.org/multipage/server-sent-events.html).
75
76### Current browser support
77
78![Browser support](/assets/posts/simple-pubsub-server/caniuse.png){:loading="lazy"}
79
80Check
81[https://caniuse.com/#feat=eventsource](https://caniuse.com/#feat=eventsource)
82for latest information about browser support.
83
84### Known issues
85
86- Firefox 52 and below do not support EventSource in web/shared workers
87- In Firefox prior to version 36 server-sent events do not reconnect
88 automatically in case of a connection interrupt (bug)
89- Reportedly, CORS in EventSource is currently supported in Firefox 10+, Opera
90 12+, Chrome 26+, Safari 7.0+.
91- Antivirus software may block the event streaming data chunks.
92
93Source: [https://caniuse.com/#feat=eventsource](https://caniuse.com/#feat=eventsource)
94
95### Message format
96
97The simplest message that can be sent is only with data attribute:
98
99```bash
100data: this is a simple message
101<blank line>
102```
103
104You can send message IDs to be used if the connection is dropped:
105
106```bash
107id: 33
108data: this is line one
109data: this is line two
110<blank line>
111```
112
113And you can specify your own event types (the above messages will all trigger
114the message event):
115
116```bash
117id: 36
118event: price
119data: 103.34
120<blank line>
121```
122
123### Server requirements
124
125The important thing is how you send headers and which headers are sent by the
126server that triggers browser to threat response as a EventStream.
127
128Headers responsible for this are:
129
130```bash
131Content-Type: text/event-stream
132Cache-Control: no-cache
133Connection: keep-alive
134```
135
136### Debugging with Google Chrome
137
138Google Chrome provides build-in debugging and exploration tool for [Server-Sent
139Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
140which is quite nice and available from Developer Tools under Network tab.
141
142> You can debug only client side events that get received and not the server
143> ones. For debugging server events add `console.log` to `server.js` code and
144> print out events.
145
146![Google Chrome Developer Tools EventStream](/assets/posts/simple-pubsub-server/chrome-debugging.png){:loading="lazy"}
147
148## Server implementation
149
150For the sake of this example we will use [Node.js](https://nodejs.org/en/) with
151[Express](https://expressjs.com) as our router since this is the easiest way to
152get started and we will use already written SSE library for node
153[sse-pubsub](https://www.npmjs.com/package/sse-pubsub) so we don't reinvent the
154wheel.
155
156```bash
157npm init --yes
158
159npm install express
160npm install body-parser
161npm install sse-pubsub
162```
163
164Basic implementation of a server (`server.js`):
165
166```js
167const express = require('express');
168const bodyParser = require('body-parser');
169const SSETopic = require('sse-pubsub');
170
171const app = express();
172const port = process.env.PORT || 4000;
173
174// topics container
175const sseTopics = {};
176
177app.use(bodyParser.json());
178
179// open for all cors
180app.all('*', (req, res, next) => {
181 res.header('Access-Control-Allow-Origin', '*');
182 res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
183 next();
184});
185
186// preflight request error fix
187app.options('*', async (req, res) => {
188 res.header('Access-Control-Allow-Origin', '*');
189 res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
190 res.send('OK');
191});
192
193// serve the event streams
194app.get('/stream/:topic', async (req, res, next) => {
195 const topic = req.params.topic;
196
197 if (!(topic in sseTopics)) {
198 sseTopics[topic] = new SSETopic({
199 pingInterval: 0,
200 maxStreamDuration: 15000,
201 });
202 }
203
204 // subscribing client to topic
205 sseTopics[topic].subscribe(req, res);
206});
207
208// accepts new messages into topic
209app.post('/publish', async (req, res) => {
210 let body = req.body;
211 let status = 200;
212
213 console.log('Incoming message:', req.body);
214
215 if (
216 body.hasOwnProperty('topic') &&
217 body.hasOwnProperty('event') &&
218 body.hasOwnProperty('message')
219 ) {
220 const topic = req.body.topic;
221 const event = req.body.event;
222 const message = req.body.message;
223
224 if (topic in sseTopics) {
225 // sends message to all the subscribers
226 sseTopics[topic].publish(message, event);
227 }
228 } else {
229 status = 400;
230 }
231
232 res.status(status).send({
233 status,
234 });
235});
236
237// returns JSON object of all opened topics
238app.get('/status', async (req, res) => {
239 res.send(sseTopics);
240});
241
242// health-check endpoint
243app.get('/', async (req, res) => {
244 res.send('OK');
245});
246
247// return a 404 if no routes match
248app.use((req, res, next) => {
249 res.set('Cache-Control', 'private, no-store');
250 res.status(404).end('Not found');
251});
252
253// starts the server
254app.listen(port, () => {
255 console.log(`PubSub server running on http://localhost:${port}`);
256});
257```
258
259### Our custom message format
260
261Each message posted on a server must be in a specific format that out server
262accepts. Having structure like this allows us to have multiple separated type of
263events on each topic.
264
265With this we can separate streams and only receive events that belong to the
266topic.
267
268One example would be, that we have index page and we want to receive messages
269about new upvotes or new subscribers but we don't want to follow events for
270other pages. This reduces clutter and overall network. And structure is much
271nicer and maintanable.
272
273```json
274{
275 "topic": "sample-topic",
276 "event": "sample-event",
277 "message": { "name": "John" }
278}
279```
280
281## Publisher and subscriber clients
282
283### Publisher and subscriber in action
284
285<video src="/assets/posts/simple-pubsub-server/clients.m4v" controls></video>
286
287You can download [the code](../simple-pubsub-server/sse-pubsub-server.zip) and
288follow along.
289
290### Publisher
291
292As talked about above publisher is the one that send messages to the
293broker/server. Message inside the payload can be whatever you want (string,
294object, array). I would however personally avoid send large chunks of data like
295blobs and such.
296
297```html
298<!DOCTYPE html>
299<html lang="en">
300
301 <head>
302 <meta charset="UTF-8">
303 <meta name="viewport" content="width=device-width, initial-scale=1.0">
304 <title>Publisher</title>
305 </head>
306
307 <body>
308
309 <h1>Publisher</h1>
310
311 <fieldset>
312 <p>
313 <label>Server:</label>
314 <input type="text" id="server" value="http://localhost:4000">
315 </p>
316 <p>
317 <label>Topic:</label>
318 <input type="text" id="topic" value="sample-topic">
319 </p>
320 <p>
321 <label>Event:</label>
322 <input type="text" id="event" value="sample-event">
323 </p>
324 <p>
325 <label>Message:</label>
326 <input type="text" id="message" value='{"name": "John"}'>
327 </p>
328 <p>
329 <button type="button" id="button">Publish message to topic</button>
330 </p>
331 </fieldset>
332
333 <script>
334
335 const button = document.querySelector('#button');
336 const server = document.querySelector('#server');
337 const topic = document.querySelector('#topic');
338 const event = document.querySelector('#event');
339 const message = document.querySelector('#message');
340
341 button.addEventListener('click', async (evt) => {
342 const req = await fetch(`${server.value}/publish`, {
343 method: 'post',
344 headers: {
345 'Accept': 'application/json',
346 'Content-Type': 'application/json',
347 },
348 body: JSON.stringify({
349 topic: topic.value,
350 event: event.value,
351 message: JSON.parse(message.value),
352 }),
353 });
354
355 const res = await req.json();
356 console.log(res);
357 });
358
359 </script>
360
361 </body>
362
363</html>
364```
365
366### Subscriber
367
368Subscriber is responsible for receiving new messages that come from server via
369publisher. The code bellow is very rudimentary but works and follows the
370implementation guidelines for EventSource.
371
372You can use either Developer Tools Console to see incoming messages or you can
373defer to Debugging with Google Chrome section above to see all EventStream
374messages.
375
376> Don't be alarmed if the subscriber gets disconnected from the server every so
377> often. The code we have here resets connection every 15s but it automatically
378> get reconnected and fetches all messages up to last received message id. This
379> setting can be adjusted in `server.js` file; search for the
380> `maxStreamDuration` variable.
381
382```html
383<!DOCTYPE html>
384<html lang="en">
385
386 <head>
387 <meta charset="UTF-8">
388 <meta name="viewport" content="width=device-width, initial-scale=1.0">
389 <title>Subscriber</title>
390 <link rel="stylesheet" href="style.css">
391 </head>
392
393 <body>
394
395 <h1>Subscriber</h1>
396
397 <fieldset>
398 <p>
399 <label>Server:</label>
400 <input type="text" id="server" value="http://localhost:4000">
401 </p>
402 <p>
403 <label>Topic:</label>
404 <input type="text" id="topic" value="sample-topic">
405 </p>
406 <p>
407 <label>Event:</label>
408 <input type="text" id="event" value="sample-event">
409 </p>
410 <p>
411 <button type="button" id="button">Subscribe to topic</button>
412 </p>
413 </fieldset>
414
415 <script>
416
417 const button = document.querySelector('#button');
418 const server = document.querySelector('#server');
419 const topic = document.querySelector('#topic');
420 const event = document.querySelector('#event');
421
422 button.addEventListener('click', async (evt) => {
423
424 let es = new EventSource(`${server.value}/stream/${topic.value}`);
425
426 es.addEventListener(event.value, function (evt) {
427 console.log(`incoming message`, JSON.parse(evt.data));
428 });
429
430 es.addEventListener('open', function (evt) {
431 console.log('connected', evt);
432 });
433
434 es.addEventListener('error', function (evt) {
435 console.log('error', evt);
436 });
437
438 });
439
440 </script>
441
442 </body>
443
444</html>
445```
446
447## Reading further
448
449- [Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
450- [Using SSE Instead Of WebSockets For Unidirectional Data Flow Over HTTP/2](https://www.smashingmagazine.com/2018/02/sse-websockets-data-flow-http2/)
451- [What is Server-Sent Events?](https://apifriends.com/api-streaming/server-sent-events/)
452- [An HTTP/2 extension for bidirectional messaging communication](https://tools.ietf.org/id/draft-xie-bidirectional-messaging-01.html)
453- [Introduction to HTTP/2](https://developers.google.com/web/fundamentals/performance/http2)
454- [The WebSocket API (WebSockets)](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
455
diff --git a/_posts/posts/2020-03-27-create-placeholder-images-with-sharp.md b/_posts/posts/2020-03-27-create-placeholder-images-with-sharp.md
new file mode 100644
index 0000000..c129396
--- /dev/null
+++ b/_posts/posts/2020-03-27-create-placeholder-images-with-sharp.md
@@ -0,0 +1,103 @@
1---
2title: Create placeholder images with sharp Node.js image processing library
3permalink: /create-placeholder-images-with-sharp.html
4date: 2020-03-27T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I have been searching for a solution to pre-generate some placeholder images for
11image server I needed to develop that resizes images on S3. I though this would
12be a 15min job and quickly found out how very mistaken I was.
13
14Even though Node.js is not really the best way to do this kind of things (surely
15something written in C or Rust or even Golang would be the correct way to do
16this but we didn't need the speed in our case) I found an excellent library
17[sharp - High performance Node.js image
18processing](https://github.com/lovell/sharp).
19
20Getting things running was a breeze.
21
22## Fetch image from S3 and save resized
23
24```js
25const sharp = require('sharp');
26const aws = require('aws-sdk');
27
28const x,y = 100;
29const s3 = new aws.S3({});
30
31aws.config.update({
32 secretAccessKey: 'secretAccessKey',
33 accessKeyId: 'accessKeyId',
34 region: 'region'
35});
36
37const originalImage = await s3.getObject({
38 Bucket: 'some-bucket-name',
39 Key: 'image.jpg',
40}).promise();
41
42const resizedImage = await sharp(originalImage.Body)
43 .resize(x, y)
44 .jpeg({ progressive: true })
45 .toBuffer();
46
47s3.putObject({
48 Bucket: 'some-bucket-name',
49 Key: `optimized/${x}x${y}/image.jpg`,
50 Body: resizedImage,
51 ContentType: 'image/jpeg',
52 ACL: 'public-read'
53}).promise();
54```
55
56All this code was wrapped inside a web service with some additional security
57checks and defensive coding to detect if key is missing on S3.
58
59And at that point I needed to return placeholder images as a response in case
60key is missing or x,y are not allowed by the server etc. I could have created
61PNG in Gimp and just serve them but I wanted to respect aspect ratio and I
62didn't want to return some mangled images.
63
64> Main problem with finding a clean solution I could copy and paste and change a
65> bit was a task. API is changing constantly and there weren't clear examples or
66> I was unable to find them.
67
68## Generating placeholder images using SVG
69
70What I ended up was using SVG to generate text and created image with sharp and
71used composition to combine both layers. Response returned by this function is a
72buffer you can use to either upload to S3 or save to local file.
73
74```js
75const generatePlaceholderImageWithText = async (width, height, message) => {
76 const overlay = `<svg width="${width - 20}" height="${height - 20}">
77 <text x="50%" y="50%" font-family="sans-serif" font-size="16" text-anchor="middle">${message}</text>
78 </svg>`;
79
80 return await sharp({
81 create: {
82 width: width,
83 height: height,
84 channels: 4,
85 background: { r: 230, g: 230, b: 230, alpha: 1 }
86 }
87 })
88 .composite([{
89 input: Buffer.from(overlay),
90 gravity: 'center',
91 }])
92 .jpeg()
93 .toBuffer();
94}
95```
96
97That is about it. Nothing more to it. You can change the color of the image by
98changing `background` and if you want to change text styling you can adapt SVG
99to your needs.
100
101> Also be careful about the length of the text. This function positions text at
102> the center and adds `20px` padding on all sides. If text is longer than the
103> image it will get cut.
diff --git a/_posts/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md b/_posts/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md
new file mode 100644
index 0000000..1aa3536
--- /dev/null
+++ b/_posts/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md
@@ -0,0 +1,109 @@
1---
2title: The strange case of Elasticsearch allocation failure
3permalink: /the-strange-case-of-elasticsearch-allocation-failure.html
4date: 2020-03-29T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I've been using Elasticsearch in production for 5 years now and never had a
11single problem with it. Hell, never even known there could be a problem. Just
12worked. All this time. The first node that I deployed is still being used in
13production, never updated, upgraded, touched in anyway.
14
15All this bliss came to an abrupt end this Friday when I got notification that
16Elasticsearch cluster went warm. Well, warm is not that bad right? Wrong!
17Quickly after that I got another email which sent chills down my spine. Cluster
18is now red. RED! Now, shit really hit the fan!
19
20I tried googling what could be the problem and after executing allocation
21function noticed that some shards were unassigned and 5 attempts were already
22made (which is BTW to my luck the maximum) and that meant I am basically fucked.
23They also applied that one should wait for cluster to re-balance itself. So, I
24waited. One hour, two hours, several hours. Nothing, still RED.
25
26The strangest thing about it all was, that queries were still being fulfilled.
27Data was coming out. On the outside it looked like nothing was wrong but
28everybody that would look at the cluster would know immediately that something
29was very very wrong and we were living on borrowed time here.
30
31> **Please, DO NOT do what I did.** Seriously! Please ask someone on official
32forums or if you know an expert please consult him. There could be million of
33reasons and these solution fit my problem. Maybe in your case it would
34disastrous. I had all the data backed up and even if I would fail spectacularly
35I would be able to restore the data. It would be a huge pain and I would loose
36couple of days but I had a plan B.
37
38Executing allocation and told me what the problem was but no clear solution yet.
39
40```yaml
41GET /_cat/allocation?format=json
42```
43
44I got a message that `ALLOCATION_FAILED` with additional info `failed to create
45shard, failure ioexception[failed to obtain in-memory shard lock]`. Well
46splendid! I must also say that our cluster is capable more than enough to handle
47the traffic. Also JVM memory pressure never was an issue. So what happened
48really then?
49
50I tried also re-routing failed ones with no success due to AWS restrictions on
51having managed Elasticsearch cluster (they lock some of the functions).
52
53```yaml
54POST /_cluster/reroute?retry_failed=true
55```
56
57I got a message that significantly reduced my options.
58
59```json
60{
61 "Message": "Your request: '/_cluster/reroute' is not allowed."
62}
63```
64
65After that I went on a hunt again. I won't bother you with all the details
66because hours/days went by until I was finally able to re-index the problematic
67index and hoped for the best. Until that moment even re-indexing was giving me
68errors.
69
70```yaml
71POST _reindex
72{
73 "source": {
74 "index": "myindex"
75 },
76 "dest": {
77 "index": "myindex-new"
78 }
79}
80```
81
82I needed to do this multiple times to get all the documents re-indexed. Then I
83dropped the original one with the following command.
84
85```yaml
86DELETE /myindex
87```
88
89And re-indexed again new one in the original one (well by name only).
90
91```yaml
92POST _reindex
93{
94 "source": {
95 "index": "myindex-new"
96 },
97 "dest": {
98 "index": "myindex"
99 }
100}
101```
102
103On the surface it looks like all is working but I have a long road in front of
104me to get all the things working again. Cluster now shows that it is in Green
105mode but I am also getting a notification that the cluster has processing status
106which could mean million of things.
107
108Godspeed!
109
diff --git a/_posts/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md b/_posts/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md
new file mode 100644
index 0000000..0299d9d
--- /dev/null
+++ b/_posts/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md
@@ -0,0 +1,112 @@
1---
2title: My love and hate relationship with Node.js
3permalink: /my-love-and-hate-relationship-with-nodejs.html
4date: 2020-03-30T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Previous project I was working on was being coded in
11[Golang](https://golang.org/). Also was my first project using it. And damn,
12that was an awesome experience. The whole thing is just superb. From how errors
13are handled. The C-like way you handle compiling. The way the language is
14structured making it incredibly versatile and easy to learn.
15
16It may cause some pain for somebody that is not used of using interfaces to map
17JSON and doing the recompilation all the time. But we have tools like
18[entr](http://eradman.com/entrproject/) and
19[make](https://www.gnu.org/software/make/) to fix that.
20
21But we are not here to talk about my undying love for **Golang**. Only in some
22way we probably should. It is an excellent example of how modern language should
23be designed. And because I have used it extensively in the last couple of years
24this probably taints my views of other languages. And is doing me a great
25disservice. Nevertheless, here we are.
26
27About two years ago I started flirting with [Node.js](https://nodejs.org/en/)
28for a project I started working on. What I wanted was to have things written in
29a language that is widely used, and we could get additional developers for. As
30much as **Golang** is amazing it's really hard to get developers for it. Even
31now. And after playing around with it for a week I felt in love with the speed
32of iteration and massive package ecosystem. Do you want SSO? You got it! Do you
33want some esoteric library for something? There is a strong chance somebody
34wrote it. It is so extensive that you find yourself evaluating packages based on
35**GitHub stars** and number of contributors. You get swallowed by the vanity
36metrics and that potentially will become the downfall of Node.js.
37
38Because of the sheer amount of choice I often got anxiety when choosing
39libraries. Will I choose the correct one? Is this library something that will be
40supported for a foreseeable future or not? I am used of using libraries that are
41being in development for 10 years plus (Python, C) and that gave me some sort of
42comfort. And it is probably unfair to Node.js and community to expect same
43dedication.
44
45Moving forward ... Work started and things were great. **Speed of iteration was
46insane**. For some feature that I would need a day in Golang only took me hour
47or two. I became lazy! Using packages all over the place. Falling into the same
48trap as others. Packages on top of packages. And [npm](https://www.npmjs.com/)
49didn't help at all. The way that the package manager works is just
50horrendous. And not allowing to have node_modules outside the project is also
51the stupidest idea ever.
52
53So at that point I started feeling the technical debt that comes with Node.js
54and the whole ecosystem. What nobody tells you is that **structuring large
55Node.js apps** is more problematic than one would think. And going microservice
56for every single thing is also a bad idea. The amount of networking you
57introduce with that approach always ends up being a pain in the ass. And I don't
58even want to go into system administration here. The overhead is
59insane. Package-lock.json made many days feel like living hell for me. And I
60would eat the cost of all this if it meant for better development
61experience. Well, it didn't.
62
63The **lack of Typescript** support in the interpreter is still mind boggling to
64me. Why haven't they added native support yet for this is beyond me?! That would
65have solved so many problems. Lack of type safety became a problem somewhere in
66the middle of the project where the codebase was sufficiently large enough to
67present problems. We started adding arguments to functions and there was **no
68way to implicitly define argument types**. And because at that point there were
69a lot of functions, it became impossible to know what each one accepts,
70development became more and more trial and error based.
71
72I tried **implementing Typescript**, but that would present a large refactor
73that we were not willing to do at that point. The benefits were not enough. I
74also tried [Flow - static type checker](https://flow.org/) but implementation
75was also horrible. What Typescript and Flow forces you is to have src folder and
76then **transpile** your code into dist folder and run it with node. WTH is that
77all about. Why can't this be done in memory or some virtual file system? Why? I
78see no reason why this couldn't be done like this. But it is what it is. I
79abandoned all hope for static type checking.
80
81One of the problems that resulted from not having interfaces or types was
82inability to model out our data from **Elasticsearch**. I could have done a
83**pedestrian implementation** of it, but there must be a better way of doing
84this without resorting to some hack basically. Or maybe I haven't found a
85solution, which is also a possibility. I have looked, though. No juice!
86
87**Error handling?** Is that a joke?
88
89Thank god for **await/async**. Without it, I would have probably just abandoned
90the whole thing and went with something else like Python. That's all I am going
91to say about this :)
92
93I started asking myself a question if Node.js is actually ready to be used in a
94**large scale applications**? And this was a totally wrong question. What I
95should have been asking myself was, how to use Node.js in large scale
96application. And you don't get this in **marketing material** for Express or Koa
97etc. They never tell you this. Making Node.js scale on infrastructure or in
98codebase is really **more of an art than a science**. And just like with the
99whole JavaScript ecosystem:
100
101- impossible to master,
102- half of your time you work on your tooling,
103- just accept transpilers that convert one code into another (holly smokes),
104- error handling is a joke,
105- standards? What standards?
106
107But on the other hand. As I did, you will also learn to love it. Learn to use it
108quickly and do impossible things in crazy limited time.
109
110I hate to admit it. But I love Node.js. Dammit, I love it :)
111
112**2023 Update**: I hate Node.js!
diff --git a/_posts/posts/2020-05-05-remote-work.md b/_posts/posts/2020-05-05-remote-work.md
new file mode 100644
index 0000000..8eb75d2
--- /dev/null
+++ b/_posts/posts/2020-05-05-remote-work.md
@@ -0,0 +1,73 @@
1---
2title: Remote work and how it affects the daily lives of people
3permalink: /remote-work.html
4date: 2020-05-05T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I have been working remotely for the past 5 years. I love it. Love the freedom
11and make your schedule thingy.
12
13## You work more not less
14
15I've heard from people things like: "Oh, you are so lucky, working from home,
16having all the free time you want". It was obvious they had no clue what means
17working remotely. They had this romantic idea of remote work. You can watch TV
18whenever you like, you can go outside for a picnic if you want and stuff like
19that.
20
21This may be true if you work a day or two in a week from home. But if you go
22completely remote all these changes completely. I take some time to acclimate
23but then you start feeling the consequences of going fully remote. And it's not
24all rainbows and unicorns. Rather the opposite.
25
26## Feeling lost
27
28At first, I remembered I felt lost. I was not used to this kind of environment.
29It felt disoriented and a part of you that is used to procrastinate turns on.
30You start thinking of a workday as a whole day. And soon this idea of "I can do
31this later" starts creeping in. Well, I have the whole day ahead of me. I can do
32this a bit later.
33
34## Hyper-performance
35
36As a direct result, you become more focused on your work since you don't have
37all the interruptions common in the workplace. And you can quickly get used to
38this hyper-performance. But this mode requires also a lot of peace and quiet.
39
40And here we come to the ugly parts of all this. **People rarely have the
41self-control** to not waste other people's time. It is paralyzing when people
42start calling you, sending you chat messages, etc. The thing is, that when I
43achieve this hyper-performance mode I am completely embroiled in the problem I
44am solving and this kind of interruptions mess with your head. I need an hour at
45least to get back in the zone. Sometimes not achieving the same focus the whole
46day.
47
48I know that life is not how you want it to be and takes its route but from what
49I've learned this kind of interruptions can be avoided in 90% of the case easily
50just by closing any chat programs and putting your phone in a drawer.
51
52## Suggestion to all the new remote workers
53
54- Stop wasting other people's time. You don't bother people at their desks in
55 the office either.
56- Do not replace daily chats in the hallways with instant messaging software.
57 It will only interrupt people. Nothing good will come of it.
58- Set your working hours and try to not allow it to bleed outside these
59 boundaries and maintain your routine.
60- Be prepared that hours will be longer regardless of your good intentions and
61 your well thought of routine.
62- Try to be hyper-focused and do only one thing at the time. Multitasking is the
63 enemy of progress.
64- Avoid long meetings and if possible eliminate them. Rather take time to write
65 them out and allow others to respond in their own time. Meetings are usually a
66 large waste of time and most of the people attending them are there just
67 because the manager said so.
68- The software will not solve your problems. And throwing money at problems
69 neither.
70- If you are in a managerial position don't supervise any single minute of
71 workers. They are probably giving you more hours anyways. Track progress
72 weekly not daily. You hired them and give them the benefit of the doubt that
73 they will deliver what you agreed upon.
diff --git a/_posts/posts/2020-08-15-systemd-disable-wake-onmouse.md b/_posts/posts/2020-08-15-systemd-disable-wake-onmouse.md
new file mode 100644
index 0000000..8122322
--- /dev/null
+++ b/_posts/posts/2020-08-15-systemd-disable-wake-onmouse.md
@@ -0,0 +1,74 @@
1---
2title: Disable mouse wake from suspend with systemd service
3permalink: /disable-mouse-wake-from-suspend-with-systemd-service.html
4date: 2020-08-15T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I recently bought [ThinkPad
11X220](https://www.laptopmag.com/reviews/laptops/lenovo-thinkpad-x220) just as a
12joke on eBay to test Linux distributions and play around with things and not
13destroy my main machine. Little to my knowledge I felt in love with it. Man,
14they really made awesome machines back then.
15
16After changing disk that came with it to SSD and installing Ubuntu to test if 
17everything works I noticed that even after a single touch of my external mouse
18the system would wake up from sleep even though the lid was shut down.
19
20I wouldn't even noticed it if laptop didn't have [LED
21sleep indicator](https://support.lenovo.com/lk/en/solutions/~/media/Images/ContentImages/p/pd025386_x1_status_03.ashx?w=426&h=262).
22I already had a bad experience with Linux and it's power management. I had a
23[Dell Inspiron 7537](https://www.pcmag.com/reviews/dell-inspiron-15-7537) laptop
24with a touchscreen and while traveling it decided to wake up and started cooking
25in my backpack to the point that the digitizer responsible for touch actually
26glue off and the whole screen got wrecked. So, I am a bit touchy about this.
27
28I went on solution hunting and to my surprise there is no easy way to disable
29specific devices to perform wake up. Why is this not under the power management 
30tab in setting is really strange.
31
32After googling for a solution I found [this nice article describing the
33solution](https://codetrips.com/2020/03/18/ubuntu-disable-mouse-wake-from-suspend/)
34that worked for me. The only problem with this solution was that he added his
35solution to `.bashrc` and this triggers `sudo` that asks for a password each
36time new terminal is opened, which get annoying quickly since I open a lot of
37terminals all the time.
38
39I followed his instructions and got to solution `sudo sh -c "echo 'disabled' >
40/sys/bus/usb/devices/2-1.1/power/wakeup"`.
41
42I created a system service file `sudo nano
43/etc/systemd/system/disable-mouse-wakeup.service` and removed `sudo` and
44replaced `sh` with `/usr/bin/sh` and pasted all that in `ExecStart`.
45
46```ini
47[Unit]
48Description=Disables wakeup on mouse event
49After=network.target
50StartLimitIntervalSec=0
51
52[Service]
53Type=simple
54Restart=always
55RestartSec=1
56User=root
57ExecStart=/usr/bin/sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup"
58
59[Install]
60WantedBy=multi-user.target
61```
62
63After that I enabled, started and checked status of service.
64
65```sh
66sudo systemctl enable disable-mouse-wakeup.service
67sudo systemctl start disable-mouse-wakeup.service
68sudo systemctl status disable-mouse-wakeup.service
69```
70
71This will permanently disable that device from wakeing up you computer on boot.
72If you have many devices you would like to surpress from waking up your machine
73I would create a shell script and call that instead of direclty doing it in
74service file.
diff --git a/_posts/posts/2020-09-06-esp-and-micropython.md b/_posts/posts/2020-09-06-esp-and-micropython.md
new file mode 100644
index 0000000..bfd05d9
--- /dev/null
+++ b/_posts/posts/2020-09-06-esp-and-micropython.md
@@ -0,0 +1,226 @@
1---
2title: Getting started with MicroPython and ESP8266
3permalink: /esp8266-and-micropython-guide.html
4date: 2020-09-06T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Introduction
11
12A while ago I bought some
13[ESP8266](https://www.espressif.com/en/products/socs/esp8266) and
14[ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to play
15around with and I finally found a project to try it out.
16
17For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32)
18but I could easily choose
19[ESP8266](https://www.espressif.com/en/products/socs/esp8266). This guide
20contains which tools I use and how I prepared my workspace to code for
21[ESP8266](https://www.espressif.com/en/products/socs/esp8266).
22
23![ESP8266 and ESP32 boards](/assets/posts/esp8366-micropython/boards.jpg){:loading="lazy"}
24
25This guide covers:
26
27- flashing SOC
28- install proper tooling
29- deploying a simple script
30
31> Make sure that you are using **a good USB cable**. I had some problems with
32mine and once I replaced it everything started to work.
33
34## Flashing the SOC
35
36Plug your ESP8266 to USB port and check if the device was recognized with
37executing `dmesg | grep ch341-uart`.
38
39Then check if the device is available under `/dev/` by running `ls
40/dev/ttyUSB*`.
41
42> **Linux users**: if a device is not available be sure you are in `dialout`
43> group. You can check this by executing `groups $USER`. You can add a user to
44> `dialout` group with `sudo adduser $USER dialout`.
45
46After these conditions are meet go to the navigate to
47[https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/)
48and download `esp8266-20200902-v1.13.bin`.
49
50```sh
51mkdir esp8266-test
52cd esp8266-test
53
54wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin
55```
56
57After obtaining firmware we will need some tooling to flash the firmware to the
58board.
59
60```sh
61sudo pip3 install esptool
62```
63
64You can read more about `esptool` at
65[https://github.com/espressif/esptool/](https://github.com/espressif/esptool/).
66
67Before flashing the firmware we need to erase the flash on device. Substitute
68`USB0` with the device listed in output of `ls /dev/ttyUSB*`.
69
70```sh
71esptool.py --port /dev/ttyUSB0 erase_flash
72```
73
74If flash was successfully erased it is now time to flash the new firmware to it.
75
76```sh
77esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin
78```
79
80If everything went ok you can try accessing MicroPython REPL with ` screen
81/dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`.
82
83> Sometimes you will need to press `ENTER` in `screen` or `picocom` to access
84> REPL.
85
86When you are in REPL you can test if all is working properly following steps.
87
88```py
89> import machine
90> machine.freq()
91```
92
93This should output a number representing a frequency of the CPU (mine was
94`80000000`).
95
96When you are in `screen` or `picocom` these can help you a bit.
97
98| Key | Command |
99| -------- | -------------------- |
100| CTRL+d | preforms soft reboot |
101| CTRL+a x | exits picocom |
102| CTRL+a \ | exits screen |
103
104
105## Install better tooling
106
107Now, to make our lives a little bit easier there are couple of additional tools
108that will make this whole experience a little more bearable.
109
110There are twq cool ways of uploading local files to SOC flash.
111
112- ampy → [https://github.com/scientifichackers/ampy](https://github.com/scientifichackers/ampy)
113- rshell → [https://github.com/dhylands/rshell](https://github.com/dhylands/rshell)
114
115### ampy
116
117```bash
118# installing ampy
119sudo pip3 install adafruit-ampy
120```
121
122Listed below are some common commands I used.
123
124```bash
125# uploads file to flash
126ampy --delay 2 --port /dev/ttyUSB0 put boot.py
127
128# lists file on flash
129ampy --delay 2 --port /dev/ttyUSB0 ls
130
131# outputs contents of file on flash
132ampy --delay 2 --port /dev/ttyUSB0 cat boot.py
133```
134
135> I added `delay` of 2 seconds because I had problems with executing commands.
136
137### rshell
138
139Even though `ampy` is a cool tool I opted with `rshell` in the end since it's
140much more polished and feature rich.
141
142```bash
143# installing ampy
144sudo pip3 install rshell
145```
146
147Now that `rshell` is installed we can connect to the board.
148
149```bash
150rshell --buffer-size=30 -p /dev/ttyUSB0 -a
151```
152
153This will open a shell inside bash and from here you can execute multiple
154commands. You can check what is supported with `help` once you are inside of a
155shell.
156
157```bash
158m@turing ~/Junk/esp8266-test
159$ rshell --buffer-size=30 -p /dev/ttyUSB0 -a
160
161Using buffer-size of 30
162Connecting to /dev/ttyUSB0 (buffer-size 30)...
163Trying to connect to REPL connected
164Testing if ubinascii.unhexlify exists ... Y
165Retrieving root directories ... /boot.py/
166Setting time ... Sep 06, 2020 23:54:28
167Evaluating board_name ... pyboard
168Retrieving time epoch ... Jan 01, 2000
169Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
170/home/m/Junk/esp8266-test> help
171
172Documented commands (type help <topic>):
173========================================
174args cat connect date edit filesize help mkdir rm shell
175boards cd cp echo exit filetype ls repl rsync
176
177Use Control-D (or the exit command) to exit rshell.
178```
179
180> Inside a shell `ls` will display list of files on your machine. To get list
181> of files on flash folder `/pyboard` is remapped inside the shell. To list files
182> on flash you must perform `ls /pyboard`.
183
184#### Moving files to flash
185
186To avoid copying files all the time I used `rsync` function from the inside of
187`rshell`.
188
189```bash
190rsync . /pyboard
191```
192
193#### Executing scripts
194
195It is a pain to continuously reboot the device to trigger `/pyboard/boot.py` and
196there is a better way of testing local scripts on remote device.
197
198Lets assume we have `src/freq.py` file that displays CPU frequency of a remote
199device.
200
201```py
202# src/freq.py
203
204import machine
205print(machine.freq())
206```
207
208Now lets upload this and execute it.
209
210```bash
211# syncs files to remove device
212rsync ./src /pyboard
213
214# goes into REPL
215repl
216
217# we import file by importing it without .py extension and this will run the script
218> import freq
219
220# CTRL+x will exit REPL
221```
222
223## Additional resources
224
225- https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/
226- http://docs.micropython.org/en/latest/esp8266/quickref.html
diff --git a/_posts/posts/2020-09-08-bind-warning-on-login.md b/_posts/posts/2020-09-08-bind-warning-on-login.md
new file mode 100644
index 0000000..4b2c983
--- /dev/null
+++ b/_posts/posts/2020-09-08-bind-warning-on-login.md
@@ -0,0 +1,55 @@
1---
2title: Fix bind warning in .profile on login in Ubuntu
3permalink: /bind-warning-on-login-in-ubuntu.html
4date: 2020-09-08T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Recently I moved back to [bash](https://www.gnu.org/software/bash/) as my
11default shell. I was previously using [fish](https://fishshell.com/) and got
12used to the cool features it has. But, regardless of that, I wanted to move to a
13more standard shell because I was hopping back and forth with exporting
14variables and stuff like that which got pretty annoying.
15
16So I embarked on a mission to make [bash](https://www.gnu.org/software/bash/)
17more like [fish](https://fishshell.com/) and in the process found that I really
18missed autosuggest with TAB on changing directories.
19
20I found a nice alternative that emulates [zsh](http://zsh.sourceforge.net/) like
21autosuggestion and autocomplete so I added the following to my `.bashrc` file.
22
23```bash
24bind "TAB:menu-complete"
25bind "set show-all-if-ambiguous on"
26bind "set completion-ignore-case on"
27bind "set menu-complete-display-prefix on"
28bind '"\e[Z":menu-complete-backward'
29```
30
31I haven't noticed anything wrong with this and all was working fine until I
32restarted my machine and then I got this error.
33
34![Profile bind error](/assets/posts/profile-bind-error/error.jpg){:loading="lazy"}
35
36When I pressed OK, I got into the [Gnome
37shell](https://wiki.gnome.org/Projects/GnomeShell) and all was working fine, but
38the error was still bugging me. I started looking for the reason why this is
39happening and found a solution to this error on [Remote SSH Commands - bash bind
40warning: line editing not enabled](https://superuser.com/a/892682).
41
42So I added a simple `if [ -t 1 ]` around `bind` statements to avoid running
43commands that presume the session is interactive when it isn't.
44
45```bash
46if [ -t 1 ]; then
47 bind "TAB:menu-complete"
48 bind "set show-all-if-ambiguous on"
49 bind "set completion-ignore-case on"
50 bind "set menu-complete-display-prefix on"
51 bind '"\e[Z":menu-complete-backward'
52fi
53```
54
55After logging out and back in the problem was gone.
diff --git a/_posts/posts/2020-09-09-digitalocean-sync.md b/_posts/posts/2020-09-09-digitalocean-sync.md
new file mode 100644
index 0000000..38696a9
--- /dev/null
+++ b/_posts/posts/2020-09-09-digitalocean-sync.md
@@ -0,0 +1,113 @@
1---
2title: Using Digitalocean Spaces to sync between computers
3permalink: /digitalocean-spaces-to-sync-between-computers.html
4date: 2020-09-09T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I've been using [Dropbox](https://www.dropbox.com/) for probably **10+ years**
11now and I-ve became so used to it that it runs in the background that I don't
12even imagine a world without it. But it's not without problems.
13
14At first I had problems with `.venv` environments for Python and the only
15solution for excluding synchronization for this folder was to manually exclude a
16specific folder which is not really scalable. FYI, my whole project folder is
17synced on [Dropbox](https://www.dropbox.com/). This of course introduced a lot
18of syncing of files and folders that are not needed or even break things on
19other machines. In the case of **Python**, I couldn't use that on my second
20machine. I needed to delete `.venv` folder and pip it again which synced files
21again to the main machine. This was very frustrating. **Nodejs** handles this
22much nicer and I can just run the scripts without deleting `node_modules` again
23and reinstalling. However, `node_modules` is a beast of its own. It creates so
24many files that OS has a problem counting them when you check the folder
25contents for size.
26
27I wanted something similar to Dropbox. I could without the instant syncing but
28it would need to be fast and had the option for me to exclude folders like
29`node_modules, .venv, .git` and folders like that.
30
31I went on a hunt for an alternative to [Dropbox](https://www.dropbox.com/)
32and found:
33
34- [Tresorit](https://tresorit.com/)
35- [Sync.com](https://sync.com)
36- [Box](https://www.box.com/)
37
38You know, the usual list of suspects. I didn't include [Google
39drive](https://drive.google.com) or [One drive](https://onedrive.live.com/)
40since they are even more draconian than Dropbox.
41
42> All this does not stem from me being paranoid but recently these companies
43> have became more and more aggressive and they keep violating our privacy when
44> they share our data with 3rd party services. It is getting out of control.
45
46So, my main problem was still there. No way of excluding a specific folder from
47syncing. And before we go into "*But you have git, isn't that enough?*", I must
48say, that many of the files (PDFs, spreadsheets, etc) I have in a `git` repo
49don't get pushed upstream to Git and I still want to have them synced across my
50computers.
51
52I initially wanted to use [rsync](https://linux.die.net/man/1/rsync) but I would
53need to then have a remote VPS or transfer between my computers directly. I
54wanted a solution where all my files could be accessible to me without my
55machine.
56
57> **WARNING: This solution will cost you money!** DigitalOcean Spaces are $5 per
58month and there are some bandwidth limitations and if you go beyond that you get
59billed additionally.
60
61Then I remembered that I could use something like
62[S3](https://en.wikipedia.org/wiki/Amazon_S3) since it has versioning and is
63fully managed. I didn't want to go down the AWS rabbit hole with this so I
64choose [DigitalOcean Spaces](https://www.digitalocean.com/products/spaces/).
65
66Then I needed a command-line tool to sync between source and target. I found
67this nice tool [s3cmd](https://s3tools.org/s3cmd) and it is in the Ubuntu
68repositories.
69
70```bash
71sudo apt install s3cmd
72```
73
74After installation will I create a new Space bucket on DigitalOcean. Remember
75the zone you will choose because you will need it when you will configure
76`s3cmd`.
77
78Then I visited [Digitalocean Applications &
79API](https://cloud.digitalocean.com/account/api/tokens) and generated **Spaces
80access keys**. Save both key and secret somewhere safe because when you will
81leave the page secret will not be available anymore to you and you will need to
82re-generate it.
83
84```bash
85# enter your key and secret and correct endpoint
86# my endpoint is ams3.digitaloceanspaces.com because
87# I created my bucket in Amsterdam regiin
88s3cmd --configure
89```
90
91After that I played around with options for `s3cmd` and got to the following
92command.
93
94```bash
95# I executed this command from my projects folder
96cd projects
97s3cmd sync --delete-removed --exclude 'node_modules/*' --exclude '.git/*' --exclude '.venv/*' ./ s3://my-bucket-name/projects/
98```
99
100When syncing int he other direction you will need to change the order of the
101`SOURCE` and `TARGET` to `s3://my-bucket-name/projects/` and `./`.
102
103> Be sure that all the paths have trailing slash so that sync knows that this
104> are directories.
105
106I am planning to implement some sort of a `.ignore` file that will enable me to
107have a project-specific exclude options.
108
109I am currently running this every hour as a cronjob which is perfectly fine for
110now when I am testing how this whole thing works and how it all will turn out.
111
112I have also created a small Gnome extension which is still very unstable, but
113when/if this whole experiment pays of I will share on Github.
diff --git a/_posts/posts/2021-01-24-replacing-dropbox-with-s3.md b/_posts/posts/2021-01-24-replacing-dropbox-with-s3.md
new file mode 100644
index 0000000..7599949
--- /dev/null
+++ b/_posts/posts/2021-01-24-replacing-dropbox-with-s3.md
@@ -0,0 +1,115 @@
1---
2title: Replacing Dropbox in favor of DigitalOcean spaces
3permalink: /replacing-dropbox-in-favor-of-digitalocean-spaces.html
4date: 2021-01-24T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10A few months ago I experimented with DigitalOcean spaces as my backup solution
11that could [replace Dropbox
12eventually](/digitalocean-spaces-to-sync-between-computers.html). That solution
13worked quite nicely, and I was amazed how smashing together a couple of existing
14solutions would work this fine.
15
16I have been running that solution in the background for a couple of months now
17and kind of forgot about it. But recent developments around deplatforming and
18having us people hostages of technology and big companies speed up my goals to
19become less dependent on
20[Google](https://edition.cnn.com/2020/12/17/tech/google-antitrust-lawsuit/index.html),
21[Dropbox](https://www.pcworld.com/article/2048680/dropbox-takes-a-peek-at-files.html)
22etc and take back some control.
23
24I am not a conspiracy theory nut, but to be honest, what these companies are
25doing lately is out of control. It is a matter of principle at this point. I
26have almost completely degoogled my life all the way from ditching Gmail,
27YouTube and most of the services surrounding Google. And I must tell you, I feel
28so good. I haven't felt this way for a long time.
29
30**Anyways. Let's get to the meat of things.**
31
32Before you continue you should read my post about [syncing to
33Dropbox](/digitalocean-spaces-to-sync-between-computers.html).
34
35> Also to note, I am using Linux on my machine with Gnome desktop environment.
36This should work on MacOS too. To use this on Windows I suggest using
37[Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
38or [Cygwin](https://www.cygwin.com/).
39
40## Folder structure
41
42I liked structure from Dropbox. One folder where everything is located and
43synced. So, that's why adopted this also for my sync setup.
44
45```go
46~/Vault
47 ↳ backup
48 ↳ bin
49 ↳ documents
50 ↳ projects
51```
52
53All of my code is located in `~/Vault/projects` folder. And most of the projects
54are Git repositories. I do not use this sync method for backup per see but in
55case I reinstall my machine I can easily recreate all the important folder
56structure with one quick command. No external drives needed that can fail etc.
57
58## Sync script
59
60My sync script is located in `~/Vault/bin/vault-backup.sh`
61
62```bash
63#!/bin/bash
64
65# dconf load /com/gexperts/Tilix/ < tilix.dconf
66# 0 2 * * * sh ~/Vault/bin/vault-backup.sh
67
68cd ~/Vault/backup/dotfiles
69
70MACHINE=$(whoami)@$(hostname)
71mkdir -p $MACHINE
72cd $MACHINE
73
74cp ~/.config/VSCodium/User/settings.json settings.json
75cp ~/.s3cfg s3cfg
76cp ~/.bash_extended bash_extended
77cp ~/.ssh ssh -rf
78
79codium --list-extensions > vscode-extension.txt
80dconf dump /com/gexperts/Tilix/ > tilix.dconf
81
82cd ~/Vault
83s3cmd sync --delete-removed --exclude 'node_modules/*' --exclude '.git/*' --exclude '.venv/*' ./ s3://bucket-name/backup/
84
85echo `date +"%D %T"` >> ~/.vault.log
86
87notify-send \
88 -u normal \
89 -i /usr/share/icons/Adwaita/96x96/status/security-medium-symbolic.symbolic.png \
90 "Vault sync succeded at `date +"%D %T"`"
91```
92
93This script also backups some of the dotfiles I use and sends notification to
94Gnome notification center. It is a straightforward solution. Nothing special
95going on.
96
97> One obvious benefit of this is that I can omit syncing Node's `node_modules`
98> or Python's `.venv` and `.git` folders.
99
100You can use this script in a combination with [Cron](https://en.wikipedia.org/wiki/Cron).
101
102```txt
1030 2 * * * sh ~/Vault/bin/vault-backup.sh
104```
105
106When you start syncing your local stuff with a remote server you can review your
107items on DigitalOcean.
108
109![Dropbox Spaces](/assets/posts/dropbox-sync/dropbox-spaces.png){:loading="lazy"}
110
111I have been using this script now for quite some time, and it's working
112flawlessly. I also uninstalled Dropbox and stopped using it completely.
113
114All I need to do is write a Bash script that does the reverse and downloads from
115remote server to local folder. This could be another post.
diff --git a/_posts/posts/2021-01-25-goaccess.md b/_posts/posts/2021-01-25-goaccess.md
new file mode 100644
index 0000000..779bce5
--- /dev/null
+++ b/_posts/posts/2021-01-25-goaccess.md
@@ -0,0 +1,205 @@
1---
2title: Using GoAccess with Nginx to replace Google Analytics
3permalink: /using-goaccess-with-nginx-to-replace-google-analytics.html
4date: 2021-01-25T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Introduction
11
12I know! You cannot simply replace Google Analytics with parsing access logs and
13displaying a couple of charts. But to be honest, I actually never used Google
14Analytics to the fullest extent and was usually interested in seeing page hits
15and which pages were visited most often.
16
17I recently moved my blog from Firebase to a VPS and also decided to remove
18Google Analytics tracking code from the site since its quite malicious and
19tracks users across other pages also and is creating a profile of a user, and
20I've had it. But I also need some insight of what is happening on a server and
21which content is being read the most etc.
22
23I have looked at many existing solutions like:
24
25- [Umami](https://umami.is/)
26- [Freshlytics](https://github.com/sheshbabu/freshlytics)
27- [Matomo](https://matomo.org/)
28
29But the more I looked at them the more I noticed that I am replacing one evil
30with another one. Don't get me wrong. Some of these solutions are absolutely
31fantastic but would require installation of databases and something like PHP or
32Node. And I was not ready to put those things on my fresh server. Also having
33Docker installed is out of the question.
34
35## Opting for log parsing
36
37So, I defaulted to parsing already existing logs and generating HTML reports
38from this data.
39
40I found this amazing software [GoAccess](https://goaccess.io/) which provides
41all the functionalities I need, and it's a single binary. Written in Go.
42
43GoAccess can be used in two different modes.
44
45![GoAccess Terminal](/assets/posts/goaccess/goaccess-dash-term.png){:loading="lazy"}
46
47*Running in a terminal*
48
49![GoAccess HTML](/assets/posts/goaccess/goaccess-dash-html.png){:loading="lazy"}
50
51*Running in a browser*
52
53I, however, need this to run in a browser. So, the second option is the way to
54go. The Idea is to periodically run cronjob and export this report into a folder
55that gets then server by Nginx behind a Basic authentication.
56
57## Getting Nginx ready
58
59I choose Ubuntu on [DigitalOcean](https://www.digitalocean.com/). First I
60installed [Nginx](https://nginx.org/en/), and
61[Letsencrypt](https://letsencrypt.org/getting-started/) certbot and all the
62necessary dependencies.
63
64```sh
65# log in as root user
66sudo su -
67
68# first let's update the system
69apt update && apt upgrade -y
70
71# let's install
72apt install nginx certbot python3-certbot-nginx apache2-utils
73```
74
75After all this is installed we can create a new configuration for a statistics.
76Stats will be available at `stats.domain.com`.
77
78```sh
79# creates directory where html will be hosted
80mkdir -p /var/www/html/stats.domain.com
81
82cp /etc/nginx/sites-available/default /etc/nginx/sites-available/stats.domain.com
83nano /etc/nginx/sites-available/stats.domain.com
84```
85
86```nginx
87server {
88 root /var/www/html/stats.domain.com;
89 server_name stats.domain.com;
90
91 index index.html;
92 location / {
93 try_files $uri $uri/ =404;
94 }
95}
96```
97
98Now we check if the configuration is ok. We can do this with `nginx -t`. If all
99is ok, we can restart Nginx with `service nginx restart`.
100
101After all that you should add A record for this domain that points to IP of a
102droplet.
103
104Before enabling SSL you should test if DNS records have propagated with `curl
105stats.domain.com`.
106
107Now, it's time to provision TLS certificate. To achieve this, you execute
108command `certbot --nginx`. Follow the wizard and when you are asked about
109redirection always choose 2 (always redirect to HTTPS).
110
111When this is done you can visit https://stats.domain.com and you should get 404
112not found error which is correct.
113
114## Getting GoAccess ready
115
116If you are using Debian like system GoAccess should be available in repository.
117Otherwise refer to the official website.
118
119```sh
120apt install goaccess
121```
122
123To enable Geo location we also need one additiona thing.
124
125```sh
126cd /var/www/html/stats.stats.com
127wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb
128```
129
130Now we create a shell script that will be executed every 10 minutes.
131
132```sh
133nano /var/www/html/stats.domain.com/generate-stats.sh
134```
135
136Contents of this file should look like this.
137
138```sh
139#!/bin/sh
140
141zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
142
143goaccess \
144 --log-file=/var/log/nginx/access-all.log \
145 --log-format=COMBINED \
146 --exclude-ip=0.0.0.0 \
147 --geoip-database=/var/www/html/stats.domain.com/GeoLite2-City.mmdb \
148 --ignore-crawlers \
149 --real-os \
150 --output=/var/www/html/stats.domain.com/index.html
151
152rm /var/log/nginx/access-all.log
153```
154
155Because after a while nginx creates multiple files with access logs we use
156[`zcat`](https://linux.die.net/man/1/zcat) to extract Gziped contents and create
157a file that has all the access logs. After this file is used we delete it.
158
159If you want to exclude your home IP's result look at the `--exclude-ip` option
160in script and instead of `0.0.0.0` add your own home IP address. You can find
161your home IP by executing `curl ifconfig.me` from your local machine and NOT
162from the droplet.
163
164Test the script by executing `sh
165/var/www/html/stats.domain.com/generate-stats.sh` and then checking
166`https://stats.domain.com`. If you can see stats instead of 404 than you are
167set.
168
169It's time to add this script to cron with `cron -e`.
170
171```go
172*/10 * * * * sh /var/www/html/stats.domain.com/generate-stats.sh
173```
174
175## Securing with Basic authentication
176
177You probably don't want stats to be publicly available, so we should create a
178user and a password for Basic authentication.
179
180First we create a password for a user `stats` with `htpasswd -c /etc/nginx/.htpasswd stats`.
181
182Now we update config file with `nano
183/etc/nginx/sites-available/stats.domain.com`. You probably noticed that the
184file looks a bit different from before. This is because `certbot` added
185additional rules for SSL.
186
187Your location portion the config file should now look like. You should add
188`auth_basic` and `auth_basic_user_file` lines to the file.
189
190```nginx
191location / {
192 try_files $uri $uri/ =404;
193 auth_basic "Private Property";
194 auth_basic_user_file /etc/nginx/.htpasswd;
195}
196```
197
198Test if config is still ok with `nginx -t` and if it is you can restart Nginx
199with `service nginx restart`.
200
201If you now visit `https://stats.domain.com` you should be prompted for username
202and password. If not, try reopening your browser.
203
204That is all. You now have analytics for your server that gets refreshed every 10
205minutes.
diff --git a/_posts/posts/2021-06-26-simple-world-clock.md b/_posts/posts/2021-06-26-simple-world-clock.md
new file mode 100644
index 0000000..d1b53b4
--- /dev/null
+++ b/_posts/posts/2021-06-26-simple-world-clock.md
@@ -0,0 +1,108 @@
1---
2title: Simple world clock with eInk display and Raspberry Pi Zero
3permalink: /simple-world-clock-with-eiink-display-and-raspberry-pi-zero.html
4date: 2021-06-26T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Our team is spread across the world, from the USA all the way to Australia, so
11having some sort of world clock makes sense.
12
13Currently, I am using an extension for Gnome called [Timezone
14extension](https://extensions.gnome.org/extension/2657/timezones-extension/),
15and it serves the purpose quite well.
16
17But I also have a bunch of electronics that I bought through the time, and I am
18not using any of them, and it's time to stop hording this stuff and use it in a
19project.
20
21A while ago I bought a small eInk display [Inky
22pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) and I
23have a bunch of [Raspberry Pi's
24Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/) lying around that
25I really need to use.
26
27![Inky pHAT, Raspberry Pi Zero](/assets/posts/world-clock/hardware.jpg){:loading="lazy"}
28
29Since the Inky [Inky
30pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) is
31essentially a HAT, it can easily be added on top of the [Raspberry Pi
32Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/).
33
34First, I installed the necessary software on Raspberry Pi with `pip3 install
35inky`.
36
37And then I created a file `clock.py` in home directory `/home/pi`.
38
39```python
40#!/usr/bin/env python
41# -*- coding: utf-8 -*-
42
43import sys
44import os
45from inky.auto import auto
46from PIL import Image, ImageFont, ImageDraw
47from font_fredoka_one import FredokaOne
48
49clocks = [
50 'America/New_York',
51 'Europe/Ljubljana',
52 'Australia/Brisbane',
53]
54
55board = auto()
56board.set_border(board.WHITE)
57board.rotation = 90
58
59img = Image.new('P', (board.WIDTH, board.HEIGHT))
60draw = ImageDraw.Draw(img)
61
62big_font = ImageFont.truetype(FredokaOne, 18)
63small_font = ImageFont.truetype(FredokaOne, 13)
64
65x = board.WIDTH / 3
66y = board.HEIGHT / 3
67
68idx = 1
69for clock in clocks:
70 ctime = os.popen('TZ="{}" date +"%a,%H:%M"'.format(clock))
71 ctime = ctime.read().strip().split(',')
72 city = clock.split('/')[1].replace('_', ' ')
73
74 draw.text((15, (idx*y)-y+10), city, fill=board.BLACK, font=small_font)
75 draw.text((110, (idx*y)-y+7), str(ctime[0]), fill=board.BLACK, font=big_font)
76 draw.text((155, (idx*y)-y+7), str(ctime[1]), fill=board.BLACK, font=big_font)
77
78 idx += 1
79
80board.set_image(img)
81board.show()
82```
83
84And because eInk displays are rather slow to refresh and the clock requires
85refreshing only once a minute, this can be done through cronjob.
86
87Before we add this job to cron we need to make `clock.py` executable with `chmod
88+x clock.py`.
89
90Then we add a cronjob with `crontab -e`.
91
92```txt
93* * * * * /home/pi/clock.py
94```
95
96So, we end up with a result like this.
97
98![World Clock](/assets/posts/world-clock/world-clock.jpg){:loading="lazy"}
99
100And for the enclosure that can be 3D printed, but I haven't yet something like
101this can be used.
102
103<iframe id="vs_iframe" src="https://www.viewstl.com/?embedded&url=https%3A%2F%2Fmitjafelicijan.com%2Fposts%2Fworld-clock%2Fenclosure.stl&color=gray&bgcolor=white&edges=no&orientation=front&noborder=no" style="border:0;margin:0;width:100%;height:400px;"></iframe>
104
105You can download my [STL file for the enclosure
106here](/assets/posts/world-clock/enclosure.stl), but make sure that dimensions make
107sense and also opening for USB port should be added or just use a drill and some
108hot glue to make it stick in the enclosure.
diff --git a/_posts/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md b/_posts/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md
new file mode 100644
index 0000000..cbcca37
--- /dev/null
+++ b/_posts/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md
@@ -0,0 +1,104 @@
1---
2title: My journey from being an internet über consumer to being a full hominum again
3permalink: /from-internet-consumer-to-full-hominum-again.html
4date: 2021-07-30T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10It's been almost a year since I started purging all my online accounts and
11going down this rabbit hole of being almost independent of the current internet
12machine. Even though I initially thought that I will have problems adapting,
13I was pleasantly surprised that the transition went so smoothly. Even better,
14it brought many benefits to my life. Such as increased focus, less stress
15about trivial things, etc.
16
17It all started with me doing small changes like unsubscribing from emails that I
18have either subscribed to by accepting terms and conditions. Or even some more
19malicious emails that I was getting because I was on a shared mailing list. And
20the later ones I hate the most of all. How the hell do they keep sharing my
21email and sending me unsolicited emails and get away with it? I have a suspicion
22that these marketing people share an Excel file between them and keep
23resubscribing me when they import lists into Mailchimp or similar software.
24
25It's fascinating to see how much crap you get subscribed to when you are not
26paying attention. It got so bad that my primary Gmail address is a full of junk
27and need constant monitoring and cleaning up. And because I want to have Inbox
28Zero, this presents an additional problem for me.
29
30The stress that email presented for me didn't occur to me for a long time. I was
31noticing that I was unable to go through one single hour without hysterically
32refreshing email. And if somebody wrote me something, I needed to see it right
33then, even though I didn't immediately reply to it. I can only describe this
34with FOMO (fear of missing out). I have no other explanation than that. It was
35crippling, and I was constantly context switching, which I will address further
36down this post in more details.
37
38This was one of the reasons why I spawned up my personal email server, and I am
39using it now as my primary and person email. I still have Gmail as my “junk”
40email that I use for throw away stuff. I log in to Gmail once a week and check
41if there are any important emails that I got, but apart from that, it's sitting
42dormant and collecting dust.
43
44The more I was watching the world loose it's self with allowing anti freedom
45things to happen to it, the more I started to realize that something has to
46change. I don't have the power to change the world. And I also don't have a
47grandiose opinion of myself to even think to try it. But what I can do is to not
48subscribe to this consumer way of thinking. I will not be complicit in this. My
49moral and ethical stances won't allow it. So, this brings us to the second part
50of my journey.
51
52I was using all these 3rd party services because I was either lazy or OK with
53the drawbacks of them. I watched these services and companies became more and
54privacy policies and everybody is OK with accepting them, and they pray on that
55more evil. It is evil if you sell your user's data in this manner. Nobody reads
56flaw in human nature. I really hate the hypocrisy they manage to muster. These
57companies prey on our laziness, and we are at fault here. Nobody else. And I
58truly understand the reasons why we rather accept and move on, and not object
59and have our lives a little more difficult. They have perfected this through
60years of small changes that make us a little more dependent on them. You could
61not convince a person to give away all his rights and data in one day. This was
62gradual and slow. And it caught us all in surprise. When I really stopped and
63thought about it, I felt repulsed. By really stopping and thinking about it, I
64really mean stopping and thinking about it. Thoroughly and in depth.
65
66Each step I took depleted my character a bit more. Like I was trading myself bit
67by bit without understanding what it all meant. What it meant to be a full
68person, not divided by all this bought attention they want from me. They don't
69just get your data, but they also take your attention away from you. They
70scatter your and go with the divide and conquer tactic from there. And a person
71divided is a person not fully there. Not at the moment. Not alive fully.
72
73I was unable to form long thoughts. Well, I thought I was. But now that I see
74what being a full person is again, I can see that I was not at my 100% back
75then.
76
77A revolt was inevitable. There was no other way of continuing my story without
78it. Without taking back my attention, my thoughts, my time, and my privacy,
79regardless of how too late it maybe is.
80
81This has nothing to do with conspiracy theories. Even less with changing the
82world. All I wanted was to get my life back in order and not waste the energy
83that could be spent in other, better places.
84
85I started reading more. I can focus now fully on things I work on. Furthermore,
86I have the mental acuity that I never had before. My mind feels sharp. I don't
87get angry so much. I can cherish the finer things in life now without the need
88to interpret them intellectually. Not only that, but I have a feeling of
89belonging again. Sense of purpose has returned with a vengeance. And I can now
90help people without depleting myself.
91
92The last step so far was to finish closing all the remaining online accounts
93that I still had. And when I was thinking what value they bring me, I wasn't
94surprised that the answer was none. I wasn't logging in them and using them. I
95stopped being afraid of FOMO. If somebody wants to get in contact me, they will
96find a way. I am one search away.
97
98We are not beholden to anybody. Our lives are our own. So dare yourself to
99delete Facebook, LinkedIn. To unsubscribe. Dare yourself to take your time and
100attention back. Use that time and energy to go for a walk without thinking about
101work. Read a book instead of reading comment on social media that you will
102forget in an hour. Enrich your life instead of wasting it. It only requires a
103small step. And you will feel the benefits immediately. Lose the weight of the
104world that is crushing you without your consent.
diff --git a/_posts/posts/2021-08-01-linux-cheatsheet.md b/_posts/posts/2021-08-01-linux-cheatsheet.md
new file mode 100644
index 0000000..b416ffa
--- /dev/null
+++ b/_posts/posts/2021-08-01-linux-cheatsheet.md
@@ -0,0 +1,288 @@
1---
2title: List of essential Linux commands for server management
3permalink: /linux-cheatsheet.html
4date: 2021-08-01T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10**Generate SSH key**
11
12```bash
13ssh-keygen -t ed25519 -C "your_email@example.com"
14
15# when no support for Ed25519 present
16ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
17```
18
19Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder.
20
21**Login to host via SSH**
22
23```bash
24# connect to host as your local username
25ssh host
26
27# connect to host as user
28ssh <user>@<host>
29
30# connect to host using port
31ssh -p <port> <user>@<host>
32```
33
34**Execute command on a server through SSH**
35
36```bash
37# execute one command
38ssh root@100.100.100.100 "ls /root"
39
40# execute many commands
41ssh root@100.100.100.100 "cd /root;touch file.txt"
42```
43
44**Displays currently logged in users in the system**
45
46```bash
47w
48```
49
50**Displays Linux system information**
51
52```bash
53uname
54```
55
56**Displays kernel release information**
57
58```bash
59uname -r
60```
61
62**Shows the system hostname**
63
64```bash
65hostname
66```
67
68**Shows system reboot history**
69
70```bash
71last reboot
72```
73
74**Displays information about the user**
75
76```bash
77sudo apt install finger
78finger <username>
79```
80
81**Displays IP addresses and all the network interfaces**
82
83```bash
84ip addr show
85```
86
87**Downloads a file from an online source**
88
89```bash
90wget https://example.com/example.tgz
91```
92
93Note: If URL contains ?, & enclose the URL in double quotes.
94
95**Compress a file with gzip**
96
97```bash
98# will not keep the original file
99gzip file.txt
100
101# will keep the original file
102gzip --keep file.txt
103```
104
105**Interactive disk usage analyzer**
106
107```bash
108sudo apt install ncdu
109
110ncdu
111ncdu <path/to/directory>
112```
113
114**Install Node.js using the Node Version Manager**
115
116```bash
117curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
118source ~/.bashrc
119
120nvm install v13
121```
122
123**Too long; didn't read**
124
125```bash
126npm install -g tldr
127
128tldr tar
129```
130
131**Combine all Nginx access logs to one big log file**
132
133```bash
134zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
135```
136
137**Set up Redis server**
138
139```bash
140sudo apt install redis-server redis-tools
141
142# check if server is running
143sudo service redis status
144
145# set and get a key value
146redis-cli set mykey myvalue
147redis-cli get mykey
148
149# interactive shell
150redis-cli
151```
152
153**Generate statistics of your webserver**
154
155```bash
156sudo apt install goaccess
157
158# check if installed
159goaccess -v
160
161# combine logs
162zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
163
164# export to single html
165goaccess \
166 --log-file=/var/log/nginx/access-all.log \
167 --log-format=COMBINED \
168 --exclude-ip=0.0.0.0 \
169 --ignore-crawlers \
170 --real-os \
171 --output=/var/www/html/stats.html
172
173# cleanup afterwards
174rm /var/log/nginx/access-all.log
175```
176
177**Search for a given pattern in files**
178
179```bash
180grep -r ‘pattern’ files
181```
182
183**Find proccess ID for a specific program**
184
185```bash
186pgrep nginx
187```
188
189**Print name of current/working directory**
190
191```bash
192pwd
193```
194
195**Creates a blank new file**
196
197```bash
198touch newfile.txt
199```
200
201**Displays first lines in a file**
202
203```bash
204# -n <x> presents the number of lines (10 by default)
205head -n 20 somefile.txt
206```
207
208**Displays last lines in a file**
209
210```bash
211# -n <x> presents the number of lines (10 by default)
212tail -n 20 somefile.txt
213
214# -f follows the changes in file (doesn't closes)
215tail -f somefile.txt
216```
217
218**Count lines in a file**
219
220```bash
221wc -l somefile.txt
222```
223
224**Find all instances of the file**
225
226```bash
227sudo apt install mlocate
228
229locate somefile.txt
230```
231
232**Find file names that begin with ‘index’ in /home folder**
233
234```bash
235find /home/ -name "index"
236```
237
238**Find files larger than 100MB in the home folder**
239
240```bash
241find /home -size +100M
242```
243
244**Displays block devices related information**
245
246```bash
247lsblk
248```
249
250**Displays free space on mounted systems**
251
252```bash
253df -h
254```
255
256**Displays free and used memory in the system**
257
258```bash
259free -h
260```
261
262**Displays all active listening ports**
263
264```bash
265sudo apt install net-tools
266
267netstat -pnltu
268```
269
270**Kill a process violently**
271
272```bash
273kill -9 <pid>
274```
275
276**List files opened by user**
277
278```bash
279lsof -u <user>
280```
281
282**Execute "df -h", showing periodic updates**
283
284```bash
285# -n 1 means every second
286watch -n 1 df -h
287```
288
diff --git a/_posts/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md b/_posts/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md
new file mode 100644
index 0000000..4f9bc09
--- /dev/null
+++ b/_posts/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md
@@ -0,0 +1,277 @@
1---
2title: Debian based riced up distribution for Developers and DevOps folks
3permalink: /debian-based-riced-up-distribution-for-developers-and-devops-folks.html
4date: 2021-12-03T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Introduction
11
12I have been using [Ubuntu](https://ubuntu.com/) for quite a longtime now. I have
13used [Debian](https://www.debian.org/) in the past and
14[Manjaro](https://manjaro.org/). Also had [Arch](https://archlinux.org/) for
15some time and even ran [Gentoo](https://www.gentoo.org/) way back.
16
17What I learned from all this is that I prefer running a bit older versions and
18having them be stable than run bleeding edge rolling release. For that reason, I
19stuck with Ubuntu for a couple of years now. I am also at a point in my life
20where I just don't care what is cool or hip anymore. I just want a stable system
21that doesn't get in my way.
22
23During all this, I noticed that these distributions were getting very bloated
24and a lot of software got included that I usually uninstall on fresh
25installation. Maybe this is my OCD speaking, but why do I have to give fresh
26installation min 1 GB of ram out of the box just to have a blank screen in front
27of me? I get it, there are many things included in the distro to make my life
28easier. I understand. But at this point I have a feeling that modern Linux
29distributions are becoming similar to [Node.js project with
30node_modules](https://devhumor.com/content/uploads/images/August2017/node-modules.jpg).
31Just a crazy number of packages serving very little or no purpose, just
32supporting other software.
33
34I felt I needed a fresh start. To start over with something minimal and clean.
35Something that would put a little more joy into using a computer again.
36
37For the first version, I wanted to target the following machines I have at home
38that I want this thing to work on.
39
40```yaml
41# My main stationary work machine
42Resolution: 3840x1080 (Super Ultrawide Monitor 32:9)
43CPU: Intel i7-8700 (12) @ 4.600GHz
44GPU: AMD ATI Radeon RX 470/480/570/570X/580/580X/590
45Memory: 32020MiB
46```
47
48```yaml
49# Thinkpad x220 for testing things and goofing around
50Resolution: 1366x768
51CPU: Intel i5-2520M (4) @ 3.200GHz
52GPU: Intel 2nd Generation Core Processor Family
53Memory: 15891MiB
54```
55
56## How should I approach this?
57
58I knew I wanted to use [minimal Debian netinst
59](https://www.debian.org/CD/netinst/) for the base to give myself a head
60start. No reason to go through changing the installer and also testing all that
61behemoth of a thing. So, some sort of ricing was the only logical option to get
62this thing of the grounds somewhat quickly.
63
64> **What is ricing anyway?**
65> The term “RICE” stands for Race Inspired Cosmetic Enhancement. A group of
66> people (could be one, idk) decided to see if they could tweak their own
67> distros like they/others did their cars. This gave rise to a community of
68> Linux/Unix enthusiasts trying to make their distros look cooler and better
69> than others... For more information, read this article
70> [What in the world is ricing!?](https://pesos.github.io/2020/07/14/what-is-ricing.html).
71
72I didn't want this to just be a set of config files for theming purpose. I
73wanted this to include a set of pre-installed tools and services that are being
74used all the time by a modern developer. Theming is just a tiny part of it.
75Fonts being applied across the distro and things like that.
76
77First, I choose terminal installer and left it to load additional components.
78Avoid using graphical installer in this case.
79
80![](/assets/posts/dfd-rice/install-00.png){:loading="lazy"}
81
82After that I selected hostname and created a normal user and set password for
83that user and root user and choose guided mode for disk partitioning.
84
85![](/assets/posts/dfd-rice/install-01.png){:loading="lazy"}
86
87I left it run to install all the things required for the base system and opted
88out of scanning additional media for use by the package manager. Those will be
89downloaded from the internet during installation.
90
91![](/assets/posts/dfd-rice/install-02.png){:loading="lazy"}
92
93I opted out of the popularity contest, and **now comes the important part**.
94Uncheck all the boxes in Software selection and only leave 'standard system
95utilities'. I also left an SSH server, so I was able to log in to the machine
96from my main PC.
97
98![](/assets/posts/dfd-rice/install-03.png){:loading="lazy"}
99
100At this point, I installed GRUB bootloader on the disk where I installed the
101system.
102
103![](/assets/posts/dfd-rice/install-04.png){:loading="lazy"}
104
105That concluded the installation of base Debian and after restarting the computer
106I was prompted with the login screen.
107
108![](/assets/posts/dfd-rice/install-05.png){:loading="lazy"}
109
110Now that I had the base installation, it was time to choose what software do I
111want to include in this so-called distribution. I wanted out of the box
112developer experience, so I had plenty to choose.
113
114Let's not waste time and go through the list.
115
116## Desktop environments
117
118I have been using [Gnome](https://www.gnome.org/) for my whole Linux life. From
119version 2 forward. It's been quite a ride. I hated version 3 when it came out
120and replaced version 2. But I got used to it. And now with version 40+ they also
121made couple of changes which I found both frustrating and presently surprised.
122
123The amount of vertical space you loose because of the beefy title bars on
124windows is ridiculous. And then in case of
125[Tilix](https://gnunn1.github.io/tilix-web/) you also have tabs, and you are
126100px deep. Vertical space is one of the most important things for a
127developer. The more real estate you have, the more code you can have in a
128viewport.
129
130But on the other hand, I still love how Gnome feels and looks. I gotta give them
131that. They really are trying to make Gnome feel unified and modern.
132
133Regardless of all the nice things Gnome has, I was looking at the tiling window
134managers for some time, but never had the nerve to actually go with it. But now
135was the ideal time to give it a go. No guts, no glory kind of a thing.
136
137One of the requirements for me was easy custom layouts because I use a really
138strange monitor with aspect ratio of 32:9. So relying on included layouts most
139of them have is a non-starter.
140
141What I was doing in Gnome was having windows in a layout like the diagram
142below. This is my common practice. And if you look at it you can clearly see I
143was replicating tiling window manager setup in Gnome.
144
145![](/assets/posts/dfd-rice/layout.png){:loading="lazy"}
146
147That made me look into a bunch of tiling window managers and then tested them
148out. Candidates I was looking at were:
149
150- [i3](https://i3wm.org/)
151- [bspwm](https://github.com/baskerville/bspwm)
152- [awesome](https://awesomewm.org/index.html)
153- [XMonad](https://xmonad.org/)
154- [sway](https://swaywm.org/)
155- [Qtile](http://www.qtile.org/)
156- [dwm](https://dwm.suckless.org/)
157
158You can also check article [13 Best Tiling Window Managers for
159Linux](https://www.tecmint.com/best-tiling-window-managers-for-linux/) I was
160referencing while testing them out.
161
162While all of them provided what I needed, I liked i3 the most. What particular
163caught my eye was the ease to use and tree based layouts which allows flexible
164layouts. I know others can be set up also to have custom layouts other than
165spiral, dwindle etc. I think i3 is a good entry-level window manager for
166somebody like me.
167
168## Batteries included
169
170The source for the whole thing is located on Github
171https://github.com/mitjafelicijan/dfd-rice.
172
173Currenly included:
174
175- `non-free` (enables non-free packages in apt)
176- `sudo` (adds sudo and adds user to sudo group)
177- `essentials` (gcc, htop, zip, curl, etc...)
178- `wifi` (network manager nmtui)
179- `desktop` (i3, dmenu, fonts, configurations)
180- `pulseaudio` (pulseaudio with pavucontrol)
181- `code-editors` (vim, micro, vscode)
182- `ohmybash` (make bash pretty)
183- `file-managers` (mc)
184- `git-ui` (terminal git gui)
185- `meld` (diff tool)
186- `profiling` (kcachegrind, valgrind, strace, ltrace)
187- `browsers` (brave, firefox, chromium)
188- programming languages:
189 - `python`
190 - `golang`
191 - `nodejs`
192 - `rust`
193 - `nim`
194 - `php`
195 - `ruby`
196- `docker` (with docker-compose)
197- `ansible`
198
199Install script also allows you to install only specific packages (example for:
200essentials ohmybash docker rust).
201
202```sh
203su - root \
204 bash -c "$(wget -q https://raw.github.com/mitjafelicijan/dfd-rice/master/tools/install.sh -O -)" -- \
205 essentials ohmybash docker rust
206```
207
208Currently, most of these recipes use what Debian and this is totally fine with
209me since I never use bleeding edge features of a package. But if something major
210would come to light, I will replace it with a possible compilation script or
211something similar.
212
213This is some of the output from the installation script.
214
215![](/assets/posts/dfd-rice/script.png){:loading="lazy"}
216
217Let's take a look at some examples in the installation script.
218
219### Docker recipe
220
221```sh
222# docker
223print_header "Installing Docker"
224curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
225echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
226apt update
227apt -y install docker-ce docker-ce-cli containerd.io docker-compose
228
229systemctl start docker
230systemctl enable docker
231systemctl status docker --no-pager
232
233/sbin/usermod -aG docker $USERNAME
234```
235
236### Making bash pretty
237
238I really like [Oh My Zsh](https://ohmyz.sh/), but I don't like zsh shell. When
239I used it, I constantly needed to be aware of it and running bash scripts was a
240pain. So, I was really delighted when I found out that a version for bash
241existed called [Oh My Bash](https://ohmybash.nntoan.com/). Let's take a look at
242the recipe for installing it.
243
244```sh
245# ohmybash
246print_header "Enabling OhMyBash"
247sudo -u $USERNAME sh -c "$(curl -fsSL https://raw.github.com/ohmybash/oh-my-bash/master/tools/install.sh)" &
248T1=${!}
249wait ${T1}
250```
251
252Because OhMyBash does `exec bash` at the end, this traps our script inside
253another shell and our script cannot continue. For that reason, I executed this
254in background. But that presents a new problem. Because this is executed in
255background, we lose track of progress naturally. And that strange trick with
256`T1=${!}` and `wait ${T1}` waits for the background process to finish before
257continuing to another task in bash script.
258
259Check [Multi-Threaded Processing in Bash Scripts](https://www.cloudsavvyit.com/12277/how-to-use-multi-threaded-processing-in-bash-scripts/)
260for more details.
261
262## Conclusion
263
264Take a look at
265https://github.com/mitjafelicijan/dfd-rice/blob/develop/tools/install.sh script
266to get familiar with it. This is just a first iteration and I will continue to
267update it because I need this in my life.
268
269The current version boots in 4s to the login prompt, and after you log in, the
270desktop environment loads in 2s. So, its fast, very fast. And on clean boot, I
271measured ~230 MB of RAM usage.
272
273And this is how it looks with two terminals side by side. I really like the
274simplicity and clean interface. I will polish the colors and stuff like that,
275but I really do like the results.
276
277![](/assets/posts/dfd-rice/desktop.png){:loading="lazy"}
diff --git a/_posts/posts/2021-12-25-running-golang-application-as-pid1.md b/_posts/posts/2021-12-25-running-golang-application-as-pid1.md
new file mode 100644
index 0000000..edd5a57
--- /dev/null
+++ b/_posts/posts/2021-12-25-running-golang-application-as-pid1.md
@@ -0,0 +1,348 @@
1---
2title: Running Golang application as PID 1 with Linux kernel
3permalink: /running-golang-application-as-pid1.html
4date: 2021-12-25T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Unikernels, kernels, and alike
11
12I have been reading a lot about
13[unikernernels](https://en.wikipedia.org/wiki/Unikernel) lately and found them
14very intriguing. When you push away all the marketing speak and look at the
15idea, it makes a lot of sense.
16
17> A unikernel is a specialized, single address space machine image constructed
18> by using library operating systems. ([Wikipedia](https://en.wikipedia.org/wiki/Unikernel))
19
20I really like the explanation from the article
21[Unikernels: Rise of the Virtual Library Operating System](https://queue.acm.org/detail.cfm?id=2566628).
22Really worth a read.
23
24If we compare a normal operating system to a unikernel side by side, they would
25look something like this.
26
27![Virtual machines vs Containers vs Unikernels](/assets/posts/pid1/unikernels.webp){:loading="lazy"}
28
29From this image, we can see how the complexity significantly decreases with
30the use of Unikernels. This comes with a price, of course. Unikernels are hard
31to get running and require a lot of work since you don't have an actual proper
32kernel running in the background providing network access and drivers etc.
33
34So as a half step to make the stack simpler, I started looking into using
35Linux kernel as a base and going from there. I came across this
36[Youtube video talking about Building the Simplest Possible Linux System](https://www.youtube.com/watch?v=Sk9TatW9ino)
37by [Rob Landley](https://landley.net) and apart from statically compiling the
38application to be run as PID1 there was really no other obstacles.
39
40## What is PID 1?
41
42PID 1 is the first process that Linux kernel starts after the boot process.
43It also has a couple of unique properties that are unique to it.
44
45- When the process with PID 1 dies for any reason, all other processes are
46 killed with KILL signal.
47- When any process having children dies for any reason, its children are
48 re-parented to process with PID 1.
49- Many signals which have default action of Term do not have one for PID 1.
50- When the process with PID 1 dies for any reason, kernel panics, which
51 result in system crash.
52
53PID 1 is considered as an Init application which takes care of running other
54and handling services like:
55
56- sshd,
57- nginx,
58- pulseaudio,
59- etc.
60
61If you are on a Linux machine, you can check what your process is with PID 1
62by running the following.
63
64```sh
65$ cat /proc/1/status
66Name: systemd
67Umask: 0000
68State: S (sleeping)
69Tgid: 1
70Ngid: 0
71Pid: 1
72PPid: 0
73...
74```
75
76As we can see on my machine the process with id of 1 is [systemd](https://systemd.io/)
77which is a software suite that provides an array of system components for Linux
78operating systems. If you look closely you can also see that the `PPid`
79(process id of the parent process) is `0` which additionally confirms that
80this process doesn't have a parent.
81
82## So why even run application as PID 1 instead of just using a container?
83
84Containers are wonderful, but they come with a lot of baggage. And because they
85are in their nature layered, the images require quite a lot of space and also a
86lot of additional software to handle them. They are not as lightweight as they
87seem, and many popular images require 500 MB plus disk space.
88
89The idea of running this as PID 1 would result in a significantly smaller footprint,
90as we will see later in the post.
91
92> You could run a simple init system inside Docker container described more
93> in this article [Docker and the PID 1 zombie reaping problem](https://blog.phusion.nl/2015/01/20/docker-and-the-pid-1-zombie-reaping-problem/).
94
95## The master plan
96
971. Compile Linux kernel with the default definitions.
982. Prepare a Hello World application in Golang that is statically compiled.
993. Run it with [QEMU](https://www.qemu.org/) and providing Golang application
100 as init application / PID 1.
101
102For the sake of simplicity we will not be cross-compiling any of it and just
103use the 64bit version.
104
105## Compiling Linux kernel
106
107```sh
108$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.7.tar.xz
109$ tar xf linux-5.15.7.tar.xz
110
111$ cd linux-5.15.7
112
113$ make clean
114
115# read more about this https://stackoverflow.com/a/41886394
116$ make defconfig
117
118$ time make -j `nproc`
119
120$ cd ..
121```
122
123At this point we have kernel image that is located in `arch/x86_64/boot/bzImage`.
124We will use this in QEMU later.
125
126To make our lives a bit easier lets move the kernel image to another place.
127Lets create a folder `bin/` in the root of our project with `mkdir -p bin`.
128
129
130At this point we can copy `bzImage` to `bin/` folder with
131`cp linux-5.15.7/arch/x86_64/boot/bzImage bin/bzImage`.
132
133The folder structure of this experiment should look like this.
134
135```txt
136pid1/
137 bin/
138 bzImage
139 linux-5.15.7/
140 linux-5.15.7.tar.xz
141```
142
143## Preparing PID 1 application in Golang
144
145This step is relatively easy. The only thing we must have in mind that we will
146need to compile the binary as a static one.
147
148Let's create `init.go` file in the root of the project.
149
150```go
151package main
152
153import (
154 "fmt"
155 "time"
156)
157
158func main() {
159 for {
160 fmt.Println("Hello from Golang")
161 time.Sleep(1 * time.Second)
162 }
163}
164```
165
166If you notice, we have a forever loop in the main, with a simple sleep of 1
167second to not overwhelm the CPU. This is because PID 1 should never complete
168and/or exit. That would result in a kernel panic. Which is BAD!
169
170There are two ways of compiling Golang application. Statically and dynamically.
171
172To statically compile the binary, use the following command.
173
174```sh
175$ go build -ldflags="-extldflags=-static" init.go
176```
177
178We can also check if the binary is statically compiled with:
179
180```sh
181$ file init
182init: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=Ypu8Zw_4NBxm1Yxg2OYO/H5x721rQ9uTPiDVh-VqP/vZN7kXfGG1zhX_qdHMgH/9vBfmK81tFrygfOXDEOo, not stripped
183
184$ ldd init
185not a dynamic executable
186```
187
188At this point, we need to create [initramfs](https://www.linuxfromscratch.org/blfs/view/svn/postlfs/initramfs.html)
189(abbreviated from "initial RAM file system", is the successor of initrd. It
190is a cpio archive of the initial file system that gets loaded into memory
191during the Linux startup process).
192
193```sh
194$ echo init | cpio -o --format=newc > initramfs
195$ mv initramfs bin/initramfs
196```
197
198The projects at this stage should look like this.
199
200```txt
201pid1/
202 bin/
203 bzImage
204 initramfs
205 linux-5.15.7/
206 linux-5.15.7.tar.xz
207 init.go
208```
209
210## Running all of it with QEMU
211
212[QEMU](https://www.qemu.org/) is a free and open-source hypervisor. It emulates
213the machine's processor through dynamic binary translation and provides a set
214of different hardware and device models for the machine, enabling it to run a
215variety of guest operating systems.
216
217```sh
218$ qemu-system-x86_64 -serial stdio -kernel bin/bzImage -initrd bin/initramfs -append "console=ttyS0" -m 128
219```
220
221```sh
222$ qemu-system-x86_64 -serial stdio -kernel bin/bzImage -initrd bin/initramfs -append "console=ttyS0" -m 128
223[ 0.000000] Linux version 5.15.7 (m@khan) (gcc (GCC) 11.2.1 20211203 (Red Hat 11.2.1-7), GNU ld version 2.37-10.fc35) #7 SMP Mon Dec 13 10:23:25 CET 2021
224[ 0.000000] Command line: console=ttyS0
225[ 0.000000] x86/fpu: x87 FPU will use FXSAVE
226[ 0.000000] signal: max sigframe size: 1440
227[ 0.000000] BIOS-provided physical RAM map:
228[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
229[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
230[ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
231[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fdffff] usable
232[ 0.000000] BIOS-e820: [mem 0x0000000007fe0000-0x0000000007ffffff] reserved
233[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
234[ 0.000000] NX (Execute Disable) protection: active
235[ 0.000000] SMBIOS 2.8 present.
236[ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-6.fc35 04/01/2014
237[ 0.000000] tsc: Fast TSC calibration failed
238...
239[ 2.016106] ALSA device list:
240[ 2.016329] No soundcards found.
241[ 2.053176] Freeing unused kernel image (initmem) memory: 1368K
242[ 2.056095] Write protecting the kernel read-only data: 20480k
243[ 2.058248] Freeing unused kernel image (text/rodata gap) memory: 2032K
244[ 2.058811] Freeing unused kernel image (rodata/data gap) memory: 500K
245[ 2.059164] Run /init as init process
246Hello from Golang
247[ 2.386879] tsc: Refined TSC clocksource calibration: 3192.032 MHz
248[ 2.387114] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x2e02e31fa14, max_idle_ns: 440795264947 ns
249[ 2.387380] clocksource: Switched to clocksource tsc
250[ 2.587895] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
251Hello from Golang
252Hello from Golang
253Hello from Golang
254```
255
256The whole [log file here](/assets/posts/pid1/qemu.log).
257
258## Size comparison
259
260The cool thing about this approach is that the Linux kernel and the application
261together only take around 12 MB, which is impressive as hell. And we need to
262also know that the size of bzImage (Linux kernel) could be greatly decreased
263by going into `make menuconfig` and removing a ton of features from the kernel,
264making the size even smaller. I managed to get kernel size down to 2 MB and
265still working properly.
266
267```sh
268total 12M
269-rw-r--r--. 1 m m 9.3M Dec 13 10:24 bzImage
270-rw-r--r--. 1 m m 1.9M Dec 27 01:19 initramfs
271```
272
273## Creating ISO image and running it with Gnome Boxes
274
275First we need to create proper folder structure with `mkdir -p iso/boot/grub`.
276
277Then we need to download the [grub binary](https://github.com/littleosbook/littleosbook/raw/master/files/stage2_eltorito).
278You can read more about this program on https://github.com/littleosbook/littleosbook.
279
280```sh
281$ wget -O iso/boot/grub/stage2_eltorito https://github.com/littleosbook/littleosbook/raw/master/files/stage2_eltorito
282```
283
284```sh
285$ tree iso/boot/
286iso/boot/
287├── bzImage
288├── grub
289│   ├── menu.lst
290│   └── stage2_eltorito
291└── initramfs
292```
293
294Let's copy files into proper folders.
295
296
297```sh
298$ cp stage2_eltorito iso/boot/grub/
299$ cp bin/bzImage iso/boot/
300$ cp bin/initramfs iso/boot/
301```
302
303Lets create a GRUB config file at `nano iso/boot/grub/menu.lst` with contents.
304
305```ini
306default=0
307timeout=5
308
309title GoAsPID1
310kernel /boot/bzImage
311initrd /boot/initramfs
312```
313
314Let's create iso file by using genisoimage:
315
316```sh
317genisoimage -R \
318 -b boot/grub/stage2_eltorito \
319 -no-emul-boot \
320 -boot-load-size 4 \
321 -A os \
322 -input-charset utf8 \
323 -quiet \
324 -boot-info-table \
325 -o GoAsPID1.iso \
326 iso
327```
328
329This will produce `GoAsPID1.iso` which you can use with [Virtualbox](https://www.virtualbox.org/)
330or [Gnome Boxes](https://apps.gnome.org/app/org.gnome.Boxes/).
331
332<video src="/assets/posts/pid1/boxes.mp4" controls></video>
333
334## Is running applications as PID 1 even worth it?
335
336Well, the answer to this is not as simple as one would think. Sometimes it is
337and sometimes it's not. For embedded systems and very specialized applications
338it is worth for sure. But in normal uses, I don't think so. It was an interesting
339exercise in compiling kernels and looking at the guts of the Linux kernel,
340but sticking to containers for most of the things is a better option in my
341opinion.
342
343An interesting experiment would be creating an image that supports networking
344and could be deployed to AWS as an EC2 instance and observing how it fares.
345But in that case, we would need to write some sort of supervisor that would
346run on a separate EC2 that would check if other EC2 instances are running
347properly. Remember that if your application fails, kernel panics and the
348whole machine is inoperable in this case.
diff --git a/_posts/posts/2021-12-30-wap-mobile-web-before-the-web.md b/_posts/posts/2021-12-30-wap-mobile-web-before-the-web.md
new file mode 100644
index 0000000..665be0f
--- /dev/null
+++ b/_posts/posts/2021-12-30-wap-mobile-web-before-the-web.md
@@ -0,0 +1,203 @@
1---
2title: Wireless Application Protocol and the mobile web before the web
3permalink: /wap-mobile-web-before-the-web.html
4date: 2021-12-30T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## A little stroll down the history lane
11
12About two weeks ago, I watched this outstanding documentary on YouTube
13[Springboard: the secret history of the first real
14smartphone](https://www.youtube.com/watch?v=b9_Vh9h3Ohw) about the history of
15smartphones and phones in general. It brought back so many memories. I never had
16an actual smartphone before the Android. The closest to smartphone was [Sony
17Ericsson P1](https://www.gsmarena.com/sony_ericsson_p1-1982.php). A fantastic
18phone and I broke it in Prague after a party and that was one of those rare
19occasions where I was actually mad at myself. But nevertheless, after that
20phone, the next one was an Android one.
21
22Before that, I only owned normal phones from Nokia and Siemens etc. Nothing
23special, actually. These are the phones we are talking about. Before 2007.
24Apple and Android phones didn't exist yet.
25
26These phones were rocking:
27
28- No selfie cameras.
29- ~2 inch displays.
30- ~120 MHz beast CPU's.
31- 144p main cameras.
32- But they had a headphone jack.
33
34Let's take a look at these beauties.
35
36![Old phones](/assets/posts/wap/phones.gif){:loading="lazy"}
37
38## WAP - Wireless Application Protocol
39
40Not that one! We are talking about Wireless Application Protocol and not Cardi
41B's song 😃
42
43WAP stands for Wireless Application Protocol. It is a protocol designed for
44micro-browsers, and it enables the access of internet in the mobile devices. It
45uses the mark-up language WML (Wireless Markup Language and not HTML), WML is
46defined as XML 1.0 application. Furthermore, it enables creating web
47applications for mobile devices. In 1998, WAP Forum was founded by Ericson,
48Motorola, Nokia and Unwired Planet whose aim was to standardize the various
49wireless technologies via protocols.
50[(source)](https://www.geeksforgeeks.org/wireless-application-protocol/)
51
52WAP protocol was resulted by the joint efforts of the various members of WAP
53Forum. In 2002, WAP forum was merged with various other forums of the industry,
54resulting in the formation of Open Mobile Alliance (OMA).
55[(source)](https://www.geeksforgeeks.org/wireless-application-protocol/)
56
57These were some wild times. Devices had tiny screens and data transmission rates
58were abominable. But they were capable of rendering WML (Wireless Markup
59Language). This was very similar to HTML, actually. It is a markup language,
60after all.
61
62These pages could be served by [Apache](https://apache.org/) and could be
63generated by CGI scripts on the backend. The only difference was the limited
64markup language.
65
66## WML - Wireless Markup Language
67
68Just like web browsers use HTML for content structure, older mobile device
69browsers use WML - if you need to support really old mobile phones using WML
70browsers, you will need to know about it. WML is XML-based (an XML vocabulary
71just like XHTML and MathML, but not HTML) and does not use the same metaphor as
72HTML. HTML is a single document with some metadata packed away in the head, and
73a body encapsulating the visible page. With WML, the metaphor does not envisage
74a page, but rather a deck of cards. A WML file might have several pages or cards
75contained within it.
76[(source)](https://www.w3.org/wiki/Introduction_to_mobile_web)
77
78```html
79<?xml version="1.0"?>
80<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
81<wml>
82 <card id="home" title="Example Homepage">
83 <p>Welcome to the Example homepage</p>
84 </card>
85</wml>
86```
87
88There is an amazing tutorial on [Tutorialpoint about
89WML](https://www.tutorialspoint.com/wml/index.htm).
90
91## Converting Digg to WML
92
93This task is completely useless and not really feasible nowadays, but I had to
94give it a try for old-time sake. Since the data is already there in a form of
95RSS feed, I could take this feed and parse it and create a WML version of the
96homepage.
97
98We will need:
99
100- Python3 + Pip
101- ImageMagick
102- feedparser and mako templating
103
104```sh
105# for fedora 35
106sudo dnf install ImageMagick python3-pip
107
108# tempalting engine for python
109pip install mako --user
110
111# for parsing rss feeds
112pip install feedparser --user
113```
114
115Project folder structure should look like the following.
116
117```
11812:43:53 m@khan wap → tree -L 1
119.
120├── generate.py
121└── template.wml
122
123```
124
125After that, I created a small template for the homepage.
126
127```html
128<?xml version="1.0"?>
129<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.xml">
130
131<wml>
132
133 <card title="Digg - What the Internet is talking about right now">
134
135 % for item in entries:
136 <p><img src="/images/${item.id}.jpg" width="175" height="95" alt="${item.title}" /></p>
137 <p><small>${item.kicker}</small></p>
138 <p><big><b>${item.title}</b></big></p>
139 <p>${item.description}</p>
140 % endfor
141
142 </card>
143
144</wml>
145```
146
147And the parser that parses RSS feed looks like this.
148
149```python
150import os
151import feedparser
152from mako.template import Template
153
154os.system('mkdir -p www/images')
155
156template = Template(filename='template.wml')
157
158feed = feedparser.parse('https://digg.com/rss/top.xml')
159
160entries = feed.entries[:15]
161
162for entry in entries:
163 print('Processing image with id {}'.format(entry.id))
164 os.system('wget -q -O www/images/{}.jpg "{}"'.format(entry.id, entry.links[1].href))
165 os.system('convert www/images/{}.jpg -type Grayscale -resize 175x -depth 3 -quality 30 www/images/{}.jpg'.format(entry.id, entry.id))
166
167html = template.render(entries = entries)
168
169with open('www/index.wml', 'w+') as fp:
170 fp.write(html)
171```
172
173This script will create a folder `www` and in the folder `www/images` for
174storing resized images.
175
176> Be sure you don't use SSL and use just normal HTTP for serving the content.
177> These old phones will have problems with TLS 1.3 etc.
178
179If you look at the python file, I convert all the images into tiny B&W images.
180They should be WBMP (Wireless BitMaP) but I choose JPEGs for this, and it seems
181to work properly.
182
183Because I currently don't have a phone old enough to test it on, I used an
184emulator. And it was really hard to find one. I found [WAP
185Proof](http://wap-proof.sharewarejunction.com/) on shareware junction, and it
186did the job well enough. I will try to find and actual device to test it on.
187
188<video src="/assets/posts/wap/emulator.mp4" controls></video>
189
190If you are using Nginx to serve the contents, add a directive to the hosts file
191that will automatically server `index.wml` file.
192
193```nginx
194server {
195 index index.wml index.html index.htm index.nginx-debian.html;
196}
197```
198
199## Conclusion
200
201Well, this was pointless, but very fun! I hope you enjoyed it as much as I did.
202I will try to find an old phone to test it on. If you have any questions, feel
203free to ask in the comments.
diff --git a/_posts/posts/2022-06-30-trying-out-helix-editor.md b/_posts/posts/2022-06-30-trying-out-helix-editor.md
new file mode 100644
index 0000000..be369a1
--- /dev/null
+++ b/_posts/posts/2022-06-30-trying-out-helix-editor.md
@@ -0,0 +1,55 @@
1---
2title: Trying out Helix code editor as my main editor
3permalink: /tying-out-helix-code-editor.html
4date: 2022-06-30T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10I have been searching for a lightweight code editor for quite some time. One of
11the main reasons was that I wanted something that doesn't burn through CPU and
12RAM usage is not through the roof. I have been mostly using Visual Studio Code.
13It's been an outstanding editor. I have no quarrel with it at all. It's just
14time to spice life up with something new.
15
16I have been on this search for a couple of years. I have tried Vim, Neovim,
17Emacs, Doom Emacs, Micro and couple more. Among most of them, I liked Micro and
18Doom Emacs the most. Micro editor was a little too basic for me. And Doom Emacs
19was a bit too hardcore. This does not reflect on any of the editors. It's just
20my personal preference.
21
22> I tried Helix Editor about a year ago. But I didn't pay attention to it.
23> Tried it and saw it's similar to Vi and just said no. I was premature to
24> dismiss it.
25
26One of the things I actually miss is line wrapping for certain files. When
27writing Markdown, line wrapping would be very helpful. Editing such a document
28is frustrating to say the least. Some of the Markdown to HTML converters don't
29take kindly of new lines between sentences. Not paragraphs, sentences. And I use
30Markdown to write this blog you are reading.
31
32But other than this, I have been extremely satisfied by it. It's been a pleasant
33surprise. There have been zero issues with the editor.
34
35One thing to do before you are able to use autocompletion and make use Language
36Server support is to install the language server with NPM.
37
38```sh
39# For C development this installs C LSP.
40sudo dnf install clang-tools-extra
41```
42
43I am still getting used to the keyboard shortcuts and getting better. What Helix
44does really well is packing in sane defaults and even though because currently
45there is no plugin support I haven't found any need for them. It has all that
46you would need. It goes to extreme measures to show a user what is going on with
47popups that show you what the keyboard shortcuts are.
48
49And it comes us packed with many
50[really good themes](https://github.com/helix-editor/helix/wiki/Themes).
51
52![Editor](/assets/posts/helix-editor/editor.png){:loading="lazy"}
53
54It's still young but has this mature feeling to it. It has sane defaults and
55mimics Vim (works a bit differently, but the overall idea is similar).
diff --git a/_posts/posts/2022-07-05-what-would-dna-sound-if-synthesized.md b/_posts/posts/2022-07-05-what-would-dna-sound-if-synthesized.md
new file mode 100644
index 0000000..6efe559
--- /dev/null
+++ b/_posts/posts/2022-07-05-what-would-dna-sound-if-synthesized.md
@@ -0,0 +1,365 @@
1---
2title: What would DNA sound if synthesized to an audio file
3permalink: /what-would-dna-sound-if-synthesized.html
4date: 2022-07-05T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Introduction
11
12Lately, I have been thinking a lot about the nature of life, what are the
13foundation blocks of life and things like that. It's remarkable how complex and
14on the other hand simple the creation is when you look at it. The miracle of
15life keeps us grounded when our imagination goes wild. If the DNA are the blocks
16of life, you could consider them to be an API nature provided us to better
17understand all of this chaos masquerading as order.
18
19I have been reading a lot about superintelligence and our somehow misguided path
20to create general artificial intelligence. What would the building blocks or our
21creation look like? Is the compression really the ultimate storage of
22information? Will our creation also ponder this questions when creating new
23worlds for themselves, or will we just disappear into the vastness of
24possibilities? It is a little offensive that we are playing God whilst being
25completely ignorant of our own reality. Who knows! Like many other
26breakthroughs, this one will also come at a cost not known to us when it finally
27happens.
28
29To keep things a bit lighter, I decided to convert some popular DNA sequences
30into an audio files for us to listen to. I am not the first one, nor I will be
31the last one to do this. But it is an interesting exercise in better
32understanding the relationship between art and science. Maybe listening to DNA
33instead of parsing it will find a way into better understanding, or at least
34enjoying the creation and cryptic nature of life.
35
36## DNA encoding and primer example
37
38I have been exploring DNA in the past in my post from about 3 years ago in
39[Encoding binary data into DNA
40sequence](/encoding-binary-data-into-dna-sequence.html) where I have been
41converting all sorts of data into DNA sequences.
42
43This will be a similar exercise but instead of converting to DNA, I will be
44generating tones from Nucleotides.
45
46| Nucleotides | Note | Frequency |
47| ---------------- | ---- | --------- |
48| **A** (Adenine) | A | 440 Hz |
49| **C** (Cytosine) | C | 783.99 Hz |
50| **G** (Guanine) | G | 523.25 Hz |
51| **T** (Thymine) | D | 587.33 Hz |
52
53Since we do not have T in equal-tempered scale, I choose D to represent T note.
54
55You can check [Frequencies for equal-tempered scale, A4 = 440
56Hz](https://pages.mtu.edu/~suits/notefreqs.html). For this tuning, we also
57choose `Speed of Sound = 345 m/s = 1130 ft/s = 770 miles/hr`.
58
59Now that we have this out of the way, we can also brush up on the DNA sequencing
60a bit. This is a famous quote I also used for the encoding tests, and it goes
61like this.
62
63> How wonderful that we have met with a paradox. Now we have some hope of
64> making progress.
65> ― Niels Bohr
66
67```shell
68>SEQ1
69GACAGCTTGTGTACAAGTGTGCTTGCTCGCGAGCGGGTACGCGCGTGGGCTAACAAGTGA
70GCCAGCAGGTGAACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGCTGGCGGGTGA
71ACAAGTGTGCCGGTGAGCCAACAAGCAGACAAGTAAGCAGGTACGCAGGCGAGCTTGTCA
72ACTCACAAGATCGCTTGTGTACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGTAT
73GCTTGCTGGCGGACAAGCCAGCTTGTAAGCGGACAAGCTTGCGCACAAGCTGGCAGGCCT
74GCCGGCTCGCGTACAAATTCACAAGTAAGTACGCTTGCGTGTACGCGGGTATGTATACTC
75AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC
76AACC
77```
78
79This is what we gonna work with to get things rolling forward, when creating
80parser and waveform generator.
81
82## Parsing DNA data
83
84This step is rather simple one. All we need to do is parse input DNA sequence in
85[FASTA format](https://en.wikipedia.org/wiki/FASTA_format) well known in
86[Bioinformatics](https://en.wikipedia.org/wiki/Bioinformatics) to extract single
87Nucleotides that will be converted into separate tones based on equal-tempered
88scale explained above.
89
90```python
91nucleotide_tone_map = {
92 'A': 440,
93 'C': 523.25,
94 'G': 783.99,
95 'T': 587.33, # converted to D
96}
97
98def split(word):
99 return [char for char in word]
100
101def generate_from_dna_sequence(sequence):
102 for nucleotide in split(sequence):
103 print(nucleotide, nucleotide_tone_map[nucleotide])
104```
105
106## Generating sine wave
107
108Because we are essentially creating a long stream of notes we will be appending
109sine notes to a global array we will later use for creating a WAV file out of
110it.
111
112```python
113import math
114
115def append_sinewave(freq=440.0, duration_milliseconds=500, volume=1.0):
116 global audio
117
118 num_samples = duration_milliseconds * (sample_rate / 1000.0)
119
120 for x in range(int(num_samples)):
121 audio.append(volume * math.sin(2 * math.pi * freq * (x / sample_rate)))
122
123 return
124```
125
126The sine wave generated here is the standard beep. If you want something more
127aggressive, you could try a square or saw tooth waveform.
128
129## Generating a WAV file from accumulated sine waves
130
131
132```python
133import wave
134import struct
135
136def save_wav(file_name):
137 wav_file = wave.open(file_name, 'w')
138 nchannels = 1
139 sampwidth = 2
140
141 nframes = len(audio)
142 comptype = 'NONE'
143 compname = 'not compressed'
144 wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
145
146 for sample in audio:
147 wav_file.writeframes(struct.pack('h', int(sample * 32767.0)))
148
149 wav_file.close()
150```
151
15244100 is the industry standard sample rate - CD quality. If you need to save on
153file size, you can adjust it downwards. The standard for low quality is, 8000 or
1548kHz.
155
156WAV files here are using short, 16 bit, signed integers for the sample size.
157So, we multiply the floating-point data we have by 32767, the maximum value for
158a short integer.
159
160> It is theoretically possible to use the floating point -1.0 to 1.0 data
161> directly in a WAV file, but not obvious how to do that using the wave module
162> in Python.
163
164## Generating Spectograms
165
166I have tried two methods of doing this and both were just fine. I however opted
167out to use the [SoX - Sound eXchange, the Swiss Army knife of audio
168manipulation](https://linux.die.net/man/1/sox) one because it didn't require
169anything else.
170
171```shell
172sox output.wav -n spectrogram -o spectrogram.png
173```
174
175An example spectrogram of Ludwig van Beethoven Symphony No. 6 First movement.
176
177<audio controls>
178 <source src="/assets/posts/dna-synthesized/symphony-no6-1st-movement.mp3" type="audio/mpeg">
179</audio>
180
181![Ludwig van Beethoven Symphony No. 6 First movement](/assets/posts/dna-synthesized/symphony-no6-1st-movement.png){:loading="lazy"}
182
183The other option could also be in combination with
184[gnuplot](http://www.gnuplot.info/). This would require an intermediary step,
185however.
186
187```shell
188sox output.wav audio.dat
189tail -n+3 audio.dat > audio_only.dat
190gnuplot audio.gpi
191```
192
193And input file `audio.gpi` that would be passed to gnuplot looks something like
194this.
195
196```txt
197# set output format and size
198set term png size 1000,280
199
200# set output file
201set output "audio.png"
202
203# set y range
204set yr [-1:1]
205
206# we want just the data
207unset key
208unset tics
209unset border
210set lmargin 0
211set rmargin 0
212set tmargin 0
213set bmargin 0
214
215# draw rectangle to change background color
216set obj 1 rectangle behind from screen 0,0 to screen 1,1
217set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "#ffffff"
218
219# draw data with foreground color
220plot "audio_only.dat" with lines lt rgb 'red'
221```
222
223## Pre-generated sequences
224
225What I did was take interesting parts from an animal's genome and feed it to a
226tone generator script. This then generated a WAV file and I converted those to
227MP3, so they can be played in a browser. The last step was creating a
228spectrogram based on a WAV file.
229
230### Niels Bohr quote
231
232<audio controls>
233 <source src="/assets/posts/dna-synthesized/quote/out.mp3" type="audio/mpeg">
234</audio>
235
236![Spectogram](/assets/posts/dna-synthesized/quote/spectogram.png){:loading="lazy"}
237
238### Mouse
239
240This is part of a mouse genome `Mus_musculus.GRCm39.dna.nonchromosomal`. You
241can get [genom data
242here](http://ftp.ensembl.org/pub/release-106/fasta/mus_musculus/dna/).
243
244<audio controls>
245 <source src="/assets/posts/dna-synthesized/mouse/out.mp3" type="audio/mpeg">
246</audio>
247
248![Spectogram](/assets/posts/dna-synthesized/mouse/spectogram.png){:loading="lazy"}
249
250### Bison
251
252This is part of a bison genome `Bison_bison_bison.Bison_UMD1.0.cdna`. You can
253get [genom data
254here](http://ftp.ensembl.org/pub/release-106/fasta/bison_bison_bison/cdna/).
255
256<audio controls>
257 <source src="/assets/posts/dna-synthesized/bison/out.mp3" type="audio/mpeg">
258</audio>
259
260![Spectogram](/assets/posts/dna-synthesized/bison/spectogram.png){:loading="lazy"}
261
262### Taurus
263
264This is part of a taurus genome `Bos_taurus.ARS-UCD1.2.cdna`. You can get
265[genom data
266here](http://ftp.ensembl.org/pub/release-106/fasta/bos_taurus/cdna/).
267
268<audio controls>
269 <source src="/assets/posts/dna-synthesized/taurus/out.mp3" type="audio/mpeg">
270</audio>
271
272![Spectogram](/assets/posts/dna-synthesized/taurus/spectogram.png){:loading="lazy"}
273
274## Making a drummer out of a DNA sequence
275
276To make things even more interesting, I decided to send this data via MIDI to my
277[Elektron Model:Samples](https://www.elektron.se/en/model-samples). This is a
278really cool piece of equipment that supports MIDI in via USB and 3.5 mm audio
279jack.
280
281Elektron is connected to my MacBook via USB cable and audio out is patched to a
282Sony Bluetooth speaker I have that supports 3.5 mm audio in. Elektron doesn't
283have internal speakers.
284
285![](/assets/posts/dna-synthesized/elektron/IMG_0619.jpg){:loading="lazy"}
286
287![](/assets/posts/dna-synthesized/elektron/IMG_0620.jpg){:loading="lazy"}
288
289![](/assets/posts/dna-synthesized/elektron/IMG_0622.jpg){:loading="lazy"}
290
291For communicating with Elektron, I choose `pygame` Python module that has MIDI
292built in. With this, it was rather simple to send notes to the device. All I did
293was map MIDI notes to the actual Nucleotides.
294
295Before all of this I also checked Audio MIDI Setup app under MacOS and checked
296MIDI Studio by pressing ⌘-2.
297
298![](/assets/posts/dna-synthesized/elektron/midi-studio.jpg){:loading="lazy"}
299
300The whole script that parses and send notes to the Elektron looks like this.
301
302```python
303import pygame.midi
304import time
305
306pygame.midi.init()
307
308print(pygame.midi.get_default_output_id())
309print(pygame.midi.get_device_info(0))
310
311player = pygame.midi.Output(1)
312player.set_instrument(2)
313
314def send_note(note, velocity):
315 global player
316 player.note_on(note, velocity)
317 time.sleep(0.3)
318 player.note_off(note, velocity)
319
320
321nucleotide_midi_map = {
322 'A': 60,
323 'C': 90,
324 'G': 160,
325 'T': 180, # is D
326}
327
328with open("quote.fa") as f:
329 sequence = f.read().replace('\n', '')
330
331for nucleotide in [char for char in sequence]:
332 print("Playing nucleotide {} with MIDI note {}".format(
333 nucleotide, nucleotide_midi_map[nucleotide]))
334 send_note(nucleotide_midi_map[nucleotide], 127)
335
336del player
337pygame.midi.quit()
338```
339
340<video src="/assets/posts/dna-synthesized/elektron/elektron.mp4" controls></video>
341
342All of this could be made much more interesting if I choose different
343instruments for different Nucleotides, or doing more funky stuff with Elektron.
344But for now, this should be enough. It is just a proof of concept. Something to
345play around with.
346
347## Going even further
348
349As you probably notice, the end results are quite similar to each other. This is
350to be expected because we are operating only with 4 notes essentially. What
351could make this more interesting is using something like
352[Supercollider](https://supercollider.github.io/) to create more interesting
353sounds. By transposing notes or using effects based on repeated data in a
354sequence. Possibilities are endless.
355
356It is really astonishing what can be achieved with a little bit of code and an
357idea. I could see this becoming an interesting background soundscape instrument
358if done properly. It could replace random note generator with something more
359intriguing, biological, natural.
360
361I actually find the results fascinating. I took some time and listened to this
362music of nature. Even though it's quite the same, it's also quite different.
363The subtle differences on repeat kind of creates music on its own. Makes you
364wonder. It kind of puts Occam’s Razor in its place. Nature for sure loves to
365make things as energy efficient as possible.
diff --git a/_posts/posts/2022-10-06-state-of-web-technologies-in-year-2022.md b/_posts/posts/2022-10-06-state-of-web-technologies-in-year-2022.md
new file mode 100644
index 0000000..e7c8d62
--- /dev/null
+++ b/_posts/posts/2022-10-06-state-of-web-technologies-in-year-2022.md
@@ -0,0 +1,297 @@
1---
2title: State of Web Technologies and Web development in year 2022
3permalink: /state-of-web-technologies-and-web-development-in-year-2022.html
4date: 2022-10-06T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10## Initial thoughts
11
12*This post is a critique on the current state of web development. It is an
13opinionated post! I will learn more about this in the future, and probably
14slightly change my mind about some of the things I criticize.*
15
16I have started working on a hobby project about two weeks ago, and I wanted to
17use that situation as a learning one. Trying new things, new technologies, new
18tools. I always considered myself to be an adventurous person when it comes to
19technology. I never shy away from trying new languages, new operating systems
20etc. Likewise, I find the whole experience satisfying, and it tickles that part
21of my brain that finds discovery the highest of the mountains to climb.
22
23What I always wanted to make was a coding game, that you would play in a browser
24(just to eliminate building binaries for each operating system) where you would
25level up your character and go into these scriptable battles. You know, RPG
26elements.
27
28So, the natural way to go would be some sort of SPA (single page application)
29with basic routing and some state management. Nothing crazy.
30
31> **Before we move on**, I have to be transparent. Take my views on this with
32> a grain of salt. I have only scratched the surface with these technologies,
33> and my knowledge is full of gaps. This is my experience using some of these
34> products for the first time or in a limited capacity.
35
36Having this out of the way, I got myself a fresh pot of coffee and down the
37rabbit hole I went.
38
39## Giving React JS a spin
40
41I first tried [React JS](https://reactjs.org/). I kind of like it. Furthermore,
42I have worked with libraries like this in the past and also wrote a couple of
43them (nothing compared to that level), but I had the basic understanding of what
44was going on. I rolled up a project quickly and had basic things done in a
45matter of two hours, which was impressive.
46
47I prefer using [Tailwind CSS](https://tailwindcss.com/) for my styling
48pleasures, and integrating that was also a painless experience. It was actually
49nice to see that some things got better with time. In about 2 minutes I got
50Tailwind working, and I was able to use classes at my disposal. All that
51`postcss` stuff was taken care of by adding a couple of things in config files
52(all described really well in their documentation).
53
54It is not that different from Vue which I have had more encounters with in the
55past People will probably call me a lunatic for saying this. But you know, it is
56the truth. Same same, but different. I still believe that using libraries like
57this is beneficial. I am not a JavaScript purist. They all have their quirks,
58but at the end of the day, I truly believe it’s worth it.
59
60## Bundlers and Transpilers
61
62I still reject calling [Typescript](https://www.typescriptlang.org/) to
63[JavaScript](https://www.javascript.com/) conversion a "compilation process". I
64call them [transpilers](https://devopedia.org/transpiler), and I don’t care! 😈
65
66The first one that I ever used was [webpack](https://webpack.js.org/), and it
67was an absolute horrific experience. Saying this, it is an absolutely fantastic
68tool. I felt more like a config editor than actually a programmer. To be fair,
69I am a huge fan of [make](https://www.gnu.org/software/make/), and you can do as
70you wish with this information. I like my build systems simple.
71
72Also, isn’t it interesting that we need something like
73[Babel](https://babeljs.io/) to make JavaScript code work in a browser that has
74only one client side scripting available, which is by no accident also
75JavaScript. Why? I know why it’s needed, but seriously, why.
76
77I haven’t used Babel for years now. Or if I did, it was packaged together by
78some other bundler thingy. Which does not make things better, but at least I
79didn’t need to worry about it.
80
81I really don’t like complicated build systems. I really don’t like abstracting
82code and making things appear magical. The older I get, the more I appreciate
83clear and clean, expressive code. No one-liners, if possible.
84
85But I have to give props to [Vite](https://vitejs.dev/)! This was one of the
86best developer experiences I have ever had. Granted, it still has magical
87properties. And yes, it still is a bundler and abstracts things to the nth
88degree. But at least it didn’t force me to configure 700 lines of JSON. And I
89know that this makes me a hypocrite. You can’t have it all. Nonetheless, my
90reasoning here is, if using bundlers is inevitable, then at least they should
91provide an excellent developer experience.
92
93I also noticed that now the catch-all phrase is “blazingly fast” and “lightning
94fast” and “next generation” and stuff like that. I mean, yeah, tools should get
95faster with time. But saying that starting a project now takes 2 seconds instead
96of 20 seconds is something that is a break it or make it kind of a deal is
97ridiculous. I don’t mind waiting a couple of seconds every couple of days. I
98also don’t create 700 projects every day, and also who does? This argument has
99no bite. All I want is a decent reload time (~100ms is more than good enough for
100me) and that is it.
101
102You don’t need to sell me benefits if I only get them when I start a fresh
103project, and then try to convince me that this is somehow changing the fate of
104the universe. First of all, it is not. And second, if this is your only argument
105for your tool, I would advise you to maybe re-focus your efforts to something
106else. Vite says that startup times are really fast. And if that would be the
107only thing differentiating it from other tools, I would ignore it. But it has
108some really compelling features like [Hot Module
109Replacement](https://www.geeksforgeeks.org/reactjs-hot-module-replacement/) that
110really works well. It was a joy to use.
111
112So, I will be definitely using Vite in the future.
113
114## Jam Stack, Mach Stack no snack
115
116Let's get a couple of the acronyms out of the way, so we all know what we are
117talking about:
118
119- Jam Stack - JavaScript, API and Markup
120- Mach Stack - Microservices, API-first, Cloud-Native SaaS, Headless
121
122It is so hard to follow all these new trendy things happening around you, that
123it makes you have a massive **FOMO** all the time. But on the other hand, you
124also don’t want to be that old fart that doesn’t move with the times and still
125writes his trusty jQuery code while listening to Blink 182 All the small things
126on full blast. It’s a good song, don’t get me wrong, but there are other songs
127out there.
128
129I have to admit. [Vercel](https://vercel.com/) is really cool! Love the
130simplicity of the service. You could compare it to
131[Netlify](https://www.netlify.com/). I haven’t tried Netlify extensively, but
132from a couple of experimental deployments I still prefer Vercel. It is much more
133streamlined, but maybe this is bias in me. I really like Vercel’s Analytics,
134which give you a [Core Web Vitals report](https://web.dev/vitals/) in their
135admin console. Kind of cool, I’m not going to lie.
136
137This whole idea about frontend and backend merging into [SSR (server-side
138rendering)](https://www.debugbear.com/blog/server-side-rendering) looks so good
139on paper. It almost doesn’t come with any major flaws.
140
141But when it comes to the actual implementation, there is much to be desired.
142I’m going to lump [Next.js](https://nextjs.org/) and
143[Nuxt.js](https://nuxtjs.org/) together because they are essentially the same
144thing, just a different library.
145
146Now comes the reality. Mixing backend and frontend in this manner creates this
147weird mental model where you kind of rely on magical properties of these
148libraries. You relinquish control over to them for better developer experience.
149But is that really true? Initially, I was so stoked about it. However, the more
150I used them, the more I felt uncomfortable. I felt dirty, actually. Maybe this
151is because I come from old ways of doing things where you control every step of
152request, and allowing something to hijack it feels like blasphemy.
153
154More than that, some pretty significant technical issues arose from this. How do
155you do JWT token authentication? You put it in `api` folder and then do some
156fetching and storing into local state management. But doing this also requires
157some tinkering with await/async stuff on the React/Vue side of things. And then
158you need to write middleware for it. And the more I look at it, the more I see
159that this whole thing was not meant to be used like this, and it all feels and
160looks like a huge hack.
161
162The issue I have with this is that they over-promise and under-deliver. They
163want to be an all-in-one replacement for everything, and they don’t deliver on
164this promise. And how could they?! We have to be fair. It is an impossible task.
165
166They sell you [NoOps](https://www.geeksforgeeks.org/overview-of-noops/), but
167when you need to accomplish something a little bit more out of the scope of
168Hello World, you have to make hacky decisions to make it work. And having a
169deployment strategy that relies on many moving parts is never a good idea.
170Abstracting too much is usually a sign of bad architecture.
171
172Lately, this has become a huge trend that will for sure bite us in the future.
173And let’s not get it twisted. By doing this, PaaS providers like
174[AWS](https://aws.amazon.com/), [GCS](https://cloud.google.com/), etc. obscure
175their billing, and you end up paying more than you really should. And even if
176that is not an issue, it comes down to the principle of things. AWS is known for
177having multiple “currencies“ inside their projects like write operations, read
178operations, etc. which add up, and it creates this impossible to track billing
179scheme. It all behaves suspiciously like a pay-to-win game you could find on
180mobile phones that scams you out of your money.
181
182And as far as I am concerned, the most important thing was me not coding the
183functionalities for the game I want to make. I was battling libraries and cloud
184providers. How to deploy, what settings are relevant. Bad documentation or
185multiple versions of achieving the same thing. You are getting bombarded by all
186this information, and you don’t really have any control over it.
187Production-ready code becomes a joke, essentially. Especially if you tend to
188work on that project for a prolonged period of time.
189
190All of these options end up creating a fatigue. What to choose, what not to
191choose. Unnecessary worrying about if the stack will still be deemed worthy in
192six months. There is elegance in simplicity.
193
194> JavaScript UI frameworks and libraries work in cycles. Every six months or
195> so, a new one pops up, claiming that it has revolutionized UI development.
196> Thousands of developers adopt it into their new projects, blog posts are
197> written, Stack Overflow questions are asked and answered, and then a newer
198> (and even more revolutionary) framework pops up to usurp the throne.
199> — Ian Allen
200
201And this jab at these libraries and cloud providers is not done out of malice.
202It is a real concern that I have about them. In my life, I have seen
203technologies come and go, but the basics always stick around. So surrendering
204all the power you have to a library or a cloud provider is in my opinion a
205stupid move.
206
207## Tailwind CSS still rocks!
208
209You know, many people say negative things about Tailwind. And after a lot of
210deliberation, I came to the conclusion that Tailwind is good for two types of
211developers. Tailwind is good for a complete noob or a senior developer. A
212complete noob doesn’t really care about inner workings of CSS, and a senior
213developer also doesn’t care about CSS. Well, at least, not anymore. And
214developers in between usually have the biggest issues with it. Not always of
215course, but in a lot of cases.
216
217I like the creature comforts of Tailwind. Being utility first would make me
218argue that it is actually more similar to [Sass](https://sass-lang.com/) or
219[Less](https://lesscss.org/) than something like Bootstrap. Not technically, but
220ideologically. After I started using it, I never looked back. I use it every
221time I need to do something web related.
222
223Writing CSS for general things feels like going several steps back. Instead of
224focusing on what you are actually trying to achieve, you focus on notations like
225[BEM](https://en.bem.info/methodology/css/), code structuring, optimizing HTML
226size. Just doing things that make 0.1% difference. You know that saying: Early
227optimization is the root of all evil. Exactly that.
228
229I am also not saying that Tailwind is the cure for everything. Sometimes custom
230CSS is necessary. But from what I found out in using it for almost two years in
231a production environment (on a site getting quite a lot of traffic and
232constantly being changed), I can say without any reservations that Tailwind
233saved our asses countless times. We would be rewriting CSS all the time without
234it. And I don’t really think writing CSS is the best way to spend my time.
235
236I have also noticed that people who criticize Tailwind the most never actually
237used it in a real project that has a long lifetime with plenty of changes that
238will happen in the future.
239
240But you know, whatever floats your boat!
241
242## Code maintainability
243
244Somehow, people also stopped talking about maintenance. If you constantly try to
245catch the latest and greatest train, you are by that logic always trying new
246things. Which is a good thing if you want to learn about technologies and try
247them. But for the production environment, you have to have a stable stack that
248doesn’t change every 6 months.
249
250You can lock dependencies for sure. Nevertheless, the hype train moves along
251anyway. And the mindset this breeds goes against locking the code. This
252bleeding-edge rolling release cycle is not helping. That is why enterprise
253solutions usually look down on these popular stacks and only do bare minimum to
254appear hip and cool.
255
256With that said, I still think that progress is good, but should be taken with a
257grain of salt. If your project is something that should be built once and then
258rarely updated, going with the latest stack is a possible way to go. But, if you
259are working on a project that lasts for years, you should probably approach it
260with some level of caution. Web development is often times too volatile.
261
262## Web development has a marketing issue
263
264I noticed that almost every project now has this marketing spin put on it.
265Everything is blazingly fast now. I get it, they are competing for your
266attention, but what happened to just being truthful and not inflating reality.
267
268And in order to appeal to mass market, they leave things out of their marketing
269materials. These open-source projects are now behaving more and more like
270companies do. Which is a scary thought on its self.
271
272And we are also seeing a rise in a concept of building a company in the open,
273which is a good thing, don't get me wrong. But when it is using open-source to
274lure people and then lock them in their ecosystem, there is where I have issues
275with it.
276
277This might be because I have been using GNU/Linux for 20 years now and have been
278so beholden for my success to open-source that I see issues when open-source is
279being used to trick people into a false sense of security that these projects
280are built in the spirit of open-source. Because there is a difference. They are
281NOT! They have a really specific goal in mind. And the open-source is being used
282as a delivery system. Which is in my opinion disgusting!
283
284## Conclusion
285
286I will end my post with this. Web development is running now in circles. People
287are discovering [RPC](https://www.tutorialspoint.com/remote-procedure-call-rpc)
288now and this is the now the next big thing. [GraphQL](https://graphql.org/) is
289so passé. And I am so tired of it all. Of blazingly fast libraries, of all these
290new technologies that are actually just a remake of old ones. Of just the
291general spirit of the web. I will just use what I already know. Which worked 10
292years ago and will work 10 years after this. I will adopt a couple of little
293tools like Vite. But I will not waste my time on this anymore.
294
295It was a good exercise to get in touch with what’s new now. Nothing really
296changed that much. FOMO is now cured! Now I have to get my ass back to actually
297code and make the project that I wanted to make in the first place.
diff --git a/_posts/posts/2022-10-16-that-sound-that-machine-makes-when-struggling.md b/_posts/posts/2022-10-16-that-sound-that-machine-makes-when-struggling.md
new file mode 100644
index 0000000..7b019e9
--- /dev/null
+++ b/_posts/posts/2022-10-16-that-sound-that-machine-makes-when-struggling.md
@@ -0,0 +1,67 @@
1---
2title: Microsoundtrack — That sound that machine makes when struggling
3permalink: /that-sound-that-machine-makes-when-struggling.html
4date: 2022-10-16T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10A couple of months ago, I got an idea about micro soundtracks. In this concept,
11you are the observer, director, and audience in this tiny movies.
12
13What you do is to attempt to imagine what would be happening around you based on
14a title of the song and let the song help you fill the void in your story.
15
16I made these songs is Logic Pro X. Every year or so I do this kind of thing and
17make a couple of songs similar to this. But this is the first time I am posting
18about it.
19
20You can listen to the whole set on
21[Youtube](https://www.youtube.com/watch?v=_5oXBhSmF3c) or scroll down the page
22and there are embedded players for each song.
23
24## A bunch of inter-dimensional people with loud clocks
25
26A group of inter-dimensional people are going up and down the elevator with you
27while having loud clocks around their necks. Each clock ticks on a different
28frequency. A lot of other sounds are getting drawn into your dimension,
29resulting in a strange merging of dimensions.
30
31<iframe style="border: 0; width: 100%; height: 42px;" src="https://bandcamp.com/EmbeddedPlayer/album=3913808801/size=small/bgcol=ffffff/linkcol=0687f5/track=1349272965/transparent=true/" seamless title="Bandcamp"><a href="https://mitjafelicijan.bandcamp.com/album/that-sound-that-machine-makes-when-struggling">That sound that machine makes when struggling by Mitja Felicijan</a></iframe>
32
33## Two black holes conversing about the weather
34
35You are a traveler in a spaceship flying very close to two colliding black holes
36having a discussion about the weather while tearing each other apart. During all
37this your ship is getting pulled into the event horizon of both black holes,
38putting a lot of strain on your spaceship.
39
40<iframe style="border: 0; width: 100%; height: 42px;" src="https://bandcamp.com/EmbeddedPlayer/album=3913808801/size=small/bgcol=ffffff/linkcol=0687f5/track=1756714200/transparent=true/" seamless title="Bandcamp"><a href="https://mitjafelicijan.bandcamp.com/album/that-sound-that-machine-makes-when-struggling">That sound that machine makes when struggling by Mitja Felicijan</a></iframe>
41
42## A planet where every organism is a plant
43
44You land on a planet where every living organism is a plant and among those
45plants some of them are highly intelligent, and you were asked to make first
46contact with the native species. Your visit takes place in a giant cave where
47you are meeting these plants, and they are talking to you.
48
49<iframe style="border: 0; width: 100%; height: 42px;" src="https://bandcamp.com/EmbeddedPlayer/album=3913808801/size=small/bgcol=ffffff/linkcol=0687f5/track=3710973979/transparent=true/" seamless title="Bandcamp"><a href="https://mitjafelicijan.bandcamp.com/album/that-sound-that-machine-makes-when-struggling">That sound that machine makes when struggling by Mitja Felicijan</a></iframe>
50
51## Bio implants having a fit and reprogramming your brain
52
53In a distant future where everybody has bio implants, you have just received
54your first one, which happens to be a brain implant. Something goes wrong, and
55your implant is starting to misbehave, and you are experiencing brain
56malfunctions. You are on the streets at night a couple of hours after your
57procedure. You can feel your sanity breaking down.
58
59<iframe style="border: 0; width: 100%; height: 42px;" src="https://bandcamp.com/EmbeddedPlayer/album=3913808801/size=small/bgcol=ffffff/linkcol=0687f5/track=1157430581/transparent=true/" seamless title="Bandcamp"><a href="https://mitjafelicijan.bandcamp.com/album/that-sound-that-machine-makes-when-struggling">That sound that machine makes when struggling by Mitja Felicijan</a></iframe>
60
61## Cow animation
62
63I also made this little cow animation. Go into full screen to see the effects in
64more details.
65
66<video src="/assets/posts/microsoundtrack/cow.m4v" controls loop></video>
67
diff --git a/_posts/posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md b/_posts/posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md
new file mode 100644
index 0000000..ced58bb
--- /dev/null
+++ b/_posts/posts/2023-01-26-trying-to-build-a-new-kind-of-terminal-emulator.md
@@ -0,0 +1,254 @@
1---
2title: Trying to build a New kind of terminal emulator for the modern age
3permalink: /trying-to-build-a-new-kind-of-terminal-emulator.html
4date: 2023-01-26T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Over the past few weeks, I have been really thinking about terminal emulators,
11how we interact with computers, the separation of text-based programs and GUI
12ones. To be perfectly honest, I got pissed off one evening when I was cleaning
13up files on my computer. Normally, I go into console and do `ncdu` and check
14where the junk is. Then I start deleting stuff. Without any discrimination,
15usually. But when it comes to screenshots, I have learned that it's good to keep
16them somewhere near if I need to refer to something that I was doing. I am an
17avid screenshot taker. So at that point I checked Pictures folder and also did a
18basic search `find . -type f -name "*.jpg"` for all the JPEG files in my home
19directory and immediately got pissed off. Why can’t I see thumbnails in my
20terminal? I know why, but why in the year of 2022 this is still a problem. I am
21used to traversing my disk via terminal. I am faster, and I am more comfortable
22this way. But when it comes to visualization, I then need to revert to GUI
23applications and again find the same file to see it. I know that programs like
24`feh` and `sxiv` are available, but I would just like to see the preview. Like
25[Jupyter notebook](https://jupyter.org/) or something similar. Just having it
26inline. Part of a result.
27
28It also didn’t help that I was spending some time with the [Plan
299](https://plan9.io/plan9/) Operating system. More specifically
30[9FRONT](http://9front.org/). The way that [ACME editor](http://acme.cat-v.org/)
31handles text editing is just wonderful. Different and fresh somehow, even though
32it’s super old.
33
34So, I went on a lookout for an interesting way of visualizing results of some
35query. I found these applications to be outstanding examples of how not to be a
36captive of a predetermined way of doing things.
37
38- [Wolfram Mathematica](https://www.wolfram.com/mathematica/)
39- [Jupyter notebooks](https://jupyter.org/)
40- [Plan 9 / 9FRONT](http://www.9front.org)
41- [Temple OS](https://templeos.org/)
42- [Emacs](https://www.gnu.org/software/emacs/)
43
44My idea is not as out there as ACME is, but it is a spin on the terminal
45emulators. I like the modes that Vi/Vim provides you with. I like the way the
46Emacs does its own `M-x` `M-c`. Furthermore, I really like how Mathematica and
47Jupyter present the data in a free flowing form. And I love how Temple OS is
48basically a C interpreter on some level.
49
50> **Note:** This is part 1 of the journey. Nowhere finished yet. I am just
51> tinkering with this at the moment. This whole thing can easily spectacularly
52> fail.
53
54So I started. I knew that I wanted to have the couple of modes, but I didn’t
55like the repetition of keystrokes, so the only option was to have some sort of
56toggle and indicate to the user that they are in a special mode. Like Vi does
57for Normal and Visual mode.
58
59These modes would for the first version be:
60
61- *Preview mode* (toggle with Ctrl + P)
62 - When this mode would be enabled, the `ls` command would try to find images
63 from the results and display thumbnails from them in the terminal itself.
64 No ASCII art. Proper images. In a grid!
65- *Detach mode* (toggle with Ctrl + D)
66 - When this mode would be enabled, every command would open a new window
67 and execute that command in it. This would be useful for starting `htop`
68 in a separate window.
69
70The reason for having these modes togglable is to not ask for previews every
71time. You enable a mode and until you disable it, it behaves that way. Purely
72out of ergonomic reasons.
73
74I would like to treat every terminal I open as a session mentally. When I start
75using the terminal, I start digging deeper into the issue I am trying to
76resolve. And while I am doing this, I would like to open detached windows
77etc. A lot of these things can be done easily with something like
78[i3](https://i3wm.org/), but also that pull you out of the context of what you
79were doing. I would like to orchestrate everything from one single point.
80
81In planning for this project, I knew that I would need to use a language like C
82and a library such as [SDL2](https://www.libsdl.org/) in order to achieve the
83desired results. I had considered other options, but ultimately determined that
84[SDL2](https://www.libsdl.org/) was the best fit based on its capabilities and
85reputation in the programming community.
86
87At first, I thought the idea of a hardware accelerated terminal was a bit of a
88joke. It seemed like such a niche and unnecessary feature, especially given the
89fact that terminal emulators have been around for decades and have always relied
90on software rendering. But to be fair, [Alacritty](https://alacritty.org/) is
91doing the same thing. Well, they are doing a remarkable job at it.
92
93So, I embarked on a journey. Everything has to start somewhere. For me, it
94started with creating a window! It has to start somewhere. 🙂
95
96```c
97// Oh, Hi Mark!
98// Create the window, obviously.
99SDL_Window *window = SDL_CreateWindow(
100 WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
101 WINDOW_WIDTH, WINDOW_HEIGHT,
102 SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
103```
104
105I continued like this to get some text displayed on the screen.
106
107I noted that
108[`TTF_RenderText_Solid`](https://wiki.libsdl.org/SDL_ttf/TTF_RenderText_Solid)
109rendered text really poorly. There were no antialiasing at all. In my wisdom, I
110never checked the documentation. Well, that was a fail. To uneducated like me:
111`TTF_RenderText_Solid` renders Latin1 text at fast quality to a new 8-bit
112surface. So, that's why the texts looked like shit. No wonder.
113
114Remarks on `TTF_RenderText_Solid`: This function will allocate a new 8-bit,
115palettized surface. The surface's 0 pixel will be the colorkey, giving a
116transparent background. The 1 pixel will be set to the text color.
117
118After I replaced it with
119[`TTF_RenderText_LCD`](https://wiki.libsdl.org/SDL_ttf/TTF_RenderText_LCD) which
120renders Latin1 text at LCD subpixel quality to a new ARGB surface, the text
121started looking good. Really make sure you read the documentation. It’s actually
122good. As a side note, you can find all the documentation regarding [SDL2 on
123their Wiki](https://wiki.libsdl.org/).
124
125After that was done, I started working on displaying other things like `Preview`
126and `Detach` modes. This wasn’t really that hard. In SDL2 you can check all the
127available events with `while (SDL_PollEvent(&event) > 0)` and have a bunch of
128switch statements to determine which key is currently being pressed. More about
129keys, [SDLKey](https://documentation.help/SDL/sdlkey.html) and mroe about
130pooling the events on
131[SDL_PollEvent](https://documentation.help/SDL/sdlpollevent.html).
132
133```c
134while (SDL_PollEvent(&event) > 0)
135{
136 switch (event.type)
137 {
138 case SDL_QUIT:
139 running = false;
140 break;
141
142 case SDL_TEXTINPUT:
143 if (!meta_key_pressed)
144 {
145 strncat(input_prompt_text, event.text.text, 1);
146 update_input_prompt = true;
147 }
148 break;
149 }
150}
151```
152
153After that was somewhat working correctly, I started creating a struct that
154would hold all the commands and results and I call them Cells. Yes, I stole that
155naming idea from Jupyter.
156
157```c
158typedef struct
159{
160 char *command;
161 char *result;
162 SDL_Surface *surface;
163 SDL_Texture *texture;
164 SDL_Rect rect;
165} Cell;
166```
167
168I am at a place now where I am starting to implement scrolling. This will for
169sure be fun to code. Memory management in C is super easy. 😂
170
171I have also added a simple [INI file like
172configuration](https://en.wikipedia.org/wiki/INI_file) support. It is done in an
173[STB style of
174header](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) and maps
175to specific options supported by the terminal. It is not universal, and the code
176below demonstrates how I will use it in the future.
177
178```c
179#ifndef CONFIG_H
180#define CONFIG_H
181
182/*
183# This is a comment
184
185# This is the first configuration option
186dettach=value11111
187
188# This is the second configuration option
189preview=value22222
190
191# This is the third configuration option
192debug=value33333
193*/
194
195// Define a struct to hold the configuration options
196typedef struct
197{
198 char dettach[256];
199 char preview[256];
200 char debug[256];
201} Config;
202
203// Read the configuration file and return the options as a struct
204extern Config read_config_file(const char *filename)
205{
206 // Create a struct to hold the configuration options
207 Config config = {0};
208
209 // Open the configuration file
210 FILE *file = fopen(filename, "r");
211
212 // Read each line from the file
213 char line[256];
214 while (fgets(line, sizeof(line), file))
215 {
216 // Check if this line is a comment or empty
217 if (line[0] == '#' || line[0] == '\n')
218 continue;
219
220 // Parse the line to get the option and value
221 char option[128], value[128];
222 if (sscanf(line, "%[^=]=%s", option, value) != 2)
223 continue;
224
225 // Set the value of the appropriate option in the config struct
226 if (strcmp(option, "dettach") == 0)
227 {
228 strncpy(config.option1, value, sizeof(config.option1));
229 }
230 else if (strcmp(option, "preview") == 0)
231 {
232 strncpy(config.option2, value, sizeof(config.option2));
233 }
234 else if (strcmp(option, "debug") == 0)
235 {
236 strncpy(config.option3, value, sizeof(config.option3));
237 }
238 }
239
240 // Close the configuration file
241 fclose(file);
242
243 // Return the configuration options
244 return config;
245}
246
247#endif
248```
249
250This is as far as I managed to get for now. I have a daily job and this
251prohibits me to work on these things full time. But I should probably get back
252and finish this. At least have a simple version working out, so I can start
253testing it on my machines. Fingers crossed. 🕵️‍♂️
254
diff --git a/_posts/posts/2023-05-16-rekindling-my-love-for-programming.md b/_posts/posts/2023-05-16-rekindling-my-love-for-programming.md
new file mode 100644
index 0000000..dc5344f
--- /dev/null
+++ b/_posts/posts/2023-05-16-rekindling-my-love-for-programming.md
@@ -0,0 +1,75 @@
1---
2title: Rekindling my love for programming and enjoying the act of creating
3permalink: /rekindling-my-love-for-programming.html
4date: 2023-05-16T12:00:00+02:00
5layout: post
6type: post
7draft: false
8---
9
10Programming can be a challenging and rewarding experience, but sometimes it's
11easy to feel burnt out or disinterested. I have lost the passion for coding over
12the past couple of months and it looked like I will never enjoy the coding as
13much as I did.
14
15I was feeling burnt out with programming. I thought taking a break from it and
16focusing on other activities that I enjoy might be helpful. This way, I could
17come back to programming with a fresh perspective and renewed energy. I also
18thought about learning a new programming language or technology to keep things
19interesting and challenging.
20
21However, what I didn't realize was that learning a new language or technology
22wasn't going to solve the underlying issue. I needed to take a step back and
23re-evaluate why I had lost my passion for programming in the first place. This
24involved taking a deep look into what I was doing that resulted in this rut.
25
26Sometimes, it's easy to get caught up in the hype of new technologies or
27languages, and we can feel like we're missing out if we're not constantly
28learning and experimenting. However, it's important to remember that the latest
29and greatest isn't always the best fit for our projects or our
30interests. Instead of constantly chasing the next big thing, it can be helpful
31to focus on what truly interests us and what we're passionate about. This can
32help us stay motivated and engaged with our work, rather than feeling like we're
33just going through the motions.
34
35I expressed that I had lost my passion for coding over the past couple of
36months, and I realized that the reason behind it was my tendency to spread
37myself too thin and not focus on completing interesting projects. In order to
38regain my passion for coding, I need to focus on projects that truly interest me
39and give me a sense of purpose and motivation.
40
41Recently, I have been playing World of Warcraft more frequently and have become
42interested in developing addons for the game.
43
44This quickly resulted in me creating three addons that improve the quality of
45life, and I subsequently developed a more useful add-on that encapsulates all
46the others I made.
47
48I found it interesting that this action sparked a new interest in me.
49Additionally, I discovered the Lua language, which reminded me that coding
50should be fun rather than just a struggle with a language. It should be pure,
51unadulterated fun.
52
53I wasn't fighting the syntax, nor was I focused on finding the most optimal
54solution. I simply created things without the pressure of making them the best
55they could possibly be.
56
57This made me realize that I actually adore simple languages that get out of the
58way and let you express what you want to do. It forced me to rethink a lot about
59what I use and what I actually enjoy.
60
61I have decided to stick to the basics. For a scripting language, I will use
62Lua. For networking, I will use Golang. And for any special needs, I will rely
63on C. I do not require Rust, Nim, or Zig. This selection is more than sufficient
64for my needs. I have to stay true to this simplicity. There is something to the
65Occam's Razor.
66
67I've been struggling with a lack of creativity lately, but now I'm experiencing
68a real change. I realized I needed to take a step back and stop actively trying
69to address the issue. I needed to stop worrying and overthinking it. I simply
70needed some time. Looking back, I don't think I've taken any significant time
71off in the last 10 years.
72
73Suddenly, I find myself with the energy and passion to complete multiple small
74projects. It doesn't feel like a chore at all. Who knew I needed WoW to
75kickstart everything. Inspiration really does come from the strangest places.
diff --git a/_posts/posts/2023-05-23-i-was-wrong-about-git-workflows.md b/_posts/posts/2023-05-23-i-was-wrong-about-git-workflows.md
new file mode 100644
index 0000000..57d887c
--- /dev/null
+++ b/_posts/posts/2023-05-23-i-was-wrong-about-git-workflows.md
@@ -0,0 +1,72 @@
1---
2title: I think I was completely wrong about Git workflows
3permalink: /i-was-wrong-about-git-workflows.html
4date: 2023-05-23T12:00:00+02:00
5layout: post
6type: post
7draft: false
8tags: []
9---
10
11I have been using some approximation of [Git
12Flow](https://jeffkreeftmeijer.com/git-flow/) for years now and never really
13questioned it to be honest. When I create a repo I create develop branch and set
14it as default one and then merge to master from there. Seems reasonable enough.
15
16One thing that I have learned is that long living branches are the devil. They
17always end up making a huge mess when they need to be merged eventually into
18master. So by that reason, what is the develop branch if not the longest living
19feature branch. And from my personal experience there was never a situation
20where I wasn’t sweating bullets when I had to merge develop back to master.
21
22This realisation started to give me pause. So why the hell am I doing this, and
23is there a better way. Well the solution was always there. And it comes in a
24form of [git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging).
25
26So what are git tags? Git tags are references to specific points in a Git
27repository's history. They are used to mark important milestones, such as
28releases or significant commits, making it easier to identify and access
29specific versions of a project.
30
31Somehow we have all hijacked the meaning of the master branch that it has to be
32the most releasable version of code. And this is also where the confusing about
33versioning the software kicks in. Because master branch implicitly says that we
34are dealing with the rolling release type of a software. And by having a develop
35branch we are hacking around this confusion. With a separation of develop and
36master we lock functionalities into place and forcing a stable vs development
37version of the software.
38
39But if that is true and the long living branches are the devil then why have
40develop at all. I think that most of this comes to how continuous integration is
41being done. There usually is no granular access to tags and CD software deploys
42what is present on a specific branch, may that be master for production and
43develop for staging. This is a gross simplification and by having this in place
44we have completely removed tagging as a viable option to create a fix point in
45software cycle that says, this is the production ready code.
46
47One cool thing about tags are that you can checkout a specific tag. So they
48behave very similarly as branches in that regard. And you don’t have the
49overhead of having two mainstream branches.
50
51So what is the solution? One approach is to use development workflow, where all
52changes are made on the smaller branches and continuously merged into
53master. Where the software is ready to be pushed to production you tag the
54master branch. This approach eliminates the need for long-lived branches and
55simplifies the development process. It also encourages developers to make small,
56incremental changes that can be tested and deployed quickly. However, this
57approach may not be suitable for all projects or teams that heavily rely on
58automated deployment based on branch names only.
59
60This also requires that developers always keep production in mind. No more
61living on an island of the develop branch. All your actions and code need to be
62ready to meet production standards on a much smaller timescale.
63
64I think that we have complicated the workflow in an honest attempt to make
65things more streamlined but in the process of doing this, we have inadvertently
66made our lives much more complicated.
67
68In conclusion, it's important to re-evaluate our workflows from time to time to
69see if they still make sense and if there are better alternatives available.
70Long-living branches can be problematic, and using tags to mark important
71milestones can simplify the development process.
72
diff --git a/_posts/posts/2023-05-31-re-inventing-task-runner-that-i-actually-used-daily.md b/_posts/posts/2023-05-31-re-inventing-task-runner-that-i-actually-used-daily.md
new file mode 100644
index 0000000..c595905
--- /dev/null
+++ b/_posts/posts/2023-05-31-re-inventing-task-runner-that-i-actually-used-daily.md
@@ -0,0 +1,160 @@
1---
2title: "Re-Inventing Task Runner That I Actually Used Daily"
3permalink: /re-inventing-task-runner-that-i-actually-used-daily.html
4date: 2023-05-31T12:21:10+02:00
5layout: post
6type: post
7draft: false
8---
9
10Couple of months ago I had this brilliant idea of re-inventing the wheel by
11making an alternative for make. And so I went. Boldly into the battle. And to my
12big surprise my attempt resulted in not a completely useless piece of software.
13
14My initial requirements were quite simple but soon grow into something more
15ambitious. And looking back I should have stuck to the simple version. My
16laziness was on my side this time though. Because I haven’t implemented some of
17the features I now realise I really didn’t need them and they would bog the
18whole program and make it be something it was never meant to be.
19
20My basic requirements were following:
21
22- Syntax should be a tiny bit inspired by Rake and Rakefiles.
23- Should borrow the overall feel of a unit test experience.
24- Using something like Python would be a bit of an overkill.
25- The program must be statically compiled, so it can run on same architecture
26 without libc, musl dependencies or things like that.
27- Install ruby for rake is a bit overkill and can not be done with certain
28 really lightweight distributions like Alpine Linux. This tool would be usable
29 on such lightweight systems for remote debugging.
30- I want to use it for more than just compiling things. I want to use it as an
31 entry-point into a project, and I want this to help me indirectly document the
32 project as well.
33- It should be an abstraction over bash shell or the default system shell.
34 - Each task essentially becomes its own shell instance.
35- Must work on Linux and macOS systems.
36- By default, running `erd` list all the available tasks (when I use make, I
37 usually put a disclaimer that you should check Makefile to see all available
38 target).
39- Should support passing arguments when you run it from a shell.
40- Normal variable as the same as environmental variables. There is no
41 distinction. Every variable is also essentially an environment variable and
42 can be used by other programs.
43- State between tasks is not shared, and this makes this “pure” shell instances.
44- Should be single-threaded for the start and later expanded with `@spawn`
45 command.
46- Variables behave like macros and are preprocessed before evaluation.
47- Should support something like `assure` that would check if programs like C
48 compiler or Python (whatever the project requires) are installed on a machine.
49
50Quite a reasonable list of requirements. I do this things already in my
51Makefiles or/and Bash scripts. But I would like to avoid repeating myself every
52time I start working on something new.
53
54So I started with the following syntax.
55
56```ruby
57@env on
58
59# Override the default shell.
60@shell /bin/bash
61
62# Assure that program is installed.
63@assure docker-compose pip python3
64
65# Load local dotenv files (these are then globally available).
66@dotenv .env
67@dotenv .env.sample
68@dotenv some_other_file
69
70# This are local variables but still accessible in tasks.
71@var HI = "hey"
72@var TOKEN = "sometoken"
73@var EMAIL = "m@m.com"
74@var PASSWORD = "pass"
75@var EDITOR = "vim"
76
77@task dev "Test chars .:'}{]!//" does
78 echo "..." $HI
79end
80
81@task clean "Cleans the obj files" does
82 rm .obj
83end
84
85@task greet "Greets the user" does
86 echo "Hi user $TOKEN or $WINDOWID $EMAIL"
87end
88
89@task stack "Starts Docker stack" does
90 docker-compose -f stack.yml up
91end
92
93@task todo "Shows all todos in source files and count them" does
94 grep -ir "TODO|FIXME" . | wc -l
95end
96
97@task test1 "For testing 1" does
98 unknown-command
99 echo "test1"
100 ls -lha
101end
102
103@task test2 "For testing 2" does
104 echo "test1"
105 ls -lha
106 docker-compose -f samples/stack.yml up
107end
108```
109
110One thing that I really like about Errand. Yes, this is what it is called. And
111it is available at https://git.mitjafelicijan.com/errand.git/about/. Moving
112on. One thing that I really like is that a task is a persistent shell. By that I
113mean, that the whole task, even if it contains multiple command in one shell.
114In make each line in a target is that and you need to combine lines or add `\`
115at the end of the line.
116
117```bash
118# How you do this things in make.
119target:
120 source .venv/bin/activate \
121 python script.py
122```
123
124This solves this problem. Consider each task and what is being executed in that
125task a shell that will only close when all the tasks are completed.
126
127By self-documenting I mean that if you are in a directory with `Errandfile` in,
128if you only type `erd` and press enter it should by default display all the
129possible targets. In make i was doing this by having a first target be something
130like `default` that echos the message “Check Makefile for all available target.”
131Because all of the tasks in Errand require a message I use that to display let’s
132call it table of contents.
133
134Because I don’t use any external dependencies this whole thing can be statically
135compiled. So that also checked one of the boxes.
136
137It works on Linux and on a Mac so that’s also a bonus. I don’t believe this
138would work on Windows machines because of the way that I use shell instances. By
139you could use something like Windows Subsystem for Linux and run it in
140there. That is a valid option.
141
142To finish this essay off, how was it to use it in “real life”. I have to be
143honest. Some of the missing features still bother me. `@dotenv` directive is
144still missing and I need to implement this ASAP.
145
146Another thing that needs to happen is support for streaming output. Currently
147commands like `docker-compose` that runs in foreground mode is not compatible
148with Errand. So commands that stream output are an issue. I need to revisit how
149I initiate shell and how I read stdout and stderr. But that shouldn’t be a
150problem.
151
152I have been very satisfied with this thing. I am pleasantly surprised by how
153useful it is. I really wanted to test this in the wild before I commit to it. I
154have more abandoned project than Google and it’s bringing a massive shame to my
155family at this point. So I wanted to be sure that this is even useful. And it
156actually is. Quite surprised at myself.
157
158I really need to package this now and write proper docs. And maybe rewrite
159tokeniser. Its atrocious right now. Site to behold! But that is an issue for
160another time.
diff --git a/_posts/posts/2023-07-01-bringing-all-of-my-projects-together-under-one-umbrella.md b/_posts/posts/2023-07-01-bringing-all-of-my-projects-together-under-one-umbrella.md
new file mode 100644
index 0000000..4bc45ce
--- /dev/null
+++ b/_posts/posts/2023-07-01-bringing-all-of-my-projects-together-under-one-umbrella.md
@@ -0,0 +1,282 @@
1---
2title: "Bringing all of my projects together under one umbrella"
3permalink: /bringing-all-of-my-projects-together-under-one-umbrella.html
4date: 2023-07-01T18:49:07+02:00
5layout: post
6type: post
7draft: false
8---
9
10## What is the issue anyway?
11
12Over the years, I have accumulated a bunch of virtual servers on my
13[DigitalOcean](https://www.digitalocean.com/) account for small experimental
14projects I dabble in. And this has resulted in quite a bill. I mean, I wouldn't
15care if these projects were actually being used. But there were just being there
16unused and wasting resources. Which makes this an unnecessary burden for me.
17
18Most of them are just small HTML pages that have an endpoint or two to read data
19from or to, and for that reason I wrote servers left and right. To be honest,
20all of those things could have been done with [CGI
21scripts](https://en.wikipedia.org/wiki/Common_Gateway_Interface) and that would
22have been more than enough.
23
24Recently, I decided to stop language hopping and focus on a simpler stack which
25includes C, Go and Lua. And I can accomplish all the things I am interested in.
26
27## Finding a web server replacement
28
29Usually I had [Nginx](https://nginx.org/en/) in front of these small web servers
30and I had to manage SSL certificates and all that jazz. I am bored with these
31things. I don't want to manage any of this bullshit anymore.
32
33So the logical move forward was to find a solid alternative for this. I have
34ended up on [Caddy server](https://caddyserver.com/). I've used it in the past
35but kind of forgotten about it. What I really like about it is an ease of use
36and a bunch of out of the box functionalities that come with it.
37
38These are the _pitch_ points from their website:
39
40- **Secure by Default**: Caddy is the only web server that uses HTTPS by
41 default. A hardened TLS stack with modern protocols preserves privacy and
42 exposes MITM attacks.
43- **Config API**: As its primary mode of configuration, Caddy's REST API makes
44 it easy to automate and integrate with your apps.
45- **No Dependencies**: Because Caddy is written in Go, its binaries are entirely
46 self-contained and run on every platform, including containers without libc.
47- **Modular Stack**: Take back control over your compute edge. Caddy can be
48 extended with everything you need using plugins.
49
50I had just a few requirements:
51
52- Automatic SSL
53- Static file server
54- Basic authentication
55- CGI script support
56
57And the vanilla version does all of it, but CGI scripts. But that can easily be
58fixed with their modular approach. You can do this on their website and build a
59custom version of the server, or do it with Docker.
60
61This is a `Dockerfile` I used to build a custom server.
62
63```Dockerfile
64FROM caddy:builder AS builder
65
66RUN xcaddy build \
67 --with github.com/aksdb/caddy-cgi
68
69FROM caddy:latest
70RUN apk add --no-cache nano
71
72COPY --from=builder /usr/bin/caddy /usr/bin/caddy
73```
74
75## Getting rid of all the unnecessary virtual machines
76
77The next step was to get a handle on the number of virtual servers I have all
78over the place.
79
80I decided to move all the projects and services into two main VMs:
81
82- personal server (still Nginx)
83 - git server
84 - static file server
85 - personal blog
86- projects server (Caddy server)
87 - personal experiments
88 - other projects
89
90I will focus on projects' server in this post since it's more interesting.
91
92## Testing CGI scripts
93
94The first thing I tested was how CGI scripts work under Caddy. This is
95particularly import to me because almost all of my experiments and mini projects
96need this to work.
97
98To configure Caddy server, you must provide the server with a configuration
99file. By default, it's called `Caaddyfile`.
100
101```caddyfile
102{
103 order cgi before respond
104}
105
106examples.mitjafelicijan.com {
107 cgi /bash-test /opt/projects/examples/bash-test.sh
108 cgi /tcl-test /opt/projects/examples/tcl-test.tcl
109 cgi /lua-test /opt/projects/examples/lua-test.lua
110 cgi /python-test /opt/projects/examples/python-test.py
111
112 root * /opt/projects/examples
113 file_server
114}
115```
116
117- The order is very important. Make sure that `order cgi before respond` is at
118 the top of the configuration file.
119- Also, when you run with Caddy v2, make sure you provide `adapter` argument
120 like this `/usr/bin/caddy run --watch --environ --config /etc/caddy/Caddyfile
121 --adapter caddyfile`. Otherwise, Caddy will try to use a different format for
122 config file.
123
124I did a small batch of tests with [Bash](https://www.gnu.org/software/bash/),
125[Tcl](https://www.tcl-lang.org/), [Lua](https://www.lua.org/) and
126[Python](https://www.python.org/). Here is a cheat sheet if you need it.
127
128Let's get Bash out of the way first.
129
130```bash
131#!/usr/bin/bash
132
133printf "Content-type: text/plain\n\n"
134
135printf "Hello from Bash\n\n"
136printf "PATH_INFO [%s]\n" $PATH_INFO
137printf "QUERY_STRING [%s]\n" $QUERY_STRING
138printf "\n"
139
140for i in {0..9..1}; do
141 printf "> %s\n" $i
142done
143
144exit 0
145```
146
147This one is for Tcl script.
148
149```tcl
150#!/usr/bin/tclsh
151
152puts "Content-type: text/plain\n"
153
154puts "Hello from Tcl\n"
155puts "PATH_INFO \[$env(PATH_INFO)\]"
156puts "QUERY_STRING \[$env(QUERY_STRING)\]"
157puts ""
158
159for {set i 0} {$i < 10} {incr i} {
160 puts "> $i"
161}
162```
163
164And for all you Python enjoyers.
165
166```python
167#!/usr/bin/python3
168
169import os
170
171print("Content-type: text/plain\n")
172
173print("Hello from Python\n")
174print("PATH_INFO [{}]".format(os.environ['PATH_INFO']))
175print("QUERY_STRING [{}]".format(os.environ['QUERY_STRING']))
176print("")
177
178for i in range(10):
179 print("> {}".format(i))
180```
181
182And for the final example, Lua.
183
184```lua
185#!/usr/bin/lua
186
187print("Content-type: text/plain\n")
188
189print("Hello from Lua\n")
190print(string.format("PATH_INFO [%s]", os.getenv("PATH_INFO")))
191print(string.format("QUERY_STRING [%s]", os.getenv("QUERY_STRING")))
192print()
193
194for i = 0, 9 do
195 print(string.format("> %d", i))
196end
197```
198
199## Basic authentication
200
201One thing was also to have an option for some sort of authentication, and
202something like [Basic access
203authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) would
204be more than enough.
205
206Thankfully, Caddy supports this out of the box already. Below is an updated
207example.
208
209```Caddyfile
210{
211 order cgi before respond
212}
213
214examples.mitjafelicijan.com {
215 cgi /bash-test /opt/projects/examples/bash-test.sh
216 cgi /tcl-test /opt/projects/examples/tcl-test.tcl
217 cgi /lua-test /opt/projects/examples/lua-test.lua
218 cgi /python-test /opt/projects/examples/python-test.py
219
220 root * /opt/projects/examples
221 file_server
222
223 basicauth * {
224 bob $2a$14$/wCgaf9oMnmQa20txB76u.nI1AldGMBT/1J7fXCfgOiRShwz/JOkK
225 }
226}
227```
228
229`basicauth *` matches everything under this domain/sub-domain and protects it
230with Basic Authentication.
231
232- `bob` is the username
233- `hash` is the password
234
235To generate these passwords, execute `caddy hash-password` and this will prompt
236you to insert a password twice and spit out a hashed password that you can put
237in your configuration file.
238
239Restart the server and you are ready to go.
240
241## Making Caddy a service with systemd
242
243After the tests were successful, I copied `caddy` to `/usr/bin/caddy` and copied
244`Caddyfile` to `/etc/caddy/Caddyfile`.
245
246Now off to the systemd. Each systemd service requires you to create a service
247file.
248
249- I created a `/etc/systemd/system/caddy.service` and put the following content
250 in the file.
251
252```systemd
253[Unit]
254Description=Caddy
255Documentation=https://caddyserver.com/docs/
256After=network.target network-online.target
257Requires=network-online.target
258
259[Service]
260Type=notify
261User=root
262Group=root
263ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile --adapter caddyfile
264ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force --adapter caddyfile
265TimeoutStopSec=5s
266LimitNOFILE=1048576
267LimitNPROC=512
268PrivateTmp=true
269ProtectSystem=full
270AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
271
272[Install]
273WantedBy=multi-user.target
274```
275
276- You might need to reload systemd with `systemctl daemon-reload`.
277- Then I enabled the service with `systemctl enable caddy.service`.
278- And then I started the service with `systemctl start caddy.service`.
279
280This was about all that I needed to do to get it running. Now I can easily add
281new subdomains and domains to the main configuration file and be done with
282it. No manual Let's Encrypt shenanigans needed.
diff --git a/_posts/posts/2023-07-08-who-knows-what-the-world-will-look-like-tomorrow.md b/_posts/posts/2023-07-08-who-knows-what-the-world-will-look-like-tomorrow.md
new file mode 100644
index 0000000..c7d52d5
--- /dev/null
+++ b/_posts/posts/2023-07-08-who-knows-what-the-world-will-look-like-tomorrow.md
@@ -0,0 +1,101 @@
1---
2title: "Who knows what the world will look like tomorrow"
3permalink: /who-knows-what-the-world-will-look-like-tomorrow.html
4date: 2023-07-08T18:49:07+02:00
5layout: post
6type: post
7draft: false
8---
9
10This site has gone through a lot of changes over the years. From being written
11in Flask and Bottle to moving on to static site generators. I have used and
12tested probably 10s of them my now. From homebrew solutions to the biggest and
13the baddest. From Bash scripts to Node.js disasters. I've seen some things, no
14doubt. Not all bad.
15
16I have been closely observing the web and where the trends are going, and I
17don't like what I see. Instead of internet being this weird place where
18experimentation is happening, it all became stale and formulized. Boring,
19actually. Really boring. And sad. Where is that old, revolutionary FU spirit I
20remember? It's still there, I know. But it's being drowned by the voices of
21mediocrity and formulaic boredom.
22
23It almost feels like that the internet stopped for 10 years and only now
24something has started happening. With all the insanity around the world. People
25hating people without actual reasons, just because it's fashionable to hate and
26crowd is saying so. Sad state of affairs.
27
28All this is contributing to this overall negativity masked as apathy. Everybody
29walking in lockstep. Instead of being creative and bold, we are just
30re-inventing the world and making the same mistakes. Maybe, just maybe, some
31things are good enough and there is no need to try to be too smart for our own
32good. After N-attempts, maybe something should click inside our heads to maybe
33say: "This thing, opinion, etc. is actually really good, and even after several
34attempts it still holds."
35
36The older I get, the more careful I am of my own thoughts and why I think the
37way I think. More and more, I try to understand people with opposite
38opinions. Far from perfect, but closer to bearable. And then I see people
39hearing or reading a thing on internet and let's fucking goooooo! Strong
40opinions are a sign of a weak and uneducated mind. I am more and more sure of
41this.
42
43It's gotten to a point where you can with great certainty deduce a person's
44personality based on one or two opinions. How boring have we become. No wonder
45people can't talk to each other. These would be very quick conversations anyway.
46
47I just got remembered of a song, ["Hi
48Ren"](https://www.youtube.com/watch?v=s_nc1IVoMxc). The ending talks about being
49stiff and not being able to dance. Such an amazing metaphor. And we as people
50have gone so far, we can't even walk or even crawl normally anymore. We have
51forgotten that the most beautiful things in life have a great deal of
52uncertainty about them. We want instant gratification. Not only that, but we
53want absolute obedience. Complete control over others, because we have zero
54control of ourselves. And all the lies we could tell ourselves will not help us
55out of this situation.
56
57It is funny how I catch myself from time to time being a complete idiot. It's
58like having an outer body experience. I can see myself being an idiot, and
59cannot stop myself. It serves as a learning lesson to stop before speaking. To
60think before saying. And to crawl before walking.
61
62So there is still time. We can dance once more. All we need to do is stop for a
63second. Me and you. Us two is a start. Let's not try to change the world, but
64rather nudge ourselves just a tiny bit. And if we only did that?! Just
65imagine. Each of us nudged ourselves a small, tiny bit, the world would heal. If
66we would just put down the phones and ignored Internet for a day or two. Put
67visiting websites that feed on us on hold. Listened to just one sentence and try
68to understand it from a person who we completely disagree with. I truly believe
69that this is possible.
70
71Life is about suffering and joy. And instead of wishing suffering on others and
72excepting joy for yourselves, we should for a brief moment want suffering for
73ourselves and wish joy on others. Wouldn't that be an amazing sight to see?
74
75I caught myself hating on Rust. And I deeply thought about it afterward. Why did
76I do it? It is obviously not for me. So why the hell was I being so negative
77towards it? I think that I know the answer. I was negative because that is
78easy. Because it's much easier to hate on things than to say to yourself: "Well,
79you know what? This is not for me. I will focus on creation and not
80destruction. This is who I want to be. This is what fills me with joy and
81purpose." Where joy is keeping me happy and purpose scares the shit out of me
82and keeps me honest. This is who I want to be. Admit to myself when I am wrong
83and accept the faults that I have without reservation and with courage march on.
84
85I just realized that this blog post is a sort of therapy for me. It's
86cathartic. Going thought the history of this site and remembering all the
87decisions and annoyances that came with it. When I was cursing at the tools. And
88time moved on, and the site is still here. It serves as a reminder that
89perseverance wins at the end. If we just let things go.
90
91This came with a decision that simplifying life and removing all the unnecessary
92negativity is key. Rather than worrying about what the internet is saying, what
93the world is trying to take from you, you are the only one who can say no. And
94create instead of destroy.
95
96I don't have an ending for this post, so I will say this. We live in the most
97amazing times in the recorded history, and we should be internally grateful for
98it. Create and study, this should be my mantra. Just create and let the world
99happen. And when you feel yourself to be too certain, stop and check how deep in the
100shit you are already. Strong opinions are a sign of a weak and uneducated
101mind. Hate and disdain is for the weak.
diff --git a/_posts/posts/2023-11-05-elitist-attitudes-are-sapping-the-fun-from-programming.md b/_posts/posts/2023-11-05-elitist-attitudes-are-sapping-the-fun-from-programming.md
new file mode 100644
index 0000000..ccee72b
--- /dev/null
+++ b/_posts/posts/2023-11-05-elitist-attitudes-are-sapping-the-fun-from-programming.md
@@ -0,0 +1,97 @@
1---
2title: "Elitist attitudes are sapping all the fun from programming"
3permalink: /elitist-attitudes-are-sapping-all-the-fun-from-programming.html
4date: 2023-11-05T09:04:28+02:00
5layout: post
6type: post
7draft: false
8---
9
10It's always been like that. Maybe it was even worse before, and I am remembering
11it with rose-tinted glasses. But from the best that I can remember, it had at
12least some roots in reality. If something was objectively bad, you could point
13to it. But what I have started noticing recently is that objectivity is not the
14only condition to bash on something. More and more, you can use subjective
15opinion to say horrible things about technology, language or just a specific
16manufacturer.
17
18And all this has achieved is that I don't really listen to anybody anymore. I
19don't care what you think about X or Y. I don't care if you like this language
20or that one. I don't care if you prefer Dell to ThinkPad over Macbook. Who gives
21a fuck, anyway? If you can do your job on it, why even care about this stuff at
22all. And if you can't, buy a different machine.
23
24It's like the politics weren't enough. Now the same tribalism is here as well. C
25developers hating on Rust. JavaScript developers laughing at jQuery users. Rust
26developers laughing at everybody except Haskell users. And everybody laughing at
27JavaScript. It's like this never-ending dream, being stuck in high school. Us
28against your team. It's like we are all stuck being 16. Such a sad state of
29affair. And it's always been like this. But it's getting worse I think.
30
31Everybody trying to be elitist. Compensating lack of JavaScript features (like
32type system, for one) with coming up with this insane terminology to make
33JavaScript sound more sophisticated, as it is. Let's invent terminology to hide
34flaws and sound more educated and academic. And the same goes for C and all the
35other languages. All languages are shitty in some ways. For the love of God,
36why? Just let it be. For once, let things just be.
37
38And I, for one, just do not care anymore. Languages are tools and not your
39identity. If you need a programming language to fill a void in your life, I
40strongly suggest that you re-evaluate where you stand currently. Try something
41else. You are not a C developer, or Go developer, or JavaScript developer. You
42are a problem solver. That's what you are. And be damn proud of it. You don't
43need a label to make that more true or more sophisticated.
44
45I use Linux and macOS. I have fun on both systems. In my personal experience,
46Macbooks are better laptops for what I need them to be. They are better fit for
47me. Portable machines with an amazing battery life. That's all that I need from
48a laptop. I don't need to come up with this insane hypothetical scenarios where
49it will fell short. Yes, it can't water the plants when I am sleeping. OMG, are
50we really going there. These insane hypotheticals. Who really cares? I don't! I
51use it, it does what I need it to do, and that is the end of the story. Not only
52that, but I don't push this down other people's throats. Like Tsodings often
53says: It is what it is, and it isn't what it isn't. Such wise words. On my main
54machine I have Linux and had it for more than 20 years and I love it. I LOVE
55it. I am used to it. And I've had some shitty experiences with it, but damn it,
56I love it. It does what it needs to do. It fits my needs. And if I needed
57Windows, I would find a way to love it too. Why not? There is enough love to go
58around where you are not being elitist and a shithead.
59
60Programming should be fun. Not going through a checklist before you even start,
61to see if you are using what is considered the “cool” thing. If you are doing
62this, you already failed in my opinion.
63
64Oh, you are not using this “insert here” algorithm? Such a pleb. Don't you know
65about O(N) complexity? OMG, such a noob. He doesn't know. Uneducated pleb. 2017
66called, and they want their stack back.
67
68Yes, there is a place for all of those things. But not everything needs to be
69perfect. There is an awesome quote in Uncharted: Sic Parvis Magna. “greatness,
70from small beginnings.”
71
72I would laugh if it wasn't sad. And in the end, who cares. Let these people
73worry about making the perfect solutions that will never ship or take years to
74finish because “Early optimization is the root of all evil.” Everybody has their
75definition of fun. I just don't want to listen to people preaching to others how
76to do stuff. If people would just shut up and think before they speak 5% of the
77time, the world would be a different place. But that will never happen. So the
78only solution is to not give a fuck.
79
80This is more a rant than an actual post with some solution, so maybe I am a part
81of the problem. Who knows? Just venting. Every so often it helps.
82
83Do your Rust thing. It's not for me, though. But if it works for you, more power
84to you. Do your project with vanilla JavaScript. You don't always need
85TypeScript, Next.js or who know what else to make a button do a thing. Use VS
86Code or Vim or Emacs or even Notepad if you wish. If you are having fun, then
87just do it. Don't worry about these elitist pricks. They will never be satisfied
88anyway. You will never get their approval. So why even bother. Just go for
89it. Use C, Rust, OCaml, whatever floats your boat. If it tickles you, just do
90it. To hell with everybody else. And if somebody says O(N) complexity, dude? You
91can say, OOOOO, fuck the fuck off.
92
93If this post triggered you, then you are the asshole. Probably. Then you
94probably are that guy preaching about O(N) or this language is soo slow
95haha. Stop it. Nobody cares! Touch grass.
96
97Anyway, back to having fun. Cheers!
diff --git a/_posts/posts/2024-02-11-k-mer.md b/_posts/posts/2024-02-11-k-mer.md
new file mode 100644
index 0000000..254b5df
--- /dev/null
+++ b/_posts/posts/2024-02-11-k-mer.md
@@ -0,0 +1,141 @@
1---
2title: "Navigating the genome using k-mers for DNA analysis and visualization"
3permalink: /navigating-the-genome-using-k-mers-for-dna-analysis-and-visualization.html
4date: 2024-02-11T01:04:28+02:00
5layout: post
6type: post
7mathjax: yes
8draft: true
9published: false
10---
11
12## Brief introduction to K-mer
13
14A "k-mer" refers to all the possible substrings of length \\(k\\) contained in a
15string, which is commonly used in computational biology and bioinformatics. In
16the context of DNA, RNA, or protein sequences, a k-mer is a sequence of \\(k\\)
17nucleotides (for DNA and RNA) or amino acids (for proteins).
18
19The concept of k-mers is fundamental in various bioinformatics applications,
20including genome assembly, sequence alignment, and identification of repeat
21sequences. By analyzing the frequency and distribution of k-mers within a
22sequence or set of sequences, researchers can infer structural characteristics,
23identify genetic variants, and compare genomic or proteomic compositions between
24different organisms or conditions.
25
26For example, in genome assembly, k-mers are used to reconstruct the sequence of
27a genome from a collection of short sequencing reads. By finding overlaps
28between the k-mers derived from these reads, assembly algorithms can piece
29together contiguous sequences (contigs), which represent longer sections of the
30genome.
31
32The choice of \\(k\\) (the length of the k-mer) is crucial and depends on the
33specific application. A larger \\(k\\) provides more specificity (useful for
34distinguishing between closely related sequences), while a smaller \\(k\\)
35offers greater sensitivity (useful for detecting repeats or low-complexity
36regions). However, the computational resources required increase with \\(k\\),
37as there are \\(4^k\\) possible k-mers for nucleotide sequences (due to the four
38types of nucleotides: A, T, C, G) and \\(20^k\\) for amino acid sequences (due
39to the twenty standard amino acids).
40
41## K-mer counting
42
43K-mer counting is a fundamental process in bioinformatics used for analyzing the
44frequency of k-mers (subsequences of length \\(k\\)) in DNA, RNA, or protein
45sequences. Efficient k-mer counting is crucial for various applications such as
46genome assembly, metagenomics, and sequence comparison. The implementation
47typically involves parsing a sequence into all possible k-mers and then counting
48the occurrences of each unique k-mer. Here's a general approach to implementing
49k-mer counting:
50
51### Reading the Sequences
52
53The first step involves reading the genetic or protein sequences from files,
54which are often in formats like FASTA or FASTQ. These files contain one or
55multiple sequences that will be processed to extract k-mers.
56
57### Generating K-mers
58
59For each sequence, generate all possible subsequences of length \\(k\\). This is
60done by sliding a window of size \\(k\\) across the sequence, one nucleotide (or
61amino acid) at a time, and extracting the subsequence within this window.
62
63### Counting K-mers
64
65The extracted k-mers are then counted. This can be achieved using various data
66structures:
67
68- **Hash Tables (Dictionaries)**: They offer an efficient way to keep track of
69 k-mer counts, with k-mers as keys and their frequencies as values. This
70 approach is straightforward but can become memory-intensive with large
71 datasets or large values of \\(k\\).
72- **Suffix Trees or Arrays**: These data structures are more space-efficient for
73 k-mer counting, especially for large datasets. They allow for efficient
74 retrieval of k-mer occurrences but are more complex to implement.
75- **Bloom Filters and Count-Min Sketch**: For very large datasets, probabilistic
76 data structures like Bloom filters or Count-Min Sketch can estimate k-mer
77 counts using significantly less memory, at the cost of a controlled error
78 rate.
79
80### Handling Memory and Performance Issues
81
82K-mer counting can be memory-intensive, especially for large values of \\(k\\) or
83large datasets. Optimizations include:
84
85- **Compressing K-mers**: Representing k-mers using a binary format rather than
86 strings can save memory.
87- **Parallel Processing**: Distributing the k-mer counting task across multiple
88 processors or machines can significantly speed up the process.
89- **Minimizing I/O Operations**: Efficiently reading and processing sequences
90 from files in chunks reduces I/O overhead.
91
92### Post-processing
93
94After counting, the k-mer frequencies can be used directly for analyses or can
95undergo further processing, such as filtering rare k-mers, which are often
96errors, or normalizing counts for comparative analysis.
97
98### Implementation Example
99
100Here's a simple Python example using a dictionary for k-mer counting:
101
102```python
103def count_kmers(sequence, k):
104 kmer_counts = {}
105 for i in range(len(sequence) - k + 1):
106 kmer = sequence[i:i+k]
107 if kmer in kmer_counts:
108 kmer_counts[kmer] += 1
109 else:
110 kmer_counts[kmer] = 1
111 return kmer_counts
112
113# Example usage
114sequence = "ATGCGATGATCTGATG"
115k = 3
116kmer_counts = count_kmers(sequence, k)
117print(kmer_counts)
118```
119
120This code snippet counts the occurrences of each 3-mer in a given sequence.
121
122For real-world applications, especially those involving large datasets, consider
123using specialized bioinformatics tools like Jellyfish, KMC, or khmer, which are
124optimized for efficiency and scalability.
125
126Now that we have the basics out of the way we can start implementing basic k-mer
127counter in C.
128
129## Implementing sequence reading in C
130
131## Additional reading material
132
133- [2101.08385](https://arxiv.org/pdf/2101.08385.pdf) - Motif Identification using CNN-based Pairwise
134- [2112.15107](https://arxiv.org/pdf/2112.15107.pdf) - Probabilistic Models of k-mer Frequencies
135- [2205.13915](https://arxiv.org/pdf/2205.13915.pdf) - DiMA: Sequence Diversity Dynamics Analyser for Viruses
136- [2209.09242](https://arxiv.org/pdf/2209.09242.pdf) - Computing Phylo-k-mers
137- [2305.07545](https://arxiv.org/pdf/2305.07545.pdf) - KmerCo: A lightweight K-mer counting technique with a tiny memory footprint
138- [2308.01920](https://arxiv.org/pdf/2308.01920.pdf) - Sequence-Based Nanobody-Antigen Binding
139- [2310.10321](https://arxiv.org/pdf/2310.10321.pdf) - Hamming Encoder: Mining Discriminative k-mers for Discrete Sequence Classification
140- [2312.03865](https://arxiv.org/pdf/2312.03865.pdf) - Learning Genomic Sequence Representations using Graph Neural Networks over De Bruijn Graphs
141- [2401.14025](https://arxiv.org/pdf/2401.14025.pdf) - DNA Sequence Classification with Compressors