Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过RESTful API公开有关资源的统计信息?_Rest_Laravel - Fatal编程技术网

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

如何通过RESTful API公开有关资源的统计信息?,rest,laravel,Rest,Laravel,我正在为我的web应用程序构建一个API,并且已经公开了我的应用程序使用的所有资源,例如,/users,/roles,/posts等,没有问题 我现在被困在如何以RESTful方式公开这些资源的统计信息上。拥有统计信息资源似乎是不对的,因为GET/statistics/1可以是任何东西,并且结果可能会更改每个请求,因为统计信息是实时的,所以它将不可缓存 背景: 对于系统中的每个/用户,应用程序会定期查询Steam的API,查找他们正在玩的/游戏,以及他们正在玩的/服务器,并将此信息与时间戳一起存

我正在为我的web应用程序构建一个API,并且已经公开了我的应用程序使用的所有资源,例如,
/users
/roles
/posts
等,没有问题

我现在被困在如何以RESTful方式公开这些资源的统计信息上。拥有
统计信息
资源似乎是不对的,因为
GET/statistics/1
可以是任何东西,并且结果可能会更改每个请求,因为统计信息是实时的,所以它将不可缓存

背景:

对于系统中的每个
/用户
,应用程序会定期查询Steam的API,查找他们正在玩的
/游戏
,以及他们正在玩的
/服务器
,并将此信息与时间戳一起存储在
/状态
资源中

此信息被汇总以显示
/statistics/games/current usage
statistics/servers/current usage
标准HTML页面上最受欢迎的游戏和服务器的列表。说明性屏幕截图:,(在不同时间拍摄)

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

"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"
            },
        ]
    }
]

你有很多并非完全不合理的选择

你可以

  • 在每个响应中包含统计信息。所有客户都想要吗 统计数字?有很多统计数据吗?如果您只跟踪玩家的数量,那么像GET/games?orderBy=numPlayers-&offset=0&limit=10这样的东西可能会起作用
  • 有一个
    /statistics/{statististicad}
    端点。这并不是天生没有效果的
  • 拥有一个
    /games/{gameId}/statistics
    端点
  • 拥有一个
    /statistics/games/{gameId}
    端点

  • 实际上,我们没有办法告诉您最好的方法是什么,因为我们没有足够的信息。

    我将创建一个
    使用率
    原因,因为所有这些统计数据都将是其他资源的使用率,无论是“现在”还是在历史时刻

    我的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)
    

    除非有某种内在的不安,否则这似乎是最合适的公开信息的方式。

    不太可能每个客户都想知道每个回复的数据,但这给了我一些启发。。。但是,选项3和4看起来不错-是否允许添加时间参数?如果缺少参数,则将其默认为“now”?我建议选项三,这是我认为最符合逻辑的。您将获得
    /games/{gameID}
    ,该资源将您链接到
    /game/{gameID}/stats
    的统计信息。是的,在末尾添加一个时间戳非常好,因为
    ../{timestamp}
    ..time={timestamp}
    我同意时间戳,尽管我建议使用?time={timestamp}选项。REST不关心ID是什么。只要你能用它来唯一地识别资源,你就很好。但是要小心,因为如果客户端时钟和服务器时钟位于不同的时区和/或不同步,时间戳可能会很棘手。