level0prc.py 4.24 KB
Newer Older
Wei Shoulin's avatar
c3  
Wei Shoulin committed
1
2
3
4
5
6
7
8
import os
import logging
import time, datetime
import shutil

from ..common.db import DBClient
from ..common.utils import *
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
9
from csst_dfs_commons.models.msc import Level0PrcRecord
Wei Shoulin's avatar
c3  
Wei Shoulin committed
10
11
12
13
14
from csst_dfs_commons.models.common import from_dict_list

log = logging.getLogger('csst')

class Level0PrcApi(object):
Wei Shoulin's avatar
Wei Shoulin committed
15
16
    def __init__(self, sub_system = "msc"):
        self.sub_system = sub_system
Wei Shoulin's avatar
c3  
Wei Shoulin committed
17
18
19
20
21
22
23
        self.root_dir = os.getenv("CSST_LOCAL_FILE_ROOT", "/opt/temp/csst")
        self.db = DBClient()

    def find(self, **kwargs):
        ''' retrieve level0 procedure records from database

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
24
            level0_id: [str]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
25
26
27
28
29
30
31
32
33
34
35
36
            pipeline_id: [str]
            prc_module: [str]
            prc_status : [int]

        return: csst_dfs_common.models.Result
        '''
        try:
            level0_id = get_parameter(kwargs, "level0_id")
            pipeline_id = get_parameter(kwargs, "pipeline_id")
            prc_module = get_parameter(kwargs, "prc_module")
            prc_status = get_parameter(kwargs, "prc_status")

Wei Shoulin's avatar
Wei Shoulin committed
37
            sql_data = f"select * from msc_level0_prc"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
38

Wei Shoulin's avatar
Wei Shoulin committed
39
            sql_condition = f"where level0_id='{level0_id}'"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
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
            if pipeline_id:
                sql_condition = sql_condition + " and pipeline_id='" + pipeline_id + "'"
            if prc_module:
                sql_condition = sql_condition + " and prc_module ='" + prc_module + "'"
            if prc_status:
                sql_condition = f"{sql_condition} and prc_status={prc_status}"   

            sql_data = f"{sql_data} {sql_condition}"

            _, records = self.db.select_many(sql_data)
            return Result.ok_data(data=from_dict_list(Level0PrcRecord, records)).append("totalCount", len(records))

        except Exception as e:
            return Result.error(message=str(e))

    def update_proc_status(self, **kwargs):
        ''' update the status of reduction

        parameter kwargs:
            id : [int],
            status : [int]

        return csst_dfs_common.models.Result
        '''
        id = get_parameter(kwargs, "id")
        status = get_parameter(kwargs, "status")

        try:
            existed = self.db.exists(
Wei Shoulin's avatar
Wei Shoulin committed
69
                "select * from msc_level0_prc where id=?",
Wei Shoulin's avatar
c3  
Wei Shoulin committed
70
71
72
73
74
75
                (id,)
            )
            if not existed:
                log.warning('%s not found' %(id, ))
                return Result.error(message ='%s not found' %(id, ))
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
76
                'update msc_level0_prc set prc_status=?, prc_time=? where id=?',
Wei Shoulin's avatar
c3  
Wei Shoulin committed
77
78
79
80
81
82
83
84
85
86
87
88
89
                (status, format_time_ms(time.time()), id)
            )  
            self.db.end() 
            return Result.ok_data()
           
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))

    def write(self, **kwargs):
        ''' insert a level0 procedure record into database
 
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
90
            level0_id : [str]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
91
92
            pipeline_id : [str]
            prc_module : [str]
Wei Shoulin's avatar
Wei Shoulin committed
93
            params_file_path : [str]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
94
95
            prc_status : [int]
            prc_time : [str]
Wei Shoulin's avatar
Wei Shoulin committed
96
            result_file_path : [str]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
97
98
99
100
101
102
103
104
        return csst_dfs_common.models.Result
        '''   

        rec = Level0PrcRecord(
            id = 0,
            level0_id = get_parameter(kwargs, "level0_id"),
            pipeline_id = get_parameter(kwargs, "pipeline_id"),
            prc_module = get_parameter(kwargs, "prc_module"),
Wei Shoulin's avatar
Wei Shoulin committed
105
            params_file_path = get_parameter(kwargs, "params_file_path"),
Wei Shoulin's avatar
Wei Shoulin committed
106
            prc_status = get_parameter(kwargs, "prc_status", -1),
Wei Shoulin's avatar
c3  
Wei Shoulin committed
107
            prc_time = get_parameter(kwargs, "prc_time"),
Wei Shoulin's avatar
Wei Shoulin committed
108
            result_file_path = get_parameter(kwargs, "result_file_path")
Wei Shoulin's avatar
c3  
Wei Shoulin committed
109
110
111
        )
        try:
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
112
                'INSERT INTO msc_level0_prc (level0_id,pipeline_id,prc_module, params_file_path, prc_status,prc_time,result_file_path) \
Wei Shoulin's avatar
c3  
Wei Shoulin committed
113
                    VALUES(?,?,?,?,?,?,?)',
Wei Shoulin's avatar
Wei Shoulin committed
114
                (rec.level0_id, rec.pipeline_id, rec.prc_module, rec.params_file_path, rec.prc_status, rec.prc_time, rec.result_file_path)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
115
116
117
118
119
120
121
122
123
124
            )
            self.db.end()
            rec.id = self.db.last_row_id()

            return Result.ok_data(data=rec)

        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))