Skip to content

Commit

Permalink
Merge pull request #100 from MarkLark86/SDESK-3981
Browse files Browse the repository at this point in the history
[SDESK-3981] Provide signals for generating custom stats
  • Loading branch information
Mayur Dhamanwala authored May 2, 2019
2 parents 11ca768 + 4389a0b commit 334107b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
4 changes: 2 additions & 2 deletions client/email_report/directives/EmailReportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export function EmailReportModal(emailReport, _, gettext) {
scope.mimeTypes = [
{type: 'image/jpeg', label: gettext('JPEG Image')},
{type: 'image/png', label: gettext('PNG Image')},
{type: 'image/svg+xml', label: gettext('SVG Image')},
{type: 'text/csv', label: gettext('CSV File')},
// {type: 'image/svg+xml', label: gettext('SVG Image')},
// {type: 'text/csv', label: gettext('CSV File')},
{type: 'application/pdf', label: gettext('PDF File')},
];

Expand Down
6 changes: 6 additions & 0 deletions server/analytics/stats/archive_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class ArchiveStatisticsResource(Resource):
'type': 'integer',
'default': 0
},

# Dictionary for statistics generated via plugins (i.e. from gen_stats_signals)
'extra': {
'type': 'dict',
'mapping': not_enabled
}
}


Expand Down
7 changes: 7 additions & 0 deletions server/analytics/stats/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,10 @@
OPERATION.REMOVE_FEATUREMEDIA: 'Remove Featuremedia',
OPERATION.UPDATE_FEATUREMEDIA_IMAGE: 'Change Image'
}

PUBLISH_OPERATIONS = [
OPERATION.PUBLISH,
OPERATION.CORRECT,
OPERATION.KILL,
OPERATION.TAKEDOWN,
]
58 changes: 57 additions & 1 deletion server/analytics/stats/gen_archive_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from superdesk import Command, command, get_resource_service, Option
from superdesk import Command, command, get_resource_service, Option, signals
from superdesk.logging import logger
from superdesk.utc import utcnow
from superdesk.metadata.item import ITEM_STATE, ITEM_TYPE, FORMAT, SCHEDULE_SETTINGS, \
Expand All @@ -24,6 +24,34 @@
from copy import deepcopy
from datetime import timedelta

gen_stats_signals = {
'generate': signals.signal('gen_archive_statistics:generate'),
'init_timeline': signals.signal('gen_archive_statistics:init_timeline'),
'process': signals.signal('gen_archive_statistics:process'),
'complete': signals.signal('gen_archive_statistics:complete'),
}


def connect_stats_signals(on_generate=None, on_init_timeline=None, on_process=None, on_complete=None):
"""Connect functions to the gen_stats_signals
:param on_generate: Callback for generate signal
:param on_init_timeline: Callback for init_timeline signal
:param on_process: Callback for process signal
:param on_complete: Callback for complete signal
"""
if on_generate:
gen_stats_signals['generate'].connect(on_generate)

if on_init_timeline:
gen_stats_signals['init_timeline'].connect(on_init_timeline)

if on_process:
gen_stats_signals['process'].connect(on_process)

if on_complete:
gen_stats_signals['complete'].connect(on_complete)


class GenArchiveStatistics(Command):
"""Generate statistics for archive documents based on archive_history documents
Expand Down Expand Up @@ -54,6 +82,19 @@ class GenArchiveStatistics(Command):
$ python manage.py analytics:gen_archive_statistics -item-id 'id-of-item-to-gen-stats-for'
$ python manage.py analytics:gen_archive_statistics -c 500
$ python manage.py analytics:gen_archive_statistics -chunk-size 500
There are 4 signals that are sent when generating statistics.
This allows custom statistics to be generated and stored in the item.
There is a dictionary in the schema for custom stats to be stored under the 'extra' attribute.
Example:
::
from analytics.stats.gen_archive_statistics import connect_stats_signals
connect_stats_signals(on_generate, on_init_timeline, on_process, on_complete)
# or by key values
connect_stats_signals(on_process=on_process_stats)
"""

option_list = [
Expand Down Expand Up @@ -406,6 +447,8 @@ def _store_update_fields(self, entry):
desk_transitions.store_update_fields(entry, update)
featuremedia_updates.store_update_fields(entry, update)

gen_stats_signals['generate'].send(self, entry=entry, update=update)

if update:
entry['update'] = update
else:
Expand All @@ -425,6 +468,8 @@ def gen_stats_from_timeline(self, item):
desk_transitions.init(stats)
featuremedia_updates.init(stats)

gen_stats_signals['init_timeline'].send(self, stats=stats)

try:
entries = sorted(
stats[STAT_TYPE.TIMELINE],
Expand Down Expand Up @@ -477,9 +522,20 @@ def gen_stats_from_timeline(self, item):
desk_transitions.process(entry, new_timeline, updates, update, stats)
featuremedia_updates.process(entry, new_timeline, updates, update, stats)

gen_stats_signals['process'].send(
self,
entry=entry,
new_timeline=new_timeline,
updates=updates,
update=update,
stats=stats
)

desk_transitions.complete(stats, updates)
featuremedia_updates.complete(stats, updates)

gen_stats_signals['complete'].send(self, stats=stats, updates=updates)

if updates.get('firstpublished') and updates.get('firstcreated'):
updates['time_to_first_publish'] = (
updates['firstpublished'] - updates['firstcreated']
Expand Down

0 comments on commit 334107b

Please sign in to comment.