summaryrefslogtreecommitdiffstats
path: root/dashboard/backend/dovetail/db/api.py
blob: a522a481e73b4870c2b94e0cce354242e58eda78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

"""
Defines interface for DB access.
"""

import logging

from dovetail.db import database
from dovetail.db import utils
from dovetail.db import models


@database.run_in_session()
def store_result(exception_when_existing=True,
                 session=None, **kwargs):
    """Storing results into database.

    :param data: Dict describes test results.
    """
    logging.debug('store_result:%s' % kwargs)
    result = utils.add_db_object(
        session, models.Result, exception_when_existing,
        **kwargs)

    return result


@database.run_in_session()
@utils.wrap_to_dict()
def list_results(session=None, **filters):
    """Get all results
    """
    logging.debug('session:%s' % session)
    results = utils.list_db_objects(
        session, models.Result, **filters
    )
    return results


@database.run_in_session()
@utils.wrap_to_dict()
def get_result(test_id, exception_when_missing=True,
               session=None, **kwargs):
    """Get specific result with the test_id

    :param test_id: the unique serial number for the test
    """
    return _get_result(test_id, session,
                       exception_when_missing=exception_when_missing, **kwargs)


def _get_result(test_id, session=None, **kwargs):
    return utils.get_db_object(
        session, models.Result, test_id=test_id, **kwargs)


@database.run_in_session()
def del_result(test_id, session=None, **kwargs):
    """Delete a results from database

    :param test_id: the unique serial number for the test
    """
    return utils.del_db_objects(session, models.Result,
                                test_id=test_id, **kwargs)