Blog


Dec

The Year 2015: A Summary


I missed my annual recap last year, mostly because I was tired after a very stressful year, but it’s time to pick up the tradition again. Here are some of the highlights from 2015.

Client projects

We had another year of great client collaborations. During the first half of the year we worked on several interesting projects together with Purpose, including a social media monitoring dashboard for the climate change awareness campaign Here Now, the One Second March campaign, and other work for non-profits like the Bill & Melinda Gates Foundation and Oxfam. We also helped our friends at Oktavilla with some work on UNICEF.se.

We were also fortunate to work with great companies like Telenor Connexion with their Internet-of-Things (IoT) platform, and Blue Box with their private cloud solution. Both of those projects are continuing next year too.

We continued working with some of our exciting startup clients, like Naturkartan and Pinracer. We also worked with FootballAddicts on our first real Elixir project. We hope to do more Elixir in 2016, so get in touch if you need any Elixir help.

Naturkartan

Towards the end of the year we got into online newspaper design with Mitt i, something we're also hoping to do more of in 2016.

ProjectPuzzle

In between client projects we finally found time to launch a brand new version of our team scheduling SaaS product ProjectPuzzle. We’ve continued to improve it throughout the year, and we have much more planned. For me personally, it is completely indispensable to running the Elabs consulting business. It let’s me see our utilization at a glance, and keep track of who’s working on what project and when. And with the improvements we’re making, pretty soon it will be a full solution to managing a client services business.

ProjectPuzzle

Flip

During Lab Days and downtime between client projects Anders and Robin created our first own iOS app: Flip, a beautiful version of the classic board game Reversi. Flip is free on the App Store, give it a try!

Flip Reversi game

Journeyman tour visit

In June we had a visit from Lennart who spent a week with us as part of his journeyman tour. Lennart is always a vitamin injection, and we really enjoyed having him with us. We finished the week with a Lab Day where we did some Elixir and Phoenix mob programming. Great fun!

Lennart wrote a blog post about his journeyman visit with Elabs, you should read it.

Lennart's visit

Nordic Ruby and True North reunion

After running conferences for 5 years straight we decided to take a break during 2015 to catch our breath a bit. We couldn’t stay away from our regular venue Yasuragi completely though, so in early July we held at Nordic Ruby and True North reunion where we invited past attendees to come hang out with us in the serene Japanese spa in the Stockholm archipelago. To our delight, we had quite a few attendees come from as far away as the US to spend the weekend with us!

Reunion at Yasuragi

While it was nice to take a break and focus on our core business, we really miss organizing a conference. There’s a good chance that we’ll do one in 2016. Follow @elabs on Twitter for updates!

Team changes

The biggest changes this year were that we said goodbye to two of our team members.

Johannes—one of our designers—left in July after 5 great years with us to work on a new product idea he had.

Johannes

Kim, who joined us as a developer 4 years ago, left right before Christmas to move back to Stockholm where his family lives.

Kim

We’ll miss them both and wish them the very best of luck!

It wasn’t all goodbyes though! In November we welcomed Max, our second apprentice. Max has been doing great so far, and we’re excited to have him on the team. He introduced himself here on the blog a few weeks ago.

What’s next?

As I’ve hinted at on Twitter, we have some very exciting things in the works for next year! I won’t give it away quite yet, but you should follow us on Twitter if you want to be among the first to know.

I can’t wait for 2016 to begin!

Happy New Year!

/ CJ

Dec

A Christmas tree formatter for RSpec!


It's December, a time of glögg and gingerbread cookies with blue cheese… and Christmas trees!

Me and Anders figured we'd bring some Christmas spirit into our test suite, so we created an RSpec formatter very much like the regular progress formatter with green dots… except it's a tree!

Source code is on GitHub, v0.1, and a nasty piece of code, but it's December so it's very much time to ship it! It's named christmas_tree_formatter.

Here's a GIF of it in action:

Imgur

Dec

The Apprentice


I am not the new apprentice of Sir Alan Sugar, as in the TV series The Apprentice. But I am the new apprentice of Elabs, and for that I am very grateful! The first week is soon completed and wow, I already love this place. A spectacular office with awesome people. Everyone here has been treating me so nicely. Except that they all seem to totally crush me in ping-pong. Maybe that is how it’s supposed to be though for a rookie.

Anyhow, my name is Max Perzon and I am a 24 year old web developer student from the school YRGO here in Gothenburg. I am spending the last six months of my education out in the “field” as an apprentice, and that is why I am here at Elabs. What excites me the most about being here is the opportunity I get to be around people way above my skill level for six months. Exposing my ignorance and learning as much as I can by osmosis. Seeing how they attack obstacles and solve problems together. There is no doubt in my mind that I will learn a lot from this experience.

I live together with my girlfriend Åsa in a small apartment here in Gothenburg. She’s from northern Sweden, Östersund. So we like to ski quite a lot together. Other big interests of mine are reading books, running and traveling. I also like dogs, they are awesome.

To round this up, I just want to say thank you to everyone in the team here for welcoming me in such a nice way. It means a lot. I’m looking forward to continuing to get to know each and everyone of them better as time passes. And also, hopefully “steal” some of the secret weapons in their coding arsenal.

Over and out.

— Max Perzon

Nov

Retrieving the last N ordered records with ActiveRecord


Here's a database of chat messages.

|  id  | created_at |
|——————|————————————|
|   1  |   13:20    |
|   2  |   13:21    |
|   3  |   13:22    |
|   …  |   ……………    |
|  121 |   14:27    |
|  122 |   14:29    |
|  123 |   14:32    |
|——————|————————————|

So, we're building a chat, and what we'd like to see is the 10 most recent messages, with the oldest of the bunch at the top, and the newest message at the bottom, something like: [<Message 113>, <Message 114>, <Message 115>, …, <Message 123>].

