<commit_before>import copy
from datetime import datetime
import json
from corehq import Domain
from corehq.apps.accounting.models import (
    SoftwarePlanEdition,
    Subscription,
)
from corehq.apps.app_manager.models import Application
from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumn
from corehq.apps.reports.dispatcher import AdminReportDispatcher
from corehq.apps.reports.generic import ElasticTabularReport, GenericTabularReport
from django.utils.translation import ugettext as _, ugettext_noop
from corehq.apps.users.models import WebUser
from corehq.elastic import es_query, parse_args_for_es, fill_mapping_with_facets
from corehq.pillows.mappings.app_mapping import APP_INDEX
from corehq.pillows.mappings.user_mapping import USER_INDEX
from corehq.apps.app_manager.commcare_settings import SETTINGS as CC_SETTINGS
from corehq.toggles import IS_DEVELOPER


INDICATOR_DATA = {
    "active_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_domains",
        "chart_title": "Active Project Spaces",
        "hide_cumulative_charts": True,
        "histogram_type": "active_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "active_self_started_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_self_started_domains",
        "chart_title": "Active Self Started Project Spaces",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "self_started": ["T"],
        },
        "histogram_type": "active_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "domains",
        "chart_title": "Total Project Spaces",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "domain_self_started_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "self_started_domains",
        "chart_title": "Self-Started Project Spaces",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "params_es_dict": {
            "internal.self_started": ["T"],
        },
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "forms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms",
        "chart_title": "All Forms",
        "histogram_type": "forms",
        "interval": "week",
        "xaxis_label": "# forms",
    },
    "forms_mobile": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms_mobile",
        "chart_title": "Forms Submitted by Mobile Workers",
        "histogram_type": "forms",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": True,
        },
        "xaxis_label": "# forms",
    },
    "forms_web": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms_web",
        "chart_title": "Forms Submitted by Web Users",
        "histogram_type": "forms",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": False,
        },
        "xaxis_label": "# forms",
    },
    "users": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users",
        "chart_title": "Total Users",
        "histogram_type": "users",
        "interval": "week",
        "xaxis_label": "# users",
    },
    "users_mobile": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users_mobile",
        "chart_title": "Mobile Users (submitted)",
        "histogram_type": "users",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": True,
        },
        "xaxis_label": "# users",
    },
    "users_web": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users_web",
        "chart_title": "Web Users (submitted)",
        "histogram_type": "users",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": False,
        },
        "xaxis_label": "# users",
    },
    "sms_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "sms_domains",
        "chart_title": "Total Projects That Have Used SMS",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "commconnect_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "commconnect_domains",
        "chart_title": "Total CommConnect Enabled Domains",
        "params_es_dict": {
            "commconnect_enabled": ["T"],
        },
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "incoming_sms_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "incoming_sms_domains",
        "chart_title": "Total Projects Using Incoming SMS",
        "params_es_dict": {
            "direction": ["i"],
        },
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "sms_only_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "sms_only_domains",
        "chart_title": "Total SMS Only Projects",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_only_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "active_commconnect_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_commconnect_domains",
        "chart_title": "Active CommConnect Project Spaces",
        "hide_cumulative_charts": True,
        "histogram_type": "active_commconnect_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_outgoing_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_outgoing_sms",
        "chart_title": "Total Outgoing SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["o"],
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_incoming_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_incoming_sms",
        "chart_title": "Total Incoming SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_outgoing_client_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_outgoing_client_sms",
        "chart_title": "Total Outgoing Client SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
            "couch_recipient_doc_type": ["commcarecase"]
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_incoming_client_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_incoming_client_sms",
        "chart_title": "Total Incoming Client SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
            "couch_recipient_doc_type": ["commcarecase"]
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_mobile_workers": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_mobile_workers",
        "chart_title": "Total Mobile Workers",
        "histogram_type": "mobile_workers",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_mobile_workers": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_mobile_workers",
        "chart_title": "Active Mobile Workers",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "couch_recipient_doc_type": ["commcareuser"],
        },
        "histogram_type": "active_mobile_users",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_dimagi_owned_gateways": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_dimagi_owned_gateways",
        "chart_title": "Active Projects Using Dimagi Owned Gateways",
        "hide_cumulative_charts": True,
        "histogram_type": "active_dimagi_gateways",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_clients": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_clients",
        "chart_title": "Total Clients",
        "histogram_type": "mobile_clients",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_clients": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_mobile_clients",
        "chart_title": "Active Mobile Clients",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "couch_recipient_doc_type": ["commcarecase"],
        },
        "histogram_type": "active_mobile_users",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_countries": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_countries",
        "chart_title": "Active Countries",
        "histogram_type": "active_countries",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "countries": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "countries",
        "chart_title": "Countries",
        "histogram_type": "countries",
        "interval": "week",
        "xaxis_label": "# countries",
    },
}


