<commit_before>﻿using System;
using System.Text;
using BookSleeve;
using Compilify.Models;
using Compilify.Web.EndPoints;
using Newtonsoft.Json;
using SignalR;

namespace Compilify.Web.Services {
    /// <summary>
    /// Processes messages sent by workers over Redis and forwards them to the client
    /// that originally initiated the request.</summary>
    public class JobDoneMessageRelay
    {

        private const string ChannelPattern = "workers:job-done:*";

        public JobDoneMessageRelay(RedisConnectionGateway redisConnectionGateway)
        {
            gateway = redisConnectionGateway;
            Subscribe();
        }

        private readonly RedisConnectionGateway gateway;
        private static RedisSubscriberConnection channel;

        private void Subscribe()
        {
            channel = gateway.GetConnection().GetOpenSubscriberChannel();
            channel.Closed += OnChannelClosed;

            channel.PatternSubscribe(ChannelPattern, OnMessageRecieved);
        }

        public void OnChannelClosed(object sender, EventArgs e)
        {
            if (channel != null) {
                channel.Closed -= OnChannelClosed;
                channel.Dispose();
                channel = null;
            }

            Subscribe();
        }
        
        /// <summary>
        /// Handle messages received from workers through Redis.</summary>
        /// <param name="key">
        /// The name of the channel on which the message was received.</param>
        /// <param name="message">
        /// A JSON message.</param>
        public void OnMessageRecieved(string key, byte[] message)
        {
            // Retrieve the client's connection ID from the key
            var parts = key.Split(new[] { ':' });
            var clientId = parts[parts.Length - 1];

            if (!string.IsNullOrEmpty(clientId))
            {
                var context = GlobalHost.ConnectionManager.GetConnectionContext<ExecuteEndPoint>();
                var response = WorkerResult.Deserialize(message);

                // Forward the message to the user's browser with SignalR
                context.Connection.Send(clientId, new { status = "ok", data = response });
            }
        }
    }
}<commit_msg>Format result string on the server<commit_after>﻿using System;
using BookSleeve;
using Compilify.Models;
using Compilify.Web.EndPoints;
using SignalR;

namespace Compilify.Web.Services
{
    /// <summary>
    /// Processes messages sent by workers over Redis and forwards them to the client
    /// that originally initiated the request.</summary>
    public class JobDoneMessageRelay
    {
        private const string ChannelPattern = "workers:job-done:*";

        public JobDoneMessageRelay(RedisConnectionGateway redisConnectionGateway)
        {
            gateway = redisConnectionGateway;
            Subscribe();
        }

        private readonly RedisConnectionGateway gateway;
        private static RedisSubscriberConnection channel;

        private void Subscribe()
        {
            channel = gateway.GetConnection().GetOpenSubscriberChannel();
            channel.Closed += OnChannelClosed;

            channel.PatternSubscribe(ChannelPattern, OnMessageRecieved);
        }

        public void OnChannelClosed(object sender, EventArgs e)
        {
            if (channel != null)
            {
                channel.Closed -= OnChannelClosed;
                channel.Dispose();
                channel = null;
            }

            Subscribe();
        }
        
        /// <summary>
        /// Handle messages received from workers through Redis.</summary>
        /// <param name="key">
        /// The name of the channel on which the message was received.</param>
        /// <param name="message">
        /// A JSON message.</param>
        public void OnMessageRecieved(string key, byte[] message)
        {
            // Retrieve the client's connection ID from the key
            var parts = key.Split(new[] { ':' });
            var clientId = parts[parts.Length - 1];

            if (!string.IsNullOrEmpty(clientId))
            {
                var context = GlobalHost.ConnectionManager.GetConnectionContext<ExecuteEndPoint>();
                var response = WorkerResult.Deserialize(message);

                // Forward the message to the user's browser with SignalR
                context.Connection.Send(clientId, new { status = "ok", data = response.ToResultString() });
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.08924731182795699, 'fraction_numerical': 0.00043010752688172043, 'mean_word_length': 1.8239222829386763, 'pattern_counts': {'":': 0, '<': 21, '<?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': '4873300', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1077, 'n_words': 303}