<commit_before>// @flow
import { observable, action, computed, runInAction } from 'mobx';
import Log from 'electron-log';
import Store from './lib/Store';
import Request from './lib/Request';
import { ROUTES } from '../Routes';

// To avoid slow reconnecting on store reset, we cache the most important props
let cachedDifficulties = null;

const STARTUP_STAGES = {
  CONNECTING: 0,
  SYNCING: 1,
  LOADING: 2,
  RUNNING: 3,
};

export default class NetworkStatusStore extends Store {

  _startTime = Date.now();
  _startupStage = STARTUP_STAGES.CONNECTING;

  @observable isConnected = false;
  @observable hasBeenConnected = false;
  @observable localDifficulty = 0;
  @observable networkDifficulty = 0;
  @observable isLoadingWallets = true;
  @observable networkDifficultyRequest = new Request(this.api, 'getSyncProgress');
  @observable _localDifficultyStartedWith = null;

  @action initialize() {
    super.initialize();
    if (cachedDifficulties !== null) Object.assign(this, cachedDifficulties);
  }

  setup() {
    this.registerReactions([
      this._redirectToWalletAfterSync,
      this._redirectToLoadingWhenDisconnected,
    ]);
    this._listenToServerStatusNotifications();
  }

  teardown() {
    super.teardown();
    cachedDifficulties = {
      isConnected: this.isConnected,
      hasBeenConnected: this.hasBeenConnected,
      localDifficulty: this.localDifficulty,
      networkDifficulty: this.networkDifficulty,
      isLoadingWallets: true,
    };
  }

  @computed get isConnecting(): bool {
    // until we start receiving network difficulty messages we are not connected to node and
    // we should be on the blue connecting screen instead of displaying "Loading wallet data"
    return !this.isConnected || this.networkDifficulty <= 1;
  }

  @computed get hasBlockSyncingStarted(): bool {
    // until we start receiving network difficulty messages we are not connected to node and
    // we should be on the blue connecting screen instead of displaying "Loading wallet data"
    return this.networkDifficulty >= 1;
  }

  @computed get relativeSyncPercentage(): number {
    if (this.networkDifficulty > 0 && this._localDifficultyStartedWith !== null) {
      const relativeLocal = this.localDifficulty - this._localDifficultyStartedWith;
      const relativeNetwork = this.networkDifficulty - this._localDifficultyStartedWith;
      // In case node is in sync after first local difficulty messages
      // local and network difficulty will be the same (0)
      Log.debug('Network difficulty: ', this.networkDifficulty);
      Log.debug('Local difficulty: ', this.localDifficulty);
      Log.debug('Relative local difficulty: ', relativeLocal);
      Log.debug('Relative network difficulty: ', relativeNetwork);

      if (relativeLocal >= relativeNetwork) return 100;
      return relativeLocal / relativeNetwork * 100;
    }
    return 0;
  }

  @computed get syncPercentage(): number {
    if (this.networkDifficulty > 0) {
      if (this.localDifficulty >= this.networkDifficulty) return 100;
      return this.localDifficulty / this.networkDifficulty * 100;
    }
    return 0;
  }

  @computed get isSyncing(): bool {
    return !this.isConnecting && this.hasBlockSyncingStarted && !this.isSynced;
  }

  @computed get isSynced(): bool {
    return !this.isConnecting && this.syncPercentage >= 100 && this.hasBlockSyncingStarted;
  }

  @action _setInitialDifficulty = async () => {
    this._localDifficultyStartedWith = null;
    const initialDifficulty = await this.networkDifficultyRequest.execute().promise;
    if (initialDifficulty) {
      runInAction('set initial difficulty', () => {
        this._localDifficultyStartedWith = initialDifficulty.localDifficulty;
        this.localDifficulty = initialDifficulty.localDifficulty;
        this.networkDifficulty = initialDifficulty.networkDifficulty;
        Log.debug('Initial difficulty: ', initialDifficulty);
      });
    }
  };

