-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
298 lines (261 loc) · 13 KB
/
index.js
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
// Define global variables to appease standardjs.com linter
/* global tableau, $, fetch, Option */
(function () {
// Initialize dependencies
const Airtable = require('airtable')
const tableauConnector = tableau.makeConnector()
// Define variables
const airtableMetadataApiBaseUrl = 'https://api.airtable.com/v0/meta'
const airtableFieldTypesToExclude = ['button', 'multipleAttachments']
const dataTypesToConsiderMetric = [tableau.dataTypeEnum.int, tableau.dataTypeEnum.float]
// Helper function to replace characters Tableau does not supprot in IDs (credit https://github.com/tagyoureit/InfluxDB_WDC/blob/gh-pages/InfluxDB_WDC.js#L28)
function replaceSpecialCharsForTableauID (str) {
const newStr = str.replace(/ /g, '_')
.replace(/"/g, '_doublequote_')
.replace(/,/g, '_comma_')
.replace(/=/g, '_equal_')
.replace(/\//g, '_fslash_')
.replace(/-/g, '_dash_')
.replace(/\./g, '_dot_')
.replace(/[^A-Za-z0-9_]/g, '_')
return newStr
}
// Helper function to take an airtable field type and return the apropriate Tableau data type
function determineTableauColumnType (airtableFieldType) {
// Tableau data types are listed at https://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.datatypeenum
// Airtable field types are listed at https://airtable.com/api/meta
// Look at the Airtable field type and return the appropriate Tableau column type
switch (airtableFieldType) {
case 'checkbox':
return tableau.dataTypeEnum.bool
case 'createdTime':
return tableau.dataTypeEnum.datetime
case 'lastModifiedTime':
return tableau.dataTypeEnum.datetime
case 'date':
return tableau.dataTypeEnum.date
case 'dateTime':
return tableau.dataTypeEnum.datetime
case 'number':
return tableau.dataTypeEnum.float
case 'currency':
return tableau.dataTypeEnum.float
case 'percent':
return tableau.dataTypeEnum.float
case 'count':
return tableau.dataTypeEnum.int
case 'rating':
return tableau.dataTypeEnum.int
case 'duration':
return tableau.dataTypeEnum.int
default: // default to a string
return tableau.dataTypeEnum.string
}
}
// Helper function to determine what value to return for a given raw value and field metadata object combination
function airtableFieldValueToTableauColumnValue (airtableRawValue, airtableFieldMeta) {
if (airtableRawValue === undefined) return airtableRawValue
switch (airtableFieldMeta.type) {
case 'singleCollaborator':
return airtableRawValue.email
case 'multipleCollaborators':
return airtableRawValue.map(e => e.email).join(',')
case 'createdBy':
return airtableRawValue.email
case 'checkbox':
return airtableRawValue === true
case 'lastModifiedBy':
return airtableRawValue.email
default: // default stringifying value (Tableau seems to be OK with this for numeric fields)
return airtableRawValue.toString()
}
}
// Helper function to generate fetch request options for an Airtable Metadata API call
function airtableMetadataApiRequestOptions (airtableApiToken) {
return {
method: 'GET',
headers: { Authorization: `Bearer ${airtableApiToken}` }
}
}
// Helper function to get a list of avaiable bases from the Airtable Metadata API
// KNOWN LIMITATION: Only the first 1,000 bases are returned; UI allows user to enter a base ID as a fallback
async function airtableGetListOfBases (airtableApiToken) {
const baseListRequest = await fetch(`${airtableMetadataApiBaseUrl}/bases`, airtableMetadataApiRequestOptions(airtableApiToken))
const baseList = await baseListRequest.json()
return baseList
}
// Helper function to get a base's metadata from the Airtable Metadata API
async function airtableGetBaseMetadata (airtableApiToken, airtableBaseId) {
const baseMetadataRequest = await fetch(`${airtableMetadataApiBaseUrl}/bases/${airtableBaseId}/tables`, airtableMetadataApiRequestOptions(tableau.password))
const baseMetadata = await baseMetadataRequest.json()
if (baseMetadata.error) throw new Error(`Status code ${baseMetadataRequest.status} received while calling Metadata API. Does your account have access to this base?\n\n${JSON.stringify(baseMetadata)}`)
return baseMetadata
}
// Function called when Tableau is ready to pull the schema
tableauConnector.getSchema = async function (schemaCallback) {
try {
// Load connection data from Tableau connector object built upon form submission
const connectionData = JSON.parse(tableau.connectionData)
const { BASE_ID, FIELD_NAME_FOR_AIRTABLE_RECORD_ID } = connectionData
// Setup structure to store field metadata above and beyond what Tableau column schema allows
const TABLE_FIELD_METADATA = {} // this will be saved back to connectionData at the end of getSchema and used by getData
// Call Airtable Metadata API
const baseMetadata = await airtableGetBaseMetadata(tableau.password, BASE_ID)
// For each table, create a schema object
const tableSchemas = baseMetadata.tables.map((tableMeta) => {
TABLE_FIELD_METADATA[tableMeta.name] = {}
// For each table field
const fieldsForTableau = tableMeta.fields.map((fieldMeta) => {
// Check to see if the field type is in our exclude list
if (!airtableFieldTypesToExclude.includes(fieldMeta.type)) {
// Store Airtable field metadata for use later in getData
TABLE_FIELD_METADATA[tableMeta.name][fieldMeta.name] = fieldMeta
const dataType = determineTableauColumnType(fieldMeta.type)
return {
id: replaceSpecialCharsForTableauID(fieldMeta.name),
alias: fieldMeta.name,
description: fieldMeta.name,
// set Tableau column role based off of dataType
columnRole: (dataTypesToConsiderMetric.includes(dataType) ? tableau.columnRoleEnum.measure : tableau.columnRoleEnum.dimension),
dataType
}
} else { // We'll filter these out later
return false
}
})
// Add airtable record ID
fieldsForTableau.push({
id: replaceSpecialCharsForTableauID(FIELD_NAME_FOR_AIRTABLE_RECORD_ID),
dataType: tableau.dataTypeEnum.string, // determineTableauColumnType(FIELD_NAME_FOR_AIRTABLE_RECORD_ID),
description: `Airtable Record ID from table ${tableMeta.name}`
})
return {
id: replaceSpecialCharsForTableauID(tableMeta.name),
alias: tableMeta.name,
description: `Airtable '${tableMeta.name}' (${tableMeta.id}) from base ${BASE_ID}.`,
columns: fieldsForTableau.filter(Boolean)
}
})
// Save updated connectionData - we need this to look up additional metadata
connectionData.TABLE_FIELD_METADATA = TABLE_FIELD_METADATA
tableau.connectionData = JSON.stringify(connectionData)
// Tell Tableau we're done and provide the array of schemas
schemaCallback(tableSchemas)
} catch (err) {
console.error(err)
tableau.abortWithError(`Error during getSchema: ${err.message}`)
}
}
// Function called when Tableau is ready to pull the data
tableauConnector.getData = async function (table, doneCallback) {
try {
console.debug('Getting data for', { table })
// Read configuration variables and initialize Airtable client
const { BASE_ID, FIELD_NAME_FOR_AIRTABLE_RECORD_ID, TABLE_FIELD_METADATA } = JSON.parse(tableau.connectionData)
const airtableFieldMetaForTable = TABLE_FIELD_METADATA[table.tableInfo.alias]
const base = new Airtable({ apiKey: tableau.password }).base(BASE_ID)
// Create an empty array of rows we will populate and eventually provide to Tableau
const rows = []
// Get all records from Airtable using the REST API
const allRecords = await base(table.tableInfo.alias).select({}).all()
// console.debug({ allRecords })
// Loop through each record received and construct the key-value pair in an object
for (const record of allRecords) {
const rowForTableau = {}
// Go through every column present in the Tableau schema and look up the value from Airtable based off of the Tableau column's "description" which is the Airtable field name
for (const col of table.tableInfo.columns) {
let value
// Check the column ID and do something special for the Airtable Record ID column
if (col.id === replaceSpecialCharsForTableauID(FIELD_NAME_FOR_AIRTABLE_RECORD_ID)) {
value = record.getId()
} else {
// Otherwise, try to get the value as a string
try {
// using description though `alias` would be better but for some reason Tableau doesnt always return it to us (TODO)
const airtableFieldMeta = airtableFieldMetaForTable[col.description]
const airtableRawValue = record.get(col.description)
value = airtableFieldValueToTableauColumnValue(airtableRawValue, airtableFieldMeta)
} catch (e) {
console.error(e)
}
}
rowForTableau[col.id] = value
}
// Add this record (tableau row) to the array of rows
rows.push(rowForTableau)
}
// Append all the rows to the Tableau table
table.appendRows(rows)
// For debugging purposes, log the table metadata and rows we just added to it
console.debug('Finished getData for', { table, rows })
// Let Tableau know we're done getting data for the table requested
doneCallback()
} catch (err) {
console.error(err)
tableau.abortWithError(`Error during getData: ${err.message}`)
}
}
// Register the constructed connector (with its handlers) with Tableau
tableau.registerConnector(tableauConnector)
// Create event listeners for when the user submits the HTML form
$(document).ready(function () {
const airtableApiTokenField = $('#airtableApiToken')
const airtableSwitchBaseInput = $('#airtableSwitchBaseInput')
const airtableBaseIdFieldId = '#airtableBaseId'
// After waiting half a second, attempt to parse the Tableau version to determine if the user is opening from within a supported version of Tableau
// If not, display some instructions
setTimeout(function () {
try {
const version = +tableau.platformVersion.split('.').slice(0, 2).join('.')
if (version < 2019.4) throw new Error('Tableau version must be > 2019.4')
} catch (err) {
console.error(err)
$('div.formFieldAndSubmitContainer').hide()
$('.formHeader').append("<hr /><br /><p class='warning formDescription'>Use this Web Data Connector from Tableau version 2019.4 or higher. <a href='https://tableau.github.io/webdataconnector/docs/wdc_use_in_tableau.html'>More info.</a></p>")
}
}, 500)
airtableSwitchBaseInput.on('click', function (e) {
$('#airtableBaseIdContainer').html('<input type="text" pattern="app[A-Za-z0-9]{5,}" data-parsley-errors-container="#errorsFor_airtableBaseId" data-parsley-pattern-message="Your base ID should start with the letters \'app\'" class="col-12 line-height-4 rounded border-thick border-darken2 border-darken3-hover detailCursor-border-blue border-blue-focus detailCursor-stroked-blue-focus" value="" id="airtableBaseId" required="" style="padding: 6px" />')
airtableSwitchBaseInput.hide()
$('#airtableBaseIdPointer').show()
})
// On API token validation...
airtableApiTokenField.parsley().on('field:success', async function (e) {
// Get a list of bases
const airtableApiToken = e.value
const baseList = await airtableGetListOfBases(airtableApiToken)
const sortedBaseList = baseList.bases.sort((a, b) => (a.name > b.name) ? 1 : -1)
// Add them to the existing <select> drop down
for (const base of sortedBaseList) {
const o = new Option(base.name, base.id)
$(o).html(base.name)
$(airtableBaseIdFieldId).append(o)
}
})
// Form validation powered by parsleyjs.org
$('#airtableWdcForm').parsley({
errors: {
container: function (elem) {
return $(elem).parent().parent().parent()
}
}
})
.on('field:validated', function () {
const ok = $('.parsley-error').length === 0
$('.bs-callout-info').toggleClass('hidden', !ok)
$('.bs-callout-warning').toggleClass('hidden', ok)
})
.on('form:submit', function () {
// Store form values in Tableau connection data
const connectionData = {
BASE_ID: $(airtableBaseIdFieldId).val(),
FIELD_NAME_FOR_AIRTABLE_RECORD_ID: '_airtableRecordId'
}
tableau.connectionData = JSON.stringify(connectionData)
tableau.password = airtableApiTokenField.val().trim()
tableau.connectionName = `Airtable Base ${connectionData.BASE_ID}`
// Send the connector object to Tableau
tableau.submit()
})
})
})()