<commit_before>/*  NETPIE Microgear Freeboard plugin                             */
/*  Developed by <NAME>                               */
/*  More information about NETPIE please visit https://netpie.io  */

if (typeof dsstore === "undefined") {
    dsstore = {};
}

(function()
{
    freeboard.loadDatasourcePlugin({
        "type_name"   : "netpie_feed",
        "display_name": "NETPIE Feed",
        "description" : "NETPIE Feed Datasource.",
        "external_scripts" : [
            "https://cdn.netpie.io/microgear.js"
        ],
        "settings"    : [
            {
                "name"         : "feedid",
                "display_name" : "Feed ID",
                "type"         : "text",
                //"description"  : "Feed ID",
                "required" : true
            },
            {
                "name"         : "apikey",
                "display_name" : "API Key",
                "type"         : "text",
                //"description"  : "Key",
                //"required"     : true
            },
            {
                "name"         : "granularity_value",
                "display_name" : "Granularity",
                "type"         : "text",
                "default_value": "5",
                "required"     : false,
            },
            {
                name: "granularity_unit",
                display_name: "",
                type: "option",
                "description"  : "Resolution of the data points.",
                options:[
                    {
                        name: "Second",
                        value: "seconds"
                    },
                    {
                        name: "Minute",
                        value: "minutes"
                    },
                    {
                        name: "Hour",
                        value: "hours"
                    },
                    {
                        name: "Day",
                        value: "days"
                    },
                    {
                        name: "Month",
                        value: "months"
                    },
                    {
                        name: "Year",
                        value: "years"
                    }
                ],
                "default_value": "minutes",
            },
            {
                "name"         : "since_value",
                "display_name" : "Since",
                "type"         : "text",
                "default_value": "6",
                "required"     : false,
            },
            {
                name: "since_unit",
                display_name: "",
                type: "option",
                "description"  : "Display data points since ... ago.",
                options:[
                    {
                        name: "Second",
                        value: "seconds"
                    },
                    {
                        name: "Minute",
                        value: "minutes"
                    },
                    {
                        name: "Hour",
                        value: "hours"
                    },
                    {
                        name: "Day",
                        value: "days"
                    },
                    {
                        name: "Month",
                        value: "months"
                    },
                    {
                        name: "Year",
                        value: "years"
                    }
                ],
                "default_value": "hours",
            },
/*
            {
                "name"         : "from_value",
                "display_name" : "From",
                "type"         : "text",
                "default_value": "",
                "description"  : "Timestamp in milliseconds.",
                "required"     : false,
            },
            {
                "name"         : "to_value",
                "display_name" : "To",
                "type"         : "text",
                "default_value": "",
                "description"  : "Timestamp in milliseconds",
                "required"     : false,
            },
*/
            {
                "name"          : "interval",
                "display_name"  : "Reload Every",
                "type"          : "text",
                "description"   : "Data reload interval (seconds)",
                "default_value" : "15"
            }

        ],

        newInstance : function(settings, newInstanceCallback, updateCallback) {
            newInstanceCallback(new feedDatasourcePlugin(settings, updateCallback));
        }
    });


    var feedDatasourcePlugin = function(settings, updateCallback) {
        var self = this;
        var apiChanged = false;
        var currentSettings = settings;
        var interval = settings.interval;
        var data = {};
        dsstore[currentSettings.name] = {};
        var timelist ={seconds:1000,minutes:1000*60,hours:1000*60*60,days:1000*60*60*24,months:1000*60*60*24*30,years:1000*60*60*24*30*12}

        function reloadhData(s) {
            if(typeof data['data'] !== "undefined"){
                var timenow = new Date().getTime();
                var lasttime;
                for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                    for (var j = 0; j<data['data']['data'][i]['values'].length; j++) {
                        if(typeof data['data']['data'][i]['values'][j] !== "undefined"){
                            if(data['data']['data'][i]['values'][j][0]+s.since_value*timelist[s.since_unit]<timenow){
                                data['data']['data'][i]['values'].splice(j, 1)
                            }
                        }
                        lasttime = data['data']['data'][i]['values'][data['data']['data'][i]['values'].length-1][0];
                    }
                }

                var apiurl = 'https://api.netpie.io/feed/'+s.feedid+'?apikey='+s.apikey+'&granularity='+s.granularity_value+s.granularity_unit+'&aggregate=avg&from='+lasttime+'&to='+timenow;
                $.getJSON( apiurl, function(datajson) {
                    if(typeof datajson['lastest_data'] !== "undefined"){
                        for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                            data['data']['data'][i]['values'].splice(data['data']['data'][i]['values'].length-1, 1)
                            data['data']['data'][i]['values'] = data['data']['data'][i]['values'].concat(datajson['data'][i]['values']);
                        }
                        data['data']['to'] = timenow;
                        data['data']['lastest_data'] = datajson['lastest_data'];
                        updateCallback(data);
                    }
                });
            }
            else{
                var apiurl = 'https://api.netpie.io/feed/'+s.feedid+'?apikey='+s.apikey+'&granularity='+s.granularity_value+s.granularity_unit+'&aggregate=avg&since='+s.since_value+s.since_unit;
                $.getJSON( apiurl, function(datajson) {
                    data['data'] = datajson;
                    var newfrom;
                    var newto;
                    if(typeof data['data']['lastest_data'] !== "undefined"){
                        for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                            var timelastitem = data['data']['data'][i]['values'][data['data']['data'][i]['values'].length-1][0];
                            if(data['data']['lastest_data'][i]['values'][0][0]>timelastitem+timelist[data['data']['granularity'][1]]*data['data']['granularity'][0]){
                                if(typeof newfrom === "undefined"){
                                    newfrom = timelastitem+1000;
                                    newto = data['data']['lastest_data'][i]['values'][0][0];
                                }<commit_msg>fix plugin feed<commit_after>/*  NETPIE Microgear Freeboard plugin                             */
/*  Developed by <NAME>                               */
/*  More information about NETPIE please visit https://netpie.io  */

if (typeof dsstore === "undefined") {
    dsstore = {};
}

(function()
{
    freeboard.loadDatasourcePlugin({
        "type_name"   : "netpie_feed",
        "display_name": "NETPIE Feed",
        "description" : "NETPIE Feed Datasource.",
        "external_scripts" : [
            "https://cdn.netpie.io/microgear.js"
        ],
        "settings"    : [
            {
                "name"         : "feedid",
                "display_name" : "Feed ID",
                "type"         : "text",
                //"description"  : "Feed ID",
                "required" : true
            },
            {
                "name"         : "apikey",
                "display_name" : "API Key",
                "type"         : "text",
                //"description"  : "Key",
                //"required"     : true
            },
            {
                "name"         : "granularity_value",
                "display_name" : "Granularity",
                "type"         : "text",
                "default_value": "5",
                "required"     : false,
            },
            {
                name: "granularity_unit",
                display_name: "",
                type: "option",
                "description"  : "Resolution of the data points.",
                options:[
                    {
                        name: "Second",
                        value: "seconds"
                    },
                    {
                        name: "Minute",
                        value: "minutes"
                    },
                    {
                        name: "Hour",
                        value: "hours"
                    },
                    {
                        name: "Day",
                        value: "days"
                    },
                    {
                        name: "Month",
                        value: "months"
                    },
                    {
                        name: "Year",
                        value: "years"
                    }
                ],
                "default_value": "minutes",
            },
            {
                "name"         : "since_value",
                "display_name" : "Since",
                "type"         : "text",
                "default_value": "6",
                "required"     : false,
            },
            {
                name: "since_unit",
                display_name: "",
                type: "option",
                "description"  : "Display data points since ... ago.",
                options:[
                    {
                        name: "Second",
                        value: "seconds"
                    },
                    {
                        name: "Minute",
                        value: "minutes"
                    },
                    {
                        name: "Hour",
                        value: "hours"
                    },
                    {
                        name: "Day",
                        value: "days"
                    },
                    {
                        name: "Month",
                        value: "months"
                    },
                    {
                        name: "Year",
                        value: "years"
                    }
                ],
                "default_value": "hours",
            },
/*
            {
                "name"         : "from_value",
                "display_name" : "From",
                "type"         : "text",
                "default_value": "",
                "description"  : "Timestamp in milliseconds.",
                "required"     : false,
            },
            {
                "name"         : "to_value",
                "display_name" : "To",
                "type"         : "text",
                "default_value": "",
                "description"  : "Timestamp in milliseconds",
                "required"     : false,
            },
*/
            {
                "name"          : "interval",
                "display_name"  : "Reload Every",
                "type"          : "text",
                "description"   : "Data reload interval (seconds)",
                "default_value" : "15"
            }

        ],

        newInstance : function(settings, newInstanceCallback, updateCallback) {
            newInstanceCallback(new feedDatasourcePlugin(settings, updateCallback));
        }
    });


    var feedDatasourcePlugin = function(settings, updateCallback) {
        var self = this;
        var apiChanged = false;
        var currentSettings = settings;
        var interval = settings.interval;
        var data = {};
        dsstore[currentSettings.name] = {};
        var timelist ={seconds:1000,minutes:1000*60,hours:1000*60*60,days:1000*60*60*24,months:1000*60*60*24*30,years:1000*60*60*24*30*12}

        function reloadhData(s) {
            if(typeof data['data'] !== "undefined"){
                var timenow = new Date().getTime();
                var lasttime;
                for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                    for (var j = 0; j<data['data']['data'][i]['values'].length; j++) {
                        if(typeof data['data']['data'][i]['values'][j] !== "undefined"){
                            if(data['data']['data'][i]['values'][j][0]+s.since_value*timelist[s.since_unit]<timenow){
                                data['data']['data'][i]['values'].splice(j, 1)
                            }
                        }
                        lasttime = data['data']['data'][i]['values'][data['data']['data'][i]['values'].length-1][0];
                    }
                }

                var apiurl = 'https://api.netpie.io/feed/'+s.feedid+'?apikey='+s.apikey+'&granularity='+s.granularity_value+s.granularity_unit+'&aggregate=avg&from='+lasttime+'&to='+timenow;
                $.getJSON( apiurl, function(datajson) {
                    if(typeof datajson['lastest_data'] !== "undefined"){
                        for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                            if(typeof datajson['data'][i] !== "undefined"){
                                data['data']['data'][i]['values'].splice(data['data']['data'][i]['values'].length-1, 1)
                                data['data']['data'][i]['values'] = data['data']['data'][i]['values'].concat(datajson['data'][i]['values']);
                            }
                           
                        }
                        data['data']['to'] = timenow;
                        data['data']['lastest_data'] = datajson['lastest_data'];
                        updateCallback(data);
                    }
                });
            }
            else{
                var apiurl = 'https://api.netpie.io/feed/'+s.feedid+'?apikey='+s.apikey+'&granularity='+s.granularity_value+s.granularity_unit+'&aggregate=avg&since='+s.since_value+s.since_unit;
                $.getJSON( apiurl, function(datajson) {
                    data['data'] = datajson;
                    var newfrom;
                    var newto;
                    if(typeof data['data']['lastest_data'] !== "undefined"){
                        for (var i = 0; i<data['data']['lastest_data'].length ; i++) {
                            var timelastitem = data['data']['data'][i]['values'][data['data']['data'][i]['values'].length-1][0];
                            if(data['data']['lastest_data'][i]['values'][0][0]>timelastitem+timelist[data['data']['granularity'][1]]*data['data']['granularity'][0]){
                                if(typeof newfrom === "undefined"){
                                    newfrom = timelastitem+1000;
                                    newto = data['data']['lastest_data'][i]['values'][0][0];
                                }
{'fraction_non_alphanumeric': 0.14386468605667208, 'fraction_numerical': 0.009986268880289601, 'mean_word_length': 1.0210645812310797, 'pattern_counts': {'":': 14, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3066722', 'n_tokens_mistral': 4324, 'n_tokens_neox': 3928, 'n_words': 625}