<commit_before>/* global c3 */
import Ember from 'ember';
import ENV from 'analytics-dashboard/config/environment';


const NIH_HARDCODE = [
    {key: 'FIC', doc_count: 685870},
    {key: 'NCATS', doc_count: 11798267},
    {key:'NCCIH', doc_count: 527109},
    {key:'NCI', doc_count: 37421432},
    {key:'NEI', doc_count: 9258786},
    {key:'NHGRI', doc_count: 5050824},
    {key:'NHLBI', doc_count: 37973512},
    {key:'NIA', doc_count: 30039371},
    {key:'NIAAA', doc_count: 6709896},
    {key:'NIAID', doc_count: 44746441},
    {key:'NIAMS', doc_count: 6571101},
    {key:'NIBIB', doc_count: 4005180},
    {key:'NICHD', doc_count: 13278402},
    {key:'NIDA', doc_count: 29823005},
    {key:'NIDCD', doc_count: 4439991},
    {key:'NIDCR', doc_count: 1155900},
    {key:'NIDDK', doc_count: 32526462},
    {key:'NIEHS', doc_count: 4674188},
    {key:'NIGMS', doc_count: 53652460},
    {key:'NIMH', doc_count: 38119017},
    {key:'NINDS', doc_count: 20635337},
    {key:'NINR', doc_count: 1976020},
    {key:'NLM', doc_count: 1069055},
    {key:'OD', doc_count: 4275564}
];


function log10ToLinear(log_num) {
    if (log_num <= 0) {
        return 0;
    }
    return Math.pow(10, log_num-1).toFixed(0);
}


function linearToLog10(lin_num) {
    if (lin_num <= 0) {
        return 0;
    }
    return (Math.log(lin_num) / Math.LN10) + 1;
}


