Current Work

Hello. Here is some current information about myself.

  • You can read about my professional history via my CV.
  • My current work involves building Backbone, React and Flux applications in JavaScript at my current company, Cxense.
  • I have long background in backend web development, but as suggested above, have been doing more frontend work.
  • I have some knowledge of Norwegian, given that I am residing in Norway now (self-taught, at a B1 reading level.)

My contact information is on the side of this page.

På norsk:

Hei. Her er en litt aktuelle opplysninger om meg.

  • Du kan lese om min faglig historie på min CV.
  • Jeg jobber nå i JavaScript og utvikler med Backbone, React og Flux.
  • Jeg jobbet i lang tid på backend web-utvikling. Men som jeg satt overfor jobber jeg nå mer frontend utvikling.
  • Jeg har noe kunnskap av norsk. Ettersom at jeg skrevet denne beskrivelsen på norsk kan det vise at jeg har denne kunnskap.
  • Jeg lærte meg norsk i fem år, på og av. Jeg leste aviser storsett og lytting av NRK nyheter på nett-tv og radio og nå gjennom bosatting her i Norge.

Kontakt opplysningen er på høyresiden av denne websiden.


On non-OOP Programming

I started to learn in bits and chunks about Clojure. While reading, it made me think how a non-trivial system in it will look. There are solutions to common problems, but it would be nice to see it manifest.

I plan to write more, but these are interesting links that I picked up on the topic.

I was interested in answering your question : “How would you write code in Clojure?” Or, how would you write a really big system in it.

http://blog.ionelmc.ro/2014/09/22/ramblings-about-data-structures/

This is about dealing with the balance of the utility of a specific data structure/class vs. a general data structure, such as a list, and the engineering balance/consideration you have to deal with

http://www.smashcompany.com/technology/object-oriented-programming-is-an-expensive-disaster-which-must-end

This is a very long and very long-winded diatribe against OOP. I like reading diatribes against OOP, because they are geeky fun reads. But he had some interesting thoughts along the way; his counter-examples all use Clojure, so this got me into looking into Clojure more.

http://loup-vaillant.fr/articles/deaths-of-oop

An article on the many ways OOP is failing.

Application architecture in Clojure: http://matthiasnehlsen.com/blog/2014/09/24/Building-Systems-in-Clojure-1/ http://matthiasnehlsen.com/blog/2014/10/06/Building-Systems-in-Clojure-2/ http://matthiasnehlsen.com/blog/2014/10/30/Building-Systems-in-Clojure-3/ http://matthiasnehlsen.com/blog/2014/11/07/Building-Systems-in-Clojure-4/

These articles are a little tough, since it tackles complex topics, but it’s interesting nonetheless.

https://github.com/stuartsierra/component

This library addresses large-system dependency injection in Clojure applications. The README in it is interesting, in perspective.


Blargh blargh and some more blargh

Nothing to see here!


Using An Ssh Reverse Proxy To Serve Web Content From Your Local Computer

There are probably better ways to do this, but I found this to work.

I need to test a Twilio API right now. Whenever I do this, I have to deal with the situation where the Twilio sandbox server has to talk to my development computer. I do not want to constantly deploy my application to some server, so proxying the request is the easier approach.

The first time I did this, I was using Swiftiply and it worked fine. However, I tried to install it today in Xubuntu and it didn’t immediately compile. Since I do not want to spend time working on getting Swiftiply to compile properly, I decided on something else. After some thought, I realized I can effectively do this with Nginx and a SSH reverse proxy.

The man page for ‘ssh’ tells us that:

“Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side. This works by allocating a socket to listen to port on the remote side, and whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the local machine.”

It says to use this syntax:

-R [bind_address:]port:host:hostport

So, to start, we want to make sure that we fork a new SSH connection and not execute remote commands (by default, this would be bash, I think.)

ssh -fNR 56789:localhost:3000 user@example.com

56789 is the port that the example.com server would connect to and 3000 is the local web server port, on your computer.

Then, all that is needed on the remote (public) server is an Nginx configuration. Here is a snippet:

upstream test_proxy {
    server 127.0.0.1:56789;
}

Test proxy frontend

server {
    listen 9000;
    server_name _;
    client_max_body_size 4G;
    keepalive_timeout 70;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_buffering on;

    location / {
        proxy_pass http://test_proxy;
    }

    location @test_proxy {
        internal;
        proxy_pass http://test_proxy;
        break;
    }
}

When you turn on Nginx on the public server and turn on your server, then you can hit http://example.com:9000, and you should see the content from your local server.


Solving Reporting Problems With Postgresql

Solving Reporting Problems Simply With A Little SQL

[I originally wrote this for engineering.beenverified.com. Just bringing it over here.]

I am always satisfied when I am able to solve a complex problem using SQL. This is because SQL allows breaking down problems much easier than writing a program. Being based on relational algebra helps, in that you can reduce your problem as a relationship between different sets of data.

At one point in my career, I was writing a lot of SQL. Since writing reports is not much of a thrill, I made it worthwhile by stretching my imagination on how to solve problems with it. Even today, I was impressed what I came up with; the data requests sound intimidating, but if it were a test, I’d at least get a A- (I didn’t care much about how the queries performed.)

