<commit_before>from __future__ import absolute_import

import requests
from datetime import datetime
from collections import Counter

from celery.schedules import crontab
from celery.task import periodic_task

from django.contrib.auth.models import User

from nurseconnect.services import get_clinic_code
from nurseconnect.settings import (
    JEMBI_URL, JEMBI_USERNAME, JEMBI_PASSWORD, JEMBI)


class JembiMetricsPoster(object):
    """
    Send metrics to Jembi
    """
    def send_metric(self, data):
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        requests.post(
            url=JEMBI_URL,
            headers=headers,
            json=data,
            auth=(JEMBI_USERNAME, JEMBI_PASSWORD),
            verify=False
        )


def nurses_registered():
    """Returns the number of nurses registered on the system."""
    num_nurses = User.objects.filter(is_staff=False).count()
    data = {
        "dataValues": [
            {
                "dataElement": JEMBI["num_nurses"]["dataElement"],
                "period":
                    str(datetime.now().year) + "%02d" % datetime.now().month,
                "value": str(num_nurses)
            },
        ]
    }

    JembiMetricsPoster().send_metric(data)


def nurses_registered_per_clinic():
    """ Returns the number of nurses registered per facility. """
    users = User.objects.filter(is_staff=False)

    clinic_codes = []
    for user in users:
        if hasattr(user.profile, "for_nurseconnect"):
            clinic_codes.append(user.profile.for_nurseconnect.clinic_code)
    nurses_per_facility = Counter(clinic_codes)
    metric_poster = JembiMetricsPoster()

    # nurses_per_facility = {"clinic_code": total} pairs
    for k, v in nurses_per_facility.items():
        if k:
            clinic = get_clinic_code(k)
            data = {
                "dataValues": [
                    {
                        "dataElement": JEMBI["nurses_per_facility"][
                            "dataElement"],
                        "period":
                            str(datetime.now().year) +
                            "%02d" % datetime.now().month,
                        "orgUnit": clinic[1],
                        "value": str(v)
                    },
                ]
            }
            metric_poster.send_metric(data)


@periodic_task(
    run_every=crontab(minute=0, hour=6),
    default_retry_delay=300,
    ignore_result=True
)
def send_data():
    nurses_registered()
    nurses_registered_per_clinic()<commit_msg>More fixes<commit_after>from __future__ import absolute_import

import requests
from datetime import datetime
from collections import Counter

from celery.schedules import crontab
from celery.task import periodic_task

from django.contrib.auth.models import User

from nurseconnect.services import get_clinic_code
from nurseconnect.settings import (
    JEMBI_URL, JEMBI_USERNAME, JEMBI_PASSWORD, JEMBI)


class JembiMetricsPoster(object):
    """
    Send metrics to Jembi
    """
    def send_metric(self, data):
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        requests.post(
            url=JEMBI_URL,
            headers=headers,
            json=data,
            auth=(JEMBI_USERNAME, JEMBI_PASSWORD),
            verify=False
        )


def nurses_registered():
    """Returns the number of nurses registered on the system."""
    num_nurses = User.objects.filter(is_staff=False).count()
    data = {
        "dataValues": [
            {
                "dataElement": JEMBI["num_nurses"]["dataElement"],
                "period":
                    str(datetime.now().year) + "%02d" % datetime.now().month,
                "value": str(num_nurses)
            },
        ]
    }

    JembiMetricsPoster().send_metric(data)


def nurses_registered_per_clinic():
    """ Returns the number of nurses registered per facility. """
    users = User.objects.filter(is_staff=False)

    clinic_codes = []
    for user in users:
        if hasattr(user.profile, "for_nurseconnect"):
            clinic_codes.append(user.profile.for_nurseconnect.clinic_code)
    nurses_per_facility = Counter(clinic_codes)
    metric_poster = JembiMetricsPoster()

    # nurses_per_facility = {"clinic_code": total} pairs
    for k, v in nurses_per_facility.items():
        clinic = get_clinic_code(k)
        if clinic is not None:
            data = {
                "dataValues": [
                    {
                        "dataElement": JEMBI["nurses_per_facility"][
                            "dataElement"],
                        "period":
                            str(datetime.now().year) +
                            "%02d" % datetime.now().month,
                        "orgUnit": clinic[1],
                        "value": str(v)
                    },
                ]
            }
            metric_poster.send_metric(data)


@periodic_task(
    run_every=crontab(minute=0, hour=6),
    default_retry_delay=300,
    ignore_result=True
)
def send_data():
    nurses_registered()
    nurses_registered_per_clinic()
{'fraction_non_alphanumeric': 0.10222395630120952, 'fraction_numerical': 0.0039016777214202106, 'mean_word_length': 2.0572450805008944, 'pattern_counts': {'":': 20, '<': 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': '5792654', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1476, 'n_words': 289}