如何计算组织在调用API时的总成本?

huangapple go评论61阅读模式
英文:

How to calculate total cost for an Organization when they call the API?

问题

我已经设计了一个名为“Organization”的集合,如下所示:
其中“name”是组织的名称,“desc”是组织的描述,“id_app”是该“organization”的应用程序数组

1- 以下是“Organization”

{
    "_id" : ObjectId("64b4b8bc3975804004fa81b4"),
    "id_app" : [
        ObjectId("64b4b842a5831248e8a6b65c"),
        ObjectId("64b4b34f54106c24dc018992")
    ],
    "is_deleted" : false,
    "name" : "Org1",
    "desc" : "Org1 desc",
    "createdAt" : ISODate("2023-07-17T03:42:52.845+0000"),
    "updatedAt" : ISODate("2023-07-17T03:42:52.845+0000"),
    "__v" : NumberInt(0)
}

2- 以下是“Application”集合:

其中“id_api”是应用程序调用的“apis”的数组

{
    "_id" : ObjectId("64b4b34f54106c24dc018992"),
    "id_api" : [
        ObjectId("64b4ac07c4784418986870dc"),
        ObjectId("64b4abcdc4784418986870d7")
    ],
    "is_deleted" : false,
    "name" : "app1",
    "desc" : "app1 desc",
    "createdAt" : ISODate("2023-07-17T03:19:43.573+0000"),
    "updatedAt" : ISODate("2023-07-17T03:19:43.573+0000"),
    "__v" : NumberInt(0)
}

3- 以下是“Apis”集合:

其中“name”是API名称,“desc”是API描述,“URL”是API的路径,“field”是API的字段,例如教育API。

{
    "_id" : ObjectId("64b4abcdc4784418986870d7"),
    "is_deleted" : false,
    "name" : "api1",
    "desc" : "Api 1 desc",
    "URL" : "http://localhost:8080/api/api1",
    "field" : "edu",
    "createdAt" : ISODate("2023-07-17T02:47:41.849+0000"),
    "updatedAt" : ISODate("2023-07-17T02:47:41.849+0000"),
    "__v" : NumberInt(0)
}

每当一个“app”调用一个“API”,都会生成一个费用(例如cost1 = 10$),并创建一个名为“Billing”的集合。

4- 以下是“Billing”集合

{
    "_id" : ObjectId("64b4c3d6068f5537e4bbd01b"),
    "is_deleted" : false,
    "cost1" : NumberInt(10),
    "cost2" : NumberInt(5),
    "cost3" : NumberInt(3),
    "id_app" : ObjectId("64b4b842a5831248e8a6b65c"),
    "createdAt" : ISODate("2023-07-17T04:30:14.646+0000"),
    "updatedAt" : ISODate("2023-07-17T04:30:14.646+0000"),
    "__v" : NumberInt(0)
}

当有“Organation”、“Application”、“Apis”和“Billing”集合时,问题是如何计算组织在其应用程序调用API时被收取的费用(总费用)。

非常感谢!

英文:

I have designed a collection named Organization as follows:
with name is the name of the organization, desc is a description of the organization, id_app is an array of applications for that organization

1- Below is Organization

{
    "_id" : ObjectId("64b4b8bc3975804004fa81b4"),
    "id_app" : [
        ObjectId("64b4b842a5831248e8a6b65c"),
        ObjectId("64b4b34f54106c24dc018992")
    ],
    "is_deleted" : false,
    "name" : "Org1",
    "desc" : "Org1 desc",
    "createdAt" : ISODate("2023-07-17T03:42:52.845+0000"),
    "updatedAt" : ISODate("2023-07-17T03:42:52.845+0000"),
    "__v" : NumberInt(0)
}

2- Bellow is Application collection:

with id_api is an array of the apis that application calls

