@@ -0,0 +1,1836 @@
+#!/usr/bin/env python3%0A%0A# Akvo Reporting is covered by the GNU Affero General Public License.%0A# See more details in the license.txt file located at the root folder of the Akvo RSR module.%0A# For additional details on the GNU license please see %3C http://www.gnu.org/licenses/agpl.html %3E.%0A%0A%22%22%22 Calculate the open rate for announcements%0A%0AUsage:%0A%0A    python manage.py announcement_open_rate announcement_tag release_date%0A%0A%22%22%22%0A%0Aimport datetime%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.contrib.auth import get_user_model%0A%0AUser = get_user_model()%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Calculate the open rate for a specific announcement%22%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument(%0A            %22announcement_tag%22, type=str, help=%22Tag for the announcment%22%0A        )%0A        parser.add_argument(%0A            %22release_date%22, type=str, help=%22Release date%22,%0A        )%0A%0A    def handle(self, *args, **options):%0A        tag = options%5B%22announcement_tag%22%5D%0A        release_date = datetime.datetime.strptime(%0A            options%5B%22release_date%22%5D, %22%25d-%25m-%25Y%22%0A        ).date()%0A        # NOTE: We use the last login date here, under the assumption that we%0A        # are calculating the open rate only after a duration of 2 weeks of the%0A        # release date, which is the cookie expiry time for our sessions%0A        logged_in_users = User.objects.filter(last_login__gte=release_date)%0A        seen_users = User.objects.filter(seen_announcements__overlap=%5Btag%5D)%0A%0A        seen_count = seen_users.count()%0A        logged_in_count = logged_in_users.count()%0A        open_rate = (%0A            seen_users.count() / logged_in_count * 100 if logged_in_count %3E 0 else None%0A        )%0A        print(f%22Seen count: %7Bseen_count%7D%22)%0A        print(f%22Logged in count: %7Blogged_in_count%7D%22)%0A        print(f%22Open rate: %7Bopen_rate%7D%22)%0A

Add command to compute the announcement open rate