scrapinghub / spidermon

Scrapy Extension for monitoring spiders execution.
https://spidermon.readthedocs.io
BSD 3-Clause "New" or "Revised" License
528 stars 96 forks source link

Create Base Stat Monitor class to help creating custom monitors #325

Closed rennerocha closed 2 years ago

rennerocha commented 2 years ago

Solves #321

This PR includes a easier way to create custom monitors based on simple comparisons between a job stat and a threshold. We are talking usually about numerical values, but for EQUAL and NOTEQUAL, this can be strings as well.

A monitor that checks if a stat called test_stat is greater than or equal to a value defined in a project setting called TEST_STAT_THRESHOLDcan be defined as follows:

    class TestStatMonitor(BaseStatMonitor):
        stat_name = "test_stat"
        threshold_setting = "TEST_STAT_THRESHOLD"
        assert_type = AssertionType.GTE

The threshold value can also be defined by a method, so we are able to create more complex rules to define what the threshold should be. Some ideas that can be accomplished using that method:

    class TestStatMonitor(BaseStatMonitor):
        stat_name = "test_stat"
        assert_type = AssertionType.GTE

        def get_threshold(self):
            # Do something to get the value
            return threshold

We still need to add the docs for this new feature and rewrite most of the built-in monitors to use this new structure.

codecov[bot] commented 2 years ago

Codecov Report

Merging #325 (dac6ebb) into master (d58090d) will increase coverage by 0.37%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #325      +/-   ##
==========================================
+ Coverage   73.54%   73.92%   +0.37%     
==========================================
  Files          68       68              
  Lines        2967     2991      +24     
  Branches      335      451     +116     
==========================================
+ Hits         2182     2211      +29     
+ Misses        723      718       -5     
  Partials       62       62              
Impacted Files Coverage Δ
spidermon/contrib/scrapy/monitors.py 98.15% <100.00%> (+0.31%) :arrow_up:
spidermon/results/monitor.py 78.51% <100.00%> (+2.47%) :arrow_up:
spidermon/contrib/scrapy/runners.py 88.76% <0.00%> (+2.24%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d58090d...dac6ebb. Read the comment docs.

rennerocha commented 2 years ago

Added the missing docs. This PR is now ready for real review and when approved, to be merged :-)

ogiaquino commented 2 years ago

LGTM