<commit_before>/*
 * (C) Copyright 2016 Ventrom LLC (http://www.ventrom.com/)
 *
 * 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.
 *
 * Contributors:
 *     <NAME>
 *     <NAME>
 */

'use strict'

const d3 = require('d3')
const inflector = require('inflected')
const pluck = require('pluck')
const reductio = require('reductio')

function pluckFullLoc(rec) {
    return Object.keys(rec["location"])
        .filter(function(k) {return typeof(rec["location"][k]) === "string"})
        .sort().map(function(k) {return k + ":" + rec["location"][k]}).join(":")
}

function pluckMetric(name) {
    return function(r) {
        for (let m of r.metrics) {
            if (m.name === name) return m.value
        }
        return null
    }
}

function generateReducer(id, accessor, filter) {
    if (!filter) filter = function() {return true}
    let reducer = reductio()
    reducer.value(id)
        .filter(filter)
        .sum(accessor)
        .valueList(accessor)
        .count(true).min(true).max(true).avg(true).median(true)
    return reducer
}

function generateGroupAccessors(id) {
    return {
        "sum": pluck("value."+id+".sum"),
        "average": pluck("value."+id+".avg"),
        "count": pluck("value."+id+".count"),
        "min": pluck("value."+id+".min"),
        "max": pluck("value."+id+".max"),
        "median": pluck("value."+id+".median"),
        "values": pluck("value."+id+".valueList")
    }
}

function recommendFilters(dataset) {
    let result = []

    Object.keys(dataset.dimensions).forEach((tag) => {
        let d = dataset.dimensions[tag]
        // TODO handle recommendations for geo filters
        if (tag === "position" || d.dim === "time" || d.items.size <= 1) return
        let type = d.items.size > 9 ? "row" : "pie"
        d.metrics.forEach((m) => result.push({"type": type, "dimension": d, "groups": [dataset.groups[m]], "defaultGroupAccessor": "sum", "title": dataset.groups[m].title + " by " + inflector.titleize(d.key)}))
    })

    return result
}
<commit_msg>fix filters for groups to handle sparsely populated data and fix accessors for group results to handle rollups into Others category<commit_after>/*
 * (C) Copyright 2016 Ventrom LLC (http://www.ventrom.com/)
 *
 * 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.
 *
 * Contributors:
 *     <NAME>
 *     <NAME>
 */

'use strict'

const d3 = require('d3')
const inflector = require('inflected')
const pluck = require('pluck')
const reductio = require('reductio')

function pluckFullLoc(rec) {
    return Object.keys(rec["location"])
        .filter(function(k) {return typeof(rec["location"][k]) === "string"})
        .sort().map(function(k) {return k + ":" + rec["location"][k]}).join(":")
}

function pluckMetric(name) {
    return function(r) {
        for (let m of r.metrics) {
            if (m.name === name) return m.value
        }
        return null
    }
}

function generateReducer(id, accessor, filter) {
    let full_filter = function(r) {return (accessor(r) !== null) ? true : false}
    if (filter) full_filter = function(r) {return (accessor(r) !== null) ? filter(r) : false}
    let reducer = reductio()
    reducer.value(id)
        .filter(full_filter)
        .sum(accessor)
        .valueList(accessor)
        .count(true).min(true).max(true).avg(true).median(true)
    return reducer
}

function generateGroupAccessors(id) {
    return {
        "sum": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".sum")(d) },
        "average": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".avg")(d) },
        "count": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".count")(d) },
        "min": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".min")(d) },
        "max": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".max")(d) },
        "median": function(d) { return (typeof(d.value) === "number") ? d.value : pluck("value."+id+".median")(d) },
        "values": function(d) { return Array.isArray(d.value) ? d.value : pluck("value."+id+".valueList")(d) }
    }
}

function recommendFilters(dataset) {
    let result = []

    Object.keys(dataset.dimensions).forEach((tag) => {
        let d = dataset.dimensions[tag]
        // TODO handle recommendations for geo filters
        if (tag === "position" || d.dim === "time" || d.items.size <= 1) return
        let type = d.items.size > 9 ? "row" : "pie"
        d.metrics.forEach((m) => result.push({"type": type, "dimension": d, "groups": [dataset.groups[m]], "defaultGroupAccessor": "sum", "title": dataset.groups[m].title + " by " + inflector.titleize(d.key)}))
    })

    return result
}

{'fraction_non_alphanumeric': 0.17111650485436894, 'fraction_numerical': 0.004160887656033287, 'mean_word_length': 3.248159057437408, 'pattern_counts': {'":': 28, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '28320', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1809, 'n_words': 485}