<commit_before>var fs = require("fs");
var ejs = require("ejs");
var path = require("path");
var buster = require("buster-core");
var captureServer = require("buster-capture-server");
var TEMPLATE_ROOT = path.join(path.dirname(__filename), "../../../templates");
var userAgentParser = require("buster-user-agent-parser");
var resourceSet = require("buster-resources").resourceSet;

function template(name, locals, callback) {
    var templatePath = path.join(TEMPLATE_ROOT, name + ".ejs");

    fs.readFile(templatePath, "utf-8", function (err, data) {
        if (err) throw err;
        callback(ejs.render(data, { locals: locals }));
    });
}

function addHeader(masterOfPuppets) {
    template("header", {}, function (string) {
        var rs = resourceSet.create();
        rs.addResource({ path: "/", content: string });
        masterOfPuppets.header(80, rs);
    });
}

module.exports = {
    create: function (httpServer) {
        var server = Object.create(this);
        server.slaves = [];
        server.captureServer = captureServer.create();
        addHeader(server.captureServer);
        if (httpServer) server.captureServer.attach(httpServer);

        server.captureServer.oncapture = function (req, res, slave) {
            var ua = userAgentParser.parse(req.headers["user-agent"]);
            ua.userAgent = req.headers["user-agent"];
            server.slaves.push(ua);
            res.writeHead(302, { "Location": slave.url });
            res.end();
        };

        server.captureServer.capturePath = "/capture";
        return server;
    },

    respond: function (req, res) {
        if (this.serveTemplate(req, res)) return true;
        return false;
    },

    serveTemplate: function (req, res) {
        if (req.url != "/") return;
        res.writeHead(200, { "Content-Type": "text/html" });

        template("index", { slaves: this.slaves }, function (string) {
            res.end(string);<commit_msg>Render fresh slave list on index<commit_after>var fs = require("fs");
var ejs = require("ejs");
var path = require("path");
var buster = require("buster-core");
var captureServer = require("buster-capture-server");
var TEMPLATE_ROOT = path.join(path.dirname(__filename), "../../../templates");
var userAgentParser = require("buster-user-agent-parser");
var resourceSet = require("buster-resources").resourceSet;

function template(name, locals, callback) {
    var templatePath = path.join(TEMPLATE_ROOT, name + ".ejs");

    fs.readFile(templatePath, "utf-8", function (err, data) {
        if (err) throw err;
        callback(ejs.render(data, { locals: locals }));
    });
}

function addHeader(masterOfPuppets) {
    template("header", {}, function (string) {
        var rs = resourceSet.create();
        rs.addResource({ path: "/", content: string });
        masterOfPuppets.header(80, rs);
    });
}

module.exports = {
    create: function (httpServer) {
        var server = Object.create(this);
        server.captureServer = captureServer.create();
        addHeader(server.captureServer);
        if (httpServer) server.captureServer.attach(httpServer);

        server.captureServer.oncapture = function (req, res, slave) {
            res.writeHead(302, { "Location": slave.url });
            res.end();
        };

        server.captureServer.capturePath = "/capture";
        return server;
    },

    respond: function (req, res) {
        if (this.serveTemplate(req, res)) return true;
        return false;
    },

    serveTemplate: function (req, res) {
        if (req.url != "/") return;
        res.writeHead(200, { "Content-Type": "text/html" });

        var slaves = this.captureServer.slaves().map(function (slave) {
            var ua = userAgentParser.parse(slave.userAgent);
            ua.userAgent = slave.userAgent;
            return ua;
        });

        template("index", { slaves: slaves }, function (string) {
            res.end(string);
{'fraction_non_alphanumeric': 0.15488301119023398, 'fraction_numerical': 0.004577822990844354, 'mean_word_length': 2.852105778648384, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '963079', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1135, 'n_words': 248}