I am not writing much SQL at this point. However, I know that the tool is there at my disposal when I really need to solve thorny data requests. I am writing this because I wanted to address one such scenario, with the hope that people can see what I see when it comes to the expressive power of SQL.

The problem that brought me to write about this.

Basically, if I make a phone call, I record when it starts and when it ends. I need to find how many calls happen concurrently, and in particular the maximum number of calls that happen concurrently.

Finding out how many calls happen concurrently was the request. I initially freaked out: what does this mean? The day before I started this, however, I came up with an idea on how to start progressing with it. The kernel of a solution involved two sets of time ranges.

PostgreSQL 9.2 introduces range types, which make these types of queries easier. Unfortunately, I had 9.1 installed on the server and on my dev computer. So I had to start off by building a new version of PostgreSQL and then dumping the data in a new database to just start on this project. Of course, nothing comes easy.

First iteration

I started out by defining what data I am working with. Earlier, I said I wanted to start with two sets of time ranges. For the first iteration, I defined them as:

A. The context of time ranges that I am working off of, B. The time range that happened in context to each item within the first set.

My first solution was for both sets to be the same exact pieces of data. It made sense at first. The logic was that for a call in set A, is there any calls in set B that happen within the A call?

For example, a call lasted 5 minutes between 1pm and 1:05pm. What calls happened during that time?

This can be addressed in psuedo-SQL as:

 select
    A.time_range, count(B.time_range)
 from
    call_session_time_range A inner join call_session_time_range B on
                            A.time_range @> B.time_range and A.time_range <> B.time_range
 group by
    A.time_range;

(Note: call_session_time_range is a view over a table, that creates a time range from each row with their created_at and updated_at fields. For our purposes, it doesn’t matter how its implemented.)

The query says, get two sets, A and B, where A = B, and for each row in A, find a row in B where the time range is in the row from A.

What was interesting was that I saw this pattern develop:

A has (B, C, D) B has (A, C, D) C has (A, D)

or, in English, A had 3 calls in its context, B had 3 calls in its context and C had 2 calls in its context. It also means that A can have B and B can have A. I was going to work with that, but it looked weird. Really weird. Like, there was something not right about it but I couldn’t put my finger on it. The real problem is that this told me nothing useful in relation to the original question.

So great, B happened during A. What does this prove?

In fact, when I ran this query over a large data set I got a maximum result of 199 calls in one time period. Initially I thought this was interesting. However, as I thought about it, it was actually garbage data. That A call could have lasted an hour!

Second iteration

I decided to change the way I thought about the first set of data. The word “concurrent” was what drove the first iteration, but “concurrent” is not possible with my data set. What is a better set of data to work with then? I decided, instead, to create a table of “timeslices”, with each range one minute in length.

In this instance, we are seeing instead: How many calls there were in a single minute? Not exactly “concurrent”, but can produce more usable data, I think.

So, to generate that first set, the context set, I wrote a PL/pgSQL function to fill a temporary table:

   create temporary table timeslices (
          timeslice tsrange
   );


    create or replace function gen_timeslices(start_time timestamp, increment interval) returns void as $func$

    declare
    	new_date timestamp := start_time;
        begin
        	while new_date <= now() loop
            		insert into timeslices (timeslice) values(tsrange(new_date, new_date + increment));
                    new_date := new_date + increment;
            end loop;
        end;

    $func$ language plpgsql;

    select gen_timeslices((now() - interval '10 days')::timestamp, interval '1 minute');

So, when the last line runs, we generate a time range for each minute from 10 days ago until now.

Now the query changes to replace the first table with the new “timeslices” table.

select max(concurrent_count) from

(select
        A.timeslice, count(B.time_range) as concurrent_count
 from
 	timeslices A inner join call_session_time_range B
    on A.timeslice @> br.time_range and ar.timeslice <> B.time_range
    group by
 A.timeslice) res;

Our new count is really “how many calls happened within a given minute?” and our final answer is “What is the maximum number of calls that happened in a given minute for our given time range of 10 days?”

So, the answer for my query was: 6. Yay! That’s actually reasonable given the parameters we are considering.

Conclusion

The point of this post was to demonstrate how a relatively complex problem can be solved with some SQL really nicely. We are using set-like operations to formally define the problem and to generate useful answers. Addressing the problem by wrapping it in a framework to think about the problem really does help and is extremely powerful.

As a side note, its nice to see that that actual SQL code for it is really simple. If I wanted the thing to go faster, I can carefully drop indices in certain places. However, it’s not important and I have my answer now.


About Me

Hello world!

I am a web developer in Oslo, Norway. I am originally from New York City, but decided to come to Norway for awhile.

I currently work as a developer at a web analytics company, doing heavy frontend JavaScript development. However, for quite a long time, I worked on various web development projects, from backend software development with MySQL and PostgreSQL, with Rails and PHP, and with frontend work in JavaScript.

Contact me at brian at bolsen dot org, if you want to chat about hacking.

About me

I am a web developer in Oslo, Norway.

I worked in Rails, PHP, JavaScript (with Backbone, React, Redux), etc.

Look at my CV: here


Send me some Bitcoin:

1M4YhjrLE25Vh5vvikHDtspawFmwJKVzEp

Send me some Litecoin:

LUTbT8nD2w5UGGeyiujozfnjy9sorg67MG

Contact me

brian [at] bolsen [dot] org

here!

subscribe via RSS