call.rb - warvox - VoIP based wardialing tool, forked from rapid7/warvox.
 (HTM) git clone git://jay.scot/warvox
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       call.rb (4559B)
       ---
            1 # == Schema Information
            2 #
            3 # Table name: calls
            4 #
            5 #  id                    :integer          not null, primary key
            6 #  created_at            :datetime
            7 #  updated_at            :datetime
            8 #  number                :text             not null
            9 #  project_id            :integer          not null
           10 #  job_id                :integer          not null
           11 #  provider_id           :integer          not null
           12 #  answered              :boolean
           13 #  busy                  :boolean
           14 #  error                 :text
           15 #  audio_length          :integer
           16 #  ring_length           :integer
           17 #  caller_id             :text
           18 #  analysis_job_id       :integer
           19 #  analysis_started_at   :datetime
           20 #  analysis_completed_at :datetime
           21 #  peak_freq             :float
           22 #  peak_freq_data        :text
           23 #  line_type             :text
           24 #  fprint                :integer          is an Array
           25 #
           26 
           27 class Call < ApplicationRecord
           28 
           29   reportable :hourly, aggregation: :count, grouping: :hour, live_data: true, cacheable: false, limit: 24
           30   reportable :daily, aggregation: :count, grouping: :day, live_data: true, cacheable: false, limit: 7
           31   reportable :weekly, aggregation: :count, grouping: :week, live_data: true, cacheable: false, limit: 52
           32   reportable :monthly, aggregation: :count, grouping: :month, live_data: true, cacheable: false, limit: 12
           33 
           34   reportable :analyzed_hourly, aggregation: :count, grouping: :hour, date_column: :analysis_completed_at, live_data: true, cacheable: false, limit: 24
           35   reportable :analyzed_daily, aggregation: :count, grouping: :day, date_column: :analysis_completed_at, live_data: true, cacheable: false, limit: 7
           36   reportable :analyzed_weekly, aggregation: :count, grouping: :week, date_column: :analysis_completed_at, live_data: true, cacheable: false, limit: 52
           37   reportable :analyzed_monthly, aggregation: :count, grouping: :month, date_column: :analysis_completed_at, live_data: true, cacheable: false, limit: 12
           38 
           39   belongs_to :project
           40   belongs_to :provider
           41   belongs_to :job
           42   has_one :call_medium, dependent: :delete
           43 
           44   def matches
           45     #    "AND (( icount(\'{#{fprint.map{|x| x.to_s}.join(",")}}\'::int[] & calls.fprint::int[]) / icount(\'{#{fprint.map{|x| x.to_s}.join(",")}}'::int[])::float ) * 100.0 ) > 10.0 " +
           46     self.find_by_sql([
           47       'SELECT calls.*,  ' +
           48       "  (( icount(ARRAY[?]::int[] & calls.fprint::int[]) / icount(ARRAY[?]::int[])::float ) * 100.0 ) AS matchscore " +
           49       'FROM calls ' +
           50       'WHERE icount(calls.fprint) > 0 AND ' +
           51       "calls.job_id = ? AND " +
           52       "calls.id != ? " +
           53       'ORDER BY matchscore DESC',
           54       fprint_map,
           55       fprint_map,
           56       self.job_id,
           57       self.id
           58       ])
           59   end
           60 
           61   def matches_all_jobs
           62 
           63     #    "AND (( icount(\'{#{fprint.map{|x| x.to_s}.join(",")}}\'::int[] & calls.fprint::int[]) / icount(\'{#{fprint.map{|x| x.to_s}.join(",")}}'::int[])::float ) * 100.0 ) > 10.0 " +
           64     self.find_by_sql([
           65       'SELECT calls.*,  ' +
           66       "  (( icount(ARRAY[?]::int[] & calls.fprint::int[]) / icount(ARRAY[?]::int[])::float ) * 100.0 ) AS matchscore " +
           67       'FROM calls ' +
           68       'WHERE icount(calls.fprint) > 0 AND ' +
           69       "calls.id != ? " +
           70       'ORDER BY matchscore DESC',
           71       fprint,
           72       fprint,
           73       self.id
           74       ])
           75   end
           76 
           77   after_save :update_linked_line
           78 
           79   def paginate_matches(scope, min_match, page, per_page)
           80 
           81     match_sql =
           82       'SELECT calls.*,  ' +
           83       "  (( icount(ARRAY[?]::int[] & calls.fprint::int[]) / icount(ARRAY[?]::int[])::float ) * 100.0 ) AS matchscore " +
           84       'FROM calls ' +
           85       'WHERE icount(calls.fprint) > 0 AND '
           86     args = [fprint, fprint]
           87 
           88     case scope
           89     when 'job'
           90       match_sql << " calls.job_id = ? AND "
           91       args << job.id.to_i
           92     when 'project'
           93       match_sql << " calls.project_id = ? AND "
           94       args << project_id.to_i
           95     end
           96 
           97     match_sql << "calls.id != ? "
           98     args << self.id
           99 
          100     match_sql << " AND (( icount(ARRAY[?]::int[] & calls.fprint::int[]) / icount(ARRAY[?]::int[])::float ) * 100.0 ) > ? ORDER BY matchscore DESC"
          101     args << fprint
          102     args << fprint
          103     args << min_match.to_f
          104 
          105     query = [match_sql, *args]
          106     Call.paginate_by_sql(query, page: page, per_page: per_page)
          107   end
          108 
          109   def media
          110     CallMedium.where(call_id: self.id, project_id: self.project_id).first_or_create
          111   end
          112 
          113   def media_fields
          114     CallMedium.columns_hash.keys.reject{|x| x =~ /^id|_id$/}
          115   end
          116 
          117   def linked_line
          118     Line.where(number: self.number, project_id: self.project_id).first_or_create
          119   end
          120 
          121   def update_linked_line
          122     line = linked_line
          123 
          124     if self[:line_type]
          125       line.line_type = self[:line_type]
          126       line.save
          127     end
          128   end
          129 
          130 end