PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/concerns/reportable.rb

https://gitlab.com/Rambabu.d/sujala
Ruby | 140 lines | 87 code | 25 blank | 28 comment | 8 complexity | 4661c0b607c71fbbf8c8f5c82faa3231 MD5 | raw file
  1. module Reportable
  2. extend ActiveSupport::Concern
  3. included do
  4. # attr_accessor :column, :type, :micro_water_shed, :label, :level
  5. scope :frmr, -> (type, micro_water_shed) { joins(:farmer => {:village => :micro_water_shed}).where("villages.micro_water_shed_id" => micro_water_shed).joins(:farmer => type) }
  6. # scope :farmers_action, -> (column, type) {frmr(type).joins(column.to_sym)}
  7. # scope :cnt, -> (column, type) { farmers_action(column, type).group("#{plural(column)}.name", "#{plural(type)}.name").count}
  8. end
  9. class_methods do
  10. def set_up(report_key, micro_water_shed, type)
  11. @type = type
  12. @micro_water_shed = micro_water_shed
  13. @label = report_key
  14. @report = REPORT[report_key]
  15. @column = REPORT[report_key]["column"]
  16. @level = REPORT[report_key]["level"]
  17. @total = REPORT[report_key]["total"]
  18. @filter = REPORT[report_key]["filter"]
  19. end
  20. def topography(report_key, micro_water_shed)
  21. set_up(report_key, micro_water_shed, "topography")
  22. report_data()
  23. end
  24. def smu(report_key, micro_water_shed)
  25. set_up(report_key, micro_water_shed, "smu")
  26. report_data()
  27. end
  28. def category(report_key, micro_water_shed)
  29. set_up(report_key, micro_water_shed, "category")
  30. report_data()
  31. end
  32. def data()
  33. self.frmr(@type, @micro_water_shed).joins(@column.to_sym).group("#{plural(@column)}.name", "#{plural(@type)}.name").count
  34. end
  35. def init_data()
  36. names(@column).product(names(@type))
  37. end
  38. def init()
  39. @list_product = init_data()
  40. @hash = Hash.new
  41. @list_product.each{|list| @hash[list] = 0}
  42. @hash
  43. end
  44. def data_merge()
  45. init().merge(data())
  46. end
  47. # type_item's are the actual items of the type
  48. # Example:
  49. # If type is Topography, then the type_items are "Ridge", "Mid" and "Valley"
  50. def total(_data, type_item)
  51. @total_value = _data.collect{ |key, value| key[1] == type_item ? value.to_f : 0 }.reduce(:+)
  52. end
  53. # def total(_data, type_item)
  54. # if @total == 'individual'
  55. # @total_value = _data.collect{ |key, value| key[1] == type_item ? value.to_i : 0 }.reduce(:+)
  56. # else
  57. # @f_count = farmer_count()
  58. # @total_value = @f_count.keys.include?(type_item) ? @f_count[type_item] : 0
  59. # end
  60. # @total_value
  61. # end
  62. def farmer_total(f_count, type_item)
  63. f_count.keys.include?(type_item) ? f_count[type_item] : 0
  64. end
  65. def farmer_count(type, mws, report)
  66. if report["total"] == 'individual'
  67. @farmer_count = {}
  68. __data = self.frmr(type, mws).joins(report['column'].to_sym).group("#{plural(report['column'])}.name", "#{plural(type)}.name").count
  69. __data.collect{|k,v| @farmer_count[k[1]] = (@farmer_count[k[1]].blank? ? 0 : @farmer_count[k[1]]).to_i + v}
  70. else
  71. @farmer_count = Farmer.joins(:village).where("villages.micro_water_shed_id" => mws).joins(type.to_sym).group("#{plural(type)}.name").count
  72. end
  73. @farmer_count
  74. end
  75. # column is the column name for the x-axis in the chart
  76. # Example:
  77. # Gender - with table name 'genders', containing master data of the genders table/ Gender model class.
  78. # Identimws
  79. # Education - with table educations, contianing master data of the educations table/ Education model class
  80. # Identified as education_id
  81. #
  82. # type - Type is the type of report. There are 3 types of reports
  83. #
  84. # 1. Topography
  85. # 2. SMU
  86. # 3. Category - Farmer category based on the acers of land owned.
  87. #
  88. def report_data()
  89. _dest_data = destination_data()
  90. _data = data()
  91. _data_farmer = farmer_count(@type, @micro_water_shed, @report)
  92. data_merge().reject{|a|a[0].blank?}.each do |key, value|
  93. item = _dest_data.find{| _item | _item["name"].strip == key[0].strip}
  94. item[key[1]] = value.blank? ? 0 : value
  95. item[key[1] + "_total"] = @total == 'individual' ? total(_data, key[1]) : farmer_total(_data_farmer, key[1])
  96. if @label.include?('Avg') || @label.include?('Average') || @label.include?('Cropping intensity')
  97. item['all_average'] = data_average()[key[0]]
  98. end
  99. end
  100. _dest_data
  101. end
  102. def destination_data()
  103. JSON.parse(to_model(@column).all.to_json)
  104. end
  105. def plural name
  106. name.pluralize
  107. end
  108. def to_model(model_name)
  109. model_name.classify.constantize
  110. end
  111. def names(column)
  112. to_model(column).pluck(:name).uniq
  113. end
  114. end
  115. end