from __future__ import absolute_import from celery.task import Task from common.models import * from utils.Logger import setupLogger from django.conf import settings import subprocess import os ''' Gets the folder path and the plan id it makes an analysis of the calibrated images (in folder "calibrated") ''' class Analysis(Task): logger = setupLogger("analysis", "analysis") def log(self, message: str) -> int: if settings.DEBUG: self.logger.info(message) return 0 def logDB(self, message: str) -> int: Log.objects.create(agent='Analyzer', message=message) return 0 def execProcess(self, command: str) -> int: self.process = subprocess.Popen(command, shell=True) return 0 def start(self, plan_id: int, folder: str) -> int: self.plan_id = plan_id self.folder = folder self.path_dir_file = os.path.dirname(os.path.realpath(__file__)) try: self.plan = Plan.objects.get(id=self.plan_id) except: self.log("analysis not found") self.logDB(message="Plan with id %d not found"%(self.plan_id)) return 1 self.dir_name = self.folder + os.sep + self.plan.name + "_" + str(self.plan_id) + os.sep + "analysis" self.plan_folder = self.plan.name + "_" + str(self.plan_id) if self.createDirectory(): self.log("analysis err dir") self.logDB(message="Could not create the directory for analysis") return 1 self.log("analysis start") return self.logDB("Starting analysis for plan " + self.plan.name) def createDirectory(self): try: if not os.path.isdir(self.dir_name): os.makedirs(self.dir_name) except: return 1 return 0 def changeDirectory(self, path: str): os.chdir(path) return 0 def execute(self) -> int: self.changeDirectory(self.path_dir_file) return self.execProcess("./analyzer '"+str(self.folder)+"' '"+self.plan_folder+"' '"+str(self.plan.duration) + "'") def end(self) -> int: self.process.wait() if self.process.returncode == 0: self.log("analysis executed") self.logDB(message="Analysis executed successfully for plan " + str(self.plan.name)) else: self.log("analysis failed") self.logDB(message="Could not make an analysis for plan " + str(self.plan.name)) return self.process.returncode def run(self, plan_id, folder): if self.start(plan_id, folder): return 1 self.execute() self.end() return 0