Exemplo n.º 1
0
def fill_response_with_indexs(replace_indexes, references, response):
    named_tuple = lightweight_named_tuple('result', list(response[0]._real_fields))

    for index, sqlobj in enumerate(response):
        new_sqlobj = list(sqlobj)
        for attribute, indexes in replace_indexes.items():
            for i in indexes:
                value = getattr(sqlobj, attribute)
                new_sqlobj[i] = references[attribute][value]

        response[index] = named_tuple(new_sqlobj)
Exemplo n.º 2
0
def new_lightweight_named_tuple(response, *new_fields):
    return lightweight_named_tuple('result', response._real_fields + tuple(new_fields))
Exemplo n.º 3
0
    def get_active_jobs(self, application_names=None, attributes=None, order_by=None):
        jobs = {}
        application_names = maybe_list(application_names)
        for domain_name in self.domain_names:
            domain_info = self.config.cache.get(JOBS_REPORT_PATTERN % domain_name, expire=None)
            if not domain_info:
                continue

            for name, info in domain_info.items():
                application_name = get_job_string_application_name(name)
                if not application_names or application_name in application_names:
                    job_info = jobs.get(name)
                    if not job_info:
                        jobs[name] = job_info = {}
                        apijob = get_job(name)
                        if not apijob:
                            continue

                        job_info['key'] = name
                        job_info['application_name'] = application_name
                        job_info['description'] = apijob.title

                    info_next = from_timestamp(info['next'])
                    if info_next:
                        added_info_next = job_info.get('next_date')
                        if not added_info_next or added_info_next > info_next:
                            job_info['next_date'] = info_next

                    info_start = from_timestamp(info['start'])
                    if not job_info.get('start_date') or info_start < job_info['start_date']:
                        job_info['start_date'] = info_start

                    called = job_info.setdefault('called', [])
                    if info.get('called'):
                        called.extend(from_timestamp(d) for d in info['called'])
                        called.sort()
                    job_info['called_length'] = len(called)

                    if called:
                        last_date = job_info.get('last_date')
                        if not last_date:
                            job_info['last_date'] = called[-1]
                        elif called[-1] > last_date:
                            job_info['last_date'] = called[-1]

                    if not job_info.get('active'):
                        job_info['active'] = info['active']

        # Give SQLAlchemy like response
        response = []
        attributes = tuple(maybe_list(attributes) or ('application_name', ))
        for info in jobs.values():
            response.append(
                lightweight_named_tuple('result', attributes)
                (tuple(info.get(key) for key in attributes)))

        if order_by:
            column = order_by.column_name
            sort_with_none(response, key=column, reverse=order_by.descendant)
        else:
            sort_with_none(response, key='description')

        return response