Skip to content

Instantly share code, notes, and snippets.

@nwoow
Last active July 6, 2018 17:31
Show Gist options
  • Save nwoow/fe3223d19395de7ec88dcb64db4116ee to your computer and use it in GitHub Desktop.
Save nwoow/fe3223d19395de7ec88dcb64db4116ee to your computer and use it in GitHub Desktop.
module Code
def search(search_scope,compare)
if search_scope == "All"
all.order(:id)
elsif compare == "Bihar vs District"
where("Districts = ? OR Districts = ?", search_scope, "Bihar")
else
where(Districts: search_scope)
end
end
end
class ProductionProductivity7sController < ApplicationController
before_action :set_production_productivity7, only: [:show, :edit, :update, :destroy]
# GET /production_productivity7s
# GET /production_productivity7s.json
def index
@production_productivity7s = ProductionProductivity7.all
respond_to do |format|
format.html { render json: @production_productivity7s}
end
end
# GET /production_productivity7s/1
# GET /production_productivity7s/1.json
def show
end
# GET /production_productivity7s/new
def new
@production_productivity7 = ProductionProductivity7.new
end
# GET /production_productivity7s/1/edit
def edit
end
def test
rain_fall_type = params[:rain_fall_type]
views = params[:views]
year = ""
compare = params[:compare]
if rain_fall_type || views
if views == "Map View"
l = rain_fall_type.gsub(" ","")
if year == "2016"
j = "#{l}_2016"
elsif year == "2017"
j = "#{l}_2017"
else
puts "no year"
end
if rain_fall_type == "All"
if year == "2016"
j = "Total_2016"
elsif year == "2017"
j = "Total_2017"
else
puts "no year"
end
b = ProductionProductivity7.map_search(j)
u = "Total"
a = ProductionProductivity7.map(b,u,year)
else
b = ProductionProductivity7.map_search(j)
a = ProductionProductivity7.map(b,rain_fall_type,year)
end
elsif views == "Table"
b = ProductionProductivity7.search(params[:search],compare)
a = ProductionProductivity7.table(b,rain_fall_type,year)
else
@ProductionProductivity7s = ProductionProductivity7.search(params[:search],compare)
a = ProductionProductivity7.query(@ProductionProductivity7s,params[:year],rain_fall_type,views,compare)
end
respond_to do |format|
format.html { render json:a }
end
else
respond_to do |format|
format.html { render json: "error"}
end
end
end
def import
# Module1.import(params[:file])
ProductionProductivity7.import1(params[:file])
redirect_to tests_path, notice: "Products imported."
end
# POST /production_productivity7s
# POST /production_productivity7s.json
def create
@production_productivity7 = ProductionProductivity7.new(production_productivity7_params)
respond_to do |format|
if @production_productivity7.save
format.html { redirect_to @production_productivity7, notice: 'Production productivity7 was successfully created.' }
format.json { render :show, status: :created, location: @production_productivity7 }
else
format.html { render :new }
format.json { render json: @production_productivity7.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /production_productivity7s/1
# PATCH/PUT /production_productivity7s/1.json
def update
respond_to do |format|
if @production_productivity7.update(production_productivity7_params)
format.html { redirect_to @production_productivity7, notice: 'Production productivity7 was successfully updated.' }
format.json { render :show, status: :ok, location: @production_productivity7 }
else
format.html { render :edit }
format.json { render json: @production_productivity7.errors, status: :unprocessable_entity }
end
end
end
# DELETE /production_productivity7s/1
# DELETE /production_productivity7s/1.json
def destroy
@production_productivity7.destroy
respond_to do |format|
format.html { redirect_to production_productivity7s_url, notice: 'Production productivity7 was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_production_productivity7
@production_productivity7 = ProductionProductivity7.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def production_productivity7_params
params.require(:production_productivity7).permit(:Districts, :Area_2015, :Production_2015, :Productivity_2015, :Area_2016, :Production_2016, :Productivity_2016)
end
end
class ProductionProductivity7 < ApplicationRecord
def self.import1(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
puts row.to_hash
product = find_by(id: row["id"]) || new
product.attributes = row.to_hash
product.save!
end
end
def self.search(search,compare)
if search == "All"
all.order(:id)
elsif compare == "Bihar vs District"
where("Districts = ? OR Districts = ?", search, "Bihar")
else
where(Districts: search)
end
end
def self.map_search(search)
if search == "All"
all.order(:id)
else
# where(Districts: search)
all.order(search)
end
end
# def self.query(b,year,rain_fall_type,views)
# array = []
# d = "Districts"
# b.each do |element|
# if element[d] == "Average "
# elsif element[d] == "StD"
# else
# hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
# array.push(hash1)
# end
# end
# return array
# end
def self.query(b,year,rain_fall_type,views,compare)
# return b
l = rain_fall_type.gsub(" ","")
d = "Districts"
# if year == "2016"
# j = "#{l}_2016"
# ji = [:WinterRain_2016, :HotWeatherRain_2016, :SouthWestMonsoonRain_2016, :NorthWestMonsoonRain_2016]
# puts j
# elsif year == "2017"
# j = "#{l}_2017"
# ji = [:WinterRain_2017, :HotWeatherRain_2017, :SouthWestMonsoonRain_2017]
# else
# puts "no year"
# end
ji = [:Area_2015, :Production_2015, :Productivity_2015, :Non_Agriculture_Land_Area,:Area_2016, :Production_2016,:Productivity_2016]
if rain_fall_type == "All"
if views == "Trend Line"
hash_data = ji.map do |column_name|
{
type:"line",
legendText: column_name,
showInLegend: true,
dataPoints: b.reject{|x| x["Districts"]== "Bihar"}.map do |el|
{ y: el[column_name], label: el[d] }
end
}
end
else
hash_data = ji.map do |column_name|
{
type:"column",
legendText: column_name,
showInLegend: true,
dataPoints: b.reject{|x| x["Districts"]== "Bihar"}.map do |el|
{ y: el[column_name], label: el[d] }
end
}
end
end
return hash_data
else
array = []
b.each do |element|
if compare == "Bihar vs District"
hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
array.push(hash1)
else
if element[d] == "Bihar"
else
hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
array.push(hash1)
end
end
end
return array
end
end
def self.map(b,j,year)
array = []
# a = []
# l = rain_fall_type.gsub(" ","")
# if year == "2016"
# j = "#{l}_2016"
# elsif year == "2017"
# j = "#{l}_2017"
# else
# j = rain_fall_type
# end
a = []
below_min = []
min = []
blow_max = []
max = []
above_max = []
extreme = []
above_extreme = []
b.map.with_index do |el,i|
if el["Districts"] == "W. Champaran"
dist = "WChamparan"
elsif el["Districts"] == "E. Champaran"
dist = "EChamparan"
else
dist = el["Districts"]
end
if (0..7) === i
hash1 = { y: el[j], label: dist, color: "Red" }
below_min.push(hash1)
elsif (7..15) === i
hash1 = { y: el[j], label: dist, color: "Orange" }
# hash1 = { y: el[j], label: el["Districts"] }
min.push(hash1)
elsif (15..21) === i
hash1 = { y: el[j], label: dist, color: "Dark_Yellow" }
# hash1 = { y: el[j], label: el["Districts"] }
blow_max.push(hash1)
elsif (21..26) === i
hash1 = { y: el[j], label: dist, color: "Yellow" }
# hash1 = { y: el[j], label: el["Districts"] }
max.push(hash1)
elsif (26..32) === i
hash1 = { y: el[j], label: dist, color: "Light_Green" }
# hash1 = { y: el[j], label: el["Districts"] }
above_max.push(hash1)
elsif (32..37) === i
hash1 = { y: el[j], label: dist, color: "Green" }
# hash1 = { y: el[j], label: el["Districts"] }
extreme.push(hash1)
elsif (36..40) === i
hash1 = { y: el[j], label: dist, color: "Dark_Green" }
# hash1 = { y: el[j], label: el["Districts"] }
above_extreme.push(hash1)
else
puts "Hello"
end
# array.push(a)
end
a.push({"below_min": below_min})
a.push({"min": min})
a.push({"blow_max": blow_max})
a.push({"max": max})
a.push({"above_max": above_max})
a.push({"extreme": extreme})
a.push({"above_extreme": above_extreme})
# array = [{name: "array"}]
# sleep 1
return a
end
def self.search1(search,compare)
puts search
# where(Districts: search)
if search == "All"
all.order(:Geographical_Area)
elsif compare == "Bihar vs District"
where("Districts = ? OR Districts = ?", search, "Bihar")
else
where(Districts: search)
end
end
end
class ProductionProductivity7 < ApplicationRecord
extend Code
def self.import1(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
puts row.to_hash
product = find_by(id: row["id"]) || new
product.attributes = row.to_hash
product.save!
end
end
# def self.search(search,compare)
# if search == "All"
# all.order(:id)
# elsif compare == "Bihar vs District"
# where("Districts = ? OR Districts = ?", search, "Bihar")
# else
# where(Districts: search)
# end
# end
def self.map_search(search)
if search == "All"
all.order(:id)
else
# where(Districts: search)
all.order(search)
end
end
# def self.query(b,year,rain_fall_type,views)
# array = []
# d = "Districts"
# b.each do |element|
# if element[d] == "Average "
# elsif element[d] == "StD"
# else
# hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
# array.push(hash1)
# end
# end
# return array
# end
def self.query(b,year,rain_fall_type,views,compare)
# return b
l = rain_fall_type.gsub(" ","")
d = "Districts"
# if year == "2016"
# j = "#{l}_2016"
# ji = [:WinterRain_2016, :HotWeatherRain_2016, :SouthWestMonsoonRain_2016, :NorthWestMonsoonRain_2016]
# puts j
# elsif year == "2017"
# j = "#{l}_2017"
# ji = [:WinterRain_2017, :HotWeatherRain_2017, :SouthWestMonsoonRain_2017]
# else
# puts "no year"
# end
ji = [:Area_2015, :Production_2015, :Productivity_2015, :Non_Agriculture_Land_Area,:Area_2016, :Production_2016,:Productivity_2016]
if rain_fall_type == "All"
if views == "Trend Line"
hash_data = ji.map do |column_name|
{
type:"line",
legendText: column_name,
showInLegend: true,
dataPoints: b.reject{|x| x["Districts"]== "Bihar"}.map do |el|
{ y: el[column_name], label: el[d] }
end
}
end
else
hash_data = ji.map do |column_name|
{
type:"column",
legendText: column_name,
showInLegend: true,
dataPoints: b.reject{|x| x["Districts"]== "Bihar"}.map do |el|
{ y: el[column_name], label: el[d] }
end
}
end
end
return hash_data
else
array = []
b.each do |element|
if compare == "Bihar vs District"
hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
array.push(hash1)
else
if element[d] == "Bihar"
else
hash1 = {:y => element[rain_fall_type] ,:label => element[d]}
array.push(hash1)
end
end
end
return array
end
end
def self.map(b,j,year)
array = []
# a = []
# l = rain_fall_type.gsub(" ","")
# if year == "2016"
# j = "#{l}_2016"
# elsif year == "2017"
# j = "#{l}_2017"
# else
# j = rain_fall_type
# end
a = []
below_min = []
min = []
blow_max = []
max = []
above_max = []
extreme = []
above_extreme = []
b.map.with_index do |el,i|
if el["Districts"] == "W. Champaran"
dist = "WChamparan"
elsif el["Districts"] == "E. Champaran"
dist = "EChamparan"
else
dist = el["Districts"]
end
if (0..7) === i
hash1 = { y: el[j], label: dist, color: "Red" }
below_min.push(hash1)
elsif (7..15) === i
hash1 = { y: el[j], label: dist, color: "Orange" }
# hash1 = { y: el[j], label: el["Districts"] }
min.push(hash1)
elsif (15..21) === i
hash1 = { y: el[j], label: dist, color: "Dark_Yellow" }
# hash1 = { y: el[j], label: el["Districts"] }
blow_max.push(hash1)
elsif (21..26) === i
hash1 = { y: el[j], label: dist, color: "Yellow" }
# hash1 = { y: el[j], label: el["Districts"] }
max.push(hash1)
elsif (26..32) === i
hash1 = { y: el[j], label: dist, color: "Light_Green" }
# hash1 = { y: el[j], label: el["Districts"] }
above_max.push(hash1)
elsif (32..37) === i
hash1 = { y: el[j], label: dist, color: "Green" }
# hash1 = { y: el[j], label: el["Districts"] }
extreme.push(hash1)
elsif (36..40) === i
hash1 = { y: el[j], label: dist, color: "Dark_Green" }
# hash1 = { y: el[j], label: el["Districts"] }
above_extreme.push(hash1)
else
puts "Hello"
end
# array.push(a)
end
a.push({"below_min": below_min})
a.push({"min": min})
a.push({"blow_max": blow_max})
a.push({"max": max})
a.push({"above_max": above_max})
a.push({"extreme": extreme})
a.push({"above_extreme": above_extreme})
# array = [{name: "array"}]
# sleep 1
return a
end
def self.search1(search,compare)
puts search
# where(Districts: search)
if search == "All"
all.order(:Geographical_Area)
elsif compare == "Bihar vs District"
where("Districts = ? OR Districts = ?", search, "Bihar")
else
where(Districts: search)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment