def _get_project_details(project_id):
        """Return a formatted dictionary containing the details of a given Project entry."""
        terms = re.findall(r'[^+ ,;]+', str(project_id))
        for term in terms:
            # Take the first thing that matches standard project id numbering
            if re.match('[0-9]+[a-z]?', term):
                project_id = term
                break
        try:
            project_entry = (Projects.get(Projects.id == project_id))
        except DoesNotExist:
            message = 'No Project with an ID of {0} was found'.format(
                project_id)
            raise HTTPError('404 Not Found', message)

        proj_inst = TransSIP()
        pi_where_clause = proj_inst.where_clause({'project_id': project_id})
        instrument_entries = (Instruments.select(
            Instruments.id, Instruments.display_name, Instruments.name,
            Instruments.name_short, Instruments.active).order_by(
                Instruments.id).join(TransSIP).where(pi_where_clause))
        instruments = {
            i.id: {
                'id': i.id,
                'display_name': i.display_name,
                'name': i.name,
                'name_short': i.name_short,
                'active': i.active
            }
            for i in instrument_entries
        }

        return QueryBase.format_project_block(project_entry, instruments)
Exemple #2
0
    def _get_transaction_info_block(transaction_id, option='details'):
        try:
            transaction_entry = TransSIP()
            where_clause = transaction_entry.where_clause(
                {'_id': transaction_id})
            transaction_info = (TransSIP
                                .select()
                                .where(where_clause)
                                .get())

            transaction_entry = transaction_info.to_hash()
        except DoesNotExist:
            message = 'No Transaction with an ID of {0} was found'.format(
                transaction_id)
            raise HTTPError('404 Not Found', message)

        transaction_metadata = QueryBase._get_base_transaction_metadata(
            transaction_entry, option)

        kv_list = {}
        kvs = QueryBase._get_transaction_key_values(transaction_id)
        for key_value in kvs:
            kv_list.update({key_value['key']: key_value['value']})

        transaction_entry.update(transaction_metadata)
        transaction_entry['key_values'] = kv_list

        return transaction_entry
Exemple #3
0
    def _get_transactions_from_group(instrument_group_id, proposal_id,
                                     start_time, end_time):
        instrument_list = TransactionsMultiSearch._get_instruments_from_group_id(
            instrument_group_id)

        where_clause = Expression(1, OP.EQ, 1)
        # now get the appropriate transactions
        where_clause &= TransSIP().where_clause({
            'updated': start_time,
            'updated_operator': 'gte'
        })
        where_clause &= TransSIP().where_clause({
            'updated': end_time,
            'updated_operator': 'lte'
        })

        if instrument_list:
            where_clause &= (TransSIP.instrument << instrument_list)
        if proposal_id:
            where_clause &= (TransSIP.proposal == proposal_id)
        transactions_list_query = TransSIP.select(
            TransSIP.id).where(where_clause)

        transactions_list = [t['id'] for t in transactions_list_query.dicts()]
        return transactions_list
