alisinabh.com

defmodule Alisinabh.Blog do

def Jalaali_calendar_for_elixir do # 2017-9-21T23:37:57Z

Hi everyone.

Today i released jalaali package version 0.2 for elixir.

It offers a new module Jalaali.Calendar which is an implementation of elixir’s Calendar module.

With help of this it is possible now to convert DateTimes to each other easily by using DateTime.convert or Date.convert functions.

Long story short

The previous version of jalaali was a mess! really.

It just provided functions to users for converting from/to ISO calendar.

The most annoying thing was it actually did not change the calendar property in a Date/DateTime struct.

Even if it did it wouldn’t be a useful feature cause conversion was not possible throw Elixir < 1.5

So after a very long talk in community about this which you can read here use of day number + day fraction was chosen to implement. This decision has made elixir one of rare languages with a very very very strong native calendar system.

It’s been a while since elixir 1.5.x was released and i’ve been really excited about adapting my jalaali library with the new Calendar system in elixir.

At first i thought it may take me several days to make that happen, BUT surprisingly it was so easy that it only took me less than 5 hours (including tests and documentations) and jalaali 0.2 is now published.

I really liked it and i may try adapting other calendar systems for elixir as well just for fun of it.

btw, i will be happy if you take a look at jalaali github.com/jalaali/elixir-jalaali and maybe star it?

Thank you

{:ok, :happy_coding}

comments()

end

def Elixir_1.5 do # 2017-8-6T19:37:1Z

Recently Elixir 1.5 has been released. Release Notes About 1.5

After a quick look on release notes you will notice that these updates, improvements and bugfixes are not major, Meaning Elixir is getting mature.

The most major change in my opinion is the Calendar module. Most of the callbacks to calendar module is changed. The reason to this change is because the Calendar conversion was impossible in Elixir <1.5

So to tackle this issue community decided (This is really pleasing for me to say that “Elixir community decided”) to use Ratta die algorithm for Calendar conversions. And that is a nice move because conversions of different chronologies in Elixir are now super accurate.

If you are interested in reading more about these changes you can visit Elixir docs. Link

Other than this the majority of changes in this release are “IEx Improvements”, “@impl for callbacks” and ability to set breakpoints on functions and modules (which is in favor of OTP/20)

I’m really excited about the future of Elixir and Erlang in our concurrent world.

Thank you for reading this.

{:ok, :lets_party_یوتی_اف8}

Wait! Did i forgot to mention UTF-8 atoms? :O

comments()

end

def Old_code_backfire! do # 2017-6-24T4:8:21Z

As a daily programmer who lives by programming as a profession, Sometimes projects are not cool and they don’t cheer you up.

Specially if they are you OLD CODES -__-

Your old projects can kill you when they are still in use. Every day you learn more and they that code makes less sense. But sometimes things go wrong when you realize that the code is bad an unreliable AT THE SAME TIME!

For instance let me tell you my story about one.

I’ve developed an Asterisk AGI server with .net and AsterNET. AsterNET does’t use the async feature of .net and uses a Thread Pool.

So if you need 500 calls, you’ll need 500 threads!

We’ve been migrated our new projects to a new server in last two years but our old projects are still running on AsterNET server. The problem with that server is it will crash from time to time. Because of StackOverflow exceptions. The old projects are not valuable enough to fix and not so useless to just go on.

So, I came up with a fairly good solution. Using a process supervisor (AKA Safe process runner).

It will simply run the process and wait for it to exit. If so, SafeProcess will re-run the process.

It’s not the perfect solution but it will buy me time and because this exception happens rarely (once or twice a month on average traffic) we can ignore it.

SafePorcess is available to use for everyone under no license: https://github.com/alisinabh/SafeProcess

{:ok, :be_safe}

comments()

end

def Prex:Elixir_Api_client_code_generator do # 2017-6-17T0:48:29Z

What you up to?

Wow, It’s my second elixir project in a about a week after numero. (Yayy!)

I don’t know why i become creative when my exams start :|

So what is it?

Prex is client code generator in elixir for whom is going to use a well documented API.

It can help developers save time and focus on their own issues instead of coding the API client.

Prex know ApiBlueprint language and can convert .apib files to elixir code.

