Changeset 9179


Ignore:
Timestamp:
May 12, 2011, 7:34:26 AM (14 years ago)
Author:
rick
Message:

Make sure points are unique (max value uses) to avoids duplicates all over the
database. Re-init database to make sure settings are into effect.

Do pre-processing on import_droidstumbler to make sure input values _are_ unique.

Location:
src/django_gheat/gheat
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/gheat/management/commands/import_droidstumbler.py

    r9178 r9179  
    22# -*- coding: utf-8 -*-
    33#
    4 # Script for importing DroidStumbler .csv files
     4# Script for importing DroidStumbler .csv files, which takes the best value of
     5# each measurement point.
    56#
    67# Original by Dennis Wagenaar <d.wagenaar@gmail.com>
     
    1011from django.core.management import setup_environ
    1112from django.core.management.base import BaseCommand, CommandError
     13from django.db.utils import IntegrityError
    1214from optparse import OptionParser, make_option
    1315from gheat.models import *
     
    1921import sys
    2022
     23def user_feedback(count, dot_step, summary_report):
     24  if (count % summary_report) == 0:
     25    sys.stdout.write(str(count))
     26  elif (count % dot_step) == 0:
     27    sys.stdout.write(".")
     28  sys.stdout.flush()
     29  return count + 1
     30
     31
    2132def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False):
    2233
    2334  g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
    2435  a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
    25   mr = MeetRondje.objects.create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a)
     36  mr, created = MeetRondje.objects.get_or_create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a)
    2637
     38  ap_cache = {}
     39  meting_pool = {}
     40
     41  # Process file
    2742  if location.endswith('.gz'):
    2843    fh = gzip.open(location,'rb')
     
    3045    fh = open(location,'rb')
    3146  csvfile = csv.reader(fh, delimiter=',')
    32   count = 0
    33   ap_cache = {}
    3447  for row in csvfile:
    3548    try:
     
    4255        ap_cache[bssid], created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities)
    4356
    44       m = Meting.objects.create(meetrondje=mr, accespoint=ap_cache[bssid], latitude=lat, longitude=lon, signaal=(100 + int(level)))
    45       # Give some feedback to the user
    46       count += 1
    47       if show_progres:
    48         if (count % 1000) == 0:
    49           sys.stdout.write(str(count))
    50         elif (count % 100) == 0:
    51           sys.stdout.write(".")
    52         sys.stdout.flush()
     57      # We store the best value found
     58      key = (ap_cache[bssid], lat, lon)
     59      signaal=(100 + int(level))
     60      if meting_pool.has_key(key):
     61        meting_pool[key] = max(meting_pool[key], signaal)
     62      else:
     63        meting_pool[key] = signaal
     64
     65
     66  # Import the data into the database
     67  count = 0
     68  for (ap,lat,lon),signal in meting_pool.iteritems():
     69    try:
     70      m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal)
     71    except IntegrityError, e:
     72      logging.error("Unable to import - %s" %  e)
     73      continue
     74
     75    # Give some feedback to the user
     76    if show_progres:
     77      count = user_feedback(count, 100, 1000)
    5378
    5479  if show_progres:
     
    91116      sys.exit(1)
    92117
     118    self.stdout.write('Meetrondje: %s @ %s\n' % (meetrondje, datum))
    93119    self.stdout.write("Going to import '%s' for gebruiker '%s <%s>'\n" % (os.path.basename(csv_file), options['gebruiker'], options['email']))
    94120    import_droidstumbler(csv_file,meetrondje,options['gebruiker'],options['email'], datum, True)
    95     self.stdout.writeln("All done, goodbye")
     121    self.stdout.write("All done, goodbye")
  • src/django_gheat/gheat/models.py

    r9164 r9179  
    3939  def __unicode__(self):
    4040    return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
     41  class Meta:
     42    # This implies that you cannot have multiple messurements on the same
     43    # location for a single 'run', thus the data needs cleaned before to make
     44    # this properly hold.
     45    unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
    4146
    4247class Node(models.Model):
Note: See TracChangeset for help on using the changeset viewer.