-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.html
214 lines (180 loc) · 6.91 KB
/
engine.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
// Include version 1 of the engine base library.
document.write('<link rel="import" href="' + window.Alteryx.LibDir + '1/lib/alteryx/engine/includes.html">');
</script>
<script>
window.JSONConstruct = {
}
const pushOne = () => {
var output = []
var data = []
data.push(JSON.stringify(JSONConstruct.JSONobj))
output.push(data)
Alteryx.Engine.SendMessage.PushRecords(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name, output)
Alteryx.Engine.SendMessage.CloseOutput(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name)
Alteryx.Engine.SendMessage.Complete()
}
const buildRecordObject = (data) => {
var JSONobj = JSONConstruct.JSONobj = JSONConstruct.JSONobj || []
for (var row in data.Records) {
var obj = {}
for (var column in data.Records[row]) {
obj[JSONConstruct.outputFieldNames[column]] = data.Records[row][column]
}
JSONobj.push(obj)
}
pushOne()
}
const buildRecordObjects = (data) => {
for (var row in data.Records) {
var outer = []
var inner = []
var obj = {}
for (var column in data.Records[row]) {
obj[JSONConstruct.outputFieldNames[column]] = data.Records[row][column]
}
for (var column in data.Records[row]) {
inner.push(data.Records[row][column])
}
inner.push(JSON.stringify(obj))
outer.push(inner)
Alteryx.Engine.SendMessage.PushRecords(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name, outer)
}
Alteryx.Engine.SendMessage.CloseOutput(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name)
Alteryx.Engine.SendMessage.Complete()
}
const buildRecordArray = (data) => {
var fieldArray = JSONConstruct.fieldArray = JSONConstruct.fieldArray || []
for (var column in data.Records[0]) {
var dataArray = [];
for (var row in data.Records) {
dataArray.push(data.Records[row][column])
}
fieldArray.push(dataArray)
}
const buildJSON = (names, data) => {
const JSONobj = {}
for (var record in names) {
JSONobj[names[record]] = data[record]
}
return JSONobj
}
var JSONobj = JSONConstruct.JSONobj = buildJSON(JSONConstruct.outputFieldNames, JSONConstruct.fieldArray)
pushOne()
}
/**
* This function defines our input and output connections.
* It must match the input and output connections defined in the GUI plugin's XML file.
*/
Alteryx.Plugin.DefineConnections = function()
{
return {
IncomingConnections: [
{
"type": "Input",
"GroupInfo": {
"count": 0,
"grouping": "false"
}
}
],
OutgoingConnections: [
{
"name": "Output"
}
]
}
}
/**
* Called at the beginning of plugin lifetime with the plugin's configuration properties.
* This implementation contains example code that prints each configuration key/value pair as
* an engine output message.
*
* @params config The plugin's configuration.
*/
Alteryx.Plugin.PI_Init = function(config)
{
var mode = JSONConstruct.mode = config.Configuration.mode
}
/**
* Called once for each incoming connection with the connection's metainfo. When a per-connection init comes in,
* we would probably store off the incoming RecordInfo.
*
* @params metaInfo The meta-information for the current incoming connection.
*/
Alteryx.Plugin.II_Init = function(metaInfo)
{
JSONConstruct.outputInfo = metaInfo.RecordInfo
var JsonField = {
"name":"JSON_Construct",
"size":1073741823,
"source":"JSON Construct Tool",
"type":"V_WString"
}
var onlyJsonField = { "Field": [ JsonField ] }
metaInfo.RecordInfo.Field.push(JsonField)
// Names for JSON object
var outputFieldNames = JSONConstruct.outputFieldNames = []
for (var item in JSONConstruct.outputInfo.Field) {
outputFieldNames.push(JSONConstruct.outputInfo.Field[item].name)
}
JSONConstruct.mode == 'objects' ?
Alteryx.Engine.SendMessage.RecordInfo(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name, metaInfo.RecordInfo) :
Alteryx.Engine.SendMessage.RecordInfo(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name, onlyJsonField)
}
/**
* After II_Init has been called for each incoming connection, II_PushRecords is called for each non-empty
* incoming connection with that connection's records. This implementation contains example code that
* pushes out the same records it receives.
*
* @param data The incoming records.
*/
Alteryx.Plugin.II_PushRecords = function(data)
{
if (data.Records.length > 0) {
JSONConstruct.mode == 'object' ? buildRecordArray(data) : JSONConstruct.mode == 'objects' ? buildRecordObjects(data) : buildRecordObject(data)
}
}
/**
* II_AllClosed is called with no arguments after all incoming connections have closed. This implementation
* sends a CloseOutput message with the name of the outgoing connection to close.
*
* All code paths must terminate with a call to Alteryx.Engine.SendMessage.Complete()
*/
Alteryx.Plugin.II_AllClosed = function()
{
Alteryx.Engine.SendMessage.CloseOutput(Alteryx.Plugin.DefineConnections().OutgoingConnections[0].name)
Alteryx.Engine.SendMessage.Complete()
}
/**
* If the tool has no input:
* PI_PushAllRecords is called instead of the II functions.
* It is also called at configure time with a record limit of 0.
*
* If this function is implemented, all code paths must signal completion with a call to
* Alteryx.Engine.SendMessage.Complete()
*
* @param recordLimit The maximum number of records that this function should return.
*/
Alteryx.Plugin.PI_PushAllRecords = function(recordLimit)
{
Alteryx.Engine.SendMessage.Error('XMSG("This tool requires an input connection.")');
Alteryx.Engine.SendMessage.Complete();
}
/**
* PI_Close is called with no arguments at the end of the plugin's lifetime.
* All code paths must terminate with a call to Alteryx.Engine.SendMessage.PI_Close()
*/
Alteryx.Plugin.PI_Close = function()
{
Alteryx.Engine.SendMessage.PI_Close();
}
</script>
</head>
<body>
</body>
</html>