]> git.somenet.org - pub/jan/aic18.git/blob - camunda-overlay/camunda.py
Renamed docker-compose.yml to "g6t4" as thats a submission requirement ¯\_(ツ)_/¯
[pub/jan/aic18.git] / camunda-overlay / camunda.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import json
6 import requests
7 import argparse
8 from pprint import pprint
9
10 CAMUNDA="http://localhost:8080/engine-rest/"
11
12 def get_current_deployments(key = 'sentiment-analysis'):
13     res = requests.get(CAMUNDA + "deployment")
14     #return [proc for proc in res.json() if proc['name'] == key]
15     return res.json()
16
17 def get_current_process_instances(key = 'sentiment-analysis'):
18     res = requests.get(CAMUNDA + 'process-instance')
19     if (key is None):
20         return res.json()
21     else:
22         return [instance for instance in res.json() if instance['definitionId'].startswith(key + ":")]
23
24 def cleanup_old_deployments(key='sentiment-analysis'):
25     print ("Cleaning up old deployments")
26     for deployment in get_current_deployments(key):
27         res = requests.delete(CAMUNDA + "deployment/" + deployment['id'] + "?cascade=true&skipCustomListeners=true")
28         if (res.status_code == 204):
29             print ("Cleaned up deployment {}".format(deployment['id']))
30         else:
31             print ("Error cleaning old deployment {}: Code: {}".format(deployment['id'], res.status_code))
32             try:
33                 pprint(res.json())
34             except:
35                 pprint(res.content)
36
37 def get_users():
38     res = requests.get(CAMUNDA + "user")
39     return res.json()
40
41 def cleanup_users(keep = ["demo"]):
42     for user in get_users():
43         # keep user "demo"
44         if user['id'] in keep:
45             continue
46
47         res = requests.delete(CAMUNDA + "user/" + user['id'])
48         if (res.status_code == 204):
49             print ("Cleaned up user {}".format(user['id']))
50         else:
51             print ("Error cleaning user {}: Code: {}".format(user['id'], res.status_code))
52             try:
53                 pprint(res.json())
54             except:
55                 pprint(res.content)
56
57 def get_filters():
58     res = requests.get(CAMUNDA + "filter")
59     return res.json()
60
61 def cleanup_filters(keep = ["All Tasks"]):
62     for filter in get_filters():
63         # keep filter "All Tasks"
64         if filter['name'] in keep:
65             continue
66
67         res = requests.delete(CAMUNDA + "filter/" + filter['id'])
68         if (res.status_code == 204):
69             print ("Cleaned up filter {}".format(filter['name']))
70         else:
71             print ("Error cleaning filter {} ({}): Code: {}".format(filter['name'], filter['id'], res.status_code))
72             try:
73                 pprint(res.json())
74             except:
75                 pprint(res.content)
76
77 def create_deployment(cleanup=False):
78     parameters = [
79             ("deployment-name", "sentiment-analysis"),
80             #("enable-duplicate-filtering", "true"),
81             #("deploy-changed-only", "true"),
82             ("file1", open("sentiment-analysis.bpmn")),
83             ("file2", open("forms/input-terms.html")),
84             ("file3", open("forms/download-pdf.html")),
85             ]
86     if cleanup:
87         cleanup_old_deployments()
88     res = requests.post(CAMUNDA + "deployment/create", files=parameters)
89     if (res.status_code == 200):
90         print ("Successfully deployed Sentiment Analysis")
91     else:
92         pprint ("Status Code: {}".format(res.status_code))
93         try:
94             pprint(res.json())
95         except:
96             pprint(res.content)
97
98 def submit_terms(terms):
99     termlist = [{'term': term} for term in terms]
100     # submit to camunda
101     params = {
102             'variables': {
103                 'terms': {
104                     'value': json.dumps(termlist),
105                     'type': "json",
106                     'valueInfo': {
107                         'serializationDataFormat': "application/json",
108                         }
109                     }
110                 }
111             }
112     res = requests.post(CAMUNDA + "process-definition/key/sentiment-analysis/start", json=params)
113     if (res.status_code == 200):
114         print ("Successfully started Sentiment Analysis")
115     else:
116         pprint ("Status Code: {}".format(res.status_code))
117         try:
118             pprint(res.json())
119         except:
120             try:
121                 import xml.dom.minidom
122                 content = res.content.decode('utf-8').replace('<!doctype html>', '')
123                 print(xml.dom.minidom.parseString(content).toprettyxml())
124             except:
125                 pprint(res.content)
126
127 def download_pdf():
128     instances = get_current_process_instances()
129     if len(instances) == 0:
130         print ("Error: no running instance found.")
131         return
132     instance = instances[0]['id']
133     res = requests.get(CAMUNDA + 'process-instance/' + instance + '/variables')
134     try:
135         pprint(res.json())
136     except:
137         pprint(res.content)
138
139 if __name__ == "__main__":
140     parser = argparse.ArgumentParser()
141     parser.add_argument('--no-deploy', dest='deploy', default=True, action='store_false', help="Do not run the deployment step")
142     parser.add_argument('--no-cleanup', dest='cleanup', default=True, action='store_false', help="Initial deploy does not need cleanup")
143     parser.add_argument('--autoclick', dest='autoclick', type=int, default=0, help="How many steps to autoclick")
144     parser.add_argument('--terms', dest='terms', type=str, nargs='*', default=["aic", "TU Wien"], help="How many steps to autoclick")
145     args = parser.parse_args()
146
147     if args.deploy:
148         # initialize camunda process
149         create_deployment(cleanup=args.cleanup)
150         if args.cleanup:
151             cleanup_users()
152             cleanup_filters()
153
154     if args.autoclick >= 1:
155         # start clicking
156         submit_terms(args.terms)
157
158     if args.autoclick >= 2:
159         download_pdf()