class AdminReport(GenericTabularReport):
    dispatcher = AdminReportDispatcher
    base_template = "hqadmin/faceted_report.html"

class AdminFacetedReport(AdminReport, ElasticTabularReport):
    default_sort = None
    es_prefix = "es_" # facet keywords in the url will be prefixed with this
    asynchronous = False
    ajax_pagination = True
    exportable = True
    es_queried = False<commit_msg>Hide cumulative charts for countries<commit_after>import copy
from datetime import datetime
import json
from corehq import Domain
from corehq.apps.accounting.models import (
    SoftwarePlanEdition,
    Subscription,
)
from corehq.apps.app_manager.models import Application
from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumn
from corehq.apps.reports.dispatcher import AdminReportDispatcher
from corehq.apps.reports.generic import ElasticTabularReport, GenericTabularReport
from django.utils.translation import ugettext as _, ugettext_noop
from corehq.apps.users.models import WebUser
from corehq.elastic import es_query, parse_args_for_es, fill_mapping_with_facets
from corehq.pillows.mappings.app_mapping import APP_INDEX
from corehq.pillows.mappings.user_mapping import USER_INDEX
from corehq.apps.app_manager.commcare_settings import SETTINGS as CC_SETTINGS
from corehq.toggles import IS_DEVELOPER


