Giter Club home page Giter Club logo

pubg_reporting's Introduction

PUBG Reporting

Database structure and sync scripts to build a reporting database fetching data from the PUBG API. This will allow you to use tools like Metabase to build custom dashboards for your team. Make pretty graphs, win nerd cred.

Status:

Everything should be basically useable. Installation is relatively simple. I want to flesh this readme out with some documentation about the tables and available views, as well as create some more useful views to begin with, but go ahead and use it now if you want to get started.

Usage:

Usage: sync.py [OPTIONS]

  Program to sync data from the Player Unknown Battlegrounds API into a
  MySQL database, for analysis and pretty nerd graphs.

Options:
  --log-level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
                                  Level of detail to include in logs
  --echo                          Echo SQL Alchemy output to stdout
  --help                          Show this message and exit.

Installation:

For Linux

Follow these mandatory pre-requisite steps:

  1. Make sure Python3 is installed.
  2. Make sure MySQL is installed
  3. Create a db on your MySQL server for the sync scripts to write data into.
  4. Create a MySQL user for the scripts to use. You can use an existing one obviously, but I'd suggest a dedicated user; never a bad idea to separate privileges. Once you've created the user in the mysql shell run grant all on $(db_name).* to '$(db_user)'@'localhost'; and then flush privileges;. Replace $(db_name) and $(db_user) with the appropriate values that you've chosen
  5. Create the following environment variables:
PUBG_API_KEY - holding your API key
PUBGDB_HOST  - holding the address to your MySQL server (probably localhost)
PUBGDB_DATABASE  - holding the name of the db you want to use on the MySQL server
PUBGDB_USERNAME  - holding the username of the MySQL user to connect to the DB
PUBGDB_PASSWORD  - holding the password for the above MySQL user
PUBGDB_CONFIG_PATH  - holding a path to the config.json (NOT including the file name, so for example C:\Users\dscally\Documents\dev\pubg_reporting\) defining which players you want to sync plus your gaming shard, in this format:
{

  "players":[
    "Player1",
    "Player2",
    ...,
  ],
  "shard":"xbox-eu"
}
  1. Run these commands to get the files and move into the repo's directory, then build the database structure:
$ git clone https://github.com/djrscally/pubg_reporting
$ cd pubg_reporting
$ python3 -m venv venv
$ source ./venv/bin/activate
$ (venv) pip3 install -r requirements.txt
$ (venv) alembic upgrade head

The script will build the database for you. At this point all the infrastructure is set up; you can run python sync.py to pull in data for the players you defined in the config.json file.

Once the script completes, you should be good to go; you can hook in whichever analysis tool takes your fancy. I like Metabase.

Upgrading to newer versions of the code

I recommend you stick to master branch, or some weird and wonderful things are likely to happen. If you pull from master to get new code, make sure you run alembic's upgrade function in case some changes were made to the underlying database; that will handle the changes to the db for you. You should definitely take a backup first though, just in case:

$ mysqldump --databases your-database-name | gzip /path/to/backup/folder/your-database-name.sql.gz
$ git pull
$ source ./venv/bin/activate
$ (venv) alembic upgrade head

And that should bring both code and database into line with the master branch. If you got the code before I was using alembic then you first need to tell alembic to register the DB against the intial revision; run alembic stamp 5d9f2ca7b98b before attempting alembic upgrade head and all will be well.

Automating things.

Obviously you can run the sync manually, but that's annoying. To automate things I recommend a cron job. As a precursor step, you'll need to create a file that sets the environment variables you've manually set above; these will need to be made available to the process before it's called, otherwise it won't know how to login to MySQL. To do that, do something like the following:

mkdir ~/.secrets
touch ~/.secrets/pubg
chmod 700 ~/.secrets

This creates a directory called .secrets in your home folder to which only you have access, and additionally creates a file called pubg - use whatever text editor you prefer to fill that file with the commands needed to fill the environment variables, for example

export PUBGDB_HOST="localhost"
export PUBGDB_DATABASE="pubg"
export PUBGDB_USERNAME="pubg"
export PUBGDB_PASSWORD="pubg"
export PUBG_API_KEY="<< API Key Goes Here >>"
export PUBGDB_CONFIG_PATH="/path/to/config/file/"

You can then "source" that file before running python sync.py and it'll work fine. Run crontab -e and simply create an entry to periodically run the sync, for example:

0 0,8,16 0 0 0 . /home/user/.secrets/pubg && /home/user/pubg_reporting/venv/bin/python3 /home/user/pubg_reporting/sync.py OPTIONS

Would run the sync 3 times per day, at midnight, 8 am and 4pm. Replace OPTIONS with anything you want to set, like --log-level DEBUG to get verbose output to sync.log.

For Windows

Everything will work fine, but you don't have CRON obviously. Use Task Scheduler instead, and for setting the envvars have Task Scheduler run a .ps1 file in this form:

$env:PUBGDB_HOST="localhost"
$env:PUBGDB_DATABASE="pubg"
$env:PUBGDB_USERNAME="pubg"
$env:PUBGDB_PASSWORD="pubg"
$env:PUBG_API_KEY="<< API Key Goes Here >>"
$env:PUBGDB_CONFIG_PATH="/path/to/config/file/"

Otherwise, should work identically.

pubg_reporting's People

Contributors

danscally avatar djrscally avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bishnabob

pubg_reporting's Issues

