Class: Geoptima::Event

Inherits:
Object
  • Object
show all
Includes:
ErrorCounter, Locatable
Defined in:
lib/geoptima/data.rb

Overview

The Geoptima::Event class represents and individual record or event

Constant Summary collapse

KNOWN_HEADERS =
{
  "gps" => ["timeoffset","latitude","longitude","altitude","accuracy","direction","speed"],
  "service" => ["timeoffset","plmn","cell_id","lac","mnc","mcc"],
  "call" => ["timeoffset","status","number"],
  "runningApps" => ["timeoffset","appName","state"],
  "batteryState" => ["timeoffset","state"],
  "trafficSpeed" => ["timeoffset","interface","direction","delay","speed"],
  "storageStatus" => ["timeoffset","path","totalSize","freeSize"],
  "signal" => ["timeoffset","strength","rxqual","ecio"],
  "roundtrip" => ["timeoffset","interface","address","type","roundtripTime"],
  "httpRequest" => ["timeoffset","interface","address","delay","speed"],
  "dnsLookup" => ["timeoffset","interface","address","lookupTime","ip"],
  "ftpSpeed" => ["timeoffset","interface","direction","delay","peak","speed"],
  "browserDedicatedTest" => ["timeoffset","url","pageRenders","pageRendered","pageSize","success"],
  "pingTest" => ["timeoffset","interface","address","count","length","pingTime","packetLossPercent","jitter","error"]
}
HEADER_BUGS =
{
  'ftpSpeed' => '#4303',
  'pingTest' => '#4509'
}
ALT_HEADERS =
{
  "pingTest" => [
    ["timeoffset","interface","address","count","length","pingTime","packetLossPercent","jitter","error"],
    ["timeoffset","id","interface","address","count","length","pingTime","packetLossPercent","jitter","error"]
  ],
  "ftpSpeed" => [
    ["timeoffset","interface","direction","delay","speed"],
    ["timeoffset","interface","direction","delay","peak","speed"],
    ["timeoffset","interface","direction","delay","peak","speed","error"],
    ["timeoffset","interface","direction","delay","peak","speed","size","error"]
  ]
}

Instance Attribute Summary collapse

Attributes included from Locatable

#next_gps, #next_point, #next_point_gap, #previous_gps, #previous_point, #previous_point_gap

Attributes included from ErrorCounter

#errors

Instance Method Summary collapse

Methods included from Locatable

#set_next_if, #set_previous_if

Methods included from ErrorCounter

#combine_errors, #incr_error, #report_errors

Constructor Details

#initialize(file, start, name, header, data, previous = nil) ⇒ Event

Returns a new instance of Event.



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/geoptima/data.rb', line 488

def initialize(file,start,name,header,data,previous=nil)
  @file = file
  @name = name
  @header = header
  @data = data
  @fields = @header.inject({}) do |a,v|
    a[v] = check_field(@data[a.length])
    a
  end
  @timeoffset = (@fields['timeoffset'].to_f / MSPERDAY.to_f)
  @time = start + timeoffset # Note we set this again later after corrections (need it now for puts output)
  if(@timeoffset<-0.0000001)
    puts "Have negative time offset: #{@fields['timeoffset']}" if($debug)
    incr_error "#4506 negative offsets"
  end
  if previous
    prev_to = previous.timeoffset
    puts "Comparing timeoffset:#{timeoffset} to previous:#{prev_to}" if($debug)
    if @timeoffset == prev_to
      puts "Found the same timeoffset in consecutive events: #{name}:#{timeoffset} == #{previous.name}:#{previous.timeoffset}"
      incr_error "#4576 same timeoffset"
      @timeoffset = @timeoffset + 1.0 / MSPERDAY.to_f
    end
  end
  @time = start + timeoffset
  @fields.reject!{|k,v| k=~/timeoffset/}
  if @fields['cell_id'].to_i > SHORT
    @fields['cell_id'] = @fields['cell_id'].to_i % SHORT
  end
  incr_error "Empty data" if(data.length == 0)
  puts "Created Event: #{self}" if($debug)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def data
  @data