  _listenToServerStatusNotifications() {
    this.api.notify(action((message) => {
      if (message === 'ConnectionClosed') {
        Log.debug('ServerStatusNotification: ConnectionClosed');
        this.isConnected = false;
        if (this._startupStage === STARTUP_STAGES.CONNECTING) {
          Log.info(
            `========== Connected after ${this._getStartupTimeDelta()} milliseconds ==========`
            );
          this._startupStage = STARTUP_STAGES.SYNCING;
        }
        return;
      }
      switch (message.tag) {
        case 'ConnectionOpened':
          Log.debug('ServerStatusNotification: ConnectionOpened');
          this._setInitialDifficulty();
          this.isConnected = true;<commit_msg>[DAEF-91] Fix logging connection time caused by merge conflict<commit_after>// @flow
import { observable, action, computed, runInAction } from 'mobx';
import Log from 'electron-log';
import Store from './lib/Store';
import Request from './lib/Request';
import { ROUTES } from '../Routes';

// To avoid slow reconnecting on store reset, we cache the most important props
let cachedDifficulties = null;

const STARTUP_STAGES = {
  CONNECTING: 0,
  SYNCING: 1,
  LOADING: 2,
  RUNNING: 3,
};

export default class NetworkStatusStore extends Store {

  _startTime = Date.now();
  _startupStage = STARTUP_STAGES.CONNECTING;

  @observable isConnected = false;
  @observable hasBeenConnected = false;
  @observable localDifficulty = 0;
  @observable networkDifficulty = 0;
  @observable isLoadingWallets = true;
  @observable networkDifficultyRequest = new Request(this.api, 'getSyncProgress');
  @observable _localDifficultyStartedWith = null;

  @action initialize() {
    super.initialize();
    if (cachedDifficulties !== null) Object.assign(this, cachedDifficulties);
  }

  setup() {
    this.registerReactions([
      this._redirectToWalletAfterSync,
      this._redirectToLoadingWhenDisconnected,
    ]);
    this._listenToServerStatusNotifications();
  }

  teardown() {
    super.teardown();
    cachedDifficulties = {
      isConnected: this.isConnected,
      hasBeenConnected: this.hasBeenConnected,
      localDifficulty: this.localDifficulty,
      networkDifficulty: this.networkDifficulty,
      isLoadingWallets: true,
    };
  }

  @computed get isConnecting(): bool {
    // until we start receiving network difficulty messages we are not connected to node and
    // we should be on the blue connecting screen instead of displaying "Loading wallet data"
    return !this.isConnected || this.networkDifficulty <= 1;
  }

  @computed get hasBlockSyncingStarted(): bool {
    // until we start receiving network difficulty messages we are not connected to node and
    // we should be on the blue connecting screen instead of displaying "Loading wallet data"
    return this.networkDifficulty >= 1;
  }

  @computed get relativeSyncPercentage(): number {
    if (this.networkDifficulty > 0 && this._localDifficultyStartedWith !== null) {
      const relativeLocal = this.localDifficulty - this._localDifficultyStartedWith;
      const relativeNetwork = this.networkDifficulty - this._localDifficultyStartedWith;
      // In case node is in sync after first local difficulty messages
      // local and network difficulty will be the same (0)
      Log.debug('Network difficulty: ', this.networkDifficulty);
      Log.debug('Local difficulty: ', this.localDifficulty);
      Log.debug('Relative local difficulty: ', relativeLocal);
      Log.debug('Relative network difficulty: ', relativeNetwork);

      if (relativeLocal >= relativeNetwork) return 100;
      return relativeLocal / relativeNetwork * 100;
    }
    return 0;
  }

  @computed get syncPercentage(): number {
    if (this.networkDifficulty > 0) {
      if (this.localDifficulty >= this.networkDifficulty) return 100;
      return this.localDifficulty / this.networkDifficulty * 100;
    }
    return 0;
  }

  @computed get isSyncing(): bool {
    return !this.isConnecting && this.hasBlockSyncingStarted && !this.isSynced;
  }

  @computed get isSynced(): bool {
    return !this.isConnecting && this.syncPercentage >= 100 && this.hasBlockSyncingStarted;
  }

  @action _setInitialDifficulty = async () => {
    this._localDifficultyStartedWith = null;
    const initialDifficulty = await this.networkDifficultyRequest.execute().promise;
    if (initialDifficulty) {
      runInAction('set initial difficulty', () => {
        this._localDifficultyStartedWith = initialDifficulty.localDifficulty;
        this.localDifficulty = initialDifficulty.localDifficulty;
        this.networkDifficulty = initialDifficulty.networkDifficulty;
        Log.debug('Initial difficulty: ', initialDifficulty);
      });
    }
  };

  _listenToServerStatusNotifications() {
    this.api.notify(action((message) => {
      if (message === 'ConnectionClosed') {
        Log.debug('ServerStatusNotification: ConnectionClosed');
        this.isConnected = false;
        return;
      }
      switch (message.tag) {
        case 'ConnectionOpened':
          Log.debug('ServerStatusNotification: ConnectionOpened');
          this._setInitialDifficulty();
          this.isConnected = true;
          if (this._startupStage === STARTUP_STAGES.CONNECTING) {
            Log.info(
              `========== Connected after ${this._getStartupTimeDelta()} milliseconds ==========`
            );
            this._startupStage = STARTUP_STAGES.SYNCING;
          }
{'fraction_non_alphanumeric': 0.10224970679176884, 'fraction_numerical': 0.006184028147990191, 'mean_word_length': 3.527027027027027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '223586', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2631, 'n_words': 667}