aboutsummaryrefslogtreecommitdiff
path: root/content/posts
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts')
-rwxr-xr-xcontent/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md24
-rwxr-xr-xcontent/posts/2012-03-09-led-technology-not-so-eco.md20
-rwxr-xr-xcontent/posts/2013-10-24-wireless-sensor-networks.md25
-rwxr-xr-xcontent/posts/2015-11-10-software-development-pitfalls.md83
-rw-r--r--content/posts/2017-03-07-golang-profiling-simplified.md111
-rw-r--r--content/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md134
-rw-r--r--content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md185
-rw-r--r--content/posts/2017-08-11-simple-iot-application.md487
-rw-r--r--content/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md261
-rw-r--r--content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md346
-rw-r--r--content/posts/2019-10-14-simplifying-and-reducing-clutter.md22
-rw-r--r--content/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md86
-rw-r--r--content/posts/2020-03-22-simple-sse-based-pubsub-server.md396
-rw-r--r--content/posts/2020-03-27-create-placeholder-images-with-sharp.md83
-rw-r--r--content/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md76
-rw-r--r--content/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md41
-rwxr-xr-xcontent/posts/2020-05-05-remote-work.md37
-rw-r--r--content/posts/2020-08-15-systemd-disable-wake-onmouse.md47
-rw-r--r--content/posts/2020-09-06-esp-and-micropython.md207
-rw-r--r--content/posts/2020-09-08-bind-warning-on-login.md40
-rw-r--r--content/posts/2020-09-09-digitalocean-sync.md64
-rw-r--r--content/posts/2021-01-24-replacing-dropbox-with-s3.md88
-rw-r--r--content/posts/2021-01-25-goaccess.md164
-rw-r--r--content/posts/2021-06-26-simple-world-clock.md87
-rw-r--r--content/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md34
-rw-r--r--content/posts/2021-08-01-linux-cheatsheet.md395
-rw-r--r--content/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md194
-rw-r--r--content/posts/2021-12-25-running-golang-application-as-pid1.md299
-rw-r--r--content/posts/2021-12-30-wap-mobile-web-before-the-web.md161
-rw-r--r--content/posts/2022-06-28-sentiment-based-on-political-bias.md84
-rw-r--r--content/posts/2022-06-30-trying-out-helix-editor.md31
-rw-r--r--content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md300
-rw-r--r--content/posts/2022-08-13-algae-spotted-on-river-sava.md24
33 files changed, 4636 insertions, 0 deletions
diff --git a/content/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md b/content/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md
new file mode 100755
index 0000000..fdd0075
--- /dev/null
+++ b/content/posts/2011-01-13-most-likely-to-succeed-in-year-of-2011.md
@@ -0,0 +1,24 @@
1---
2title: Most likely to succeed in the year of 2011
3url: most-likely-to-succeed-in-year-of-2011.html
4date: 2011-01-13
5draft: false
6---
7
8The year of 2010 was definitely the year of Geo-location. The market responded beautifully and lots of very cool services were launched. We all have to thank the mobile market for such extensive adoption. With new generations of mobile phones that are not only buffed with high-tech hardware but are also affordable. We can now manage tasks that were not so long time ago, almost Star Trek’ish. And all this had and has great influence on the destination to which we are going now.
9
10Reading all this articles about new innovation about new thriving technologies makes me wonder what’s the next step. The future is the mesh, like Lisa Gansky said in her book The Mesh.
11
12Many still have conservative views on distributed systems. The problems with security of information. Fear of not controlling every aspect of information flow. I am very opened to distributed systems and heterogeneous applications, and I think this is the correct and best way to proceed.
13
14This year will definitely be about communication platforms. Mobile to mobile. Machine to mobile and vice versa. All the tech is available and ready to put into action. Wireless is today’s new mantra. And the concept of semantic web is now ready for industry.
15
16Applications and developers now can gain access to new layers of systems and can prepare and build solutions to meet the high quality needs of market. The speed is everything now.
17
18My vote goes to “Machine to Machine” and “Embedded Systems”!
19
20- [Machine-to-Machine](http://en.wikipedia.org/wiki/Machine-to-Machine)
21- [The ultimate M2M communication protocol](http://www.bitxml.org/)
22- [COOS Project (connectivity initiative)](http://www.coosproject.org/maven-site/1.0.0/project-info.html)
23- [Community for machine-to-machine](http://m2m.com/index.jspa)
24- [Embedded system](http://en.wikipedia.org/wiki/Embedded_system)
diff --git a/content/posts/2012-03-09-led-technology-not-so-eco.md b/content/posts/2012-03-09-led-technology-not-so-eco.md
new file mode 100755
index 0000000..514b1c3
--- /dev/null
+++ b/content/posts/2012-03-09-led-technology-not-so-eco.md
@@ -0,0 +1,20 @@
1---
2title: LED technology might not be as eco-friendly as you think
3url: led-technology-not-so-eco.html
4date: 2012-03-09
5draft: false
6---
7
8There is a lot of talk about LED technology. It is beginning to infiltrate industry at a fast rate, and it’s a challenge for designers and also engineers. I wondered when a weakness will be revealed. Then I stomped on an article talking about harm in using LED technology. It looks like this magical technology is not so magical and eco-friendly.
9
10A new study from the University of California indicates that LED lights contain toxic metals, and should be produced, used and disposed of carefully. Besides the lead and nickel, the bulbs and their associated parts were also found to contain arsenic, copper, and other metals that have been linked to different cancers, neurological damage, kidney disease, hypertension, skin rashes and other illnesses in humans, and to ecological damage in waterways.
11
12Since then, I haven’t yet found any regulation for disposal of LED lights or any other regulation or standard. This might be a problem in the future. And it is a massive drawback. This might have quite an impact on consumer market.
13
14Nevertheless, there is a potential, and I am sure the market will adapt. I also hope I will be reading documents regarding solution for this concern soon.
15
16
17**Additional resources:**
18
19- [Recycling and Disposal of Light Bulbs](http://ezinearticles.com/?Recycling-and-Disposal-of-Light-Bulbs&id=1091304)
20- [How to Dispose of a Low-Energy Light Bulb](http://www.ehow.com/how_7483442_dispose-lowenergy-light-bulb.html)
diff --git a/content/posts/2013-10-24-wireless-sensor-networks.md b/content/posts/2013-10-24-wireless-sensor-networks.md
new file mode 100755
index 0000000..e8948fb
--- /dev/null
+++ b/content/posts/2013-10-24-wireless-sensor-networks.md
@@ -0,0 +1,25 @@
1---
2title: Wireless sensor networks
3url: wireless-sensor-networks.html
4date: 2013-10-24
5draft: false
6---
7
8Zigbee networks have this wonderful capability to self-heal, which means they can reorder connections between them if one of them is inoperable. This works our of the box when you deploy them. But you have to have in mind that achieving this is not as easy as you would think. None of it is plug&play. So to make your life a bit easier, here are some pointers which, I hope, will help you.
9
10- Be careful when you are ordering your equipment abroad. There are many rules and regulations you need to comply before you get your Xbee radios. What they do is they wait until you prove that you won’t use the technology for some kind of evil take over control of the world project :). For this, they have EAR (Export Administration Regulations) which basically means “This product may require a license to export from the United States.”.
11- I don’t know if this applies for every country, but when we purchased our Xbee radios from Mouser, this was mandatory! What we needed to do was to print out a form and write information about our company and send them a copy via email. With this document, we proved that we are a legitimate company.
12- When you complete your purchase and send all the documentation, you are not clear yet. Then customs will take it from there :). There will be some additional costs. Before purchasing, make sure you have as much information about costs as possible. Because it can get costly in the end.
13- I suggest you use companies from your country. You can seriously cut your costs. Here in Slovenia, the best option so far as I know is Farnell. And based on my personal experience, they rock! All I need to say!
14- Make plans when ordering larger quantities. Do not, I say, do not make your orders in December! :) Believe me! You will have problems with stock they can provide for you. So, we were forced to buy some things from Mouser, which was extremely painful because of all the regulations you need to obey when importing goods from the USA.
15- Make sure that firmware version on your Xbee radios is exactly the same! Do not get creative!!! I propose using templates. You can get template by exporting settings/profile in X-CTU application. Make sure you have enabled “Upgrade firmware” so you can be sure each radio has the same firmware.
16- And again: make plans! Plan everything! In months advanced! You will thank me later :)
17- Test, test, test. Wireless networks can be tricky.
18
19If you are serious, I suggest you buy this book, Building Wireless Sensor Networks. You will get a glimpse of how networks work in lumens terms. It is a good starting point for everybody who wants to build wireless networks.
20
21**Additional resources:**
22
23- http://www.digi.com/aboutus/export/generalexportinfo
24- http://doresearch.stanford.edu/research-scholarship/export-controls/export-controlled-or-embargoed-countries-entities-and-persons
25- http://www.bis.doc.gov/licensing/exportingbasics.htm
diff --git a/content/posts/2015-11-10-software-development-pitfalls.md b/content/posts/2015-11-10-software-development-pitfalls.md
new file mode 100755
index 0000000..c5d243a
--- /dev/null
+++ b/content/posts/2015-11-10-software-development-pitfalls.md
@@ -0,0 +1,83 @@
1---
2title: Software development and my favorite pitfalls
3url: software-development-pitfalls.html
4date: 2015-11-10
5draft: false
6---
7
8**Table of contents**
9
101. [Ping emails](#ping-emails)
112. [Everybody is a project manager](#everybody-is-a-project-manager)
123. [We are never wrong](#we-are-never-wrong)
134. [Micromanaging](#micromanaging)
145. [Human contact — no need for it!](#human-contact--no-need-for-it)
156. [MVP is killing innovation](#mvp-is-killing-innovation)
167. [Pressure wasteland](#pressure-wasteland)
178. [Conclusion](#conclusion)
18
19Over the years I had the privilege to work on some very excited projects both in software development field and also in electronics field and every experience taught me some invaluable lessons about how NOT TO approach development. And through this post I will try to point out some absurd, outdated techniques I find the most annoying and damaging during a development cycle. There will be swearing because this topic really gets on my nerves and I never coherently tried to explain them in writing. So if I get heated up, please bear with me.
20
21As new methods of project management are emerging, underlying processes still stay old and outdated. This is mainly because we as people are unable to completely shift away from these approaches.
22
23I was always struggling with communication, and many times that cost me a relationship or two because I was not on the ball all the time. Through every experience, I became more convinced that I am the problem and never ever doubted that the problem may be that communication never evolved a single step from emails. And if you think for a second, not many things have changed around this topic. We just have different representations of email (message boards, chats, project management tools). And I believe this is the real issue we are facing now.
24
25There are many articles written about hyper connectivity and the effects that are a direct result of it. But mainstream does nothing towards it. We are just putting out fires, and we do nothing to prevent it. I am certain this will be a major source of grief in coming years. And what we all can do to avoid this is to change our mindset and experiment on our communication skills, development approaches. We need to maximize possible output that a person can give. And to achieve this we need to listen to them, encourage them. I know that not everybody is a naturally born leader, but with enough practice and encouragement they also can become active participants in leadership.
26
27There are many talks now about methodologies such as Scrum, Kanban, Cleanroom and they all fucking piss me of :). These are all boxes that imprison people and take away their freedom of thought. This is a straightforward mindfuck / amputation of creativity.
28
29Let me list a couple of things that I find really destructive and bad for a project and in a long run company.
30
31## Ping emails
32
33Ping emails are emails you have to write as soon as you receive an email. Its sole purpose is to inform the sender that you received their email, and you are working on it. Its result is only to calm down the sender that their task is being dealt with. It’s intent basically is, I did my job by sending you this email, so I am on clear grounds. I categorize this email as fuck you email. This is one of the most irritating types of emails I need to write. This is the ultimate control freak show you can experience, and it gives the sender a false feeling of control. Newsflash: We do not live in 1982 where there was a possibility that email never reached the destination. I really hate this from the bottom of my heart.
34
35They should be like: “Yes, I am fucking alive, and I am at your service my leash!”. I guess if I would reply like this, I wouldn’t have to write any more of this kind of messages.
36
37## Everybody is a project manager
38
39Well, this is a tough one. I noticed that as soon as you let people to give their suggestions, you are basically screwed. There is a truth in the saying: “Give low expectations and deliver little more than you promised.”.
40
41People tend to take a role of a manager as soon as they are presented with an opportunity. And by getting angry at them, you only provoke yourself. They are not at fault. You just need to tell them they are only giving suggestions and not tasks at the beginning and everything will be alright. But if you give them a feeling that they are in control, you will have immense problems explaining why their features are not in current release.
42
43Project mission must be always leading project requirements and any deviation from it will result in major project butchering. And by this, I mean that the project will get its own path, and you will be left with half done software that helps nobody. Clear mission goals and clean execution will allow you to develop software will clear intent.
44
45## We are never wrong
46
47I find this type of arrogance the worst. We must always conduct ourselves that we are infallible and cannot make mistakes. As soon as a procedure or process is established, there is no room for changes or improvements. This is the most idiotic thing someone can say of think. I think that processes need to involve and change over time. This is imperative and need to have in your organization if you want to improve and develop company. We all need to grow balls and change everything in order to adapt to current situations. Being a prisoner of predefined processes kills creativity.
48
49I am constantly trying new software for project managing and communication. I believe every team has its own dynamic, and it needs to be discovered organically and naturally through many experiments. By putting the team in a box, you are amputating their creativity and therefore minimizing their potential. But if you talk to an executive, you will mainly find archetypical thinking and a strong need to compartmentalize everything from business processes to resource management. And this type of management that often displays micromanagement techniques only works for short periods (couple of years) and then employees either leave the company or become basically retarded drones on autopilot.
50
51## Micromanaging
52
53This basically implies that everybody on the team is an idiot who needs to have a to-do list that they cannot write themselves. How about spoon-feeding the team at launch because besides the team leader, everybody must be a retarded idiot at best?
54
55I prefer milestones as they give developers much more freedom and creativity in developing and not waste their time checking some bizarre to-do list that was not even thought through. Projects constantly change throughout the development cycle, and all you are left at the end is a list of unchecked tasks and the wrath of management why they are not completed. Best WTF moment!
56
57## Human contact — no need for it!
58
59We are vigorously trying to eliminate physical contact by replacing short meetings with software, with no regards that we are not machines. Many times a simple 5-min meeting at morning can solve most of the problems. In rapid development, short bursts of man to man communication is possibly the best way to go.
60
61We now have all this software available, and all what we get out of it is a giant clusterfuck. An obstacle and not a solution. So, why we still use them?
62
63## MVP is killing innovation
64
65Many will disagree with me on this one, but I stand strong by this statement. What I noticed in my experience that all this buzz words around us only mislead and capture us in a circle of solving issues that already have a solution, but we are unable to see it without using some fancy word for it.
66
67The toughest thing to do for a developer is to minimize requirements. Well, this is though only for bad developers. Yes, I said it. There are many types of developers out there. And those unable to minimize feature scope are the ones you don’t need on your team. Their only goal is to solve problems that exist only in their heads. And then you have to argue with them, and waste energy on them, instead of developing your awesome product. They are a cancer and I suggest you cut them off.
68
69MVP as an idea is great, but sadly people don’t understand underlying philosophy, and they spent too much time focusing and fixating on something that every sane person with normal IQ will understand without some made up acronym. And the result is a lot of talking and barely no execution.
70
71Well, MVP is not directly killing innovation, but stupid people do when they try to understand it.
72
73## Pressure wasteland
74
75You must never allow to be pressured into confirming a deadline if you are not confident. We often feel a need that we are in service of others, which is true to some extent. But it is also true that others are in service to us to some extent. And we forget this all the time. We are all pressured all the time to make decisions just to calm other people down. And when they leave your office you experience WTF moment :) How the hell did they manage to fuck me up again?
76
77People need to realize that the more pressure you put on somebody, the less they will be able to do. So 5-min update email requests will only resolve in mental breakdown and inability to work that day. Constant poking is probably the only thing I lose my mind instantly. For all you that are doing this: “Stop bothering us with your insecurities and let us do our job. We will do it quicker and better without you breathing down our necks.”
78
79If this happens to me, I end up with no energy at the end. Don’t you get it? You will get much more from and out of me if you ask me like a human person and not your personal butler. On a long run, you are destroying your relationships and nobody would want to work with you. Your schizophrenic approach will damage only you in a long run. Nobody is anybody’s property.
80
81## Conclusion
82
83I am guilty of many things described in this post. And I find it hard sometimes to acknowledge this. And I lie to myself and try vigorously to find some explanation why I do these things. There is always space for growth. And maybe you will also find some of yourself in this post and realize what needs to change for you to evolve.
diff --git a/content/posts/2017-03-07-golang-profiling-simplified.md b/content/posts/2017-03-07-golang-profiling-simplified.md
new file mode 100644
index 0000000..d784dcc
--- /dev/null
+++ b/content/posts/2017-03-07-golang-profiling-simplified.md
@@ -0,0 +1,111 @@
1---
2title: Golang profiling simplified
3url: golang-profiling-simplified.html
4date: 2017-03-07
5draft: false
6---
7
8Many posts have been written regarding profiling in Golang and I haven’t found proper tutorial regarding this. Almost all of them are missing some part of important information and it gets pretty frustrating when you have a deadline and are not finding simple distilled solution.
9
10Nevertheless, after searching and experimenting I have found a solution that works for me and probably should also for you.
11
12## Where are my pprof files?
13
14By default pprof files are generated in /tmp/ folder. You can override folder where this files are generated programmatically in your golang code as we will see below in example.
15
16## Why is my CPU profile empty?
17
18I have found out that sometimes CPU profile is empty because program was not executing long enough. Programs, that execute too quickly don’t produce pprof file in my cases. Well, file is generated but only contains 4KB of information.
19
20## Profiling
21
22As you can see from examples we are executing dummy_benchmark functions to ensure some sort of execution. Memory profiling can be done without such a “complex” function. But CPU profiling needs it.
23
24Both memory and CPU profiling examples are almost the same. Only parameters in main function when calling profile.Start are different. When we set profile.ProfilePath(“.”) we tell profiler to store pprof files in the same folder as our program.
25
26### Memory profiling
27
28```go
29package main
30
31import (
32 "fmt"
33 "time"
34 "github.com/pkg/profile"
35)
36
37func dummy_benchmark() {
38
39 fmt.Println("first set ...")
40 for i := 0; i < 918231333; i++ {
41 i *= 2
42 i /= 2
43 }
44
45 <-time.After(time.Second*3)
46
47 fmt.Println("sencond set ...")
48 for i := 0; i < 9182312232; i++ {
49 i *= 2
50 i /= 2
51 }
52}
53
54func main() {
55 defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
56 dummy_benchmark()
57}
58```
59
60### CPU profiling
61
62```go
63package main
64
65import (
66 "fmt"
67 "time"
68 "github.com/pkg/profile"
69)
70
71func dummy_benchmark() {
72
73 fmt.Println("first set ...")
74 for i := 0; i < 918231333; i++ {
75 i *= 2
76 i /= 2
77 }
78
79 <-time.After(time.Second*3)
80
81 fmt.Println("sencond set ...")
82 for i := 0; i < 9182312232; i++ {
83 i *= 2
84 i /= 2
85 }
86}
87
88func main() {
89 defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
90 dummy_benchmark()
91}
92```
93
94### Generating profiling reports
95
96```bash
97# memory profiling
98go build mem.go
99./mem
100go tool pprof -pdf ./mem mem.pprof > mem.pdf
101
102# cpu profiling
103go build cpu.go
104./cpu
105go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf
106```
107
108This will generate PDF document with visualized profile.
109
110- [Memory PDF profile example](/go-profiling/golang-profiling-mem.pdf)
111- [CPU PDF profile example](/go-profiling/golang-profiling-cpu.pdf)
diff --git a/content/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md b/content/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md
new file mode 100644
index 0000000..6fca4ce
--- /dev/null
+++ b/content/posts/2017-04-17-what-i-ve-learned-developing-ad-server.md
@@ -0,0 +1,134 @@
1---
2title: What I've learned developing ad server
3url: what-i-ve-learned-developing-ad-server.html
4date: 2017-04-17
5draft: false
6---
7
8For the past year and half I have been developing native advertising server that contextually matches ads and displays them in different template forms on variety of websites. This project grew from serving thousands of ads per day to millions.
9
10The system is made from couple of core components:
11
12- API for serving ads,
13- Utils - cronjobs and queue management tools,
14- Dashboard UI.
15
16Initial release was using [MongoDB](https://www.mongodb.com/) for full-text search but was later replaced by [Elasticsearch](https://www.elastic.co/) for better CPU utilization and better search performance. This provided us with many amazing functionalities of [Elasticsearch](https://www.elastic.co/). You should check it out if you do any search related operations.
17
18Because the premise of the server is to provide native ad experience, they are rendered on the client side via simple templating engine. This ensures that ads can be displayed number of different ways based on the visual style of the page. And this makes JavaScript client library quite complex.
19
20So now that you know basic information about the product lets get into the lessons we learned.
21
22## Aggregate everything
23
24After beta version was released everything (impressions, clicks, etc) was written in nanosecond resolution in the database. At that time we were using [PostgreSQL](https://www.postgresql.org/) and database quickly grew way above 200GB in disk space. And that was problematic. Statistics took disturbingly long time to aggregate. Also using indexes on stats table in database was no help after we reached 500 million datapoints.
25
26> There is a marketing product information and there is real life experience. And the tend to be quite the opposite.
27
28This was the reason that now everything is aggregated on daily basis and this data is then fed to Elastic in form of daily summary. With this we achieved we can now track many more dimensions such as zone, channel and platform information. And with this information we can now adapt occurrences of ads on specific places more precisely.
29
30We have also adapted [Redis](https://redis.io/) as a full-time citizen in our stack. Because Redis also stores information on a local disk we have some sort of backup if server would accidentally suffer some failure.
31
32All the real-time statistics for ad serving and redirecting is presented as counters in Redis instance and daily extracted and pushed to Elastic.
33
34## Measure everything
35
36The thing about software is that we really don't know how well it is performing under load until such load is presented. When testing locally everything is fine but when on production things tend to fall apart.
37
38As a solution for this we are measuring everything we can. Function execution time (by encapsulating functions with timers), server performance (cpu, memory, disk, etc), Nginx and [uWSGI](https://uwsgi-docs.readthedocs.io/) performance. We sacrifice a bit of performance for the sake of this information. And we store all this information for later analysis.
39
40**Example of function execution time**
41
42```json
43{
44 "get_final_filtered_ads": {
45 "counter": 1931250,
46 "avg": 0.0066143431,
47 "elapsed": 12773.9500310003
48 },
49 "store_keywords_statistics": {
50 "counter": 1931011,
51 "avg": 0.0004605267,
52 "elapsed": 889.2821669996
53 },
54 "match_by_context": {
55 "counter": 1931011,
56 "avg": 0.0055960716,
57 "elapsed": 10806.0758889999
58 },
59 "match_by_high_performance": {
60 "counter": 262,
61 "avg": 0.0152770229,
62 "elapsed": 4.00258
63 },
64 "store_impression_stats": {
65 "counter": 1931250,
66 "avg": 0.0006189991,
67 "elapsed": 1195.4419869999
68 }
69}
70```
71
72We have also started profiling with [cProfile](https://pymotw.com/2/profile/) and then visualizing with [KCachegrind](http://kcachegrind.sourceforge.net/). This provides much more detailed look into code execution.
73
74## Cache control is your friend
75
76Because we use Javascript library for rendering ads we rely on this script extensively and when in need we need to be able to change behavior of the script quickly.
77
78In our case we can not simply replace javascript url in html code. It usually takes a day or two for the guys who maintain sites to change code or add ?ver=xxx attribute. And this makes rapid deployment and testing very difficult and time consuming. There is a limitation of how much you can test locally.
79
80We are now in the process of integrating [Google Tag Manager](https://www.google.com/analytics/tag-manager/) but couple of websites are developed on ASP.net platform that have some problems with tag manager. With a solution below we are certain that we are serving latest version of the script.
81
82And it only takes one mistake and users have the script cached and in case of caching it for 1 year you probably know where the problem is.
83
84```nginx
85# nginx ➜ /etc/nginx/sites-available/default
86location /static/ {
87 alias /path-to-static-content/;
88 autoindex off;
89 charset utf-8;
90 gzip on;
91 gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
92 location ~* \.(ico|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
93 expires 1y;
94 add_header Pragma public;
95 add_header Cache-Control "public";
96 }
97 location ~* \.(css|js|txt)$ {
98 expires 3600s;
99 add_header Pragma public;
100 add_header Cache-Control "public, must-revalidate";
101 }
102}
103```
104
105Also be careful when redirecting to url in your python code. We noticed that if we didn't precisely setup cache control and expire headers in response we didn't get the request on the server and therefore couldn't measure clicks. So when redirecting do as follows and there will be no problems.
106
107```python
108# python ➜ bottlepy web micro-framework
109response = bottle.HTTPResponse(status=302)
110response.set_header("Cache-Control", "no-store, no-cache, must-revalidate")
111response.set_header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
112response.set_header("Location", url)
113return response
114```
115
116> Cache control in browsers is quite aggressive and you need to be precise to avoid future problems. We learned that lesson the hard way.
117
118## Learn NGINX
119
120When deciding on a web server we went with Nginx as a reverse proxy for our applications. We adapted micro-service oriented architecture early in the project to ensure when we scale we can easily add additional servers to our cluster. And Nginx was crucial to perform load balancing and static content delivery.
121
122At first our config file was quite simple and later grew larger. After patching and adding new settings I sat down and learned more about the guts of Nginx. This proved to be very useful and we were able to squeeze much more out of our setup. So I advise you to take your time and read through the [documentation](https://nginx.org/en/docs/). This saved us a lot of headache. Googling for solutions only goes so far.
123
124## Use Redis/Memcached
125
126As explained above we are using caching basically for everything. It is the corner stone of our services. At first we were very careful about the quantity of things we stored in [Redis](https://redis.io/). But we later found out that the memory footprint is very low even when storing large amount of data in it.
127
128So we gradually increased our usage to caching whole HTML outputs of dashboard. This improved our performance in order of magnitude. And by using native TTL support this goes hand in hand with our needs.
129
130The reason why we choose [Redis](https://redis.io/) over [Memcached](https://memcached.org/) was the nature of scalability of Redis out of the box. But all this can be achieved with Memcached.
131
132## Conclusion
133
134There are a lot more details that could have been written and every single topic in here deserves it's own post but you probably got the idea about the problems we faced.
diff --git a/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md b/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
new file mode 100644
index 0000000..3fe8a9d
--- /dev/null
+++ b/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
@@ -0,0 +1,185 @@
1---
2title: Profiling Python web applications with visual tools
3url: profiling-python-web-applications-with-visual-tools.html
4date: 2017-04-21
5draft: false
6---
7
8I have been profiling my software with KCachegrind for a long time now and I was missing this option when I am developing API's or other web services. I always knew that this is possible but never really took the time and dive into it.
9
10Before we begin there are some requirements. We will need to:
11
12- implement [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile) into our web app,
13- convert output to [callgrind](http://valgrind.org/docs/manual/cl-manual.html) format with [pyprof2calltree](https://pypi.python.org/pypi/pyprof2calltree/),
14- visualize data with [KCachegrind](http://kcachegrind.sourceforge.net/html/Home.html) or [Profiling Viewer](http://www.profilingviewer.com/).
15
16
17If you are using MacOS you should check out [Profiling Viewer](http://www.profilingviewer.com/) or [MacCallGrind](http://www.maccallgrind.com/).
18
19![KCachegrind](/python-profiling/kcachegrind.png)
20
21We will be dividing this post into two main categories:
22
23- writing simple web-service,
24- visualize profile of this web-service.
25
26## Simple web-service
27
28Let's use virtualenv so we won't pollute our base system. If you don't have virtualenv installed on your system you can install it with pip command.
29
30```bash
31# let's install virtualenv globally
32$ sudo pip install virtualenv
33
34# let's also install pyprof2calltree globally
35$ sudo pip install pyprof2calltree
36
37# now we create project
38$ mkdir demo-project
39$ cd demo-project/
40
41# now let's create folder where we will store profiles
42$ mkdir prof
43
44# now we create empty virtualenv in venv/ folder
45$ virtualenv --no-site-packages venv
46
47# we now need to activate virtualenv
48$ source venv/bin/activate
49
50# you can check if virtualenv was correctly initialized by
51# checking where your python interpreter is located
52# if command bellow points to your created directory and not some
53# system dir like /usr/bin/python then everything is fine
54$ which python
55
56# we can check now if all is good ➜ if ok couple of
57# lines will be displayed
58$ pip freeze
59# appdirs==1.4.3
60# packaging==16.8
61# pyparsing==2.2.0
62# six==1.10.0
63
64# now we are ready to install bottlepy ➜ web micro-framework
65$ pip install bottle
66
67# you can deactivate virtualenv but you will then go
68# under system domain ➜ for now don't deactivate
69$ deactivate
70```
71
72We are now ready to write simple web service. Let's create file app.py and paste code bellow in this newly created file.
73
74```python
75# -*- coding: utf-8 -*-
76
77import bottle
78import random
79import cProfile
80
81app = bottle.Bottle()
82
83# this function is a decorator and encapsulates function
84# and performs profiling and then saves it to subfolder
85# prof/function-name.prof
86# in our example only awesome_random_number function will
87# be profiled because it has do_cprofile defined
88def do_cprofile(func):
89 def profiled_func(*args, **kwargs):
90 profile = cProfile.Profile()
91 try:
92 profile.enable()
93 result = func(*args, **kwargs)
94 profile.disable()
95 return result
96 finally:
97 profile.dump_stats("prof/" + str(func.__name__) + ".prof")
98 return profiled_func
99
100
101# we use profiling over specific function with including
102# @do_cprofile above function declaration
103@app.route("/")
104@do_cprofile
105def awesome_random_number():
106 awesome_random_number = random.randint(0, 100)
107 return "awesome random number is " + str(awesome_random_number)
108
109@app.route("/test")
110def test():
111 return "dummy test"
112
113if __name__ == '__main__':
114 bottle.run(
115 app = app,
116 host = "0.0.0.0",
117 port = 4000
118 )
119
120# run with 'python app.py'
121# open browser 'http://0.0.0.0:4000'
122```
123
124When browser hits awesome\_random\_number() function profile is created in prof/ subfolder.
125
126## Visualize profile
127
128Now let's create callgrind format from this cProfile output.
129
130```bash
131$ cd prof/
132$ pyprof2calltree -i awesome_random_number.prof
133# this creates 'awesome_random_number.prof.log' file in the same folder
134```
135
136This file can be opened with visualizing tools listed above. In this case we will be using Profilling Viewer under MacOS. You can open image in new tab. As you can see from this example there is hierarchy of execution order of your code.
137
138![Profilling Viewer](/python-profiling/profiling-viewer.png)
139
140> Make sure you convert output of the cProfile output every time you want to refresh and take a look at your possible optimizations because cProfile updates .prof file every time browser hits the function.
141
142This is just a simple example but when you are developing real-life applications this can be very illuminating, especially to see which parts of your code are bottlenecks and need to be optimized.
143
144## Update 2017-04-22
145
146Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this awesome web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/) that directly takes output from [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile) module.
147
148<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>
149
150```bash
151# let's install it globally as well
152$ sudo pip install snakeviz
153
154# now let's visualize
155$ cd prof/
156$ snakeviz awesome_random_number.prof
157# this automatically opens browser window and
158# shows visualized profile
159```
160
161![SnakeViz](/python-profiling/snakeviz.png)
162
163Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better way for installing pip software by targeting user level instead of using sudo.
164
165<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>
166
167```bash
168# now we need to add this path to our $PATH variable
169# we do this my adding this line at the end of your
170# ~/.bashrc file
171PATH=$PATH:$HOME/.local/bin/
172
173# in order to use this new configuration you can close
174# and reopen terminal or reload .bashrc file
175$ source ~/.bashrc
176
177# now let's test if new directory is present in $PATH
178$ echo $PATH
179
180# now we can install on user level by adding --user
181# without use of sudo
182$ pip install snakeviz --user
183```
184
185Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can use [pipsi](https://github.com/mitsuhiko/pipsi).
diff --git a/content/posts/2017-08-11-simple-iot-application.md b/content/posts/2017-08-11-simple-iot-application.md
new file mode 100644
index 0000000..b48f2fb
--- /dev/null
+++ b/content/posts/2017-08-11-simple-iot-application.md
@@ -0,0 +1,487 @@
1---
2title: Simple IOT application supported by real-time monitoring and data history
3url: simple-iot-application.html
4date: 2017-08-11
5draft: false
6---
7
8## Initial thoughts
9
10I have been developing these kind of application for the better part of my last 5 years and people keep asking me how to approach developing such application and I will give a try explaining it here.
11
12IOT applications are really no different than any other kind of applications. We have data that needs to be collected and visualized in some form of tables or charts. The main difference here is that most of the times these data is collected by some kind of device foreign to developer that mainly operates in web domain. But fear not, it's not that different than writing some JavaScript.
13
14There are many devices able to transmit data via wireless or wired network by default but for the sake of example we will be using commonly known Arduino with wireless module already on the board → [Arduino MKR1000](https://store.arduino.cc/arduino-mkr1000).
15
16In order to make this little project as accessible to others as possible I will try to make it as inexpensive as possible. And by this I mean that I will avoid using hosted virtual servers and will be using my own laptop as a server. But you must buy Arduino MKR1000 to follow steps below. But if you would want to deploy this software I would suggest using [DigitalOcean](https://www.digitalocean.com) → smallest VPS is only per month making this one of the most affordable option out there. Please notice that this software will not run on stock web hosting that only supports LAMP (Linux, Apache, MySQL, and PHP).
17
18_But before we begin please take notice that this is strictly experimental code and not well optimized and there are much better ways in handling some aspects of the application but that requires much deeper knowledge of technology that is not needed for an example like this._
19
20**Development steps**
21
221. Simple Python API that will receive and store incoming data.
232. Prototype C++ code that will read "sensor data" and transmit it to API.
243. Data visualization with charts → extends Python web application.
25
26Step 1. and 3. will share the same web application. One route will be dedicated to API and another to serving HTML with chart.
27
28Schema below represents what we will try to achieve and how different parts correlates to each other.
29
30![Overview](/iot-application/simple-iot-application-overview.svg)
31
32## Simple Python API
33
34I have always been a fan of simplicity so we will be using [Bottle: Python Web Framework](https://bottlepy.org/docs/dev/). It is a single file web framework that seriously simplifies working with routes, templating and has built-in web server that satisfies our need in this case.
35
36First we need to install bottle package. This can be done by downloading ```bottle.py``` and placing it in the root of your application or by using pip software ```pip install bottle --user```.
37
38If you are using Linux or MacOS then Python is already installed. If you will try to test this on Windows please install [Python for Windows](https://www.python.org/downloads/windows/). There may be some problems with path when you will try to launch ```python webapp.py``` so please take care of this before you continue.
39
40### Basic web application
41
42Most basic bottle application is quite simple. Paste code below in ```webapp.py``` file and save.
43
44```python
45# -*- coding: utf-8 -*-
46
47import bottle
48
49# initializing bottle app
50app = bottle.Bottle()
51
52# triggered when / is accessed from browser
53# only accepts GET → no POST allowed
54@app.route("/", method=["GET"])
55def route_default():
56 return "howdy from python"
57
58# starting server on http://0.0.0.0:5000
59if __name__ == "__main__":
60 bottle.run(
61 app = app,
62 host = "0.0.0.0",
63 port = 5000,
64 debug = True,
65 reloader = True,
66 catchall = True,
67 )
68```
69
70To run this simple application you should open command prompt or terminal on your machine and go to the folder containing your file and type ```python webapp.py```. If everything goes ok then open your web browser and point it to ```http://0.0.0.0:5000```.
71
72If you would like change the port of your application (like port 80) and not use root to run your app this will present a problem. The TCP/IP port numbers below 1024 are privileged ports → this is a security feature. So in order of simplicity and security use a port number above 1024 like I have used port 5000.
73
74If this fails at any time please fix it before you continue, because nothing below will work otherwise.
75
76We use 0.0.0.0 as default host so that this app is available over your local network. If you find your local ip ```ifconfig``` and try accessing this site with your phone (if on same network/router as your machine) this should work as well (example of such ip ```http://192.168.1.15:5000```). This is a must have because Arduino will be accessing this application to send it's data.
77
78### Web application security
79
80There is a lot to be said about security and is a topic of many books. Of course all this can not be written here but to just establish some basic security → you should always use SSL with your application. Some fantastic free certificates are available by [Let's Encrypt - Free SSL/TLS Certificates](https://letsencrypt.org). With SSL certificate installed you should then make use of HTTP headers and send your "API key" via a header. If your key is send via header then this key is encrypted by SSL and send encrypted over the network. Never send your api keys by GET parameter like ```http://example.com/?api_key=somekeyvalue```. The problem that this kind of sending presents is that this key is visible in logs and by network sniffers.
81
82There is a fantastic article describing some aspects about security: [11 Web Application Security Best Practices](https://www.keycdn.com/blog/web-application-security-best-practices/). Please check it out.
83
84### Simple API for writing data-points
85
86We will now be using boilerplate code from example above and extend it to be able to write data received by API to local storage. For example use I will use SQLite3 because it plays well with Python and can store quite large amount of data. I have been using it to collect gigabytes of data in a single database without any corruption or problems → your experience may vary.
87
88To avoid learning SQLite I will be using [Dataset: databases for lazy people](https://dataset.readthedocs.io/en/latest/index.html). This package abstracts SQL and simplifies writing and reading data from database. You should install this package with pip software ```pip install dataset --user```.
89
90Because API will use POST method I will be testing if code works correctly by using [Restlet Client for Google Chrome](https://chrome.google.com/webstore/detail/restlet-client-rest-api-t/aejoelaoggembcahagimdiliamlcdmfm). This software also allows you to set headers → for basic security with API_KEY.
91
92To quickly generate passwords or API keys I usually use this nifty website [RandomKeygen](https://randomkeygen.com/).
93
94Copy and paste code below over your previous code in file ```webapp.py```.
95
96```python
97# -*- coding: utf-8 -*-
98
99import time
100import bottle
101import random
102import dataset
103
104# initializing bottle app
105app = bottle.Bottle()
106
107# connects to sqlite database
108# check_same_thread=False allows using it in multi-threaded mode
109app.config["dsn"] = dataset.connect("sqlite:///data.db?check_same_thread=False")
110
111# api key that will be used in Arduino code
112app.config["api_key"] = "JtF2aUE5SGHfVJBCG5SH"
113
114# triggered when /api is accessed from browser
115# only accepts POST → no GET allowed
116@app.route("/api", method=["POST"])
117def route_default():
118 status = 400
119 ts = int(time.time()) # current timestamp
120 value = bottle.request.body.read() # data from device
121 api_key = bottle.request.get_header("Api_Key") # api key from header
122
123 # outputs to console received data for debug reason
124 print ">>> {} :: {}".format(value, api_key)
125
126 # if api_key is correct and value is present
127 # then writes attribute to point table
128 if api_key == app.config["api_key"] and value:
129 app.config["dsn"]["point"].insert(dict(ts=ts, value=value))
130 status = 200
131
132 # we only need to return status
133 return bottle.HTTPResponse(status=status, body="")
134
135# starting server on http://0.0.0.0:5000
136if __name__ == "__main__":
137 bottle.run(
138 app = app,
139 host = "0.0.0.0",
140 port = 5000,
141 debug = True,
142 reloader = True,
143 catchall = True,
144 )
145```
146
147To run this simply go to folder containing python file and run ```python webapp.py``` from terminal. If everything goes ok you should have simple API available via POST method on /api route.
148
149After testing the service with Restlet Client you should be able to view your data in a database file ```data.db```.
150
151![REST settings example](/iot-application/iot-rest-example.png)
152
153You can also check the contents of new database file by using desktop client for SQLite → [DB Browser for SQLite](http://sqlitebrowser.org/).
154
155![SQLite database example](/iot-application/iot-sqlite-db.png)
156
157Table structure is as simple as it can be. We have ts (timestamp) and value (value from Arduino). As you can see timestamp is generated on API side. If you would happen to have atomic clock on Arduino it would be then better to generate and send timestamp with the value. This would be particularity useful if we would be collecting sensor data at a higher frequency and then sending this data in bulk to API.
158
159If you will deploy this app with uWSGI and multi-threaded, use DSN (Data Source Name) url with ```?check_same_thread=False```.
160
161Ok, now that we have some sort of a working API with some basic security so unwanted people can not post data to your database can we proceed further and try to program Arduino to send data to API.
162
163## Sending data to API with Arduino MKR1000
164
165First of all you should have MKR1000 module and microUSB cable to proceed. If you have ever done any work with Arduino you should know that you also need [Arduino IDE](https://www.arduino.cc/en/Main/Software). On provided link you should be able to download and install IDE. Once that task is completed and you have successfully run blink example you should proceed to the next step.
166
167In order to use wireless capabilities of MKR1000 you need to first install [WiFi101 library](https://www.arduino.cc/en/Reference/WiFi101) in Arduino IDE. Please check before you install, you may already have it installed.
168
169Code below is a working example that sends data to API. Before you try to test your code make sure you have run Python web application. Then change settings for wifi, api endpoint and api_key. If by some reason code bellow doesn't work for you please leave a comment and I'll try to help.
170
171Once you have opened IDE and copied this code try to compile and upload it. Then open "Serial monitor" to see if any output is presented by Arduino.
172
173```c
174#include <WiFi101.h>
175
176// wifi settings
177char ssid[] = "ssid-name";
178char pass[] = "ssid-password";
179
180// api server enpoint
181char server[] = "192.168.6.22";
182int port = 5000;
183
184// api key that must be the same as the one in Python code
185String api_key = "JtF2aUE5SGHfVJBCG5SH";
186
187// frequency data is sent in ms - every 5 seconds
188int timeout = 1000 * 5;
189
190int status = WL_IDLE_STATUS;
191
192void setup() {
193
194 // initialize serial and wait for port to open:
195 Serial.begin(9600);
196 delay(1000);
197
198 // check for the presence of the shield
199 if (WiFi.status() == WL_NO_SHIELD) {
200 Serial.println("WiFi shield not present");
201 while (true);
202 }
203
204 // attempt to connect to wifi network
205 while (status != WL_CONNECTED) {
206 Serial.print("Attempting to connect to SSID: ");
207 Serial.println(ssid);
208 status = WiFi.begin(ssid, pass);
209 // wait 10 seconds for connection
210 delay(10000);
211 }
212
213 // output wifi status to serial monitor
214 Serial.print("SSID: ");
215 Serial.println(WiFi.SSID());
216
217 IPAddress ip = WiFi.localIP();
218 Serial.print("IP Address: ");
219 Serial.println(ip);
220
221 long rssi = WiFi.RSSI();
222 Serial.print("signal strength (RSSI):");
223 Serial.print(rssi);
224 Serial.println(" dBm");
225}
226
227void loop() {
228
229 WiFiClient client;
230
231 if (client.connect(server, port)) {
232
233 // I use random number generator for this example
234 // but you can use analog or digital inputs from arduino
235 String content = String(random(1000));
236
237 client.println("POST /api HTTP/1.1");
238 client.println("Connection: close");
239 client.println("Api-Key: " + api_key);
240 client.println("Content-Length: " + String(content.length()));
241 client.println();
242 client.println(content);
243
244 delay(100);
245 client.stop();
246 Serial.println("Data sent successfully ...");
247
248 } else {
249 Serial.println("Problem sending data ...");
250 }
251
252 // waits for x seconds and continue looping
253 delay(timeout);
254
255}
256```
257
258As seen from example you can notice that Arduino is generating random integer between [ 0 .. 1000 ]. You can easily replace this with a temperature sensor or any other kind of sensor.
259
260Now that we have API under the hood and Arduino is sending demo data we can now focus on data visualization.
261
262## Data visualization
263
264Before we continue we should examine our project folder structure. Currently we only have two files in our project:
265
266_simple-iot-app/_
267
268* _webapp.py_
269* _data.db_
270
271We will now add HTML template that will contain CSS and JavaScript code inline for the simplicity reason. And for the bottle framework to be able to scan root application folder for templates we will add ```bottle.TEMPLATE_PATH.insert(0, "./")``` in ```webapp.py```. By default bottle framework uses ```views/``` subfolder to store templates. This is not the ideal situation and if you will use bottle to develop web applications you should use native behavior and store templates in it's predefined folder. But for the sake of example we will over-ride this. Be careful to fully replace your code with new code that is provided below. Avoid partially replacing code in file :) Also new code for reading data-points is provided in Python example below.
272
273First we add new route to our web application. It should be trigger when browser hits root of application ```http://0.0.0.0:5000/```. This route will do nothing more than render ```frontend.html``` template. This is done by ```return bottle.template("frontend.html")```. Check code below to further examine how exactly this is done.
274
275Now we will expand ```/api``` route and use different methods to write or read data-points. For writing data-point we will use POST method and for reading points we will use GET method. GET method will return JSON object with latest readings and historical data.
276
277There is a fantastic JavaScript library for plotting time-series charts called [MetricsGraphics.js](https://www.metricsgraphicsjs.org) that is based on [D3.js](https://d3js.org/) library for visualizing data.
278
279Data schema required by MetricsGraphics.js → to achieve this we need to transform data from database into this format:
280
281```json
282[
283 {
284 "date": "2017-08-11 01:07:20",
285 "value": 933
286 },
287 {
288 "date": "2017-08-11 01:07:30",
289 "value": 743
290 }
291]
292```
293
294Web application is now complete and we only need ```frontend.html``` that we will develop now. If you would try to start web app now and go to root app this will return error because we don't have frontend.html yet.
295
296```python
297# -*- coding: utf-8 -*-
298
299import time
300import bottle
301import json
302import datetime
303import random
304import dataset
305
306# initializing bottle app
307app = bottle.Bottle()
308
309# adds root directory as template folder
310bottle.TEMPLATE_PATH.insert(0, "./")
311
312# connects to sqlite database
313# check_same_thread=False allows using it in multi-threaded mode
314app.config["db"] = dataset.connect("sqlite:///data.db?check_same_thread=False")
315
316# api key that will be used in Arduino code
317app.config["api_key"] = "JtF2aUE5SGHfVJBCG5SH"
318
319# triggered when / is accessed from browser
320# only accepts GET → no POST allowed
321@app.route("/", method=["GET"])
322def route_default():
323 return bottle.template("frontend.html")
324
325# triggered when /api is accessed from browser
326# accepts POST and GET
327@app.route("/api", method=["GET", "POST"])
328def route_default():
329
330 # if method is POST then we write datapoint
331 if bottle.request.method == "POST":
332 status = 400
333 ts = int(time.time()) # current timestamp
334 value = bottle.request.body.read() # data from device
335 api_key = bottle.request.get_header("Api-Key") # api key from header
336
337 # outputs to console recieved data for debug reason
338 print ">>> {} :: {}".format(value, api_key)
339
340 # if api_key is correct and value is present
341 # then writes attribute to point table
342 if api_key == app.config["api_key"] and value:
343 app.config["db"]["point"].insert(dict(ts=ts, value=value))
344 status = 200
345
346 # we only need to return status
347 return bottle.HTTPResponse(status=status, body="")
348
349 # if method is GET then we read datapoint
350 else:
351 response = []
352 datapoints = app.config["db"]["point"].all()
353
354 for point in datapoints:
355 response.append({
356 "date": datetime.datetime.fromtimestamp(int(point["ts"])).strftime("%Y-%m-%d %H:%M:%S"),
357 "value": point["value"]
358 })
359
360 bottle.response.content_type = "application/json"
361 return json.dumps(response)
362
363# starting server on http://0.0.0.0:5000
364if __name__ == "__main__":
365 bottle.run(
366 app = app,
367 host = "0.0.0.0",
368 port = 5000,
369 debug = True,
370 reloader = True,
371 catchall = True,
372 )
373```
374
375And now finally we can implement ```frontend.html```. Create file with this name and copy code below. When you are done you can start web application. Steps for this part are listed below the code.
376
377```html
378<!DOCTYPE html>
379<html>
380
381 <head>
382 <meta charset="utf-8">
383 <title>Simple IOT application</title>
384 </head>
385
386 <body>
387
388 <h1>Simple IOT application</h1>
389
390 <div class="chart-placeholder">
391 <div id="chart"></div>
392 </div>
393
394 <!-- application main script -->
395 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
396 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
397 <script src="https://cdnjs.cloudflare.com/ajax/libs/metrics-graphics/2.11.0/metricsgraphics.min.js"></script>
398 <script>
399 function fetch_and_render() {
400 d3.json("/api", function(data) {
401 data = MG.convert.date(data, "date", "%Y-%m-%d %H:%M:%S");
402 MG.data_graphic({
403 data: data,
404 chart_type: "line",
405 full_width: true,
406 height: 270,
407 target: document.getElementById("chart"),
408 x_accessor: "date",
409 y_accessor: "value"
410 });
411 });
412 }
413 window.onload = function() {
414 // initial call for rendering
415 fetch_and_render();
416
417 // updates chart every 5 seconds
418 setInterval(function() {
419 fetch_and_render();
420 }, 5000);
421 }
422 </script>
423
424 <!-- application styles -->
425 <style>
426 body {
427 font: 13px sans-serif;
428 padding: 20px 50px;
429 }
430 .chart-placeholder {
431 border: 2px solid #ccc;
432 width: 100%;
433 user-select: none;
434 }
435 /* chart styles */
436 .mg-line1-color {
437 stroke: red;
438 stroke-width: 2;
439 }
440 .mg-main-area, .mg-main-line {
441 fill: #fff;
442 }
443 .mg-x-axis line, .mg-y-axis line {
444 stroke: #b3b2b2;
445 stroke-width: 1px;
446 }
447 </style>
448
449 </body>
450
451</html>
452```
453
454Now the folder structure should look like:
455
456_simple-iot-app/_
457
458* _webapp.py_
459* _data.db_
460* _frontend.html_
461
462Ok, lets now start application and start feeding it data.
463
4641. ```python webapp.py```
4652. connect Arduino MKR1000 to power source
4663. open browser and go to ```http://0.0.0.0:5000```
467
468If everything goes well you should be seeing new data-points rendered on chart every 5 seconds.
469
470If you navigate to ```http://0.0.0.0:5000``` you should see rendered chart as shown on picture below.
471
472![Application output](/iot-application/iot-app-output.png)
473
474Complete application with all the code is available for [download](/iot-application/simple-iot-application.zip).
475
476## Conclusion
477
478I hope this clarifies some aspects of IOT application development. Of course this is a minimal example and is far from what can be done in real life with some further dive into other technologies.
479
480If you would like to continue exploring IOT world here are some interesting resources for you to examine:
481
482* [Reading Sensors with an Arduino](https://www.allaboutcircuits.com/projects/reading-sensors-with-an-arduino/)
483* [MQTT 101 – How to Get Started with the lightweight IoT Protocol](http://www.hivemq.com/blog/how-to-get-started-with-mqtt)
484* [Stream Updates with Server-Sent Events](https://www.html5rocks.com/en/tutorials/eventsource/basics/)
485* [Internet of Things (IoT) Tutorials](http://www.tutorialspoint.com/internet_of_things/)
486
487Any comment or additional ideas are welcomed in comments below.
diff --git a/content/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md b/content/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md
new file mode 100644
index 0000000..65cc7b5
--- /dev/null
+++ b/content/posts/2018-01-16-using-digitalocean-spaces-object-storage-with-fuse.md
@@ -0,0 +1,261 @@
1---
2title: Using DigitalOcean Spaces Object Storage with FUSE
3url: using-digitalocean-spaces-object-storage-with-fuse.html
4date: 2018-01-16
5draft: false
6---
7
8Couple of months ago [DigitalOcean](https://www.digitalocean.com) introduced new product called [Spaces](https://blog.digitalocean.com/introducing-spaces-object-storage/) which is Object Storage very similar to Amazon's S3. This really peaked my interest, because this was something I was missing and even the thought of going over the internet for such functionality was in no interest to me. Also in fashion with their previous pricing this also is very cheap and pricing page is a no-brainer compared to AWS or GCE. [Prices are clearly and precisely defined and outlined](https://www.digitalocean.com/pricing/). You must love them for that :)
9
10### Initial requirements
11
12* Is it possible to use them as a mounted drive with FUSE? (tl;dr YES)
13* Will the performance degrade over time and over different sizes of objects? (tl;dr NO&YES)
14* Can storage be mounted on multiple machines at the same time and be writable? (tl;dr YES)
15
16> Let me be clear. This scripts I use are made just for benchmarking and are not intended to be used in real-life situations. Besides that, I am looking into using this approaches but adding caching service in front of it and then dumping everything as an object to storage. This could potentially be some interesting post of itself. But in case you would need real-time data without eventual consistency please take this scripts as they are: not usable in such situations.
17
18## Is it possible to use them as a mounted drive with FUSE?
19
20Well, actually they can be used in such manor. Because they are similar to [AWS S3](https://aws.amazon.com/s3/) many tools are available and you can find many articles and [Stackoverflow items](https://stackoverflow.com/search?q=s3+fuse).
21
22To make this work you will need DigitalOcean account. If you don't have one you will not be able to test this code. But if you have an account then you go and [create new Droplet](https://cloud.digitalocean.com/droplets/new?size=s-1vcpu-1gb&region=ams3&distro=debian&distroImage=debian-9-x64&options=private_networking,install_agent). If you click on this link you will already have preselected Debian 9 with smallest VM option.
23
24* Please be sure to add you SSH key, because we will login to this machine remotely.
25* If you change your region please remember which one you choose because we will need this information when we try to mount space to our machine.
26
27Instuctions on how to use SSH keys and how to setup them are available in article [How To Use SSH Keys with DigitalOcean Droplets](https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets).
28
29![DigitalOcean Droplets](/do-fuse/fuse-droplets.png)
30
31After we created Droplet it's time to create new Space. This is done by clicking on a button [Create](https://cloud.digitalocean.com/spaces/new) (right top corner) and selecting Spaces. Choose pronounceable ```Unique name``` because we will use it in examples below. You can either choose Private or Public, it doesn't matter in our case. And you can always change that in the future.
32
33When you have created new Space we should [generate Access key](https://cloud.digitalocean.com/settings/api/tokens). This link will guide to the page when you can generate this key. After you create new one, please save provided Key and Secret because Secret will not be shown again.
34
35![DigitalOcean Spaces](/do-fuse/fuse-spaces.png)
36
37Now that we have new Space and Access key we should SSH into our machine.
38
39```bash
40# replace IP with the ip of your newly created droplet
41ssh root@IP
42
43# this will install utilities for mounting storage objects as FUSE
44apt install s3fs
45
46# we now need to provide credentials (access key we created earlier)
47# replace KEY and SECRET with your own credentials but leave the colon between them
48# we also need to set proper permissions
49echo "KEY:SECRET" > .passwd-s3fs
50chmod 600 .passwd-s3fs
51
52# now we mount space to our machine
53# replace UNIQUE-NAME with the name you choose earlier
54# if you choose different region for your space be careful about -ourl option (ams3)
55s3fs UNIQUE-NAME /mnt/ -ourl=https://ams3.digitaloceanspaces.com -ouse_cache=/tmp
56
57# now we try to create a file
58# once you mount it may take a couple of seconds to retrieve data
59echo "Hello cruel world" > /mnt/hello.txt
60```
61
62After all this you can return to your browser and go to [DigitalOcean Spaces](https://cloud.digitalocean.com/spaces) and click on your created space. If file hello.txt is present you have successfully mounted space to your machine and wrote data to it.
63
64I choose the same region for my Droplet and my Space but you don't have to. You can have different regions. What this actually does to performance I don't know.
65
66Additional information on FUSE:
67
68* [Github project page for s3fs](https://github.com/s3fs-fuse/s3fs-fuse)
69* [FUSE - Filesystem in Userspace](https://en.wikipedia.org/wiki/Filesystem_in_Userspace)
70
71## Will the performance degrade over time and over different sizes of objects?
72
73For this task I didn't want to just read and write text files or uploading images. I actually wanted to figure out if using something like SQlite is viable in this case.
74
75### Measurement experiment 1: File copy
76
77```bash
78# first we create some dummy files at different sizes
79dd if=/dev/zero of=10KB.dat bs=1024 count=10 #10KB
80dd if=/dev/zero of=100KB.dat bs=1024 count=100 #100KB
81dd if=/dev/zero of=1MB.dat bs=1024 count=1024 #1MB
82dd if=/dev/zero of=10MB.dat bs=1024 count=10240 #10MB
83
84# now we set time command to only return real
85TIMEFORMAT=%R
86
87# now lets test it
88(time cp 10KB.dat /mnt/) |& tee -a 10KB.results.txt
89
90# and now we automate
91# this will perform the same operation 100 times
92# this will output results into separated files based on objecty size
93n=0; while (( n++ < 100 )); do (time cp 10KB.dat /mnt/10KB.$n.dat) |& tee -a 10KB.results.txt; done
94n=0; while (( n++ < 100 )); do (time cp 100KB.dat /mnt/100KB.$n.dat) |& tee -a 100KB.results.txt; done
95n=0; while (( n++ < 100 )); do (time cp 1MB.dat /mnt/1MB.$n.dat) |& tee -a 1MB.results.txt; done
96n=0; while (( n++ < 100 )); do (time cp 10MB.dat /mnt/10MB.$n.dat) |& tee -a 10MB.results.txt; done
97```
98
99Files of size 100MB were not successfully transferred and ended up displaying error (cp: failed to close '/mnt/100MB.1.dat': Operation not permitted).
100
101As I suspected, object size is not really that important. Sadly I don't have the time to test performance over periods of time. But if some of you would do it please send me your data. I would be interested in seeing results.
102
103**Here are plotted results**
104
105You can download [raw result here](/do-fuse/copy-benchmarks.tsv). Measurements are in seconds.
106
107<script src="//cdn.plot.ly/plotly-latest.min.js"></script>
108<div id="copy-benchmarks"></div>
109<script>
110(function(){
111 var request = new XMLHttpRequest();
112 request.open("GET", "/do-fuse/copy-benchmarks.tsv", true);
113 request.onload = function() {
114 if (request.status >= 200 && request.status < 400) {
115 var payload = request.responseText.trim();
116 var tsv = payload.split("\n");
117 for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); }
118 var traces = [];
119 var headers = tsv[0];
120 tsv.shift();
121 Array.prototype.forEach.call(headers, function(el, idx) {
122 var x = [];
123 var y = [];
124 for (var j=0; j<tsv.length; j++) {
125 x.push(j);
126 y.push(parseFloat(tsv[j][idx].replace(",", ".")));
127 }
128 traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } });
129 });
130 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 } } });
131 } else { }
132 };
133 request.onerror = function() { };
134 request.send(null);
135})();
136</script>
137
138As far as these tests show, performance is quite stable and can be predicted which is fantastic. But this is a small test and spans only over couple of hours. So you should not completely trust them.
139
140### Measurement experiment 2: SQLite performanse
141
142I was unable to use database file directly from mounted drive so this is a no-go as I suspected. So I executed code below on a local disk just to get some benchmarks. I inserted 1000 records with DROPTABLE, CREATETABLE, INSERTMANY, FETCHALL, COMMIT for 1000 times to generate statistics. As you can see performance of SQLite is quite amazing. You could then potentially just copy file to mounted drive and be done with it.
143
144```python
145import time
146import sqlite3
147import sys
148
149if len(sys.argv) < 3:
150 print("usage: python sqlite-benchmark.py DB_PATH NUM_RECORDS REPEAT")
151 exit()
152
153def data_iter(x):
154 for i in range(x):
155 yield "m" + str(i), "f" + str(i*i)
156
157header_line = "%s\t%s\t%s\t%s\t%s\n" % ("DROPTABLE", "CREATETABLE", "INSERTMANY", "FETCHALL", "COMMIT")
158with open("sqlite-benchmarks.tsv", "w") as fp:
159 fp.write(header_line)
160
161start_time = time.time()
162conn = sqlite3.connect(sys.argv[1])
163c = conn.cursor()
164end_time = time.time()
165result_time = CONNECT = end_time - start_time
166print("CONNECT: %g seconds" % (result_time))
167
168start_time = time.time()
169c.execute("PRAGMA journal_mode=WAL")
170c.execute("PRAGMA temp_store=MEMORY")
171c.execute("PRAGMA synchronous=OFF")
172result_time = PRAGMA = end_time - start_time
173print("PRAGMA: %g seconds" % (result_time))
174
175for i in range(int(sys.argv[3])):
176 print("#%i" % (i))
177
178 start_time = time.time()
179 c.execute("drop table if exists test")
180 end_time = time.time()
181 result_time = DROPTABLE = end_time - start_time
182 print("DROPTABLE: %g seconds" % (result_time))
183
184 start_time = time.time()
185 c.execute("create table if not exists test(a,b)")
186 end_time = time.time()
187 result_time = CREATETABLE = end_time - start_time
188 print("CREATETABLE: %g seconds" % (result_time))
189
190 start_time = time.time()
191 c.executemany("INSERT INTO test VALUES (?, ?)", data_iter(int(sys.argv[2])))
192 end_time = time.time()
193 result_time = INSERTMANY = end_time - start_time
194 print("INSERTMANY: %g seconds" % (result_time))
195
196 start_time = time.time()
197 c.execute("select count(*) from test")
198 res = c.fetchall()
199 end_time = time.time()
200 result_time = FETCHALL = end_time - start_time
201 print("FETCHALL: %g seconds" % (result_time))
202
203 start_time = time.time()
204 conn.commit()
205 end_time = time.time()
206 result_time = COMMIT = end_time - start_time
207 print("COMMIT: %g seconds" % (result_time))
208
209 print
210 log_line = "%f\t%f\t%f\t%f\t%f\n" % (DROPTABLE, CREATETABLE, INSERTMANY, FETCHALL, COMMIT)
211 with open("sqlite-benchmarks.tsv", "a") as fp:
212 fp.write(log_line)
213
214start_time = time.time()
215conn.close()
216end_time = time.time()
217result_time = CLOSE = end_time - start_time
218print("CLOSE: %g seconds" % (result_time))
219```
220
221You can download [raw result here](/do-fuse/sqlite-benchmarks.tsv). And again, these results are done on a local block storage and do not represent capabilities of object storage. With my current approach and state of the test code these can not be done. I would need to make Python code much more robust and check locking etc.
222
223<div id="sqlite-benchmarks"></div>
224<script>
225(function(){
226 var request = new XMLHttpRequest();
227 request.open("GET", "/do-fuse/sqlite-benchmarks.tsv", true);
228 request.onload = function() {
229 if (request.status >= 200 && request.status < 400) {
230 var payload = request.responseText.trim();
231 var tsv = payload.split("\n");
232 for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); }
233 var traces = [];
234 var headers = tsv[0];
235 tsv.shift();
236 Array.prototype.forEach.call(headers, function(el, idx) {
237 var x = [];
238 var y = [];
239 for (var j=0; j<tsv.length; j++) {
240 x.push(j);
241 y.push(parseFloat(tsv[j][idx].replace(",", ".")));
242 }
243 traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } });
244 });
245 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 } } });
246 } else { }
247 };
248 request.onerror = function() { };
249 request.send(null);
250})();
251</script>
252
253## Can storage be mounted on multiple machines at the same time and be writable?
254
255Well, this one didn't take long to test. And the answer is **YES**. I mounted space on both machines and measured same performance on both machines. But because file is downloaded before write and then uploaded on complete there could potentially be problems is another process is trying to access the same file.
256
257## Observations and conslusion
258
259Using Spaces in this way makes it easier to access and manage files. But besides that you would need to write additional code to make this one play nice with you applications.
260
261Nevertheless, this was extremely simple to setup and use and this is just another excellent product in DigitalOcean product line. I found this exercise very valuable and am thinking about implementing some sort of mechanism for SQLite, so data can be stored on Spaces and accessed by many VM's. For a project where data doesn't need to be accessible in real-time and can have couple of minutes old data this would be very interesting. If any of you find this proposal interesting please write in a comment box below or shoot me an email and I will keep you posted.
diff --git a/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md b/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
new file mode 100644
index 0000000..ff346d4
--- /dev/null
+++ b/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
@@ -0,0 +1,346 @@
1---
2title: Encoding binary data into DNA sequence
3url: encoding-binary-data-into-dna-sequence.html
4date: 2019-01-03
5draft: false
6---
7
8## Initial thoughts
9
10Imagine a world where you could go outside and take a leaf from a tree and put it through your personal DNA sequencer and get data like music, videos or computer programs from it. Well, this is all possible now. It was not done on a large scale because it is quite expensive to create DNA strands but it's possible.
11
12Encoding data into DNA sequence is relatively simple process once you understand the relationship between binary data and nucleotides and scientists have been making large leaps in this field in order to provide viable long-term storage solution for our data that would potentially survive our specie if case of global disaster. We could imprint all the world's knowledge into plants and ensure the survival of our knowledge.
13
14More optimistic usage for this technology would be easier storage of ever growing data we produce every day. Once machines for sequencing DNA become fast enough and cheaper this could mean the next evolution of storing data and abandoning classical hard and solid state drives in data warehouses.
15
16As we currently stand this is still not viable but it is quite an amazing and cool technology.
17
18My interests in this field are purely in encoding processes and experimental testing mainly because I don't have the access to this expensive machines. My initial goal was to create a toolkit that can be used by everybody to encode their data into a proper DNA sequence.
19
20## Glossary
21
22**deoxyribose**
23A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group in the 2′ position; the sugar component of DNA nucleotides.
24
25**double helix**
26The molecular shape of DNA in which two strands of nucleotides wind around each other in a spiral shape.
27
28**nitrogenous base**
29A nitrogen-containing molecule that acts as a base; often referring to one of the purine or pyrimidine components of nucleic acids.
30
31**phosphate group**
32A molecular group consisting of a central phosphorus atom bound to four oxygen atoms.
33
34**RGB**
35The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors.
36
37**GCC**
38The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages.
39
40## Data encoding
41
42**TL;DR:** Encoding involves the use of a code to change original data into a form that can be used by an external process.
43
44Encoding is the process of converting data into a format required for a number of information processing needs, including:
45
46- Program compiling and execution
47- Data transmission, storage and compression/decompression
48- Application data processing, such as file conversion
49
50Encoding can have two meanings:
51
52- In computer technology, encoding is the process of applying a specific code, such as letters, symbols and numbers, to data for conversion into an equivalent cipher.
53- In electronics, encoding refers to analog to digital conversion.
54
55## Quick history of DNA
56
57- **1869** - Friedrich Miescher identifies "nuclein".
58- **1900s** - The Eugenics Movement.
59- **1900** – Mendel's theories are rediscovered by researchers.
60- **1944** - Oswald Avery identifies DNA as the 'transforming principle'.
61- **1952** - Rosalind Franklin photographs crystallized DNA fibres.
62- **1953** - James Watson and Francis Crick discover the double helix structure of DNA.
63- **1965** - Marshall Nirenberg is the first person to sequence the bases in each codon.
64- **1983** - Huntington's disease is the first mapped genetic disease.
65- **1990** - The Human Genome Project begins.
66- **1995** - Haemophilus Influenzae is the first bacterium genome sequenced.
67- **1996** - Dolly the sheep is cloned.
68- **1999** - First human chromosome is decoded.
69- **2000** – Genetic code of the fruit fly is decoded.
70- **2002** – Mouse is the first mammal to have its genome decoded.
71- **2003** – The Human Genome Project is completed.
72- **2013** – DNA Worldwide and Eurofins Forensic discover identical twins have differences in their genetic makeup.
73
74## What is DNA?
75
76Deoxyribonucleic acid, a self-replicating material which is **present in nearly all living organisms** as the main constituent of chromosomes. It is the **carrier of genetic information**.
77
78> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood, the carbon in our apple pies were made in the interiors of collapsing stars. We are made of starstuff.
79>
80> **-- Carl Sagan, Cosmos**
81
82The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases (cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate. Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine bases. The sugar and the base together are called a nucleoside.
83
84![DNA](/dna-sequence/dna-basics.jpg)
85
86*DNA (a) forms a double stranded helix, and (b) adenine pairs with thymine and cytosine pairs with guanine. (credit a: modification of work by Jerome Walker, Dennis Myts)*
87
88## Encode binary data into DNA sequence
89
90As an input file you can use any file you want:
91- ASCII files,
92- Compiled programs,
93- Multimedia files (MP3, MP4, MVK, etc),
94- Images,
95- Database files,
96- etc.
97
98Note: If you would copy all the bytes from RAM to file or pipe data to file you could encode also this data as long as you provide file pointer to the encoder.
99
100### Basic Encoding
101
102As already mentioned, the Basic Encoding is based on a simple mapping. Since DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually referred using the first letter). Using this technique we can encode
103
104$$ log_2(4) = log_2(2^2) = 2 bits $$
105
106using a single nucleotide. In this way, we are able to use the 4 bases that compose the DNA strand to encode each byte of data.
107
108| Two bits | Nucleotides |
109| -------- | ---------------- |
110| 00 | **A** (Adenine) |
111| 10 | **G** (Guanine) |
112| 01 | **C** (Cytosine) |
113| 11 | **T** (Thymine) |
114
115With this in mind we can simply encode any data by using two-bit to Nucleotides conversion
116
117```python
118{ Algorithm 1: Naive byte array to DNA encode }
119procedure EncodeToDNASequence(f) string
120begin
121 enc string
122 while not eof(f) do
123 c byte := buffer[0] { Read 1 byte from buffer }
124 bin integer := sprintf('08b', c) { Convert to string binary }
125 for e in range[0, 2, 4, 6] do
126 if e[0] == 48 and e[1] == 48 then { 0x00 - A (Adenine) }
127 enc += 'A'
128 else if e[0] == 48 and e[1] == 49 then { 0x01 - G (Guanine) }
129 enc += 'G'
130 else if e[0] == 49 and e[1] == 48 then { 0x10 - C (Cytosine) }
131 enc += 'C'
132 else if e[0] == 49 and e[1] == 49 then { 0x11 - T (Thymine) }
133 enc += 'T'
134 return enc { Return DNA sequence }
135end
136```
137
138Another encoding would be **Goldman encoding**. Using this encoding helps with Nonsense mutation (amino acids replaced by a stop codon) that occurs and is the most problematic during translation because it leads to truncated amino acid sequences, which in turn results in truncated proteins.
139
140[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU)
141
142### FASTA file format
143
144In bioinformatics, FASTA format is a text-based format for representing either nucleotide sequences or peptide sequences, in which nucleotides or amino acids are represented using single-letter codes. The format also allows for sequence names and comments to precede the sequences. The format originates from the FASTA software package, but has now become a standard in the field of bioinformatics.
145
146The first line in a FASTA file started either with a ">" (greater-than) symbol or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent lines starting with a semicolon would be ignored by software. Since the only comment used was the first, it quickly became used to hold a summary description of the sequence, often starting with a unique library accession number, and with time it has become commonplace to always use ">" for the first line and to not use ";" comments (which would otherwise be ignored).
147
148```
149;LCBO - Prolactin precursor - Bovine
150; a sample sequence in FASTA format
151MDSKGSSQKGSRLLLLLVVSNLLLCQGVVSTPVCPNGPGNCQVSLRDLFDRAVMVSHYIHDLSS
152EMFNEFDKRYAQGKGFITMALNSCHTSSLPTPEDKEQAQQTHHEVLMSLILGLLRSWNDPLYHL
153VTEVRGMKGAPDAILSRAIEIEEENKRLLEGMEMIFGQVIPGAKETEPYPVWSGLPSLQTKDED
154ARYSAFYNLLHCLRRDSSKIDTYLKLLNCRIIYNNNC*
155
156>MCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
157ADQLTEEQIAEFKEAFSLFDKDGDGTITTKELGTVMRSLGQNPTEAELQDMINEVDADGNGTID
158FPEFLTMMARKMKDTDSEEEIREAFRVFDKDGNGYISAAELRHVMTNLGEKLTDEEVDEMIREA
159DIDGDGQVNYEEFVQMMTAK*
160
161>gi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]
162LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV
163EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG
164LLILILLLLLLALLSPDMLGDPDNHMPADPLNTPLHIKPEWYFLFAYAILRSVPNKLGGVLALFLSIVIL
165GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX
166IENY
167```
168
169FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge.
170
171### PNG encoded DNA sequence
172
173| Nucleotides | RGB | Color name |
174| ------------ | ----------- | ---------- |
175| A ➞ Adenine | (0,0,255) | Blue |
176| G ➞ Guanine | (0,100,0) | Green |
177| C ➞ Cytosine | (255,0,0) | Red |
178| T ➞ Thymine | (255,255,0) | Yellow |
179
180With this in mind we can create a simple algorithm to create PNG representation of a DNA sequence.
181
182```python
183{ Algorithm 2: Naive DNA to PNG encode from FASTA file }
184procedure EncodeDNASequenceToPNG(f)
185begin
186 i image
187 while not eof(f) do
188 c char := buffer[0] { Read 1 char from buffer }
189 case c of
190 'A': color := RGB(0, 0, 255) { Blue }
191 'G': color := RGB(0, 100, 0) { Green }
192 'C': color := RGB(255, 0, 0) { Red }
193 'T': color := RGB(255, 255, 0) { Yellow }
194 drawRect(i, [x, y], color)
195 save(i) { Save PNG image }
196end
197```
198
199## Encoding text file in practice
200
201In this example we will take a simple text file as our input stream for encoding. This file will have a quote from Niels Bohr and saved as txt file.
202
203> How wonderful that we have met with a paradox. Now we have some hope of making progress.
204> ― Niels Bohr
205
206First we encode text file into FASTA file.
207
208```bash
209./dnae-encode -i quote.txt -o quote.fa
2102019/01/10 00:38:29 Gathering input file stats
2112019/01/10 00:38:29 Starting encoding ...
212 106 B / 106 B [==================================] 100.00% 0s
2132019/01/10 00:38:29 Saving to FASTA file ...
2142019/01/10 00:38:29 Output FASTA file length is 438 B
2152019/01/10 00:38:29 Process took 987.263µs
2162019/01/10 00:38:29 Done ...
217```
218
219Output of `quote.fa` file contains the encoded DNA sequence in ASCII format.
220
221```
222>SEQ1
223GACAGCTTGTGTACAAGTGTGCTTGCTCGCGAGCGGGTACGCGCGTGGGCTAACAAGTGA
224GCCAGCAGGTGAACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGCTGGCGGGTGA
225ACAAGTGTGCCGGTGAGCCAACAAGCAGACAAGTAAGCAGGTACGCAGGCGAGCTTGTCA
226ACTCACAAGATCGCTTGTGTACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGTAT
227GCTTGCTGGCGGACAAGCCAGCTTGTAAGCGGACAAGCTTGCGCACAAGCTGGCAGGCCT
228GCCGGCTCGCGTACAAATTCACAAGTAAGTACGCTTGCGTGTACGCGGGTATGTATACTC
229AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC
230AACC
231```
232
233Then we encode FASTA file from previous operation to encode this data into PNG.
234
235```bash
236./dnae-png -i quote.fa -o quote.png
2372019/01/10 00:40:09 Gathering input file stats ...
2382019/01/10 00:40:09 Deconstructing FASTA file ...
2392019/01/10 00:40:09 Compositing image file ...
240 424 / 424 [==================================] 100.00% 0s
2412019/01/10 00:40:09 Saving output file ...
2422019/01/10 00:40:09 Output image file length is 1.1 kB
2432019/01/10 00:40:09 Process took 19.036117ms
2442019/01/10 00:40:09 Done ...
245```
246
247After encoding into PNG format this file looks like this.
248
249![Encoded Quote in PNG format](/dna-sequence/quote.png)
250
251The larger the input stream is the larger the PNG file would be.
252
253Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) would [look like](/dna-sequence/sample.png).
254
255```c
256// gcc -O3 -o sample sample.c
257#include <stdio.h>
258
259main() {
260 printf("Hello, world!\n");
261 return 0;
262}
263```
264
265## Toolkit for encoding data
266
267I have created a toolkit with two main programs:
268- dnae-encode (encodes file into FASTA file)
269- dnae-png (encodes FASTA file into PNG)
270
271Toolkit with full source code is available on [github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding).
272
273### dnae-encode
274
275```bash
276> ./dnae-encode --help
277usage: dnae-encode --input=INPUT [<flags>]
278
279A command-line application that encodes file into DNA sequence.
280
281Flags:
282 --help Show context-sensitive help (also try --help-long and --help-man).
283 -i, --input=INPUT Input file (ASCII or binary) which will be encoded into DNA sequence.
284 -o, --output="out.fa" Output file which stores DNA sequence in FASTA format.
285 -s, --sequence=SEQ1 The description line (defline) or header/identifier line, gives a name and/or a unique identifier for the sequence.
286 -c, --columns=60 Row characters length (no more than 120 characters). Devices preallocate fixed line sizes in software.
287 --version Show application version.
288```
289
290### dnae-png
291
292```bash
293> ./dnae-png --help
294usage: dnae-png --input=INPUT [<flags>]
295
296A command-line application that encodes FASTA file into PNG image.
297
298Flags:
299 --help Show context-sensitive help (also try --help-long and --help-man).
300 -i, --input=INPUT Input FASTA file which will be encoded into PNG image.
301 -o, --output="out.png" Output file in PNG format that represents DNA sequence in graphical way.
302 -s, --size=10 Size of pairings of DNA bases on image in pixels (lower resolution lower file size).
303 --version Show application version.
304```
305
306## Benchmarks
307
308First we generate some binary sample data with dd.
309
310```bash
311dd 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
312```
313
314Our freshly generated 1KB file looks something like this (its full of garbage data as intended).
315
316![Sample binary file 1KB](/dna-sequence/sample-binary-file.png)
317
318We create following binary files:
319- 1KB.bin
320- 10KB.bin
321- 100KB.bin
322- 1MB.bin
323- 10MB.bin
324- 100MB.bin
325
326After this we create FASTA files for all the binary files by encoding them into DNA sequence.
327
328```bash
329./dnae-encode -i 100MB.bin -o 100MB.fa
330```
331
332Then we GZIP all the FASTA files to see how much the can be compressed.
333
334```bash
335gzip -9 < 10MB.fa > 10MB.fa.gz
336```
337
338[Download ODS file with benchmarks](/dna-sequence/benchmarks.ods).
339
340## References
341
342- https://www.techopedia.com/definition/948/encoding
343- https://www.dna-worldwide.com/resource/160/history-dna-timeline
344- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/
345- https://arxiv.org/abs/1801.04774
346- https://en.wikipedia.org/wiki/FASTA_format
diff --git a/content/posts/2019-10-14-simplifying-and-reducing-clutter.md b/content/posts/2019-10-14-simplifying-and-reducing-clutter.md
new file mode 100644
index 0000000..deb0cb8
--- /dev/null
+++ b/content/posts/2019-10-14-simplifying-and-reducing-clutter.md
@@ -0,0 +1,22 @@
1---
2title: Simplifying and reducing clutter in my life and work
3url: simplifying-and-reducing-clutter.html
4date: 2019-10-14
5draft: false
6---
7
8I recently moved my main working machine back from Hachintosh to Linux. Well the experiment was interesting and I have done some great work on macOS but it was time to move back.
9
10I actually really missed Linux. The simplicity of `apt-get` or just the amount of software that exists for Linux should be a no-brainer. I spent most of my time on macOS finding solutions to make things work. Using [Brew](https://brew.sh/) was just a horrible experience and far from package managers of Linux. At least they managed to get that `sudo` debacle sorted.
11
12Not all was bad. macOS in general was a perfectly good environment. Things like Docker and tooling like this worked without any hiccups. My normal tools like coding IDE worked flawlessly and the whole look and feel is just superb. I have been using MacBook Air for couple of years so I was used to the system but never as a daily driver.
13
14One of the things I did after I installed Linux back on my machine was cleaning up my Dropbox folder. I have everything on Dropbox. Even projects folder. I write code for living so my whole life revolves around couple of megs of code (with assets). So it's not like I have huge files on my machine. I don't have movies or music or pictures on my PC. All of that stuff is in cloud. I use Google music and I have Netflix account which is more than enough for me.
15
16I also went and deleted some of the repositories on my Github account. I have deleted more code than deployed. People find this strange but for me deleting something feels so cathartic and also forces me to write better code next time around when I am faced with similar problem. That was a huge relief if I am being totally honest.
17
18Next step was to do something with my webpage. I have been using some scripts I wrote a while ago to generate static pages from markdown source posts. I kept on adding and adding stuff on top of it and it became a source of a frustration. And this is just a simple blog and I was using gulp and npm. Anyways after couple of hours of searching and testing static generators I found an interesting one [https://github.com/piranha/gostatic](https://github.com/piranha/gostatic) and I just decided to use this one. It was the only one that had a simple templating engine, not that I really need one. But others had this convoluted way of trying to solve everything and at the end just required quite bigger learning curve I was ready to go with. So I deleted couple of old posts, simplified HTML, trashed most of the CSS and went with [https://motherfuckingwebsite.com/](https://motherfuckingwebsite.com/) aesthetics. Yeah, the previous site was more visually stimulating but all I really care is the content at this point. And Times New Roman font is kind of awesome.
19
20I stopped working on most of the projects in the past couple of months because the overhead was just too insane. There comes a point when you stretch yourself too much and then you stop progressing and with that comes dissatisfaction.
21
22So that's about it. Moving forward minimal style.
diff --git a/content/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md b/content/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
new file mode 100644
index 0000000..0f5d994
--- /dev/null
+++ b/content/posts/2019-10-19-using-sentiment-analysis-for-clickbait-detection.md
@@ -0,0 +1,86 @@
1---
2title: Using sentiment analysis for clickbait detection in RSS feeds
3url: using-sentiment-analysis-for-clickbait-detection-in-rss-feeds.html
4date: 2019-10-19
5draft: false
6---
7
8## Initial thoughts
9
10One of the things that interested me for a while now is if major well established news sites use click bait titles to drive additional traffic to their sites and generate additional impressions.
11
12Goal is to see how article titles and actual content of article differ from each other and see if titles are clickbaited.
13
14## Preparing and cleaning data
15
16For this example I opted to just use RSS feed from a new website and decided to go with [The Guardian](https://www.theguardian.com) World news. While this gets us limited data (~40) articles and also description (actual content) is trimmed this really doesn't reflect the actual article contents.
17
18To get better content I could use web scraping and use RSS as link list and fetch contents directly from website, but for this simple example this will suffice.
19
20There are couple of requirements we need to install before we continue:
21
22- `pip3 install feedparser` (parses RSS feed from url)
23- `pip3 install vaderSentiment` (does sentiment polarity analysis)
24- `pip3 install matplotlib` (plots chart of results)
25
26So first we need to fetch RSS data and sanitize HTML content from description.
27
28```python
29import re
30import feedparser
31
32feed_url = "https://www.theguardian.com/world/rss"
33feed = feedparser.parse(feed_url)
34
35# sanitize html
36for item in feed.entries:
37 item.description = re.sub('<[^<]+?>', '', item.description)
38```
39
40## Perform sentiment analysis
41
42Since we now have cleaned up data in our `feed.entries` object we can start with performing sentiment analysis.
43
44There are many sentiment analysis libraries available that range from rule-based sentiment analysis up to machine learning supported analysis. To keep things simple I decided to use rule-based analysis library [vaderSentiment](https://github.com/cjhutto/vaderSentiment) from [C.J. Hutto](https://github.com/cjhutto). Really nice library and quite easy to use.
45
46```python
47from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
48analyser = SentimentIntensityAnalyzer()
49
50sentiment_results = []
51for item in feed.entries:
52 sentiment_title = analyser.polarity_scores(item.title)
53 sentiment_description = analyser.polarity_scores(item.description)
54 sentiment_results.append([sentiment_title['compound'], sentiment_description['compound']])
55```
56
57Now that we have this data in a shape that is compatible with matplotlib we can plot results to see the difference between title and description sentiment of an article.
58
59```python
60import matplotlib.pyplot as plt
61
62plt.rcParams['figure.figsize'] = (15, 3)
63plt.plot(sentiment_results, drawstyle='steps')
64plt.title('Sentiment analysis relationship between title and description (Guardian World News)')
65plt.legend(['title', 'description'])
66plt.show()
67```
68
69## Results and assets
70
711. Because of the small sample size further conclusions are impossible to make.
722. Rule-based approach may not be the best way of doing this. By using deep learning we would be able to get better insights.
733. **Next step would be to** periodically fetch RSS items and store them over a longer period of time and then perform analysis again and use either machine learning or deep learning on top of it.
74
75![Relationship between title and description](/sentiment-analysis/guardian-sa-title-desc-relationship.png)
76
77Figure above displays difference between title and description sentiment for specific RSS feed item. 1 means positive and -1 means negative sentiment.
78
79[» Download Jupyter Notebook](/sentiment-analysis/sentiment-analysis.ipynb)
80
81## Going further
82
83- [Twitter Sentiment Analysis by Bryan Schwierzke](https://github.com/bswiss/news_mood)
84- [AFINN-based sentiment analysis for Node.js by Andrew Sliwinski](https://github.com/thisandagain/sentiment)
85- [Sentiment Analysis with LSTMs in Tensorflow by Adit Deshpande](https://github.com/adeshpande3/LSTM-Sentiment-Analysis)
86- [Sentiment analysis on tweets using Naive Bayes, SVM, CNN, LSTM, etc. by Abdul Fatir](https://github.com/abdulfatir/twitter-sentiment-analysis)
diff --git a/content/posts/2020-03-22-simple-sse-based-pubsub-server.md b/content/posts/2020-03-22-simple-sse-based-pubsub-server.md
new file mode 100644
index 0000000..166ef8a
--- /dev/null
+++ b/content/posts/2020-03-22-simple-sse-based-pubsub-server.md
@@ -0,0 +1,396 @@
1---
2title: Simple Server-Sent Events based PubSub Server
3url: simple-server-sent-events-based-pubsub-server.html
4date: 2020-03-22
5draft: false
6---
7
8## Before we continue ...
9
10Publisher Subscriber model is nothing new and there are many amazing solutions out there, so writing a new one would be a waste of time if other solutions wouldn't have quite complex install procedures and weren't so hard to maintain. But to be fair, comparing this simple server with something like [Kafka](https://kafka.apache.org/) or [RabbitMQ](https://www.rabbitmq.com/) is laughable at the least. Those solutions are enterprise grade and have many mechanisms there to ensure messages aren't lost and much more. Regardless of these drawbacks, this method has been tested on a large website and worked until now without any problems. So now, that we got that cleared up, let's continue.
11
12***Wiki definition:** Publish/subscribe messaging, or pub/sub messaging, is a form of asynchronous service-to-service communication used in serverless and microservices architectures. In a pub/sub model, any message published to a topic is immediately received by all the subscribers to the topic.*
13
14## General goals
15
16- provide a simple server that relays messages to all the connected clients,
17- messages can be posted on specific topics,
18- messages get sent via [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) to all the subscribers.
19
20## How exactly does the pub/sub model work?
21
22The easiest way to explain this is with diagram bellow. Basic function is simple. We have subscribers that receive messages, and we have publishers that create and post messages. Similar model is also well know pattern that works on a premise of consumers and producers, and they take similar roles.
23
24![How PubSub works](/simple-pubsub-server/pubsub-overview.png)
25
26**These are some naive characteristics we want to achieve:**
27
28- producer is publishing messages to subscribe topic,
29- consumer is receiving messages from subscribed topic,
30- servers is also known as Broker,
31- broker does not store messages or tracks success,
32- broker uses [FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)) method for delivering messages,
33- if consumer wants to receive messages from a topic, producer and consumer topics must match,
34- consumer can subscribe to multiple topics,
35- producer can publish to multiple topics,
36- each message has a messageId.
37
38**Known drawbacks:**
39
40- messages will not be stored in a persistent queue or unreceived messages like [DeadLetterQueue](https://en.wikipedia.org/wiki/Dead_letter_queue) so old messages could be lost on server restart,
41- [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) opens a long-running connection between the client and the server so make sure if your setup is load balanced that the load balancer in this case can have long opened connection,
42- no system moderation due to the dynamic nature of creating queues.
43
44## Server-Sent Events
45
46Read more about it on [official specification page](https://html.spec.whatwg.org/multipage/server-sent-events.html).
47
48### Current browser support
49
50![Browser support](../simple-pubsub-server/caniuse.png)
51
52Check [https://caniuse.com/#feat=eventsource](https://caniuse.com/#feat=eventsource) for latest information about browser support.
53
54### Known issues
55
56- Firefox 52 and below do not support EventSource in web/shared workers
57- In Firefox prior to version 36 server-sent events do not reconnect automatically in case of a connection interrupt (bug)
58- Reportedly, CORS in EventSource is currently supported in Firefox 10+, Opera 12+, Chrome 26+, Safari 7.0+.
59- Antivirus software may block the event streaming data chunks.
60
61Source: [https://caniuse.com/#feat=eventsource](https://caniuse.com/#feat=eventsource)
62
63### Message format
64
65The simplest message that can be sent is only with data attribute:
66
67```bash
68data: this is a simple message
69<blank line>
70```
71
72You can send message IDs to be used if the connection is dropped:
73
74```bash
75id: 33
76data: this is line one
77data: this is line two
78<blank line>
79```
80
81And you can specify your own event types (the above messages will all trigger the message event):
82
83```bash
84id: 36
85event: price
86data: 103.34
87<blank line>
88```
89
90### Server requirements
91
92The important thing is how you send headers and which headers are sent by the server that triggers browser to threat response as a EventStream.
93
94Headers responsible for this are:
95
96```bash
97Content-Type: text/event-stream
98Cache-Control: no-cache
99Connection: keep-alive
100```
101
102### Debugging with Google Chrome
103
104Google Chrome provides build-in debugging and exploration tool for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) which is quite nice and available from Developer Tools under Network tab.
105
106> You can debug only client side events that get received and not the server ones. For debugging server events add `console.log` to `server.js` code and print out events.
107
108![Google Chrome Developer Tools EventStream](../simple-pubsub-server/chrome-debugging.png)
109
110## Server implementation
111
112For the sake of this example we will use [Node.js](https://nodejs.org/en/) with [Express](https://expressjs.com) as our router since this is the easiest way to get started and we will use already written SSE library for node [sse-pubsub](https://www.npmjs.com/package/sse-pubsub) so we don't reinvent the wheel.
113
114```bash
115npm init --yes
116
117npm install express
118npm install body-parser
119npm install sse-pubsub
120```
121
122Basic implementation of a server (`server.js`):
123
124```js
125const express = require('express');
126const bodyParser = require('body-parser');
127const SSETopic = require('sse-pubsub');
128
129const app = express();
130const port = process.env.PORT || 4000;
131
132// topics container
133const sseTopics = {};
134
135app.use(bodyParser.json());
136
137// open for all cors
138app.all('*', (req, res, next) => {
139 res.header('Access-Control-Allow-Origin', '*');
140 res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
141 next();
142});
143
144// preflight request error fix
145app.options('*', async (req, res) => {
146 res.header('Access-Control-Allow-Origin', '*');
147 res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
148 res.send('OK');
149});
150
151// serve the event streams
152app.get('/stream/:topic', async (req, res, next) => {
153 const topic = req.params.topic;
154
155 if (!(topic in sseTopics)) {
156 sseTopics[topic] = new SSETopic({
157 pingInterval: 0,
158 maxStreamDuration: 15000,
159 });
160 }
161
162 // subscribing client to topic
163 sseTopics[topic].subscribe(req, res);
164});
165
166// accepts new messages into topic
167app.post('/publish', async (req, res) => {
168 let body = req.body;
169 let status = 200;
170
171 console.log('Incoming message:', req.body);
172
173 if (
174 body.hasOwnProperty('topic') &&
175 body.hasOwnProperty('event') &&
176 body.hasOwnProperty('message')
177 ) {
178 const topic = req.body.topic;
179 const event = req.body.event;
180 const message = req.body.message;
181
182 if (topic in sseTopics) {
183 // sends message to all the subscribers
184 sseTopics[topic].publish(message, event);
185 }
186 } else {
187 status = 400;
188 }
189
190 res.status(status).send({
191 status,
192 });
193});
194
195// returns JSON object of all opened topics
196app.get('/status', async (req, res) => {
197 res.send(sseTopics);
198});
199
200// health-check endpoint
201app.get('/', async (req, res) => {
202 res.send('OK');
203});
204
205// return a 404 if no routes match
206app.use((req, res, next) => {
207 res.set('Cache-Control', 'private, no-store');
208 res.status(404).end('Not found');
209});
210
211// starts the server
212app.listen(port, () => {
213 console.log(`PubSub server running on http://localhost:${port}`);
214});
215```
216
217### Our custom message format
218
219Each message posted on a server must be in a specific format that out server accepts. Having structure like this allows us to have multiple separated type of events on each topic.
220
221With this we can separate streams and only receive events that belong to the topic.
222
223One example would be, that we have index page and we want to receive messages about new upvotes or new subscribers but we don't want to follow events for other pages. This reduces clutter and overall network. And structure is much nicer and maintanable.
224
225```json
226{
227 "topic": "sample-topic",
228 "event": "sample-event",
229 "message": { "name": "John" }
230}
231```
232
233## Publisher and subscriber clients
234
235### Publisher and subscriber in action
236
237<video src="/simple-pubsub-server/clients.mp4" controls></video>
238
239You can download [the code](../simple-pubsub-server/sse-pubsub-server.zip) and follow along.
240
241### Publisher
242
243As talked about above publisher is the one that send messages to the broker/server. Message inside the payload can be whatever you want (string, object, array). I would however personally avoid send large chunks of data like blobs and such.
244
245```html
246<!DOCTYPE html>
247<html lang="en">
248
249 <head>
250 <meta charset="UTF-8">
251 <meta name="viewport" content="width=device-width, initial-scale=1.0">
252 <title>Publisher</title>
253 </head>
254
255 <body>
256
257 <h1>Publisher</h1>
258
259 <fieldset>
260 <p>
261 <label>Server:</label>
262 <input type="text" id="server" value="http://localhost:4000">
263 </p>
264 <p>
265 <label>Topic:</label>
266 <input type="text" id="topic" value="sample-topic">
267 </p>
268 <p>
269 <label>Event:</label>
270 <input type="text" id="event" value="sample-event">
271 </p>
272 <p>
273 <label>Message:</label>
274 <input type="text" id="message" value='{"name": "John"}'>
275 </p>
276 <p>
277 <button type="button" id="button">Publish message to topic</button>
278 </p>
279 </fieldset>
280
281 <script>
282
283 const button = document.querySelector('#button');
284 const server = document.querySelector('#server');
285 const topic = document.querySelector('#topic');
286 const event = document.querySelector('#event');
287 const message = document.querySelector('#message');
288
289 button.addEventListener('click', async (evt) => {
290 const req = await fetch(`${server.value}/publish`, {
291 method: 'post',
292 headers: {
293 'Accept': 'application/json',
294 'Content-Type': 'application/json',
295 },
296 body: JSON.stringify({
297 topic: topic.value,
298 event: event.value,
299 message: JSON.parse(message.value),
300 }),
301 });
302
303 const res = await req.json();
304 console.log(res);
305 });
306
307 </script>
308
309 </body>
310
311</html>
312
313```
314
315### Subscriber
316
317Subscriber is responsible for receiving new messages that come from server via publisher. The code bellow is very rudimentary but works and follows the implementation guidelines for EventSource.
318
319You can use either Developer Tools Console to see incoming messages or you can defer to Debugging with Google Chrome section above to see all EventStream messages.
320
321> Don't be alarmed if the subscriber gets disconnected from the server every so often. The code we have here resets connection every 15s but it automatically get reconnected and fetches all messages up to last received message id. This setting can be adjusted in `server.js` file; search for the `maxStreamDuration` variable.
322
323```html
324<!DOCTYPE html>
325<html lang="en">
326
327 <head>
328 <meta charset="UTF-8">
329 <meta name="viewport" content="width=device-width, initial-scale=1.0">
330 <title>Subscriber</title>
331 <link rel="stylesheet" href="style.css">
332 </head>
333
334 <body>
335
336 <h1>Subscriber</h1>
337
338 <fieldset>
339 <p>
340 <label>Server:</label>
341 <input type="text" id="server" value="http://localhost:4000">
342 </p>
343 <p>
344 <label>Topic:</label>
345 <input type="text" id="topic" value="sample-topic">
346 </p>
347 <p>
348 <label>Event:</label>
349 <input type="text" id="event" value="sample-event">
350 </p>
351 <p>
352 <button type="button" id="button">Subscribe to topic</button>
353 </p>
354 </fieldset>
355
356 <script>
357
358 const button = document.querySelector('#button');
359 const server = document.querySelector('#server');
360 const topic = document.querySelector('#topic');
361 const event = document.querySelector('#event');
362
363 button.addEventListener('click', async (evt) => {
364
365 let es = new EventSource(`${server.value}/stream/${topic.value}`);
366
367 es.addEventListener(event.value, function (evt) {
368 console.log(`incoming message`, JSON.parse(evt.data));
369 });
370
371 es.addEventListener('open', function (evt) {
372 console.log('connected', evt);
373 });
374
375 es.addEventListener('error', function (evt) {
376 console.log('error', evt);
377 });
378
379 });
380
381 </script>
382
383 </body>
384
385</html>
386
387```
388
389## Reading further
390
391- [Using server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
392- [Using SSE Instead Of WebSockets For Unidirectional Data Flow Over HTTP/2](https://www.smashingmagazine.com/2018/02/sse-websockets-data-flow-http2/)
393- [What is Server-Sent Events?](https://apifriends.com/api-streaming/server-sent-events/)
394- [An HTTP/2 extension for bidirectional messaging communication](https://tools.ietf.org/id/draft-xie-bidirectional-messaging-01.html)
395- [Introduction to HTTP/2](https://developers.google.com/web/fundamentals/performance/http2)
396- [The WebSocket API (WebSockets)](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
diff --git a/content/posts/2020-03-27-create-placeholder-images-with-sharp.md b/content/posts/2020-03-27-create-placeholder-images-with-sharp.md
new file mode 100644
index 0000000..fc8eb0c
--- /dev/null
+++ b/content/posts/2020-03-27-create-placeholder-images-with-sharp.md
@@ -0,0 +1,83 @@
1---
2title: Create placeholder images with sharp Node.js image processing library
3url: create-placeholder-images-with-sharp.html
4date: 2020-03-27
5draft: false
6---
7
8I have been searching for a solution to pre-generate some placeholder images for image server I needed to develop that resizes images on S3. I though this would be a 15min job and quickly found out how very mistaken I was.
9
10Even though Node.js is not really the best way to do this kind of things (surely something written in C or Rust or even Golang would be the correct way to do this but we didn't need the speed in our case) I found an excellent library [sharp - High performance Node.js image processing](https://github.com/lovell/sharp).
11
12Getting things running was a breeze.
13
14## Fetch image from S3 and save resized
15
16```js
17const sharp = require('sharp');
18const aws = require('aws-sdk');
19
20const x,y = 100;
21const s3 = new aws.S3({});
22
23aws.config.update({
24 secretAccessKey: 'secretAccessKey',
25 accessKeyId: 'accessKeyId',
26 region: 'region'
27});
28
29const originalImage = await s3.getObject({
30 Bucket: 'some-bucket-name',
31 Key: 'image.jpg',
32}).promise();
33
34const resizedImage = await sharp(originalImage.Body)
35 .resize(x, y)
36 .jpeg({ progressive: true })
37 .toBuffer();
38
39s3.putObject({
40 Bucket: 'some-bucket-name',
41 Key: `optimized/${x}x${y}/image.jpg`,
42 Body: resizedImage,
43 ContentType: 'image/jpeg',
44 ACL: 'public-read'
45}).promise();
46```
47
48All this code was wrapped inside a web service with some additional security checks and defensive coding to detect if key is missing on S3.
49
50And at that point I needed to return placeholder images as a response in case key is missing or x,y are not allowed by the server etc. I could have created PNG in Gimp and just serve them but I wanted to respect aspect ratio and I didn't want to return some mangled images.
51
52> Main problem with finding a clean solution I could copy and paste and change a bit was a task. API is changing constantly and there weren't clear examples or I was unable to find them.
53
54## Generating placeholder images using SVG
55
56What I ended up was using SVG to generate text and created image with sharp and used composition to combine both layers. Response returned by this function is a buffer you can use to either upload to S3 or save to local file.
57
58```js
59const generatePlaceholderImageWithText = async (width, height, message) => {
60 const overlay = `<svg width="${width - 20}" height="${height - 20}">
61 <text x="50%" y="50%" font-family="sans-serif" font-size="16" text-anchor="middle">${message}</text>
62 </svg>`;
63
64 return await sharp({
65 create: {
66 width: width,
67 height: height,
68 channels: 4,
69 background: { r: 230, g: 230, b: 230, alpha: 1 }
70 }
71 })
72 .composite([{
73 input: Buffer.from(overlay),
74 gravity: 'center',
75 }])
76 .jpeg()
77 .toBuffer();
78}
79```
80
81That is about it. Nothing more to it. You can change the color of the image by changing `background` and if you want to change text styling you can adapt SVG to your needs.
82
83> Also be careful about the length of the text. This function positions text at the center and adds `20px` padding on all sides. If text is longer than the image it will get cut.
diff --git a/content/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md b/content/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md
new file mode 100644
index 0000000..a976a7e
--- /dev/null
+++ b/content/posts/2020-03-29-the-strange-case-of-elasticsearch-allocation-failure.md
@@ -0,0 +1,76 @@
1---
2title: The strange case of Elasticsearch allocation failure
3url: the-strange-case-of-elasticsearch-allocation-failure.html
4date: 2020-03-29
5draft: false
6---
7
8I've been using Elasticsearch in production for 5 years now and never had a single problem with it. Hell, never even known there could be a problem. Just worked. All this time. The first node that I deployed is still being used in production, never updated, upgraded, touched in anyway.
9
10All this bliss came to an abrupt end this Friday when I got notification that Elasticsearch cluster went warm. Well, warm is not that bad right? Wrong! Quickly after that I got another email which sent chills down my spine. Cluster is now red. RED! Now, shit really hit the fan!
11
12I tried googling what could be the problem and after executing allocation function noticed that some shards were unassigned and 5 attempts were already made (which is BTW to my luck the maximum) and that meant I am basically fucked. They also applied that one should wait for cluster to re-balance itself. So, I waited. One hour, two hours, several hours. Nothing, still RED.
13
14The strangest thing about it all was, that queries were still being fulfilled. Data was coming out. On the outside it looked like nothing was wrong but everybody that would look at the cluster would know immediately that something was very very wrong and we were living on borrowed time here.
15
16> **Please, DO NOT do what I did.** Seriously! Please ask someone on official forums or if you know an expert please consult him. There could be million of reasons and these solution fit my problem. Maybe in your case it would disastrous. I had all the data backed up and even if I would fail spectacularly I would be able to restore the data. It would be a huge pain and I would loose couple of days but I had a plan B.
17
18Executing allocation and told me what the problem was but no clear solution yet.
19
20```yaml
21GET /_cat/allocation?format=json
22```
23
24I got a message that `ALLOCATION_FAILED` with additional info `failed to create shard, failure ioexception[failed to obtain in-memory shard lock]`. Well splendid! I must also say that our cluster is capable more than enough to handle the traffic. Also JVM memory pressure never was an issue. So what happened really then?
25
26I tried also re-routing failed ones with no success due to AWS restrictions on having managed Elasticsearch cluster (they lock some of the functions).
27
28```yaml
29POST /_cluster/reroute?retry_failed=true
30```
31
32I got a message that significantly reduced my options.
33
34```json
35{
36 "Message": "Your request: '/_cluster/reroute' is not allowed."
37}
38```
39
40After that I went on a hunt again. I won't bother you with all the details because hours/days went by until I was finally able to re-index the problematic index and hoped for the best. Until that moment even re-indexing was giving me errors.
41
42```yaml
43POST _reindex
44{
45 "source": {
46 "index": "myindex"
47 },
48 "dest": {
49 "index": "myindex-new"
50 }
51}
52```
53
54I needed to do this multiple times to get all the documents re-indexed. Then I dropped the original one with the following command.
55
56```yaml
57DELETE /myindex
58```
59
60And re-indexed again new one in the original one (well by name only).
61
62```yaml
63POST _reindex
64{
65 "source": {
66 "index": "myindex-new"
67 },
68 "dest": {
69 "index": "myindex"
70 }
71}
72```
73
74On the surface it looks like all is working but I have a long road in front of me to get all the things working again. Cluster now shows that it is in Green mode but I am also getting a notification that the cluster has processing status which could mean million of things.
75
76Godspeed!
diff --git a/content/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md b/content/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md
new file mode 100644
index 0000000..2b8b35b
--- /dev/null
+++ b/content/posts/2020-03-30-my-love-and-hate-relationship-with-nodejs.md
@@ -0,0 +1,41 @@
1---
2title: My love and hate relationship with Node.js
3url: my-love-and-hate-relationship-with-nodejs.html
4date: 2020-03-30
5draft: false
6---
7
8Previous project I was working on was being coded in [Golang](https://golang.org/). Also was my first project using it. And damn, that was an awesome experience. The whole thing is just superb. From how errors are handled. The C-like way you handle compiling. The way the language is structured making it incredibly versatile and easy to learn.
9
10It may cause some pain for somebody that is not used of using interfaces to map JSON and doing the recompilation all the time. But we have tools like [entr](http://eradman.com/entrproject/) and [make](https://www.gnu.org/software/make/) to fix that.
11
12But we are not here to talk about my undying love for **Golang**. Only in some way we probably should. It is an excellent example of how modern language should be designed. And because I have used it extensively in the last couple of years this probably taints my views of other languages. And is doing me a great disservice. Nevertheless, here we are.
13
14About two years ago I started flirting with [Node.js](https://nodejs.org/en/) for a project I started working on. What I wanted was to have things written in a language that is widely used, and we could get additional developers for. As much as **Golang** is amazing it's really hard to get developers for it. Even now. And after playing around with it for a week I felt in love with the speed of iteration and massive package ecosystem. Do you want SSO? You got it! Do you want some esoteric library for something? There is a strong chance somebody wrote it. It is so extensive that you find yourself evaluating packages based on **GitHub stars** and number of contributors. You get swallowed by the vanity metrics and that potentially will become the downfall of Node.js.
15
16Because of the sheer amount of choice I often got anxiety when choosing libraries. Will I choose the correct one? Is this library something that will be supported for a foreseeable future or not? I am used of using libraries that are being in development for 10 years plus (Python, C) and that gave me some sort of comfort. And it is probably unfair to Node.js and community to expect same dedication.
17
18Moving forward ... Work started and things were great. **Speed of iteration was insane**. For some feature that I would need a day in Golang only took me hour or two. I became lazy! Using packages all over the place. Falling into the same trap as others. Packages on top of packages. And [npm](https://www.npmjs.com/) didn't help at all. The way that the package manager works is just horrendous. And not allowing to have node_modules outside the project is also the stupidest idea ever.
19
20So at that point I started feeling the technical debt that comes with Node.js and the whole ecosystem. What nobody tells you is that **structuring large Node.js apps** is more problematic than one would think. And going microservice for every single thing is also a bad idea. The amount of networking you introduce with that approach always ends up being a pain in the ass. And I don't even want to go into system administration here. The overhead is insane. Package-lock.json made many days feel like living hell for me. And I would eat the cost of all this if it meant for better development experience. Well, it didn't.
21
22The **lack of Typescript** support in the interpreter is still mind boggling to me. Why haven't they added native support yet for this is beyond me?! That would have solved so many problems. Lack of type safety became a problem somewhere in the middle of the project where the codebase was sufficiently large enough to present problems. We started adding arguments to functions and there was **no way to implicitly define argument types**. And because at that point there were a lot of functions, it became impossible to know what each one accepts, development became more and more trial and error based.
23
24I tried **implementing Typescript**, but that would present a large refactor that we were not willing to do at that point. The benefits were not enough. I also tried [Flow - static type checker](https://flow.org/) but implementation was also horrible. What Typescript and Flow forces you is to have src folder and then **transpile** your code into dist folder and run it with node. WTH is that all about. Why can't this be done in memory or some virtual file system? Why? I see no reason why this couldn't be done like this. But it is what it is. I abandoned all hope for static type checking.
25
26One of the problems that resulted from not having interfaces or types was inability to model out our data from **Elasticsearch**. I could have done a **pedestrian implementation** of it, but there must be a better way of doing this without resorting to some hack basically. Or maybe I haven't found a solution, which is also a possibility. I have looked, though. No juice!
27
28**Error handling?** Is that a joke?
29
30Thank god for **await/async**. Without it, I would have probably just abandoned the whole thing and went with something else like Python. That's all I am going to say about this :)
31
32I started asking myself a question if Node.js is actually ready to be used in a **large scale applications**? And this was a totally wrong question. What I should have been asking myself was, how to use Node.js in large scale application. And you don't get this in **marketing material** for Express or Koa etc. They never tell you this. Making Node.js scale on infrastructure or in codebase is really **more of an art than a science**. And just like with the whole JavaScript ecosystem:
33- impossible to master,
34- half of your time you work on your tooling,
35- just accept transpilers that convert one code into another (holly smokes),
36- error handling is a joke,
37- standards? What standards?
38
39But on the other hand. As I did, you will also learn to love it. Learn to use it quickly and do impossible things in crazy limited time.
40
41I hate to admit it. But I love Node.js. Dammit, I love it :)
diff --git a/content/posts/2020-05-05-remote-work.md b/content/posts/2020-05-05-remote-work.md
new file mode 100755
index 0000000..d4b5cbe
--- /dev/null
+++ b/content/posts/2020-05-05-remote-work.md
@@ -0,0 +1,37 @@
1---
2title: Remote work and how it affects the daily lives of people
3url: remote-work.html
4date: 2020-05-05
5draft: false
6---
7
8I have been working remotely for the past 5 years. I love it. Love the freedom and make your schedule thingy.
9
10## You work more not less
11
12I've heard from people things like: "Oh, you are so lucky, working from home, having all the free time you want". It was obvious they had no clue what means working remotely. They had this romantic idea of remote work. You can watch TV whenever you like, you can go outside for a picnic if you want and stuff like that.
13
14This may be true if you work a day or two in a week from home. But if you go completely remote all these changes completely. I take some time to acclimate but then you start feeling the consequences of going fully remote. And it's not all rainbows and unicorns. Rather the opposite.
15
16## Feeling lost
17
18At first, I remembered I felt lost. I was not used to this kind of environment. It felt disoriented and a part of you that is used to procrastinate turns on. You start thinking of a workday as a whole day. And soon this idea of "I can do this later" starts creeping in. Well, I have the whole day ahead of me. I can do this a bit later.
19
20## Hyper-performance
21
22As a direct result, you become more focused on your work since you don't have all the interruptions common in the workplace. And you can quickly get used to this hyper-performance. But this mode requires also a lot of peace and quiet.
23
24And here we come to the ugly parts of all this. **People rarely have the self-control** to not waste other people's time. It is paralyzing when people start calling you, sending you chat messages, etc. The thing is, that when I achieve this hyper-performance mode I am completely embroiled in the problem I am solving and this kind of interruptions mess with your head. I need an hour at least to get back in the zone. Sometimes not achieving the same focus the whole day.
25
26I know that life is not how you want it to be and takes its route but from what I've learned this kind of interruptions can be avoided in 90% of the case easily just by closing any chat programs and putting your phone in a drawer.
27
28## Suggestion to all the new remote workers
29
30- Stop wasting other people's time. You don't bother people at their desks in the office either.
31- Do not replace daily chats in the hallways with instant messaging software. It will only interrupt people. Nothing good will come of it.
32- Set your working hours and try to not allow it to bleed outside these boundaries and maintain your routine.
33- Be prepared that hours will be longer regardless of your good intentions and your well thought of routine.
34- Try to be hyper-focused and do only one thing at the time. Multitasking is the enemy of progress.
35- Avoid long meetings and if possible eliminate them. Rather take time to write them out and allow others to respond in their own time. Meetings are usually a large waste of time and most of the people attending them are there just because the manager said so.
36- The software will not solve your problems. And throwing money at problems neither.
37- If you are in a managerial position don't supervise any single minute of workers. They are probably giving you more hours anyways. Track progress weekly not daily. You hired them and give them the benefit of the doubt that they will deliver what you agreed upon.
diff --git a/content/posts/2020-08-15-systemd-disable-wake-onmouse.md b/content/posts/2020-08-15-systemd-disable-wake-onmouse.md
new file mode 100644
index 0000000..09e5036
--- /dev/null
+++ b/content/posts/2020-08-15-systemd-disable-wake-onmouse.md
@@ -0,0 +1,47 @@
1---
2title: Disable mouse wake from suspend with systemd service
3url: disable-mouse-wake-from-suspend-with-systemd-service.html
4date: 2020-08-15
5draft: false
6---
7
8I recently bought [ThinkPad X220](https://www.laptopmag.com/reviews/laptops/lenovo-thinkpad-x220) just as a joke on eBay to test Linux distributions and play around with things and not destroy my main machine. Little to my knowledge I felt in love with it. Man, they really made awesome machines back then.
9
10After changing disk that came with it to SSD and installing Ubuntu to test if everything works I noticed that even after a single touch of my external mouse the system would wake up from sleep even though the lid was shut down.
11
12I wouldn't even noticed it if laptop didn't have [LED sleep indicator](https://support.lenovo.com/lk/en/solutions/~/media/Images/ContentImages/p/pd025386_x1_status_03.ashx?w=426&h=262). I already had a bad experience with Linux and it's power management. I had a [Dell Inspiron 7537](https://www.pcmag.com/reviews/dell-inspiron-15-7537) laptop with a touchscreen and while traveling it decided to wake up and started cooking in my backpack to the point that the digitizer responsible for touch actually glue off and the whole screen got wrecked. So, I am a bit touchy about this.
13
14I went on solution hunting and to my surprise there is no easy way to disable specific devices to perform wake up. Why is this not under the power management tab in setting is really strange.
15
16After googling for a solution I found [this nice article describing the solution](https://codetrips.com/2020/03/18/ubuntu-disable-mouse-wake-from-suspend/) that worked for me. The only problem with this solution was that he added his solution to `.bashrc` and this triggers `sudo` that asks for a password each time new terminal is opened, which get annoying quickly since I open a lot of terminals all the time.
17
18I followed his instructions and got to solution `sudo sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup"`.
19
20I created a system service file `sudo nano /etc/systemd/system/disable-mouse-wakeup.service` and removed `sudo` and replaced `sh` with `/usr/bin/sh` and pasted all that in `ExecStart`.
21
22```ini
23[Unit]
24Description=Disables wakeup on mouse event
25After=network.target
26StartLimitIntervalSec=0
27
28[Service]
29Type=simple
30Restart=always
31RestartSec=1
32User=root
33ExecStart=/usr/bin/sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup"
34
35[Install]
36WantedBy=multi-user.target
37```
38
39After that I enabled, started and checked status of service.
40
41```sh
42sudo systemctl enable disable-mouse-wakeup.service
43sudo systemctl start disable-mouse-wakeup.service
44sudo systemctl status disable-mouse-wakeup.service
45```
46
47This will permanently disable that device from wakeing up you computer on boot. If you have many devices you would like to surpress from waking up your machine I would create a shell script and call that instead of direclty doing it in service file.
diff --git a/content/posts/2020-09-06-esp-and-micropython.md b/content/posts/2020-09-06-esp-and-micropython.md
new file mode 100644
index 0000000..4b6e568
--- /dev/null
+++ b/content/posts/2020-09-06-esp-and-micropython.md
@@ -0,0 +1,207 @@
1---
2title: Getting started with MicroPython and ESP8266
3url: esp8266-and-micropython-guide.html
4date: 2020-09-06
5draft: false
6---
7
8**Table of contents**
9
101. [Introduction](#introduction)
112. [Flashing the SOC](#flashing-the-soc)
123. [Install better tooling](#install-better-tooling)
13 1. [ampy](#ampy)
14 2. [rshell](#rshell)
15 1. [Moving files to flash](#moving-files-to-flash)
16 2. [Executing scripts](#executing-scripts)
174. [Additional resources](#additional-resources)
18
19
20## Introduction
21
22A while ago I bought some [ESP8266](https://www.espressif.com/en/products/socs/esp8266) and [ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to play around with and I finally found a project to try it out.
23
24For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32) but I could easily choose [ESP8266](https://www.espressif.com/en/products/socs/esp8266). This guide contains which tools I use and how I prepared my workspace to code for [ESP8266](https://www.espressif.com/en/products/socs/esp8266).
25
26![ESP8266 and ESP32 boards](/esp8366-micropython/boards.jpg)
27
28This guide covers:
29- flashing SOC
30- install proper tooling
31- deploying a simple script
32
33> Make sure that you are using **a good USB cable**. I had some problems with mine and once I replaced it everything started to work.
34
35## Flashing the SOC
36
37Plug your ESP8266 to USB port and check if the device was recognized with executing `dmesg | grep ch341-uart`.
38
39Then check if the device is available under `/dev/` by running `ls /dev/ttyUSB*`.
40
41> **Linux users**: if a device is not available be sure you are in `dialout` group. You can check this by executing `groups $USER`. You can add a user to `dialout` group with `sudo adduser $USER dialout`.
42
43After these conditions are meet go to the navigate to [https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/) and download `esp8266-20200902-v1.13.bin`.
44
45```sh
46mkdir esp8266-test
47cd esp8266-test
48
49wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin
50```
51
52After obtaining firmware we will need some tooling to flash the firmware to the board.
53
54```sh
55sudo pip3 install esptool
56```
57
58You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/).
59
60Before flashing the firmware we need to erase the flash on device. Substitute `USB0` with the device listed in output of `ls /dev/ttyUSB*`.
61
62```sh
63esptool.py --port /dev/ttyUSB0 erase_flash
64```
65
66If flash was successfully erased it is now time to flash the new firmware to it.
67
68```sh
69esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin
70```
71
72If everything went ok you can try accessing MicroPython REPL with `screen /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`.
73
74> Sometimes you will need to press `ENTER` in `screen` or `picocom` to access REPL.
75
76When you are in REPL you can test if all is working properly following steps.
77
78```py
79> import machine
80> machine.freq()
81```
82
83This should output a number representing a frequency of the CPU (mine was `80000000`).
84
85When you are in `screen` or `picocom` these can help you a bit.
86
87| Key | Command |
88| -------- | -------------------- |
89| CTRL+d | preforms soft reboot |
90| CTRL+a x | exits picocom |
91| CTRL+a \ | exits screen |
92
93
94## Install better tooling
95
96Now, to make our lives a little bit easier there are couple of additional tools that will make this whole experience a little more bearable.
97
98There are twq cool ways of uploading local files to SOC flash.
99
100- ampy → [https://github.com/scientifichackers/ampy](https://github.com/scientifichackers/ampy)
101- rshell → [https://github.com/dhylands/rshell](https://github.com/dhylands/rshell)
102
103### ampy
104
105```bash
106# installing ampy
107sudo pip3 install adafruit-ampy
108```
109
110Listed below are some common commands I used.
111
112```bash
113
114# uploads file to flash
115ampy --delay 2 --port /dev/ttyUSB0 put boot.py
116
117# lists file on flash
118ampy --delay 2 --port /dev/ttyUSB0 ls
119
120# outputs contents of file on flash
121ampy --delay 2 --port /dev/ttyUSB0 cat boot.py
122```
123
124> I added `delay` of 2 seconds because I had problems with executing commands.
125
126### rshell
127
128Even though `ampy` is a cool tool I opted with `rshell` in the end since it's much more polished and feature rich.
129
130```bash
131# installing ampy
132sudo pip3 install rshell
133```
134
135Now that `rshell` is installed we can connect to the board.
136
137```bash
138rshell --buffer-size=30 -p /dev/ttyUSB0 -a
139```
140
141This will open a shell inside bash and from here you can execute multiple commands. You can check what is supported with `help` once you are inside of a shell.
142
143```bash
144m@turing ~/Junk/esp8266-test
145$ rshell --buffer-size=30 -p /dev/ttyUSB0 -a
146
147Using buffer-size of 30
148Connecting to /dev/ttyUSB0 (buffer-size 30)...
149Trying to connect to REPL connected
150Testing if ubinascii.unhexlify exists ... Y
151Retrieving root directories ... /boot.py/
152Setting time ... Sep 06, 2020 23:54:28
153Evaluating board_name ... pyboard
154Retrieving time epoch ... Jan 01, 2000
155Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
156/home/m/Junk/esp8266-test> help
157
158Documented commands (type help <topic>):
159========================================
160args cat connect date edit filesize help mkdir rm shell
161boards cd cp echo exit filetype ls repl rsync
162
163Use Control-D (or the exit command) to exit rshell.
164```
165
166> Inside a shell `ls` will display list of files on your machine. To get list of files on flash folder `/pyboard` is remapped inside the shell. To list files on flash you must perform `ls /pyboard`.
167
168#### Moving files to flash
169
170To avoid copying files all the time I used `rsync` function from the inside of `rshell`.
171
172```bash
173rsync . /pyboard
174```
175
176#### Executing scripts
177
178It is a pain to continuously reboot the device to trigger `/pyboard/boot.py` and there is a better way of testing local scripts on remote device.
179
180Lets assume we have `src/freq.py` file that displays CPU frequency of a remote device.
181
182```py
183# src/freq.py
184
185import machine
186print(machine.freq())
187```
188
189Now lets upload this and execute it.
190
191```bash
192# syncs files to remove device
193rsync ./src /pyboard
194
195# goes into REPL
196repl
197
198# we import file by importing it without .py extension and this will run the script
199> import freq
200
201# CTRL+x will exit REPL
202```
203
204## Additional resources
205
206- [https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/](https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/)
207- [http://docs.micropython.org/en/latest/esp8266/quickref.html](http://docs.micropython.org/en/latest/esp8266/quickref.html)
diff --git a/content/posts/2020-09-08-bind-warning-on-login.md b/content/posts/2020-09-08-bind-warning-on-login.md
new file mode 100644
index 0000000..3f276c6
--- /dev/null
+++ b/content/posts/2020-09-08-bind-warning-on-login.md
@@ -0,0 +1,40 @@
1---
2title: Fix bind warning in .profile on login in Ubuntu
3url: bind-warning-on-login-in-ubuntu.html
4date: 2020-09-08
5draft: false
6---
7
8Recently I moved back to [bash](https://www.gnu.org/software/bash/) as my default shell. I was previously using [fish](https://fishshell.com/) and got used to the cool features it has. But, regardless of that, I wanted to move to a more standard shell because I was hopping back and forth with exporting variables and stuff like that which got pretty annoying.
9
10So I embarked on a mission to make [bash](https://www.gnu.org/software/bash/) more like [fish](https://fishshell.com/) and in the process found that I really missed autosuggest with TAB on changing directories.
11
12I found a nice alternative that emulates [zsh](http://zsh.sourceforge.net/) like autosuggestion and autocomplete so I added the following to my `.bashrc` file.
13
14```bash
15bind "TAB:menu-complete"
16bind "set show-all-if-ambiguous on"
17bind "set completion-ignore-case on"
18bind "set menu-complete-display-prefix on"
19bind '"\e[Z":menu-complete-backward'
20```
21
22I haven't noticed anything wrong with this and all was working fine until I restarted my machine and then I got this error.
23
24![Profile bind error](/profile-bind-error/error.jpg)
25
26When I pressed OK, I got into the [Gnome shell](https://wiki.gnome.org/Projects/GnomeShell) and all was working fine, but the error was still bugging me. I started looking for the reason why this is happening and found a solution to this error on [Remote SSH Commands - bash bind warning: line editing not enabled](https://superuser.com/a/892682).
27
28So I added a simple `if [ -t 1 ]` around `bind` statements to avoid running commands that presume the session is interactive when it isn't.
29
30```bash
31if [ -t 1 ]; then
32 bind "TAB:menu-complete"
33 bind "set show-all-if-ambiguous on"
34 bind "set completion-ignore-case on"
35 bind "set menu-complete-display-prefix on"
36 bind '"\e[Z":menu-complete-backward'
37fi
38```
39
40After logging out and back in the problem was gone.
diff --git a/content/posts/2020-09-09-digitalocean-sync.md b/content/posts/2020-09-09-digitalocean-sync.md
new file mode 100644
index 0000000..e7da657
--- /dev/null
+++ b/content/posts/2020-09-09-digitalocean-sync.md
@@ -0,0 +1,64 @@
1---
2title: Using Digitalocean Spaces to sync between computers
3url: digitalocean-spaces-to-sync-between-computers.html
4date: 2020-09-09
5draft: false
6---
7
8I've been using [Dropbox](https://www.dropbox.com/) for probably **10+ years** now and I-ve became so used to it that it runs in the background that I don't even imagine a world without it. But it's not without problems.
9
10At first I had problems with `.venv` environments for Python and the only solution for excluding synchronization for this folder was to manually exclude a specific folder which is not really scalable. FYI, my whole project folder is synced on [Dropbox](https://www.dropbox.com/). This of course introduced a lot of syncing of files and folders that are not needed or even break things on other machines. In the case of **Python**, I couldn't use that on my second machine. I needed to delete `.venv` folder and pip it again which synced files again to the main machine. This was very frustrating. **Nodejs** handles this much nicer and I can just run the scripts without deleting `node_modules` again and reinstalling. However, `node_modules` is a beast of its own. It creates so many files that OS has a problem counting them when you check the folder contents for size.
11
12I wanted something similar to Dropbox. I could without the instant syncing but it would need to be fast and had the option for me to exclude folders like `node_modules, .venv, .git` and folders like that.
13
14I went on a hunt for an alternative to [Dropbox](https://www.dropbox.com/) and found:
15
16- [Tresorit](https://tresorit.com/)
17- [Sync.com](https://sync.com)
18- [Box](https://www.box.com/)
19
20You know, the usual list of suspects. I didn't include [Google drive](https://drive.google.com) or [One drive](https://onedrive.live.com/) since they are even more draconian than Dropbox.
21
22> All this does not stem from me being paranoid but recently these companies have became more and more aggressive and they keep violating our privacy when they share our data with 3rd party services. It is getting out of control.
23
24So, my main problem was still there. No way of excluding a specific folder from syncing. And before we go into "*But you have git, isn't that enough?*", I must say, that many of the files (PDFs, spreadsheets, etc) I have in a `git` repo don't get pushed upstream to Git and I still want to have them synced across my computers.
25
26I initially wanted to use [rsync](https://linux.die.net/man/1/rsync) but I would need to then have a remote VPS or transfer between my computers directly. I wanted a solution where all my files could be accessible to me without my machine.
27
28> **WARNING: This solution will cost you money!** DigitalOcean Spaces are $5 per month and there are some bandwidth limitations and if you go beyond that you get billed additionally.
29
30Then I remembered that I could use something like [S3](https://en.wikipedia.org/wiki/Amazon_S3) since it has versioning and is fully managed. I didn't want to go down the AWS rabbit hole with this so I choose [DigitalOcean Spaces](https://www.digitalocean.com/products/spaces/).
31
32Then I needed a command-line tool to sync between source and target. I found this nice tool [s3cmd](https://s3tools.org/s3cmd) and it is in the Ubuntu repositories.
33
34```bash
35sudo apt install s3cmd
36```
37
38After installation will I create a new Space bucket on DigitalOcean. Remember the zone you will choose because you will need it when you will configure `s3cmd`.
39
40Then I visited [Digitalocean Applications & API](https://cloud.digitalocean.com/account/api/tokens) and generated **Spaces access keys**. Save both key and secret somewhere safe because when you will leave the page secret will not be available anymore to you and you will need to re-generate it.
41
42```bash
43# enter your key and secret and correct endpoint
44# my endpoint is ams3.digitaloceanspaces.com because
45# I created my bucket in Amsterdam regiin
46s3cmd --configure
47```
48After that I played around with options for `s3cmd` and got to the following command.
49
50```bash
51# I executed this command from my projects folder
52cd projects
53s3cmd sync --delete-removed --exclude 'node_modules/*' --exclude '.git/*' --exclude '.venv/*' ./ s3://my-bucket-name/projects/
54```
55
56When syncing int he other direction you will need to change the order of the `SOURCE` and `TARGET` to `s3://my-bucket-name/projects/` and `./`.
57
58> Be sure that all the paths have trailing slash so that sync knows that this are directories.
59
60I am planning to implement some sort of a `.ignore` file that will enable me to have a project-specific exclude options.
61
62I am currently running this every hour as a cronjob which is perfectly fine for now when I am testing how this whole thing works and how it all will turn out.
63
64I have also created a small Gnome extension which is still very unstable, but when/if this whole experiment pays of I will share on Github.
diff --git a/content/posts/2021-01-24-replacing-dropbox-with-s3.md b/content/posts/2021-01-24-replacing-dropbox-with-s3.md
new file mode 100644
index 0000000..12fb8cf
--- /dev/null
+++ b/content/posts/2021-01-24-replacing-dropbox-with-s3.md
@@ -0,0 +1,88 @@
1---
2title: Replacing Dropbox in favor of DigitalOcean spaces
3url: replacing-dropbox-in-favor-of-digitalocean-spaces.html
4date: 2021-01-24
5draft: false
6---
7
8A few months ago I experimented with DigitalOcean spaces as my backup solution that could [replace Dropbox eventually](/digitalocean-spaces-to-sync-between-computers.html). That solution worked quite nicely, and I was amazed how smashing together a couple of existing solutions would work this fine.
9
10I have been running that solution in the background for a couple of months now and kind of forgot about it. But recent developments around deplatforming and having us people hostages of technology and big companies speed up my goals to become less dependent on [Google](https://edition.cnn.com/2020/12/17/tech/google-antitrust-lawsuit/index.html), [Dropbox](https://www.pcworld.com/article/2048680/dropbox-takes-a-peek-at-files.html) etc and take back some control.
11
12I am not a conspiracy theory nut, but to be honest, what these companies are doing lately is out of control. It is a matter of principle at this point. I have almost completely degoogled my life all the way from ditching Gmail, YouTube and most of the services surrounding Google. And I must tell you, I feel so good. I haven't felt this way for a long time.
13
14**Anyways. Let's get to the meat of things.**
15
16Before you continue you should read my post about [syncing to Dropbox](/digitalocean-spaces-to-sync-between-computers.html).
17
18> Also to note, I am using Linux on my machine with Gnome desktop environment. This should work on MacOS too. To use this on Windows I suggest using [Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) or [Cygwin](https://www.cygwin.com/).
19
20## Folder structure
21
22I liked structure from Dropbox. One folder where everything is located and synced. So, that's why adopted this also for my sync setup.
23
24```go
25~/Vault
26 ↳ backup
27 ↳ bin
28 ↳ documents
29 ↳ projects
30```
31
32All of my code is located in `~/Vault/projects` folder. And most of the projects are Git repositories. I do not use this sync method for backup per see but in case I reinstall my machine I can easily recreate all the important folder structure with one quick command. No external drives needed that can fail etc.
33
34## Sync script
35
36My sync script is located in `~/Vault/bin/vault-backup.sh`
37
38```bash
39#!/bin/bash
40
41# dconf load /com/gexperts/Tilix/ < tilix.dconf
42# 0 2 * * * sh ~/Vault/bin/vault-backup.sh
43
44cd ~/Vault/backup/dotfiles
45
46MACHINE=$(whoami)@$(hostname)
47mkdir -p $MACHINE
48cd $MACHINE
49
50cp ~/.config/VSCodium/User/settings.json settings.json
51cp ~/.s3cfg s3cfg
52cp ~/.bash_extended bash_extended
53cp ~/.ssh ssh -rf
54
55codium --list-extensions > vscode-extension.txt
56dconf dump /com/gexperts/Tilix/ > tilix.dconf
57
58cd ~/Vault
59s3cmd sync --delete-removed --exclude 'node_modules/*' --exclude '.git/*' --exclude '.venv/*' ./ s3://bucket-name/backup/
60
61echo `date +"%D %T"` >> ~/.vault.log
62
63notify-send \
64 -u normal \
65 -i /usr/share/icons/Adwaita/96x96/status/security-medium-symbolic.symbolic.png \
66 "Vault sync succeded at `date +"%D %T"`"
67```
68
69This script also backups some of the dotfiles I use and sends notification to Gnome notification center. It is a straightforward solution. Nothing special going on.
70
71> One obvious benefit of this is that I can omit syncing Node's `node_modules` or Python's `.venv` and `.git` folders.
72
73You can use this script in a combination with [Cron](https://en.wikipedia.org/wiki/Cron).
74
75```
760 2 * * * sh ~/Vault/bin/vault-backup.sh
77```
78
79When you start syncing your local stuff with a remote server you can review your items on DigitalOcean.
80
81![Dropbox Spaces](/dropbox-sync/dropbox-spaces.png)
82
83I have been using this script now for quite some time, and it's working flawlessly. I also uninstalled Dropbox and stopped using it completely.
84
85All I need to do is write a Bash script that does the reverse and downloads from remote server to local folder. This could be another post.
86
87
88
diff --git a/content/posts/2021-01-25-goaccess.md b/content/posts/2021-01-25-goaccess.md
new file mode 100644
index 0000000..2f3d56b
--- /dev/null
+++ b/content/posts/2021-01-25-goaccess.md
@@ -0,0 +1,164 @@
1---
2title: Using GoAccess with Nginx to replace Google Analytics
3url: using-goaccess-with-nginx-to-replace-google-analytics.html
4date: 2021-01-25
5draft: false
6---
7
8**Table of contents**
9
101. [Opting for log parsing](#opting-for-log-parsing)
112. [Getting Nginx ready](#getting-nginx-ready)
123. [Getting GoAccess ready](#getting-goaccess-ready)
134. [Securing with Basic authentication](#securing-with-basic-authentication)
14
15I know! You cannot simply replace Google Analytics with parsing access logs and displaying a couple of charts. But to be honest, I actually never used Google Analytics to the fullest extent and was usually interested in seeing page hits and which pages were visited most often.
16
17I recently moved my blog from Firebase to a VPS and also decided to remove Google Analytics tracking code from the site since its quite malicious and tracks users across other pages also and is creating a profile of a user, and I've had it. But I also need some insight of what is happening on a server and which content is being read the most etc.
18
19I have looked at many existing solutions like:
20- [Umami](https://umami.is/)
21- [Freshlytics](https://github.com/sheshbabu/freshlytics)
22- [Matomo](https://matomo.org/)
23
24But the more I looked at them the more I noticed that I am replacing one evil with another one. Don't get me wrong. Some of these solutions are absolutely fantastic but would require installation of databases and something like PHP or Node. And I was not ready to put those things on my fresh server. Also having Docker installed is out of the question.
25
26## Opting for log parsing
27
28So, I defaulted to parsing already existing logs and generating HTML reports from this data.
29
30I found this amazing software [GoAccess](https://goaccess.io/) which provides all the functionalities I need, and it's a single binary. Written in Go.
31
32GoAccess can be used in two different modes.
33
34![GoAccess Terminal](/goaccess/goaccess-dash-term.png)
35<center><i>Running in a terminal</i></center>
36
37![GoAccess HTML](/goaccess/goaccess-dash-html.png)
38<center><i>Running in a browser</i></center>
39
40I, however, need this to run in a browser. So, the second option is the way to go. The Idea is to periodically run cronjob and export this report into a folder that gets then server by Nginx behind a Basic authentication.
41
42## Getting Nginx ready
43
44I choose Ubuntu on [DigitalOcean](https://www.digitalocean.com/). First I installed [Nginx](https://nginx.org/en/), and [Letsencrypt](https://letsencrypt.org/getting-started/) certbot and all the necessary dependencies.
45
46```sh
47# log in as root user
48sudo su -
49
50# first let's update the system
51apt update && apt upgrade -y
52
53# let's install
54apt install nginx certbot python3-certbot-nginx apache2-utils
55```
56
57After all this is installed we can create a new configuration for a statistics. Stats will be available at `stats.domain.com`.
58
59```sh
60# creates directory where html will be hosted
61mkdir -p /var/www/html/stats.domain.com
62
63cp /etc/nginx/sites-available/default /etc/nginx/sites-available/stats.domain.com
64nano /etc/nginx/sites-available/stats.domain.com
65```
66
67```nginx
68server {
69 root /var/www/html/stats.domain.com;
70 server_name stats.domain.com;
71
72 index index.html;
73 location / {
74 try_files $uri $uri/ =404;
75 }
76}
77```
78
79Now we check if the configuration is ok. We can do this with `nginx -t`. If all is ok, we can restart Nginx with `service nginx restart`.
80
81After all that you should add A record for this domain that points to IP of a droplet.
82
83Before enabling SSL you should test if DNS records have propagated with `curl stats.domain.com`.
84
85Now, it's time to provision TLS certificate. To achieve this, you execute command `certbot --nginx`. Follow the wizard and when you are asked about redirection always choose 2 (always redirect to HTTPS).
86
87When this is done you can visit https://stats.domain.com and you should get 404 not found error which is correct.
88
89
90## Getting GoAccess ready
91
92If you are using Debian like system GoAccess should be available in repository. Otherwise refer to the official website.
93
94```sh
95apt install goaccess
96```
97
98To enable Geo location we also need one additiona thing.
99
100```sh
101cd /var/www/html/stats.stats.com
102wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb
103```
104
105Now we create a shell script that will be executed every 10 minutes.
106
107```sh
108nano /var/www/html/stats.domain.com/generate-stats.sh
109```
110
111Contents of this file should look like this.
112
113```sh
114#!/bin/sh
115
116zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
117
118goaccess \
119 --log-file=/var/log/nginx/access-all.log \
120 --log-format=COMBINED \
121 --exclude-ip=0.0.0.0 \
122 --geoip-database=/var/www/html/stats.domain.com/GeoLite2-City.mmdb \
123 --ignore-crawlers \
124 --real-os \
125 --output=/var/www/html/stats.domain.com/index.html
126
127rm /var/log/nginx/access-all.log
128```
129
130Because after a while nginx creates multiple files with access logs we use [`zcat`](https://linux.die.net/man/1/zcat) to extract Gziped contents and create a file that has all the access logs. After this file is used we delete it.
131
132If you want to exclude your home IP's result look at the `--exclude-ip` option in script and instead of `0.0.0.0` add your own home IP address. You can find your home IP by executing `curl ifconfig.me` from your local machine and NOT from the droplet.
133
134Test the script by executing `sh /var/www/html/stats.domain.com/generate-stats.sh` and then checking `https://stats.domain.com`. If you can see stats instead of 404 than you are set.
135
136It's time to add this script to cron with `cron -e`.
137
138```go
139*/10 * * * * sh /var/www/html/stats.domain.com/generate-stats.sh
140```
141
142## Securing with Basic authentication
143
144You probably don't want stats to be publicly available, so we should create a user and a password for Basic authentication.
145
146First we create a password for a user `stats` with `htpasswd -c /etc/nginx/.htpasswd stats`.
147
148Now we update config file with `nano /etc/nginx/sites-available/stats.domain.com`. You probably noticed that the file looks a bit different from before. This is because `certbot` added additional rules for SSL.
149
150Your location portion the config file should now look like. You should add `auth_basic` and `auth_basic_user_file` lines to the file.
151
152```nginx
153location / {
154 try_files $uri $uri/ =404;
155 auth_basic "Private Property";
156 auth_basic_user_file /etc/nginx/.htpasswd;
157}
158```
159
160Test if config is still ok with `nginx -t` and if it is you can restart Nginx with `service nginx restart`.
161
162If you now visit `https://stats.domain.com` you should be prompted for username and password. If not, try reopening your browser.
163
164That is all. You now have analytics for your server that gets refreshed every 10 minutes.
diff --git a/content/posts/2021-06-26-simple-world-clock.md b/content/posts/2021-06-26-simple-world-clock.md
new file mode 100644
index 0000000..e06d534
--- /dev/null
+++ b/content/posts/2021-06-26-simple-world-clock.md
@@ -0,0 +1,87 @@
1---
2title: Simple world clock with eInk display and Raspberry Pi Zero
3url: simple-world-clock-with-eiink-display-and-raspberry-pi-zero.html
4date: 2021-06-26
5draft: false
6---
7
8Our team is spread across the world, from the USA all the way to Australia, so having some sort of world clock makes sense.
9
10Currently, I am using an extension for Gnome called [Timezone extension](https://extensions.gnome.org/extension/2657/timezones-extension/), and it serves the purpose quite well.
11
12But I also have a bunch of electronics that I bought through the time, and I am not using any of them, and it's time to stop hording this stuff and use it in a project.
13
14A while ago I bought a small eInk display [Inky pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) and I have a bunch of [Raspberry Pi's Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/) lying around that I really need to use.
15
16![Inky pHAT, Raspberry Pi Zero](/world-clock/hardware.jpg)
17
18Since the Inky [Inky pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) is essentially a HAT, it can easily be added on top of the [Raspberry Pi Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/).
19
20First, I installed the necessary software on Raspberry Pi with `pip3 install inky`.
21
22And then I created a file `clock.py` in home directory `/home/pi`.
23
24```python
25#!/usr/bin/env python
26# -*- coding: utf-8 -*-
27
28import sys
29import os
30from inky.auto import auto
31from PIL import Image, ImageFont, ImageDraw
32from font_fredoka_one import FredokaOne
33
34clocks = [
35 'America/New_York',
36 'Europe/Ljubljana',
37 'Australia/Brisbane',
38]
39
40board = auto()
41board.set_border(board.WHITE)
42board.rotation = 90
43
44img = Image.new('P', (board.WIDTH, board.HEIGHT))
45draw = ImageDraw.Draw(img)
46
47big_font = ImageFont.truetype(FredokaOne, 18)
48small_font = ImageFont.truetype(FredokaOne, 13)
49
50x = board.WIDTH / 3
51y = board.HEIGHT / 3
52
53idx = 1
54for clock in clocks:
55 ctime = os.popen('TZ="{}" date +"%a,%H:%M"'.format(clock))
56 ctime = ctime.read().strip().split(',')
57 city = clock.split('/')[1].replace('_', ' ')
58
59 draw.text((15, (idx*y)-y+10), city, fill=board.BLACK, font=small_font)
60 draw.text((110, (idx*y)-y+7), str(ctime[0]), fill=board.BLACK, font=big_font)
61 draw.text((155, (idx*y)-y+7), str(ctime[1]), fill=board.BLACK, font=big_font)
62
63 idx += 1
64
65board.set_image(img)
66board.show()
67```
68
69And because eInk displays are rather slow to refresh and the clock requires refreshing only once a minute, this can be done through cronjob.
70
71Before we add this job to cron we need to make `clock.py` executable with `chmod +x clock.py`.
72
73Then we add a cronjob with `crontab -e`.
74
75```
76* * * * * /home/pi/clock.py
77```
78
79So, we end up with a result like this.
80
81![World Clock](/world-clock/world-clock.jpg)
82
83And for the enclosure that can be 3D printed, but I haven't yet something like this can be used.
84
85<iframe id="vs_iframe" src="https://www.viewstl.com/?embedded&url=https%3A%2F%2Fmitjafelicijan.com%2Fassets%2Fworld-clock%2Fenclosure.stl&color=gray&bgcolor=white&edges=no&orientation=front&noborder=no" style="border:0;margin:0;width:100%;height:400px;"></iframe>
86
87You can download my [STL file for the enclosure here](/world-clock/enclosure.stl), but make sure that dimensions make sense and also opening for USB port should be added or just use a drill and some hot glue to make it stick in the enclosure.
diff --git a/content/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md b/content/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md
new file mode 100644
index 0000000..85b6c91
--- /dev/null
+++ b/content/posts/2021-07-30-from-internet-consumer-to-full-hominum-again.md
@@ -0,0 +1,34 @@
1---
2title: My journey from being an internet über consumer to being a full hominum again
3url: from-internet-consumer-to-full-hominum-again.html
4date: 2021-07-30
5draft: false
6---
7
8It's been almost a year since I started purging all my online accounts and going down this rabbit hole of being almost independent of the current internet machine. Even though I initially thought that I will have problems adapting, I was pleasantly surprised that the transition went so smoothly. Even better, it brought many benefits to my life. Such as increased focus, less stress about trivial things, etc.
9
10It all started with me doing small changes like unsubscribing from emails that I have either subscribed to by accepting terms and conditions. Or even some more malicious emails that I was getting because I was on a shared mailing list. And the later ones I hate the most of all. How the hell do they keep sharing my email and sending me unsolicited emails and get away with it? I have a suspicion that these marketing people share an Excel file between them and keep resubscribing me when they import lists into Mailchimp or similar software.
11
12It's fascinating to see how much crap you get subscribed to when you are not paying attention. It got so bad that my primary Gmail address is a full of junk and need constant monitoring and cleaning up. And because I want to have Inbox Zero, this presents an additional problem for me.
13
14The stress that email presented for me didn't occur to me for a long time. I was noticing that I was unable to go through one single hour without hysterically refreshing email. And if somebody wrote me something, I needed to see it right then, even though I didn't immediately reply to it. I can only describe this with FOMO (fear of missing out). I have no other explanation than that. It was crippling, and I was constantly context switching, which I will address further down this post in more details.
15
16This was one of the reasons why I spawned up my personal email server, and I am using it now as my primary and person email. I still have Gmail as my “junk” email that I use for throw away stuff. I log in to Gmail once a week and check if there are any important emails that I got, but apart from that, it's sitting dormant and collecting dust.
17
18The more I was watching the world loose it's self with allowing anti freedom things to happen to it, the more I started to realize that something has to change. I don't have the power to change the world. And I also don't have a grandiose opinion of myself to even think to try it. But what I can do is to not subscribe to this consumer way of thinking. I will not be complicit in this. My moral and ethical stances won't allow it. So, this brings us to the second part of my journey.
19
20I was using all these 3rd party services because I was either lazy or OK with the drawbacks of them. I watched these services and companies became more and more evil. It is evil if you sell your user's data in this manner. Nobody reads privacy policies and everybody is OK with accepting them, and they pray on that flaw in human nature. I really hate the hypocrisy they manage to muster. These companies prey on our laziness, and we are at fault here. Nobody else. And I truly understand the reasons why we rather accept and move on, and not object and have our lives a little more difficult. They have perfected this through years of small changes that make us a little more dependent on them. You could not convince a person to give away all his rights and data in one day. This was gradual and slow. And it caught us all in surprise. When I really stopped and thought about it, I felt repulsed. By really stopping and thinking about it, I really mean stopping and thinking about it. Thoroughly and in depth.
21
22Each step I took depleted my character a bit more. Like I was trading myself bit by bit without understanding what it all meant. What it meant to be a full person, not divided by all this bought attention they want from me. They don't just get your data, but they also take your attention away from you. They scatter your and go with the divide and conquer tactic from there. And a person divided is a person not fully there. Not at the moment. Not alive fully.
23
24I was unable to form long thoughts. Well, I thought I was. But now that I see what being a full person is again, I can see that I was not at my 100% back then.
25
26A revolt was inevitable. There was no other way of continuing my story without it. Without taking back my attention, my thoughts, my time, and my privacy, regardless of how too late it maybe is.
27
28This has nothing to do with conspiracy theories. Even less with changing the world. All I wanted was to get my life back in order and not waste the energy that could be spent in other, better places.
29
30I started reading more. I can focus now fully on things I work on. Furthermore, I have the mental acuity that I never had before. My mind feels sharp. I don't get angry so much. I can cherish the finer things in life now without the need to interpret them intellectually. Not only that, but I have a feeling of belonging again. Sense of purpose has returned with a vengeance. And I can now help people without depleting myself.
31
32The last step so far was to finish closing all the remaining online accounts that I still had. And when I was thinking what value they bring me, I wasn't surprised that the answer was none. I wasn't logging in them and using them. I stopped being afraid of FOMO. If somebody wants to get in contact me, they will find a way. I am one search away.
33
34We are not beholden to anybody. Our lives are our own. So dare yourself to delete Facebook, LinkedIn. To unsubscribe. Dare yourself to take your time and attention back. Use that time and energy to go for a walk without thinking about work. Read a book instead of reading comment on social media that you will forget in an hour. Enrich your life instead of wasting it. It only requires a small step. And you will feel the benefits immediately. Lose the weight of the world that is crushing you without your consent.
diff --git a/content/posts/2021-08-01-linux-cheatsheet.md b/content/posts/2021-08-01-linux-cheatsheet.md
new file mode 100644
index 0000000..126e922
--- /dev/null
+++ b/content/posts/2021-08-01-linux-cheatsheet.md
@@ -0,0 +1,395 @@
1---
2title: List of essential Linux commands for server management
3url: linux-cheatsheet.html
4date: 2021-08-01
5draft: false
6---
7
8**Table of contents**
9
101. [Generate SSH key](#generate-ssh-key)
112. [Login to host via SSH](#login-to-host-via-ssh)
123. [Execute command on a server through SSH](#execute-command-on-a-server-through-ssh)
134. [Displays currently logged in users in the system](#displays-currently-logged-in-users-in-the-system)
145. [Displays Linux system information](#displays-linux-system-information)
156. [Displays kernel release information](#displays-kernel-release-information)
167. [Shows the system hostname](#shows-the-system-hostname)
178. [Shows system reboot history](#shows-system-reboot-history)
189. [Displays information about the user](#displays-information-about-the-user)
1910. [Displays IP addresses and all the network interfaces](#displays-ip-addresses-and-all-the-network-interfaces)
2011. [Downloads a file from an online source](#downloads-a-file-from-an-online-source)
2112. [Compress a file with gzip](#compress-a-file-with-gzip)
2213. [Interactive disk usage analyzer](#interactive-disk-usage-analyzer)
2314. [Install Node.js using the Node Version Manager](#install-nodejs-using-the-node-version-manager)
2415. [Too long; didn't read](#too-long-didnt-read)
2516. [Combine all Nginx access logs to one big log file](#combine-all-nginx-access-logs-to-one-big-log-file)
2617. [Set up Redis server](#set-up-redis-server)
2718. [Generate statistics of your webserver](#generate-statistics-of-your-webserver)
2819. [Search for a given pattern in files](#search-for-a-given-pattern-in-files)
2920. [Find proccess ID for a specific program](#find-proccess-id-for-a-specific-program)
3021. [Print name of current/working directory](#print-name-of-currentworking-directory)
3122. [Creates a blank new file](#creates-a-blank-new-file)
3223. [Displays first lines in a file](#displays-first-lines-in-a-file)
3324. [Displays last lines in a file](#displays-last-lines-in-a-file)
3425. [Count lines in a file](#count-lines-in-a-file)
3526. [Find all instances of the file](#find-all-instances-of-the-file)
3627. [Find file names that begin with ‘index’ in /home folder](#find-file-names-that-begin-with-index-in-home-folder)
3728. [Find files larger than 100MB in the home folder](#find-files-larger-than-100mb-in-the-home-folder)
3829. [Displays block devices related information](#displays-block-devices-related-information)
3930. [Displays free space on mounted systems](#displays-free-space-on-mounted-systems)
4031. [Displays free and used memory in the system](#displays-free-and-used-memory-in-the-system)
4132. [Displays all active listening ports](#displays-all-active-listening-ports)
4233. [Kill a process violently](#kill-a-process-violently)
4334. [List files opened by user](#list-files-opened-by-user)
4435. [Execute "df -h", showing periodic updates](#execute-df--h-showing-periodic-updates)
45
46
47##### Generate SSH key
48
49```bash
50ssh-keygen -t ed25519 -C "your_email@example.com"
51
52# when no support for Ed25519 present
53ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
54```
55
56Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder.
57
58
59
60##### Login to host via SSH
61
62```bash
63# connect to host as your local username
64ssh host
65
66# connect to host as user
67ssh <user>@<host>
68
69# connect to host using port
70ssh -p <port> <user>@<host>
71```
72
73
74
75##### Execute command on a server through SSH
76
77```bash
78# execute one command
79ssh root@100.100.100.100 "ls /root"
80
81# execute many commands
82ssh root@100.100.100.100 "cd /root;touch file.txt"
83```
84
85
86
87##### Displays currently logged in users in the system
88
89```bash
90w
91```
92
93
94
95##### Displays Linux system information
96
97```bash
98uname
99```
100
101
102
103##### Displays kernel release information
104
105```bash
106uname -r
107```
108
109
110
111##### Shows the system hostname
112
113```bash
114hostname
115```
116
117
118
119##### Shows system reboot history
120
121```bash
122last reboot
123```
124
125
126
127##### Displays information about the user
128
129```bash
130sudo apt install finger
131finger <username>
132```
133
134
135
136##### Displays IP addresses and all the network interfaces
137
138```bash
139ip addr show
140```
141
142
143
144##### Downloads a file from an online source
145
146```bash
147wget https://example.com/example.tgz
148```
149
150Note: If URL contains ?, & enclose the URL in double quotes.
151
152
153
154##### Compress a file with gzip
155
156```bash
157# will not keep the original file
158gzip file.txt
159
160# will keep the original file
161gzip --keep file.txt
162```
163
164
165
166##### Interactive disk usage analyzer
167
168```bash
169sudo apt install ncdu
170
171ncdu
172ncdu <path/to/directory>
173```
174
175
176
177##### Install Node.js using the Node Version Manager
178
179```bash
180curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
181source ~/.bashrc
182
183nvm install v13
184```
185
186
187
188##### Too long; didn't read
189
190```bash
191npm install -g tldr
192
193tldr tar
194```
195
196
197
198##### Combine all Nginx access logs to one big log file
199
200```bash
201zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
202```
203
204
205
206##### Set up Redis server
207
208```bash
209sudo apt install redis-server redis-tools
210
211# check if server is running
212sudo service redis status
213
214# set and get a key value
215redis-cli set mykey myvalue
216redis-cli get mykey
217
218# interactive shell
219redis-cli
220```
221
222
223
224##### Generate statistics of your webserver
225
226```bash
227sudo apt install goaccess
228
229# check if installed
230goaccess -v
231
232# combine logs
233zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
234
235# export to single html
236goaccess \
237 --log-file=/var/log/nginx/access-all.log \
238 --log-format=COMBINED \
239 --exclude-ip=0.0.0.0 \
240 --ignore-crawlers \
241 --real-os \
242 --output=/var/www/html/stats.html
243
244# cleanup afterwards
245rm /var/log/nginx/access-all.log
246```
247
248
249
250##### Search for a given pattern in files
251
252```bash
253grep -r ‘pattern’ files
254```
255
256
257
258##### Find proccess ID for a specific program
259
260```bash
261pgrep nginx
262```
263
264
265
266##### Print name of current/working directory
267
268```bash
269pwd
270```
271
272
273
274##### Creates a blank new file
275
276```bash
277touch newfile.txt
278```
279
280
281
282##### Displays first lines in a file
283
284```bash
285# -n <x> presents the number of lines (10 by default)
286head -n 20 somefile.txt
287```
288
289
290
291##### Displays last lines in a file
292
293```bash
294# -n <x> presents the number of lines (10 by default)
295tail -n 20 somefile.txt
296
297# -f follows the changes in file (doesn't closes)
298tail -f somefile.txt
299```
300
301
302
303##### Count lines in a file
304
305```bash
306wc -l somefile.txt
307```
308
309
310
311##### Find all instances of the file
312
313```bash
314sudo apt install mlocate
315
316locate somefile.txt
317```
318
319
320
321##### Find file names that begin with ‘index’ in /home folder
322
323```bash
324find /home/ -name "index"
325```
326
327
328
329##### Find files larger than 100MB in the home folder
330
331```bash
332find /home -size +100M
333```
334
335
336
337##### Displays block devices related information
338
339```bash
340lsblk
341```
342
343
344
345##### Displays free space on mounted systems
346
347```bash
348df -h
349```
350
351
352
353##### Displays free and used memory in the system
354
355```bash
356free -h
357```
358
359
360
361##### Displays all active listening ports
362
363```bash
364sudo apt install net-tools
365
366netstat -pnltu
367```
368
369
370
371##### Kill a process violently
372
373```bash
374kill -9 <pid>
375```
376
377
378
379##### List files opened by user
380
381```bash
382lsof -u <user>
383```
384
385
386
387##### Execute "df -h", showing periodic updates
388
389```bash
390# -n 1 means every second
391watch -n 1 df -h
392```
393
394
395
diff --git a/content/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md b/content/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md
new file mode 100644
index 0000000..1f5bd89
--- /dev/null
+++ b/content/posts/2021-12-03-debian-based-riced-up-distribution-for-developers.md
@@ -0,0 +1,194 @@
1---
2title: Debian based riced up distribution for Developers and DevOps folks
3url: debian-based-riced-up-distribution-for-developers-and-devops-folks.html
4date: 2021-12-03
5draft: false
6---
7
8I have been using [Ubuntu](https://ubuntu.com/) for quite a longtime now. I have used [Debian](https://www.debian.org/) in the past and [Manjaro](https://manjaro.org/). Also had [Arch](https://archlinux.org/) for some time and even ran [Gentoo](https://www.gentoo.org/) way back.
9
10What I learned from all this is that I prefer running a bit older versions and having them be stable than run bleeding edge rolling release. For that reason, I stuck with Ubuntu for a couple of years now. I am also at a point in my life where I just don't care what is cool or hip anymore. I just want a stable system that doesn't get in my way.
11
12During all this, I noticed that these distributions were getting very bloated and a lot of software got included that I usually uninstall on fresh installation. Maybe this is my OCD speaking, but why do I have to give fresh installation min 1 GB of ram out of the box just to have a blank screen in front of me? I get it, there are many things included in the distro to make my life easier. I understand. But at this point I have a feeling that modern Linux distributions are becoming similar to [Node.js project with node_modules](https://devhumor.com/content/uploads/images/August2017/node-modules.jpg). Just a crazy number of packages serving very little or no purpose, just supporting other software.
13
14I felt I needed a fresh start. To start over with something minimal and clean. Something that would put a little more joy into using a computer again.
15
16For the first version, I wanted to target the following machines I have at home that I want this thing to work on.
17
18```yaml
19# My main stationary work machine
20Resolution: 3840x1080 (Super Ultrawide Monitor 32:9)
21CPU: Intel i7-8700 (12) @ 4.600GHz
22GPU: AMD ATI Radeon RX 470/480/570/570X/580/580X/590
23Memory: 32020MiB
24```
25
26```yaml
27# Thinkpad x220 for testing things and goofing around
28Resolution: 1366x768
29CPU: Intel i5-2520M (4) @ 3.200GHz
30GPU: Intel 2nd Generation Core Processor Family
31Memory: 15891MiB
32```
33
34## How should I approach this?
35
36I knew I wanted to use [minimal Debian netinst ](https://www.debian.org/CD/netinst/) for the base to give myself a head start. No reason to go through changing the installer and also testing all that behemoth of a thing. So, some sort of ricing was the only logical option to get this thing of the grounds somewhat quickly.
37
38
39> **What is ricing anyway?**
40> The term “RICE” stands for Race Inspired Cosmetic Enhancement. A group of people (could be one, idk) decided to see if they could tweak their own distros like they/others did their cars. This gave rise to a community of Linux/Unix enthusiasts trying to make their distros look cooler and better than others... For more information, read this article [What in the world is ricing!?](https://pesos.github.io/2020/07/14/what-is-ricing.html).
41
42I didn't want this to just be a set of config files for theming purpose. I wanted this to include a set of pre-installed tools and services that are being used all the time by a modern developer. Theming is just a tiny part of it. Fonts being applied across the distro and things like that.
43
44First, I choose terminal installer and left it to load additional components. Avoid using graphical installer in this case.
45
46![](/dfd-rice/install-00.png)
47
48After that I selected hostname and created a normal user and set password for that user and root user and choose guided mode for disk partitioning.
49
50![](/dfd-rice/install-01.png)
51
52I left it run to install all the things required for the base system and opted out of scanning additional media for use by the package manager. Those will be downloaded from the internet during installation.
53
54![](/dfd-rice/install-02.png)
55
56
57I opted out of the popularity contest, and **now comes the important part**. Uncheck all the boxes in Software selection and only leave 'standard system utilities'. I also left an SSH server, so I was able to log in to the machine from my main PC.
58
59![](/dfd-rice/install-03.png)
60
61At this point, I installed GRUB bootloader on the disk where I installed the system.
62
63![](/dfd-rice/install-04.png)
64
65That concluded the installation of base Debian and after restarting the computer I was prompted with the login screen.
66
67![](/dfd-rice/install-05.png)
68
69Now that I had the base installation, it was time to choose what software do I want to include in this so-called distribution. I wanted out of the box developer experience, so I had plenty to choose.
70
71Let's not waste time and go through the list.
72
73## Desktop environments
74
75I have been using [Gnome](https://www.gnome.org/) for my whole Linux life. From version 2 forward. It's been quite a ride. I hated version 3 when it came out and replaced version 2. But I got used to it. And now with version 40+ they also made couple of changes which I found both frustrating and presently surprised.
76
77The amount of vertical space you loose because of the beefy title bars on windows is ridiculous. And then in case of [Tilix](https://gnunn1.github.io/tilix-web/) you also have tabs, and you are 100px deep. Vertical space is one of the most important things for a developer. The more real estate you have, the more code you can have in a viewport.
78
79But on the other hand, I still love how Gnome feels and looks. I gotta give them that. They really are trying to make Gnome feel unified and modern.
80
81Regardless of all the nice things Gnome has, I was looking at the tiling window managers for some time, but never had the nerve to actually go with it. But now was the ideal time to give it a go. No guts, no glory kind of a thing.
82
83One of the requirements for me was easy custom layouts because I use a really strange monitor with aspect ratio of 32:9. So relying on included layouts most of them have is a non-starter.
84
85What I was doing in Gnome was having windows in a layout like the diagram below. This is my common practice. And if you look at it you can clearly see I was replicating tiling window manager setup in Gnome.
86
87![](/dfd-rice/layout.png)
88
89
90That made me look into a bunch of tiling window managers and then tested them out. Candidates I was looking at were:
91
92- [i3](https://i3wm.org/)
93- [bspwm](https://github.com/baskerville/bspwm)
94- [awesome](https://awesomewm.org/index.html)
95- [XMonad](https://xmonad.org/)
96- [sway](https://swaywm.org/)
97- [Qtile](http://www.qtile.org/)
98- [dwm](https://dwm.suckless.org/)
99
100You can also check article [13 Best Tiling Window Managers for Linux](https://www.tecmint.com/best-tiling-window-managers-for-linux/) I was referencing while testing them out.
101
102While all of them provided what I needed, I liked i3 the most. What particular caught my eye was the ease to use and tree based layouts which allows flexible layouts. I know others can be set up also to have custom layouts other than spiral, dwindle etc. I think i3 is a good entry-level window manager for somebody like me.
103
104
105## Batteries included
106
107The source for the whole thing is located on Github https://github.com/mitjafelicijan/dfd-rice.
108
109Currenly included:
110- `non-free` (enables non-free packages in apt)
111- `sudo` (adds sudo and adds user to sudo group)
112- `essentials` (gcc, htop, zip, curl, etc...)
113- `wifi` (network manager nmtui)
114- `desktop` (i3, dmenu, fonts, configurations)
115- `pulseaudio` (pulseaudio with pavucontrol)
116- `code-editors` (vim, micro, vscode)
117- `ohmybash` (make bash pretty)
118- `file-managers` (mc)
119- `git-ui` (terminal git gui)
120- `meld` (diff tool)
121- `profiling` (kcachegrind, valgrind, strace, ltrace)
122- `browsers` (brave, firefox, chromium)
123- programming languages:
124 - `python`
125 - `golang`
126 - `nodejs`
127 - `rust`
128 - `nim`
129 - `php`
130 - `ruby`
131- `docker` (with docker-compose)
132- `ansible`
133
134Install script also allows you to install only specific packages (example for: essentials ohmybash docker rust).
135
136```sh
137su - root \
138 bash -c "$(wget -q https://raw.github.com/mitjafelicijan/dfd-rice/master/tools/install.sh -O -)" -- \
139 essentials ohmybash docker rust
140
141```
142
143Currently, most of these recipes use what Debian and this is totally fine with me since I never use bleeding edge features of a package. But if something major would come to light, I will replace it with a possible compilation script or something similar.
144
145This is some of the output from the installation script.
146
147![](/dfd-rice/script.png)
148
149Let's take a look at some examples in the installation script.
150
151
152##### Docker recipe
153
154```sh
155# docker
156print_header "Installing Docker"
157curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
158echo "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
159apt update
160apt -y install docker-ce docker-ce-cli containerd.io docker-compose
161
162systemctl start docker
163systemctl enable docker
164systemctl status docker --no-pager
165
166/sbin/usermod -aG docker $USERNAME
167```
168
169##### Making bash pretty
170
171I really like [Oh My Zsh](https://ohmyz.sh/), but I don't like zsh shell. When I used it, I constantly needed to be aware of it and running bash scripts was a pain. So, I was really delighted when I found out that a version for bash existed called [Oh My Bash](https://ohmybash.nntoan.com/). Let's take a look at the recipe for installing it.
172
173```sh
174# ohmybash
175print_header "Enabling OhMyBash"
176sudo -u $USERNAME sh -c "$(curl -fsSL https://raw.github.com/ohmybash/oh-my-bash/master/tools/install.sh)" &
177T1=${!}
178wait ${T1}
179```
180
181Because OhMyBash does `exec bash` at the end, this traps our script inside another shell and our script cannot continue. For that reason, I executed this in background. But that presents a new problem. Because this is executed in background, we lose track of progress naturally. And that strange trick with `T1=${!}` and `wait ${T1}` waits for the background process to finish before continuing to another task in bash script.
182
183Check [Multi-Threaded Processing in Bash Scripts](https://www.cloudsavvyit.com/12277/how-to-use-multi-threaded-processing-in-bash-scripts/) for more details.
184
185
186## Conclusion
187
188Take a look at https://github.com/mitjafelicijan/dfd-rice/blob/develop/tools/install.sh script to get familiar with it. This is just a first iteration and I will continue to update it because I need this in my life.
189
190The current version boots in 4s to the login prompt, and after you log in, the desktop environment loads in 2s. So, its fast, very fast. And on clean boot, I measured ~230 MB of RAM usage.
191
192And this is how it looks with two terminals side by side. I really like the simplicity and clean interface. I will polish the colors and stuff like that, but I really do like the results.
193
194![](/dfd-rice/desktop.png)
diff --git a/content/posts/2021-12-25-running-golang-application-as-pid1.md b/content/posts/2021-12-25-running-golang-application-as-pid1.md
new file mode 100644
index 0000000..2f49466
--- /dev/null
+++ b/content/posts/2021-12-25-running-golang-application-as-pid1.md
@@ -0,0 +1,299 @@
1---
2title: Running Golang application as PID 1 with Linux kernel
3url: running-golang-application-as-pid1.html
4date: 2021-12-25
5draft: false
6---
7
8**Table of contents**
9
101. [Unikernels, kernels, and alike](#unikernels-kernels-and-alike)
112. [What is PID 1?](#what-is-pid-1)
123. [So why even run application as PID 1 instead of just using a container?](#so-why-even-run-application-as-pid-1-instead-of-just-using-a-container)
134. [The master plan](#the-master-plan)
145. [Compiling Linux kernel](#compiling-linux-kernel)
156. [Preparing PID 1 application in Golang](#preparing-pid-1-application-in-golang)
167. [Running all of it with QEMU](#running-all-of-it-with-qemu)
178. [Size comparison](#size-comparison)
189. [Creating ISO image and running it with Gnome Boxes](#creating-iso-image-and-running-it-with-gnome-boxes)
1910. [Is running applications as PID 1 even worth it?](#is-running-applications-as-pid-1-even-worth-it)
20
21## Unikernels, kernels, and alike
22
23I have been reading a lot about [unikernernels](https://en.wikipedia.org/wiki/Unikernel) lately and found them very intriguing. When you push away all the marketing speak and look at the idea, it makes a lot of sense.
24
25> A unikernel is a specialized, single address space machine image constructed by using library operating systems. ([Wikipedia](https://en.wikipedia.org/wiki/Unikernel))
26
27I really like the explanation from the article [Unikernels: Rise of the Virtual Library Operating System](https://queue.acm.org/detail.cfm?id=2566628). Really worth a read.
28
29If we compare a normal operating system to a unikernel side by side, they would look something like this.
30
31![Virtual machines vs Containers vs Unikernels](/pid1/unikernels.png)
32
33From this image, we can see how the complexity significantly decreases with the use of Unikernels. This comes with a price, of course. Unikernels are hard to get running and require a lot of work since you don't have an actual proper kernel running in the background providing network access and drivers etc.
34
35So as a half step to make the stack simpler, I started looking into using Linux kernel as a base and going from there. I came across this [Youtube video talking about Building the Simplest Possible Linux System](https://www.youtube.com/watch?v=Sk9TatW9ino) by [Rob Landley](https://landley.net) and apart from statically compiling the application to be run as PID1 there was really no other obstacles.
36
37## What is PID 1?
38
39PID 1 is the first process that Linux kernel starts after the boot process. It also has a couple of unique properties that are unique to it.
40
41- When the process with PID 1 dies for any reason, all other processes are killed with KILL signal.
42- When any process having children dies for any reason, its children are re-parented to process with PID 1.
43- Many signals which have default action of Term do not have one for PID 1.
44- When the process with PID 1 dies for any reason, kernel panics, which result in system crash.
45
46PID 1 is considered as an Init application which takes care of running other and handling services like:
47
48- sshd,
49- nginx,
50- pulseaudio,
51- etc.
52
53If you are on a Linux machine, you can check what your process is with PID 1 by running the following.
54
55```sh
56$ cat /proc/1/status
57Name: systemd
58Umask: 0000
59State: S (sleeping)
60Tgid: 1
61Ngid: 0
62Pid: 1
63PPid: 0
64...
65```
66
67As we can see on my machine the process with id of 1 is [systemd](https://systemd.io/) which is a software suite that provides an array of system components for Linux operating systems. If you look closely you can also see that the `PPid` (process id of the parent process) is `0` which additionally confirms that this process doesn't have a parent.
68
69## So why even run application as PID 1 instead of just using a container?
70
71Containers are wonderful, but they come with a lot of baggage. And because they are in their nature layered, the images require quite a lot of space and also a lot of additional software to handle them. They are not as lightweight as they seem, and many popular images require 500 MB plus disk space.
72
73The idea of running this as PID 1 would result in a significantly smaller footprint, as we will see later in the post.
74
75> You could run a simple init system inside Docker container described more 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/).
76
77## The master plan
78
791. Compile Linux kernel with the default definitions.
802. Prepare a Hello World application in Golang that is statically compiled.
813. Run it with [QEMU](https://www.qemu.org/) and providing Golang application as init application / PID 1.
82
83For the sake of simplicity we will not be cross-compiling any of it and just use the 64bit version.
84
85## Compiling Linux kernel
86
87```sh
88$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.7.tar.xz
89$ tar xf linux-5.15.7.tar.xz
90
91$ cd linux-5.15.7
92
93$ make clean
94
95# read more about this https://stackoverflow.com/a/41886394
96$ make defconfig
97
98$ time make -j `nproc`
99
100$ cd ..
101```
102
103At this point we have kernel image that is located in `arch/x86_64/boot/bzImage`. We will use this in QEMU later.
104
105To make our lives a bit easier lets move the kernel image to another place. Lets create a folder `bin/` in the root of our project with `mkdir -p bin`.
106
107
108At this point we can copy `bzImage` to `bin/` folder with `cp linux-5.15.7/arch/x86_64/boot/bzImage bin/bzImage`.
109
110The folder structure of this experiment should look like this.
111
112```
113pid1/
114 bin/
115 bzImage
116 linux-5.15.7/
117 linux-5.15.7.tar.xz
118```
119
120## Preparing PID 1 application in Golang
121
122This step is relatively easy. The only thing we must have in mind that we will need to compile the binary as a static one.
123
124Let's create `init.go` file in the root of the project.
125
126```go
127package main
128
129import (
130 "fmt"
131 "time"
132)
133
134func main() {
135 for {
136 fmt.Println("Hello from Golang")
137 time.Sleep(1 * time.Second)
138 }
139}
140```
141
142If you notice, we have a forever loop in the main, with a simple sleep of 1 second to not overwhelm the CPU. This is because PID 1 should never complete and/or exit. That would result in a kernel panic. Which is BAD!
143
144There are two ways of compiling Golang application. Statically and dynamically.
145
146To statically compile the binary, use the following command.
147
148```sh
149$ go build -ldflags="-extldflags=-static" init.go
150```
151
152We can also check if the binary is statically compiled with:
153
154```sh
155$ file init
156init: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=Ypu8Zw_4NBxm1Yxg2OYO/H5x721rQ9uTPiDVh-VqP/vZN7kXfGG1zhX_qdHMgH/9vBfmK81tFrygfOXDEOo, not stripped
157
158$ ldd init
159not a dynamic executable
160```
161
162At this point, we need to create [initramfs](https://www.linuxfromscratch.org/blfs/view/svn/postlfs/initramfs.html) (abbreviated from "initial RAM file system", is the successor of initrd. It is a cpio archive of the initial file system that gets loaded into memory during the Linux startup process).
163
164```sh
165$ echo init | cpio -o --format=newc > initramfs
166$ mv initramfs bin/initramfs
167```
168
169The projects at this stage should look like this.
170
171```
172pid1/
173 bin/
174 bzImage
175 initramfs
176 linux-5.15.7/
177 linux-5.15.7.tar.xz
178 init.go
179```
180
181## Running all of it with QEMU
182
183[QEMU](https://www.qemu.org/) is a free and open-source hypervisor. It emulates the machine's processor through dynamic binary translation and provides a set of different hardware and device models for the machine, enabling it to run a variety of guest operating systems.
184
185```sh
186$ qemu-system-x86_64 -serial stdio -kernel bin/bzImage -initrd bin/initramfs -append "console=ttyS0" -m 128
187```
188
189```sh
190$ qemu-system-x86_64 -serial stdio -kernel bin/bzImage -initrd bin/initramfs -append "console=ttyS0" -m 128
191[ 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
192[ 0.000000] Command line: console=ttyS0
193[ 0.000000] x86/fpu: x87 FPU will use FXSAVE
194[ 0.000000] signal: max sigframe size: 1440
195[ 0.000000] BIOS-provided physical RAM map:
196[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
197[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
198[ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
199[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000007fdffff] usable
200[ 0.000000] BIOS-e820: [mem 0x0000000007fe0000-0x0000000007ffffff] reserved
201[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
202[ 0.000000] NX (Execute Disable) protection: active
203[ 0.000000] SMBIOS 2.8 present.
204[ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-6.fc35 04/01/2014
205[ 0.000000] tsc: Fast TSC calibration failed
206...
207[ 2.016106] ALSA device list:
208[ 2.016329] No soundcards found.
209[ 2.053176] Freeing unused kernel image (initmem) memory: 1368K
210[ 2.056095] Write protecting the kernel read-only data: 20480k
211[ 2.058248] Freeing unused kernel image (text/rodata gap) memory: 2032K
212[ 2.058811] Freeing unused kernel image (rodata/data gap) memory: 500K
213[ 2.059164] Run /init as init process
214Hello from Golang
215[ 2.386879] tsc: Refined TSC clocksource calibration: 3192.032 MHz
216[ 2.387114] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x2e02e31fa14, max_idle_ns: 440795264947 ns
217[ 2.387380] clocksource: Switched to clocksource tsc
218[ 2.587895] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
219Hello from Golang
220Hello from Golang
221Hello from Golang
222```
223
224The whole [log file here](/pid1/qemu.log).
225
226## Size comparison
227
228The cool thing about this approach is that the Linux kernel and the application together only take around 12 MB, which is impressive as hell. And we need to also know that the size of bzImage (Linux kernel) could be greatly decreased by going into `make menuconfig` and removing a ton of features from the kernel, making the size even smaller. I managed to get kernel size down to 2 MB and still working properly.
229
230```sh
231total 12M
232-rw-r--r--. 1 m m 9.3M Dec 13 10:24 bzImage
233-rw-r--r--. 1 m m 1.9M Dec 27 01:19 initramfs
234```
235
236## Creating ISO image and running it with Gnome Boxes
237
238First we need to create proper folder structure with `mkdir -p iso/boot/grub`.
239
240Then we need to download the [grub binary](https://github.com/littleosbook/littleosbook/raw/master/files/stage2_eltorito). You can read more about this program on https://github.com/littleosbook/littleosbook.
241
242```sh
243$ wget -O iso/boot/grub/stage2_eltorito https://github.com/littleosbook/littleosbook/raw/master/files/stage2_eltorito
244```
245
246```sh
247$ tree iso/boot/
248iso/boot/
249├── bzImage
250├── grub
251│   ├── menu.lst
252│   └── stage2_eltorito
253└── initramfs
254```
255
256Let's copy files into proper folders.
257
258
259```sh
260$ cp stage2_eltorito iso/boot/grub/
261$ cp bin/bzImage iso/boot/
262$ cp bin/initramfs iso/boot/
263```
264
265Lets create a GRUB config file at `nano iso/boot/grub/menu.lst` with contents.
266
267```ini
268default=0
269timeout=5
270
271title GoAsPID1
272kernel /boot/bzImage
273initrd /boot/initramfs
274```
275
276Let's create iso file by using genisoimage:
277
278```sh
279genisoimage -R \
280 -b boot/grub/stage2_eltorito \
281 -no-emul-boot \
282 -boot-load-size 4 \
283 -A os \
284 -input-charset utf8 \
285 -quiet \
286 -boot-info-table \
287 -o GoAsPID1.iso \
288 iso
289```
290
291This will produce `GoAsPID1.iso` which you can use with [Virtualbox](https://www.virtualbox.org/) or [Gnome Boxes](https://apps.gnome.org/app/org.gnome.Boxes/).
292
293<video src="/pid1/boxes.mp4" controls></video>
294
295## Is running applications as PID 1 even worth it?
296
297Well, the answer to this is not as simple as one would think. Sometimes it is and sometimes it's not. For embedded systems and very specialized applications it is worth for sure. But in normal uses, I don't think so. It was an interesting exercise in compiling kernels and looking at the guts of the Linux kernel, but sticking to containers for most of the things is a better option in my opinion.
298
299An interesting experiment would be creating an image that supports networking and could be deployed to AWS as an EC2 instance and observing how it fares. But in that case, we would need to write some sort of supervisor that would run on a separate EC2 that would check if other EC2 instances are running properly. Remember that if your application fails, kernel panics and the whole machine is inoperable in this case.
diff --git a/content/posts/2021-12-30-wap-mobile-web-before-the-web.md b/content/posts/2021-12-30-wap-mobile-web-before-the-web.md
new file mode 100644
index 0000000..b421535
--- /dev/null
+++ b/content/posts/2021-12-30-wap-mobile-web-before-the-web.md
@@ -0,0 +1,161 @@
1---
2title: Wireless Application Protocol and the mobile web before the web
3url: wap-mobile-web-before-the-web.html
4date: 2021-12-30
5draft: false
6---
7
8**Table of contents**
9
101. [A little stroll down the history lane](#a-little-stroll-down-the-history-lane)
112. [WAP - Wireless Application Protocol](#wap---wireless-application-protocol)
123. [WML - Wireless Markup Language](#wml---wireless-markup-language)
134. [Converting Digg to WML](#converting-digg-to-wml)
145. [Conclusion](#conclusion)
15
16## A little stroll down the history lane
17
18About two weeks ago, I watched this outstanding documentary on YouTube [Springboard: the secret history of the first real smartphone](https://www.youtube.com/watch?v=b9_Vh9h3Ohw) about the history of smartphones and phones in general. It brought back so many memories. I never had an actual smartphone before the Android. The closest to smartphone was [Sony Ericsson P1](https://www.gsmarena.com/sony_ericsson_p1-1982.php). A fantastic phone and I broke it in Prague after a party and that was one of those rare occasions where I was actually mad at myself. But nevertheless, after that phone, the next one was an Android one.
19
20Before that, I only owned normal phones from Nokia and Siemens etc. Nothing special, actually. These are the phones we are talking about. Before 2007. Apple and Android phones didn't exist yet.
21
22These phones were rocking:
23
24- No selfie cameras.
25- ~2 inch displays.
26- ~120 MHz beast CPU's.
27- 144p main cameras.
28- But they had a headphone jack.
29
30Let's take a look at these beauties.
31
32![Old phones](/wap/phones.gif)
33
34## WAP - Wireless Application Protocol
35
36Not that one! We are talking about Wireless Application Protocol and not Cardi B's song 😃
37
38WAP stands for Wireless Application Protocol. It is a protocol designed for micro-browsers, and it enables the access of internet in the mobile devices. It uses the mark-up language WML (Wireless Markup Language and not HTML), WML is defined as XML 1.0 application. Furthermore, it enables creating web applications for mobile devices. In 1998, WAP Forum was founded by Ericson, Motorola, Nokia and Unwired Planet whose aim was to standardize the various wireless technologies via protocols. [(source)](https://www.geeksforgeeks.org/wireless-application-protocol/)
39
40WAP protocol was resulted by the joint efforts of the various members of WAP Forum. In 2002, WAP forum was merged with various other forums of the industry, resulting in the formation of Open Mobile Alliance (OMA). [(source)](https://www.geeksforgeeks.org/wireless-application-protocol/)
41
42These were some wild times. Devices had tiny screens and data transmission rates were abominable. But they were capable of rendering WML (Wireless Markup Language). This was very similar to HTML, actually. It is a markup language, after all.
43
44These pages could be served by [Apache](https://apache.org/) and could be generated by CGI scripts on the backend. The only difference was the limited markup language.
45
46## WML - Wireless Markup Language
47
48Just like web browsers use HTML for content structure, older mobile device browsers use WML - if you need to support really old mobile phones using WML browsers, you will need to know about it. WML is XML-based (an XML vocabulary just like XHTML and MathML, but not HTML) and does not use the same metaphor as HTML. HTML is a single document with some metadata packed away in the head, and a body encapsulating the visible page. With WML, the metaphor does not envisage a page, but rather a deck of cards. A WML file might have several pages or cards contained within it. [(source)](https://www.w3.org/wiki/Introduction_to_mobile_web)
49
50```html
51<?xml version="1.0"?>
52<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
53<wml>
54 <card id="home" title="Example Homepage">
55 <p>Welcome to the Example homepage</p>
56 </card>
57</wml>
58```
59
60There is an amazing tutorial on [Tutorialpoint about WML](https://www.tutorialspoint.com/wml/index.htm).
61
62## Converting Digg to WML
63
64This task is completely useless and not really feasible nowadays, but I had to give it a try for old-time sake. Since the data is already there in a form of RSS feed, I could take this feed and parse it and create a WML version of the homepage.
65
66We will need:
67
68- Python3 + Pip
69- ImageMagick
70- feedparser and mako templating
71
72```sh
73# for fedora 35
74sudo dnf install ImageMagick python3-pip
75
76# tempalting engine for python
77pip install mako --user
78
79# for parsing rss feeds
80pip install feedparser --user
81```
82
83Project folder structure should look like the following.
84
85```
8612:43:53 m@khan wap → tree -L 1
87.
88├── generate.py
89└── template.wml
90
91```
92
93After that, I created a small template for the homepage.
94
95```html
96<?xml version="1.0"?>
97<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml_1.2.xml">
98
99<wml>
100
101 <card title="Digg - What the Internet is talking about right now">
102
103 % for item in entries:
104 <p><img src="/images/${item.id}.jpg" width="175" height="95" alt="${item.title}" /></p>
105 <p><small>${item.kicker}</small></p>
106 <p><big><b>${item.title}</b></big></p>
107 <p>${item.description}</p>
108 % endfor
109
110 </card>
111
112</wml>
113```
114
115And the parser that parses RSS feed looks like this.
116
117```python
118import os
119import feedparser
120from mako.template import Template
121
122os.system('mkdir -p www/images')
123
124template = Template(filename='template.wml')
125
126feed = feedparser.parse('https://digg.com/rss/top.xml')
127
128entries = feed.entries[:15]
129
130for entry in entries:
131 print('Processing image with id {}'.format(entry.id))
132 os.system('wget -q -O www/images/{}.jpg "{}"'.format(entry.id, entry.links[1].href))
133 os.system('convert www/images/{}.jpg -type Grayscale -resize 175x -depth 3 -quality 30 www/images/{}.jpg'.format(entry.id, entry.id))
134
135html = template.render(entries = entries)
136
137with open('www/index.wml', 'w+') as fp:
138 fp.write(html)
139```
140
141This script will create a folder `www` and in the folder `www/images` for storing resized images.
142
143> Be sure you don't use SSL and use just normal HTTP for serving the content. These old phones will have problems with TLS 1.3 etc.
144
145If you look at the python file, I convert all the images into tiny B&W images. They should be WBMP (Wireless BitMaP) but I choose JPEGs for this, and it seems to work properly.
146
147Because I currently don't have a phone old enough to test it on, I used an emulator. And it was really hard to find one. I found [WAP Proof](http://wap-proof.sharewarejunction.com/) on shareware junction, and it did the job well enough. I will try to find and actual device to test it on.
148
149<video src="/wap/emulator.mp4" controls></video>
150
151If you are using Nginx to serve the contents, add a directive to the hosts file that will automatically server `index.wml` file.
152
153```nginx
154server {
155 index index.wml index.html index.htm index.nginx-debian.html;
156}
157```
158
159## Conclusion
160
161Well, this was pointless, but very fun! You can [give it a try on digg.mitjafelicijan.com](digg.mitjafelicijan.com) but make sure you are using a browser or old emulator that supports WML.
diff --git a/content/posts/2022-06-28-sentiment-based-on-political-bias.md b/content/posts/2022-06-28-sentiment-based-on-political-bias.md
new file mode 100644
index 0000000..5b7816c
--- /dev/null
+++ b/content/posts/2022-06-28-sentiment-based-on-political-bias.md
@@ -0,0 +1,84 @@
1---
2title: Sentiment distribution analysis based on political bias in online publications
3url: sentiment-based-on-political-bias.html
4date: 2022-06-28
5draft: true
6---
7
8I have been wondering for a long time what would sentiment differences look based on political leaning from popular publications. Is it the left that is more optimistic, center or the right.
9
10> Before people loose their minds, I don't care about political stuff and this is data we are talking about and not my personal feelings about it. So, before saying anything have this in mind and let data speak for itself.
11
12## Preparing the data
13
14The first step in getting stories from publications so we can start doing sentiment analysis on it. I have chosen to select 30 publications. 10 from left leaning publications, 10 from centre and 10 from right leaning ones.
15
16To find out leaning of a publication I will defer to AllSides website which provide a [Media Bias Ratings](https://www.allsides.com/media-bias/ratings).
17
18![AllSides Bias Chart](https://www.allsides.com/sites/default/files/AllSidesMediaBiasChart-Version6_0.jpg)
19
20The chart above is taken from AllSides and demonstrates political leaning of publications. This data changes over time and AllSides have made the revisions publicly available on their website.
21
22- [Learn more about Version 6](https://www.allsides.com/blog/new-allsides-media-bias-chart-version-6-updated-ratings-npr-newsmax-and-more)
23- [Learn more about Version 5](https://www.allsides.com/blog/new-allsides-media-bias-chart-version-42)
24- [Learn about Version 4](https://www.allsides.com/blog/new-allsides-media-bias-chart-announcing-version-4)
25- [Learn about Version 3](https://www.allsides.com/blog/new-allsides-media-bias-chart-version-3)
26- [Learn about Version 2](https://www.allsides.com/blog/new-allsides-media-bias-chart-version-2-updated-media-bias-ratings)
27- [Learn about Version 1.1](https://www.allsides.com/blog/updated-allsides-media-bias-chart-version-11)
28- [Learn about Version 1](https://www.allsides.com/blog/introducing-allsides-media-bias-chart)
29
30
31They categorise political bias AllSides came up with is:
32
33TODO: CREATE A HORIZONTAL ARRAY STYLE OF CHART IMAGE
34
35- Left,
36- Lean Left,
37- Center,
38- Lean Right,
39- Right.
40
41I will group Left and Lean Left together. And the same goes for Lean Right and Right. So we end up with three groups [Left, Center, Right].
42
43The list I have ended up with contains these publications:
44
45- Left political bias:
46 - BuzzFeed News (https://www.buzzfeednews.com)
47 - CNN (https://cnn.com)
48 - Daily Beast (https://www.thedailybeast.com)
49 - HuffPost (https://www.huffpost.com)
50 - The Intercept (https://theintercept.com)
51 - Vox (https://www.vox.com)
52 - Slate (https://slate.com)
53 - The New Yorker (https://www.newyorker.com)
54 - MSNBC (https://www.msnbc.com)
55 - New York Times News (https://www.nytimes.com)
56- Center political "bias":
57 - Axios (https://www.axios.com)
58 - BBC (https://www.bbc.com)
59 - News Week (https://www.newsweek.com)
60 - Reuters (https://www.reuters.com)
61 - RealClear Politics (https://www.realclearpolitics.com)
62 - The Hill (https://thehill.com)
63 - The Wall Street Journal News(https://www.wsj.com)
64 - Associated Press News (https://apnews.com)
65 - CNET (https://www.cnet.com)
66 - Forbes (https://www.forbes.com)
67- Right political bias:
68 - The American Spectator (https://spectator.org)
69 - Breitbart News (http://www.breitbart.com)
70 - The Blaze (https://www.theblaze.com)
71 - Daily Caller (http://dailycaller.com)
72 - Daily Mail (https://www.dailymail.co.uk)
73 - The Daily Wire (https://www.dailywire.com)
74 - Fox News (https://www.foxnews.com)
75 - The Federalist (https://thefederalist.com)
76 - New York Post Opinion (https://nypost.com/opinion/)
77 - OANN (https://www.oann.com)
78
79
80
81
82
83
84
diff --git a/content/posts/2022-06-30-trying-out-helix-editor.md b/content/posts/2022-06-30-trying-out-helix-editor.md
new file mode 100644
index 0000000..8286735
--- /dev/null
+++ b/content/posts/2022-06-30-trying-out-helix-editor.md
@@ -0,0 +1,31 @@
1---
2title: Trying out Helix code editor as my main editor
3url: tying-out-helix-code-editor.html
4date: 2022-06-30
5draft: false
6---
7
8I have been searching for a lightweight code editor for quite some time. One of the main reasons was that I wanted something that doesn't burn through CPU and RAM usage is not through the roof. I have been mostly using Visual Studio Code. It's been an outstanding editor. I have no quarrel with it at all. It's just time to spice life up with something new.
9
10I have been on this search for a couple of years. I have tried Vim, Neovim, Emacs, Doom Emacs, Micro and couple more. Among most of them, I liked Micro and Doom Emacs the most. Micro editor was a little too basic for me. And Doom Emacs was a bit too hardcore. This does not reflect on any of the editors. It's just my personal preference.
11
12> I tried Helix Editor about a year ago. But I didn't pay attention to it. Tried it and saw it's similar to Vi and just said no. I was premature to dismiss it.
13
14One of the things I actually miss is line wrapping for certain files. When writing Markdown, line wrapping would be very helpful. Editing such a document is frustrating to say the least. Some of the Markdown to HTML converters don't take kindly of new lines between sentences. Not paragraphs, sentences. And I use Markdown to write this blog you are reading.
15
16But other than this, I have been extremely satisfied by it. It's been a pleasant surprise. There have been zero issues with the editor.
17
18One thing to do before you are able to use autocompletion and make use Language Server support is to install the language server with NPM.
19
20```sh
21npm install -g typescript typescript-language-server
22```
23
24I am still getting used to the keyboard shortcuts and getting better. What Helix does really well is packing in sane defaults and even though because currently there is no plugin support I haven't found any need for them. It has all that you would need. It goes to extreme measures to show a user what is going on with popups that show you what the keyboard shortcuts are.
25
26And it comes us packed with many [really good themes](https://github.com/helix-editor/helix/wiki/Themes).
27
28![Editor](/helix-editor/editor.png)
29
30It's still young but has this mature feeling to it. It has sane defaults and mimics Vim (works a bit differently, but the overall idea is similar).
31
diff --git a/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md b/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md
new file mode 100644
index 0000000..c028ec2
--- /dev/null
+++ b/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md
@@ -0,0 +1,300 @@
1---
2title: What would DNA sound if synthesized to an audio file
3url: what-would-dna-sound-if-synthesized.html
4date: 2022-07-05
5draft: false
6---
7
8**Table of contents**
9
101. [Introduction](#introduction)
112. [DNA encoding and primer example](#dna-encoding-and-primer-example)
123. [Parsing DNA data](#parsing-dna-data)
134. [Generating sine wave](#generating-sine-wave)
145. [Generating a WAV file from accumulated sine waves](#generating-a-wav-file-from-accumulated-sine-waves)
156. [Generating Spectograms](#generating-spectograms)
167. [Pre-generated sequences](#pre-generated-sequences)
17 1. [Niels Bohr quote](#niels-bohr-quote)
18 2. [Mouse](#mouse)
19 3. [Bison](#bison)
20 4. [Taurus](#taurus)
218. [Making a drummer out of a DNA sequence](#making-a-drummer-out-of-a-dna-sequence)
229. [Going even further](#going-even-further)
23
24## Introduction
25
26Lately, I have been thinking a lot about the nature of life, what are the foundation blocks of life and things like that. It's remarkable how complex and on the other hand simple the creation is when you look at it. The miracle of life keeps us grounded when our imagination goes wild. If the DNA are the blocks of life, you could consider them to be an API nature provided us to better understand all of this chaos masquerading as order.
27
28I have been reading a lot about superintelligence and our somehow misguided path to create general artificial intelligence. What would the building blocks or our creation look like? Is the compression really the ultimate storage of information? Will our creation also ponder this questions when creating new worlds for themselves, or will we just disappear into the vastness of possibilities? It is a little offensive that we are playing God whilst being completely ignorant of our own reality. Who knows! Like many other breakthroughs, this one will also come at a cost not known to us when it finally happens.
29
30To keep things a bit lighter, I decided to convert some popular DNA sequences into an audio files for us to listen to. I am not the first one, nor I will be the last one to do this. But it is an interesting exercise in better understanding the relationship between art and science. Maybe listening to DNA instead of parsing it will find a way into better understanding, or at least enjoying the creation and cryptic nature of life.
31
32## DNA encoding and primer example
33
34I have been exploring DNA in the past in my post from about 3 years ago in [Encoding binary data into DNA sequence](/encoding-binary-data-into-dna-sequence.html) where I have been converting all sorts of data into DNA sequences.
35
36This will be a similar exercise but instead of converting to DNA, I will be generating tones from Nucleotides.
37
38| Nucleotides | Note | Frequency |
39| ---------------- | ---- | --------- |
40| **A** (Adenine) | A | 440 Hz |
41| **C** (Cytosine) | C | 783.99 Hz |
42| **G** (Guanine) | G | 523.25 Hz |
43| **T** (Thymine) | D | 587.33 Hz |
44
45Since we do not have T in equal-tempered scale, I choose D to represent T note.
46
47You can check [Frequencies for equal-tempered scale, A4 = 440 Hz](https://pages.mtu.edu/~suits/notefreqs.html). For this tuning, we also choose `Speed of Sound = 345 m/s = 1130 ft/s = 770 miles/hr`.
48
49Now that we have this out of the way, we can also brush up on the DNA sequencing a bit. This is a famous quote I also used for the encoding tests, and it goes like this.
50
51> How wonderful that we have met with a paradox. Now we have some hope of making progress.
52> ― Niels Bohr
53
54```shell
55>SEQ1
56GACAGCTTGTGTACAAGTGTGCTTGCTCGCGAGCGGGTACGCGCGTGGGCTAACAAGTGA
57GCCAGCAGGTGAACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGCTGGCGGGTGA
58ACAAGTGTGCCGGTGAGCCAACAAGCAGACAAGTAAGCAGGTACGCAGGCGAGCTTGTCA
59ACTCACAAGATCGCTTGTGTACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGTAT
60GCTTGCTGGCGGACAAGCCAGCTTGTAAGCGGACAAGCTTGCGCACAAGCTGGCAGGCCT
61GCCGGCTCGCGTACAAATTCACAAGTAAGTACGCTTGCGTGTACGCGGGTATGTATACTC
62AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC
63AACC
64```
65
66This is what we gonna work with to get things rolling forward, when creating parser and waveform generator.
67
68## Parsing DNA data
69
70This step is rather simple one. All we need to do is parse input DNA sequence in [FASTA format](https://en.wikipedia.org/wiki/FASTA_format) well known in [Bioinformatics](https://en.wikipedia.org/wiki/Bioinformatics) to extract single Nucleotides that will be converted into separate tones based on equal-tempered scale explained above.
71
72```python
73nucleotide_tone_map = {
74 'A': 440,
75 'C': 523.25,
76 'G': 783.99,
77 'T': 587.33, # converted to D
78}
79
80def split(word):
81 return [char for char in word]
82
83def generate_from_dna_sequence(sequence):
84 for nucleotide in split(sequence):
85 print(nucleotide, nucleotide_tone_map[nucleotide])
86```
87
88## Generating sine wave
89
90Because we are essentially creating a long stream of notes we will be appending sine notes to a global array we will later use for creating a WAV file out of it.
91
92```python
93import math
94
95def append_sinewave(freq=440.0, duration_milliseconds=500, volume=1.0):
96 global audio
97
98 num_samples = duration_milliseconds * (sample_rate / 1000.0)
99
100 for x in range(int(num_samples)):
101 audio.append(volume * math.sin(2 * math.pi * freq * (x / sample_rate)))
102
103 return
104```
105
106The sine wave generated here is the standard beep. If you want something more aggressive, you could try a square or saw tooth waveform.
107
108## Generating a WAV file from accumulated sine waves
109
110
111```python
112import wave
113import struct
114
115def save_wav(file_name):
116 wav_file = wave.open(file_name, 'w')
117 nchannels = 1
118 sampwidth = 2
119
120 nframes = len(audio)
121 comptype = 'NONE'
122 compname = 'not compressed'
123 wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
124
125 for sample in audio:
126 wav_file.writeframes(struct.pack('h', int(sample * 32767.0)))
127
128 wav_file.close()
129```
130
13144100 is the industry standard sample rate - CD quality. If you need to save on file size, you can adjust it downwards. The standard for low quality is, 8000 or 8kHz.
132
133WAV files here are using short, 16 bit, signed integers for the sample size. So, we multiply the floating-point data we have by 32767, the maximum value for a short integer.
134
135> It is theoretically possible to use the floating point -1.0 to 1.0 data directly in a WAV file, but not obvious how to do that using the wave module in Python.
136
137## Generating Spectograms
138
139I have tried two methods of doing this and both were just fine. I however opted out to use the [SoX - Sound eXchange, the Swiss Army knife of audio manipulation](https://linux.die.net/man/1/sox) one because it didn't require anything else.
140
141```shell
142sox output.wav -n spectrogram -o spectrogram.png
143```
144
145An example spectrogram of Ludwig van Beethoven Symphony No. 6 First movement.
146
147<audio controls>
148 <source src="/dna-synthesized/symphony-no6-1st-movement.mp3" type="audio/mpeg">
149</audio>
150
151![Ludwig van Beethoven Symphony No. 6 First movement](/dna-synthesized/symphony-no6-1st-movement.png)
152
153The other option could also be in combination with [gnuplot](http://www.gnuplot.info/). This would require an intermediary step, however.
154
155```shell
156sox output.wav audio.dat
157tail -n+3 audio.dat > audio_only.dat
158gnuplot audio.gpi
159```
160
161And input file `audio.gpi` that would be passed to gnuplot looks something like this.
162
163```
164# set output format and size
165set term png size 1000,280
166
167# set output file
168set output "audio.png"
169
170# set y range
171set yr [-1:1]
172
173# we want just the data
174unset key
175unset tics
176unset border
177set lmargin 0
178set rmargin 0
179set tmargin 0
180set bmargin 0
181
182# draw rectangle to change background color
183set obj 1 rectangle behind from screen 0,0 to screen 1,1
184set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "#ffffff"
185
186# draw data with foreground color
187plot "audio_only.dat" with lines lt rgb 'red'
188```
189
190## Pre-generated sequences
191
192What I did was take interesting parts from an animal's genome and feed it to a tone generator script. This then generated a WAV file and I converted those to MP3, so they can be played in a browser. The last step was creating a spectrogram based on a WAV file.
193
194### Niels Bohr quote
195
196<audio controls>
197 <source src="/dna-synthesized/quote/out.mp3" type="audio/mpeg">
198</audio>
199
200![Spectogram](/dna-synthesized/quote/spectogram.png)
201
202### Mouse
203
204This is part of a mouse genome `Mus_musculus.GRCm39.dna.nonchromosomal`. You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/mus_musculus/dna/).
205
206<audio controls>
207 <source src="/dna-synthesized/mouse/out.mp3" type="audio/mpeg">
208</audio>
209
210![Spectogram](/dna-synthesized/mouse/spectogram.png)
211
212### Bison
213
214This is part of a bison genome `Bison_bison_bison.Bison_UMD1.0.cdna`. You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bison_bison_bison/cdna/).
215
216<audio controls>
217 <source src="/dna-synthesized/bison/out.mp3" type="audio/mpeg">
218</audio>
219
220![Spectogram](/dna-synthesized/bison/spectogram.png)
221
222### Taurus
223
224This is part of a taurus genome `Bos_taurus.ARS-UCD1.2.cdna`. You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bos_taurus/cdna/).
225
226<audio controls>
227 <source src="/dna-synthesized/taurus/out.mp3" type="audio/mpeg">
228</audio>
229
230![Spectogram](/dna-synthesized/taurus/spectogram.png)
231
232## Making a drummer out of a DNA sequence
233
234To make things even more interesting, I decided to send this data via MIDI to my [Elektron Model:Samples](https://www.elektron.se/en/model-samples). This is a really cool piece of equipment that supports MIDI in via USB and 3.5 mm audio jack.
235
236Elektron is connected to my MacBook via USB cable and audio out is patched to a Sony Bluetooth speaker I have that supports 3.5 mm audio in. Elektron doesn't have internal speakers.
237
238![](/dna-synthesized/elektron/IMG_0619.jpg)
239
240![](/dna-synthesized/elektron/IMG_0620.jpg)
241
242![](/dna-synthesized/elektron/IMG_0622.jpg)
243
244For communicating with Elektron, I choose `pygame` Python module that has MIDI built in. With this, it was rather simple to send notes to the device. All I did was map MIDI notes to the actual Nucleotides.
245
246Before all of this I also checked Audio MIDI Setup app under MacOS and checked MIDI Studio by pressing ⌘-2.
247
248![](/dna-synthesized/elektron/midi-studio.jpg)
249
250The whole script that parses and send notes to the Elektron looks like this.
251
252```python
253import pygame.midi
254import time
255
256pygame.midi.init()
257
258print(pygame.midi.get_default_output_id())
259print(pygame.midi.get_device_info(0))
260
261player = pygame.midi.Output(1)
262player.set_instrument(2)
263
264def send_note(note, velocity):
265 global player
266 player.note_on(note, velocity)
267 time.sleep(0.3)
268 player.note_off(note, velocity)
269
270
271nucleotide_midi_map = {
272 'A': 60,
273 'C': 90,
274 'G': 160,
275 'T': 180, # is D
276}
277
278with open("quote.fa") as f:
279 sequence = f.read().replace('\n', '')
280
281for nucleotide in [char for char in sequence]:
282 print("Playing nucleotide {} with MIDI note {}".format(
283 nucleotide, nucleotide_midi_map[nucleotide]))
284 send_note(nucleotide_midi_map[nucleotide], 127)
285
286del player
287pygame.midi.quit()
288```
289
290<video src="/dna-synthesized/elektron/elektron.mp4" controls></video>
291
292All of this could be made much more interesting if I choose different instruments for different Nucleotides, or doing more funky stuff with Elektron. But for now, this should be enough. It is just a proof of concept. Something to play around with.
293
294## Going even further
295
296As you probably notice, the end results are quite similar to each other. This is to be expected because we are operating only with 4 notes essentially. What could make this more interesting is using something like [Supercollider](https://supercollider.github.io/) to create more interesting sounds. By transposing notes or using effects based on repeated data in a sequence. Possibilities are endless.
297
298It is really astonishing what can be achieved with a little bit of code and an idea. I could see this becoming an interesting background soundscape instrument if done properly. It could replace random note generator with something more intriguing, biological, natural.
299
300I actually find the results fascinating. I took some time and listened to this music of nature. Even though it's quite the same, it's also quite different. The subtle differences on repeat kind of creates music on its own. Makes you wonder. It kind of puts Occam’s Razor in its place. Nature for sure loves to make things as energy efficient as possible.
diff --git a/content/posts/2022-08-13-algae-spotted-on-river-sava.md b/content/posts/2022-08-13-algae-spotted-on-river-sava.md
new file mode 100644
index 0000000..2a7e6cd
--- /dev/null
+++ b/content/posts/2022-08-13-algae-spotted-on-river-sava.md
@@ -0,0 +1,24 @@
1---
2title: Aerial photography of algae spotted on river Sava
3url: aerial-photography-of-algae-spotted-on-river-sava.html
4date: 2022-08-13
5draft: false
6---
7
8This is a bit of a different post than I usually write, but quite interesting one to me. River Sava has plenty of hydropower plants located down the stream. This makes regulating the strength of a current easier than normally. Because of lower stream strength and high temperatures, algae has formed on the river. This is the first time I've seen something like this in my whole life.
9
10Below are some photographs taken from a DJI drone capturing the event.
11
12![Algae on Sava](/algae-sava/dji-algae-0.jpg)
13
14![Algae on Sava](/algae-sava/dji-algae-1.jpg)
15
16![Algae on Sava](/algae-sava/dji-algae-2.jpg)
17
18![Algae on Sava](/algae-sava/dji-algae-3.jpg)
19
20![Algae on Sava](/algae-sava/dji-algae-4.jpg)
21
22![Algae on Sava](/algae-sava/dji-algae-5.jpg)
23
24I will try to get more photos of this in the future days and if something intriguing shows up will post it again on the blog.