end

#fieldsObject (readonly)

Returns the value of attribute fields.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def fields
  @fields
end

#fileObject (readonly)

Returns the value of attribute file.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def file
  @file
end

#gpsObject (readonly)

Returns the value of attribute gps.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def gps
  @gps
end

#headerObject (readonly)

Returns the value of attribute header.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def header
  @header
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def longitude
  @longitude
end

#nameObject (readonly)

Returns the value of attribute name.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def name
  @name
end

#timeObject (readonly)

Returns the value of attribute time.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def time
  @time
end

#timeoffsetObject (readonly)

Returns the value of attribute timeoffset.



487
488
489
# File 'lib/geoptima/data.rb', line 487

def timeoffset
  @timeoffset
end

Instance Method Details

#-(other) ⇒ Object



541
542
543
# File 'lib/geoptima/data.rb', line 541

def -(other)
  (self.time - other.time) * SPERDAY
end

#[](key) ⇒ Object



535
536
537
# File 'lib/geoptima/data.rb', line 535

def [](key)
  @fields[key] || @fields[key.gsub(/#{name}\./,'')]
end

#[]=(key, value) ⇒ Object



538
539
540
# File 'lib/geoptima/data.rb', line 538

def []=(key,value)
  @fields[key] ||= value
end

#check_field(field) ⇒ Object



520
521
522
# File 'lib/geoptima/data.rb', line 520

def check_field(field)
  (field && field.respond_to?('length') && field =~ /\d\,\d/) ? field.gsub(/\,/,'.').to_f : field
end

#closer_than(other, seconds = 60) ⇒ Object



544
545
546
# File 'lib/geoptima/data.rb', line 544

def closer_than(other,seconds=60)
  (self - other).abs < seconds
end

#days_from(other) ⇒ Object



529
530
531
# File 'lib/geoptima/data.rb', line 529

def days_from(other)
  (other.time - time).abs
end

#descriptionObject



569
570
571
# File 'lib/geoptima/data.rb', line 569

def description
  "#{name}"
end

#distance_from(other) ⇒ Object



532
533
534
# File 'lib/geoptima/data.rb', line 532

def distance_from(other)
  Math.sqrt((other.latitude.to_f - latitude.to_f)**2 + (other.longitude.to_f - longitude.to_f)**2)
end

#gpx_idObject



575
576
577
# File 'lib/geoptima/data.rb', line 575

def gpx_id
  file.id
end

#locationObject



553
554
555
# File 'lib/geoptima/data.rb', line 553

def location
  @location ||= latitude && Point.new(latitude,longitude)
end

#puts(line) ⇒ Object



563
564
565
# File 'lib/geoptima/data.rb', line 563

def puts line
  Kernel.puts "#{name}[#{time}]: #{line}"
end

#set_location(gps) ⇒ Object



556
557
558
559
560
561
562
# File 'lib/geoptima/data.rb', line 556

def set_location(gps)
  incr_error "GPS String Data" if(gps['latitude'].is_a? String)
  @latitude = gps['latitude'].to_f
  @longitude = gps['longitude'].to_f
  @location = nil
  @gps = gps
end

#time_keyObject



526
527
528
# File 'lib/geoptima/data.rb', line 526

def time_key
  utc.strftime("%Y-%m-%d %H:%M:%S.%3N").gsub(/\.(\d{3})\d+/,'.\1')
end

#to_sObject



566
567
568
# File 'lib/geoptima/data.rb', line 566

def to_s
  "#{name}[#{time}]: #{@fields.inspect}"
end

#to_typeObject



578
579
580
581
582
583
584
585
# File 'lib/geoptima/data.rb', line 578

def to_type
  case name
  when 'runningApps'
    RunningApps.new(self)
  else
    self
  end
end

#utcObject



523
524
525
# File 'lib/geoptima/data.rb', line 523

def utc
  time.new_offset(0)
end

#valid_gpx?Boolean

Returns:

  • (Boolean)


572
573
574
# File 'lib/geoptima/data.rb', line 572

def valid_gpx?
  location
end