Knowledgebase: Sisense Tips
Limit Drill Dimensions by Dashboard
Posted by Daniel Ranisavljevic, Last modified by Daniel Ranisavljevic on 30 August 2022 07:20 pm

The dashboard script below will limit the drill item selection on all widgets within a dashboard based on what is set on the 'dimList'.

Add additional dim 'titles' to the list separated by comma in order to add items to drill to.

Alternatively, set defaultInclude to false to exclude the items from the list instead.

const dimList = ['Country']
const defaultInclude = true

const drillInclude = (dimList, defaultInclude) => {
  const fromBrackets = (str) => {
  // validate.
    if (!angular.isString(str) || str.length < 2 || str[0] !== '[' || str[str.length - 1] !== ']') {
      return str
    }

    return str.substring(1, str.length - 1)
  }

  const stuffRepo = (data, datasource) => {
    const service = prism.$injector.get('eucalyptus.services.metaService')
    datasource = datasource.toLowerCase()
    data.forEach((item) => {
      const id = fromBrackets(item.id).toLowerCase()

      if (!Object.hasOwn(service.repository, datasource)) { service.repository[datasource] = {} }

      if (service.repository[datasource][id] === undefined) {
        service.repository[datasource][id] = {
          type: item.dimtype,
          isMerged: item.merged,
          isIndexed: item.indexed
        }
      }
    })
  }

  prism.$injector.get('eucalyptus.services.metaService').getFields = (ds, q, offset, count, ignoretypes) => {
    if (window.currentWidget && prism.filteredWidgets[window.currentWidget.oid]) {
      let firstTime = false
      if (count === 100) {
        firstTime = true
      }
      // $('.md-modal') - prevent from filtering fields when creating new widget
      if (count === 50 && $('.md-modal').length === 0) {
        count = maxCount
      }
    }
    if (typeof ds !== 'string' && typeof ds !== 'object') { throw 'Datasource parameter must be a string or object!' }

    if (defined(q) && typeof q !== 'string') { throw 'Query must be a string!' }

    if (defined(offset) && defined(count) && (isNaN(offset) || isNaN(count))) { throw 'Offset/Count must be integers!' }

    const dsTitle = typeof ds === 'string' ? ds : `${ds.address}/${ds.title}`
    const url = `../api/datasources/${dsTitle}/fields/search`
    const payload = {}

    if (defined(offset) && defined(count)) {
      payload.count = count
      payload.offset = offset
    }

    if (defined(q)) {
      payload.term = q
    }

    // if (defined(ignoretypes)) {
    //  const types = angular.isArray(ignoretypes) ? ignoretypes.join('|') : ignoretypes
    //  payload.types = `ignoretypes=${encodeURIComponent(types)}`
    // }

    const promise = prism.$injector.get('$http')({
      method: 'POST',
      url: url,
      data: payload
    })

    promise.then((e) => {
      e.data = (defaultInclude) ? e.data.filter((item) => { return dimList.includes(item.title) }) : e.data.filter((item) => { return !dimList.includes(item.title) })
      if (window.currentWidget && prism.filteredWidgets[window.currentWidget.oid]) {
        if (offset === 50) {
          e.data.splice(0, e.data.length)
        }
        if (!firstTime && $('.md-modal').length === 0) {
          if (prism.filteredWidgets[window.currentWidget.oid].fieldsToRemove.length > 0) {
            const i = e.data.length - 1
            while (i >= 0) {
              if (prism.filteredWidgets[window.currentWidget.oid].fieldsToRemove.indexOf(e.data[i].id) > -1) {
                e.data.splice(i, 1)
              }
              i--
            }
          } else { // fieldsToKeep.length > 0
            const i = e.data.length - 1
            while (i >= 0) {
              if (prism.filteredWidgets[window.currentWidget.oid].fieldsToKeep.indexOf(e.data[i].id) < 0) {
                e.data.splice(i, 1)
              }
              i--
            }
          }
        }
      }

      const datasource = ds.title || ds
      stuffRepo(e.data, datasource)
    })
    return promise
  }
}

drillInclude(dimList, defaultInclude)
(0 vote(s))
Helpful
Not helpful

Comments (0)
Copyright © RAPID BI Pty Ltd 2020