elasticsearch-secure/generate/hamlet.py

raw link view readme
1 #+ copyright (c) 2015-2018 David Betz
2
3 import datetime
4 import sys, os
5 from random import randint
6 from time import time
7 import requests
8 import json
9
10 from random import randrange, randint
11
12 with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'hamlet_distinct.t'), 'r') as f:
13 hamlet_all = f.read()
14
15 genData = hamlet_all.split(' ')
16
17 def hamlet(count):
18 return genData[randint(1, len(genData) - 1)] + ('' if count == 1 else ' ' + hamlet(count - 1))
19
20 endpoint = 'http://127.0.0.1:9200/librarygen'
21
22 def call(verb, endpoint, obj = None):
23 headers = {
24 "Accept": "application/json",
25 }
26 endpoint = endpoint.lower()
27
28 verb = verb.lower()
29
30 jsonData = json.dumps(obj)
31
32 if verb == 'get':
33 response = requests.get(endpoint, headers=headers)
34 elif verb == 'post':
35 response = requests.post(endpoint, headers=headers, data=jsonData)
36 elif verb == 'put':
37 response = requests.put(endpoint, headers=headers, data=jsonData)
38 elif verb == 'delete':
39 response = requests.delete(endpoint, headers=headers)
40
41 return response
42
43 def create_date():
44 return '{}-{:02}-{:02}T{:02}:{:02}:{:02}Z'.format(randint(2006, 2016), randint(1, 12), randint(1, 28), randint(0, 23), randint(0, 59), randint(0, 59))
45
46 call('PUT', endpoint, {
47 "settings": {
48 "index": {
49 "number_of_replicas": 1,
50 "number_of_shards": 6
51 }
52 }
53 })
54
55 def run():
56 then = time()
57 count = 0
58 while True:
59 try:
60 item = {
61 "title": hamlet(4),
62 "authors": [hamlet(_) for _ in range(1, randrange(2,4))],
63 "editor": hamlet(1) if randrange(4) == 0 else None,
64 "abstract": hamlet(randrange(100, 400)),
65 "metadata": {
66 "pages": randrange(1,400),
67 "isbn": '9780' + str(randrange(100000000, 999999999)),
68 "genre": hamlet(1),
69 },
70 "created": create_date(),
71 "modified": create_date(),
72 }
73 call('POST', '{}/book'.format(endpoint), item)
74 count = count + 1
75 except KeyboardInterrupt:
76 now = time()
77 print('Stopped ({})'.format(count / (now - then)))
78 sys.exit(0)
79
80
81 if __name__ == '__main__':
82 run()
83