<commit_before>"""
Old JobManager / container module. Likely to go away since using database for jobs.
"""

from cloudscheduler.job import Job

# We need to decide on logic for deciding if a job has been scheduled or not yet
# Unsure if we can rely on the metadata from the job but right now it just defaults to not scheduled

class JobManager:

    """
    Job Manager Class to assist with scheduling jobs.
    """
    def __init__(self):
        """
        Init for job manager.
        """

        self.unscheduled_jobs = {}
        self.unscheduled_jobs_by_user = {}
        self.scheduled_jobs = {}
        self.scheduled_jobs_by_user = {}

        self.user_list = list()


    # accepts a group of job dictionaries and shuffles them into the existing
    #  dictionaries based on their GlobalJobIds.
    def update_jobs(self, jobs):
        """
        update job states.
        :param jobs: list of jobs from condor?
        """
        redis_key_list = []
        for job in jobs:
            j = Job(**job)
            redis_key_list.append(j.GlobalJobId)
            if j.GlobalJobId in self.unscheduled_jobs:
                #update unscheduled entry
                self.unscheduled_jobs[j.GlobalJobId] = j
                self.unscheduled_jobs_by_user[j.User][j.GlobalJobId] = j

            elif j.GlobalJobId in self.scheduled_jobs:
                #update scheduled entry
                j.set_state(1)
                self.scheduled_jobs[j.GlobalJobId] = j
                self.scheduled_jobs_by_user[j.User][j.GlobalJobId] = j

            else:
                #brand new job, insert into unscheduled dicts
                self.unscheduled_jobs[j.GlobalJobId] = j
                self.unscheduled_jobs_by_user[j.User][j.GlobalJobId] = j
        # Clean up jobs that were not in the redis store
        for key in self.scheduled_jobs:
            if key not in redis_key_list:
                del self.scheduled_jobs[key]
                del self.scheduled_jobs_by_user[key]
        for key in self.unscheduled_jobs:
            if key not in redis_key_list:
                del self.unscheduled_jobs[key]
                del self.unscheduled_jobs_by_user[key]


    def schedule_job(self, jobid):
        """
        Mark a job as scheduled.
        :param jobid: id of job to mark
        """
        self.unscheduled_jobs[jobid].set_state(1)
        job = self.unscheduled_jobs[jobid]
        self.scheduled_jobs[jobid] = job
        self.scheduled_jobs_by_user[job.User][jobid] = job
        del self.unscheduled_jobs[jobid]
        del self.unscheduled_jobs_by_user[job.User][jobid]


    def unschedule_job(self, jobid):
        """
        Unmark a previously scheduled job.
        :param jobid: id of job to unmark
        """
        self.scheduled_jobs[jobid].set_state(0)
        job = self.scheduled_jobs[jobid]
        self.unscheduled_jobs[jobid] = job
        self.unscheduled_jobs_by_user[job.User][jobid] = job
        del self.scheduled_jobs[jobid]
        del self.scheduled_jobs_by_user[job.User][jobid]

    def get_jobs_user(self, user):
        """
        Fetch the jobs for specified user.
        :param user: user to get jobs for.
        :return: list of jobs belonging to user or None.
        """
        unsched = self.unscheduled_jobs_by_user[user]
        sched = self.scheduled_jobs_by_user[user]
        # python 3 expression for merging 2 dicts
        user_jobs = {**unsched, **sched}
        return user_jobs

    def get_scheduled_jobs_user(self, user):
        """
        Get the scheduled jobs for user.<commit_msg>naming fixes<commit_after>"""
Old JobManager / container module. Likely to go away since using database for jobs.
"""

from cloudscheduler.job import Job

# We need to decide on logic for deciding if a job has been scheduled or not yet
# Unsure if we can rely on the metadata from the job but right now it just defaults to not scheduled

class JobManager:

    """
    Job Manager Class to assist with scheduling jobs.
    """
    def __init__(self):
        """
        Init for job manager.
        """

        self.unscheduled_jobs = {}
        self.unscheduled_jobs_by_user = {}
        self.scheduled_jobs = {}
        self.scheduled_jobs_by_user = {}

        self.user_list = list()


    # accepts a group of job dictionaries and shuffles them into the existing
    #  dictionaries based on their globaljobids.
    def update_jobs(self, jobs):
        """
        update job states.
        :param jobs: list of jobs from condor?
        """
        redis_key_list = []
        for job in jobs:
            j = Job(**job)
            redis_key_list.append(j.globaljobid)
            if j.globaljobid in self.unscheduled_jobs:
                #update unscheduled entry
                self.unscheduled_jobs[j.globaljobid] = j
                self.unscheduled_jobs_by_user[j.user][j.globaljobid] = j

            elif j.globaljobid in self.scheduled_jobs:
                #update scheduled entry
                j.set_state(1)
                self.scheduled_jobs[j.globaljobid] = j
                self.scheduled_jobs_by_user[j.user][j.globaljobid] = j

            else:
                #brand new job, insert into unscheduled dicts
                self.unscheduled_jobs[j.globaljobid] = j
                self.unscheduled_jobs_by_user[j.user][j.globaljobid] = j
        # Clean up jobs that were not in the redis store
        for key in self.scheduled_jobs:
            if key not in redis_key_list:
                del self.scheduled_jobs[key]
                del self.scheduled_jobs_by_user[key]
        for key in self.unscheduled_jobs:
            if key not in redis_key_list:
                del self.unscheduled_jobs[key]
                del self.unscheduled_jobs_by_user[key]


    def schedule_job(self, jobid):
        """
        Mark a job as scheduled.
        :param jobid: id of job to mark
        """
        self.unscheduled_jobs[jobid].set_state(1)
        job = self.unscheduled_jobs[jobid]
        self.scheduled_jobs[jobid] = job
        self.scheduled_jobs_by_user[job.user][jobid] = job
        del self.unscheduled_jobs[jobid]
        del self.unscheduled_jobs_by_user[job.user][jobid]


    def unschedule_job(self, jobid):
        """
        Unmark a previously scheduled job.
        :param jobid: id of job to unmark
        """
        self.scheduled_jobs[jobid].set_state(0)
        job = self.scheduled_jobs[jobid]
        self.unscheduled_jobs[jobid] = job
        self.unscheduled_jobs_by_user[job.user][jobid] = job
        del self.scheduled_jobs[jobid]
        del self.scheduled_jobs_by_user[job.user][jobid]

    def get_jobs_user(self, user):
        """
        Fetch the jobs for specified user.
        :param user: user to get jobs for.
        :return: list of jobs belonging to user or None.
        """
        unsched = self.unscheduled_jobs_by_user[user]
        sched = self.scheduled_jobs_by_user[user]
        # python 3 expression for merging 2 dicts
        user_jobs = {**unsched, **sched}
        return user_jobs

    def get_scheduled_jobs_user(self, user):
        """
        Get the scheduled jobs for user.
{'fraction_non_alphanumeric': 0.07906132141360525, 'fraction_numerical': 0.0013968431345159939, 'mean_word_length': 2.075601374570447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '94567', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2125, 'n_words': 576}