At first, you might think "I'll just sort all messages by created_at in ascending order, and take the last 10". OK, here's what that looks like.

Message.order(created_at: :asc).last(10) # => [<Message 113>, <Message 114>, …, <Message 123>]

Looks good? Yes? No. Have a look at the SQL:

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" ASC

   |  id  | created_at |
   |——————|————————————|
-> |   1  |   13:20    |
-> |   2  |   13:21    |
-> |   3  |   13:22    |
-> |   …  |   ……………    |
-> |  121 |   14:27    |
-> |  122 |   14:29    |
-> |  123 |   14:32    |
   |——————|————————————|

What, no mention of 10 in our SQL query?! .last is not so clever. We end up loading all messages in our database to Ruby, maybe a few hundred thousand, and then we throw away all messages except for the last 10, what a waste.

Okay, how about using OFFSET? Let's try.

Message.order(created_at: :asc).offset(Message.count - 10).limit(10) # => [<Message 113>, <Message 114>, …, <Message 123>]        

Looks good? Yes? No. Let's look at the SQL:

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" ASC OFFSET 112 LIMIT 10

     |  id  | created_at |
     |——————|————————————|
SKIP |   1  |   13:20    |
SKIP |   2  |   13:21    |
SKIP |   …  |   ……………    |
  -> |  113 |   14:13    |
  -> |   …  |   ……………    |
  -> |  123 |   14:32    |
     |——————|————————————|

A few important notes about this.

  1. OFFSET in SQL must be a positive number so we can't simply use -10 as our offset.
  2. ActiveRecord will try to coerce our offset to an integer using #to_i, so we can't pass a subquery as our offset, which leaves us with precomputed positive numbers only.
  3. What is our offset? It must be calculated ahead of time using Message.count, this is slow, and prone to race conditions if we get more messages in between our count and select.

Even if we disregard all the above points, have a look at the documentation for LIMIT/OFFSET in PostgreSQL: "The rows skipped by an OFFSET clause still have to be computed inside the server; therefore a large OFFSET might be inefficient."

Oh, OK, so offset is off.

One more try. Let's sort this out with a new mindset. We change the order of our SQL, and reverse the thing in Ruby. That must work!

Message.order(created_at: :asc).reverse_order.limit(10).reverse # => [<Message 113>, <Message 114>, …, <Message 123>]

Looks good? Yes? Let's look at the SQL.

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" DESC LIMIT 10

   |  id  | created_at |
   |——————|————————————|
-> |  123 |   14:32    |
-> |   …  |   ……………    |
-> |  113 |   14:13    |
   |——————|————————————|

Well, this is actually quite OK. We only retrieve 10 records from the database, which is what we want. Sure, the order of the messages is wrong which is kind of sad, but we can fix that later. Let's make a scope of this and call it a good day!

class << Message
  def in_order
    order(created_at: :asc)
  end

  def recent(n)
    in_order.reverse_order.limit(n).reverse
  end
end

There's a few downsides with this, can you spot it?

  1. We have to reverse it in Ruby.
  2. The return value is an Array, and not an ActiveRecord::Relation, since we force it with reverse.
  3. We can't merge an Array with other scopes.
  4. We can't chain additional SQL conditions to the end of our array to further filter the 10 results, e.g. Message.recent(5).where(…).

We can still do better!

What if I told you there is a way to reverse the result in SQL, and that there's also an OK way to do so with ActiveRecord? I'm sure you'd believe me after all of this, anything else would be cruel.

class Message
  class << self
    def in_order
      order(created_at: :asc)
    end

    def recent(n)
      in_order.endmost(n)
    end

    def endmost(n)
      all.only(:order).from(all.reverse_order.limit(n), table_name)
    end
  end
end

Looks good? Yes! Let's use this and have a look at the SQL, Message.recent(10):

SELECT "messages".* FROM (
  SELECT  "messages".* FROM "messages"  ORDER BY "messages"."created_at" DESC LIMIT 10
) messages  ORDER BY "messages"."created_at" ASC

   |  id  | created_at |
   |——————|————————————|
-> |  113 |   14:13    |
-> |   …  |   ……………    |
-> |  123 |   14:32    |
   |——————|————————————|

This is exactly what we want, and it has none of the downsides of reversing the results in Ruby. The final .recent method is our final implementation, and it works as expected.

The keen eye will notice that I extracted part of the logic into a general-purpose .endmost method. .endmost is what you want when you call .last: the last N records in the result set without having to retrieve all records from the database, and it works with any ordering. You can impose filtering before, Message.where(…).in_order.endmost(10), and afterwards to filter your final results as well Message.in_order.endmost(10).where(…).

Thanks for reading! I hope you found it as useful as I did!

Sep

Introducing Serial, a light-weight no-magic serialization library (for Ruby, and Rails)


Jonas and I created a serialization library recently while working on ProjectPuzzle, it's named Serial.

Serial will generate a Hash or an Array of hashes from an object of your choosing. It has a very small API surface, and is designed to be easy to reason about. It's suitable for where you'd use YourModel#as_json, ActiveModel::Serializers, or JBuilder. It could look something like this:

# app/serializers/person_serializer.rb
PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:id, person.id)
  h.attribute(:name, person.name)
  h.attribute(:url, account_person_path(person.account, person))
  h.attribute(:assignable, policy(person).assignable?)
  h.attribute(:skills, person.skills.map(&:name))
  h.map(:groups, person.groups, &GroupSerializer)
end

# app/controllers/api/people_controller.rb
include Serial::RailsHelpers
def index
  people = People.all
  render json: { people: serialize(people) }
end

You're very welcome to have a look, you can find it at github.com/elabs/serial, we'd love to hear what you think!