Exemple #4
0
    def _get_earliest_latest(item_type, item_list, time_basis):
        accepted_item_types = list(
            set(
                list(QueryBase.object_type_mappings.keys()) +
                list(QueryBase.object_type_mappings.values())))
        accepted_time_basis_types = [
            'submitted',
            'modified',
            'created',
            'submit',
            'modified',
            'create',
            'submit_time',
            'modified_time',
            'create_time',
            'submitted_date',
            'modified_date',
            'created_date',
        ]
        item_type = QueryBase.object_type_mappings.get(item_type)
        time_basis = time_basis.lower()
        if item_type not in accepted_item_types or time_basis not in accepted_time_basis_types:
            raise HTTPError('400 Invalid Query')

        short_time_basis = time_basis[:5]

        time_basis = {
            'submi': lambda x: 'submitted',
            'modif': lambda x: 'modified',
            'creat': lambda x: 'created'
        }[short_time_basis](short_time_basis)
        search_field = getattr(TransSIP, '{0}'.format(item_type))
        if time_basis == 'submitted':
            query = TransSIP().select(
                fn.Min(TransSIP.updated).alias('earliest'),
                fn.Max(TransSIP.updated).alias('latest'),
            )
        if time_basis in ['modified', 'created']:
            time_basis_field = getattr(Files, '{0}time'.format(time_basis[:1]))
            query = Files().select(
                fn.Min(time_basis_field).alias('earliest'),
                fn.Max(time_basis_field).alias('latest'),
            ).join(TransSIP, on=(TransSIP.id == Files.transaction))

        query = query.where(search_field << item_list)
        row = query.get()
        if row.earliest is None or row.latest is None:
            message = ''
            raise HTTPError('404 Not Found', message)

        return {
            'earliest': row.earliest.strftime('%Y-%m-%d %H:%M:%S'),
            'latest': row.latest.strftime('%Y-%m-%d %H:%M:%S')
        }
Exemple #5
0
    def _search_transactions(search_terms):
        # build the search query from keyword bits
        trans = TransSIP()
        where_clause = Expression(1, OP.EQ, 1)
        query = trans.select()
        item_count = 100
        page_num = -1
        for term in search_terms:
            value = str(search_terms[term]).replace('+', ' ')
            if term in ['proposal', 'proposal_id'] and value != '-1':
                where_clause &= TransSIP().where_clause({'proposal': value})
                continue
            if term in ['instrument', 'instrument_id'] and value != '-1':
                where_clause &= TransSIP().where_clause({'instrument': value})
                continue
            if term in ['start', 'start_time']:
                where_clause &= TransSIP().where_clause({
                    'updated':
                    value,
                    'updated_operator':
                    'gte'
                })
                continue
            if term in ['end', 'end_time']:
                where_clause &= TransSIP().where_clause({
                    'updated':
                    value,
                    'updated_operator':
                    'lte'
                })
                continue
            if term in [
                    'user', 'user_id', 'person', 'person_id', 'submitter',
                    'submitter_id'
            ] and value != '-1':
                where_clause &= TransSIP().where_clause({'submitter': value})
                continue
            if term in ['transaction_id'] and value != '-1':
                where_clause &= TransSIP().where_clause({'_id': value})
                continue
            if term in ['item_count'] and value != '-1':
                item_count = int(value)
            if term in ['page'] and value != '-1':
                page_num = int(value)
        query = query.where(where_clause)
        total_transaction_count = query.count()
        transaction_search_stats = {
            'total_count': total_transaction_count,
            'items_per_page': item_count,
            'page_num': page_num
        }
        if item_count > 0 and page_num > 0:
            query = query.paginate(page_num, item_count)

        return [t.id for t in query], transaction_search_stats
Exemple #6
0
    def _search_transactions(search_terms):
        # build the search query from keyword bits
        trans = TransSIP()
        where_clause = Expression(1, OP.EQ, 1)
        query = trans.select()
        item_count = 100
        page_num = -1
        offset = -1
        for term in search_terms:
            value = str(search_terms[term]).replace('+', ' ')
            if term in ['project', 'project_id'] and value != '-1':
                where_clause &= TransSIP().where_clause(
                    {'project': value})
                continue
            if term in ['instrument', 'instrument_id'] and value != '-1':
                where_clause &= TransSIP().where_clause(
                    {'instrument': value})
                continue
            if term in ['start', 'start_time']:
                where_clause &= TransSIP().where_clause(
                    {'updated': value, 'updated_operator': 'gte'})
                continue
            if term in ['end', 'end_time']:
                where_clause &= TransSIP().where_clause(
                    {'updated': value, 'updated_operator': 'lte'})
                continue
            if term in ['user', 'user_id', 'person',
                        'person_id', 'submitter', 'submitter_id'] and value != '-1':
                where_clause &= TransSIP().where_clause(
                    {'submitter': value})
                continue
            if term in ['transaction_id'] and value != '-1':
                where_clause &= TransSIP().where_clause({'_id': value})
                continue
            if term in ['item_count'] and value != '-1':
                item_count = int(value)
            if term in ['page'] and value != '-1':
                page_num = int(value)
        query = query.where(where_clause)
        total_transaction_count = query.count()
        if item_count > 0 and (page_num > 0 or offset >= 0):
            offset = item_count * (page_num - 1)
            query = query.limit(item_count).offset(offset)

        query = query.order_by(TransSIP.id.desc())

        transaction_search_stats = {
            'total_count': total_transaction_count,
            'items_per_page': item_count,
            'page': page_num,
            'offset': offset
        }

        return [t.id for t in query], transaction_search_stats
