zorchenhimer / MovieNight

Single instance video streaming server with integrated chat.
https://discord.gg/F2VSgjJ
MIT License
691 stars 87 forks source link

Minimal statistics dashboard #155

Closed zorglube closed 3 years ago

zorglube commented 3 years ago

I just open this issue to ask if it is possible to work on a very minimal statistics dashboard. I'm thinking about something like :

Some work in progress is visible here.

zorchenhimer commented 3 years ago

These are already commands in chat. I'm not really sure we'd really need a specific dashboard for them.

zorglube commented 3 years ago

Yup I saw that, but :

The statistics I'm interested in are the video-steam's stats. Maybe I missed something ?

zorchenhimer commented 3 years ago

Stats about the video streams aren't currently recorded at all, so adding a dashboard for it might not be trivial.

As for the IPs being wrong behind nginx, you need to set the X-Forwarded-For proxy header for the websocket (Note: banning users also requires this. Otherwise you'd be banning everybody lol):

location /ws {
        proxy_pass http://movienight;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
zorglube commented 3 years ago

I wasn't thinking about recording stream stats. Adding the stream stats into the /stats message can be a nice minimal achievement.

Thank for the tips, I'll check my Nginx conf ;-)

zorchenhimer commented 3 years ago

In order to display stats, I need to actually get them from somewhere. This is what "recording stats" does. It figures out the stats and saves them somewhere so the command doesn't hang.

zorglube commented 3 years ago

Just to be sure I don't miss anything, if you don't mind can you have a small look on this Nginx setup

    set                         $upstream [MovieNight-IP];
    # Movie Night
    location / {
        proxy_pass          http://$upstream;
        proxy_http_version      1.1;

        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade";
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect          off;
        proxy_next_upstream error timeout invalid_header http_500 http_404;
        proxy_connect_timeout   2;
        proxy_intercept_errors  on;
    }

    # Movie Night Chat Websoket forward 
    location /ws {
        proxy_pass          http://$upstream;
        proxy_http_version      1.1;
        proxy_set_header        Host $host;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade";
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
zorglube commented 3 years ago

In order to display stats, I need to actually get them from somewhere. This is what "recording stats" does. It figures out the stats and saves them somewhere so the command doesn't hang.

Yes yes definitely, by recording I understood get/grab & write the stats somewhere on a disk.

zorchenhimer commented 3 years ago

Just to be sure I don't miss anything, if you don't mind can you have a small look on this Nginx setup

It looks like you're doing a bunch of extra stuff for location / that shouldn't really be needed. Additionally, I don't think proxy_next_upstream will actually do anything if you didn't define a block of upstream servers. The relevant bit of my config:

upstream movienight {
        server 127.0.0.1:8089;
}

server {
        location / {
                proxy_pass http://movienight;
        }

        # ... 
}

If I wanted to grab video stats I would need to set the X-Forwarded-For header, but iirc that isn't even looked at on the video end right now.

zorglube commented 3 years ago

The proxy_next_upstream is here to provide an default response when MovieNight is off, actually today it's like that

server {
    set                         $upstream [MovieNight-IP];

    # Movie Night
    location / {
        proxy_pass              http://$upstream;
        [...]
        proxy_next_upstream     error timeout invalid_header http_500 http_404;
        proxy_connect_timeout   2;
        proxy_intercept_errors  on;
    }

    # Movie Night Chat Websoket forward 
    location /ws {
        [...]
    }

    error_page 500 502 503 504 /error.html;
    location /error.html {
        return                  200 "<!DOCTYPE html><html><head><title>Movie Night</title></head><body style=\"background-color:black;\"><h1 style=\"color:grey;\">Server is down</h1></body></html>";
    }
}
zorchenhimer commented 3 years ago

Ah, that's fair. I just capture the 502 error and use a custom error page for that. Same thing, different execution, I suppose.

zorglube commented 3 years ago

Probably Way toooo much options in Nginx ^_^

zorglube commented 3 years ago

But... About the stream stats. Let say I'll work on that. It imply I'll have to dig into Joy4 rmtp server to get some kind of viewer += 1 each time a new client is connecting.

and have the viewer value stored into :

type streamStats struct {
    messageIn   int
    messageOut  int
    maxUsers    int
    start       time.Time
    mutex       sync.Mutex
    viewers     int
    maxViewers  int
    streamStart time.Time
    streamLive  bool // True if live
}
zorchenhimer commented 3 years ago

Shouldn't be necessary to dive into that library for that. Iirc, there's a map of channels around somewhere that does packet coping from the server to each client. Have a look at the http handler for the /live endpoint.

zorglube commented 3 years ago

Thank !

zorglube commented 3 years ago

By the way, I just red that the X-Forwarded-For header is now standardized as Forwarded header.