如何通过RESTful API公开有关资源的统计信息?

时间:2014-06-24 17:18:16

标签: rest laravel

我正在为我的网络应用构建API,并且已经公开了我的应用使用的所有资源,例如: /users/roles/posts等没有任何问题。

我现在停留在如何以RESTful方式公开其中一些资源的统计信息。拥有statistics资源似乎不对,因为GET /statistics/1可能是任何东西,结果可能会改变每个请求,因为统计数据是实时的,因此它不会被缓存。

背景:

对于系统中的每个/users,该应用会定期向Steam的API查询他们正在播放的/games,以及他们正在播放的/servers,并将此信息与时间戳一起存储在/states资源中。

汇总此信息是为了显示/statistics/games/current-usagestatistics/servers/current-usage标准HTML网页上最受欢迎的游戏和服务器的统计信息。说明性屏幕截图:serversgames(在不同时间拍摄)。

编辑:基本资源的示例数据

"state": {
    "id": 292002,
    "user_id": 135,
    "game_id": 24663,
    "server_id": 135,
    "created_at":"2014-06-22 21:12:03"
},
"user": {
    "id": 112,
    "username": "ilumos",
    "steam_id_64": "76561197970613738"
},
"server": {
    "id": 135,
    "application_id": 24663,
    "name": null,
    "address": "192.168.241.65",
    "port": "0"
},
"game": {
    "id": 24663,
    "name": "DEFCON",
    "steam_app_id": 1520
}

编辑2:REST是否允许使用时间戳作为资源标识符的端点?例如:

GET /statistics/1403681498/games得到这样的回复:

[
    "game": {
        "id": 123,
        "name": "DEFCON",
        "users": [
            {
                "id": 7654,
                "username": "daryl",
                "server": {
                    "id": 127,
                    "ip": "123.123.123.123",
                    "port": "27960"
                }
            },
            {
                "id": 135,
                "username": "ilumos"
            },
        ]
    }
]

2 个答案:

答案 0 :(得分:6)

你有各种不完全不合理的选择。

你可以

  1. 包含每个回复的统计信息。所有客户都想要 统计?有很多统计数据吗?也许像GET /游戏这样的东西?orderBy = numPlayers-& offset = 0& limit = 10如果所有你跟踪的是玩家数量都会有效。
  2. /statistics/{statisticId}个端点。这本身并不是不合时宜的。
  3. /games/{gameId}/statistics个端点。
  4. 有一个/statistics/games/{gameId}端点。
  5. 实际上,我们无法告诉您实施此操作的最佳方法是什么,因为我们没有足够的信息。

答案 1 :(得分:1)

我将创建一个usage resosuce,因为所有这些统计数据都将是其他资源的使用,现在"现在"或者在历史时间点。

我的URI将如下所示:     GET / usage / {resource-name} / {resource-id}

GET /usage/games/                           collection of games in use right now (with user totals)
GET /usage/servers/                         collection of servers in use right now
GET /usage/games/?timestamp=1234567890      collection of games in use at {timestamp}

GET /usage/games/1                          usage of game with id 1 right now
GET /usage/games/1?timestamp=1234567890     usage of game with id 1 at {timestamp}
GET /usage/games/?user_id=123               usage of game with id 1 filtered to show only user with id 123

将来我可以将资源扩展到电力使用的回报用途

GET /usage/phases/                          collection of phases in use right now (with power draw totals)
GET /usage/phases/1                         usage of phase with id 1 right now
GET /usage/phases/?timestamp=1234567890     collection of phases in use at {timestsamp} (with power draw totals)

除非有关于此问题的内容不完整,否则它似乎是揭示此信息的最合适方式。

相关问题