#!/usr/bin/env python
#pylint: disable=C0103
Scheduling algo.
from AsyncStageOut.SchedPlugins.Algo import Algo
import time
import datetime
import logging
def fifo_algo(user_by_start):
    FIFO algo.
    sorted_users = []
    for i in range(1, len(user_by_start)+1):
        min = user_by_start.keys()[0]
        for u in user_by_start:
            if u < min:
                min = u
        del user_by_start[min]
    return sorted_users
def priority_algo(fifo_sort_users, priority_users):
    Priority applied to the FIFO list.
    sorted_list = []
    for u in fifo_sort_users:
        if u in priority_users:
    priority_sort_list = sorted_list
    for user in fifo_sort_users:
        if user not in sorted_list:
    return priority_sort_list
class FIFOPriority(Algo):
    FIFOPriority plugins to schedule transfers.
    def __init__(self, config, logger, users, pool_size):
        Initialise class members
        Algo.__init__(self, config, logger, users, pool_size)
        self.logger.debug('Connected to files database')
    def __call__(self):
        Get the result of viewSource from central_monitoring db.
        start_by_user = {}
        user_time = 0
        for u in self.users:
            user = u['key']
            end = user.append({})
            query = {'limit' : 1, 'descending': True, 'startkey':u['key'], 'endkey':end}
                UserByStartTime = self.db.loadView('AsyncTransfer', 'UserByStartTime', query)['rows'][0]['key']
                return []
            self.logger.debug( 'User %s and start time %s' % (u, UserByStartTime[3:]) )
            user_time = int(time.mktime(time.strptime(\
                                        str(UserByStartTime[3]), '%Y-%m-%d %H:%M:%S'))) \
                                        - time.timezone
            start_by_user[user_time] = u['key'][:3]
        self.logger.debug('Start by user %s' %start_by_user)
        fifo_sort_users = fifo_algo(start_by_user)
        query = {}
            users = self.config_db.loadView('asynctransfer_config', 'GetHighPriorityUsers', query)
            return []
        def keys_map(inputDict):
            Map function.
            return inputDict['key']
        priority_users = map(keys_map, users['rows'])
        priority_sort_users = priority_algo(fifo_sort_users, priority_users)
        self.logger.debug('Final list %s' %priority_sort_users)
        return priority_sort_users