INDICATOR_DATA = {
    "active_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_domains",
        "chart_title": "Active Project Spaces",
        "hide_cumulative_charts": True,
        "histogram_type": "active_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "active_self_started_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_self_started_domains",
        "chart_title": "Active Self Started Project Spaces",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "self_started": ["T"],
        },
        "histogram_type": "active_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "domains",
        "chart_title": "Total Project Spaces",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "domain_self_started_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "self_started_domains",
        "chart_title": "Self-Started Project Spaces",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "params_es_dict": {
            "internal.self_started": ["T"],
        },
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "forms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms",
        "chart_title": "All Forms",
        "histogram_type": "forms",
        "interval": "week",
        "xaxis_label": "# forms",
    },
    "forms_mobile": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms_mobile",
        "chart_title": "Forms Submitted by Mobile Workers",
        "histogram_type": "forms",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": True,
        },
        "xaxis_label": "# forms",
    },
    "forms_web": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "forms_web",
        "chart_title": "Forms Submitted by Web Users",
        "histogram_type": "forms",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": False,
        },
        "xaxis_label": "# forms",
    },
    "users": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users",
        "chart_title": "Total Users",
        "histogram_type": "users",
        "interval": "week",
        "xaxis_label": "# users",
    },
    "users_mobile": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users_mobile",
        "chart_title": "Mobile Users (submitted)",
        "histogram_type": "users",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": True,
        },
        "xaxis_label": "# users",
    },
    "users_web": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "users_web",
        "chart_title": "Web Users (submitted)",
        "histogram_type": "users",
        "interval": "week",
        "params_es_dict": {
            "user_type_mobile": False,
        },
        "xaxis_label": "# users",
    },
    "sms_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "sms_domains",
        "chart_title": "Total Projects That Have Used SMS",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "commconnect_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "commconnect_domains",
        "chart_title": "Total CommConnect Enabled Domains",
        "params_es_dict": {
            "commconnect_enabled": ["T"],
        },
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "incoming_sms_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "incoming_sms_domains",
        "chart_title": "Total Projects Using Incoming SMS",
        "params_es_dict": {
            "direction": ["i"],
        },
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "sms_only_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "sms_only_domains",
        "chart_title": "Total SMS Only Projects",
        "date_field_opts": [
            {
                "name": "Date Created",
                "value": "date_created",
            },
        ],
        "histogram_type": "sms_only_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "active_commconnect_domain_count": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_commconnect_domains",
        "chart_title": "Active CommConnect Project Spaces",
        "hide_cumulative_charts": True,
        "histogram_type": "active_commconnect_domains",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_outgoing_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_outgoing_sms",
        "chart_title": "Total Outgoing SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["o"],
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_incoming_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_incoming_sms",
        "chart_title": "Total Incoming SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_outgoing_client_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_outgoing_client_sms",
        "chart_title": "Total Outgoing Client SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
            "couch_recipient_doc_type": ["commcarecase"]
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_incoming_client_sms": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_incoming_client_sms",
        "chart_title": "Total Incoming Client SMS",
        "date_field_opts": [
            {
                "name": "Date Sent",
                "value": "date",
            },
        ],
        "params_es_dict": {
            "direction": ["i"],
            "couch_recipient_doc_type": ["commcarecase"]
        },
        "histogram_type": "real_sms_messages",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_mobile_workers": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_mobile_workers",
        "chart_title": "Total Mobile Workers",
        "histogram_type": "mobile_workers",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_mobile_workers": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_mobile_workers",
        "chart_title": "Active Mobile Workers",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "couch_recipient_doc_type": ["commcareuser"],
        },
        "histogram_type": "active_mobile_users",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_dimagi_owned_gateways": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_dimagi_owned_gateways",
        "chart_title": "Active Projects Using Dimagi Owned Gateways",
        "hide_cumulative_charts": True,
        "histogram_type": "active_dimagi_gateways",
        "interval": "week",
        "xaxis_label": "# domains",
    },
    "total_clients": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "total_clients",
        "chart_title": "Total Clients",
        "histogram_type": "mobile_clients",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_clients": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_mobile_clients",
        "chart_title": "Active Mobile Clients",
        "hide_cumulative_charts": True,
        "params_es_dict": {
            "couch_recipient_doc_type": ["commcarecase"],
        },
        "histogram_type": "active_mobile_users",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "active_countries": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "active_countries",
        "chart_title": "Active Countries",
        "hide_cumulative_charts": True,
        "histogram_type": "active_countries",
        "interval": "week",
        "xaxis_label": "# workers",
    },
    "countries": {
        "ajax_view": "admin_reports_stats_data",
        "chart_name": "countries",
        "chart_title": "Countries",
        "hide_cumulative_charts": True,
        "histogram_type": "countries",
        "interval": "week",
        "xaxis_label": "# countries",
    },
}


class AdminReport(GenericTabularReport):
    dispatcher = AdminReportDispatcher
    base_template = "hqadmin/faceted_report.html"

class AdminFacetedReport(AdminReport, ElasticTabularReport):
    default_sort = None
    es_prefix = "es_" # facet keywords in the url will be prefixed with this
    asynchronous = False
    ajax_pagination = True
    exportable = True
    es_queried = False
{'fraction_non_alphanumeric': 0.13962085715513906, 'fraction_numerical': 0.0, 'mean_word_length': 2.309716887181676, 'pattern_counts': {'":': 498, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99541', 'n_tokens_mistral': 7326, 'n_tokens_neox': 7082, 'n_words': 1251}