|
| 1 | +import { DataSource } from '@tableau/extensions-api-types'; |
| 2 | + |
| 3 | +// Wrap everything in an anonymous function to avoid polluting the global namespace |
| 4 | +(async () => { |
| 5 | + class DataSources { |
| 6 | + // Avoid globals. |
| 7 | + constructor(private _$: JQueryStatic) { } |
| 8 | + |
| 9 | + /** |
| 10 | + * Refreshes the given dataSource |
| 11 | + * @param dataSource |
| 12 | + */ |
| 13 | + private static async refreshDataSource(dataSource: DataSource) { |
| 14 | + await dataSource.refreshAsync(); |
| 15 | + console.log(dataSource.name + ': Refreshed Successfully'); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Initializes the extension |
| 20 | + */ |
| 21 | + public async initialize() { |
| 22 | + console.log('Waiting for DOM ready'); |
| 23 | + await this._$.ready; |
| 24 | + console.log('Initializing extension API'); |
| 25 | + await tableau.extensions.initializeAsync(); |
| 26 | + |
| 27 | + // Since dataSource info is attached to the worksheet, we will perform |
| 28 | + // one async call per worksheet to get every dataSource used in this |
| 29 | + // dashboard. This demonstrates the use of Promise.all to combine |
| 30 | + // promises together and wait for each of them to resolve. |
| 31 | + const dataSourceFetchPromises: Array<Promise<DataSource[]>> = []; |
| 32 | + |
| 33 | + // To get dataSource info, first get the dashboard. |
| 34 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 35 | + // Then loop through each worksheet and get its dataSources, save promise for later. |
| 36 | + dashboard.worksheets.forEach(worksheet => dataSourceFetchPromises.push(worksheet.getDataSourcesAsync())); |
| 37 | + const fetchResults = await Promise.all(dataSourceFetchPromises); |
| 38 | + |
| 39 | + // Maps dataSource id to dataSource so we can keep track of unique dataSources. |
| 40 | + const dataSourcesCheck = {}; |
| 41 | + const dashboardDataSources: DataSource[] = []; |
| 42 | + |
| 43 | + fetchResults.forEach(dss => { |
| 44 | + dss.forEach(ds => { |
| 45 | + if (!dataSourcesCheck[ds.id]) { |
| 46 | + // We've already seen it, skip it. |
| 47 | + dataSourcesCheck[ds.id] = true; |
| 48 | + dashboardDataSources.push(ds); |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + this.buildDataSourcesTable(dashboardDataSources); |
| 54 | + |
| 55 | + // This just modifies the UI by removing the loading banner and showing the dataSources table. |
| 56 | + this._$('#loading').addClass('hidden'); |
| 57 | + this._$('#dataSourcesTable') |
| 58 | + .removeClass('hidden') |
| 59 | + .addClass('show'); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Displays a modal dialog with more details about the given dataSource. |
| 64 | + * @param dataSource |
| 65 | + */ |
| 66 | + private async showModal(dataSource: DataSource) { |
| 67 | + const modal = this._$('#infoModal'); |
| 68 | + |
| 69 | + this._$('#nameDetail').text(dataSource.name); |
| 70 | + this._$('#idDetail').text(dataSource.id); |
| 71 | + this._$('#typeDetail').text((dataSource.isExtract) ? 'Extract' : 'Live'); |
| 72 | + |
| 73 | + // Loop through every field in the dataSource and concat it to a string. |
| 74 | + let fieldNamesStr = ''; |
| 75 | + dataSource.fields.forEach(function(field) { |
| 76 | + fieldNamesStr += field.name + ', '; |
| 77 | + }); |
| 78 | + // Slice off the last ", " for formatting. |
| 79 | + this._$('#fieldsDetail').text(fieldNamesStr.slice(0, -2)); |
| 80 | + |
| 81 | + // Loop through each connection summary and list the connection's |
| 82 | + // name and type in the info field |
| 83 | + const connectionSummaries = await dataSource.getConnectionSummariesAsync(); |
| 84 | + let connectionsStr = ''; |
| 85 | + connectionSummaries.forEach(function(summary) { |
| 86 | + connectionsStr += summary.name + ': ' + summary.type + ', '; |
| 87 | + }); |
| 88 | + // Slice of the last ", " for formatting. |
| 89 | + this._$('#connectionsDetail').text(connectionsStr.slice(0, -2)); |
| 90 | + |
| 91 | + // Loop through each table that was used in creating this datasource |
| 92 | + const activeTables = await dataSource.getActiveTablesAsync(); |
| 93 | + let tableStr = ''; |
| 94 | + activeTables.forEach(function(table) { |
| 95 | + tableStr += table.name + ', '; |
| 96 | + }); |
| 97 | + // Slice of the last ", " for formatting. |
| 98 | + this._$('#activeTablesDetail').text(tableStr.slice(0, -2)); |
| 99 | + |
| 100 | + // @ts-ignore |
| 101 | + modal.modal('show'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Constructs UI that displays all the dataSources in this dashboard |
| 106 | + * given a mapping from dataSourceId to dataSource objects. |
| 107 | + * @param dataSources |
| 108 | + */ |
| 109 | + private buildDataSourcesTable(dataSources: DataSource[]) { |
| 110 | + // Clear the table first. |
| 111 | + this._$('#dataSourcesTable > tbody tr').remove(); |
| 112 | + const dataSourcesTable = this._$('#dataSourcesTable > tbody')[0]; |
| 113 | + |
| 114 | + // Add an entry to the dataSources table for each dataSource. |
| 115 | + for (const dataSource of dataSources) { |
| 116 | + // @ts-ignore |
| 117 | + const newRow = dataSourcesTable.insertRow(dataSourcesTable.rows.length); |
| 118 | + const nameCell = newRow.insertCell(0); |
| 119 | + const refreshCell = newRow.insertCell(1); |
| 120 | + const infoCell = newRow.insertCell(2); |
| 121 | + |
| 122 | + const refreshButton = document.createElement('button'); |
| 123 | + refreshButton.innerHTML = 'Refresh Now'; |
| 124 | + refreshButton.type = 'button'; |
| 125 | + refreshButton.className = 'btn btn-primary'; |
| 126 | + refreshButton.addEventListener('click', () => DataSources.refreshDataSource(dataSource)); |
| 127 | + |
| 128 | + const infoSpan = document.createElement('span'); |
| 129 | + infoSpan.className = 'glyphicon glyphicon-info-sign'; |
| 130 | + infoSpan.addEventListener('click', () => this.showModal(dataSource)); |
| 131 | + |
| 132 | + nameCell.innerHTML = dataSource.name; |
| 133 | + refreshCell.appendChild(refreshButton); |
| 134 | + infoCell.appendChild(infoSpan); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + console.log('Initializing DataSources extension.'); |
| 140 | + await new DataSources($).initialize(); |
| 141 | +})(); |
0 commit comments