Git Product home page Git Product logo

scorekeeper's Introduction

Scorekeeper

Documentation Status Updates

Keep score of the results of functions over time and take action based on a generated score.

Sometimes, you have a problem that requires you to look at a set of circumstances over time and weigh them before taking action like intermittent failures in a system or calls to external APIs that may or may not respond.

For example, maybe you have an API endpoint you're monitoring and you want to do some self-healing, but only if there are at least 5 failures in 5 minutes. You don't want to have a long-running process keeping state over 5 minutes, so you need something that will maintain state between calls. That might look something like this:

@score(ShadyAPIScorekeeper, 20, threshold=100, decay=10, callback=restart_shady_api)
def check_shady_api():
    if shady_api_test().successful:
        return True
    else:
        return False

Every time check_shady_api() is called and returns False, the ShadyAPIScorekeeper will accumulate 20 points. Once the threshold of 100 points is reached, the callback will be executed. Because you might execute this script from a cron job or in some other intermittent fashion, the accumulated score for ShadyAPIScorekeeper is persisted on the local file system. Finally, the (optional) decay parameter lets you set the number of seconds it takes for the accumulated score to tick down by 1 point.

In this example, ShadyAPIScorekeeper is just a subclass of Scorekeeper and can have its own defaults set for things like threshold, decay, and callback. This allows the decorator invocation to be much leaner. In addition, all instances of a given subclass of Scorekeeper use the same persisted score, so you could decorate multiple functions all with different points based on their severity in order to generate your final score and decide whether or not to take action.

In addition to using Scorekeeper as a decorator, you can also just directly interface with the objects you've described. For example:

>>> score_keeper = ShadyApiScorekeeper()
>>> score_keeper.score
0
>>> score_keeper(10)
>>> score_keeper.score
10
>>> score_keeper(100)
Shady API restarted!

Features

  • TODO

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

scorekeeper's People

Contributors

pyup-bot avatar

Watchers

James Cloos avatar

scorekeeper's Issues

Add ability to namespace Scorekeeper objects

Currently, a score is stored persistently using the Scorekeeper subclass's name, but this could lead to a collision if two different projects were sharing the same environment and had Scorekeepers with the same class name.

An example:

Project A

class MyScoreKeeper(Scorekeeper):
     namespace = project_a

Project B

class MyScoreKeeper(Scorekeeper):
    namespace = project_b

Add simple cli for viewing/cleaning Scorekeepers

It'd be beneficial to be able to interact with Scorekeeper persistence through a simple cli with the following functionality:

  1. Show all stored scores and times
  2. She an individual score and time
  3. Clear all scores and times
  4. Clear an individual score and time

Allow score accumulation to be weighted by time

Currently, decay is completely time based whereas score accumulation is based on how frequently the decorated function is executed.

For instance, let's say we have the following:

@score(MyScorekeeper, 50, decay=5)
def my_func():
    pass

I can be assured that no matter how often I run my_func() in a given time, my decay will remain constant at 1 point every 5 seconds. However, the amount of score I can potentially accumulate during that same period depends entirely on how often I execute my_func().

So we have an issue where one factor is determined wholly by an outside influence (a cron job, for example) and the other is determined wholly by an inside influence (the decay).

Providing a way to weigh score accumulation by time will make using scorekeepers easier to reason about as well as mitigating a class of bug that could result from operations outside the package's control.

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.