Beispiel #1
0
def addapp():

    # Retrieve the list of existing applications.
    s = sqla.db()
    applications = s.query(Application).all()

    if request.method == "POST":

        # Read the input.

        name = request.values.get("name")
        if name is None:
            flash("Please, provide an application name", "error")
            return render_template("add_app.html", applications=applications)

        description = request.values.get("description")
        if description is None:
            flash("Please, provide an application description", "error")
            return render_template("add_app.html", applications=applications)

        # Add the specified app itself.

        app = Application(name=name, description=description)
        s.add(app)
        s.commit()

        appid = app.id

        flash("The specified app has been added with ID: %r" % appid, "success")

        return redirect(url_for("addapp"))

    return render_template("add_app.html", applications=applications)
Beispiel #2
0
def fill():
    start_date = datetime.datetime(year=2014, month=1, day=1)
    end_date = datetime.datetime(year=2015, month=1, day=1)
    for single_date in daterange(start_date, end_date):
        hits = random.randint(0, 15)
        s = sqla.db()
        u = Hits(hits=hits, user_id=2, ts=single_date)
        s.add(u)
        s.commit()

    return "Ok"
Beispiel #3
0
def addhits():
    if request.method == "POST":

        # Read the input.

        isodate = request.values.get("datetime")
        if isodate is None:
            flash("Please, provide a date and time", "error")
            return render_template("add_hits.html")
        try:
            session["last_datetime"] = isodate
            isodate = dateutil.parser.parse(isodate)
        except:
            flash("Could not parse the date you provided", "error")
            return render_template("add_hits.html")

        hits = request.values.get("hits")
        if hits is None:
            flash("Please, provide the number of hits", "error")
            return render_template("add_hits.html")
        try:
            hits = int(hits)
        except:
            flash("The number of hits must be a valid integer")
            return render_template("add_hits.html")

        appid = request.values.get("appid")
        if appid is None:
            flash("Please, provide an application ID", "error")
            return render_template("add_hits.html")
        try:
            session["last_appid"] = appid
            appid = int(appid)
        except:
            flash("The Application ID must be a valid integer")
            return render_template("add_hits.html")


        # Add the specified hits themselves.
        hits = Hits(ts=isodate, hits=hits, app_id=appid)

        # WARNING: Doing db() only results in issues from the tests when we
        # modify the DB. TODO: A better method for testing.
        s = sqla.db()
        s.add(hits)
        s.commit()

        flash("The specified hits have been added", "success")
        return redirect(url_for("addhits"))

    return render_template("add_hits.html")
Beispiel #4
0
def hits(uid):
    from_dt = request.values.get("from")
    to_dt = request.values.get("to")

    # The granularity can be: daily, yearly, monthly, weekly, hourly
    granularity = request.values.get("granularity")

    if from_dt is not None:
        from_dt = dateutil.parser.parse(from_dt)
    else:
        from_dt = datetime.datetime.utcfromtimestamp(0)

    if to_dt is not None:
        to_dt = dateutil.parser.parse(to_dt)
    else:
        to_dt = datetime.datetime.utcnow()

    # Retrieve hits from DB but filtering by the from and to dates.
    s = sqla.db()
    hits_list = s.query(Hits).filter(Hits.app_id == uid, Hits.ts <= to_dt,
                                     Hits.ts >= from_dt).all()

    # Consider accumulating the hits for a period.
    if granularity is not None:
        hits_dict = accumulate_hits(hits_list, granularity)
        # Convert the dictionary to an actual list.
        hits_list = [hits_dict[t] for t in hits_dict]

    data = []
    for h in hits_list:
        data.append({
            "hits": h.hits,
            "user_id": h.app_id,
            "ts": h.ts.isoformat()
        })

    # Sort the list.
    data = sorted(data, key=lambda h: h["ts"])

    hits_str = json.dumps(data)

    return Response(hits_str, mimetype="application/json")
Beispiel #5
0
def hits(uid):
    from_dt = request.values.get("from")
    to_dt = request.values.get("to")

    # The granularity can be: daily, yearly, monthly, weekly, hourly
    granularity = request.values.get("granularity")

    if from_dt is not None:
        from_dt = dateutil.parser.parse(from_dt)
    else:
        from_dt = datetime.datetime.utcfromtimestamp(0)

    if to_dt is not None:
        to_dt = dateutil.parser.parse(to_dt)
    else:
        to_dt = datetime.datetime.utcnow()


    # Retrieve hits from DB but filtering by the from and to dates.
    s = sqla.db()
    hits_list = s.query(Hits).filter(Hits.app_id == uid, Hits.ts <= to_dt, Hits.ts >= from_dt).all()

    # Consider accumulating the hits for a period.
    if granularity is not None:
        hits_dict = accumulate_hits(hits_list, granularity)
        # Convert the dictionary to an actual list.
        hits_list = [hits_dict[t] for t in hits_dict]

    data = []
    for h in hits_list:
        data.append({"hits": h.hits, "user_id": h.app_id, "ts": h.ts.isoformat()})

    # Sort the list.
    data = sorted(data, key=lambda h: h["ts"])

    hits_str = json.dumps(data)

    return Response(hits_str, mimetype="application/json")
Beispiel #6
0
def new_hits(uid):
    s = sqla.db()
    u = Hits(hits=1, user_id=uid)
    s.add(u)
    s.commit()
    return "Done"
Beispiel #7
0
def new_hits(uid):
    s = sqla.db()
    u = Hits(hits=1, user_id=uid)
    s.add(u)
    s.commit()
    return "Done"
Beispiel #8
0
def create_new_user(name):
    s = sqla.db()
    u = Application(name=name)
    s.add(u)
    s.commit()
    return "New user: %s" % u.id