<commit_before>import Ember from 'ember';
import ENV from 'analytics-dashboard/config/environment';
import isUrl from 'npm:is-url';

var isHTTPURL = function(value) {
    var httpURL = /^https?/;
    return httpURL.test(value.trim()) && isUrl(value);
};

export default Ember.Component.extend({

    showJSON: false,

    objectData: Ember.computed(function() {
        return this.get('data');
    }),

    dataAsString: Ember.computed(function() {
        var data = this.get('data');
        return JSON.stringify(data, null, '    ');
    }),

    identifierURLs: Ember.computed('objectData._source.identifiers', function() {
        return this.get('objectData')._source.identifiers.filter(function(id) {
            return isHTTPURL(id);
        });
    }),

    dataUrl: Ember.computed(function() {
        // Use the doi url to link to the resource, otherwise use the first http url
        var data = this.get('objectData');
        var identifiers = data._source.identifiers;
        var httpUrl = null;
        for (var id of identifiers) {
            if (id.includes('doi')) {
                return id;
            }
            else if (!httpUrl && id.includes('http')) {
                httpUrl = id;
            }
        }
        return httpUrl;
    }),

    funders: Ember.computed('objectData._source.lists.funders', function() {
        this.get('objectData')._source.lists.funders.map((funder) => {
            let total = funder.awards.reduce((total, award) => total + award.amount)
            let formattedTotal = total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            return {name: funder.name, awardTotal: formattedTotal}
        })
    }),

    init(){
        this._super(...arguments);
        let data = this.processData(this.get('data'));
        this.set('data', data);
    },

    processData (data) {
            let datum = data.hits.hits[0];
            var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
            options.timeZone = 'UTC';
            options.timeZoneName = 'short'
            if (datum._source.date) {
                datum["_source"]["date"] = (new Date(datum["_source"]["date"])).toLocaleDateString('en-US', options);
            }
            if (datum._source.date_created) {
                datum["_source"]["date_created"] = (new Date(datum["_source"]["date_created"])).toLocaleDateString('en-US', options)
            }
            if (datum._source.date_modified) {
                datum["_source"]["date_modified"] = (new Date(datum["_source"]["date_modified"])).toLocaleDateString('en-US', options)
            }
            if (datum._source.date_published) {
                datum["_source"]["date_published"] = (new Date(datum["_source"]["date_published"])).toLocaleDateString('en-US', options)
            }<commit_msg>Fix funder/award display on object detail page<commit_after>import Ember from 'ember';
import ENV from 'analytics-dashboard/config/environment';
import isUrl from 'npm:is-url';

var isHTTPURL = function(value) {
    var httpURL = /^https?/;
    return httpURL.test(value.trim()) && isUrl(value);
};

export default Ember.Component.extend({

    showJSON: false,

    objectData: Ember.computed(function() {
        return this.get('data');
    }),

    dataAsString: Ember.computed(function() {
        var data = this.get('data');
        return JSON.stringify(data, null, '    ');
    }),

    identifierURLs: Ember.computed('objectData._source.identifiers', function() {
        return this.get('objectData')._source.identifiers.filter(function(id) {
            return isHTTPURL(id);
        });
    }),

    dataUrl: Ember.computed(function() {
        // Use the doi url to link to the resource, otherwise use the first http url
        var data = this.get('objectData');
        var identifiers = data._source.identifiers;
        var httpUrl = null;
        for (var id of identifiers) {
            if (id.includes('doi')) {
                return id;
            }
            else if (!httpUrl && id.includes('http')) {
                httpUrl = id;
            }
        }
        return httpUrl;
    }),

    funders: Ember.computed('objectData._source.lists.funders', function() {
        var funders = this.get('objectData')._source.lists.funders;
        if (funders) {
            return funders.map((funder) => {
                let total = funder.awards.reduce((total, award) => {
                    return total + award.amount;
                }, 0);
                let formattedTotal = total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                return {name: funder.name, awardTotal: formattedTotal}
            })
        }
    }),

    init(){
        this._super(...arguments);
        let data = this.processData(this.get('data'));
        this.set('data', data);
    },

    processData (data) {
            let datum = data.hits.hits[0];
            var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
            options.timeZone = 'UTC';
            options.timeZoneName = 'short'
            if (datum._source.date) {
                datum["_source"]["date"] = (new Date(datum["_source"]["date"])).toLocaleDateString('en-US', options);
            }
            if (datum._source.date_created) {
                datum["_source"]["date_created"] = (new Date(datum["_source"]["date_created"])).toLocaleDateString('en-US', options)
            }
            if (datum._source.date_modified) {
                datum["_source"]["date_modified"] = (new Date(datum["_source"]["date_modified"])).toLocaleDateString('en-US', options)
            }
            if (datum._source.date_published) {
                datum["_source"]["date_published"] = (new Date(datum["_source"]["date_published"])).toLocaleDateString('en-US', options)
            }
{'fraction_non_alphanumeric': 0.15726495726495726, 'fraction_numerical': 0.0008547008547008547, 'mean_word_length': 2.2778711484593837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4916', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1717, 'n_words': 326}