Metrics¶
-
class
postgres_metrics.metrics.
MetricRegistry
[source]¶ Bases:
object
-
register
(metric)[source]¶ Given a class (not class instance) of
Metric
, adds it to the available list of metrics to show it to a user.
-
sorted
¶ All registered metrics ordered by their label.
-
-
class
postgres_metrics.metrics.
Metric
(ordering=None)[source]¶ Bases:
object
The superclass for all Metric implementations. Inherit from this to define your own metric.
If you want to implement your own metric, here’s a gist:
from django.utils.translation import ugettext_lazy as _ from postgres_metrics.metrics import Metric, registry class DjangoMigrationStatistics(Metric): """ Count the number of applied Django migrations per app and sort by descending count and ascending app name. """ label = _('Migration Statistics') slug = 'django-migration-statistics' ordering = '-2.1' sql = ''' SELECT app, count(*) FROM django_migrations GROUP BY app {ORDER_BY} ; ''' registry.register(DjangoMigrationStatistics)
-
description
¶ Don’t define this value directly. Instead define a docstring on the metric class.
The docstring will be processed by Python internals to trim leading white spaces and fix newlines.
'\r\n'
and'\r'
line breaks will be normalized to'\n'
. Two or more consecutive occurances of'\n'
mark a paragraph which will be escaped and wrapped in<p></p>
HTML tags. Further, each paragraph will call into Django’surlize()
method to create<a></a>
HTML tags around links.
-
full_sql
¶ The
sql
formatted withget_order_by_clause()
.
-
get_data
()[source]¶ Iterate over all configured PostgreSQL database and execute the
full_sql
there.Returns: Returns a list of MetricResult
instances.Return type: list
-
get_order_by_clause
()[source]¶ Turn an ordering string like
1.5.-3.-2.6
into the respective SQL.SQL’s column numbering starts at 1, so do we here. Given
self.ordering
as1.5.-3.-2.6
return a stringORDER BY 1 ASC, 5 ASC, 3 DESC, 2 DESC, 6 ASC
.Ensures that each column (excluding the
-
prefix) is an integer by callingint()
on it.
-
get_record_item_style
(record, item, index)[source]¶ Given a single record from
MetricResult
, the value of the current item within, and the current item’s index, decide how to style it. Most likely to be used with the template tagrecord_item_style()
.By default, django-postgres-metrics supports for styling classes:
ok
warning
critical
info
Override this method and return one of the above strings or
None
to apply the given style to the entire record. In the Django Admin this will highlight the entire row.
-
get_record_style
(record)[source]¶ Given a single record from
MetricResult
, decide how to style it. Most likely to be used with the template tagrecord_style()
.By default, django-postgres-metrics supports for styling classes:
ok
warning
critical
info
Override this method and return one of the above strings or
None
to apply the given style to the entire record. In the Django Admin this will highlight the entire row.
-
label
= ''¶ The label is what is used in the Django Admin views. Consider marking this string as translateable.
-
ordering
= ''¶ The default ordering that should be applied to the SQL query by default. This needs to be a valid ordering string as defined on
parsed_ordering
.
-
parsed_ordering
¶ Turn an ordering string like
1.5.-3.-2.6
into the respective abstraction.Given
self.ordering
as1.5.-3.-2.6
return a lis of 2-tuples like[('', 1), ('', 5), ('-', 3), ('-', 2), ('', 6)]
.
-
slug
= ''¶ A URL safe representation of the label and unique across all metrics.
-
sql
= ''¶ The actual SQL statement that is being used to query the database. In order to make use of the
ordering
, include the string{ORDER_BY}
in the query as necessary. For details on that value seeget_order_by_clause()
.
-
-
class
postgres_metrics.metrics.
MetricResult
(connection, records)[source]¶ Bases:
object
Hold a metric’s data for a single database.
-
alias
¶ The alias under which a database connection is known to Django.
-
records
¶ The rows returned by a metric for the given database.
-
-
class
postgres_metrics.metrics.
MetricHeader
(name, index, ordering)[source]¶ Bases:
object
A single column header; mostly takes care of a column’s sorting status.
-
ascending
¶ True
if the column is in ascending orderFalse
otherwise
-
sort_priority
¶ The priority of the columns order. 1 (high), n(low). Default 0.
-
url_primary
¶ Querystring value making this the primary sorting header.
-
url_remove
¶ Querystring value removing this column from sorting.
-
url_toggle
¶ Querystring value toggling ascending/descending for this header.
-
-
class
postgres_metrics.metrics.
AvailableExtensions
[source]¶ PostgreSQL can be extended by installing extensions with the CREATE EXTENSION command. The list of available extensions on each database is shown below.
-
class
postgres_metrics.metrics.
CacheHitsMetric
[source]¶ The typical rule for most applications is that only a fraction of its data is regularly accessed. As with many other things data can tend to follow the 80/20 rule with 20% of your data accounting for 80% of the reads and often times its higher than this. Postgres itself actually tracks access patterns of your data and will on its own keep frequently accessed data in cache. Generally you want your database to have a cache hit rate of about 99%.
(Source: http://www.craigkerstiens.com/2012/10/01/understanding-postgres-performance/)
-
class
postgres_metrics.metrics.
IndexUsageMetric
[source]¶ While there is no perfect answer, if you’re not somewhere around 99% on any table over 10,000 rows you may want to consider adding an index. When examining where to add an index you should look at what kind of queries you’re running. Generally you’ll want to add indexes where you’re looking up by some other id or on values that you’re commonly filtering on such as created_at fields.
(Source: http://www.craigkerstiens.com/2012/10/01/understanding-postgres-performance/)