Saturday, November 19, 2016

Yesterday, we were discussing websockets. We said it facilitates duplex communication and is independent of http. As an example use case, we considered displaying real time monitoring of cpu, memory metrics on a dashboard using a python django server. the clients publish ata from each monitored machine to a central django server that in turn publishes to the frontend. the frontend updates on each new statistic because it listens for those events with say Autobahn. Here the difference is highlighted from the traditional model where the front-end pulls the information from a time series database by running a query every once in a while.
The frontend therefore looks something like this:
<code>
      window.addEventListener("load", function(){
        var connection = new autobahn.Connection({
           url: 'ws://127.0.0.1:8080/ws',
           realm: 'realm1'
        });
        connection.onopen = function(session) {
          var clients = document.getElementById("clients");
          session.subscribe('clientstats', function(args){
            var stats = args[0];
            var serverNode = document.getElementById(stats.ip);
            // update
            }
          });
          }
         });
        connection.open();
</code>

The clients push the code and this can be done with statsdclient which runs on every source of metrics.
The server however is different from traditional.
@receiver(post_save, sender=Client, dispatch_uid="server_post_save")
def notify_server_config_changed(sender, instance, **kwargs):
    requests.post("http://127.0.0.1:8080/notify",
                  json={
                      'topic': 'clientconfig',
                      'args': [model_to_dict(instance)]
                  })
The 'notify' is the url configured for the push service.
#codingexercise
We discussed the next palindrome problem yesterday. It involved an addition and carry over. We do it this way:
       static void AddOne(ref List<int>digits, int start)
        {
            if (start > digits.Count) return;
            if (digits[start] + 1 <= 9)
            {
                digits[start] += 1;
                return;
            }
            int carry_over = 1;
            while (start >= 0 && carry_over != 0)
            {
            int sum = digits[start] + 1;
            digits[start] = sum %10;
            carry_over = sum /10;
            if (start == 0)
            {
                digits.Insert(0, carry_over);
            }
            start--;
            }
        }

No comments:

Post a Comment