forked from usc-isi-i2/SemanticLabelingService
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
944 lines (885 loc) · 39 KB
/
server.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
import traceback
from flask import Flask, request
from flask_cors import CORS
from flask_restful import Api, Resource
from flask_restful_swagger import swagger
import service.serverLogic
from service import *
app = Flask(__name__, static_folder='../static')
api = swagger.docs(Api(app), apiVersion='0.2', basePath='/', resourcePath='/',
produces=["application/json", "text/html"], api_spec_url='/api/spec', description='Semantic Typing')
CORS(app)
service = service.serverLogic.Server()
################################################################################################################################
#
# #
# This class is only used for what gets called by the API and swagger docs. There isn't any logic code here aside
# from the #
# try/catch for returning 500's and verifying parameters since I think it would be messy and harder to maintain.
# It's all #
# in service/serverLogic.py
# #
#
# #
# All of the constant values used here are set in service/__init__.py
# #
#
# #
# Each of the API functions in this class should call a helper function in the Server class of serverLogic.py whose
# name #
# follows the form of {class_name}_{type}, where {class_name} is the name of the class in this file,
# but with underscores #
# instead of camelCase and {type} is the HTTP method name, such as GET or DELETE. Example: semantic_types_get()
# #
# Note that POST and PUT call the same method since the code for each of them is nearly identical. These follow the
# form: #
# {class_name}_post_put(). Example: semantic_types_post_put()
# #
#
# #
################################################################################################################################
class parameters(object):
@staticmethod
def type_id(required=False, multiple=True, param_type="query"):
return {
"name": TYPE_ID_INPUT_PATH,
"enum": ["typeID", "class&&property"],
"description": "Format of id" if multiple else "Format of ids",
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def type_id_value(required=False, multiple=True, param_type="query"):
return {
"name": TYPE_ID_VALUE_PATH if param_type == "path" else TYPE_ID_VALUE_PATHS if multiple else
TYPE_ID_VALUE_PATH,
"description": "Id or name (class&&property) of the semantic type" if multiple
else "Ids or names (""class&&property) of the semantic types",
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def class_(required=False):
return {
"name": CLASS,
"description": "Name of a class",
"required": required,
"allowMultiple": False,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def property(required=False):
return {
"name": PROPERTY,
"description": "Name of a property",
"required": required,
"allowMultiple": False,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def namespaces(required=False):
return {
"name": NAMESPACES,
"description": "List of URIs of parent URIs of property which to consider",
"required": required,
"allowMultiple": True,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def source_names(required=False, desc="List of source names that the column(s) should have", multiple=True):
return {
"name": SOURCE_NAMES if multiple else SOURCE_NAME,
"description": desc,
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def column_names(required=False, desc="List of column names which the semantic type(s) should have", multiple=True):
return {
"name": COLUMN_NAMES if multiple else COLUMN_NAME,
"description": desc,
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def column_ids(required=False, desc="List of column ids which the semantic type(s) should have", multiple=True,
param_type="query"):
return {
"name": COLUMN_ID_PATH if param_type == "path" or multiple else COLUMN_IDS,
"description": desc,
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": param_type
}
@staticmethod
def models(required=False, desc="List of models which the column(s) should have", multiple=True):
return {
"name": MODELS if multiple else MODEL,
"description": desc,
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def return_column_data(desc="If the data in the columns should be in the return body"):
return {
"name": RETURN_COLUMN_DATA,
"description": desc,
"required": False,
"allowMultiple": False,
"dataType": "boolean",
"paramType": "query"
}
@staticmethod
def body(required=False,
desc="List of data values which will be inserted into the column (one per line), all lines will be "
"included as values, including blank ones"):
return {
"name": BODY,
"description": desc,
"required": required,
"allowMultiple": False,
"dataType": "string",
"paramType": "body"
}
@staticmethod
def model_names(required=False):
return {
"name": MODEL_NAMES,
"description": "Name of the models",
"required": required,
"allowMultiple": True,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def model(desc):
return {
"name": MODEL,
"description": desc,
"required": False,
"allowMultiple": False,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def model_desc(required=False):
return {
"name": MODEL_DESC,
"description": "Part or all of a model description",
"required": required,
"allowMultiple": False,
"dataType": "string",
"paramType": "query"
}
@staticmethod
def model_id(required=False, multiple=True, param_type="query"):
return {
"name": MODEL_ID_PATH if param_type == "path" else MODEL_IDS if multiple else MODEL_ID,
"description": "Id(s) of the model.json",
"required": required,
"allowMultiple": multiple,
"dataType": "string",
"paramType": param_type
}
@staticmethod
def crunch_data():
return {
"name": DO_NOT_CRUNCH_DATA_NOW,
"description": "If this is true, the model will not be evaluated and whatever is currently stored will be "
"used",
"required": False,
"allowMultiple": False,
"dataType": "boolean",
"paramType": "query"
}
class responses(object):
@staticmethod
def standard_get():
return [
{"code": 200, "message": "Success"},
{"code": 400, "message": "Bad Request"},
{"code": 404, "message": "Not Found"},
{"code": 500, "message": "Internal Server Error"}
]
@staticmethod
def standard_put():
return [
{"code": 201, "message": "Created"},
{"code": 400, "message": "Bad Request"},
{"code": 500, "message": "Internal Server Error"}
]
@staticmethod
def standard_post():
return [
{"code": 201, "message": "Created"},
{"code": 400, "message": "Bad Request"},
{"code": 409, "message": "Already exists"},
{"code": 500, "message": "Internal Server Error"}
]
@staticmethod
def standard_delete():
return [
{"code": 200, "message": "Deleted"},
{"code": 400, "message": "Bad Request"},
{"code": 404, "message": "Not Found"},
{"code": 500, "message": "Internal Server Error"}
]
class Predict(Resource):
@swagger.operation(
parameters=[
parameters.namespaces(),
parameters.column_names(False, "Name of the column the data may belong to", False),
parameters.models(False, "The model the data may belong to", False),
parameters.source_names(False, "Sources the data may belong to", True),
parameters.body(True, "List of data values to predict")
],
responseMessages=responses.standard_get()
)
def post(self):
"""
Predict the semantic type of data
Predicts the semantic type of the given data. Returns a list of types in the following format (sorted from
most to least likely):
<pre>
[
{
"type_id": "",
"score":
}
]
</pre>
"""
# print("request data", request.data)
try:
# print("request data", request.data)
if request.data is None or request.data == "": return "Invalid message body", 400
args = request.args.copy()
namespaces = args.pop(NAMESPACES).split(",") if args.get(NAMESPACES) else None
column_names = args.pop(COLUMN_NAME).split(",") if args.get(COLUMN_NAME) else None
source_names = args.pop(SOURCE_NAMES).split(",") if args.get(SOURCE_NAMES) else None
models = args.pop(MODEL).split(",") if args.get(MODEL) else None
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if column_names is None: column_names = [DEFAULT_NAME]
return service.predict_post(request.data.splitlines(), namespaces, column_names, source_names, models)
except Exception as e:
print(e)
return str(traceback.format_exc()), 500
class SemanticTypes(Resource):
@swagger.operation(
parameters=[
parameters.class_(),
parameters.property(),
parameters.namespaces(),
parameters.source_names(),
parameters.column_names(),
parameters.column_ids(),
parameters.models(),
{
"name": RETURN_COLUMNS,
"description": "If the columns for the semantic type(s) should be in the return body",
"required": False,
"allowMultiple": False,
"dataType": "boolean",
"paramType": "query"
},
parameters.return_column_data(
"If the data in the columns should be in the return body, if this is true it will override "
"returnColumns")
],
responseMessages=responses.standard_get()
)
def get(self):
"""
Get semantic types
Returns all of the semantic types which fit the given parameters.
Returned body will have the following format, but if returnColumns or returnColumnData is not given as true,
then "columns" will be omitted and if returnDataColumns is not given as true "data" will be omitted:
<pre>
[
{
"type_id": "",
"class": "",
"property": "",
"namespace": "",
"columns": [
{
"column_id": "",
"name": "",
"source": "",
"model": "",
"data": [
"",
"",
""
]
}
]
}
]
</pre>
Note that giving no parameters will return all semantic types with no columns.
"""
try:
args = request.args.copy()
class_ = args.pop(CLASS, None)
property_ = args.pop(PROPERTY, None)
namespaces = args.pop(NAMESPACES).split(",") if args.get(NAMESPACES) else None
source_names = args.pop(SOURCE_NAMES).split(",") if args.get(SOURCE_NAMES) else None
column_names = args.pop(COLUMN_NAMES).split(",") if args.get(COLUMN_NAMES) else None
column_ids = args.pop(COLUMN_IDS).split(",") if args.get(COLUMN_IDS) else None
models = args.pop(MODELS).split(",") if args.get(MODELS) else None
return_columns = args.pop(RETURN_COLUMNS, None)
return_column_data = args.pop(RETURN_COLUMN_DATA, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
return_column_data = True if return_column_data is not None and return_column_data.lower() == "true" else \
False
return_columns = True if return_columns is not None and return_columns.lower() == "true" else \
return_column_data
return service.semantic_types_get(class_, property_, namespaces, source_names, column_names, column_ids,
models, return_columns, return_column_data)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.class_(True),
parameters.property(True)
],
responseMessages=responses.standard_post()
)
def post(self):
"""
Create a semantic type
Creates a semantic type and returns its id.
"""
try:
args = request.args.copy()
class_ = args.pop(CLASS, None)
property_ = args.pop(PROPERTY, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if class_ is None or property_ is None: return "Both 'class' and 'property' must be specified", 400
return service.semantic_types_post_put(class_, property_, False)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.class_(True),
parameters.property(True)
],
responseMessages=responses.standard_put()
)
def put(self):
"""
Create/Replace a semantic type
Creates a semantic type if it doesn't exist or replaces it if it does, then returns its id. Note that
replacing a semantic type will remove all of it's columns.
"""
try:
args = request.args.copy()
class_ = args.pop(CLASS, None)
property_ = args.pop(PROPERTY, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if class_ is None or property_ is None: return "Both 'class' and 'property' must be specified", 400
return service.semantic_types_post_put(class_, property_, True)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.type_id(),
parameters.type_id_value(),
parameters.class_(),
parameters.property(),
parameters.namespaces(),
parameters.source_names(),
parameters.column_names(),
parameters.column_ids(),
parameters.models(),
{
"name": DELETE_ALL,
"description": "Set this to true to delete all semantic types",
"required": False,
"allowMultiple": False,
"dataType": "boolean",
"paramType": "query"
}
],
responseMessages=responses.standard_delete(),
)
def delete(self):
"""
Delete a semantic type
Deletes all semantic types which match the given parameters. Note that if no parameters are given a 400 will
be returned. If deleteAll is true, all semantic types will be deleted regardless of other parameters.
"""
try:
args = request.args.copy()
if len(args) < 1: return "At least one argument needs to be provided", 400
class_ = args.pop(CLASS, None)
property_ = args.pop(PROPERTY, None)
type_input = args.pop(TYPE_ID_INPUT_PATH, None)
type_id_values = args.pop(TYPE_ID_VALUE_PATHS).split(",") if args.get(TYPE_ID_VALUE_PATHS) else None
if str(type_input) == "class&&property":
type_ids = []
if type_id_values is not None:
for type_id_value in type_id_values:
try:
_class = type_id_value.split("&&")[0]
_property = type_id_value.split("&&")[1]
type_ids.append(get_type_id(_class, _property))
except:
return "Invalid format of class&&property", 400
else:
type_ids = type_id_values
if type_id_values is not None and type_input is None:
return "Type input needs to be selected", 400
if len(type_ids) == 0: type_ids = None
namespaces = args.pop(NAMESPACES).split(",") if args.get(NAMESPACES) else None
source_names = args.pop(SOURCE_NAMES).split(",") if args.get(SOURCE_NAMES) else None
column_names = args.pop(COLUMN_NAMES).split(",") if args.get(COLUMN_NAMES) else None
column_ids = args.pop(COLUMN_IDS).split(",") if args.get(COLUMN_IDS) else None
models = args.pop(MODELS).split(",") if args.get(MODELS) else None
delete_all = args.pop(DELETE_ALL, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
return service.semantic_types_delete(class_, property_, type_ids, namespaces, source_names, column_names,
column_ids, models, delete_all and delete_all.lower() == "true")
except:
return str(traceback.format_exc()), 500
class SemanticTypeColumns(Resource):
@swagger.operation(
parameters=[
parameters.type_id(True, False),
# parameters.type_id_value(True, False, "path"),
parameters.column_ids(desc="The ids of the column(s) to be returned"),
parameters.source_names(),
parameters.column_names(desc="The names of the column(s) to be returned"),
parameters.models(),
parameters.return_column_data()
],
responseMessages=responses.standard_get()
)
def get(self, type_id):
"""
Get the columns in a semantic type
Returns all of the columns in a semantic type that match the given parameters.
Returned body will have the following format, but if returnColumnData is not given as true, "data" will be
omitted:
<pre>
[
{
"column_id": "",
"name": "",
"source": "",
"model": "",
"data": [
"",
"",
""
]
}
]
</pre>
Note that giving no parameters will return all columns with no data.
"""
try:
args = request.args.copy()
format_type = args.pop(TYPE_ID_INPUT_PATH, None)
# type_id = args.pop(TYPE_ID_VALUE_PATH, None)
if type_id is None or len(type_id) < 1: return "Invalid type_id", 400
if str(format_type) == "class&&property":
try:
_class = type_id.split("&&")[0]
_property = type_id.split("&&")[1]
type_id = get_type_id(_class, _property)
except:
return "Invalid format of class&&property", 400
column_ids = args.pop(COLUMN_IDS).split(",") if args.get(COLUMN_IDS) else None
column_names = args.pop(COLUMN_NAMES).split(",") if args.get(COLUMN_NAMES) else None
source_names = args.pop(SOURCE_NAMES).split(",") if args.get(SOURCE_NAMES) else None
models = args.pop(MODELS).split(",") if args.get(MODELS) else None
return_column_data = args.pop(RETURN_COLUMN_DATA, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
return_column_data = True if return_column_data is not None and return_column_data.lower() == "true" else \
False
return service.semantic_types_columns_get(type_id, column_ids, column_names, source_names, models,
return_column_data)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.type_id(True, False),
# parameters.type_id_value(True, False, "path"),
parameters.column_names(True, "Name of the column to be created", False),
parameters.source_names(True, "Name of the source of the column to be created", False),
parameters.models(False, "Model of the column to be created, if none is given 'default' will be used",
False),
parameters.body(False)
],
responseMessages=responses.standard_post()
)
def post(self, type_id):
"""
Add a column to a semantic type
Creates the column and returns the id
"""
try:
args = request.args.copy()
format_type = args.pop(TYPE_ID_INPUT_PATH, None)
# type_id = args.pop(TYPE_ID_VALUE_PATH, None)
if type_id is None or len(type_id) < 1: return "Invalid type_id", 400
if str(format_type) == "class&&property":
try:
class_ = type_id.split("&&")[0]
property_ = type_id.split("&&")[1]
type_id = get_type_id(class_, property_)
except:
return "Invalid format of class&&property", 400
column_name = args.pop(COLUMN_NAME, None)
source_name = args.pop(SOURCE_NAME, None)
model = args.pop(MODEL, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if column_name is None or source_name is None: return "Either 'columnName' or 'sourceName' was omitted " \
"and they are both required"
if model is None: model = DEFAULT_MODEL
data = request.data.split(
"\n") if request.data is not None and request.data.strip() != "" and request.data.strip() != "{}" \
else []
return service.semantic_types_columns_post_put(type_id, column_name, source_name, model, data, False)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.type_id(True, False),
# parameters.type_id_value(True, False, "path"),
parameters.column_names(True, "Name of the column to be created", False),
parameters.source_names(True, "Name of the source of the column to be created", False),
parameters.models(False, "Model of the column to be created, if none is given 'default' will be used",
False),
parameters.body(False)
],
responseMessages=responses.standard_put()
)
def put(self, type_id):
"""
Add/Replace a column to a semantic type
Creates the column if it does not exist and replaces the column if it does, then returns the id if the column.
"""
try:
args = request.args.copy()
format_type = args.pop(TYPE_ID_INPUT_PATH, None)
# type_id = args.pop(TYPE_ID_VALUE_PATH, None)
if type_id is None or len(type_id) < 1: return "Invalid type_id", 400
if str(format_type) == "class&&property":
try:
class_ = type_id.split("&&")[0]
property_ = type_id.split("&&")[1]
type_id = get_type_id(class_, property_)
except:
return "Invalid format of class&&property", 400
column_name = args.pop(COLUMN_NAME, None)
source_name = args.pop(SOURCE_NAME, None)
model = args.pop(MODEL, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if column_name is None or source_name is None: return "Either 'columnName' or 'sourceColumn' was omitted " \
"and they are both required"
if model is None: model = DEFAULT_MODEL
data = request.data.split(
"\n") if request.data is not None and request.data.strip() != "" and request.data.strip() != "{}" \
else []
return service.semantic_types_columns_post_put(type_id, column_name, source_name, model, data, False)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.type_id(True, False),
# parameters.type_id_value(True, False, "path"),
parameters.column_ids(desc="The ids of the column(s) to be deleted"),
parameters.source_names(),
parameters.column_names(desc="The names of the column(s) to be deleted"),
parameters.models()
],
responseMessages=responses.standard_delete(),
)
def delete(self, type_id):
"""
Delete a column from a semantic type
Deletes all columns which match the given parameters. Note that if no parameters are given all columns in
that semantic type are deleted.
"""
try:
args = request.args.copy()
format_type = args.pop(TYPE_ID_INPUT_PATH, None)
# type_id = args.pop(TYPE_ID_VALUE_PATH, None)
if type_id is None or len(type_id) < 1: return "Invalid type_id", 400
if str(format_type) == "class&&property":
try:
class_ = type_id.split("&&")[0]
property_ = type_id.split("&&")[1]
type_id = get_type_id(class_, property_)
except:
return "Invalid format of class&&property", 400
column_ids = args.pop(COLUMN_IDS).split(",") if args.get(COLUMN_IDS) else None
column_names = args.pop(COLUMN_NAMES).split(",") if args.get(COLUMN_NAMES) else None
source_names = args.pop(SOURCE_NAMES).split(",") if args.get(SOURCE_NAMES) else None
models = args.pop(MODELS).split(",") if args.get(MODELS) else None
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
return service.semantic_types_columns_delete(type_id, column_ids, column_names, source_names, models)
except:
return str(traceback.format_exc()), 500
class SemanticTypeColumnData(Resource):
@swagger.operation(
# parameters=[parameters.column_ids(True, "The id of the column to get the info on", False, "path")],
responseMessages=responses.standard_get()
)
def get(self, column_id):
"""
Get the information on a column in a semantic type
Returns all of the information on a column in a semantic type.
Returned body will have the following format:
<pre>
{
"column_id": "",
"name": "",
"source": "",
"model": "",
"data": [
"",
"",
""
]
}
</pre>
"""
try:
if column_id is None or len(column_id) < 1: return "Invalid column_id", 400
if len(request.args) > 0: return "Invalid arguments, there should be none", 400
return service.semantic_types_column_data_get(column_id)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
# parameters.column_ids(True, "The ids of the column to add the data to", False, "path"),
parameters.body(True)
],
responseMessages=responses.standard_post()
)
def post(self, column_id):
"""
Adds data to the given column
Appends data to the given column. Use put to replace the data instead
"""
try:
if request.data is None or request.data == "": return "Invalid message body", 400
if column_id is None or len(column_id) < 1: return "Invalid column_id", 400
if len(request.args) > 0: return "Invalid arguments, there should be none", 400
return service.semantic_types_column_data_post_put(column_id, request.data, False)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
# parameters.column_ids(True, "The ids of the column to add the data to", False, "path"),
parameters.body(True)
],
responseMessages=responses.standard_put()
)
def put(self, column_id):
"""
Replaces the data in the column
Replaces the data in the column with the provided data
"""
try:
if request.data is None or request.data == "": return "Invalid message body", 400
if column_id is None or len(column_id) < 1: return "Invalid column_id", 400
if len(request.args) > 0: return "Invalid arguments, there should be none", 400
return service.semantic_types_column_data_post_put(column_id, request.data, True)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
# parameters=[parameters.column_ids(True, "The ids of the column to remove the data from", False, "path")],
responseMessages=responses.standard_delete(),
)
def delete(self, column_id):
"""
Delete all of the data in a column
Removes all of the data in the column
"""
try:
if column_id is None or len(column_id) < 1: return "Invalid column_id", 400
if len(request.args) > 0: return "Invalid arguments, there should be none", 400
return service.semantic_types_column_data_delete(column_id)
except:
return str(traceback.format_exc()), 500
class BulkAddModels(Resource):
@swagger.operation(
parameters=[
parameters.model_id(),
parameters.model_names(),
parameters.model_desc(),
parameters.crunch_data(),
{
"name": SHOW_ALL,
"description": "Show all of the model data",
"required": False,
"allowMultiple": False,
"dataType": "boolean",
"paramType": "query"
}
],
responseMessages=responses.standard_get()
)
def get(self):
"""
Get bulk add models
Returns all of the models which fit all of the given parameters (and all of them if no parameters are given).
If showAllData is true then the current state of each of the model.json files, otherwise "model" will be
omitted. If doNotCrunchDataNow is true, the learned semantic types will not be generated now,
instead whatever is in the db will be used, which may or may not be current. Every time a GET is run on a
model with this set to false (or not given at all) the model in the db will be updated as well as returned
as long as showAllData is true. Return body will have the following format:
<pre>
[
{
"modelId": "",
"name": "",
"description": "",
"model": { ... }
}
]
</pre>
"""
try:
args = request.args.copy()
model_ids = args.pop(MODEL_IDS).split(",") if args.get(MODEL_IDS) else None
model_names = args.pop(MODEL_NAMES).split(",") if args.get(MODEL_NAMES) else None
model_desc = args.pop(MODEL_DESC, None)
show_all = args.pop(SHOW_ALL, None)
crunch_data = args.pop(DO_NOT_CRUNCH_DATA_NOW, None)
if len(args) > 0: return json_response("The following query parameters are invalid: " + str(args.keys()),
400)
show_all = True if show_all is not None and show_all.lower() == "true" else False
crunch_data = False if crunch_data is not None and crunch_data.lower() == "false" else True
return service.bulk_add_models_get(model_ids, model_names, model_desc, show_all, crunch_data)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.model(
"The model for each of the created columns to be, if none is given 'bulk_add' will be used"),
parameters.body(True, "The model.json file")
],
responseMessages=responses.standard_put()
)
def post(self):
"""
Add a bulk add model
Add a bulk add model for adding information through POST /models/{model_id}, note that the id listed in the
model is the id assigned to it, so it is not returned and must be unique. The semantic types and columns
given in the model will be created when this is sent.
"""
try:
if request.data is None or len(request.data) < 1: return "Invalid message body", 400
args = request.args.copy()
column_model = args.pop(MODEL, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if column_model is None: column_model = DEFAULT_BULK_MODEL
return service.bulk_add_models_post(json.loads(request.data), column_model)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.model_id(),
parameters.model_names(),
parameters.model_desc()
],
responseMessages=responses.standard_delete()
)
def delete(self):
"""
Remove a bulk add model
Removes all models which fit all of the given parameters. Note that if no parameters are given all models
will be removed, but the semantic types and data inside them will be left intact.
"""
try:
args = request.args.copy()
model_ids = args.pop(MODEL_IDS).split(",") if args.get(MODEL_IDS) else None
model_names = args.pop(MODEL_NAMES).split(",") if args.get(MODEL_NAMES) else None
model_desc = args.pop(MODEL_DESC, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
return service.bulk_add_models_delete(model_ids, model_names, model_desc)
except:
return str(traceback.format_exc()), 500
class BulkAddModelData(Resource):
@swagger.operation(
parameters=[
parameters.model_id(True, False, "path"),
parameters.crunch_data()
],
responseMessages=responses.standard_get()
)
def get(self, model_id):
"""
Gets the current state of a bulk add model
Returns the current state of the given bulk add model id. If doNotCrunchDataNow is true, the learned
semantic types will not be generated now, instead whatever is in the db will be used, which may or may not be
current. Every time a GET is run on a model with this set to false (or not given at all) the model in the db
will be updated as well as returned.
"""
try:
if model_id is None or len(model_id) < 1: return "Invalid model_id", 400
args = request.args.copy()
crunch_data = args.pop(DO_NOT_CRUNCH_DATA_NOW, None)
if len(args) > 0: return json_response("The following query parameters are invalid: " + str(args.keys()),
400)
crunch_data = False if crunch_data is not None and crunch_data.lower() == "false" else True
return service.bulk_add_model_data_get(model_id, crunch_data)
except:
return str(traceback.format_exc()), 500
@swagger.operation(
parameters=[
parameters.model_id(True, False, "path"),
parameters.model(
"The model of the columns the data should be sent to, if none is given 'bulk_add' will be used"),
parameters.body(True, "The jsonlines which contain the data to add")
],
responseMessages=responses.standard_put()
)
def post(self, model_id):
"""
Add data to the semantic types
Adds data from jsonlines into the semantic types. Each line of the body should be a full json file,
with everything specified in the model.json. This is the same as using POST /semantic_types/{type_id} to add
data to columns, but faster for large amounts of data.
"""
try:
if model_id is None or len(model_id) < 1: return "Invalid model_id", 400
if request.data is None or len(request.data) < 1: return "Invalid message body", 400
args = request.args.copy()
column_model = args.pop(MODEL, None)
if len(args) > 0: return "The following query parameters are invalid: " + str(args.keys()), 400
if column_model is None: column_model = DEFAULT_BULK_MODEL
data = []
for line in request.data.split("\n"):
if line.strip() != "":
data.append(json.loads(line))
return service.bulk_add_model_data_post(model_id, column_model, data)
except:
return str(traceback.format_exc()), 500
api.add_resource(Predict, "/predict")
api.add_resource(SemanticTypes, "/semantic_types")
api.add_resource(SemanticTypeColumns, "/semantic_types/<string:" + TYPE_ID_PATH + ">")
api.add_resource(SemanticTypeColumnData, "/semantic_types/type/<string:" + COLUMN_ID_PATH + ">")
api.add_resource(BulkAddModels, "/bulk_add_models")
api.add_resource(BulkAddModelData, "/bulk_add_models/<string:" + MODEL_ID_PATH + ">")
app.run(debug=True, port=5000, use_reloader=False)