计算模型总和
Calculated Model Sum
目前,Calculated Model Sample app 仅计算每个 Type 的 count 条记录。我还想计算每个 Type 的评分的 sum。
简单说明一下app的数据模型,每条记录有3个字段:Name(str),Type(str),Rating(数量).
我已将 sum field 添加到 Aggregation 计算模型,但不清楚我到底需要在应用程序中的代码 CalculatedModels 脚本中更改什么。
这是计算每个 Type 的 count 的代码块。我添加了 records.Sum =
来指示 sum 的记录方式和位置。但是,由于现有的 for 循环递增 1(以计算 count),我是否需要另一个 for 循环来递增每个记录的 Rating(因为 sum 将是每个 Type 的 Ratings 的总和?或者可以我只是使用现有的 for 循环,正如我通过插入 records.Sum =
所指示的那样?
/**
* Gathers statistics of Data records distribution by Type field values.
* Used by the calculated datasource AggregationByType.
* @return {Array<Aggregation>} array of records with the stats by Type.
*/
function getStatisticsByType_() {
var allRecords = app.models.Data.newQuery().run();
var stats = {};
for (var i = 0; i < allRecords.length; i++) {
var recordType = allRecords[i].Type;
if (!stats[recordType]) {
stats[recordType] = 0;
}
stats[recordType]++;
}
var records = [];
var properties = Object.getOwnPropertyNames(stats);
for (var j = 0; j < properties.length; j++) {
var record = app.models.Aggregation.newRecord();
record.Name = properties[j];
record.Count = stats[properties[j]];
record.Sum =
records.push(record);
}
records.sort(sortDataByName_);
return records;
}
这应该可以做到。抱歉暂时没时间解释。
function getStatisticsByType_() {
var allRecords = app.models.Data.newQuery().run();
var stats = {};
var data = [];
for (var i = 0; i < allRecords.length; i++) {
var recordType = allRecords[i].Type;
var num = allRecords[i].Rating;
if (!stats[recordType]) {
stats[recordType] = {'Count': 0, 'Sum': 0};
}
stats[recordType].Count++;
stats[recordType].Sum += num;
}
var records = [];
var properties = Object.getOwnPropertyNames(stats);
console.log(stats);
for (var j = 0; j < properties.length; j++) {
var record = app.models.Aggregation.newRecord();
record.Name = properties[j];
record.Count = stats[properties[j]].Count;
record.Sum = stats[properties[j]].Sum;
records.push(record);
}
records.sort(sortDataByName_);
return records;
}
目前,Calculated Model Sample app 仅计算每个 Type 的 count 条记录。我还想计算每个 Type 的评分的 sum。
简单说明一下app的数据模型,每条记录有3个字段:Name(str),Type(str),Rating(数量).
我已将 sum field 添加到 Aggregation 计算模型,但不清楚我到底需要在应用程序中的代码 CalculatedModels 脚本中更改什么。
这是计算每个 Type 的 count 的代码块。我添加了 records.Sum =
来指示 sum 的记录方式和位置。但是,由于现有的 for 循环递增 1(以计算 count),我是否需要另一个 for 循环来递增每个记录的 Rating(因为 sum 将是每个 Type 的 Ratings 的总和?或者可以我只是使用现有的 for 循环,正如我通过插入 records.Sum =
所指示的那样?
/**
* Gathers statistics of Data records distribution by Type field values.
* Used by the calculated datasource AggregationByType.
* @return {Array<Aggregation>} array of records with the stats by Type.
*/
function getStatisticsByType_() {
var allRecords = app.models.Data.newQuery().run();
var stats = {};
for (var i = 0; i < allRecords.length; i++) {
var recordType = allRecords[i].Type;
if (!stats[recordType]) {
stats[recordType] = 0;
}
stats[recordType]++;
}
var records = [];
var properties = Object.getOwnPropertyNames(stats);
for (var j = 0; j < properties.length; j++) {
var record = app.models.Aggregation.newRecord();
record.Name = properties[j];
record.Count = stats[properties[j]];
record.Sum =
records.push(record);
}
records.sort(sortDataByName_);
return records;
}
这应该可以做到。抱歉暂时没时间解释。
function getStatisticsByType_() {
var allRecords = app.models.Data.newQuery().run();
var stats = {};
var data = [];
for (var i = 0; i < allRecords.length; i++) {
var recordType = allRecords[i].Type;
var num = allRecords[i].Rating;
if (!stats[recordType]) {
stats[recordType] = {'Count': 0, 'Sum': 0};
}
stats[recordType].Count++;
stats[recordType].Sum += num;
}
var records = [];
var properties = Object.getOwnPropertyNames(stats);
console.log(stats);
for (var j = 0; j < properties.length; j++) {
var record = app.models.Aggregation.newRecord();
record.Name = properties[j];
record.Count = stats[properties[j]].Count;
record.Sum = stats[properties[j]].Sum;
records.push(record);
}
records.sort(sortDataByName_);
return records;
}