UNPKG

kpiplus

Version:

KPI+

1 lines 3.74 kB
var _=require("underscore"),async=require("async"),moment=require("moment"),redisCache=require("./redis-cache"),cachedKeys=require("./redis-cache").cachedKeys();Array.prototype.csum=function(e,t,r){return _.reduce(this,function(i,n){return!_.isMatch(n,t)||r&&_.isMatch(n,r)?i:i+(Number(n[e])||0)},0)},Array.prototype.cmax=function(e,t){return _.reduce(this,function(r,i){let n=i[e];return _.isMatch(i,t)&&!_.isUndefined(n)?r?_.isNumber(n)?r>n?r:n:_.isString(n)?1==n.localeCompare(r)?n:r:n:n:r},null)},Array.prototype.reg=function(){return 0==this.length?"":(this.forEach(function(t){""!==t&&(e=e?e+"|^"+t:"^"+t)}),e);var e},Array.prototype.groupBy=function(e,t,r,i){var n=_.groupBy(this,function(t){if(_.isArray(e)){let r="";for(let i of e){let e=t[i];_.isDate(e)&&(e=moment(e).format("YYYYMMDD")),e||(e=""),r=r?r+"_"+e:e}return _.isString(r)&&(r=r.split(" ").join("").split(".").join("")),r||(r="ZZZ"),r}return t[e]});async.map(_.keys(n),function(r,o){let a=n[r],c={};if(_.isArray(e)&&a.length>0)for(let t of e)c[t]=a[0][t],c[t]||(c[t]="");else c[e]=r;t.forEach(function(e){_.isObject(e)?c[e.name]=a.csum(e.value):c[e]=a.csum(e)}),i&&i.fieldmaxs&&i.fieldmaxs.forEach(function(e){_.isObject(e)?c[e.name]=a.cmax(e.value):c[e]=a.cmax(e)}),o(null,c)},function(e,t){if(e)return r(e);r(null,t)})},Array.prototype.joinModel=function(e,t,r,i){_.isArray(r)&&r.forEach(e=>{if(e.where){let t={};for(let r in e.where){t[e.where[r]]=r}e.where=t}}),_.isArray(e)&&_.isArray(t)&&t.forEach(e=>{if(e.where){let t={};for(let r in e.where){t[e.where[r]]=r}e.where=t}}),this.joinModel2(e,t,r,i)},Array.prototype.joinModel2=function(e,t,r,i){if(_.isArray(e)){let n=e,o=t,a=r;e=void 0,t=n,r=o,i=a}var n=this;let o,a=t.collection.name;e&&(a+=e),_.isArray(t)||(o=t,cacheDatas[a]&&(new Date).getTime()-cacheDatas[a].time.getTime()<864e5&&(t=cacheDatas[a].data));new Promise((e,r)=>{_.isArray(t)?e(t):e([])}).then(t=>{async.map(n,function(i,n){_.isArray(r)||(r=[r]),async.map(r,function(r,n){let c={};if(r.where){if(_.isString(r.where)){let e=r.where;r.where={},r.where[e]=e}for(let e in r.where){let t=r.where[e];if(c[t]=i[e],"_id"==t&&!i[e])return n(null)}}if(r.akey&&r.bkey){if("_id"==r.bkey&&!i[r.akey])return n(null);c[r.bkey]=i[r.akey]}let s=_.findWhere(t,c),l="";for(let e in c)"_id"!==e&&c[e]&&(l=l+e+":"+c[e]);l=l?(e||"")+":"+l:c._id?"stp:"+c._id:"";new Promise((t,r)=>{if(o&&!s)new Promise((e,t)=>{l&&o.collection&&o.collection.name&&cachedKeys[o.collection.name.substring(0,o.collection.name.length-1)]?clientRedis.get(l,function(t,r){e(r)}):e()}).then(i=>{if(i)s=JSON.parse(i),t(s);else{let i={};e&&(i.id_app=e),_.extend(i,c),o.findOne(i).lean().exec(function(e,i){if(e)return r(e);i&&(i._id=i._id.toString(),s=i,cacheDatas&&cacheDatas[a]&&cacheDatas[a].data&&cacheDatas[a].data.push(s),l&&clientRedis.set(l,JSON.stringify(s))),t(s)})}});else t(s)}).then(()=>{s&&r.fields&&(_.isArray(r.fields)||(r.fields=[r.fields]),r.fields.forEach(function(e){if(_.isObject(e)){let t=e.name,r=e.value;if(t&&r)i[t]=s[r];else for(let t in e)i[t]=s[e[t]]}else i[e]=s[e]}),r.setFields&&r.setFields(i,s)),n(null)})},function(e,t){if(e)return n(e);n()})},function(e,t){e&&console.log(e),i(n)})}).catch(e=>{console.log(e),i(n)})},Array.prototype.crossJoin=function(e,t,r){let i=[];for(let r of this){let n={};if(t)for(let e in t){n[t[e]]=r[e]}let o=_.where(e,n);if(0===o.length)i.push(r);else for(let e of o){let t={};_.extend(t,r),_.extend(t,e),i.push(t)}}r(i)},Array.prototype.pivot=function(e,t,i,n,o){this.groupBy(t,[],(a,c)=>{c=_.pluck(c,t),this.groupBy(e,[],(o,a)=>{for(r of a)for(let n of c){let o={};o[t]=n,e.forEach(function(e){o[e]=r[e]}),_.isArray(i)?i.forEach(e=>{r[n.toString()+e]=this.csum(e,o)}):r[n.toString()+i]=this.csum(i,o)}n(null,{data:a,cols:c})},o)})};