Ejemplo n.º 1
0
    def results(key, update=False):
        cache_key = AdminQuery.cache_key_template.format(**{"id": key})
        results = cache.get(cache_key)
        if results is None or update:
            engine = db.get_engine(current_app)
            connection = engine.connect()
            aqs = connection.execute(
                select([AdminQuery]).where(AdminQuery.id == key))
            if aqs.returns_rows:
                aqs = [{key: value for (key, value) in o.items()} for o in aqs]
                connection.close()
                if len(aqs):
                    query_text = f"{aqs[0]['query_text']};".replace(
                        '\r\n', ' ').replace('\n', ' ').replace("%", "%%")

                    query_name = f"{aqs[0]['name']}"
                    headings, rows = AdminQuery.execute(query_text)
                    results = headings, rows, query_name
                    cache.set(cache_key,
                              results,
                              timeout=current_app.
                              config['ADMIN_QUERY_CACHE_TIMEOUT'])
                else:
                    results = [], [], "unknown"
            else:
                results = [], [], "unknown"
        return results
Ejemplo n.º 2
0
def user_activity_cached(day, user, update=False, timeout=3600):
    cache_key = "USER_ACTIVITY_NOW_{}_{}".format(day, user)

    result = cache.get(cache_key)
    if update:
        result = None
    if result is not None:
        d, users = result
    else:
        active = find_user_activity_now(day, user)

        start_date = datetime(2018, 1, 1, 0, 0, 0)
        d = []
        v = []
        users = set()
        for x in active:
            v.append(x)
            users.add(x[0])

        for td in (start_date + timedelta(hours=1 * it) for it in xrange(24)):
            row = dict()
            row['y'] = td.strftime("%H:%M")
            for u in users:
                row[u] = 0
            d.append(row)

        for r in v:
            for item in range(0, len(d)):
                if d[item]['y'] == r[1]:
                    d[item][r[0]] = r[2]

        cache.set(cache_key, (d, users), timeout=timeout)
    return d, users
Ejemplo n.º 3
0
def home():
    result = cache.get("dashboard_today_items")
    if result is None:
        items = find_created_items_today()
        result = []
        for i in items:
            result.append(i)
        cache.set("dashboard_today_items", result, timeout=10 * 60)

    return render_template('page/dashboard.html', stats=result)
Ejemplo n.º 4
0
def get_user_emotion_list():
    user_names = cache.get('user-emotions-names')

    if user_names is None:
        user_names = []

        sql = text('select distinct owner from emotion')
        result = db.engine.execute(sql)
        for row in result:
            user_names.append(row[0])
            cache.set('user-emotions-names', user_names, timeout=7200)

    return user_names
Ejemplo n.º 5
0
def get_sense_relation_list():
    relations = cache.get('sense-relations')

    if relations is None:
        relations = []

        sql = text(
            'select distinct tsr.REL_ID, r.name  from tracker_lexicalrelation tsr join relationtype r on r.ID=tsr.REL_ID'
        )
        result = db.engine.execute(sql)
        for row in result:
            relations.append(row)
            cache.set('sense-relations', relations, timeout=7200)

    return relations
Ejemplo n.º 6
0
def paginate(query, page=None, per_page=None, total_cache_key='', update=False):
    """Returns ``per_page`` items from page ``page``.

    If ``page`` or ``per_page`` are ``None``, they will be retrieved from
    the request query. If ``max_per_page`` is specified, ``per_page`` will
    be limited to that value. If there is no request or they aren't in the
    query, they default to 1 and 20 respectively.

    When ``error_out`` is ``True`` (default), the following rules will
    cause a 404 response:

    * No items are found and ``page`` is not 1.
    * ``page`` is less than 1, or ``per_page`` is negative.
    * ``page`` or ``per_page`` are not ints.

    When ``error_out`` is ``False``, ``page`` and ``per_page`` default to
    1 and 20 respectively.

    Returns a :class:`Pagination` object.
    """

    error_out = True
    max_per_page = None

    if request:
        if page is None:
            try:
                page = int(request.args.get('page', 1))
            except (TypeError, ValueError):
                if error_out:
                    abort(404)

                page = 1

        if per_page is None:
            try:
                per_page = int(request.args.get('per_page', 20))
            except (TypeError, ValueError):
                if error_out:
                    abort(404)

                per_page = 20
    else:
        if page is None:
            page = 1

        if per_page is None:
            per_page = 20

    if max_per_page is not None:
        per_page = min(per_page, max_per_page)

    if page < 1:
        if error_out:
            abort(404)
        else:
            page = 1

    if per_page < 0:
        if error_out:
            abort(404)
        else:
            per_page = 20

    items = query.limit(per_page).offset((page - 1) * per_page).all()

    if not items and page != 1 and error_out:
        abort(404)

    # No need to count if we're on the first page and there are fewer
    # items than we expected.
    if page == 1 and len(items) < per_page:
        total = len(items)
    else:
        if total_cache_key != '':
            if update:
                total = None
            else:
                total = cache.get(total_cache_key)
            if total is None:
                total = query.order_by(None).count()
                cache.set(total_cache_key, total, timeout=7200)
        else:
            total = query.order_by(None).count()

    return Pagination(query, page, per_page, total, items)