├── .editorconfig ├── .github └── ISSUE_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs ├── Makefile ├── conf.py ├── contributing.rst ├── history.rst ├── index.rst ├── installation.rst ├── make.bat ├── modules.rst ├── readme.rst └── redis_timeseries.rst ├── redis_timeseries.py ├── requirements.txt ├── requirements_dev.txt ├── setup.cfg ├── setup.py ├── tests ├── __init__.py └── test_redis_timeseries.py ├── tox.ini └── travis_pypi_setup.py /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | charset = utf-8 11 | end_of_line = lf 12 | 13 | [*.bat] 14 | indent_style = tab 15 | end_of_line = crlf 16 | 17 | [LICENSE] 18 | insert_final_newline = false 19 | 20 | [Makefile] 21 | indent_style = tab 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | * Redis Timeseries version: 2 | * Python version: 3 | * Operating System: 4 | 5 | ### Description 6 | 7 | Describe what you were trying to get done. 8 | Tell us what happened, what went wrong, and what you expected to happen. 9 | 10 | ### What I Did 11 | 12 | ``` 13 | Paste the command(s) you ran and the output. 14 | If there was a crash, please include the traceback here. 15 | ``` 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | 91 | .idea/ 92 | .DS_Store 93 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # This file was autogenerated and will overwrite each time you run travis_pypi_setup.py 2 | deploy: 3 | true: 4 | condition: $TOXENV == py27 5 | repo: ryananguiano/python-redis-timeseries 6 | tags: true 7 | distributions: sdist bdist_wheel 8 | password: 9 | secure: NQgUZO78S3Ei5aAGyxFihJ1CyHbzsugLUM3IykZtJen8gpGNPYr7LM8a1Z4pTt3SdbHfa5yhjb/XFw1D8znp2EanNaij+otcbPyhGg/bgEYOiVkgkeE6tLEaEGkN5+Acn1eoh/O12UCKrcZqMx/dVg0VnX8H5Z2xofvIPgwcOZZYg99/EpDwEroIGBODDy+eIQuzT+bCZtE3bmf1yPhHXf9xFtQGguFrJN0JMyYW6+DJchmBMZS6/s2fPM7iTmQwybrXqTr+rsh0TSWew3jO1hefe0yTlWOJCJqrjF+VFyOpYFuN/JmhvloeGgGCMgWfOSm+AtX9PMTTRpxjeCUkAQ9kl8lxgaBmWslWvlp5VGe4fIiaZMPGesPnVCxNQ+wa5wMVSuyBDfPNbKIIe12bmR/YRfEyTP+JPGdDEwynCEMUU6dMQyhnOJ4p1Yf7GHbQe/AMtJxad7AYRhJA6wkFCYAQJ6gSvtP5PhGkfrhAlTKuzvdjCF7kQaA1GL0pnksf/XKkdk0gGSNDOaVktLigcWzhqKF98oxdIegqFi7QR3CwgMdgYfZ7LVAJoJ+K2FgMbbzDPQ3qYyS2TCxMjXTnGDbAD5X54WPZ+GenxgmFp11vypw9tZAaKYljvEIg9zFghdVBKMsliFKyLbC328hIDDi1yh3Sg6ru9/ZL7nHUp/4= 10 | provider: pypi 11 | user: ryananguiano 12 | install: pip install -U tox 13 | language: python 14 | matrix: 15 | include: 16 | - python: 3.6 17 | env: 18 | - TOXENV=py36 19 | - python: 3.5 20 | env: 21 | - TOXENV=py35 22 | - python: 3.4 23 | env: 24 | - TOXENV=py34 25 | - python: 2.7 26 | env: 27 | - TOXENV=py27 28 | - python: pypy 29 | env: 30 | - TOXENV=pypy 31 | 32 | script: tox -e ${TOXENV} 33 | services: 34 | - redis-server 35 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Contributing 5 | ============ 6 | 7 | Contributions are welcome, and they are greatly appreciated! Every 8 | little bit helps, and credit will always be given. 9 | 10 | You can contribute in many ways: 11 | 12 | Types of Contributions 13 | ---------------------- 14 | 15 | Report Bugs 16 | ~~~~~~~~~~~ 17 | 18 | Report bugs at https://github.com/ryananguiano/python-redis-timeseries/issues. 19 | 20 | If you are reporting a bug, please include: 21 | 22 | * Your operating system name and version. 23 | * Any details about your local setup that might be helpful in troubleshooting. 24 | * Detailed steps to reproduce the bug. 25 | 26 | Fix Bugs 27 | ~~~~~~~~ 28 | 29 | Look through the GitHub issues for bugs. Anything tagged with "bug" 30 | and "help wanted" is open to whoever wants to implement it. 31 | 32 | Implement Features 33 | ~~~~~~~~~~~~~~~~~~ 34 | 35 | Look through the GitHub issues for features. Anything tagged with "enhancement" 36 | and "help wanted" is open to whoever wants to implement it. 37 | 38 | Write Documentation 39 | ~~~~~~~~~~~~~~~~~~~ 40 | 41 | Redis Timeseries could always use more documentation, whether as part of the 42 | official Redis Timeseries docs, in docstrings, or even on the web in blog posts, 43 | articles, and such. 44 | 45 | Submit Feedback 46 | ~~~~~~~~~~~~~~~ 47 | 48 | The best way to send feedback is to file an issue at https://github.com/ryananguiano/python-redis-timeseries/issues. 49 | 50 | If you are proposing a feature: 51 | 52 | * Explain in detail how it would work. 53 | * Keep the scope as narrow as possible, to make it easier to implement. 54 | * Remember that this is a volunteer-driven project, and that contributions 55 | are welcome :) 56 | 57 | Get Started! 58 | ------------ 59 | 60 | Ready to contribute? Here's how to set up `python-redis-timeseries` for local development. 61 | 62 | 1. Fork the `python-redis-timeseries` repo on GitHub. 63 | 2. Clone your fork locally:: 64 | 65 | $ git clone git@github.com:your_name_here/python-redis-timeseries.git 66 | 67 | 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: 68 | 69 | $ mkvirtualenv python-redis-timeseries 70 | $ cd python-redis-timeseries/ 71 | $ python setup.py develop 72 | 73 | 4. Create a branch for local development:: 74 | 75 | $ git checkout -b name-of-your-bugfix-or-feature 76 | 77 | Now you can make your changes locally. 78 | 79 | 5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:: 80 | 81 | $ flake8 redis_timeseries tests 82 | $ python setup.py test or py.test 83 | $ tox 84 | 85 | To get flake8 and tox, just pip install them into your virtualenv. 86 | 87 | 6. Commit your changes and push your branch to GitHub:: 88 | 89 | $ git add . 90 | $ git commit -m "Your detailed description of your changes." 91 | $ git push origin name-of-your-bugfix-or-feature 92 | 93 | 7. Submit a pull request through the GitHub website. 94 | 95 | Pull Request Guidelines 96 | ----------------------- 97 | 98 | Before you submit a pull request, check that it meets these guidelines: 99 | 100 | 1. The pull request should include tests. 101 | 2. If the pull request adds functionality, the docs should be updated. Put 102 | your new functionality into a function with a docstring, and add the 103 | feature to the list in README.rst. 104 | 3. The pull request should work for Python 2.6, 2.7, 3.4 and 3.5, and for PyPy. Check 105 | https://travis-ci.org/ryananguiano/python-redis-timeseries/pull_requests 106 | and make sure that the tests pass for all supported Python versions. 107 | 108 | Tips 109 | ---- 110 | 111 | To run a subset of tests:: 112 | 113 | $ py.test tests.test_redis_timeseries 114 | 115 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | History 3 | ======= 4 | 5 | 0.1.8 (2017-07-25) 6 | ------------------ 7 | 8 | * Fix bug in _round_time() method 9 | 10 | 0.1.7 (2017-07-25) 11 | ------------------ 12 | 13 | * Fix bug in _round_time() method 14 | 15 | 0.1.6 (2017-07-25) 16 | ------------------ 17 | 18 | * Add timezone so day buckets will start at midnight in the correct timezone 19 | 20 | 0.1.5 (2017-07-18) 21 | ------------------ 22 | 23 | * Update default granularities 24 | 25 | 0.1.4 (2017-07-12) 26 | ------------------ 27 | 28 | * Add float value capabilities 29 | * Add increase() and decrease() methods 30 | * Move get_hits() -> get_buckets() and get_total_hits() -> get_total() 31 | 32 | 0.1.3 (2017-03-30) 33 | ------------------ 34 | 35 | * Remove six package 36 | * Clean up source file 37 | 38 | 0.1.2 (2017-03-30) 39 | ------------------ 40 | 41 | * Make Python 3 compatible 42 | * Fix tox to make PyPy work 43 | 44 | 0.1.1 (2017-03-30) 45 | ------------------ 46 | 47 | * Minor project file updates 48 | 49 | 0.1.0 (2017-03-30) 50 | ------------------ 51 | 52 | * First release on PyPI. 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ryan Anguiano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | 2 | include CONTRIBUTING.rst 3 | include HISTORY.rst 4 | include LICENSE 5 | include README.rst 6 | 7 | recursive-include tests * 8 | recursive-exclude * __pycache__ 9 | recursive-exclude * *.py[co] 10 | 11 | recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean clean-test clean-pyc clean-build docs help 2 | .DEFAULT_GOAL := help 3 | define BROWSER_PYSCRIPT 4 | import os, webbrowser, sys 5 | try: 6 | from urllib import pathname2url 7 | except: 8 | from urllib.request import pathname2url 9 | 10 | webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) 11 | endef 12 | export BROWSER_PYSCRIPT 13 | 14 | define PRINT_HELP_PYSCRIPT 15 | import re, sys 16 | 17 | for line in sys.stdin: 18 | match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) 19 | if match: 20 | target, help = match.groups() 21 | print("%-20s %s" % (target, help)) 22 | endef 23 | export PRINT_HELP_PYSCRIPT 24 | BROWSER := python -c "$$BROWSER_PYSCRIPT" 25 | 26 | help: 27 | @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) 28 | 29 | clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts 30 | 31 | 32 | clean-build: ## remove build artifacts 33 | rm -fr build/ 34 | rm -fr dist/ 35 | rm -fr .eggs/ 36 | find . -name '*.egg-info' -exec rm -fr {} + 37 | find . -name '*.egg' -exec rm -f {} + 38 | 39 | clean-pyc: ## remove Python file artifacts 40 | find . -name '*.pyc' -exec rm -f {} + 41 | find . -name '*.pyo' -exec rm -f {} + 42 | find . -name '*~' -exec rm -f {} + 43 | find . -name '__pycache__' -exec rm -fr {} + 44 | 45 | clean-test: ## remove test and coverage artifacts 46 | rm -fr .tox/ 47 | rm -f .coverage 48 | rm -fr htmlcov/ 49 | 50 | lint: ## check style with flake8 51 | flake8 redis_timeseries tests 52 | 53 | test: ## run tests quickly with the default Python 54 | py.test 55 | 56 | 57 | test-all: ## run tests on every Python version with tox 58 | tox 59 | 60 | coverage: ## check code coverage quickly with the default Python 61 | coverage run --source redis_timeseries -m pytest 62 | 63 | coverage report -m 64 | coverage html 65 | $(BROWSER) htmlcov/index.html 66 | 67 | docs: ## generate Sphinx HTML documentation, including API docs 68 | rm -f docs/redis_timeseries.rst 69 | rm -f docs/modules.rst 70 | sphinx-apidoc -o docs/ . setup.py tests travis_pypi_setup.py 71 | $(MAKE) -C docs clean 72 | $(MAKE) -C docs html 73 | $(BROWSER) docs/_build/html/index.html 74 | 75 | servedocs: docs ## compile the docs watching for changes 76 | watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . 77 | 78 | release: clean ## package and upload a release 79 | python setup.py sdist upload 80 | python setup.py bdist_wheel upload 81 | 82 | dist: clean ## builds source and wheel package 83 | python setup.py sdist 84 | python setup.py bdist_wheel 85 | ls -l dist 86 | 87 | install: clean ## install the package to the active Python's site-packages 88 | python setup.py install 89 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | Redis Timeseries 3 | =============================== 4 | 5 | 6 | Time series API built on top of Redis that can be used to store and query 7 | time series statistics. Multiple time granularities can be used to keep 8 | track of different time intervals. 9 | 10 | 11 | .. image:: https://img.shields.io/pypi/v/redis_timeseries.svg 12 | :target: https://pypi.python.org/pypi/redis_timeseries 13 | 14 | .. image:: https://api.travis-ci.org/ryananguiano/python-redis-timeseries.svg?branch=master 15 | :target: https://travis-ci.org/ryananguiano/python-redis-timeseries 16 | 17 | .. image:: https://readthedocs.org/projects/redis-timeseries/badge/?version=latest 18 | :target: https://redis-timeseries.readthedocs.io/en/latest/?badge=latest 19 | :alt: Documentation Status 20 | 21 | .. image:: https://pyup.io/repos/github/ryananguiano/python-redis-timeseries/shield.svg 22 | :target: https://pyup.io/repos/github/ryananguiano/python-redis-timeseries/ 23 | :alt: Updates 24 | 25 | 26 | * Free software: MIT license 27 | * Documentation: https://redis-timeseries.readthedocs.io. 28 | 29 | Install 30 | ------- 31 | 32 | To install Redis Timeseries, run this command in your terminal: 33 | 34 | .. code-block:: console 35 | 36 | $ pip install redis_timeseries 37 | 38 | 39 | Usage 40 | ----- 41 | 42 | To initialize the TimeSeries class, you must pass a Redis client to 43 | access the database. You may also override the base key for the time series. 44 | 45 | >>> import redis 46 | >>> client = redis.StrictRedis() 47 | >>> ts = TimeSeries(client, base_key='my_timeseries') 48 | 49 | To customize the granularities, make sure each granularity has a ``ttl`` 50 | and ``duration`` in seconds. You can use the helper functions for 51 | easier definitions. 52 | 53 | >>> my_granularities = { 54 | ... '1minute': {'ttl': hours(1), 'duration': minutes(1)}, 55 | ... '1hour': {'ttl': days(7), 'duration': hours(1)} 56 | ... } 57 | >>> ts = TimeSeries(client, granularities=my_granularities) 58 | 59 | ``.record_hit()`` accepts a key and an optional timestamp and increment 60 | count. It will record the data in all defined granularities. 61 | 62 | >>> ts.record_hit('event:123') 63 | >>> ts.record_hit('event:123', datetime(2017, 1, 1, 13, 5)) 64 | >>> ts.record_hit('event:123', count=5) 65 | 66 | ``.record_hit()`` will automatically execute when ``execute=True``. If you 67 | set ``execute=False``, you can chain the commands into a single redis 68 | pipeline. You must then execute the pipeline with ``.execute()``. 69 | 70 | >>> ts.record_hit('event:123', execute=False) 71 | >>> ts.record_hit('enter:123', execute=False) 72 | >>> ts.record_hit('exit:123', execute=False) 73 | >>> ts.execute() 74 | 75 | ``.get_hits()`` will query the database for the latest data in the 76 | selected granularity. If you want to query the last 3 minutes, you 77 | would query the ``1minute`` granularity with a count of 3. This will return 78 | a list of tuples ``(bucket, count)`` where the bucket is the rounded timestamp. 79 | 80 | >>> ts.get_hits('event:123', '1minute', 3) 81 | [(datetime(2017, 1, 1, 13, 5), 1), (datetime(2017, 1, 1, 13, 6), 0), (datetime(2017, 1, 1, 13, 7), 3)] 82 | 83 | ``.get_total_hits()`` will query the database and return only a sum of all 84 | the buckets in the query. 85 | 86 | >>> ts.get_total_hits('event:123', '1minute', 3) 87 | 4 88 | 89 | ``.scan_keys()`` will return a list of keys that could exist in the 90 | selected range. You can pass a search string to limit the keys returned. 91 | The search string should always have a ``*`` to define the wildcard. 92 | 93 | >>> ts.scan_keys('1minute', 10, 'event:*') 94 | ['event:123', 'event:456'] 95 | 96 | 97 | Features 98 | -------- 99 | 100 | * Multiple granularity tracking 101 | * Redis pipeline chaining 102 | * Key scanner 103 | * Easy to integrate with charting packages 104 | * Can choose either integer or float counting 105 | * Date bucketing with timezone support 106 | 107 | Credits 108 | ------- 109 | 110 | Algorithm copied from `tonyskn/node-redis-timeseries`_ 111 | 112 | This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template. 113 | 114 | .. _`tonyskn/node-redis-timeseries`: https://github.com/tonyskn/node-redis-timeseries 115 | .. _Cookiecutter: https://github.com/audreyr/cookiecutter 116 | .. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage 117 | 118 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/redis_timeseries.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/redis_timeseries.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/redis_timeseries" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/redis_timeseries" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # redis_timeseries documentation build configuration file, created by 5 | # sphinx-quickstart on Tue Jul 9 22:26:36 2013. 6 | # 7 | # This file is execfile()d with the current directory set to its 8 | # containing dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | import sys 17 | import os 18 | 19 | # If extensions (or modules to document with autodoc) are in another 20 | # directory, add these directories to sys.path here. If the directory is 21 | # relative to the documentation root, use os.path.abspath to make it 22 | # absolute, like shown here. 23 | #sys.path.insert(0, os.path.abspath('.')) 24 | 25 | # Get the project root dir, which is the parent dir of this 26 | cwd = os.getcwd() 27 | project_root = os.path.dirname(cwd) 28 | 29 | # Insert the project root dir as the first element in the PYTHONPATH. 30 | # This lets us ensure that the source package is imported, and that its 31 | # version is used. 32 | sys.path.insert(0, project_root) 33 | 34 | import redis_timeseries 35 | 36 | # -- General configuration --------------------------------------------- 37 | 38 | # If your documentation needs a minimal Sphinx version, state it here. 39 | #needs_sphinx = '1.0' 40 | 41 | # Add any Sphinx extension module names here, as strings. They can be 42 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 43 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 44 | 45 | # Add any paths that contain templates here, relative to this directory. 46 | templates_path = ['_templates'] 47 | 48 | # The suffix of source filenames. 49 | source_suffix = '.rst' 50 | 51 | # The encoding of source files. 52 | #source_encoding = 'utf-8-sig' 53 | 54 | # The master toctree document. 55 | master_doc = 'index' 56 | 57 | # General information about the project. 58 | project = u'Redis Timeseries' 59 | copyright = u"2017, Ryan Anguiano" 60 | 61 | # The version info for the project you're documenting, acts as replacement 62 | # for |version| and |release|, also used in various other places throughout 63 | # the built documents. 64 | # 65 | # The short X.Y version. 66 | version = redis_timeseries.__version__ 67 | # The full version, including alpha/beta/rc tags. 68 | release = redis_timeseries.__version__ 69 | 70 | # The language for content autogenerated by Sphinx. Refer to documentation 71 | # for a list of supported languages. 72 | #language = None 73 | 74 | # There are two options for replacing |today|: either, you set today to 75 | # some non-false value, then it is used: 76 | #today = '' 77 | # Else, today_fmt is used as the format for a strftime call. 78 | #today_fmt = '%B %d, %Y' 79 | 80 | # List of patterns, relative to source directory, that match files and 81 | # directories to ignore when looking for source files. 82 | exclude_patterns = ['_build'] 83 | 84 | # The reST default role (used for this markup: `text`) to use for all 85 | # documents. 86 | #default_role = None 87 | 88 | # If true, '()' will be appended to :func: etc. cross-reference text. 89 | #add_function_parentheses = True 90 | 91 | # If true, the current module name will be prepended to all description 92 | # unit titles (such as .. function::). 93 | #add_module_names = True 94 | 95 | # If true, sectionauthor and moduleauthor directives will be shown in the 96 | # output. They are ignored by default. 97 | #show_authors = False 98 | 99 | # The name of the Pygments (syntax highlighting) style to use. 100 | pygments_style = 'sphinx' 101 | 102 | # A list of ignored prefixes for module index sorting. 103 | #modindex_common_prefix = [] 104 | 105 | # If true, keep warnings as "system message" paragraphs in the built 106 | # documents. 107 | #keep_warnings = False 108 | 109 | 110 | # -- Options for HTML output ------------------------------------------- 111 | 112 | # The theme to use for HTML and HTML Help pages. See the documentation for 113 | # a list of builtin themes. 114 | html_theme = 'default' 115 | 116 | # Theme options are theme-specific and customize the look and feel of a 117 | # theme further. For a list of options available for each theme, see the 118 | # documentation. 119 | #html_theme_options = {} 120 | 121 | # Add any paths that contain custom themes here, relative to this directory. 122 | #html_theme_path = [] 123 | 124 | # The name for this set of Sphinx documents. If None, it defaults to 125 | # " v documentation". 126 | #html_title = None 127 | 128 | # A shorter title for the navigation bar. Default is the same as 129 | # html_title. 130 | #html_short_title = None 131 | 132 | # The name of an image file (relative to this directory) to place at the 133 | # top of the sidebar. 134 | #html_logo = None 135 | 136 | # The name of an image file (within the static path) to use as favicon 137 | # of the docs. This file should be a Windows icon file (.ico) being 138 | # 16x16 or 32x32 pixels large. 139 | #html_favicon = None 140 | 141 | # Add any paths that contain custom static files (such as style sheets) 142 | # here, relative to this directory. They are copied after the builtin 143 | # static files, so a file named "default.css" will overwrite the builtin 144 | # "default.css". 145 | html_static_path = ['_static'] 146 | 147 | # If not '', a 'Last updated on:' timestamp is inserted at every page 148 | # bottom, using the given strftime format. 149 | #html_last_updated_fmt = '%b %d, %Y' 150 | 151 | # If true, SmartyPants will be used to convert quotes and dashes to 152 | # typographically correct entities. 153 | #html_use_smartypants = True 154 | 155 | # Custom sidebar templates, maps document names to template names. 156 | #html_sidebars = {} 157 | 158 | # Additional templates that should be rendered to pages, maps page names 159 | # to template names. 160 | #html_additional_pages = {} 161 | 162 | # If false, no module index is generated. 163 | #html_domain_indices = True 164 | 165 | # If false, no index is generated. 166 | #html_use_index = True 167 | 168 | # If true, the index is split into individual pages for each letter. 169 | #html_split_index = False 170 | 171 | # If true, links to the reST sources are added to the pages. 172 | #html_show_sourcelink = True 173 | 174 | # If true, "Created using Sphinx" is shown in the HTML footer. 175 | # Default is True. 176 | #html_show_sphinx = True 177 | 178 | # If true, "(C) Copyright ..." is shown in the HTML footer. 179 | # Default is True. 180 | #html_show_copyright = True 181 | 182 | # If true, an OpenSearch description file will be output, and all pages 183 | # will contain a tag referring to it. The value of this option 184 | # must be the base URL from which the finished HTML is served. 185 | #html_use_opensearch = '' 186 | 187 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 188 | #html_file_suffix = None 189 | 190 | # Output file base name for HTML help builder. 191 | htmlhelp_basename = 'redis_timeseriesdoc' 192 | 193 | 194 | # -- Options for LaTeX output ------------------------------------------ 195 | 196 | latex_elements = { 197 | # The paper size ('letterpaper' or 'a4paper'). 198 | #'papersize': 'letterpaper', 199 | 200 | # The font size ('10pt', '11pt' or '12pt'). 201 | #'pointsize': '10pt', 202 | 203 | # Additional stuff for the LaTeX preamble. 204 | #'preamble': '', 205 | } 206 | 207 | # Grouping the document tree into LaTeX files. List of tuples 208 | # (source start file, target name, title, author, documentclass 209 | # [howto/manual]). 210 | latex_documents = [ 211 | ('index', 'redis_timeseries.tex', 212 | u'Redis Timeseries Documentation', 213 | u'Ryan Anguiano', 'manual'), 214 | ] 215 | 216 | # The name of an image file (relative to this directory) to place at 217 | # the top of the title page. 218 | #latex_logo = None 219 | 220 | # For "manual" documents, if this is true, then toplevel headings 221 | # are parts, not chapters. 222 | #latex_use_parts = False 223 | 224 | # If true, show page references after internal links. 225 | #latex_show_pagerefs = False 226 | 227 | # If true, show URL addresses after external links. 228 | #latex_show_urls = False 229 | 230 | # Documents to append as an appendix to all manuals. 231 | #latex_appendices = [] 232 | 233 | # If false, no module index is generated. 234 | #latex_domain_indices = True 235 | 236 | 237 | # -- Options for manual page output ------------------------------------ 238 | 239 | # One entry per manual page. List of tuples 240 | # (source start file, name, description, authors, manual section). 241 | man_pages = [ 242 | ('index', 'redis_timeseries', 243 | u'Redis Timeseries Documentation', 244 | [u'Ryan Anguiano'], 1) 245 | ] 246 | 247 | # If true, show URL addresses after external links. 248 | #man_show_urls = False 249 | 250 | 251 | # -- Options for Texinfo output ---------------------------------------- 252 | 253 | # Grouping the document tree into Texinfo files. List of tuples 254 | # (source start file, target name, title, author, 255 | # dir menu entry, description, category) 256 | texinfo_documents = [ 257 | ('index', 'redis_timeseries', 258 | u'Redis Timeseries Documentation', 259 | u'Ryan Anguiano', 260 | 'redis_timeseries', 261 | 'One line description of project.', 262 | 'Miscellaneous'), 263 | ] 264 | 265 | # Documents to append as an appendix to all manuals. 266 | #texinfo_appendices = [] 267 | 268 | # If false, no module index is generated. 269 | #texinfo_domain_indices = True 270 | 271 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 272 | #texinfo_show_urls = 'footnote' 273 | 274 | # If true, do not generate a @detailmenu in the "Top" node's menu. 275 | #texinfo_no_detailmenu = False 276 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst 2 | -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst 2 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to Redis Timeseries's documentation! 2 | ====================================== 3 | 4 | Contents: 5 | 6 | .. toctree:: 7 | :maxdepth: 2 8 | 9 | readme 10 | installation 11 | contributing 12 | history 13 | 14 | Indices and tables 15 | ================== 16 | 17 | * :ref:`genindex` 18 | * :ref:`modindex` 19 | * :ref:`search` 20 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | 8 | Stable release 9 | -------------- 10 | 11 | To install Redis Timeseries, run this command in your terminal: 12 | 13 | .. code-block:: console 14 | 15 | $ pip install redis_timeseries 16 | 17 | This is the preferred method to install Redis Timeseries, as it will always install the most recent stable release. 18 | 19 | If you don't have `pip`_ installed, this `Python installation guide`_ can guide 20 | you through the process. 21 | 22 | .. _pip: https://pip.pypa.io 23 | .. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ 24 | 25 | 26 | From sources 27 | ------------ 28 | 29 | The sources for Redis Timeseries can be downloaded from the `Github repo`_. 30 | 31 | You can either clone the public repository: 32 | 33 | .. code-block:: console 34 | 35 | $ git clone git://github.com/ryananguiano/python-redis-timeseries 36 | 37 | Or download the `tarball`_: 38 | 39 | .. code-block:: console 40 | 41 | $ curl -OL https://github.com/ryananguiano/python-redis-timeseries/tarball/master 42 | 43 | Once you have a copy of the source, you can install it with: 44 | 45 | .. code-block:: console 46 | 47 | $ python setup.py install 48 | 49 | 50 | .. _Github repo: https://github.com/ryananguiano/python-redis-timeseries 51 | .. _tarball: https://github.com/ryananguiano/python-redis-timeseries/tarball/master 52 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\redis_timeseries.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\redis_timeseries.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /docs/modules.rst: -------------------------------------------------------------------------------- 1 | python-redis-timeseries 2 | ======================= 3 | 4 | .. toctree:: 5 | :maxdepth: 4 6 | 7 | redis_timeseries 8 | -------------------------------------------------------------------------------- /docs/readme.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | -------------------------------------------------------------------------------- /docs/redis_timeseries.rst: -------------------------------------------------------------------------------- 1 | redis_timeseries module 2 | ======================= 3 | 4 | .. automodule:: redis_timeseries 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /redis_timeseries.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = 'Ryan Anguiano' 4 | __email__ = 'ryan.anguiano@gmail.com' 5 | __version__ = '0.1.9' 6 | 7 | 8 | import calendar 9 | import functools 10 | import operator 11 | from collections import OrderedDict 12 | from datetime import datetime 13 | 14 | try: 15 | import pytz 16 | except ImportError: # pragma: no cover 17 | pytz = None 18 | 19 | 20 | __all__ = ['TimeSeries', 'seconds', 'minutes', 'hours', 'days'] 21 | 22 | 23 | seconds = lambda i: i 24 | minutes = lambda i: i * seconds(60) 25 | hours = lambda i: i * minutes(60) 26 | days = lambda i: i * hours(24) 27 | 28 | 29 | class TimeSeries(object): 30 | granularities = OrderedDict([ 31 | ('1minute', {'duration': minutes(1), 'ttl': hours(1)}), 32 | ('5minute', {'duration': minutes(5), 'ttl': hours(6)}), 33 | ('10minute', {'duration': minutes(10), 'ttl': hours(12)}), 34 | ('1hour', {'duration': hours(1), 'ttl': days(7)}), 35 | ('1day', {'duration': days(1), 'ttl': days(31)}), 36 | ]) 37 | 38 | def __init__(self, client, base_key='stats', use_float=False, 39 | timezone=None, granularities=None): 40 | self.client = client 41 | self.base_key = base_key 42 | self.use_float = use_float 43 | self.timezone = timezone 44 | self.granularities = granularities or self.granularities 45 | self.chain = self.client.pipeline() 46 | 47 | def get_key(self, key, timestamp, granularity): 48 | ttl = self.granularities[granularity]['ttl'] 49 | timestamp_key = round_time(timestamp, ttl) # No timezone offset in the key 50 | return ':'.join([self.base_key, granularity, str(timestamp_key), str(key)]) 51 | 52 | def increase(self, key, amount, timestamp=None, execute=True): 53 | pipe = self.client.pipeline() if execute else self.chain 54 | 55 | for granularity, props in self.granularities.items(): 56 | hkey = self.get_key(key, timestamp, granularity) 57 | bucket = round_time_with_tz(timestamp, props['duration'], self.timezone) 58 | 59 | _incr = pipe.hincrbyfloat if self.use_float else pipe.hincrby 60 | _incr(hkey, bucket, amount) 61 | 62 | pipe.expire(hkey, props['ttl']) 63 | 64 | if execute: 65 | pipe.execute() 66 | 67 | def decrease(self, key, amount, timestamp=None, execute=True): 68 | self.increase(key, -1 * amount, timestamp, execute) 69 | 70 | def execute(self): 71 | results = self.chain.execute() 72 | self.chain = self.client.pipeline() 73 | return results 74 | 75 | def get_buckets(self, key, granularity, count, timestamp=None): 76 | props = self.granularities[granularity] 77 | if count > (props['ttl'] / props['duration']): 78 | raise ValueError('Count exceeds granularity limit') 79 | 80 | pipe = self.client.pipeline() 81 | buckets = [] 82 | rounded = round_time_with_tz(timestamp, props['duration'], self.timezone) 83 | bucket = rounded - (count * props['duration']) 84 | 85 | for _ in range(count): 86 | bucket += props['duration'] 87 | buckets.append(unix_to_dt(bucket)) 88 | pipe.hget(self.get_key(key, bucket, granularity), bucket) 89 | 90 | _type = float if self.use_float else int 91 | parse = lambda x: _type(x or 0) 92 | 93 | results = map(parse, pipe.execute()) 94 | 95 | return list(zip(buckets, results)) 96 | 97 | def get_total(self, *args, **kwargs): 98 | return sum([ 99 | amount for bucket, amount in self.get_buckets(*args, **kwargs) 100 | ]) 101 | 102 | def scan_keys(self, granularity, count, search='*', timestamp=None): 103 | props = self.granularities[granularity] 104 | if count > (props['ttl'] / props['duration']): 105 | raise ValueError('Count exceeds granularity limit') 106 | 107 | hkeys = set() 108 | prefixes = set() 109 | rounded = round_time_with_tz(timestamp, props['duration'], self.timezone) 110 | bucket = rounded - (count * props['duration']) 111 | 112 | for _ in range(count): 113 | bucket += props['duration'] 114 | hkeys.add(self.get_key(search, bucket, granularity)) 115 | prefixes.add(self.get_key('', bucket, granularity)) 116 | 117 | pipe = self.client.pipeline() 118 | for key in hkeys: 119 | pipe.keys(key) 120 | results = functools.reduce(operator.add, pipe.execute()) 121 | 122 | parsed = set() 123 | for result in results: 124 | result = result.decode('utf-8') 125 | for prefix in prefixes: 126 | result = result.replace(prefix, '') 127 | parsed.add(result) 128 | 129 | return sorted(parsed) 130 | 131 | def record_hit(self, key, timestamp=None, count=1, execute=True): 132 | self.increase(key, count, timestamp, execute) 133 | 134 | def remove_hit(self, key, timestamp=None, count=1, execute=True): 135 | self.decrease(key, count, timestamp, execute) 136 | 137 | get_hits = get_buckets 138 | get_total_hits = get_total 139 | 140 | 141 | def round_time(dt, precision): 142 | seconds = dt_to_unix(dt or tz_now()) 143 | return int((seconds // precision) * precision) 144 | 145 | 146 | def round_time_with_tz(dt, precision, tz=None): 147 | rounded = round_time(dt, precision) 148 | 149 | if tz and precision % days(1) == 0: 150 | rounded_dt = unix_to_dt(rounded).replace(tzinfo=None) 151 | offset = tz.utcoffset(rounded_dt).total_seconds() 152 | rounded = int(rounded - offset) 153 | 154 | dt = unix_to_dt(dt or tz_now()) 155 | dt_seconds = (hours(dt.hour) + minutes(dt.minute) + seconds(dt.second)) 156 | if offset < 0 and dt_seconds < abs(offset): 157 | rounded -= precision 158 | elif offset > 0 and dt_seconds >= days(1) - offset: 159 | rounded += precision 160 | 161 | return rounded 162 | 163 | 164 | def tz_now(): 165 | if pytz: 166 | return datetime.utcnow().replace(tzinfo=pytz.utc) 167 | else: 168 | return datetime.now() 169 | 170 | 171 | def dt_to_unix(dt): 172 | if isinstance(dt, datetime): 173 | dt = calendar.timegm(dt.utctimetuple()) 174 | return dt 175 | 176 | 177 | def unix_to_dt(dt): 178 | if isinstance(dt, (int, float)): 179 | utc = pytz.utc if pytz else None 180 | try: 181 | dt = datetime.fromtimestamp(dt, utc) 182 | except ValueError: 183 | dt = datetime.fromtimestamp(dt / 1000., utc) 184 | return dt 185 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | redis==3.3.11 2 | -------------------------------------------------------------------------------- /requirements_dev.txt: -------------------------------------------------------------------------------- 1 | bumpversion==0.5.3 2 | coverage==4.5.4 3 | flake8==3.7.9 4 | pytest==5.3.1 5 | pytz==2019.3 6 | Sphinx==2.2.2 7 | tox==3.14.2 8 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 0.1.9 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:setup.py] 7 | search = version='{current_version}' 8 | replace = version='{new_version}' 9 | 10 | [bumpversion:file:redis_timeseries.py] 11 | search = __version__ = '{current_version}' 12 | replace = __version__ = '{new_version}' 13 | 14 | [bdist_wheel] 15 | universal = 1 16 | 17 | [flake8] 18 | exclude = docs 19 | 20 | [coverage:run] 21 | source = redis_timeseries.py 22 | omit = tests/,docs/ 23 | 24 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup 5 | 6 | with open('README.rst') as readme_file: 7 | readme = readme_file.read() 8 | 9 | with open('HISTORY.rst') as history_file: 10 | history = history_file.read() 11 | 12 | requirements = [ 13 | 'redis', 14 | ] 15 | 16 | test_requirements = [ 17 | 'pytest', 18 | 'pytz', 19 | ] 20 | 21 | setup( 22 | name='redis_timeseries', 23 | version='0.1.9', 24 | description="Timeseries API built on top of Redis", 25 | long_description=readme + '\n\n' + history, 26 | author="Ryan Anguiano", 27 | author_email='ryan.anguiano@gmail.com', 28 | url='https://github.com/ryananguiano/python-redis-timeseries', 29 | py_modules=['redis_timeseries'], 30 | install_requires=requirements, 31 | license="MIT license", 32 | zip_safe=False, 33 | keywords='redis_timeseries', 34 | classifiers=[ 35 | 'Development Status :: 4 - Beta', 36 | 'Intended Audience :: Developers', 37 | 'License :: OSI Approved :: MIT License', 38 | 'Natural Language :: English', 39 | "Programming Language :: Python :: 2", 40 | 'Programming Language :: Python :: 2.7', 41 | 'Programming Language :: Python :: 3', 42 | 'Programming Language :: Python :: 3.4', 43 | 'Programming Language :: Python :: 3.5', 44 | 'Programming Language :: Python :: 3.6', 45 | ], 46 | test_suite='tests', 47 | tests_require=test_requirements 48 | ) 49 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /tests/test_redis_timeseries.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | test_redis_timeseries 6 | ---------------------------------- 7 | 8 | Tests for `redis_timeseries` module. 9 | """ 10 | 11 | from collections import OrderedDict 12 | from datetime import datetime, timedelta 13 | import pytest 14 | import pytz 15 | 16 | import redis_timeseries as timeseries 17 | 18 | TEST_GRANULARITIES = OrderedDict([ 19 | ('1m', {'duration': timeseries.minutes(1), 'ttl': timeseries.hours(1)}), 20 | ('1h', {'duration': timeseries.hours(1), 'ttl': timeseries.days(7)}), 21 | ('1d', {'duration': timeseries.days(1), 'ttl': timeseries.days(31)}), 22 | ]) 23 | 24 | eastern = pytz.timezone('US/Eastern') 25 | 26 | 27 | @pytest.fixture 28 | def redis_client(): 29 | import redis 30 | client = redis.StrictRedis(db=9) 31 | client.flushdb() 32 | return client 33 | 34 | 35 | # Run all baseline tests with and without timezone 36 | @pytest.fixture(params=[None, eastern]) 37 | def ts(request, redis_client): 38 | return timeseries.TimeSeries(redis_client, 'tests', timezone=request.param, granularities=TEST_GRANULARITIES) 39 | 40 | 41 | @pytest.fixture 42 | def ts_float(redis_client): 43 | return timeseries.TimeSeries(redis_client, 'float_tests', use_float=True, granularities=TEST_GRANULARITIES) 44 | 45 | 46 | @pytest.fixture 47 | def ts_timezone(redis_client): 48 | return timeseries.TimeSeries(redis_client, 'timezone_tests', timezone=eastern, granularities=TEST_GRANULARITIES) 49 | 50 | 51 | def test_client_connection(ts): 52 | assert ts.client.ping() 53 | 54 | 55 | def test_record_hit_basic(ts): 56 | ts.record_hit('event:123') 57 | assert ts.get_total_hits('event:123', '1m', 1) == 1 58 | 59 | 60 | def test_record_hit_count(ts): 61 | ts.record_hit('event:123', count=5) 62 | assert ts.get_total_hits('event:123', '1m', 1) == 5 63 | 64 | 65 | def test_record_hit_datetime(ts): 66 | now = timeseries.tz_now() 67 | ts.record_hit('event:123', now - timedelta(minutes=1)) 68 | assert ts.get_total_hits('event:123', '1m', 1, now) == 0 69 | assert ts.get_total_hits('event:123', '1m', 2, now) == 1 70 | 71 | 72 | def test_record_hit_chain(ts): 73 | ts.record_hit('event:123', execute=False) 74 | ts.record_hit('enter:123', execute=False) 75 | ts.execute() 76 | assert ts.get_total_hits('event:123', '1m', 1) == 1 77 | assert ts.get_total_hits('enter:123', '1m', 1) == 1 78 | 79 | 80 | def test_get_hits(ts): 81 | now = timeseries.tz_now() 82 | ts.record_hit('event:123', now - timedelta(minutes=4)) 83 | ts.record_hit('event:123', now - timedelta(minutes=2)) 84 | ts.record_hit('event:123', now - timedelta(minutes=1)) 85 | ts.record_hit('event:123') 86 | hits = ts.get_hits('event:123', '1m', 5) 87 | assert len(hits) == 5 88 | assert len(hits[0]) == 2 89 | assert isinstance(hits[0][0], datetime) 90 | assert isinstance(hits[0][1], int) 91 | 92 | first_event = timeseries.unix_to_dt(timeseries.round_time(now - timedelta(minutes=4), timeseries.minutes(1))) 93 | assert hits[0][0] == first_event 94 | 95 | assert hits[0][1] == 1 96 | assert hits[1][1] == 0 97 | assert hits[2][1] == 1 98 | assert hits[3][1] == 1 99 | assert hits[4][1] == 1 100 | 101 | 102 | def test_get_hits_invalid_count(ts): 103 | with pytest.raises(ValueError): 104 | ts.get_hits('event:123', '1m', 100) 105 | 106 | 107 | def test_get_total_hits(ts): 108 | now = timeseries.tz_now() 109 | ts.record_hit('event:123', now - timedelta(minutes=4)) 110 | ts.record_hit('event:123', now - timedelta(minutes=2)) 111 | ts.record_hit('event:123', now - timedelta(minutes=1)) 112 | ts.record_hit('event:123') 113 | assert ts.get_total_hits('event:123', '1m', 5) == 4 114 | 115 | 116 | def test_get_total_hits_no_pytz(ts): 117 | timeseries.pytz, _pytz = None, timeseries.pytz 118 | now = timeseries.tz_now() 119 | ts.record_hit('event:123', now - timedelta(minutes=4)) 120 | ts.record_hit('event:123', now - timedelta(minutes=2)) 121 | ts.record_hit('event:123', now - timedelta(minutes=1)) 122 | ts.record_hit('event:123') 123 | assert ts.get_total_hits('event:123', '1m', 5) == 4 124 | timeseries.pytz = _pytz 125 | 126 | 127 | def test_scan_keys(ts): 128 | ts.record_hit('event:123') 129 | ts.record_hit('event:456') 130 | assert ts.scan_keys('1m', 1) == ['event:123', 'event:456'] 131 | 132 | 133 | def test_scan_keys_invalid_count(ts): 134 | with pytest.raises(ValueError): 135 | ts.scan_keys('1m', 100) 136 | 137 | 138 | def test_scan_keys_search(ts): 139 | ts.record_hit('event:123') 140 | ts.record_hit('event:456') 141 | ts.record_hit('enter:123') 142 | assert ts.scan_keys('1m', 1, 'event:*') == ['event:123', 'event:456'] 143 | 144 | 145 | def test_float_increase(ts_float): 146 | ts = ts_float 147 | ts.increase('account:123', 1.23) 148 | assert ts.get_total_hits('account:123', '1m', 1) == 1.23 149 | 150 | 151 | def test_float_decrease(ts_float): 152 | ts = ts_float 153 | ts.increase('account:123', 5) 154 | ts.decrease('account:123', 2.5) 155 | assert ts.get_total_hits('account:123', '1m', 1) == 2.5 156 | 157 | 158 | test_timezone_round_days = [ 159 | (datetime(2017, 1, 16, 4, 59, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 1, 15, 5, tzinfo=pytz.utc)), 160 | (datetime(2017, 1, 16, 5, 0, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 1, 16, 5, tzinfo=pytz.utc)), 161 | (datetime(2017, 1, 16, 5, 59, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 1, 16, 5, tzinfo=pytz.utc)), 162 | (datetime(2017, 7, 16, 3, 59, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 7, 15, 4, tzinfo=pytz.utc)), 163 | (datetime(2017, 7, 16, 4, 0, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 7, 16, 4, tzinfo=pytz.utc)), 164 | (datetime(2017, 7, 16, 4, 59, tzinfo=pytz.utc), timeseries.days(1), datetime(2017, 7, 16, 4, tzinfo=pytz.utc)), 165 | (datetime(2017, 6, 1, 10, tzinfo=pytz.utc), timeseries.hours(6), datetime(2017, 6, 1, 6, tzinfo=pytz.utc)), 166 | (datetime(2017, 8, 4, 12, 58, tzinfo=pytz.utc), timeseries.hours(1), datetime(2017, 8, 4, 12, tzinfo=pytz.utc)), 167 | (datetime(2017, 11, 23, 18, 59, 59, tzinfo=pytz.utc), timeseries.minutes(1), datetime(2017, 11, 23, 18, 59, tzinfo=pytz.utc)), 168 | ] 169 | 170 | 171 | @pytest.mark.parametrize('dt, precision, expected', test_timezone_round_days) 172 | def test_round_time_with_tz(dt, precision, expected): 173 | tz_rounded = timeseries.round_time_with_tz(dt, precision, eastern) 174 | assert timeseries.unix_to_dt(tz_rounded) == expected 175 | 176 | 177 | def test_get_total_hits_days(ts_timezone): 178 | ts = ts_timezone 179 | ts.record_hit('event:123', datetime(2017, 7, 12, 4, tzinfo=pytz.utc)) 180 | ts.record_hit('event:123', datetime(2017, 7, 13, 4, tzinfo=pytz.utc)) 181 | ts.record_hit('event:123', datetime(2017, 7, 15, 3, tzinfo=pytz.utc)) 182 | ts.record_hit('event:123', datetime(2017, 7, 15, 4, tzinfo=pytz.utc)) 183 | ts.record_hit('event:123', datetime(2017, 7, 15, 5, tzinfo=pytz.utc)) 184 | ts.record_hit('event:123', datetime(2017, 7, 16, 3, tzinfo=pytz.utc)) 185 | ts.record_hit('event:123', datetime(2017, 7, 16, 4, tzinfo=pytz.utc)) 186 | ts.record_hit('event:123', datetime(2017, 7, 16, 5, tzinfo=pytz.utc)) 187 | 188 | buckets = ts.get_buckets('event:123', '1d', 5, timestamp=datetime(2017, 7, 17, 0, tzinfo=pytz.utc)) 189 | 190 | assert len(buckets) == 5 191 | assert buckets[0][0] == datetime(2017, 7, 12, 4, tzinfo=pytz.utc) 192 | assert buckets[1][0] == datetime(2017, 7, 13, 4, tzinfo=pytz.utc) 193 | assert buckets[2][0] == datetime(2017, 7, 14, 4, tzinfo=pytz.utc) 194 | assert buckets[3][0] == datetime(2017, 7, 15, 4, tzinfo=pytz.utc) 195 | assert buckets[4][0] == datetime(2017, 7, 16, 4, tzinfo=pytz.utc) 196 | 197 | assert buckets[0][1] == 1 198 | assert buckets[1][1] == 1 199 | assert buckets[2][1] == 1 200 | assert buckets[3][1] == 3 201 | assert buckets[4][1] == 2 202 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py27, py34, py35, py36, flake8 3 | 4 | [testenv:flake8] 5 | basepython=python 6 | deps=flake8 7 | commands=flake8 redis_timeseries 8 | 9 | [testenv] 10 | setenv = 11 | PYTHONPATH = {toxinidir} 12 | deps = 13 | pytest 14 | pytz 15 | ; -r{toxinidir}/requirements_dev.txt 16 | commands = 17 | pip install -U pip 18 | py.test --basetemp={envtmpdir} 19 | -------------------------------------------------------------------------------- /travis_pypi_setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """Update encrypted deploy password in Travis config file 4 | """ 5 | 6 | 7 | from __future__ import print_function 8 | import base64 9 | import json 10 | import os 11 | from getpass import getpass 12 | import yaml 13 | from cryptography.hazmat.primitives.serialization import load_pem_public_key 14 | from cryptography.hazmat.backends import default_backend 15 | from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 16 | 17 | 18 | try: 19 | from urllib import urlopen 20 | except: 21 | from urllib.request import urlopen 22 | 23 | 24 | GITHUB_REPO = 'ryananguiano/python-redis-timeseries' 25 | TRAVIS_CONFIG_FILE = os.path.join( 26 | os.path.dirname(os.path.abspath(__file__)), '.travis.yml') 27 | 28 | 29 | def load_key(pubkey): 30 | """Load public RSA key, with work-around for keys using 31 | incorrect header/footer format. 32 | 33 | Read more about RSA encryption with cryptography: 34 | https://cryptography.io/latest/hazmat/primitives/asymmetric/rsa/ 35 | """ 36 | try: 37 | return load_pem_public_key(pubkey.encode(), default_backend()) 38 | except ValueError: 39 | # workaround for https://github.com/travis-ci/travis-api/issues/196 40 | pubkey = pubkey.replace('BEGIN RSA', 'BEGIN').replace('END RSA', 'END') 41 | return load_pem_public_key(pubkey.encode(), default_backend()) 42 | 43 | 44 | def encrypt(pubkey, password): 45 | """Encrypt password using given RSA public key and encode it with base64. 46 | 47 | The encrypted password can only be decrypted by someone with the 48 | private key (in this case, only Travis). 49 | """ 50 | key = load_key(pubkey) 51 | encrypted_password = key.encrypt(password, PKCS1v15()) 52 | return base64.b64encode(encrypted_password) 53 | 54 | 55 | def fetch_public_key(repo): 56 | """Download RSA public key Travis will use for this repo. 57 | 58 | Travis API docs: http://docs.travis-ci.com/api/#repository-keys 59 | """ 60 | keyurl = 'https://api.travis-ci.org/repos/{0}/key'.format(repo) 61 | data = json.loads(urlopen(keyurl).read().decode()) 62 | if 'key' not in data: 63 | errmsg = "Could not find public key for repo: {}.\n".format(repo) 64 | errmsg += "Have you already added your GitHub repo to Travis?" 65 | raise ValueError(errmsg) 66 | return data['key'] 67 | 68 | 69 | def prepend_line(filepath, line): 70 | """Rewrite a file adding a line to its beginning. 71 | """ 72 | with open(filepath) as f: 73 | lines = f.readlines() 74 | 75 | lines.insert(0, line) 76 | 77 | with open(filepath, 'w') as f: 78 | f.writelines(lines) 79 | 80 | 81 | def load_yaml_config(filepath): 82 | with open(filepath) as f: 83 | return yaml.load(f) 84 | 85 | 86 | def save_yaml_config(filepath, config): 87 | with open(filepath, 'w') as f: 88 | yaml.dump(config, f, default_flow_style=False) 89 | 90 | 91 | def update_travis_deploy_password(encrypted_password): 92 | """Update the deploy section of the .travis.yml file 93 | to use the given encrypted password. 94 | """ 95 | config = load_yaml_config(TRAVIS_CONFIG_FILE) 96 | 97 | config['deploy']['password'] = dict(secure=encrypted_password) 98 | 99 | save_yaml_config(TRAVIS_CONFIG_FILE, config) 100 | 101 | line = ('# This file was autogenerated and will overwrite' 102 | ' each time you run travis_pypi_setup.py\n') 103 | prepend_line(TRAVIS_CONFIG_FILE, line) 104 | 105 | 106 | def main(args): 107 | public_key = fetch_public_key(args.repo) 108 | password = args.password or getpass('PyPI password: ') 109 | update_travis_deploy_password(encrypt(public_key, password.encode())) 110 | print("Wrote encrypted password to .travis.yml -- you're ready to deploy") 111 | 112 | 113 | if '__main__' == __name__: 114 | import argparse 115 | parser = argparse.ArgumentParser(description=__doc__) 116 | parser.add_argument('--repo', default=GITHUB_REPO, 117 | help='GitHub repo (default: %s)' % GITHUB_REPO) 118 | parser.add_argument('--password', 119 | help='PyPI password (will prompt if not provided)') 120 | 121 | args = parser.parse_args() 122 | main(args) 123 | --------------------------------------------------------------------------------