<commit_before>import { BrowserWindow } from 'electron';
import fs from 'fs';
import moment, { Moment } from 'moment';
import readline from 'readline';
import path from 'path';
import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress';
import type { GetBlockSyncProgressType } from '../../common/ipc/api';
import { BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL } from '../config';
import { BlockSyncType } from '../../common/types/cardano-node.types';
import { isItFreshLog } from './blockSyncProgressHelpers';

const blockKeyword = 'Replayed block';
const validatingChunkKeyword = 'Validating chunk';
const validatedChunkKeyword = 'Validated chunk';
const ledgerKeyword = 'Pushing ledger state';

const progressKeywords = [
  blockKeyword,
  validatingChunkKeyword,
  validatedChunkKeyword,
  ledgerKeyword,
];

const keywordTypeMap: Record<string, GetBlockSyncProgressType> = {
  [blockKeyword]: BlockSyncType.replayedBlock,
  [validatingChunkKeyword]: BlockSyncType.validatingChunk,
  [validatedChunkKeyword]: BlockSyncType.validatingChunk,
  [ledgerKeyword]: BlockSyncType.pushingLedger,
};

function containProgressKeywords(line: string) {
  return progressKeywords.some((keyword) => line.includes(keyword));
}

function getProgressType(line: string): GetBlockSyncProgressType | null {
  const key = progressKeywords.find((k) => line.includes(k));

  if (!key) {
    return null;
  }

  return keywordTypeMap[key];
}

const applicationStartDate = moment.utc();

export const handleCheckBlockReplayProgress = (
  mainWindow: BrowserWindow,
  logsDirectoryPath: string
) => {
  const checkBlockReplayProgress = async () => {
    const filename = 'node.log';
    const logFilePath = `${logsDirectoryPath}/pub/`;
    const filePath = path.join(logFilePath, filename);
    if (!fs.existsSync(filePath)) return;
    const fileStream = fs.createReadStream(filePath);
    const rl = readline.createInterface({
      input: fileStream,
    });
    const progress = [];

    for await (const line of rl) {
      if (
        containProgressKeywords(line) &&
        isItFreshLog(applicationStartDate, line)
      ) {
        progress.push(line);
      }
    }

    if (!progress.length) return;
    const finalProgress = progress.slice(-1).pop();
    const percentage = finalProgress.match(/Progress:([\s\d.,]+)%/)?.[1];
    const progressType = getProgressType(finalProgress);
    if (!percentage || !progressType) {
      return;
    }
    const finalProgressPercentage = parseFloat(percentage);
    // Send result to renderer process (NetworkStatusStore)
    getBlockSyncProgressChannel.send(
      { progress: finalProgressPercentage, type: progressType },
      mainWindow.webContents
    );

    if (finalProgressPercentage === 100 && progressType === 'pushingLedger') {
      clearInterval(checkBlockReplayProgressInterval);
    }
  };

  const setBlockReplayProgressCheckingInterval = () => {
    return setInterval(async () => {
      checkBlockReplayProgress();<commit_msg>[DDW-812] Use value from enum<commit_after>import { BrowserWindow } from 'electron';
import fs from 'fs';
import moment, { Moment } from 'moment';
import readline from 'readline';
import path from 'path';
import { getBlockSyncProgressChannel } from '../ipc/get-block-sync-progress';
import type { GetBlockSyncProgressType } from '../../common/ipc/api';
import { BLOCK_REPLAY_PROGRESS_CHECK_INTERVAL } from '../config';
import { BlockSyncType } from '../../common/types/cardano-node.types';
import { isItFreshLog } from './blockSyncProgressHelpers';

const blockKeyword = 'Replayed block';
const validatingChunkKeyword = 'Validating chunk';
const validatedChunkKeyword = 'Validated chunk';
const ledgerKeyword = 'Pushing ledger state';

const progressKeywords = [
  blockKeyword,
  validatingChunkKeyword,
  validatedChunkKeyword,
  ledgerKeyword,
];

const keywordTypeMap: Record<string, GetBlockSyncProgressType> = {
  [blockKeyword]: BlockSyncType.replayedBlock,
  [validatingChunkKeyword]: BlockSyncType.validatingChunk,
  [validatedChunkKeyword]: BlockSyncType.validatingChunk,
  [ledgerKeyword]: BlockSyncType.pushingLedger,
};

function containProgressKeywords(line: string) {
  return progressKeywords.some((keyword) => line.includes(keyword));
}

function getProgressType(line: string): GetBlockSyncProgressType | null {
  const key = progressKeywords.find((k) => line.includes(k));

  if (!key) {
    return null;
  }

  return keywordTypeMap[key];
}

const applicationStartDate = moment.utc();

export const handleCheckBlockReplayProgress = (
  mainWindow: BrowserWindow,
  logsDirectoryPath: string
) => {
  const checkBlockReplayProgress = async () => {
    const filename = 'node.log';
    const logFilePath = `${logsDirectoryPath}/pub/`;
    const filePath = path.join(logFilePath, filename);
    if (!fs.existsSync(filePath)) return;
    const fileStream = fs.createReadStream(filePath);
    const rl = readline.createInterface({
      input: fileStream,
    });
    const progress = [];

    for await (const line of rl) {
      if (
        containProgressKeywords(line) &&
        isItFreshLog(applicationStartDate, line)
      ) {
        progress.push(line);
      }
    }

    if (!progress.length) return;
    const finalProgress = progress.slice(-1).pop();
    const percentage = finalProgress.match(/Progress:([\s\d.,]+)%/)?.[1];
    const progressType = getProgressType(finalProgress);
    if (!percentage || !progressType) {
      return;
    }
    const finalProgressPercentage = parseFloat(percentage);
    // Send result to renderer process (NetworkStatusStore)
    getBlockSyncProgressChannel.send(
      { progress: finalProgressPercentage, type: progressType },
      mainWindow.webContents
    );

    if (
      finalProgressPercentage === 100 &&
      progressType === BlockSyncType.pushingLedger
    ) {
      clearInterval(checkBlockReplayProgressInterval);
    }
  };

  const setBlockReplayProgressCheckingInterval = () => {
    return setInterval(async () => {
      checkBlockReplayProgress();
{'fraction_non_alphanumeric': 0.11989372301560944, 'fraction_numerical': 0.002158751245433411, 'mean_word_length': 4.315975286849073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '130354', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1729, 'n_words': 377}