Optimize ns instant.
[vfc/nfvo/lcm.git] / lcm / pub / utils / jobutil.py
1 # Copyright 2016-2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import datetime
15 from functools import reduce
16 import logging
17 import traceback
18 import uuid
19
20 from lcm.pub.database.models import JobStatusModel, JobModel
21 from lcm.pub.enum import JOB_PROGRESS
22 from lcm.pub.enum import JOB_STATUS
23 from lcm.pub.utils import idutil
24
25 logger = logging.getLogger(__name__)
26
27
28 class JobUtil(object):
29     def __init__(self):
30         pass
31
32     @staticmethod
33     def __gen_job_id(job_name):
34         return "%s-%s" % (job_name if job_name else "UnknownJob", uuid.uuid1())
35
36     @staticmethod
37     def query_job_status(job_id, index_id=-1):
38         # logger.info("Query job status, jobid =[%s], responseid [%d]" % (job_id, index_id))
39         jobs = []
40         if index_id < 0:
41             row = JobStatusModel.objects.filter(jobid=job_id).order_by("-indexid").first()
42             if row:
43                 jobs.append(row)
44         else:
45             [jobs.append(job) for job in JobStatusModel.objects.filter(jobid=job_id).order_by("-indexid")
46              if job.indexid > index_id]
47
48         # logger.info("Query job status, rows=%s" % str(jobs))
49         return jobs
50
51     @staticmethod
52     def is_job_exists(job_id):
53         jobs = JobModel.objects.filter(jobid=job_id)
54         return len(jobs) > 0
55
56     @staticmethod
57     def create_job(inst_type, jobaction, inst_id, user='', job_id=None, res_name=''):
58         if job_id is None:
59             job_id = JobUtil.__gen_job_id(
60                 '%s-%s-%s' % (str(inst_type).replace(' ', '_'), str(jobaction).replace(' ', '_'), str(inst_id)))
61         job = JobModel()
62         job.jobid = job_id
63         job.jobtype = inst_type
64         job.jobaction = jobaction
65         job.resid = str(inst_id)
66         job.status = JOB_STATUS.PROCESSING
67         job.user = user
68         job.starttime = datetime.datetime.now().strftime('%Y-%m-%d %X')
69         job.progress = JOB_PROGRESS.STARTED
70         job.resname = res_name
71         logger.debug("create a new job, jobid=%s, jobtype=%s, jobaction=%s, resid=%s, status=%d" %
72                      (job.jobid, job.jobtype, job.jobaction, job.resid, job.status))
73         job.save()
74         return job_id
75
76     @staticmethod
77     def clear_job(job_id):
78         [job.delete() for job in JobModel.objects.filter(jobid=job_id)]
79         logger.debug("Clear job, job_id=%s" % job_id)
80
81     @staticmethod
82     def add_job_status(job_id, progress, status_decs, error_code=""):
83         jobs = JobModel.objects.filter(jobid=job_id)
84         if not jobs:
85             logger.error("Job[%s] is not exists, please create job first." % job_id)
86             raise Exception("Job[%s] is not exists." % job_id)
87         try:
88             int_progress = int(progress)
89             job_status = JobStatusModel()
90             job_status.indexid = int(idutil.get_auto_id(job_id))
91             job_status.jobid = job_id
92             job_status.status = "processing"
93             job_status.progress = int_progress
94
95             if job_status.progress == 0:
96                 job_status.status = "started"
97             elif job_status.progress == 100:
98                 job_status.status = "finished"
99             elif job_status.progress == 101:
100                 job_status.status = "partly_finished"
101             elif job_status.progress > 101:
102                 job_status.status = "error"
103
104             if error_code == "255":
105                 job_status.status = "error"
106
107             job_status.descp = status_decs
108             job_status.errcode = error_code if error_code else "0"
109             job_status.addtime = datetime.datetime.now().strftime('%Y-%m-%d %X')
110             job_status.save()
111             logger.debug("Add a new job status, jobid=%s, indexid=%d,"
112                          " status=%s, description=%s, progress=%d, errcode=%s, addtime=%r" %
113                          (job_status.jobid, job_status.indexid, job_status.status, job_status.descp,
114                           job_status.progress, job_status.errcode, job_status.addtime))
115
116             job = jobs[0]
117             job.progress = int_progress
118             if job_status.progress >= 100:
119                 job.status = JOB_STATUS.FINISH
120                 job.endtime = datetime.datetime.now().strftime('%Y-%m-%d %X')
121             job.save()
122             logger.debug("update job, jobid=%s, progress=%d" % (job_status.jobid, int_progress))
123         except:
124             logger.error(traceback.format_exc())
125
126     @staticmethod
127     def clear_job_status(job_id):
128         [job.delete() for job in JobStatusModel.objects.filter(jobid=job_id)]
129         logger.debug("Clear job status, job_id=%s" % job_id)
130
131     @staticmethod
132     def get_unfinished_jobs(url_prefix, inst_id, inst_type):
133         jobs = JobModel.objects.filter(resid=inst_id, jobtype=inst_type, status=JOB_STATUS.PROCESSING)
134         progresses = reduce(lambda content, job: content + [url_prefix + "/" + job.jobid], jobs, [])
135         return progresses