add api implementation

This commit is contained in:
Siddharth Jain 2025-05-23 12:27:58 -04:00
parent e3dd39b5df
commit 5606f7fa69
114 changed files with 3202 additions and 0 deletions

0
app/assets/images/.keep Normal file
View file

View file

@ -0,0 +1,10 @@
/*
* This is a manifest file that'll be compiled into application.css.
*
* With Propshaft, assets are served efficiently without preprocessing steps. You can still include
* application-wide styles in this file, but keep in mind that CSS precedence will follow the standard
* cascading order, meaning styles declared later in the document or manifest will override earlier ones,
* depending on specificity.
*
* Consider organizing styles into separate files for maintainability.
*/

View file

@ -0,0 +1,4 @@
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
end

View file

View file

@ -0,0 +1,92 @@
class DashboardController < ApplicationController
require 'httparty'
require 'csv'
require 'prawn'
def index
@filters = fetch_filter_options
if params[:org_type]
@selected = {
org_type: params[:org_type],
sy: params[:sy],
grad_rate_type: params[:grad_rate_type],
stu_grp: params[:stu_grp]
}
@data = fetch_filtered_data(@selected)
else
@selected = {}
@data = []
end
end
def download_csv
data = fetch_filtered_data(params)
csv_string = CSV.generate(headers: true) do |csv|
csv << data.first.keys if data.any?
data.each { |row| csv << row.values }
end
send_data csv_string, filename: "graduation_stats.csv"
end
def download_pdf
data = fetch_filtered_data(params)
pdf = Prawn::Document.new
pdf.text "Graduation Stats Report", size: 14, align: :center
pdf.move_down 10
data.each_with_index do |row, i|
pdf.text "#{i+1}. #{row.values.join(", ")}"
end
send_data pdf.render, filename: "graduation_stats.pdf", type: "application/pdf"
end
private
def fetch_filter_options
response = HTTParty.get("https://educationtocareer.data.mass.gov/resource/n2xa-p822.json?$limit=1000", headers: headers)
df = response.parsed_response
{
org_types: df.map { |x| x["org_type"] }.compact.uniq.sort,
school_years: df.map { |x| x["sy"] }.compact.uniq.sort.reverse,
rate_types: df.map { |x| x["grad_rate_type"] }.compact.uniq.sort,
student_groups: df.map { |x| x["stu_grp"] }.compact.uniq.sort
}
end
def fetch_filtered_data(filters)
where_clause = URI.encode_www_form_component("org_type='#{filters[:org_type]}' AND sy='#{filters[:sy]}' AND grad_rate_type='#{filters[:grad_rate_type]}' AND stu_grp='#{filters[:stu_grp]}'")
url = "https://educationtocareer.data.mass.gov/resource/n2xa-p822.json?$where=#{where_clause}&$limit=50000"
response = HTTParty.get(url, headers: headers)
df = response.parsed_response
numeric_cols = %w[cohort_cnt grad_pct in_sch_pct non_grad_pct ged_pct drpout_pct exclud_pct]
df.each do |row|
numeric_cols.each { |col| row[col] = row[col].to_f if row[col] }
end
name_col, code_col = case filters[:org_type]
when "District" then ["dist_name", "dist_code"]
when "School" then ["org_name", "org_code"]
else ["org_name", "org_code"]
end
df.map.with_index(1) do |row, idx|
{
"S. No." => idx,
name_col => row[name_col],
code_col => row[code_col],
"# in Cohort" => row["cohort_cnt"],
"% Graduated" => row["grad_pct"],
"% Still in School" => row["in_sch_pct"],
"% Non-Grad Completers" => row["non_grad_pct"],
"% H.S. Equiv." => row["ged_pct"],
"% Dropped Out" => row["drpout_pct"],
"% Permanently Excluded" => row["exclud_pct"]
}
end
end
def headers
{ "X-App-Token" => ENV["SOCRATA_APP_TOKEN"] }
end
end

View file

@ -0,0 +1,2 @@
module ApplicationHelper
end

View file

@ -0,0 +1,2 @@
module DashboardHelper
end

View file

@ -0,0 +1,3 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"

View file

@ -0,0 +1,9 @@
import { Application } from "@hotwired/stimulus"
const application = Application.start()
// Configure Stimulus development experience
application.debug = false
window.Stimulus = application
export { application }

View file

@ -0,0 +1,7 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
this.element.textContent = "Hello World!"
}
}

View file

@ -0,0 +1,4 @@
// Import and register all your controllers from the importmap via controllers/**/*_controller
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)

View file

@ -0,0 +1,7 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end

View file

@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout "mailer"
end

View file

@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end

View file

View file

@ -0,0 +1,38 @@
<%= form_with url: root_path, method: :get, local: true do |f| %>
<div>
<%= label_tag :org_type, "Report Type" %>
<%= select_tag :org_type, options_for_select(@filters[:org_types], @selected[:org_type]) %>
<%= label_tag :sy, "School Year" %>
<%= select_tag :sy, options_for_select(@filters[:school_years], @selected[:sy]) %>
<%= label_tag :grad_rate_type, "Graduation Rate Type" %>
<%= select_tag :grad_rate_type, options_for_select(@filters[:rate_types], @selected[:grad_rate_type]) %>
<%= label_tag :stu_grp, "Student Group" %>
<%= select_tag :stu_grp, options_for_select(@filters[:student_groups], @selected[:stu_grp]) %>
<%= submit_tag "Filter" %>
</div>
<% end %>
<% if @data.present? %>
<table>
<thead>
<tr><% @data.first.keys.each do |key| %><th><%= key %></th><% end %></tr>
</thead>
<tbody>
<% @data.each do |row| %>
<tr><% row.each { |_, value| %><td><%= value %></td><% } %></tr>
<% end %>
</tbody>
</table>
<%= link_to "Download CSV", download_csv_path(request.query_parameters), class: "button" %>
<%= link_to "Download PDF", download_pdf_path(request.query_parameters), class: "button" %>
<% else %>
<% if @selected.present? %>
<p>No data found for the selected combination.</p>
<% end %>
<% end %>

View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title><%= content_for(:title) || "Myapp" %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= yield :head %>
<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>
<link rel="icon" href="/icon.png" type="image/png">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/icon.png">
<%# Includes all stylesheet files in app/assets/stylesheets %>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<%= yield %>
</body>
</html>

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1,22 @@
{
"name": "Myapp",
"icons": [
{
"src": "/icon.png",
"type": "image/png",
"sizes": "512x512"
},
{
"src": "/icon.png",
"type": "image/png",
"sizes": "512x512",
"purpose": "maskable"
}
],
"start_url": "/",
"display": "standalone",
"scope": "/",
"description": "Myapp.",
"theme_color": "red",
"background_color": "red"
}

View file

@ -0,0 +1,26 @@
// Add a service worker for processing Web Push notifications:
//
// self.addEventListener("push", async (event) => {
// const { title, options } = await event.data.json()
// event.waitUntil(self.registration.showNotification(title, options))
// })
//
// self.addEventListener("notificationclick", function(event) {
// event.notification.close()
// event.waitUntil(
// clients.matchAll({ type: "window" }).then((clientList) => {
// for (let i = 0; i < clientList.length; i++) {
// let client = clientList[i]
// let clientPath = (new URL(client.url)).pathname
//
// if (clientPath == event.notification.data.path && "focus" in client) {
// return client.focus()
// }
// }
//
// if (clients.openWindow) {
// return clients.openWindow(event.notification.data.path)
// }
// })
// )
// })