Class: NdlStatistic

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ndl_statistic.rb

Constant Summary collapse

TYPE_LIST =
[ "all_items", "removed", "removed_sum" ]
REGION_LIST =
[ "domestic", "foreign" ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calc_sumObject

呼び出し用メソッド



19
20
21
22
23
# File 'app/models/ndl_statistic.rb', line 19

def self.calc_sum
  term = Term.current_term
  NdlStatistic.where(:term_id => term.id).destroy_all
  NdlStatistic.create!(:term_id => term.id).calc_all
end

.calc_sum_prev_yearObject



25
26
27
28
29
# File 'app/models/ndl_statistic.rb', line 25

def self.calc_sum_prev_year
  term = Term.previous_term
  NdlStatistic.where(:term_id => term.id).destroy_all
  NdlStatistic.create!(:term_id => term.id).calc_all 
end

Instance Method Details

#calc_accept_countsObject

  1. 受入



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/ndl_statistic.rb', line 110

def calc_accept_counts
  NdlStatistic.transaction do
    # 公開区分
    [ TRUE, FALSE].each do |pub_flg|
      items_all = Item.joins(:manifestation).
        where("bookbinding_id IS NULL OR items.bookbinder IS TRUE").  # 環境省以外は bookbinder_id
        where("items.created_at BETWEEN ? AND ?" ,@prev_term_end ,@curr_term_end)
      items_all = items_all.where("public_flg IS TRUE") unless pub_flg
      # 日本、外国
      [ "domestic", "foreign" ].each do |region|
        if region == "domestic"
          items = items_all.where("language_id = ?", @language_japanese_id)
        else
          items = items_all.where("language_id != ?", @language_japanese_id)
        end
        # 貸出区分/資料区分(環境省)
        @checkout_types.each do |checkout_type|
          # 資料形態
          @carrier_types.each do |carrier_type|
            # 受入区分
            @accept_types.each do |accept_type|
              count = items.where("checkout_type_id = ?", checkout_type.id).
                            where("carrier_type_id = ?", carrier_type.id).
                            where("accept_type_id = ?", accept_type.id).count
       # サブクラス生成
              ndl_stat_accepts.create(
         :region => region,
         :checkout_type_id => checkout_type.id,
         :carrier_type_id => carrier_type.id,
         :accept_type_id => accept_type.id,
                :pub_flg => pub_flg,
                :count => count)
            end
          end
        end
	end
    end
  end
rescue Exception => e
  p "Failed to accept counts: #{e}"
  logger.error "Failed to accept manifestation counts: #{e}"
end

#calc_access_countsObject

  1. アクセス件数



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/ndl_statistic.rb', line 184

def calc_access_counts
  NdlStatistic.transaction do 
    # 内部/外部
    [ TRUE, FALSE ].each do |internal|
      # アクセス画面
      AccessLog.group(:log_type).select(:log_type).map(&:log_type).each do |log_type|
        datas = AccessLog.where(:log_type => log_type, :internal => internal).
                          where("date between ? and ?", @prev_term_end, @curr_term_end)
        ndl_stat_accesses.create(
          :log_type => log_type,
          :internal => internal,
          :count => datas.sum(:value))
      end
    end
  end
rescue Exception => e
  p "Failed to access counts: #{e}"
  logger.error "Failed to access counts: #{e}"
end

#calc_allObject

NDL 年報用集計処理



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/ndl_statistic.rb', line 32

def calc_all
  # validates term_id
  begin
    @prev_term_end = Term.where(:id => term_id).first.start_at.yesterday
    @curr_term_end = Term.where(:id => term_id).first.end_at
    @language_japanese_id = Language.find_by_name('Japanese').id
    @circulation_removed_id = CirculationStatus.find_by_name('Removed').id
    @checkout_types = CheckoutType.all
    @carrier_types = CarrierType.all
    @accept_types = AcceptType.all
  rescue Exception => e
    p "Failed: #{e}"
    logger.error "Failed: #{e}"
    return false
  end
  # calculate ndl statistics
  self.calc_manifestation_counts
  self.calc_accept_counts
  self.calc_checkout_counts
  self.calc_access_counts
rescue Exception => e
  p "Failed to calculate ndl statistics: #{e}"
  logger.error "Failed to calculate ndl statistics: #{e}"
end

#calc_checkout_countsObject

  1. 利用



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/ndl_statistic.rb', line 154

def calc_checkout_counts
  NdlStatistic.transaction do
    # p "ndl_statistics of checkout_counts"
    # 貸出区分
    @checkout_types.each do |checkout_type|
      # 資料形態
      @carrier_types.each do |carrier_type|
        checkouts = Checkout.joins(:item => :manifestation).
                             where("checkout_type_id = ?", checkout_type.id).
                             where("carrier_type_id = ?", carrier_type.id)
        # 貸出者数
        user = checkouts.where("checkouts.created_at between ? and ?",
                                @prev_term_end, @curr_term_end).count
 # 貸出資料数
        item = checkouts.where("checkouts.created_at between ? and ?",
                                @prev_term_end, @curr_term_end).count
        ndl_stat_checkouts.create(
          :checkout_type_id => checkout_type.id,
          :carrier_type_id => carrier_type.id,
   :users_count => user,
   :items_count => item)
      end
    end
  end
rescue Exception => e
  p "Failed to checkout counts: #{e}"
  logger.error "Failed to calculate checkout counts: #{e}"
end

#calc_manifestation_countsObject

  1. 所蔵



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/ndl_statistic.rb', line 58

def calc_manifestation_counts
  NdlStatistic.transaction do
    # 公開区分
    [ TRUE, FALSE].each do |pub_flg|
      TYPE_LIST.each do |type|
        # 製本済み資料を除く # 環境省以外は bookbinding_id ではなく bookbinder_id
        query = "bookbinding_id IS NULL OR items.bookbinder IS TRUE AND items.created_at <= ?"
        case type
        when "all_items", "public_items"
          query += " AND circulation_status_id != #{@circulation_removed_id}"
          query += " AND public_flg IS TRUE" unless pub_flg
        when "removed", "removed_sum"
          query += " AND circulation_status_id = #{@circulation_removed_id}"
          query += " AND removed_at between ? and ?" if type == "removed"
        end
        items_all = type == "removed" ? Item.joins(:manifestation).where(query, @curr_term_end, @prev_term_end, @curr_term_end):
                                        Item.joins(:manifestation).where(query, @curr_term_end)
      
        # 日本、外国
        REGION_LIST.each do |region|
          if region == "domestic"
            items = items_all.where("language_id = ?", @language_japanese_id)
          else
            items = items_all.where("language_id != ?", @language_japanese_id)
          end
          # 貸出区分/資料区分(環境省)
          @checkout_types.each do |checkout_type|
            # 資料形態
            @carrier_types.each do |carrier_type|
              count = items.where("checkout_type_id = ?", checkout_type.id).
                            where("carrier_type_id = ?", carrier_type.id).count
              # サブクラス生成
              n= ndl_stat_manifestations.new(
                :stat_type => type,
         :region => region,
                :checkout_type_id => checkout_type.id,
                :carrier_type_id => carrier_type.id,
                :count => count)
              n.pub_flg = pub_flg
              n.save!
            end
          end
        end
	end
    end
  end
rescue Exception => e
  p "Failed to manifestation counts: #{e}"
  logger.error "Failed to calculate manifestation counts: #{e}"
end