<commit_before>/*jslint node: true*/
"use strict";
var phantom = require('phantom'),
    http = require('http'),
    url = require('url'),
    colors = require('colors'),
    moment = require('moment');

var Logger = {
    logTimestamp: function () {
        var timestamp = moment().format('D MMM YYYY, H:m');
        return timestamp.blue;
    },
    perform: function (action, targetUrl, req, reqMoment) {
        switch (action) {
        case 'processing':
            console.log(this.logTimestamp(), 'Processing url:'.yellow, targetUrl, 'for ' + req.headers['user-agent'].grey + ' (' + req.headers.host + ')');
            break;
        case 'request-error':
            console.log(this.logTimestamp(), 'Error occured with url:'.red, targetUrl, '(' + req.headers.host + ')');
            break;
        case 'success':
            console.log(this.logTimestamp(), 'Served static version of:'.green, targetUrl, '(' + req.headers.host + ')', 'in', moment().diff(reqMoment, 'milliseconds') + 'ms');
            break;
        case 'wrong-args':
            console.log(this.logTimestamp(), 'Wrong request arguments'.red, 'from ' + req.headers['user-agent'].grey + ' (' + req.headers.host + ')');
            break;
        }
    }
};

phantom.create(function (ph) {
    http.createServer(function (req, res) {
        var query, targetUrl,
            reqMoment = moment();
        query = url.parse(req.url, true).query;
        if (typeof query === 'object' && query.host !== undefined && query.fragment !== undefined) {
            targetUrl = "http://" + query.host + "/#!" + query.fragment;
            Logger.perform('processing', targetUrl, req);
            ph.createPage(function (page) {
                page.set('onLoadFinished', function (status) {
                    if (status !== 'success') {
                        res.writeHead(500, {'Content-Type': 'text/plain'});
                        res.end("Couldn't open desired page\n", 'utf-8');
                        Logger.perform('request-error', targetUrl, req);
                        page.close();
                    } else {
                        page.get('content', function (content) {
                            res.writeHead(200, {'Content-Type': 'text/html'});
                            res.end(content, 'utf-8');
                            Logger.perform('success', targetUrl, req, reqMoment);
                            page.close();
                        });
                    }
                });<commit_msg>More elegant log record composition<commit_after>/*jslint node: true*/
"use strict";
var phantom = require('phantom'),
    http = require('http'),
    url = require('url'),
    colors = require('colors'),
    moment = require('moment');

var Logger = {
    logTimestamp: function () {
        var timestamp = moment().format('D MMM YYYY, H:mm');
        return timestamp.blue;
    },
    compose: function (action, targetUrl, req, reqMoment) {
        switch (action) {
        case 'processing':
            return ['Processing url'.yellow, targetUrl, 'for', req.headers['user-agent'].grey, '(' + req.headers.host + ')'];
        case 'request-error':
            return ['Error occured with url'.red, targetUrl, '(' + req.headers.host + ')'];
        case 'success':
            return ['Served static version of'.green, targetUrl, '(' + req.headers.host + ')', 'in', moment().diff(reqMoment, 'milliseconds') + 'ms'];
        case 'wrong-args':
            return ['Wrong request arguments'.red, 'from', req.headers['user-agent'].grey, '(' + req.headers.host + ')'];
        }
    },
    perform: function () {
        var record = [];
        record.push(this.logTimestamp());
        record = record.concat(this.compose.apply(this, arguments));
        console.log.apply(console, record);
    }
};

phantom.create(function (ph) {
    http.createServer(function (req, res) {
        var query, targetUrl,
            reqMoment = moment();
        query = url.parse(req.url, true).query;
        if (typeof query === 'object' && query.host !== undefined && query.fragment !== undefined) {
            targetUrl = "http://" + query.host + "/#!" + query.fragment;
            Logger.perform('processing', targetUrl, req);
            ph.createPage(function (page) {
                page.set('onLoadFinished', function (status) {
                    if (status !== 'success') {
                        res.writeHead(500, {'Content-Type': 'text/plain'});
                        res.end("Couldn't open desired page\n", 'utf-8');
                        Logger.perform('request-error', targetUrl, req);
                        page.close();
                    } else {
                        page.get('content', function (content) {
                            res.writeHead(200, {'Content-Type': 'text/html'});
                            res.end(content, 'utf-8');
                            Logger.perform('success', targetUrl, req, reqMoment);
                            page.close();
                        });
                    }
                });
{'fraction_non_alphanumeric': 0.16179952644041043, 'fraction_numerical': 0.0031570639305445935, 'mean_word_length': 1.847752808988764, 'pattern_counts': {'":': 0, '<': 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': '1939255', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1339, 'n_words': 294}