[英]How to group array or column based data by a specific item's/column's value while summing-up the values of another?
我們正在 SAAS 產品中構建集成,該產品從表中導出數據並將其寫入 CSV 文件。 該工具允許編寫 JavaScript(我不太熟悉)代碼,該代碼可以從表中讀取數據並將其寫入 CSV 文件並將文件傳輸到 S3 存儲桶。
直到現在我才能開始工作。 示例 JavaScript -
// Manually invoke this method via 'Test connection'
function testConnection(context) {
return true;
}
// Manually invoke this method via 'Run manually'
function exportData(context) {
// Write the message to the log.
ai.log.logInfo("Script Execution Starting...");
var dataSource = context.getDataSource();
var bucketName = dataSource.getSetting("Bucket Name").getValue();
var awsAccessKey = dataSource.getSetting("Access Key").getValue();
var awsSecretKey = dataSource.getSetting("Access Secret Key").getValue();
var region = dataSource.getSetting("Region").getValue();
var date = new Date();
var currentDate = date.toLocaleDateString().replaceAll("/","");
//Append unique number and current date to the file name.
var key = dataSource.getSetting("Output File Name").getValue() + "_" + Date.now() + "_" + currentDate + ".csv";
ai.log.logInfo("Preparing Output File : " + key);
// Step 1: Build an array with the data that needs to be written to the csv file.
var reader = context.createTableReader(['SqlProjectCodeOnly', 'ResCodeCategory', 'Period', 'SqlAWSAccountID', 'SqlAWSAccountName', 'SqlAWSAccountRegion', 'Value']);
// Step 2: Write the header of the output file.
var result = "Project|Product Name|Report Month|AWS Account ID|AWS Account Name|AWS Region|Adjusted Cost\n";
var row = null;
// If reader.readRow() returns null, then there are no more records.
while ((row = reader.readRow()) !== null) {
//Separate each field by pipe.
result += row.join("|") + '\n';
}
ai.awss3.putFile(bucketName, key, result, region, awsAccessKey, awsSecretKey);
ai.log.logInfo("Script Execution Completed.");
}
它以以下格式輸出數據 -
Project|Product Name|Report Month|AWS Account ID|AWS Account Name|AWS Region|Adjusted Cost
XXXXXXX|AWS Elastic Compute Cloud|01/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|2588.598833
XXXXXXX|AWS Elastic Compute Cloud|01/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|1553.1593004
XXXXXXX|AWS Elastic Compute Cloud|02/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|4141.7581328
XXXXXXX|AWS Elastic Compute Cloud|02/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|1553.1593004
現在它需要按前六列對數據進行分組,並對最后一列的值求和,以獲得以下格式的數據 -
Project|Product Name|Report Month|AWS Account ID|AWS Account Name|AWS Region|Adjusted Cost
XXXXXXX|AWS Elastic Compute Cloud|01/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|4,141.7581334
XXXXXXX|AWS Elastic Compute Cloud|02/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|5,694.9174338
我嘗試使用 reduce 功能,但無法使其正常工作。 當我在下面寫日志時-
ai.log.logVerbose( " row: " + row);
它寫入如下數據 -
row: XXXXXXX|AWS Elastic Compute Cloud|01/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|2588.598833
row: XXXXXXX|AWS Elastic Compute Cloud|01/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|1553.1593004
row: XXXXXXX|AWS Elastic Compute Cloud|02/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|4141.7581328
row: XXXXXXX|AWS Elastic Compute Cloud|02/2022|AWS Account ID (Uncategorized)|AWS Account Name (Uncategorized)|AWS Region (Uncategorized)|1553.1593004.
如果那里的專家可以提供幫助,我將不勝感激。
在讀取每一row
,必須將其push
入例如rows
數組中。 然后可以將此reduce
歸約為一個對象,該對象包含按同一報告月份分組的聚合列組。 然后可以將此類groups
對象的values
map
並join
最終結果。
function collectAndAggregateSameReportMonthAdjustedCost(groups, recordColumns) { const reportMonth = recordColumns[2]; const adjustedCost = parseFloat(recordColumns[6]); let groupedColumns = groups[reportMonth]; if (!groupedColumns) { groupedColumns = groups[reportMonth] = [...recordColumns]; groupedColumns[6] = adjustedCost; } else { groupedColumns[6] = groupedColumns[6] + adjustedCost; } return groups; } // const rows = []; let row; // // If reader.readRow() returns null, then there are no more records. // while ((row = reader.readRow()) !== null) { // rows.push(row); // } // `rows` after being aggregated by the above commented code // will loke like the next provided demonstrator ... const rows = [[ 'XXXXXXX', 'AWS Elastic Compute Cloud', '01/2022', 'AWS Account ID (Uncategorized)', 'AWS Account Name (Uncategorized)', 'AWS Region (Uncategorized)', '2588.598833', ], [ 'XXXXXXX', 'AWS Elastic Compute Cloud', '01/2022', 'AWS Account ID (Uncategorized)', 'AWS Account Name (Uncategorized)', 'AWS Region (Uncategorized)', '1553.1593004', ], [ 'XXXXXXX', 'AWS Elastic Compute Cloud', '02/2022', 'AWS Account ID (Uncategorized)', 'AWS Account Name (Uncategorized)', 'AWS Region (Uncategorized)', '4141.7581328', ], [ 'XXXXXXX', 'AWS Elastic Compute Cloud', '02/2022', 'AWS Account ID (Uncategorized)', 'AWS Account Name (Uncategorized)', 'AWS Region (Uncategorized)', '1553.1593004', ]]; const aggregatedRows = Object .values( rows .reduce(collectAndAggregateSameReportMonthAdjustedCost, {}) ); console.log({ aggregatedRows }); const result = [ 'Project|Product Name|Report Month|AWS Account ID|AWS Account Name|AWS Region|Adjusted Cost', ...aggregatedRows .map(columns => { columns[6] = columns[6] .toLocaleString('en', { useGrouping: 'always', maximumFractionDigits: 7, }); return columns.join('|'); }), ].join('\n'); console.log(result);
.as-console-wrapper { min-height: 100%!important; top: 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.