Error in player_season_stats

Hey there,

all data seems to be loaded correctily but the player season stats.
I get the following error:

2020-02-26 20:29:58,090 INFO sqlalchemy.engine.base.Engine {'player_id_1': 'account.712916eb68b24d6691437221cf8b7529', 'season_id_1': 'division.bro.official.pc-2018-04'}
2020-02-26 20:29:58,091 INFO sqlalchemy.engine.base.Engine SELECT EXISTS (SELECT 1
FROM player_season_stats
WHERE player_season_stats.player_id = %(player_id_1)s AND player_season_stats.season_id = %(season_id_1)s) AS anon_1
2020-02-26 20:29:58,091 INFO sqlalchemy.engine.base.Engine {'player_id_1': 'account.712916eb68b24d6691437221cf8b7529', 'season_id_1': 'division.bro.official.pc-2018-05'}
Traceback (most recent call last):
  File "sync.py", line 140, in <module>
    sync()
  File "/usr/lib/python3/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "sync.py", line 53, in sync
    __sync(api, pubgdb)
  File "sync.py", line 124, in __sync
    api.get_player_season_stats(combo)
  File "/pubg/pubg_reporting/pubg/pubg_api.py", line 154, in get_player_season_stats
    logging.debug("get_player_season_stats: {0}: {1}: {2}".format(combo[0], combo[1], json.dumps(r.json()['data']['attributes']['gameModeStats'], indent=4)))
KeyError: 'data'

Thanks for your work and support!

Add player that still had games with others

Hey there, its me again ;)

Szenario:
I added player A and run the sync. Everything works perfect.
Then i added player B who had a match with player A yesterday. Sync works, but:

Issue:
The stats of the match player A and B played together is not showing up in player_match_stats for player B.

Indexing

Basic covering indexes are probably enough. Provide an initial level of indexing to help future queries and anyone not savvy about mysql but using a visualisation layer like metabase.

Season Stats don't refresh

Hi there,

by running data the table player_season_stats will not be updated until i change Last Sync Datetime to a date in the past.

Cross Platform Tournaments

I am currently using your process to create analysis that supports my PUBG tournaments. Cross platform custom matches are now a thing and we had our first tournament on the 18th that included both xbox and PS players.

The current setup only pulls data for Xbox players so none of my PS players are appearing within the analysis. Would be great to develop on the existing setup to pull data from both shards.

Kind regards
MajorClagNut

optimise get_player_ranked_season_stats

I just matched it to get_player_season_stats; I.E. it syncs for a player where they've played since the last sync. But actually, only certain matchType (Only one actually; "competitive") counts towards changing the ranked season stats, so we should only sync those data for players who've played a competitive match since the last sync

new installation on windows doesn't work

Enviornment variables are set, software installed locally. Recieve this error

PS D:\pubg_reporting> & D:/Python/python.exe d:/pubg_reporting/sync.py D:\Python\lib\site-packages\sqlalchemy\dialects\mysql\base.py:1308: SAWarning: Additional column names not matching any column keys in table 'players': 'status' util.warn( D:\Python\lib\site-packages\pymysql\cursors.py:170: Warning: (1287, "'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and replace VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col instead") result = self._query(query) Traceback (most recent call last): File "d:/pubg_reporting/sync.py", line 219, in <module> sync() File "D:\Python\lib\site-packages\click\core.py", line 829, in __call__ return self.main(*args, **kwargs) File "D:\Python\lib\site-packages\click\core.py", line 782, in main rv = self.invoke(ctx) File "D:\Python\lib\site-packages\click\core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "D:\Python\lib\site-packages\click\core.py", line 610, in invoke return callback(*args, **kwargs) File "d:/pubg_reporting/sync.py", line 59, in sync __sync(api, pubgdb) File "d:/pubg_reporting/sync.py", line 187, in __sync api.get_player_season_stats(combo) File "d:\pubg_reporting\pubg\pubg_api.py", line 200, in get_player_season_stats logging.debug("get_player_season_stats: {0}: {1}: {2}".format(combo[0], combo[1], json.dumps(r.json()['data']['attributes']['gameModeStats'], indent=4))) KeyError: 'data' PS D:\pubg_reporting>

it looks like it created some tables, but was unable to finish the job

get_season() now 429's if you call it too often

PUBG seem to have updated the API to throw a 429 error if you call the seasons endpoint too often. TBF their docs do say not to call more often than once a month but they never seemed to enforce it before so i ignored it. That's changed, so the code needs to too.

Players Removed From Custom List Still In data Pull

I currently update a MySQL table with 'Active' players, this is then used in the python query to pull the stats data. At present if I remove a player from the list they are not removed from the data pull.

Teams/clans

Ability to link players together as teams or clans.

Table required for 'Teams', then table required for linking players to the teams.

Multiple shards

Code doesn't support multiple shards.

Config file needs to support shards in a comma separated list.

New players not appearing on leader-boards

I currently pull out match and season data into metabase leader-boards. Yesterday I added a few new players to my included players table and updated the names of a few existing ones. These new players don't seem to be appearing in my leader-boards (season stats based), the updated name ones are.

I also have a leader-board pulling from match data for specific matches, the new and updated name players are not showing on there.

Adding Players by database

It would be great to add players by insert just the player data (or just the name) to table players.

Background: I would like to build a REST API for getting stats. But adding players won't work with config.json.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.