Exemple #1
0
def send_analytics_if_necessary():
    """Send analytics to the CamCOPS base server, if required.

    If analytics reporting is enabled, and analytics have not been sent
    recently, collate and send them to the CamCOPS base server in Cambridge,
    UK.
    """
    if not pls.SEND_ANALYTICS:
        # User has disabled analytics reporting.
        return
    lastSentVar = cc_storedvar.ServerStoredVar("lastAnalyticsSentAt", "text",
                                               None)
    lastSentVal = lastSentVar.getValue()
    if lastSentVal:
        elapsed = pls.NOW_UTC_WITH_TZ - cc_dt.get_datetime_from_string(
            lastSentVal)
        if elapsed < ANALYTICS_PERIOD:
            # We sent analytics recently.
            return

    # Compile analytics
    now_as_utc_iso_string = cc_dt.format_datetime(pls.NOW_UTC_WITH_TZ,
                                                  DATEFORMAT.ISO8601)
    (table_names, record_counts) = get_all_tables_with_record_counts()

    # This is what's sent:
    d = {
        "source": "server",
        "now": now_as_utc_iso_string,
        "camcops_version": str(cc_version.CAMCOPS_SERVER_VERSION),
        "server": pls.SERVER_NAME,
        "table_names": ",".join(table_names),
        "record_counts": ",".join([str(x) for x in record_counts]),
    }
    # The HTTP_HOST variable might provide some extra information, but is
    # per-request rather than per-server, making analytics involving it that
    # bit more intrusive for little extra benefit, so let's not send it.
    # See http://stackoverflow.com/questions/2297403 for details.

    # Send it.
    encoded_dict = urllib.urlencode(d)
    request = urllib2.Request(ANALYTICS_URL, encoded_dict)
    try:
        urllib2.urlopen(request, timeout=ANALYTICS_TIMEOUT_MS)
        # connection = urllib2.urlopen(request, timeout=ANALYTICS_TIMEOUT_MS)
        # response = connection.read()  # we don't care
    except (urllib2.URLError, urllib2.HTTPError):
        # something broke; try again next time
        logger.info("Failed to send analytics to {}".format(ANALYTICS_URL))
        return

    # Store current time as last-sent time
    logger.debug("Analytics sent.")
    lastSentVar.setValue(now_as_utc_iso_string)
Exemple #2
0
def clear_dummy_login_failures_if_necessary():
    """Clear dummy login failures if we haven't done so for a while.

    Not too often! See CLEAR_DUMMY_LOGIN_FREQUENCY_DAYS.
    """
    lastClearedVar = cc_storedvar.ServerStoredVar(
        "lastDummyLoginFailureClearanceAt", "text", None)
    lastClearedVal = lastClearedVar.getValue()
    if lastClearedVal:
        elapsed = pls.NOW_UTC_WITH_TZ - cc_dt.get_datetime_from_string(
            lastClearedVal)
        if elapsed < CLEAR_DUMMY_LOGIN_PERIOD:
            # We cleared it recently.
            return

    clear_login_failures_for_nonexistent_users()
    logger.debug("Dummy login failures cleared.")
    now_as_utc_iso_string = cc_dt.format_datetime(pls.NOW_UTC_WITH_TZ,
                                                  DATEFORMAT.ISO8601)
    lastClearedVar.setValue(now_as_utc_iso_string)
Exemple #3
0
 def get_filter_end_datetime(self):
     """Get end date filter as a datetime."""
     return cc_dt.get_datetime_from_string(self.filter_end_datetime_iso8601)
Exemple #4
0
 def get_filter_dob(self):
     """Get filtering DOB as a datetime."""
     return cc_dt.get_datetime_from_string(self.filter_dob_iso8601)