Prex: github.com/alisinabh/prex

ApiBlueprint: apiblueprint.org

How you got idea?

Anyways, In last month i was dealing with REST Api services mostly from client perspective and i realized how much people don’t care about documentation of their APIs. That’s Crazy! There was even one company who hadn’t any docs at all! They just sent me their real-time generated docs (seriously?) as we were talking on messenger and almost all of it were WRONG!

Especially most .NET developers in my country care much less about documenting. (I was a .net developer for 7 years. I know what i’m talking about)

So what is the reason?

I think the first reason for that is education!

In colleges in IRAN there is absolutely no courses for documentation and if there is it’s fairly outdated.

The second reason i think is the ecosystem. For example .NET ecosystem won’t encourage a programmer to write good documents. There is no proper ways defined for documentations in that community. Even in C# learning videos no one talk about documentation in new features.

But take Elixir for example. It has a nice way of documenting your code. Its super friendly and in most Elixir tutorials they tech newbies about documentation and how important they are.

How is Prex?

I’ve only started working on Prex for two days. But its fairly usable now since Prex itself is not going directly into your compiled code. It just generates code for you that you can customize and edit.

I’m planing on completely supporting ApiBlueprint data and adding tests based on examples in blueprint files. More importantly Prex is going to support SOAP and WSDL. :D

Can you show us a sample?

Consider this example blueprint: real-world-api.apib

After running mix prex.gen.from_blueprint real-world-api.apib example Prex is going to generate following for you:

lib/
└── example
   └── posts.ex

example/post.ex :

# Created by Prex
defmodule Example.Posts do
  @moduledoc """
  This section groups App.net post resources.
  """

  @base_url "https://alpha-api.app.net"

  ###
  # API Calls
  ###

  # Post

  @doc """
  Returns a specific Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def retrieve_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:get, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def retrieve_a_post!(postid \\ "") do
    {:ok, result} = retrieve_a_post(postid)
    result
  end

  @doc """
  Delete a Post. The current user must be the same user who created the Post. It
returns the deleted Post on success.

  ## Parameters
    - postid: The id of the Post.
  """
  def delete_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def delete_a_post!(postid \\ "") do
    {:ok, result} = delete_a_post(postid)
    result
  end

  # Posts Collection

  @doc """
  Create a new Post object. Mentions and hashtags will be parsed out of the post
text, as will bare URLs...
  """
  def create_a_post do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:post, req_url)
  end

  def create_a_post! do
    {:ok, result} = create_a_post()
    result
  end

  @doc """
  Retrieves all posts.
  """
  def retrieve_all_posts do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:get, req_url)
  end

  def retrieve_all_posts! do
    {:ok, result} = retrieve_all_posts()
    result
  end

  # Stars

  @doc """
  Save a given Post to the current User’s stars. This is just a “save” action,
not a sharing action.

*Note: A repost cannot be starred. Please star the parent Post.*

  ## Parameters
    - postid: The id of the Post.
  """
  def star_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:post, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def star_a_post!(postid \\ "") do
    {:ok, result} = star_a_post(postid)
    result
  end

  @doc """
  Remove a Star from a Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def unstar_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def unstar_a_post!(postid \\ "") do
    {:ok, result} = unstar_a_post(postid)
    result
  end

end

Have fun integrating with APIs :)

{:ok, :api_is_fun!}

comments()

end

def handling_non-standard_utf-8_digits_in_elixir_like_a_boss! do # 2017-6-2T1:44:47Z

Hey everyone.

Recently i was so busy with my job and college so i had no time for writing here. I apologize to you.

In one of the projects we’ve needed an http service for some kind of software activation.

Although we did’t have a decent time for that project i’ve decided to build that service using phoenix framework. And i decided to make it open sourced!

Project was a nice experiment for me on API’s using elixir and phoenix in production. However i came to a simple problem that i could handle more easily with .NET

The problem was that some users entered their license number in the app using their native language keyboard and as you know, there are lots of different numbers in utf-8 table. In .NET you can simple resolve this issue using char.GetNumericValue, but in elixir i didn’t found any reasonable easy solutions for that. So i’ve decided to make a micro library for that matter.

