Created
April 19, 2010 21:45
-
-
Save adamwiggins/371687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'json' | |
class Pgdump | |
attr_reader :name | |
def initialize | |
@name = "dump-#{rand(99999)}" | |
@step = 0 | |
end | |
def self.create | |
dump = new | |
@@pgdumps ||= {} | |
@@pgdumps[dump.name] = dump | |
end | |
def self.find(name) | |
return nil if !defined?(@@pgdumps) | |
@@pgdumps[name] | |
end | |
def to_h | |
{ | |
'name' => name, | |
'state' => state, | |
'progress' => progress, | |
'size' => 123456 | |
} | |
end | |
def to_json | |
to_h.to_json | |
end | |
def advance_progress | |
@step += 1 if @step < highest_step | |
end | |
def progress | |
case @step | |
when 0: [ ] | |
when 1: [ ['dump','0.3MB my_table1'] ] | |
when 2: [ ['dump','1.7MB other_table'] ] | |
when 3: [ ['dump','2.8MB the_last_table'] ] | |
when 4: [ ['dump','3.1MB'], ['gzip','1.2MB'] ] | |
when 5: [ ['dump','3.1MB'], ['gzip','2.0MB'] ] | |
when 6: [ ['dump','3.1MB'], ['gzip','2.0MB'], ['store','0.4MB'] ] | |
when 7: [ ['dump','3.1MB'], ['gzip','2.0MB'], ['store','1.8MB'] ] | |
when 8: [ ['dump','3.1MB'], ['gzip','2.0MB'], ['store','2.0MB'] ] | |
end | |
end | |
def highest_step | |
8 | |
end | |
def state | |
return 'underway' if @step < highest_step | |
'complete' | |
end | |
end | |
post '/apps/:app/pgdumps' do | |
Pgdump.create.to_json | |
end | |
get '/apps/:app/pgdumps/:name' do | |
pgdump = Pgdump.find(params[:name]) | |
pgdump.advance_progress | |
pgdump.to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment