<commit_before>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file bootstrap-socket.js
 * @description Bootstrap the socket app
 * @module Model
 * @author <NAME>
 *
 */
const os = require('os');
const cluster = require('cluster');
const net = require('net');
const Express = require('express');
const sio = require('socket.io');
const sioRedis = require('socket.io-redis');
const sioEmitter = require('socket.io-emitter');

const Config = require('node-env-obj')('../');
const Model = require('./model');
const Logging = require('./logging');
const MongoClient = require('mongodb').MongoClient;
// const ObjectId = require('mongodb').ObjectId;
const NRP = require('node-redis-pubsub');

class BootstrapSocket {
	constructor() {
		Logging.setLogLevel(Logging.Constants.LogLevel.INFO);

		this.processes = os.cpus().length;
		this.workers = [];

		this.__apps = [];
		this.__tokens = [];
		this.__namespace = {};

		this.__superApps = [];

		this.isPrimary = Config.sio.app === 'primary';

		this.emitter = null;

		let socketInitTask = null;
		if (cluster.isMaster) {
			socketInitTask = (db) => this.__initMaster(db);
		} else {
			socketInitTask = (db) => this.__initWorker(db);
		}

		return this.__nativeMongoConnect()
			.then(socketInitTask)
			.then(() => cluster.isMaster);
	}

	__initMaster(db) {
		const nrp = new NRP(Config.redis);

		this.emitter = sioEmitter(Config.redis);

		if (this.isPrimary) {
			Logging.logDebug(`Primary Master`);
			nrp.on('activity', (data) => this.__onActivityReceived(data));
		}

		return this.__initMongoConnect()
			.then((db) => {
			// Load Apps
				return new Promise((resolve, reject) => {
					Model.App.findAll().toArray((err, _apps) => {
						if (err) reject(err);
						resolve(this.__apps = _apps);
					});
				});
			})
			.then(() => {
			// Load Tokens
				return new Promise((resolve, reject) => {
					Model.Token.findAll().toArray((err, _tokens) => {
						if (err) reject(err);
						resolve(this.__tokens = _tokens);
					});
				});
			})
			.then(() => {
			// Spawn worker processes, pass through build app objects
				this.__apps.map((app) => {
					const token = this.__tokens.find((t) => {
						return app._token && t._id.equals(app._token);
					});
					if (!token) {
						Logging.logWarn(`No Token found for ${app.name}`);
						return null;
					}

					app.token = token;
					app.publicId = Model.App.genPublicUID(app.name, app._id);

					const isSuper = token.authLevel > 2;
					Logging.logSilly(`Name: ${app.name}, App ID: ${app._id}, Public ID: ${app.publicId}`);

					this.__namespace[app.publicId] = {
						emitter: this.emitter.of(`/${app.publicId}`),
						sequence: 0,
					};
					Logging.logDebug(`[${app.publicId}]: Created Namespace for ${app.name}, ${(isSuper) ? 'SUPER' : ''}`);

					if (isSuper) {<commit_msg>- FIX: Log out app public ID on startup<commit_after>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file bootstrap-socket.js
 * @description Bootstrap the socket app
 * @module Model
 * @author <NAME>
 *
 */
const os = require('os');
const cluster = require('cluster');
const net = require('net');
const Express = require('express');
const sio = require('socket.io');
const sioRedis = require('socket.io-redis');
const sioEmitter = require('socket.io-emitter');

const Config = require('node-env-obj')('../');
const Model = require('./model');
const Logging = require('./logging');
const MongoClient = require('mongodb').MongoClient;
// const ObjectId = require('mongodb').ObjectId;
const NRP = require('node-redis-pubsub');

class BootstrapSocket {
	constructor() {
		Logging.setLogLevel(Logging.Constants.LogLevel.INFO);

		this.processes = os.cpus().length;
		this.workers = [];

		this.__apps = [];
		this.__tokens = [];
		this.__namespace = {};

		this.__superApps = [];

		this.isPrimary = Config.sio.app === 'primary';

		this.emitter = null;

		let socketInitTask = null;
		if (cluster.isMaster) {
			socketInitTask = (db) => this.__initMaster(db);
		} else {
			socketInitTask = (db) => this.__initWorker(db);
		}

		return this.__nativeMongoConnect()
			.then(socketInitTask)
			.then(() => cluster.isMaster);
	}

	__initMaster(db) {
		const nrp = new NRP(Config.redis);

		this.emitter = sioEmitter(Config.redis);

		if (this.isPrimary) {
			Logging.logDebug(`Primary Master`);
			nrp.on('activity', (data) => this.__onActivityReceived(data));
		}

		return this.__initMongoConnect()
			.then((db) => {
			// Load Apps
				return new Promise((resolve, reject) => {
					Model.App.findAll().toArray((err, _apps) => {
						if (err) reject(err);
						resolve(this.__apps = _apps);
					});
				});
			})
			.then(() => {
			// Load Tokens
				return new Promise((resolve, reject) => {
					Model.Token.findAll().toArray((err, _tokens) => {
						if (err) reject(err);
						resolve(this.__tokens = _tokens);
					});
				});
			})
			.then(() => {
			// Spawn worker processes, pass through build app objects
				this.__apps.map((app) => {
					const token = this.__tokens.find((t) => {
						return app._token && t._id.equals(app._token);
					});
					if (!token) {
						Logging.logWarn(`No Token found for ${app.name}`);
						return null;
					}

					app.token = token;
					app.publicId = Model.App.genPublicUID(app.name, app._id);

					const isSuper = token.authLevel > 2;
					Logging.log(`Name: ${app.name}, App ID: ${app._id}, Public ID: ${app.publicId}`);

					this.__namespace[app.publicId] = {
						emitter: this.emitter.of(`/${app.publicId}`),
						sequence: 0,
					};
					Logging.logDebug(`[${app.publicId}]: Created Namespace for ${app.name}, ${(isSuper) ? 'SUPER' : ''}`);

					if (isSuper) {
{'fraction_non_alphanumeric': 0.18041146474415334, 'fraction_numerical': 0.000703358537014243, 'mean_word_length': 3.29607250755287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '5181766', 'n_tokens_mistral': 2246, 'n_tokens_neox': 1862, 'n_words': 388}