I’ve named it ‘Numero’ which means Number in Spanish (I don’t know Spanish at all!)

With numero you can normalize number chars in a string or parse a string to number (Integer or Float). Numero is also smart on number types. if your number has fraction points in its string Numero will return you a float, otherwise it will hand you your number as Integer.

Usage Example

result = Numero.normalize("1۲۳۰4a۳tس")
# result = "12304a3tس"

result = Numero.normalize_as_number("1۲۳۰4۳") # Strings without fraction points return Integer
# result = {:ok, 123043}

result = Numero.normalize_as_number("1۲۳۰4۳.۴5") # Strings with fraction points return Float
# result = {:ok, 123043.45}

result = Numero.normalize_as_number!("1۲۳۰4۳.۴5") # Return number as result
# result = 123043.45

Installation

You can visit numero here: https://github.com/alisinabh/Numero (FORK ME!)

Or install it in your projects with:

def deps do
  [{:numero, "~> 0.1.2"}]
end

Thank you for reading this.

comments()

end

def X_window_multi_monitor_brightness_control do # 2017-4-24T6:35:46Z

Not so long ago, I’ve decided to move from Windows to Arch for both my office and home PCs.

Why?

Because i loved Elixir so much and it works much better and easier on linux.

Why arch?

Two reasons:

  1. It’s a rolling release, means i always can use latest version of software unlike Debian.
  2. AUR man. B) AUR ROCKS!!!

You mean Debian is bad?

Absolutely not, Debian is perfect choice for servers. Indeed debian is super robust!

It just limits you as a home user.

Tell me more

I’ve finally managed to get arch up and running after three failures thanks to Nvidia’s lack of support for linux. It was my mistake to install proprietary drivers. Everything was OK with open sourced drivers provided by community.

My station setup for both home and work has two 21.6 monitors. When i first learnt about Night-shift i was using Windows. Back in windows everything was nicely handled with f.lux. But flux lack good user interface in linux. after searching just a bit i found Redshift it is really nice. it lacks users interface but seriously a linux user wont need that, will they?

Everything was fine with Redshift even it does fake brightness adjustment BUT my problem was when i wanted to turn Redshift off at nights, it would also turn off brightness settings. So I’ve made a script to have two presets and even adjust brightness by fractions like f.lux.

The script is available here: https://gist.github.com/alisinabh/9b0508e995431bb32be696663815be39

Just remember to change DVI-I- 1 or 2 with your display port names.

You can find them by running xrandr -q command.

Thank you for reading

{:ok, :controll_is_illusion}

comments()

end

def Daylight_saving_headache do # 2017-4-9T21:5:23Z

Daylight saving headache!

Not so long ago, I’ve ported jalaali calendar for Elixir. In that process i’ve figured out Elixir’s standard Date structure is missing something to accomplish calendar conversions. So i’ve started a conversation in Elixir forum which you can read here

My idea was to force every calendar to provide callbacks to convert specific Date to ISO date or Unix Epoch (Which i liked more at that time!)

Then… I was intruduced to Date and DateTime complexities!

Calendars were not as easy as i thought it was. I even didn’t know that we have Leap seconds in some years!!!

A leap second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC) in order to keep its time of day close to the mean solar time, or UT1. Without such a correction, time reckoned by Earth’s rotation drifts away from atomic time because of irregularities in the Earth’s rate of rotation.

The brilliant people in elixir community, @josevalim, @kip, @Qqwy came up with a nice solution using Ratta die. It is awesome an i think Elixir has one of the best native calendar implementation.

