You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
13 KiB
28 lines
13 KiB
// All material copyright Esri, All Rights Reserved, unless otherwise specified. |
|
// See https://js.arcgis.com/4.30/esri/copyright.txt for details. |
|
//>>built |
|
define("../../../Graphic ../../../chunks/languageUtils ./Adapted ./AttributeFilter ./OrderBy ../support/errorsupport ../support/FeatureSet ../support/IdSet ../support/OrderbyClause ../support/shared ../support/sqlUtils ../support/stats ../support/StatsField ../../../core/MD5 ../../../core/sql/DateOnly ../../../core/sql/SqlTimestampOffset ../../../core/sql/TimeOnly ../../../core/sql/WhereClause ../../../geometry/SpatialReference ../../../layers/support/Field ../../../layers/support/FieldsIndex".split(" "), |
|
function(w,D,u,E,x,m,y,t,z,n,l,p,A,B,F,G,H,q,I,v,J){function K(a){if(!a)return"COUNT";switch(a.toLowerCase()){case "max":return"MAX";case "var":case "variance":return"VAR";case "avg":case "average":case "mean":return"AVG";case "min":return"MIN";case "sum":return"SUM";case "stdev":case "stddev":return"STDDEV"}return"COUNT"}class C extends y{constructor(a){super(a);this._decodedStatsfield=[];this._decodedGroupbyfield=[];this._candosimplegroupby=!0;this.phsyicalgroupbyfields=[];this._internalObjectIdField= |
|
this.objectIdField="ROW__ID";this._adaptedFields=[];this.declaredClass="esri.arcade.featureset.actions.Aggregate";this._uniqueIds=1;this._maxProcessing=this._maxQuery=10;this._parent=a.parentfeatureset;this._config=a}isTable(){return!0}async _getSet(a){null===this._wset&&(this._wset=await this._getFilteredSet("",null,null,null,a));return this._wset}_isInFeatureSet(){return n.IdState.InFeatureSet}_nextUniqueName(a){for(;1===a["T"+this._uniqueIds.toString()];)this._uniqueIds++;const c="T"+this._uniqueIds.toString(); |
|
a[c]=1;return c}_convertToEsriFieldType(a){return a}_initialiseFeatureSet(){const a={};var c=!1;let d=1;var b=this._parent?this._parent.getFieldsIndex():new J([]);this.objectIdField="ROW__ID";for(this.globalIdField="";!1===c;){let r=!1;for(var e=0;e<this._config.groupbyfields.length;e++)if(this._config.groupbyfields[e].name.toLowerCase()===this.objectIdField.toLowerCase()){r=!0;break}if(!1===r)for(e=0;e<this._config.statsfields.length;e++)if(this._config.statsfields[e].name.toLowerCase()===this.objectIdField.toLowerCase()){r= |
|
!0;break}!1===r?c=!0:(this.objectIdField="ROW__ID"+d.toString(),d++)}for(var g of this._config.statsfields)c=new A,c.field=g.name,c.tofieldname=g.name,c.workingexpr=g.expression instanceof q.WhereClause?g.expression:q.WhereClause.create(g.expression,b,this.dateFieldsTimeZoneDefaultUTC),c.typeofstat=K(g.statistic),this._decodedStatsfield.push(c);this._decodedGroupbyfield=[];for(var f of this._config.groupbyfields)g={name:f.name,singlefield:null,tofieldname:f.name,expression:f.expression instanceof |
|
q.WhereClause?f.expression:q.WhereClause.create(f.expression,b,this.dateFieldsTimeZoneDefaultUTC),sqlType:null},this._decodedGroupbyfield.push(g);if(null!==this._parent){this.geometryType=this._parent.geometryType;this.spatialReference=this._parent.spatialReference;this.hasM=this._parent.hasM;this.hasZ=this._parent.hasZ;this.typeIdField="";for(const r of this._parent.fields)a[r.name.toUpperCase()]=1;this.subtypes=this.types=null;this.subtypeField=""}else this.geometryType=n.layerGeometryEsriConstants.point, |
|
this.typeIdField="",this.subtypes=this.types=null,this.subtypeField="",this.spatialReference=new I({wkid:4326});this.fields=[];b=new A;b.field=this._nextUniqueName(a);b.tofieldname=this.objectIdField;b.workingexpr=q.WhereClause.create(this._parent.objectIdField,this._parent.getFieldsIndex(),this.dateFieldsTimeZoneDefaultUTC);b.typeofstat="MIN";this._decodedStatsfield.push(b);for(var h of this._decodedGroupbyfield){b=new v;h.name=this._nextUniqueName(a);b.name=h.tofieldname;b.alias=b.name;if(l.isSingleField(h.expression)){f= |
|
this._parent.getField(l.toWhereClause(h.expression,n.FeatureServiceDatabaseType.Standardised));if(!f)throw new m.FeatureSetError(m.FeatureSetErrorCodes.AggregationFieldNotFound);h.name=f.name;h.singlefield=f.name;this.phsyicalgroupbyfields.push(f.name);b.type=f.type;h.sqlType=f.type}else b.type=this._convertToEsriFieldType(l.predictType(h.expression,this._parent.fields)),f=new v,f.name=h.name,f.alias=f.name,this.phsyicalgroupbyfields.push(h.name),this._adaptedFields.push(new u.SqlExpressionAdapted(f, |
|
h.expression)),this._candosimplegroupby=!1,h.sqlType=b.type;this.fields.push(b)}if(0<this._adaptedFields.length)for(var k of this._parent.fields)this._adaptedFields.push(new u.OriginalField(k));for(h=0;h<this._decodedStatsfield.length;h++){k=new v;b=null;b=this._decodedStatsfield[h];b.field=this._nextUniqueName(a);b.tofieldname===this.objectIdField&&(this._internalObjectIdField=b.field);k.name=b.tofieldname;k.alias=k.name;b=null!==b.workingexpr?l.isSingleField(b.workingexpr)?l.toWhereClause(b.workingexpr, |
|
n.FeatureServiceDatabaseType.Standardised):"":"";switch(this._decodedStatsfield[h].typeofstat){case "SUM":if(""!==b){b=this._parent.getField(b);if(!b)throw new m.FeatureSetError(m.FeatureSetErrorCodes.AggregationFieldNotFound);k.type=b.type}else k.type="double";break;case "MIN":case "MAX":if(""!==b){b=this._parent.getField(b);if(!b)throw new m.FeatureSetError(m.FeatureSetErrorCodes.AggregationFieldNotFound);k.type=b.type}else k.type="double";break;case "COUNT":k.type="integer";break;case "STDDEV":case "VAR":case "AVG":if(""!== |
|
b&&(b=this._parent.getField(b),!b))throw new m.FeatureSetError(m.FeatureSetErrorCodes.AggregationFieldNotFound);k.type="double"}this.fields.push(k)}}async _canDoAggregates(){return!1}async _getFeatures(a,c,d,b){const e=this._maxQuery;return!0===this._checkIfNeedToExpandKnownPage(a,e)?(await this._expandPagedSet(a,e,0,0,b),this._getFeatures(a,c,d,b)):"success"}async _getFilteredSet(a,c,d,b,e){if(""!==a)return new t([],[],!0,null);a=null;a=c=!1;await this._ensureLoaded();if(null!==d)for(var g=0;g<this._decodedStatsfield.length;g++)if(!0=== |
|
l.scanForField(d,this._decodedStatsfield[g].tofieldname)){a=!0;d=null;break}if(null!==b){c=!0;for(g=0;g<this._decodedStatsfield.length;g++)if(!0===b.scanForField(this._decodedStatsfield[g].tofieldname)){b=null;c=!1;break}if(null!==b)for(var f of this._decodedGroupbyfield)if(null===f.singlefield&&!0===b.scanForField(f.tofieldname)){b=null;c=!1;break}}if(!1===this._candosimplegroupby?0:await this._parent._canDoAggregates(this.phsyicalgroupbyfields,this._decodedStatsfield,"",null,null))return f=null, |
|
d&&(f=this._reformulateWhereClauseWithoutGroupByFields(d)),d=null,b&&(d=this._reformulateOrderClauseWithoutGroupByFields(b)),d=await this._parent._getAggregatePagesDataSourceDefinition(this.phsyicalgroupbyfields,this._decodedStatsfield,"",null,f,d,this._internalObjectIdField),this._checkCancelled(e),a=!0===a?new t(d._candidates.slice(0).concat(d._known.slice(0)),[],!0===c?d._ordered:!1,this._clonePageDefinition(d.pagesDefinition)):new t(d._candidates.slice(0),d._known.slice(0),!0===c?d._ordered:!1, |
|
this._clonePageDefinition(d.pagesDefinition));e=this._parent;0<this._adaptedFields.length&&(e=new u.AdaptedFeatureSet({parentfeatureset:this._parent,adaptedFields:this._adaptedFields,extraFilter:null}));!0===a?a=new t(["GETPAGES"],[],!1,{aggregatefeaturesetpagedefinition:!0,resultOffset:0,resultRecordCount:this._maxQuery,internal:{fullyResolved:!1,workingItem:null,type:"manual",iterator:null,set:[],subfeatureset:new x({parentfeatureset:e,orderbyclause:new z(this.phsyicalgroupbyfields.join(",")+","+ |
|
this._parent.objectIdField+" ASC")})}}):(null!==d&&(b=null,d&&(b=this._reformulateWhereClauseWithoutGroupByFields(d)),e=new E({parentfeatureset:e,whereclause:b})),a=new t(["GETPAGES"],[],!1,{aggregatefeaturesetpagedefinition:!0,resultOffset:0,resultRecordCount:this._maxQuery,internal:{fullyResolved:!1,workingItem:null,type:"manual",iterator:null,set:[],subfeatureset:new x({parentfeatureset:e,orderbyclause:new z(this.phsyicalgroupbyfields.join(",")+","+this._parent.objectIdField+" ASC")})}}));return a}_reformulateWhereClauseWithoutStatsFields(a){for(const c of this._decodedStatsfield)a= |
|
l.reformulateWithoutField(a,c.tofieldname,l.toWhereClause(c.workingexpr,n.FeatureServiceDatabaseType.Standardised),this._parent.getFieldsIndex());return a}_reformulateWhereClauseWithoutGroupByFields(a){for(const c of this._decodedGroupbyfield)c.tofieldname!==c.name&&(a=l.reformulateWithoutField(a,c.tofieldname,l.toWhereClause(c.expression,n.FeatureServiceDatabaseType.Standardised),this._parent.getFieldsIndex()));return a}_reformulateOrderClauseWithoutGroupByFields(a){const c=[];for(const d of this._decodedGroupbyfield)d.tofieldname!== |
|
d.name&&c.push({field:d.tofieldname,newfield:d.name});return 0<c.length?a.replaceFields(c):a}_clonePageDefinition(a){return null===a?null:!0===a.aggregatefeaturesetpagedefinition?{aggregatefeaturesetpagedefinition:!0,resultRecordCount:a.resultRecordCount,resultOffset:a.resultOffset,internal:a.internal}:this._parent._clonePageDefinition(a)}async _refineSetBlock(a,c,d){if(!0===this._checkIfNeedToExpandCandidatePage(a,this._maxQuery))return await this._expandPagedSet(a,this._maxQuery,0,0,d),this._refineSetBlock(a, |
|
c,d);this._checkCancelled(d);this._refineKnowns(a,c);return a}_expandPagedSet(a,c,d,b,e){return this._expandPagedSetFeatureSet(a,c,d,b,e)}async _getPhysicalPage(a,c,d){if(!0===a.pagesDefinition.aggregatefeaturesetpagedefinition)return this._sequentialGetPhysicalItem(a,a.pagesDefinition.resultRecordCount,d,[]);a=await this._getAgregagtePhysicalPage(a,c,d);for(const b of a){c={geometry:b.geometry,attributes:{}};d={};for(const e in b.attributes)d[e.toLowerCase()]=b.attributes[e];for(const e of this._decodedGroupbyfield)c.attributes[e.tofieldname]= |
|
d[e.name.toLowerCase()];for(const e of this._decodedStatsfield)c.attributes[e.tofieldname]=d[e.field.toLowerCase()];this._featureCache[c.attributes[this.objectIdField]]=new w(c)}return a.length}_sequentialGetPhysicalItem(a,c,d,b){return new Promise((e,g)=>{null===a.pagesDefinition.internal.iterator&&(a.pagesDefinition.internal.iterator=a.pagesDefinition.internal.subfeatureset.iterator(d));!0===a.pagesDefinition.internal.fullyResolved?e(b.length):0===c?e(b.length):this._nextAggregateItem(a,c,d,b,f=> |
|
{null===f?e(b.length):(--c,e(this._sequentialGetPhysicalItem(a,c,d,b)))},g)})}_nextAggregateItem(a,c,d,b,e,g){try{D.tick(a.pagesDefinition.internal.iterator.next()).then(f=>{if(null===f)null!==a.pagesDefinition.internal.workingItem&&(f=this._calculateAndAppendAggregateItem(a.pagesDefinition.internal.workingItem),b.push(f),a.pagesDefinition.internal.workingItem=null,a.pagesDefinition.internal.set.push(f.attributes[this.objectIdField])),a.pagesDefinition.internal.fullyResolved=!0,e(null);else{const h= |
|
this._generateAggregateHash(f);if(null===a.pagesDefinition.internal.workingItem)a.pagesDefinition.internal.workingItem={features:[f],id:h};else{if(h!==a.pagesDefinition.internal.workingItem.id){const k=this._calculateAndAppendAggregateItem(a.pagesDefinition.internal.workingItem);b.push(k);a.pagesDefinition.internal.workingItem=null;a.pagesDefinition.internal.set.push(k.attributes[this.objectIdField]);--c;a.pagesDefinition.internal.workingItem={features:[f],id:h};e(k);return}a.pagesDefinition.internal.workingItem.features.push(f)}this._nextAggregateItem(a, |
|
c,d,b,e,g)}},g)}catch(f){g(f)}}_calculateFieldStat(a,c,d){const b=[];for(let e=0;e<a.features.length;e++)if(null!==c.workingexpr){const g=c.workingexpr.calculateValue(a.features[e]);null!==g&&(g instanceof F.DateOnly?b.push(g.toNumber()):g instanceof H.TimeOnly?b.push(g.toNumber()):g instanceof G.SqlTimeStampOffset?b.push(g.toMilliseconds()):b.push(g))}else b.push(null);switch(c.typeofstat){case "MIN":d.attributes[c.tofieldname]=p.calculateStat("min",b,-1);break;case "MAX":d.attributes[c.tofieldname]= |
|
p.calculateStat("max",b,-1);break;case "SUM":d.attributes[c.tofieldname]=p.calculateStat("sum",b,-1);break;case "COUNT":d.attributes[c.tofieldname]=b.length;break;case "VAR":d.attributes[c.tofieldname]=p.calculateStat("var",b,-1);break;case "STDDEV":d.attributes[c.tofieldname]=p.calculateStat("stddev",b,-1);break;case "AVG":d.attributes[c.tofieldname]=p.calculateStat("avg",b,-1)}return!0}_calculateAndAppendAggregateItem(a){const c={attributes:{},geometry:null};for(var d of this._decodedGroupbyfield){var b= |
|
d.singlefield?a.features[0].attributes[d.singlefield]:q.WhereClause.convertValueToStorageFormat(d.expression.calculateValue(a.features[0]),d.sqlType);c.attributes[d.tofieldname]=b}for(const e of this._decodedStatsfield)this._calculateFieldStat(a,e,c);d=[];for(b=0;b<this._decodedStatsfield.length;b++)d.push(this._calculateFieldStat(a,this._decodedStatsfield[b],c));this._featureCache[c.attributes[this.objectIdField]]=new w({attributes:c.attributes,geometry:c.geometry});return c}_generateAggregateHash(a){let c= |
|
"";for(const d of this._decodedGroupbyfield){const b=d.singlefield?a.attributes[d.singlefield]:d.expression.calculateValue(a);c=null===b||void 0===b?c+":":c+(":"+b.toString())}return B.createMD5Hash(c,B.outputTypes.String)}async _stat(){return{calculated:!1}}async getFeatureByObjectId(){return null}static registerAction(){y._featuresetFunctions.groupby=function(a,c){return new C({parentfeatureset:this,groupbyfields:a,statsfields:c})}}}return C}); |