{
    "_id" : ObjectId("64b4b34f54106c24dc018992"),
    "id_api" : [
        ObjectId("64b4ac07c4784418986870dc"),
        ObjectId("64b4abcdc4784418986870d7")
    ],
    "is_deleted" : false,
    "name" : "app1",
    "desc" : "app1 desc",
    "createdAt" : ISODate("2023-07-17T03:19:43.573+0000"),
    "updatedAt" : ISODate("2023-07-17T03:19:43.573+0000"),
    "__v" : NumberInt(0)
}

3- Bellow is Apis collection:

with name is the API name, desc is the api description, URL is the path of the API and
field is the field of that api, for example education api.


{
    "_id" : ObjectId("64b4abcdc4784418986870d7"),
    "is_deleted" : false,
    "name" : "api1",
    "desc" : "Api 1 desc",
    "URL" : "http://localhost:8080/api/api1",
    "field" : "edu",
    "createdAt" : ISODate("2023-07-17T02:47:41.849+0000"),
    "updatedAt" : ISODate("2023-07-17T02:47:41.849+0000"),
    "__v" : NumberInt(0)
}

Each time an app calls an API, a fee will be generated (eg cost1 = 10$ ), and a collection named Billing will be created.

4- And below is Billing collection

{
    "_id" : ObjectId("64b4c3d6068f5537e4bbd01b"),
    "is_deleted" : false,
    "cost1" : NumberInt(10),
    "cost2" : NumberInt(5),
    "cost3" : NumberInt(3),
    "id_app" : ObjectId("64b4b842a5831248e8a6b65c"),
    "createdAt" : ISODate("2023-07-17T04:30:14.646+0000"),
    "updatedAt" : ISODate("2023-07-17T04:30:14.646+0000"),
    "__v" : NumberInt(0)
}

While there are Organation, Application, Apis, and Billing collections. So the question is, how to calculate the billing amount (total cost) that an organization is charged when their apps call the API.

Thanks you so much

答案1

得分: 1

  1. 你可以使用组织ID从组织集合中获取应用程序ID。
db.getCollection('organization').find({_id: ObjectId("64b4b8bc3975804004fa81b4")});
  1. 然后,你可以使用应用程序ID进行聚合操作。
db.getCollection('Billing').aggregate([
    {
        $match: {
            id_app: {
                $in: [ObjectId("64b4b842a5831248e8a6b65c"), ObjectId("64b4b34f54106c24dc018992")]
            }
        }
    },
    {
        $group: {
            _id: null, totalCost: {
                $sum: "$cost1"
            }
        }
    }
]);
  1. 结果如下:
{
    "_id" : null,
    "totalCost" : 10
}
  1. 你可以使用以下查询来计算总成本。
db.getCollection('Billing').aggregate([
    {
        $match: {
            id_app: {
                $in: [ObjectId("64b646f602dbe6f481912d85"), ObjectId("64b4b34f54106c24dc018992")]
            }
        }
    },
    {
        $project: {
            totalCost: {
                $sum: ["$cost1", "$cost2", "$cost3"]
            }
        }
    }
]);
英文:
  1. You can app ids from organization collection using organization id
    db.getCollection('organization').find({_id: ObjectId("64b4b8bc3975804004fa81b4")});

  1. And next you can use aggregate using application ids
    db.getCollection('Billing').aggregate([
        {
            $match: {
                id_app: {
                    $in: [ObjectId("64b4b842a5831248e8a6b65c"), ObjectId("64b4b34f54106c24dc018992")
                    ]
                }
            }
        },
        {
            $group: {
                _id: null, totalCost: {
                    $sum: "$cost1"
                }
            }
        }
    ]);

  1. The result
    {
        "_id" : null,
        "totalCost" : 10
    }
  1. You can use following query to calculate total cost in a unique query
 db.getCollection('Billing').aggregate([
        {
            $match: {
                id_app: {
                    $in: [ObjectId("64b646f602dbe6f481912d85"), ObjectId("64b4b34f54106c24dc018992")
                    ]
                }
            }
        },
        {
            $project: {
                totalCost: {
                    $sum: ["$cost1", "$cost2", "$cost3"]
                }
            }
        }
    ]);

huangapple
  • 本文由 发表于 2023年7月17日 12:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定