<commit_before>/**
 * Status event
 * @module tracker/events/status
 */
const debug = require('debug')('bhit:tracker');
const moment = require('moment-timezone');
const WError = require('verror').WError;

/**
 * Status event class
 */
class Status {
    /**
     * Create service
     * @param {App} app                                 The application
     * @param {object} config                           Configuration
     * @param {Logger} logger                           Logger service
     * @param {UserRepository} userRepo                 User repository
     * @param {DaemonRepository} daemonRepo             Daemon repository
     * @param {PathRepository} pathRepo                 Path repository
     * @param {ConnectionRepository} connectionRepo     Connection repository
     */
    constructor(app, config, logger, userRepo, daemonRepo, pathRepo, connectionRepo) {
        this._app = app;
        this._config = config;
        this._logger = logger;
        this._userRepo = userRepo;
        this._daemonRepo = daemonRepo;
        this._pathRepo = pathRepo;
        this._connectionRepo = connectionRepo;
    }

    /**
     * Service name is 'modules.tracker.events.status'
     * @type {string}
     */
    static get provides() {
        return 'modules.tracker.events.status';
    }

    /**
     * Dependencies as constructor arguments
     * @type {string[]}
     */
    static get requires() {
        return [ 'app', 'config', 'logger', 'repositories.user', 'repositories.daemon', 'repositories.path', 'repositories.connection' ];
    }

    /**
     * Event handler
     * @param {string} id           ID of the client
     * @param {object} message      The message
     */
    handle(id, message) {
        let client = this.tracker.clients.get(id);
        if (!client)
            return;

        debug(`Got STATUS from ${client.socket.remoteAddress}:${client.socket.remotePort}`);
        return Promise.resolve()
            .then(() => {
                if (!client.daemonId)
                    return [];

                return this._daemonRepo.find(client.daemonId);
            })
            .then(daemons => {
                let daemon = daemons.length && daemons[0];
                if (!daemon)
                    return;

                let parts = message.status.connectionName.split('/');
                let emailPart = parts.shift();
                let pathPart = '/' + parts.join('/');
                return this._userRepo.findByEmail(emailPart)
                    .then(users => {
                        let user = users.length && users[0];
                        if (!user)
                            return;

                        return this._pathRepo.findByUserAndPath(user, pathPart)
                            .then(paths => {
                                let path = paths.length && paths[0];
                                if (!path)
                                    return;

                                return this._connectionRepo.findByPath(path)
                                    .then(connections => {
                                        let connection = connections.length && connections[0];
                                        if (!connection)
                                            return;

                                        return this._daemonRepo.getActingAs(daemon, connection)
                                            .then(actingAs => {
                                                if (!actingAs)
                                                    return;

                                                let status = client.status.get(message.status.connectionName);
                                                if (!status) {
                                                    status = {
                                                        server: actingAs == 'server',
                                                        connected: 0,
                                                    };
                                                    client.status.set(message.status.connectionName, status);
                                                }
                                                status.connected = message.status.connected;
                                                this._logger.info(`${status.connected} connected to ${client.daemonName} in ${message.status.connectionName}`);

                                                let waiting = this.tracker.waiting.get(message.status.connectionName);
                                                if (!waiting) {
                                                    waiting = {
                                                        server: null,<commit_msg>Log daemon role in connection status<commit_after>/**
 * Status event
 * @module tracker/events/status
 */
const debug = require('debug')('bhit:tracker');
const moment = require('moment-timezone');
const WError = require('verror').WError;

/**
 * Status event class
 */
class Status {
    /**
     * Create service
     * @param {App} app                                 The application
     * @param {object} config                           Configuration
     * @param {Logger} logger                           Logger service
     * @param {UserRepository} userRepo                 User repository
     * @param {DaemonRepository} daemonRepo             Daemon repository
     * @param {PathRepository} pathRepo                 Path repository
     * @param {ConnectionRepository} connectionRepo     Connection repository
     */
    constructor(app, config, logger, userRepo, daemonRepo, pathRepo, connectionRepo) {
        this._app = app;
        this._config = config;
        this._logger = logger;
        this._userRepo = userRepo;
        this._daemonRepo = daemonRepo;
        this._pathRepo = pathRepo;
        this._connectionRepo = connectionRepo;
    }

    /**
     * Service name is 'modules.tracker.events.status'
     * @type {string}
     */
    static get provides() {
        return 'modules.tracker.events.status';
    }

    /**
     * Dependencies as constructor arguments
     * @type {string[]}
     */
    static get requires() {
        return [ 'app', 'config', 'logger', 'repositories.user', 'repositories.daemon', 'repositories.path', 'repositories.connection' ];
    }

    /**
     * Event handler
     * @param {string} id           ID of the client
     * @param {object} message      The message
     */
    handle(id, message) {
        let client = this.tracker.clients.get(id);
        if (!client)
            return;

        debug(`Got STATUS from ${client.socket.remoteAddress}:${client.socket.remotePort}`);
        return Promise.resolve()
            .then(() => {
                if (!client.daemonId)
                    return [];

                return this._daemonRepo.find(client.daemonId);
            })
            .then(daemons => {
                let daemon = daemons.length && daemons[0];
                if (!daemon)
                    return;

                let parts = message.status.connectionName.split('/');
                let emailPart = parts.shift();
                let pathPart = '/' + parts.join('/');
                return this._userRepo.findByEmail(emailPart)
                    .then(users => {
                        let user = users.length && users[0];
                        if (!user)
                            return;

                        return this._pathRepo.findByUserAndPath(user, pathPart)
                            .then(paths => {
                                let path = paths.length && paths[0];
                                if (!path)
                                    return;

                                return this._connectionRepo.findByPath(path)
                                    .then(connections => {
                                        let connection = connections.length && connections[0];
                                        if (!connection)
                                            return;

                                        return this._daemonRepo.getActingAs(daemon, connection)
                                            .then(actingAs => {
                                                if (!actingAs)
                                                    return;

                                                let status = client.status.get(message.status.connectionName);
                                                if (!status) {
                                                    status = {
                                                        server: actingAs == 'server',
                                                        connected: 0,
                                                    };
                                                    client.status.set(message.status.connectionName, status);
                                                }
                                                status.connected = message.status.connected;
                                                this._logger.info(`${status.connected} connected to ${client.daemonName} (${actingAs}) in ${message.status.connectionName}`);

                                                let waiting = this.tracker.waiting.get(message.status.connectionName);
                                                if (!waiting) {
                                                    waiting = {
                                                        server: null,
{'fraction_non_alphanumeric': 0.09257906272831647, 'fraction_numerical': 0.001043732387015969, 'mean_word_length': 1.0253646163601775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 96, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3606673', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1951, 'n_words': 449}