export default Ember.Component.extend({

    classNames: ['chart'],
    charttitle: 'Untitled Chart',

    dataChanged: Ember.observer('data', function() {
        this.updateChart();
    }),

    dataChanged2: Ember.observer('data.[]', function() {
        this.updateChart();
    }),

    data: [],

    init() {
        this._super(...arguments);
        if (this.get('name') === 'Data Providers') {
            this.processData(this.get("aggregations.publishers.buckets"))
        }
        if (this.get('name') === "Awards") {
            this.processData(NIH_HARDCODE);
        }
        if (this.get('name') === "Funding") {
            this.processData(this.get('aggregations.funders.buckets'));
        }
    },

    processData: async function(data) {
        if (this.get('name') === 'Data Providers') {
            this.set("data",
                await Ember.RSVP.Promise.all(
                    data.map(async function(datum, index, array) {
                        let agent_info = await Ember.$.ajax({
                            url: ENV.apiUrl + '/search/agents/' + datum.key,
                            crossDomain: true,
                            type: 'GET',
                            contentType: 'application/json',
                        });
                        data[index]._source = agent_info._source;
                        data[index].doc_count = data[index].doc_count;
                        data[index].key = data[index].key;
                        return data[index];
                    })
                )
            );
        }
        if (this.get('name') === "Awards") {
            this.set("data", await data);
        }
        if (this.get('name') === "Funding") {
            data =  data.map(function(datum) {
                datum._source = {
                    id: datum.key
                }
                return datum;
            });
            this.set('data', await data);
        }

    },

    sizeChanged: Ember.observer('resizedSignal', function() {
        if (this.get('resizedSignal') === false) { return; }
        this.updateChart();
        this.set('resizedSignal', false);
    }),

    charTypeChange: Ember.observer('chartType', function(){
        this.updateChart();
    }),

    updateChart: async function() {

        var self = this;

        let chart_type = this.get('chartType');

        let chart_options = {
            bindto: this.$(this.element).find('.renderChart')[0],
            data: {
                columns: null, //to be filled later
                type: chart_type,
                onclick: (d) => {
                    self.get('actions').transitionToFacet.call(self, d.id);
                },
            },
            legend: { show: false },
            [chart_type]: {
                title: this.get('charttitle'),  //to be filled later
                label: {
                    show: false
                }
            },
        };

        if (chart_type == 'donut') {
            var title = '';

            var _data = this.get('data');

            if (this.get('item.mappingType') === 'OBJECT_TO_ARRAY') {
                var columns = this.get('data').map(({ key, doc_count }) => [key, doc_count]);
            }

            if (this.get('item.mappingType') === 'OBJECT_AWARDS_NESTED_VALUE_TO_ARRAY') {
                var columns = this.get('data').map(({ key, doc_count, awards }) => [key, awards.value]);
            }

            chart_options['tooltip'] = {
                format: { // We want to return a nice-looking tooltip whose content is determined by (or at least consistent with) sour TS intervals
                    name: function (id, percentage) {
                        return self.data.reduce(function(acc, cur, idx, arr) {
                            if (cur._source.id === id) {
                                return cur._source.name;
                            }
                            return acc;
                        }, false);
                    },
                    value: function (value, percent, id) {
                        return Math.floor(percent*100) + "% (" + value + " records)"; // This isn't perfect, but it's at least more verbose than before
                    }
                }<commit_msg>Fix data providers for curated ucsd data<commit_after>/* global c3 */
import Ember from 'ember';
import ENV from 'analytics-dashboard/config/environment';


const NIH_HARDCODE = [
    {key: 'FIC', doc_count: 685870},
    {key: 'NCATS', doc_count: 11798267},
    {key:'NCCIH', doc_count: 527109},
    {key:'NCI', doc_count: 37421432},
    {key:'NEI', doc_count: 9258786},
    {key:'NHGRI', doc_count: 5050824},
    {key:'NHLBI', doc_count: 37973512},
    {key:'NIA', doc_count: 30039371},
    {key:'NIAAA', doc_count: 6709896},
    {key:'NIAID', doc_count: 44746441},
    {key:'NIAMS', doc_count: 6571101},
    {key:'NIBIB', doc_count: 4005180},
    {key:'NICHD', doc_count: 13278402},
    {key:'NIDA', doc_count: 29823005},
    {key:'NIDCD', doc_count: 4439991},
    {key:'NIDCR', doc_count: 1155900},
    {key:'NIDDK', doc_count: 32526462},
    {key:'NIEHS', doc_count: 4674188},
    {key:'NIGMS', doc_count: 53652460},
    {key:'NIMH', doc_count: 38119017},
    {key:'NINDS', doc_count: 20635337},
    {key:'NINR', doc_count: 1976020},
    {key:'NLM', doc_count: 1069055},
    {key:'OD', doc_count: 4275564}
];


function log10ToLinear(log_num) {
    if (log_num <= 0) {
        return 0;
    }
    return Math.pow(10, log_num-1).toFixed(0);
}


function linearToLog10(lin_num) {
    if (lin_num <= 0) {
        return 0;
    }
    return (Math.log(lin_num) / Math.LN10) + 1;
}


export default Ember.Component.extend({

    classNames: ['chart'],
    charttitle: 'Untitled Chart',

    dataChanged: Ember.observer('data', function() {
        this.updateChart();
    }),

    dataChanged2: Ember.observer('data.[]', function() {
        this.updateChart();
    }),

    data: [],

    init() {
        this._super(...arguments);
        if (this.get('name') === 'Data Providers') {
            this.processData(this.get("aggregations.publishers.buckets"))
        }
        if (this.get('name') === "Awards") {
            this.processData(NIH_HARDCODE);
        }
        if (this.get('name') === "Funding") {
            this.processData(this.get('aggregations.funders.buckets'));
        }
    },

    processData: async function(data) {
        if (this.get('name') === 'Data Providers') {
            this.set("data",
                await Ember.RSVP.Promise.all(
                    data.map(async function(datum, index, array) {
                        let agent_info = await Ember.$.ajax({
                            url: ENV.apiUrl + '/search/agents/' + datum.key,
                            crossDomain: true,
                            type: 'GET',
                            contentType: 'application/json',
                        });
                        data[index]._source = agent_info._source;
                        data[index].doc_count = data[index].doc_count;
                        data[index].key = data[index].key;
                        return data[index];
                    })
                )
            );
        }
        if (this.get('name') === "Awards") {
            this.set("data", await data);
        }
        if (this.get('name') === "Funding") {
            data =  data.map(function(datum) {
                datum._source = {
                    id: datum.key
                }
                return datum;
            });
            this.set('data', await data);
        }

    },

    sizeChanged: Ember.observer('resizedSignal', function() {
        if (this.get('resizedSignal') === false) { return; }
        this.updateChart();
        this.set('resizedSignal', false);
    }),

    charTypeChange: Ember.observer('chartType', function(){
        this.updateChart();
    }),

    updateChart: async function() {

        var self = this;

        let chart_type = this.get('chartType');

        let chart_options = {
            bindto: this.$(this.element).find('.renderChart')[0],
            data: {
                columns: null, //to be filled later
                type: chart_type,
                onclick: (d) => {
                    self.get('actions').transitionToFacet.call(self, d.id);
                },
            },
            legend: { show: false },
            [chart_type]: {
                title: this.get('charttitle'),  //to be filled later
                label: {
                    show: false
                }
            },
        };

        if (chart_type == 'donut') {
            var title = '';

            var _data = this.get('data');

            if (this.get('data').length === 0 && this.get('name') === 'Data Providers') {
                var key = 'UC San Diego Library';
                var data = [{
                   'key': key,
                   'doc_count': 55,
                   '_source': {
                      'id': key,
                      'name': key
                   }
                }];
                this.set('data', data);
            }

            if (this.get('item.mappingType') === 'OBJECT_TO_ARRAY') {
                var columns = this.get('data').map(({ key, doc_count }) => [key, doc_count]);
            }

            if (this.get('item.mappingType') === 'OBJECT_AWARDS_NESTED_VALUE_TO_ARRAY') {
                var columns = this.get('data').map(({ key, doc_count, awards }) => [key, awards.value]);
            }

            chart_options['tooltip'] = {
                format: { // We want to return a nice-looking tooltip whose content is determined by (or at least consistent with) sour TS intervals
                    name: function (id, percentage) {
                        return self.data.reduce(function(acc, cur, idx, arr) {
                            if (cur._source.id === id) {
                                return cur._source.name;
                            }
                            return acc;
                        }, false);
                    },
                    value: function (value, percent, id) {
                        return Math.floor(percent*100) + "% (" + value + " records)"; // This isn't perfect, but it's at least more verbose than before
                    }
                }
{'fraction_non_alphanumeric': 0.14882528833831696, 'fraction_numerical': 0.03408799658265699, 'mean_word_length': 1.6001776988005332, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '663436', 'n_tokens_mistral': 3867, 'n_tokens_neox': 3412, 'n_words': 703}