/
wrapper.py
155 lines (112 loc) · 4.47 KB
/
wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from flask import Flask
from flask import jsonify
from flask import request
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, query, Q, DocType, utils
from elasticsearch_dsl.exceptions import IllegalOperation
from flasgger import Swagger
es = Elasticsearch(timeout=60)
app = Flask(__name__)
from flask_cors import CORS, cross_origin
CORS(app)
app.config['SWAGGER'] = {
'title': 'Bitshares ES API',
'uiversion': 2
}
Swagger(app, template_file='wrapper.yaml')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
@app.route('/get_account_history')
def get_account_history():
account_id = request.args.get('account_id', False)
operation_type = request.args.get('operation_type', False)
from_ = request.args.get('from_', 0)
size = request.args.get('size', 10)
from_date = request.args.get('from_date', "2015-10-10")
to_date = request.args.get('to_date', "now")
sort_by = request.args.get('sort_by', "-block_data.block_time")
type = request.args.get('type', "data")
agg_field = request.args.get('agg_field', "operation_type")
if type != "data":
s = Search(using=es, index="graphene-*")
else:
s = Search(using=es, index="graphene-*", extra={"size": size, "from": from_})
q = Q()
if account_id and operation_type:
q = Q("match", account_history__account=account_id) & Q("match", operation_type=operation_type)
elif account_id and not operation_type:
q = Q("match", account_history__account=account_id)
elif not account_id and operation_type:
q = Q("match", operation_type=operation_type)
range_query = Q("range", block_data__block_time={'gte': from_date, 'lte': to_date})
s.query = q & range_query
if type != "data":
s.aggs.bucket('per_field', 'terms', field=agg_field, size=size)
s = s.sort(sort_by)
response = s.execute()
# print response.to_dict()
results = []
# print s.count()
if response:
if type == "data":
for hit in response:
results.append(hit.to_dict())
else:
for field in response.aggregations.per_field.buckets:
# print field.to_dict()
results.append(field.to_dict())
else:
print('"/get_account_history": Response is empty!!!!')
return jsonify(results)
@app.route('/get_trxs')
def get_trxs():
from_ = request.args.get('from_', 0)
size = request.args.get('size', 1)
from_date = request.args.get('from_date', "2015-10-10")
to_date = request.args.get('to_date', "now")
sort_by = request.args.get('sort_by', "-block_data.block_time")
s = Search(using=es, index="graphene-*", extra={"size": size, "from": from_})
q = Q()
range_query = Q("range", block_data__block_time={'gte': from_date, 'lte': to_date})
s.query = q & range_query
s.aggs.bucket('per_field', 'terms', field='block_data.trx_id.keyword', size=size).bucket(
'per_op', 'terms', field='account_history.operation_id.keyword', size=10000)
s = s.sort(sort_by)
# print('!!!! {}'.format(s.to_dict()))
response = s.execute()
results = []
if response:
for field in response.aggregations.per_field.buckets:
# print field.to_dict()
results.append(field.to_dict())
else:
print('"/get_trxs": Response is empty!!!!')
return jsonify(results)
@app.route('/get_single_operation')
def get_single_operation():
operation_id = request.args.get('operation_id', "1.11.0")
s = Search(using=es, index="graphene-*", extra={"size": 1})
q = Q("match", account_history__operation_id=operation_id)
s.query = q
response = s.execute()
results = []
for hit in response:
results.append(hit.to_dict())
return jsonify(results)
@app.route('/get_trx')
def get_trx():
trx = request.args.get('trx', "738be2bd22e2da31d587d281ea7ee9bd02b9dbf0")
from_ = request.args.get('from_', 0)
size = request.args.get('size', 10)
s = Search(using=es, index="graphene-*", extra={"size": size, "from": from_})
q = Q("match", block_data__trx_id=trx)
s.query = q
response = s.execute()
results = []
for hit in response:
# print hit.to_dict()
results.append(hit.to_dict())
return jsonify(results)
if __name__ == '__main__':
app.run()