<commit_before>
import { mergeMap, map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { MainService } from '../main/main.service';
import { DrupalConstants } from '../application/drupal-constants';
import { SystemConnection } from '../models/system';


/**
 * system service for drupal
 * @see BACK_END/admin/structure/services/list/END_POINT/resources for more details
 */
@Injectable()
export class SystemService extends MainService {
  entityType = 'system';

  /**
   * if there is already a token in the browser cookies and it is not expired this will fetch a new token before trying to connect
   * @param refreshToken boolean to force the application to request new token
   * @return observable of the connect method
   * the subscription data is an object of SystemConnection interface
   */
  connect(refreshToken: boolean = false): Observable<SystemConnection> {

    if (this.isConnectionExpired()) {
      this.removeSession();
    }

    if (!DrupalConstants.Connection || DrupalConstants.Connection.token || refreshToken) {
      return this.getToken().pipe(mergeMap(token => {
        return this.post({}, 'connect').pipe(map(connection => {
          connection.token = token;
          this.saveConnection(connection);
          return connection;
        }));
      }));
    }

    return this.post({}, 'connect').pipe(map(connection => {
      this.saveConnection(connection);
      return connection;
    }));
  }

  /**
   * implement get_variable resource<commit_msg>updated system connect condition<commit_after>
import { mergeMap, map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { MainService } from '../main/main.service';
import { DrupalConstants } from '../application/drupal-constants';
import { SystemConnection } from '../models/system';


/**
 * system service for drupal
 * @see BACK_END/admin/structure/services/list/END_POINT/resources for more details
 */
@Injectable()
export class SystemService extends MainService {
  entityType = 'system';

  /**
   * if there is already a token in the browser cookies and it is not expired it will return it or this will fetch a new token before trying to connect
   * @param refreshToken boolean to force the application to request new token
   * @return observable of the connect method
   * the subscription data is an object of SystemConnection interface
   */
  connect(refreshToken: boolean = false): Observable<SystemConnection> {

    if (this.isConnectionExpired()) {
      this.removeSession();
    }

    if (!DrupalConstants.Connection || !DrupalConstants.Connection.token || refreshToken) {
      return this.getToken().pipe(mergeMap(token => {
        return this.post({}, 'connect').pipe(map(connection => {
          connection.token = token;
          this.saveConnection(connection);
          return connection;
        }));
      }));
    }

    return this.post({}, 'connect').pipe(map(connection => {
      this.saveConnection(connection);
      return connection;
    }));
  }

  /**
   * implement get_variable resource
{'fraction_non_alphanumeric': 0.11583624246271025, 'fraction_numerical': 0.0, 'mean_word_length': 3.4709219858156026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5011093', 'n_tokens_mistral': 817, 'n_tokens_neox': 811, 'n_words': 255}