<commit_before>#!/usr/bin/env node
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fileencoding=utf-8 : */
/*
 *     Copyright 2013 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */

// enable JavaScript strict mode.
"use strict";

// import modules
var http = require('http');
var querystring = require('querystring');
var url = require('url');
var util = require('util');

// set to enable debug logging.
var debug = false;

/**
 *  Singleton object for agent database.
 */
var agentdb = {
    /** Default URL used to send feedback. */
    FEEDBACK_URL : 'http://alscan.info/agents/feedback',

    /** mask on source/group value to extract Group id. */
    GROUP_MASK : 0x1F,
    
    /** shift on source/group value to extract Source id. */
    SOURCE_SHIFT : 5,
    
    /** mask on source/group value to extract Source id. */
    SOURCE_MASK : 0xFFFF,
    
    /** Agent database. */
    database : undefined,

    /**
     *  Initialize the database.
     */
    _initDatabase : function () {
        this.database = require('./database-min.js');
        var HashTable = require('./hash.js').HashTable;
        this.database.hash = new HashTable(this.database.HASH_TABLE_SIZE);
        this.database.hash.table = this.database.table;
    },
    
    /**
     *  return Group name.
     *
     *  @param x source/group value.
     *  @rtype String.
     */
    getGroup : function(x) {
        var groupId = x & this.GROUP_MASK;
        if (groupId >= this.database.groups.length) {
            return "Unknown";
        }
        return this.database.groups[groupId];
    },
    
    /**
     *  return Source name.
     *
     *  @param x source/group value.
     *  @rtype String.
     */
    getSource : function (x) {
        var sourceId = (x >>> this.SOURCE_SHIFT) & this.SOURCE_MASK;
        if (sourceId >= this.database.sources.length) {
            debug && console.error("getSource: x=" + x + ", sourceId=" + sourceId);
            return "Unknown";
        }
        return this.database.sources[sourceId];
    },
    
    /**
     *  return source/group value from groupId and sourceId.
     *
     *  @param groupId
     *  @param sourceId
     *  @rtype Number
     */
    getX : function (groupId, sourceId) {
        return (sourceId << this.SOURCE_SHIFT) + groupId;
    },
    
    /**
     *  Lookup an agent using the hash table.
     *
     *  @param agent string.
     *  @rtype null|object
     */
    lookupHash : function(agent) {
        if (!this.database) {
            this._initDatabase();
        }
        var r = this.database.hash.lookup(agent, 'a');
        if (r === null) {
            debug && console.error("samplx-agentdb::lookupHash: not found.");
            return null;
        }
        return { "group" : this.getGroup(r.x), "source" : this.getSource(r.x) };
    },
    
    lookupPattern : function(agent) {
        if (!this.database) {
            this._initDatabase();
        }
        for (var n=0, length= this.database.patterns.length; n < length; n++) {
            if (this.database.patterns[n].p.test(agent)) {
                return { "group"  : this.getGroup(this.database.patterns[n].x),
                         "source" : this.getSource(this.database.patterns[n].x) };
            }
        }
        debug && console.error("samplx-agentdb::lookupPattern: not found.");
        return null;
    },
    
    lookup : function(agent) {
        debug && console.error("samplx-agentdb::lookup('" + agent + "')");
        var r = this.lookupHash(agent);
        if (r === null) {
            r = this.lookupPattern(agent);
        }
        if (r === null) {
            return { "group" : "Unknown", "source" : "Unknown" };
        }
        debug && console.error("samplx-agentdb::lookup: found result=" + util.inspect(r));
        return r;
    },
    
    /**
     *  Report a list of user-agents for feedback.
     *
     *  @param agents array of user-agent strings to send.
     *  @param reportingUrl where to report.
     */
    sendFeedback : function(agents, reportingUrl) {
        if (util.isArray(agents) && (agents.length > 0)) {
            var parsedUrl = url.parse(reportingUrl || agentdb.FEEDBACK_URL);<commit_msg>Handle non-string args. Map empty string to None/None. Allow hash table seed, size and multiplier parameters to be set from database parameters.<commit_after>#!/usr/bin/env node
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fileencoding=utf-8 : */
/*
 *     Copyright 2013 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */

// enable JavaScript strict mode.
"use strict";

// import modules
var http = require('http');
var querystring = require('querystring');
var url = require('url');
var util = require('util');

// set to enable debug logging.
var debug = false;

/**
 *  Singleton object for agent database.
 */
var agentdb = {
    /** Default URL used to send feedback. */
    FEEDBACK_URL : 'http://alscan.info/agents/feedback',

    /** mask on source/group value to extract Group id. */
    GROUP_MASK : 0x1F,
    
    /** shift on source/group value to extract Source id. */
    SOURCE_SHIFT : 5,
    
    /** mask on source/group value to extract Source id. */
    SOURCE_MASK : 0xFFFF,
    
    /** Agent database. */
    database : undefined,

    /**
     *  Initialize the database.
     */
    _initDatabase : function () {
        this.database = require('./database-min.js');
        var HashTable = require('./hash.js').HashTable;
        var size = this.database.HASH_TABLE_SIZE;
        var seed = this.database.HASH_TABLE_SEED;
        var multiplier = this.database.HASH_TABLE_MULTIPLIER;
        this.database.hash = new HashTable(size, seed, multiplier);
        this.database.hash.table = this.database.table;
    },
    
    /**
     *  return Group name.
     *
     *  @param x source/group value.
     *  @rtype String.
     */
    getGroup : function(x) {
        var groupId = x & this.GROUP_MASK;
        if (groupId >= this.database.groups.length) {
            return "Unknown";
        }
        return this.database.groups[groupId];
    },
    
    /**
     *  return Source name.
     *
     *  @param x source/group value.
     *  @rtype String.
     */
    getSource : function (x) {
        var sourceId = (x >>> this.SOURCE_SHIFT) & this.SOURCE_MASK;
        if (sourceId >= this.database.sources.length) {
            debug && console.error("getSource: x=" + x + ", sourceId=" + sourceId);
            return "Unknown";
        }
        return this.database.sources[sourceId];
    },
    
    /**
     *  return source/group value from groupId and sourceId.
     *
     *  @param groupId
     *  @param sourceId
     *  @rtype Number
     */
    getX : function (groupId, sourceId) {
        return (sourceId << this.SOURCE_SHIFT) + groupId;
    },
    
    /**
     *  Lookup an agent using the hash table.
     *
     *  @param agent string.
     *  @rtype null|object
     */
    lookupHash : function(agent) {
        if (!this.database) {
            this._initDatabase();
        }
        if (typeof agent != 'string') {
            debug && console.error("samplx-agentdb::lookupHash: non-string argument.");
            return null;
        }
        var r = this.database.hash.lookup(agent, 'a');
        if (r === null) {
            debug && console.error("samplx-agentdb::lookupHash: not found.");
            return null;
        }
        return { "group" : this.getGroup(r.x), "source" : this.getSource(r.x) };
    },
    
    lookupPattern : function(agent) {
        if (!this.database) {
            this._initDatabase();
        }
        if (typeof agent != 'string') {
            debug && console.error("samplx-agentdb::lookupPattern: non-string argument.");
            return null;
        }
        for (var n=0, length= this.database.patterns.length; n < length; n++) {
            if (this.database.patterns[n].p.test(agent)) {
                return { "group"  : this.getGroup(this.database.patterns[n].x),
                         "source" : this.getSource(this.database.patterns[n].x) };
            }
        }
        debug && console.error("samplx-agentdb::lookupPattern: not found.");
        return null;
    },
    
    lookup : function(agent) {
        debug && console.error("samplx-agentdb::lookup('" + agent + "')");
        var r = this.lookupHash(agent);
        if (r === null) {
            r = this.lookupPattern(agent);
        }
        if (r === null) {
            if ((typeof agent != 'string') || (agent === '')) {
                return { "group" : "None", "source" : "None" };
            } else {
                return { "group" : "Unknown", "source" : "Unknown" };
            }
        }
        debug && console.error("samplx-agentdb::lookup: found result=" + util.inspect(r));
        return r;
    },
    
    /**
     *  Report a list of user-agents for feedback.
     *
     *  @param agents array of user-agent strings to send.
     *  @param reportingUrl where to report.
     */
    sendFeedback : function(agents, reportingUrl) {
        if (util.isArray(agents) && (agents.length > 0)) {
            var parsedUrl = url.parse(reportingUrl || agentdb.FEEDBACK_URL);
{'fraction_non_alphanumeric': 0.13159425911559347, 'fraction_numerical': 0.0034910783553141972, 'mean_word_length': 2.0403891509433962, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5861746', 'n_tokens_mistral': 3058, 'n_tokens_neox': 2999, 'n_words': 812}