Exemple #7
0
    def get_transaction_date_range_details(start_date, end_date):
        """Return a transaction set grouped on instrument and project for a given time span."""
        # pylint: disable=no-member

        transsip_alias = TransSIP.alias()

        subquery = (transsip_alias.select(
            transsip_alias.id, transsip_alias.project,
            transsip_alias.instrument, transsip_alias.updated).where(
                transsip_alias.updated >= start_date).where(
                    transsip_alias.updated <= end_date).alias('data_subselect')
                    )

        transaction_query = (TransSIP().select(
            fn.Count(TransSIP.id).alias('transaction_count'),
            fn.Min(TransSIP.updated).alias('earliest_upload_date'),
            fn.Max(TransSIP.updated).alias('latest_upload_date'),
            TransSIP.project.alias('project_id'),
            TransSIP.instrument.alias('instrument_id')).join(
                subquery, on=((TransSIP.id == subquery.c.id_id))).group_by(
                    TransSIP.project,
                    TransSIP.instrument).order_by(TransSIP.project,
                                                  TransSIP.instrument))

        # pylint: enable=no-member
        transaction_results = defaultdict(dict)

        for rec in transaction_query.dicts():
            transaction_results[rec['project_id']][rec['instrument_id']] = {
                'transaction_count':
                int(rec['transaction_count']),
                'upload_date_start':
                SummarizeByDate.utc_to_local(
                    rec['earliest_upload_date']).date().strftime('%Y-%m-%d'),
                'upload_date_end':
                SummarizeByDate.utc_to_local(
                    rec['latest_upload_date']).date().strftime('%Y-%m-%d'),
                'project_id':
                rec['project_id'],
                'instrument_id':
                rec['instrument_id']
            }

        return transaction_results
 def _get_transaction_entries(transaction_list):
     # pylint: disable=no-member
     transactions = (TransSIP.select(
         TransSIP,
         fn.Sum(Files.size).alias('file_size_bytes'),
         fn.Count(Files.id).alias('file_count')).join(
             Files, JOIN.LEFT_OUTER,
             on=(TransSIP.id == Files.transaction)).group_by(
                 TransSIP).where(TransSIP.id << transaction_list))
     # pylint: enable=no-member
     return transactions
Exemple #9
0
 def POST():
     """CherryPy GET method."""
     ret_hash = {}
     for project_id in cherrypy.request.json:
         ret_hash[project_id] = []
         instlist = [
             trans.instrument
             for trans in TransSIP.select(TransSIP.instrument).where(
                 TransSIP.project == project_id).distinct()
         ]
         for instrument in instlist:
             data = [
                 x.created for x in TransSIP.select(TransSIP.created).where(
                     (TransSIP.project == project_id)
                     & (TransSIP.instrument == instrument)).order_by(
                         TransSIP.created.desc()).limit(10)
             ]
             ret_hash[project_id].append({
                 'instrument': instrument.id,
                 'end_time': data[0].isoformat(),
                 'start_time': data[-1].isoformat(),
                 'num_results': len(data)
             })
     return ret_hash