Changeset 9181 for src/django_gheat/gheat/management/commands
- Timestamp:
- May 12, 2011, 8:20:33 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/management/commands/import_droidstumbler.py
r9180 r9181 11 11 from django.core.management import setup_environ 12 12 from django.core.management.base import BaseCommand, CommandError 13 from django.db import connection, transaction 13 14 from django.db.utils import IntegrityError 14 15 from optparse import OptionParser, make_option … … 21 22 import sys 22 23 23 def user_feedback( count, dot_step, summary_report):24 def user_feedback(output, count, dot_step, summary_report): 24 25 if (count % summary_report) == 0: 25 sys.stdout.write(str(count))26 output.write(str(count)) 26 27 elif (count % dot_step) == 0: 27 sys.stdout.write(".")28 sys.stdout.flush()28 output.write(".") 29 output.flush() 29 30 return count + 1 30 31 31 32 32 def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False ):33 def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False,output=sys.stdout,bulk_import=True): 33 34 """ Import all points, return tuple with summary""" 34 35 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email) … … 41 42 meting_pool = {} 42 43 43 # Process file44 if show_progres: output.write('#INFO: Pre-Processing file: ') 44 45 if location.endswith('.gz'): 45 46 fh = gzip.open(location,'rb') … … 47 48 fh = open(location,'rb') 48 49 csvfile = csv.reader(fh, delimiter=',') 50 count = 0 49 51 for row in csvfile: 52 if show_progres: count = user_feedback(output, count, 100, 1000) 50 53 try: 51 54 epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row … … 65 68 else: 66 69 meting_pool[key] = signaal 70 if show_progres: output.write("%s\n" % count) 67 71 68 72 69 # Import the data into the database73 if show_progres: output.write('#INFO: Importing the data into the database: ') 70 74 meting_count = 0 75 cursor = connection.cursor() 76 sql_values = [] 71 77 for (ap,lat,lon),signal in meting_pool.iteritems(): 72 78 try: 73 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal) 79 if bulk_import: 80 value_str = str((int(mr.id),int(ap.id),float(lat),float(lon),int(signaal))) 81 sql_values.append(value_str) 82 else: 83 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal) 74 84 except IntegrityError, e: 75 85 logging.error("Unable to import - %s" % e) … … 77 87 78 88 # Give some feedback to the user 79 if show_progres: 80 meting_count = user_feedback(meting_count, 100, 1000) 89 if show_progres: meting_count = user_feedback(output, meting_count, 100, 1000) 81 90 82 if show_progres: 83 sys.stdout.write("%s\n" % meting_count) 91 # Make sure to include closing newline 92 if show_progres: output.write("%s\n" % meting_count) 93 if bulk_import: 94 sql = "INSERT INTO gheat_meting (`meetrondje_id`, `accespoint_id`, `lat`, `lng`, `signaal`) VALUES %s" % ','.join(sql_values) 95 cursor.execute(sql) 96 transaction.commit_unless_managed() 84 97 85 98 return (len(ap_cache), new_ap_count, meting_count, len(meting_pool) - meting_count)
Note:
See TracChangeset
for help on using the changeset viewer.