# Path: QueueDB/models.py
# class Training(models.Model):
#     """Training Table
#     Save training items
#     """
#     step_hash = models.CharField(max_length=50, db_index=True)
#     input = models.CharField(max_length=50, blank=True, null=True)
#     output = models.CharField(max_length=50, blank=True, null=True)
#     mem = models.CharField(max_length=50, blank=True, null=True)
#     vrt_mem = models.CharField(max_length=50, blank=True, null=True)
#     cpu = models.CharField(max_length=50, blank=True, null=True)
#     create_time = models.DateTimeField(auto_now_add=True)
#     lock = models.SmallIntegerField(default=1)
# 
#     def __str__(self):
#         return self.step_hash
# 
#     def step_name(self):
#         steps = Step.objects.filter(hash=self.step_hash)
#         step_key = None
#         if steps:
#             step_key = steps[0]
#         if step_key:
#             return step_key.software + ' ' + step_key.parameter
#         else:
#             return 'Step missing'
# 
#     def update_cpu_mem(self, cpu, mem, vrt_mem):
#         self.mem = mem
#         self.cpu = cpu
#         self.vrt_mem = vrt_mem
#         self.save()
# 
#     def mem_in_gb(self):
#         if self.mem:
#             try:
#                 return str(round(float(self.mem) / 1024 / 1024 / 1024, 2)) + 'GB'
#             except:
#                 return ''
#         else:
#             return '-'
# 
#     def vrt_mem_in_gb(self):
#         if self.vrt_mem:
#             try:
#                 return str(round(float(self.vrt_mem) / 1024 / 1024 / 1024, 2)) + 'GB'
#             except:
#                 return ''
#         else:
#             return '-'
# 
#     step_name.admin_order_field = 'step'


import psutil
import time
import getopt
import sys
import django_initial
from QueueDB.models import Training
        while True:

            if process_id in psutil.pids():
                process_info = psutil.Process(process_id)

                if process_info.is_running():
                    try:
                        total_memory_usage, vrt = get_mem(process_id)
                        total_cpu_usage = get_cpu(process_id)
                        children = process_info.children()
                        for child in children:
                            t1, t2 = get_mem(child.pid)
                            total_memory_usage += t1
                            vrt += t2
                            total_cpu_usage += get_cpu(child.pid)
                        mem_list.append(total_memory_usage)
                        vrt_mem_list.append(vrt)
                        cpu_list.append(total_cpu_usage)
                        time.sleep(30)
                    except Exception as e:
                        print(e)
                        break
                else:
                    break
            else:
                break

        cpu_usage, mem_usage, vrt_mem_usage = get_cpu_mem(cpu_list, mem_list, vrt_mem_list)

        try:

# Path: QueueDB/models.py
# class Training(models.Model):
#     """Training Table
#     Save training items
#     """
#     step_hash = models.CharField(max_length=50, db_index=True)
#     input = models.CharField(max_length=50, blank=True, null=True)
#     output = models.CharField(max_length=50, blank=True, null=True)
#     mem = models.CharField(max_length=50, blank=True, null=True)
#     vrt_mem = models.CharField(max_length=50, blank=True, null=True)
#     cpu = models.CharField(max_length=50, blank=True, null=True)
#     create_time = models.DateTimeField(auto_now_add=True)
#     lock = models.SmallIntegerField(default=1)
# 
#     def __str__(self):
#         return self.step_hash
# 
#     def step_name(self):
#         steps = Step.objects.filter(hash=self.step_hash)
#         step_key = None
#         if steps:
#             step_key = steps[0]
#         if step_key:
#             return step_key.software + ' ' + step_key.parameter
#         else:
#             return 'Step missing'
# 
#     def update_cpu_mem(self, cpu, mem, vrt_mem):
#         self.mem = mem
#         self.cpu = cpu
#         self.vrt_mem = vrt_mem
#         self.save()
# 
#     def mem_in_gb(self):
#         if self.mem:
#             try:
#                 return str(round(float(self.mem) / 1024 / 1024 / 1024, 2)) + 'GB'
#             except:
#                 return ''
#         else:
#             return '-'
# 
#     def vrt_mem_in_gb(self):
#         if self.vrt_mem:
#             try:
#                 return str(round(float(self.vrt_mem) / 1024 / 1024 / 1024, 2)) + 'GB'
#             except:
#                 return ''
#         else:
#             return '-'
# 
#     step_name.admin_order_field = 'step'


# Path: worker/ml_collector.py
import psutil
import time
import getopt
import sys
import django_initial
from QueueDB.models import Training

        while True:

            if process_id in psutil.pids():
                process_info = psutil.Process(process_id)

                if process_info.is_running():
                    try:
                        total_memory_usage, vrt = get_mem(process_id)
                        total_cpu_usage = get_cpu(process_id)
                        children = process_info.children()
                        for child in children:
                            t1, t2 = get_mem(child.pid)
                            total_memory_usage += t1
                            vrt += t2
                            total_cpu_usage += get_cpu(child.pid)
                        mem_list.append(total_memory_usage)
                        vrt_mem_list.append(vrt)
                        cpu_list.append(total_cpu_usage)
                        time.sleep(30)
                    except Exception as e:
                        print(e)
                        break
                else:
                    break
            else:
                break

        cpu_usage, mem_usage, vrt_mem_usage = get_cpu_mem(cpu_list, mem_list, vrt_mem_list)

        try:

            training_item = Training.objects.get(id=job_id)