| 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
|
|---|
| 7 | from django.core.management.base import BaseCommand
|
|---|
| 8 | from gheat.models import *
|
|---|
| 9 | from optparse import OptionParser, make_option
|
|---|
| 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 |
|
|---|
| 21 | accespoint, created = Accespoint.objects.get_or_create(mac='00:11:22:33:44',
|
|---|
| 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 |
|
|---|
| 29 | sql_insert = "INSERT INTO gheat_meting (meetrondje_id, accespoint_id, lat, lng, signaal) VALUES ";
|
|---|
| 30 | sql_insert_values = []
|
|---|
| 31 | for i in range(0,count):
|
|---|
| 32 | latitude = 52 + random.uniform(0.140252,0.167846)
|
|---|
| 33 | longitude = 4 + random.uniform(0.459019,0.514637)
|
|---|
| 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,
|
|---|
| 40 | # accespoint=accespoint,latitude=latitude,
|
|---|
| 41 | # longitude=longitude,signaal=signal)
|
|---|
| 42 | #meting.save()
|
|---|
| 43 | sql_insert_values.append("('" + "','".join(map(str,[rondje.id, accespoint.id, latitude, longitude, signal])) + "')")
|
|---|
| 44 |
|
|---|
| 45 | sql_insert += ','.join(sql_insert_values) + ';';
|
|---|
| 46 | cursor = connection.cursor()
|
|---|
| 47 | cursor.execute(sql_insert)
|
|---|
| 48 | transaction.commit_unless_managed()
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 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 |
|
|---|
| 59 | if __name__ == "__main__":
|
|---|
| 60 | # Awefull hack for argument parsing
|
|---|
| 61 | count = int(sys.argv[1]) if len(sys.argv) > 1 else 10000
|
|---|
| 62 | add_random_measurements(count)
|
|---|
| 63 |
|
|---|