<commit_before>import Backbone from 'backbone';
import debounce from 'debounce';

import { search, searchAllRecords } from '../';

import OpenSearchCollection from './OpenSearchCollection';
import EOWCSCollection from './EOWCSCollection';

/**
 *
 *
 * @memberof search/models
 */

class SearchModel extends Backbone.Model {
  defaults() {
    return {
      defaultPageSize: 9,
      maxCount: 200,
      totalResults: undefined,
      isSearching: false,
      hasError: false,

      downloadSelection: new Backbone.Collection,
      searchState: 0,

      hasLoaded: 0,
      debounceTime: 250,
    };
  }

  initialize(attributes, options = {}) {
    const layerModel = this.get('layerModel');
    switch (layerModel.get('search.protocol')) {
      case 'EO-WCS':
        this.set('results', new EOWCSCollection);
        break;
      case 'OpenSearch':
        this.set('results', new OpenSearchCollection);
        break;
      default:
        throw new Error(`Unsupported search protocol '${layerModel.get('search.protocol')}'.`);
    }

    this.listenTo(this, 'change:debounceTime', this.onDebounceTimeChange);

    this.listenTo(this.get('results'), 'reset', this.onSearchCollectionReset);
    this.listenTo(this.get('filtersModel'), 'change', this.onFiltersModelChange);
    this.listenTo(this.get('mapModel'), 'change:bbox', this.onMapBBOXChange);
    this.listenTo(this.get('mapModel'), 'change:time', this.onMapTimeChange);

    this.onDebounceTimeChange();

    this.automaticSearch = options.automaticSearch;
    if (this.automaticSearch) {
      this.search();
    }
  }

  search(reset) {
    const layerModel = this.get('layerModel');
    const filtersModel = this.get('filtersModel');
    const mapModel = this.get('mapModel');

    this.doSearchDebounced(layerModel, filtersModel, mapModel);
  }

  doSearch(layerModel, filtersModel, mapModel) {
    console.time("XXX")
    const request = searchAllRecords(layerModel, filtersModel, mapModel, {
      itemsPerPage: this.get('defaultPageSize'),
      maxCount: this.get('maxCount'),
    });

    const searchState = this.get('searchState') + 1;

    this.set({
      isSearching: true,
      hasError: false,
      searchState,
      hasLoaded: 0,
    });

    console.log("Clearing")
    this.get('results').reset([]);

    return request.then((result) => {
      console.timeEnd("XXX")
      if (searchState !== this.get('searchState')) {
        // abort when the search is not the current one
        return;
      }
      this.set({
        totalResults: result.totalResults,
        startIndex: result.startIndex,
        itemsPerPage: result.itemsPerPage,
        isSearching: false,
        hasLoaded: result.records.length,
      });
      this.get('results').reset(result.records);
    }).catch((error) => {
      if (searchState !== this.get('searchState')) {
        console.log("not setting error:", searchState, this.get('searchState'));
        return
      }
      this.set({
        isSearching: false,
        hasError: true,
      });
      this.get('results').reset([]);
      this.trigger('search:error', error);
    });
  }

  onDebounceTimeChange() {
    this.doSearchDebounced = debounce(
      (...args) => this.doSearch(...args), this.get('debounceTime')
    );
  }

  onSearchCollectionReset() {
    this.trigger('search:complete', this);
  }

  onFiltersModelChange() {
    if (this.automaticSearch) {
      this.search();
    }
  }

  onMapBBOXChange() {
    if (this.automaticSearch && !this.get('filtersModel').get('area')) {
      this.search();
    }
  }

  onMapTimeChange() {
    if (this.automaticSearch && !this.get('filtersModel').get('time')) {
      this.search();
    }
  }
}

export default SearchModel;<commit_msg>Cleanup of unused stuff. Making 'automaticSearch' a normal model field.<commit_after>import Backbone from 'backbone';
import debounce from 'debounce';

import { search, searchAllRecords } from '../';

import OpenSearchCollection from './OpenSearchCollection';
import EOWCSCollection from './EOWCSCollection';

