Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Plex dashboard bandwidth and resources #636

Merged
merged 17 commits into from
Jan 11, 2021

Conversation

JonnyWong16
Copy link
Collaborator

@JonnyWong16 JonnyWong16 commented Jan 3, 2021

Description

Adds support for the bandwidth and resources data on the Plex dashboard.

New objects:

  • server.SystemAccount is an account associated with the server. (Updated existing object)
  • server.SystemDevice is a device associated with the server.
  • server.StatisticsBandwidth is the bandwidth data for a single account / device / timestamp combination.
  • server.StatisticsResources is the resource data for a single timestamp.

New methods:

  • PlexServer.systemAccounts() returns a list of accounts associated with the Plex server. (Updated existing method)
  • PlexServer.systemDevices() returns a list of devices associated with the Plex server.
  • PlexServer.bandwidth() will return a list of the dashboard bandwidth data. The timespan parameter will bin the bandwidth data and **kwargs can be used to filter the bandwidth data.
  • PlexServer.resources() will return a list of the dashboard CPU and RAM usage data. Only live data (1 second intervals) is available.

Other changes:

  • MyPlexAccount.id casted to an int instead of a str.

Example:

from plexapi.server import PlexServer
plex = PlexServer('http://localhost:32400', token='xxxxxxxxxxxxxxxxxxxx')

# Filter bandwidth data for December 2020 and later and more than 1 GB used.
filters = {
    'at>': datetime(2020, 12, 1),
    'bytes>': 1024**3
}

# Retrieve bandwidth data in one day timespans.
bandwidthData = plex.bandwidth(timespan='days', **filters)

# Print out bandwidth usage for each account and device combination
for bandwidth in sorted(bandwidthData, key=lambda x: x.at):
    account = bandwidth.account()
    device = bandwidth.device()
    gigabytes = round(bandwidth.bytes / 1024**3, 3)
    local = 'local' if bandwidth.lan else 'remote'
    date = bandwidth.at.strftime('%Y-%m-%d')
    print('%s used %s GB of %s bandwidth on %s from %s'
          % (account.name, gigabytes, local, date, device.name))

Partially addresses #356

TODO: Add some tests. I don't know if bandwidth tests will work since the test server won't have any history. Maybe just check that the dashboard returns something using the live seconds interval.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated the docstring for new or existing methods
  • I have added tests when applicable

@Hellowlol
Copy link
Collaborator

Nice job! About the testing, would it be possible to just add some \data in tests/payloads.py so we have something to test against?

@JonnyWong16
Copy link
Collaborator Author

So I didn't need to mock bandwidth, but I had to mock the resources. I don't know why the test server can't read CPU/RAM usage.

Passing tests are here. (Note that I modified the CI to only run the server tests in this example.)
https://github.com/JonnyWong16/python-plexapi/actions/runs/459798422

@Hellowlol Hellowlol merged commit 5049221 into pkkid:master Jan 11, 2021
@JonnyWong16 JonnyWong16 mentioned this pull request Feb 3, 2021
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants