API¶
This part of the documentation covers all the interfaces of Flask-Sustainable.
Interface¶
BaseHeader¶
- class flask_sustainable.base.BaseHeader¶
Bases:
objectBase class for all indicators and scores.
This class is an abstract class. It must be inherited by all classes that are indicators or scores.
- abstract after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- abstract property name¶
Name of the header.
The name of the header that the client will ask through “Perf-“. This name will also be in the response of the request
The name must be a valid HTTP header name and must not contain any whitespace. Plus, it must be unique and don’t interfere with other headers. Finally, it must start with “Perf-”
This header will be used in theses scenarios:
(server side) In a OPTIONS response to indicate the available headers
- (client side) In a request through the “Perf” header to indicate
the headers expected by the client.
- (server side) In the response to indicate through the name header the value
of the header.
- Returns
Name of the header
- Return type
Compression¶
- class flask_sustainable.compress.Compression(response, accept_encodings=None)¶
Bases:
objectCompress the response data when it’s possible.
This class is a wrapper around the flask.Response object. It adds the Content-Encoding header to the response. The response passed through this class is not modified.
To do this, it checks the Accept-Encoding header of the request. The best compression algorithm is chosen based on the Accept-Encoding header. etc.
response = flask.Response("Welcome!") response = Compression(response, "deflate, gzip;q=1.0, *;q=0.5").compress() response.headers["Content-Encoding"] "gzip"
Initialize the Compression object.
If the
accept_encodingsis not given, it will be taken from the request If it’s given, we’ll use werkzeug.http.parse_accept_header to parse it- Parameters
response (flask.Response) – The response object
accept_encodings (str) – The Accept-Encoding header of the request (optional)
- compress(check=False)¶
Compress the response data with the highest compression level available.
The best compression algorithm is chosen based on the Accept-Encoding header.
Example:
response = flask.Response("Welcome!") response = Compression(response, "deflate, gzip;q=1.0, *;q=0.5").compress() response.content_encoding 'gzip'
- Parameters
check (bool) – If True, check if the compression is supported
- Returns
The response object
- Return type
- static compress_data(algorithm, data, level=None)¶
Compress the data with the given algorithm.
This function raises an KeyError exception if the algorithm is not supported.
data = b"Welcome!" Compression.compress_data("gzip", data) b'\x1f\x8b\x08\x004\x0c\xe4b\x02\xff\x0bO\xcdI\xce\xcfMU\x04\x00\xd2\xb4B5\x08\x00\x00\x00'
- make_response(algorithm, check=True)¶
Make a response with the given algorithm.
This function change the reponse data and adds the Content-Encoding header.
Example:
response = flask.Response("Welcome!") response.data b'Welcome!' response = Compression(response).make_response("gzip") response.data b'\x1f\x8b\x08\x004\x0c\xe4b\x02\xff\x0bO\xcdI\xce\xcfMU\x04\x00\xd2\xb4B5\x08\x00\x00\x00' response.headers["Content-Encoding"] 'gzip'
- Parameters
- Returns
The response object
- Return type
Indicator¶
BaseIndicator¶
- class flask_sustainable.base.BaseIndicator¶
Bases:
BaseHeaderBase class for all indicators.
- abstract after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- abstract before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
- abstract property name¶
Name of the header.
The name of the header that the client will ask through “Perf-“. This name will also be in the response of the request
The name must be a valid HTTP header name and must not contain any whitespace. Plus, it must be unique and don’t interfere with other headers. Finally, it must start with “Perf-”
This header will be used in theses scenarios:
(server side) In a OPTIONS response to indicate the available headers
- (client side) In a request through the “Perf” header to indicate
the headers expected by the client.
- (server side) In the response to indicate through the name header the value
of the header.
- Returns
Name of the header
- Return type
Implementation of BaseIndicator¶
This module represents examples of indicator implementation.
For more information about an indicator, see BaseIndicator
class.
- class flask_sustainable.indicator.PerfCPU¶
Bases:
BaseIndicatorIndicator that measure the CPU time of the request.
When the request is done, the response will contain a header named “Perf-CPU” with the CPU time of the request in milliseconds.
The CPU time is the time spent by the process that is different from the execution time.
Example
from flask_sustainable import Sustainable from flask_sustainable.indicator import PerfCPU app = flask.Flask(__name__) sustainable = Sustainable(app) sustainable.add_indicator(PerfCPU())
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
- class flask_sustainable.indicator.PerfEnergy¶
Bases:
BaseIndicatorIndicator that measure the energy usage of the request.
When the request is done, the response will contain a header named “Perf-Energy” with the energy usage of the request in watt-seconds.
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
- class flask_sustainable.indicator.PerfPower¶
Bases:
BaseIndicatorIndicator that measure the power usage of the request.
When the request is done, the response will contain a header named “Perf-Power” with the power usage of the request in watt.
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
- class flask_sustainable.indicator.PerfRAM¶
Bases:
BaseIndicatorIndicator that measure the RAM usage of the request.
When the request is done, the response will contain a header named “Perf-RAM” with the RAM usage of the request in megabytes.
Example
from flask_sustainable import Sustainable from flask_sustainable.indicator import PerfRAM app = flask.Flask(__name__) sustainable = Sustainable(app) sustainable.add_indicator(PerfRAM())
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
- class flask_sustainable.indicator.PerfTime¶
Bases:
BaseIndicatorIndicator that measure the time of the request.
When the request is done, the response will contain a header named “Perf-Time” with the time of the request in milliseconds.
Example
from flask_sustainable import Sustainable from flask_sustainable.indicator import PerfTime app = flask.Flask(__name__) sustainable = Sustainable(app) sustainable.add_indicator(PerfTime())
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- before_request()¶
Method called before the request.
This method will be called before each request. It can be used to do some initialization.
A good example is to use the flask.g object to store some data. This data will be available in the after_request method.
This method is called in the same way as the
flask.before_request()method of Flask.- Returns
None
- Return type
None
Score¶
BaseScore¶
- class flask_sustainable.base.BaseScore¶
Bases:
BaseHeaderBase class for all scores.
A score is executed after all indicators.
The name of the score must follow Perf-ScoreX where
Xis a int > 0.The result of a score is called a score. A score don’t have a
before_request()because his execution comes at last.Indeed, because all indicators are already used, we can call them to get the score. It’s possible that a score is not requested by the user, it will not be available at the score level.
It’s the responsibility of the implementation to verify whether the indicator is present in the headers. If an indicator is required and is not present, the function after_request must return none and not raise an exception. It would not be appropriate to add a header if the calculation is not possible.
Example:
class Example(BaseScore): name: str = "Perf-Score1" def after_request(self, response): indicator = response.headers.get("Perf-Example") if not indicator: return return indicator ** 2
- abstract after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type
- abstract property name¶
Name of the header.
The name of the header that the client will ask through “Perf-“. This name will also be in the response of the request
The name must be a valid HTTP header name and must not contain any whitespace. Plus, it must be unique and don’t interfere with other headers. Finally, it must start with “Perf-”
This header will be used in theses scenarios:
(server side) In a OPTIONS response to indicate the available headers
- (client side) In a request through the “Perf” header to indicate
the headers expected by the client.
- (server side) In the response to indicate through the name header the value
of the header.
- Returns
Name of the header
- Return type
Implementation of BaseScore¶
This module represents examples of score implementation.
For more information about what a score, see BaseScore class.
- class flask_sustainable.score.PerfScoreCO2¶
Bases:
BaseScoreScore that measure the CO2 emissions of the request.
When the request is done, the response will contain a header named “Perf-Score-1” with an equivalent of CO2 emissions of the request in kilograms.
The CO2 emissions are the emissions of the process that is different from the execution time.
Example
from flask_sustainable import Sustainable from flask_sustainable.score import PerfScoreCO2 app = flask.Flask(__name__) sustainable = Sustainable(app) sustainable.add_score(PerfScoreCO2())
- after_request(response)¶
Method called after the request.
This method will be called after each request. It can use the
flask.gobject to get the data stored in the before_request. It can also use the response object to modify the response.This method is called in the same way as the after_request method of Flask.
- Parameters
response (flask.Response) – The initial response object
- Returns
The response object (modified or not)
- Return type