<commit_before>import ElasticsearchAdapter from '../ElasticsearchAdapter'
import ElasticsearchVersionService from './ElasticsearchVersionService'

export default class ConnectionService {
  constructor (host) {
    this.host = host
    this.client = null
  }

  async testConnection () {
    try {
      let client = await this.buildClient()
      let adapter = new ElasticsearchAdapter(client)
      await adapter.ping()
      await adapter.search({ size: 0 })
      return Promise.resolve(adapter)
    } catch (e) {
      return Promise.reject(e)
    }
  }

  async buildClient () {
    let apiVersion = await new ElasticsearchVersionService(this.host).getApiVersion().catch(e => {
      throw new TypeError(e)
    })

    return import(/* webpackChunkName: "elasticsearch-js" */ 'elasticsearch').then(Elasticsearch => {
      return new Elasticsearch.Client({
        host: this.host,
        apiVersion: apiVersion,
        log: false // process.env.NODE_ENV === 'production' ? false : 'trace'
      })
    })
  }

  async getAdapter () {
    return this.testConnection()
  }
}<commit_msg>do not test adapter on each time we access it<commit_after>import ElasticsearchAdapter from '../ElasticsearchAdapter'
import ElasticsearchVersionService from './ElasticsearchVersionService'

export default class ConnectionService {
  constructor (host) {
    this.host = host
  }

  async testConnection () {
    try {
      let client = await this.buildClient()
      let adapter = new ElasticsearchAdapter(client)
      await adapter.ping()
      await adapter.search({ size: 0 })
      return Promise.resolve(adapter)
    } catch (e) {
      return Promise.reject(e)
    }
  }

  async buildClient () {
    let apiVersion = await new ElasticsearchVersionService(this.host).getApiVersion().catch(e => {
      throw new TypeError(e)
    })

    return import(/* webpackChunkName: "elasticsearch-js" */ 'elasticsearch').then(Elasticsearch => {
      return new Elasticsearch.Client({
        host: this.host,
        apiVersion: apiVersion,
        log: false // process.env.NODE_ENV === 'production' ? false : 'trace'
      })
    })
  }

  async getAdapter () {
    let client = await this.buildClient()
    return new ElasticsearchAdapter(client)
  }
}
{'fraction_non_alphanumeric': 0.11076512455516015, 'fraction_numerical': 0.0008896797153024911, 'mean_word_length': 3.0449640287769784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7479671', 'n_tokens_mistral': 616, 'n_tokens_neox': 629, 'n_words': 158}