示例#1
0
def main():
    timestamp = datetime.utcnow()

    try:
        result = urlfetch.fetch(url, headers=request_headers, deadline=deadline)
    except urlfetch.Error:
        ping_result = PingResultModel(
            timestamp   = timestamp,
            content     = db.Text(traceback.format_exc()),
        )
    else:
        delta = datetime.utcnow() - timestamp

        encoding = 'UTF-8'
        if 'content-type' in result.headers:
            match = charset_re.search(result.headers['content-type'])
            if match:
                encoding = match.group(1)

        ping_result = PingResultModel(
            timestamp   = timestamp,
            duration    = delta.seconds + delta.microseconds / (1000.0 ** 2),
            status_code = result.status_code,
            content     = db.Text(result.content, encoding),
        )

        for header, value in result.headers.items():
            setattr(ping_result, 'header-%s' % header, value)

    ping_result.put()
示例#2
0
def main():
    timestamp = datetime.utcnow()
    interval = int(os.environ['PATH_INFO'][12:])

    minute = timestamp.minute - timestamp.minute % interval
    end = timestamp.replace(minute=minute, second=0, microsecond=0)
    start = end - timedelta(minutes=interval)

    query = PingResultModel.gql('WHERE timestamp >= :start AND timestamp < :end', start=start, end=end)
    samples = query.count()

    average_result = AverageResultModel(
        start    = start,
        interval = interval,
        samples  = samples,
    )

    if samples:
        average_result.min_delay = min(ping.duration for ping in query)
        average_result.max_delay = max(ping.duration for ping in query)
        average_result.avg_delay = sum(ping.duration for ping in query) / samples

        for ping in query:
            if hasattr(ping, 'status_code'):
                status_count = getattr(average_result, 'status_%d' % ping.status_code, 0)
                setattr(average_result, 'status_%d' % ping.status_code, status_count + 1)

    average_result.put()