An open source, stand-alone, customisable public spending data web app.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

148 líneas
4.0 KiB

  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'sinatra-helpers/haml/partials'
  4. require 'haml'
  5. require 'lib/models'
  6. PAYMENTS_FILTER_MIN = 1000
  7. helpers do
  8. def commify(amount)
  9. amount.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
  10. end
  11. end
  12. get '/' do
  13. @directorates = Directorate.all( :order => ['name'] )
  14. @payments_count = Payment.count
  15. @suppliers_count = Supplier.count
  16. @services_count = Service.count
  17. haml :home
  18. end
  19. get '/directorates/:slug' do
  20. @directorate = Directorate.first(:slug => params[:slug])
  21. haml :directorate
  22. end
  23. get '/suppliers/:slug.csv' do
  24. @supplier = Supplier.first(:slug => params[:slug])
  25. headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
  26. "Content-Type" => "application/octet-stream"
  27. result = "Date,Ref.,URL,Directorate,Service,Amount ex. VAT\n"
  28. for payment in @supplier.payments
  29. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
  30. end
  31. result
  32. end
  33. get '/suppliers/:slug' do
  34. @supplier = Supplier.first(:slug => params[:slug])
  35. @total = @supplier.payments.sum(:amount)
  36. @count = @supplier.payments.size # Payment.count(:supplier_id => @supplier.id) ?
  37. @avg = @supplier.payments.avg(:amount)
  38. @max = @supplier.payments.max(:amount)
  39. @min = @supplier.payments.min(:amount)
  40. haml :supplier
  41. end
  42. get '/suppliers/?' do
  43. @suppliers = Supplier.all( :order => ['name'] )
  44. haml :suppliers
  45. end
  46. get '/services/:slug/payments.csv' do
  47. @service = Service.first(:slug => params[:slug])
  48. headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
  49. "Content-Type" => "application/octet-stream"
  50. result = "Date,Ref.,URL,Directorate,Supplier,Amount ex. VAT\n"
  51. for payment in @service.payments
  52. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
  53. end
  54. result
  55. end
  56. get '/services/:slug.json' do
  57. @service = Service.first(:slug => params[:slug])
  58. headers "Content-Type" => "application/json"
  59. @service.to_json(:relationships => { :payments => { :include => :all }, :directorate => { :include => :all } })
  60. end
  61. get '/services/:slug' do
  62. @service = Service.first(:slug => params[:slug])
  63. @total = @service.payments.sum(:amount)
  64. @count = @service.payments.size
  65. @avg = @service.payments.avg(:amount)
  66. @max = @service.payments.max(:amount)
  67. @min = @service.payments.min(:amount)
  68. @results = repository(:default).adapter.query("
  69. SELECT s.name AS supplier_name, s.slug AS supplier_slug, SUM(p.amount) AS total
  70. FROM payments p, suppliers s
  71. WHERE p.supplier_id = s.id
  72. AND p.service_id = #{@service.id}
  73. GROUP BY s.name, s.slug
  74. ORDER BY total DESC")
  75. haml :service
  76. end
  77. get '/services/:slug/payments' do
  78. @FILTER_VALUES = %w[ 500 1000 2500 5000 10000 20000 ]
  79. @service = Service.first(:slug => params[:slug])
  80. # payments_filter_min cookie persists user selection of filter value
  81. unless @min = request.cookies["payments_filter_min"]
  82. @min = PAYMENTS_FILTER_MIN
  83. response.set_cookie(
  84. "payments_filter_min",
  85. { :value => @min, :expires => Time.now + (60 * 24 * 60 * 60) }
  86. ) # 60 days
  87. end
  88. @payments = Payment.all(:service_id => @service.id, :amount.gte => @min, :order => [ 'd' ])
  89. @total = @payments.sum(:amount)
  90. haml :servicepayments
  91. end
  92. get '/services/:slug/paymentsdetail' do
  93. @service = Service.first(:slug => params[:slug])
  94. min = PAYMENTS_FILTER_MIN
  95. if params[:min].to_i > 0
  96. min = params[:min].to_i
  97. end
  98. @payments = Payment.all(:service_id => @service.id, :amount.gte => min, :order => [ 'd' ])
  99. @total = @payments.sum(:amount)
  100. haml :servicepaymentsdetail, :layout => false
  101. end
  102. get '/services/?' do
  103. @services = Service.all( :order => ['name'] )
  104. haml :services
  105. end
  106. get '/payments/:id' do
  107. @payment = Payment.get(params[:id])
  108. haml :payment
  109. end
  110. get '/error' do
  111. haml :error
  112. end
  113. get '/about' do
  114. haml :about
  115. end
  116. not_found do
  117. haml :not_found
  118. end