var db = require("../database/mongo");
var Enumerable = require("linq");

var dt = new Date();
var month = ("0" + (dt.getMonth() + 1)).substr(-2);
var currentmonth = dt.getFullYear() + "-" + month;


var query = {};
query.month = currentmonth;


db.appiId.find({},function(err,data){
    if(!err)
    {
        
        var query = {};
        query.month = currentmonth;
        db.apiusage.find(query,function(err1,usagedata){
            if(!err1)
            {
                console.log("AHA");
                console.log(data.length);
                for(i=0;i<data.length;i++)
                {
                    var usage = Enumerable.from(usagedata).where(function(it){
                        return it.appid == data[i].appid;
                    }).toArray();
                    console.log(usage);
                    if(usage.length < 1)
                    {
                        var theUsage = {};
                        theUsage.appid = data[i].appid;
                        theUsage.month = currentmonth;
                        theUsage.usage = 0;
                        db.apiusage.insert(theUsage);
                    }
                }
            }
            else{
                console.log(err1);
            }
        });
    }
    else{
        console.log("err");
        console.log(err);
    }
});