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 delete_old_sessions():
    """Delete all expired sessions."""
    logger.info("Deleting expired sessions")
    cutoff = pls.NOW_UTC_NO_TZ - pls.SESSION_TIMEOUT
    pls.db.db_exec("DELETE FROM " + Session.TABLENAME
                   + " WHERE last_activity_utc < ?", cutoff)