<commit_before>from flask import Flask, request, render_template, redirect
from google.appengine.ext import deferred

import json
import pytz
import datetime
import random
import time
import logging

import models
import consolidate
import alertfunctions

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/')
def hello():
    # Redirect to the "show" url
    return redirect("/show", code=302)


@app.route('/chart/<identifier>/<type_>')
def chart(identifier, type_):
    since = request.args.get('since', str(int(time.time()) - 24 * 60 * 60))  # Last 24 hrs
    return render_template('chart.template.html', id=identifier, type=type_, since=since)


def do_publish(identifier, type_, value, consolidate_every, ts=None):
    measurement = models.Measurement(identifier=identifier, type=type_, value=value)
    if ts is not None:
        measurement.timestamp = ts
    measurement.put()

    if consolidate_every:
        if random.random() < 1.0 / consolidate_every:  # consolidate every consolidate_every calls on average
            deferred.defer(consolidate.consolidate_measurements, identifier, type_, _queue="index-queue")


@app.route('/publish')
def publish():
    secret = request.args.get('secret')
    identifier = request.args.get('id')

    # first verify if we're allowed to publish
    sensor = models.Sensor.query(models.Sensor.identifier == identifier).fetch()
    if not sensor or sensor[0].secret != secret:
        return "Please specify the correct secret!", 403

    # then publish the data to the specified channels.
    for key, value in request.args.iteritems():
        if key in ("id", "secret"):
            continue
        do_publish(identifier, key, value, sensor[0].consolidate_every)
    return "Ok."


@app.route('/show')
def show():
    measurements = models.Measurement.query(projection=[models.Measurement.identifier, models.Measurement.type],
                                            distinct=True).fetch()<commit_msg>Issue warning if measurements are published over http.<commit_after>from flask import Flask, request, render_template, redirect
from google.appengine.ext import deferred

import json
import pytz
import datetime
import random
import time
import logging

import models
import consolidate
import alertfunctions

app = Flask(__name__)
app.config['DEBUG'] = True


@app.route('/')
def hello():
    # Redirect to the "show" url
    return redirect("/show", code=302)


@app.route('/chart/<identifier>/<type_>')
def chart(identifier, type_):
    since = request.args.get('since', str(int(time.time()) - 24 * 60 * 60))  # Last 24 hrs
    return render_template('chart.template.html', id=identifier, type=type_, since=since)


def do_publish(identifier, type_, value, consolidate_every, ts=None):
    measurement = models.Measurement(identifier=identifier, type=type_, value=value)
    if ts is not None:
        measurement.timestamp = ts
    measurement.put()

    if consolidate_every:
        if random.random() < 1.0 / consolidate_every:  # consolidate every consolidate_every calls on average
            deferred.defer(consolidate.consolidate_measurements, identifier, type_, _queue="index-queue")


@app.route('/publish')
def publish():
    # Is this http or https?
    https = request.url.startswith('https')
    if not https:
        logging.warning('Measurements published over insecure connection!')

    secret = request.args.get('secret')
    identifier = request.args.get('id')

    # first verify if we're allowed to publish
    sensor = models.Sensor.query(models.Sensor.identifier == identifier).fetch()
    if not sensor or sensor[0].secret != secret:
        return "Please specify the correct secret!", 403

    # then publish the data to the specified channels.
    for key, value in request.args.iteritems():
        if key in ("id", "secret"):
            continue
        do_publish(identifier, key, value, sensor[0].consolidate_every)

    if not https:
        return "Please connect over https. Support for http will go away soon."
    return "Ok."


@app.route('/show')
def show():
    measurements = models.Measurement.query(projection=[models.Measurement.identifier, models.Measurement.type],
                                            distinct=True).fetch()
{'fraction_non_alphanumeric': 0.11377527033380348, 'fraction_numerical': 0.00846262341325811, 'mean_word_length': 3.5950323974082075, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2505139', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1163, 'n_words': 352}