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 | import settings
|
---|
7 | from django.core.management import setup_environ
|
---|
8 | setup_environ(settings)
|
---|
9 |
|
---|
10 |
|
---|
11 | from django.db import connection, transaction
|
---|
12 | from gheat.models import *
|
---|
13 | import datetime
|
---|
14 | import os
|
---|
15 | import random
|
---|
16 | import sys
|
---|
17 |
|
---|
18 | def add_random_measurements(count):
|
---|
19 | # Create User/Equipment Objects
|
---|
20 | username = os.getenv('USER', 'foobar')
|
---|
21 | user, created = Gebruiker.objects.get_or_create(naam=username, email=username + '@example.org')
|
---|
22 | equipment, created = Apparatuur.objects.get_or_create(antenne='itern', kaart='device')
|
---|
23 |
|
---|
24 | accespoint, created = Accespoint.objects.get_or_create(mac='00:11:22:33:44',
|
---|
25 | ssid='ap-WirelessLeiden-Test', encryptie=True)
|
---|
26 |
|
---|
27 | # Mesurement run
|
---|
28 | rondje = MeetRondje.objects.create(datum=datetime.datetime.now(), naam='Test Ronde',
|
---|
29 | gebruiker=user, apparatuur=equipment)
|
---|
30 | rondje.save()
|
---|
31 |
|
---|
32 | sql_insert = "INSERT INTO gheat_meting (meetrondje_id, accespoint_id, lat, lng, signaal) VALUES ";
|
---|
33 | sql_insert_values = []
|
---|
34 | for i in range(0,count):
|
---|
35 | latitude = 52 + random.uniform(0.140252,0.167846)
|
---|
36 | longitude = 4 + random.uniform(0.459019,0.514637)
|
---|
37 | signal = random.randint(0,100)
|
---|
38 | # This takes roughly 1 minute on 10000 items which logically cause it does
|
---|
39 | # 10000 sql commits! So use the SQL INSERT hack, instead.
|
---|
40 | #if (i % 1000) == 0:
|
---|
41 | # print "#Importing Random mesurements: %s/%s" % (i,count)
|
---|
42 | #meting = Meting.objects.create(meetrondje=rondje,
|
---|
43 | # accespoint=accespoint,latitude=latitude,
|
---|
44 | # longitude=longitude,signaal=signal)
|
---|
45 | #meting.save()
|
---|
46 | sql_insert_values.append("('" + "','".join(map(str,[rondje.id, accespoint.id, latitude, longitude, signal])) + "')")
|
---|
47 |
|
---|
48 | sql_insert += ','.join(sql_insert_values) + ';';
|
---|
49 | cursor = connection.cursor()
|
---|
50 | cursor.execute(sql_insert)
|
---|
51 | transaction.commit_unless_managed()
|
---|
52 |
|
---|
53 |
|
---|
54 | if __name__ == "__main__":
|
---|
55 | # Awefull hack for argument parsing
|
---|
56 | count = int(sys.argv[0]) if len(sys.argv) > 1 else 10000
|
---|
57 | add_random_measurements(count)
|
---|
58 |
|
---|