HustleTime Development: Status page (api)

I created the HustleTime back end with a rails api, and when I actually deployed it to Digital Ocean, I left some of the wrong settings in place and I ended up with this:

What the HELL is this?

That was what was on hustletime.paulbomba.com if you happened to look there… it’s a long list of pregenerated routes that don’t really lead anywhere. They are routes that are vestiges of prior versions of code, and I never went in and cleaned things up.

I also thought it might be helpful to have a view at that address. I wanted to have something there in case someone visited the URL.

I decided that I’d put a status page up, where you can see the current seed status of HustleTime’s data.

In order to do that, I had to do a couple of things.

a- create a view
b- put the assets together
c- clean up the routes
d- create a route for the new page
e-figure out how to collect and store latest seed information

One of the things about rails api is that each controller has inheritance that is namespaced for the api

class Api::V1::SomeController < ApplicationController

So when I am creating a controller for the view that I want, I need to set up the class a little differently and inherit direction from ActionController

class StatusPagesController < ActionController::Base

Now, I’ll create a folder in views called status_pages and drop an .erb file in there.

Finally, I’ll clean up my routes and get rid of all those unnecessary old routes and put a route to the status page in the correct namespace

Rails.application.routes.draw do
 root 'status_pages#index'
 resources :status_pages
.
.
.

easy.

Now that we have the status page view configured, I need to figure out what I’m actually putting there. I basically want it to tell me when any of the various feeds have reseeded.

I experimented with postgres, but I decided Redis is the way to go for this, so what I’ll do is create a number of keys in Redis for each feed, and when that feed gets updated, I’ll assign that timestamp to that key in redis, and when I render the page, I’ll check that particular key for the timestamp.

I’ll need to create a helpermethod in the controller so I can call it via erb in the view

app/controllers/status_pages_controller

class StatusPagesController < ActionController::Base

def get_last_sync(route)
  StatusPage.check_redis(route)
end

helper_method :get_last_sync
end

and I’ll need to define the class function in the model:

class StatusPage

require_relative './data_helper.rb'
require 'date'

  def self.check_redis(route)
    feed_time = DataHelper.get_last_time(route).to_i
    time_now = DateTime.now.strftime('%s').to_i
    difference = (time_now - feed_time)/60
    difference
  end
end

DataHelper.rb is where I put all my Redis methods, so I’ll create a method there to get the latest time from a feed.

/DataHelper.rb	

def self.get_last_time(route)
	time = JSON.parse(@@redis.get(route))
	time
end

Ok, so now I can get the last seed time from a particular route. All I need is a way to store that in redis. So let me create a method that will put that key into Redis for me.

def self.store_update_time(route, time)
	@@redis.set(route, time)
end

Now, whenever we reseed, if that particular feed is stored, I’ll store the updated time.
before:

require 'date'
time_now = DateTime.now.strftime('%s').to_i

.
.
.

begin
  feed123456S = Transit_realtime::FeedMessage.decode(data123456S)
rescue Protobuf::InvalidWireType
  errors = errors + ' skipped: 123456S (wireType)'
end

and after…

begin
  feed123456S = Transit_realtime::FeedMessage.decode(data123456S)
  DataHelper.update_time('data123456S', time_now) // pass feed and time
rescue Protobuf::InvalidWireType
  errors = errors + ' skipped: 123456S (wireType)'
end

The seeding helper file is a little complicated, so I’ll spare you the minutiae, but what’s happening here is that I download a data feed from the MTA api, and it needs to be decoded with protobuf, and something that happens quite often is the data is somehow corrupted and throws a wireType error.
So, if the decoding is successful, and we have data, I will tell DataHelper.rb that we’ve reseeded successfully.

you can learn more about my adventures with wireType errors here.

Now that all this is set up, I can go ahead and actually create the view. It’s super basic at this point, but that’s what I’m going for. I need to continue to experiment and learn as I go.

It’s not pretty, but it gets the job done- much like the rest of HustleTime.

Remember…
You can get HustleTime on both Android and iPhone free!