[9047] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Example code for random inserting points into the database.
|
---|
| 4 | #
|
---|
| 5 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
| 6 | from django.db import connection, transaction
|
---|
[9057] | 7 | from django.core.management.base import BaseCommand
|
---|
[9047] | 8 | from gheat.models import *
|
---|
[9057] | 9 | from optparse import OptionParser, make_option
|
---|
[9047] | 10 | import datetime
|
---|
| 11 | import os
|
---|
| 12 | import random
|
---|
| 13 | import sys
|
---|
| 14 |
|
---|
| 15 | def add_random_measurements(count):
|
---|
| 16 | # Create User/Equipment Objects
|
---|
| 17 | username = os.getenv('USER', 'foobar')
|
---|
| 18 | user, created = Gebruiker.objects.get_or_create(naam=username, email=username + '@example.org')
|
---|
| 19 | equipment, created = Apparatuur.objects.get_or_create(antenne='itern', kaart='device')
|
---|
| 20 |
|
---|
[9819] | 21 | accesspoint, created = Accesspoint.objects.get_or_create(mac='00:11:22:33:44',
|
---|
[9047] | 22 | ssid='ap-WirelessLeiden-Test', encryptie=True)
|
---|
| 23 |
|
---|
| 24 | # Mesurement run
|
---|
| 25 | rondje = MeetRondje.objects.create(datum=datetime.datetime.now(), naam='Test Ronde',
|
---|
| 26 | gebruiker=user, apparatuur=equipment)
|
---|
| 27 | rondje.save()
|
---|
| 28 |
|
---|
[9819] | 29 | sql_insert = "INSERT INTO gheat_meting (meetrondje_id, accesspoint_id, lat, lng, signaal) VALUES ";
|
---|
[9047] | 30 | sql_insert_values = []
|
---|
| 31 | for i in range(0,count):
|
---|
[9055] | 32 | latitude = 52 + random.uniform(0.140252,0.167846)
|
---|
| 33 | longitude = 4 + random.uniform(0.459019,0.514637)
|
---|
[9047] | 34 | signal = random.randint(0,100)
|
---|
| 35 | # This takes roughly 1 minute on 10000 items which logically cause it does
|
---|
| 36 | # 10000 sql commits! So use the SQL INSERT hack, instead.
|
---|
| 37 | #if (i % 1000) == 0:
|
---|
| 38 | # print "#Importing Random mesurements: %s/%s" % (i,count)
|
---|
| 39 | #meting = Meting.objects.create(meetrondje=rondje,
|
---|
[9819] | 40 | # accesspoint=accesspoint,latitude=latitude,
|
---|
[9047] | 41 | # longitude=longitude,signaal=signal)
|
---|
| 42 | #meting.save()
|
---|
[9819] | 43 | sql_insert_values.append("('" + "','".join(map(str,[rondje.id, accesspoint.id, latitude, longitude, signal])) + "')")
|
---|
[9047] | 44 |
|
---|
| 45 | sql_insert += ','.join(sql_insert_values) + ';';
|
---|
| 46 | cursor = connection.cursor()
|
---|
| 47 | cursor.execute(sql_insert)
|
---|
| 48 | transaction.commit_unless_managed()
|
---|
| 49 |
|
---|
| 50 |
|
---|
[9057] | 51 | class Command(BaseCommand):
|
---|
| 52 | option_list = BaseCommand.option_list + (
|
---|
| 53 | make_option("-c", "--count", dest="count", type="int", default=100, help="Number of points to add"),
|
---|
| 54 | )
|
---|
| 55 |
|
---|
| 56 | def handle(self, *args, **options):
|
---|
| 57 | add_random_measurements(options['count'])
|
---|
| 58 |
|
---|
[9047] | 59 | if __name__ == "__main__":
|
---|
| 60 | # Awefull hack for argument parsing
|
---|
[9056] | 61 | count = int(sys.argv[1]) if len(sys.argv) > 1 else 10000
|
---|
[9047] | 62 | add_random_measurements(count)
|
---|
| 63 |
|
---|