Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expose statistics about resources through a RESTful API?

Tags:

rest

laravel

I'm building an API for my web app and have got as far as exposing all the resources my app uses, e.g. /users, /roles, /posts etc with no problem.

I'm now stuck on how to expose statistics about some of these resources in a RESTful way. It doesn't seem right to have a statistics resource, as GET /statistics/1 could be anything, and the results will likely change each request, as the stats are real-time, so it will not be cacheable.

Background:

For each of the /users in the system, the app periodically queries Steam's API for the /games they are playing, and the /servers they are playing it on, and stores this information along with a timestamp in the /states resource.

This information is aggregated to show a tally of the most popular games and servers on the /statistics/games/current-usage and statistics/servers/current-usage standard HTML pages. Illustrative screenshots: servers, games (taken at different times).

EDIT: Sample data for the basic resources

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

EDIT 2: Does REST permit endpoints that use a timestamp as the resource identifier? e.g:

GET /statistics/1403681498/games to get a response like this:

[
    "game": {
        "id": 123,
        "name": "DEFCON",
        "users": [
            {
                "id": 7654,
                "username": "daryl",
                "server": {
                    "id": 127,
                    "ip": "123.123.123.123",
                    "port": "27960"
                }
            },
            {
                "id": 135,
                "username": "ilumos"
            },
        ]
    }
]
like image 445
ilumos Avatar asked Jun 24 '14 17:06

ilumos


People also ask

What is a REST API (RESTful API)?

A REST API (also called a “RESTful” API) is a specific type of API that follows these guidelines. REST stands for Representational State Transfer. This means that when a client requests a resource using a REST API, the server transfers back the current state of the resource in a standardized representation.

How do I use expose REST API?

The REST API uses Basic authentication. REST APIs allow you to expose data and functionality of your application over HTTP to be used by other systems. Each REST API method of this project illustrates one or two use cases of how you can use expose REST APIs. Check how to: 1. In the method, set the 'HTTP method' property to GET. 2.

What are the resource methods associated with REST APIs?

The four main resource methods that are associated with REST APIs are: GET: This method allows for the server to find the data you requested and sends it back to you. PUT: If you perform the ‘PUT’ request, then the server will update an entry in the database. POST: This method permits the server to create a new entry in the database.

How do I start building a REST API?

The first thing to do when building a REST API is to identify which resources will be exposed by your module. Note that resources are not necessarily domain entities, althought it may appear to be so. Let’s take for instance an application that manages a library. Examples of resources that you might want to expose are: …


1 Answers

I'm going to go with creating a usage resosuce as all of these statistics will be the usage of other resources, either "right now" or at a historic point in time.

My URIs will look like this: 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

And in future I can extend the resource to for example return usage for electricity usage

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)

Unless there's something inhernatly un-RESTful about this it seems to be the most fitting way of exposing this info.

like image 99
ilumos Avatar answered Nov 02 '22 13:11

ilumos