Anyways, I didn’t learn my lesson yet at that time. :(

So i begun to use epoch millisecond as time in one of enterprise projects. We needed an offline TimeTable to handle timings of events in our application. So we designed ZigorTimeTable

Each time table entry uses a startDate::DateTime as beginning of time, A duration::Long in milliseconds as time of availability and a cycle::Long in millisecond as how often is this entry repeated.

Everything was awesome and fine. It worked like a charm! You understand? Worked!

And suddenly, Daylight saving time occurred! :(( Every time table was messed up. they were all behind by one hour.

Reason?

because of DST+1 (Day light saving) there was one hour missing from DateTime but it wasn’t missing in milliseconds!

We had to change real times from server to wrong ones so the app would work correctly.

Conclusion

Please do not mess with calendar and calendar systems in your enterprise level projects. these methods should be tested and verified precisely over long periods of time and not by normal tests. Instead, try to use a known implementation like JodaTime via updated TZDB data.

{:error, :sick_of_this_shit!}

comments()

end

def DIY_3D_printer_with_CD_Roms_build do # 2017-4-2T10:52:6Z

Today is last day of Nowruz holidays in Iran.

Having Fun?

I didn’t write much code in holidays. I’ve decided to spend more family time in holidays and have some fun. But as you can guess, i could’t resist the Cheap 3D printer Project. It was so damn fun :D

What did you do?

I’ve successfully competed the build. (Yayyyyy!) The X, Y and Z axis are good. the platform is fair. Extruder is a bit weak! and the Hotend Rocks! Im Using an Arduino UNO as controller with grbl 1.1f (Which is a CNC controller software for atmega32)

What can it do?

grbl supports G-Code but grbl doesn’t know about Extruder and Hotend temperatures. It only knows spindle and its direction.

Sure you can use spindle enable as extruder controller BUT the tricky part is slicers does not know spindle G-Code. So i had to design a script that translate Reprap flavor G-Code into grbl G-Code. It still needs some work.

But Currently i can do fallow paths on my printer with help of MakerCam.com I have done stars, hand, rocket, and eiffel tower.

3D printed objects

Build size?

It is roughly 35x35x35mm

Extruder and hot end?

I have used a 3D printing pen as hotend and extruder. I have completely removed pen’s circuit, connected the hotend to the power supply and the extruder to arduino PWM pin of grbl (D11)

Matterial?

The pen came with it’s own 1.75mm ABS filament.

What about the housing?

Uh… Due to small build size i cannot print NanoPi housing with it. NanoPi NEO has dimensions of 40x40x40mm.

But i’m planning on building a bigger Reprap printer without the kit. If i can finish my works i may be able to do that.

Wasn’t it a waste of your time?

Absolutely not! I’ve learned so many things with this project and this is really the important part. learning everyday and having fun. it was a lot of fun finishing this project as i didn’t think i could do that at the very begining.

If you are interested in building this you can click HERE for instructions

Thank you for reading this

{:ok, :lets_3d_print!}

comments()

end

def NanoPi_NEO_Housing do # 2017-3-20T12:50:10Z

So in the last post Nanopi Neo as Webserver i came up to idea of serving alisinabh.com on a NanoPi Neo small arm based computer.

The latest things that i struggled with was:

  • Housing
  • Heatsync
  • Static IP

About the heatsync, I found an old asus mainboard which had a tiny heatsync near it’s south bridge. It fit perfectly. Even i used some thermal paste on back of board for better heat flow.

But about the housing… Things got out of hand. Seriously out of hand!

First i wanted to outsource printing of the normal NanoPi Neo housing (Since i cannot buy it from my location) But…

I really wanted a 3D printer for long time! Maybe buy one?

No shit! They’re really expensive and out of my budget.

So after some researching around 3D printers i ended up reading “Super Cheap 3D Printer From CD-Rom Drives”. Of course it is not going to be in any good shape or quality bit it can bdo the job i think.

So this is the reason for the delay of serving my blog on a NanoPi NEO board.

Thank you for reading this.

{:ok, :cheers!}

comments()

end

def NanoPi_Neo_as_Webserver do # 2017-3-11T15:9:37Z

This weekend i was really bored. I just thought maybe its a good idea to play with the nerves-project a bit, So i went to find my raspberry pi from the closet and oh boy, It was a giant mess :| Throw the process of finding the rpi_2 something caught my eye. A NanoPi NEO I bought it around a year ago to make an automatic garden keeper. but things went wrong after i sank it in mineral oil for cooling. the board went crazy and froze 10 seconds after bootup. I’ve wrapped it inside some paper towels and left it inside closet.

“Let’s try and see if it’s still broken.” I downloaded UbuntuCore with Qt-Embedded Image File for NanoPi NEO, flashed image into sd card and pluged it in. It worked!! Best said: “It’s better give time for some problems”

Anyways, it was stable. I liked the performance of Allwinner H3 SOC and it is absolutely a charming feeling watching such a tiny computer working with fair performance. Wait a minute… Can i host this blog on it?

This single seed of idea has taken my entire weekend. I first tried to install Erlang on it. but binaries for arm71 was old. I’ve downloaded otp source code from github and build it on NanoPi NEO. It took about 40 minutes to build. I was not disappointed. In fact, i was surprised because i thought it may take a couple hours.

Then i tried building Elixir from source code. It was all good until it became to compiling unicode. As @josevalim said in issue #5856 to me, compiling unicode module requires loading large amount of unicode tables and optimizing it and because i only have 512mb of RAM and no swap, OS killed the build process. He suggested using precompiled Elixir. (Why i didn’t thought of that?). And Oh boy. It worked.

Elixir 1.4.2 and OTP 19.2 were installed and working on NanoPi NEO.

Now i just needed to run my blog on it. After cloning mix phoenix.server didn’t let me down. Alisinabh.com has successfully built and run on NanoPi NEO (since i use flat files as database, i don’t need anything else.)

So after all, i decided to host alisinabh.com on my NanoPi NEO. there is only a couple things i need to worry about for doing so:

  • Get a housing and a heatsync for it.
  • Get an static IP address for my home internet connection or use DDNS (i don’t like this!)

{:ok, :unbelievable!}

comments()

end

def AWS_Outage! do # 2017-3-6T10:30:10Z

TL;DR: If you use an online service as storage or API it is always better to do Load balancing and failover within the Client side.

About a couple weeks ago we decided to use AWS S3 for storage of a mobile application which needed about 3gb of data dynamically.

We’ve created an AWS account and uploaded all files in a new S3 bucket. Everything was good, until… {:err, :aws_outage}

AWS outage happened. The service was down but fortunately the app was not released at that time.

Our CEO said: Ok, this can happen. What have we thought to prevent this? We did’t! We have not even thought about S3 getting unavailable for a second.

These things can happen. And whatever can happen will happen (Morphies law)

We are responsible for outages. Not Amazon. We could prevent this easily with having another backup bucket on a different IaaS or even on a cheap VPS just to prevent a complete outage.

After this occasion i realized how important is client level redundancy. It’s what we’ve been doing for API services to do the load balancing and failover on client side. It does not require any API gateways and in fact, It’s free of charge and works better if implemented correctly.

comments()

end

def knine_project do # 2017-3-3T17:14:8Z

Knine (pronounced K-9) is a tool for helping maintain of mid size services.

My very first motivation for Knine was about our chat service in one of our projects. unlike most of IM services our service didn’t use XMPP protocol. We have created a light-weight protocol over TCP called Zigor inspired by Telegram’s MTProto. In this structure we cared about every bit of waste and designed communication mechanism in a bitwise level. So one big advantage and of course, a lot of disadvantages and scary things!

One of those scary things was watching the cluster nodes health on a service that is not watchable easily. We could not just check for an open port, Instead we wanted running full diagnosys on protocol to see if it is not working the way it should.

On the other hand there were many other things that needed to be maintained like the charge we got left in our SMS service provider panel. we needed something to watch on that. For example wee needed it to tell us when the balance of the service is below a configured amount.

So Knine was the idea. A daemon that watches other services and can predict and help you prevent service outages.

{:still_working, :release_soon}

comments()

end

def Hello_world do # 2017-3-3T12:56:6Z

Hello and welcome!

This is the first post of this blog after developing is almost finished :D

Yes i know i could build a blog with no misery and of course for free! But i love elixir :) I built this blog with phoenix just to learn it and make something that really works on a daily basis for me with elixir.

Theme of this site is inspired from atom (my first favorite text editor) dark theme with elixir code.

This blog uses markdown text for posts and stores them in flat text files. (seriously?!) It is running on a vps with 512mb of ram and a weak shared cpu. I just want to see how BEAM reacts with this kind of hardware for myself.

This blog is Open Sourced and available at github.com/alisinabh/alisinabh.com

Thank you for visiting

{:ok, :feels_awesome!}

comments()

end

end

defmodule Alisinabh.Contact do

end