/**
 *
 *
 * @memberof search/models
 */

class SearchModel extends Backbone.Model {
  defaults() {
    return {
      automaticSearch: false,
      defaultPageSize: 9,
      maxCount: 200,
      totalResults: undefined,
      isSearching: false,
      hasError: false,

      downloadSelection: new Backbone.Collection,

      hasLoaded: 0,
      debounceTime: 250,
    };
  }

  initialize(attributes, options = {}) {
    const layerModel = this.get('layerModel');
    switch (layerModel.get('search.protocol')) {
      case 'EO-WCS':
        this.set('results', new EOWCSCollection);
        break;
      case 'OpenSearch':
        this.set('results', new OpenSearchCollection);
        break;
      default:
        throw new Error(`Unsupported search protocol '${layerModel.get('search.protocol')}'.`);
    }

    // back reference from results to collection
    this.get('results').searchModel = this;

    this.listenTo(this, 'change:debounceTime', this.onDebounceTimeChange);
    this.listenTo(this, 'change:automaticSearch', this.onAutomaticSearchChange);

    this.listenTo(layerModel, 'change:display.visible', this.onLayerVisibleChange);
    this.listenTo(this.get('results'), 'reset', this.onSearchCollectionReset);
    this.listenTo(this.get('filtersModel'), 'change', this.onFiltersModelChange);
    this.listenTo(this.get('mapModel'), 'change:bbox', this.onMapBBOXChange);
    this.listenTo(this.get('mapModel'), 'change:time', this.onMapTimeChange);

    this.onDebounceTimeChange();
    this.set('automaticSearch', layerModel.get('display.visible'));
  }

  search() {
    const layerModel = this.get('layerModel');
    const filtersModel = this.get('filtersModel');
    const mapModel = this.get('mapModel');

    this.doSearchDebounced(layerModel, filtersModel, mapModel);
  }

  doSearch(layerModel, filtersModel, mapModel) {
    if (this.prevRequest && this.prevRequest.cancel) {
      this.prevRequest.cancel();
    }
    this.prevRequest = searchAllRecords(layerModel, filtersModel, mapModel, {
      itemsPerPage: this.get('defaultPageSize'),
      maxCount: this.get('maxCount'),
    });

    this.set({
      isSearching: true,
      hasError: false,
      hasLoaded: 0,
    });
    this.get('results').reset([]);

    return this.prevRequest.then((result) => {
      this.set({
        totalResults: result.totalResults,
        startIndex: result.startIndex,
        itemsPerPage: result.itemsPerPage,
        isSearching: false,
        hasLoaded: result.records.length,
      });
      this.get('results').reset(result.records);
    }).catch((error) => {
      this.set({
        isSearching: false,
        hasError: true,
      });
      this.get('results').reset([]);
      this.trigger('search:error', error);
    });
  }

  onDebounceTimeChange() {
    this.doSearchDebounced = debounce(
      (...args) => this.doSearch(...args), this.get('debounceTime')
    );
  }

  onAutomaticSearchChange() {
    if (this.get('automaticSearch')) {
      this.search();
    }
  }

  onLayerVisibleChange() {
    const layerModel = this.get('layerModel');
    this.set('automaticSearch', layerModel.get('display.visible'));
  }

  onSearchCollectionReset() {
    this.trigger('search:complete', this);
  }

  onFiltersModelChange() {
    if (this.get('automaticSearch')) {
      this.search();
    }
  }

  onMapBBOXChange() {
    if (this.get('automaticSearch') && !this.get('filtersModel').get('area')) {
      this.search();
    }
  }

  onMapTimeChange() {
    if (this.get('automaticSearch') && !this.get('filtersModel').get('time')) {
      this.search();
    }
  }
}

export default SearchModel;
{'fraction_non_alphanumeric': 0.14887787056367432, 'fraction_numerical': 0.0026096033402922755, 'mean_word_length': 3.181669394435352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4647543', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2278, 'n_words': 414}