| 1 | from builtins import str |
| 2 | from builtins import object |
| 3 | import logging |
| 4 | import copy |
| 5 | from elasticsearch import Elasticsearch |
| 6 | |
| 7 | |
| 8 | class BmajIndex(object): |
| 9 | """ |
| 10 | ElasticSearch indexation and search |
| 11 | """ |
| 12 | |
| 13 | """ |
| 14 | ElasticSearch server |
| 15 | """ |
| 16 | es = None |
| 17 | |
| 18 | """ |
| 19 | Index name |
| 20 | """ |
| 21 | index = 'biomaj' |
| 22 | |
| 23 | """ |
| 24 | Do indexing |
| 25 | """ |
| 26 | do_index = False |
| 27 | |
| 28 | """ |
| 29 | Skip if failure (tests) |
| 30 | """ |
| 31 | skip_if_failure = False |
| 32 | |
| 33 | @staticmethod |
| 34 | def load(hosts=None, index='biomaj', do_index=True): |
| 35 | """ |
| 36 | Initialize index |
| 37 | |
| 38 | :param hosts: List of elastic search nodes to connect to |
| 39 | :type hosts: list |
| 40 | :param do_index: index data or not |
| 41 | :type do_index: bool |
| 42 | """ |
| 43 | if hosts is None: |
| 44 | hosts = ['localhost'] |
| 45 | if not do_index: |
| 46 | return |
| 47 | BmajIndex.index = index |
| 48 | BmajIndex.do_index = do_index |
| 49 | if BmajIndex.es is None: |
| 50 | BmajIndex.es = Elasticsearch(hosts) |
| 51 | |
| 52 | mapping = { |
| 53 | "mappings": { |
| 54 | "production": { |
| 55 | "date_detection": False |
| 56 | }, |
| 57 | "releasestats": { |
| 58 | "date_detection": False, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | try: |
| 63 | if not BmajIndex.es.indices.exists(index=BmajIndex.index): |
| 64 | BmajIndex.es.indices.create(index=BmajIndex.index, body=mapping) |
| 65 | except Exception as e: |
| 66 | logging.error('ElasticSearch connection error, check server is running and configuration: ' + str(e)) |
| 67 | |
| 68 | @staticmethod |
| 69 | def _bulk_delete(query, flush=True): |
| 70 | try: |
| 71 | page = BmajIndex.es.search( |
| 72 | index=BmajIndex.index, |
| 73 | doc_type='production', |
| 74 | search_type="query_then_fetch", |
| 75 | size=1000, |
| 76 | body={ |
| 77 | 'query': { |
| 78 | 'match': { |
| 79 | 'bank': query['bank'] |
| 80 | } |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | if page is None: |
| 85 | return |
| 86 | bulk_delete = '' |
| 87 | for del_hit in page['hits']['hits']: |
| 88 | if ('release' in query and query['release'] == del_hit['_source']['release']) or 'release' not in query: |
| 89 | bulk_delete += "{ \"delete\" : {\"_index\":\"" + BmajIndex.index + "\",\"_type\":\"production\", \"_id\" : \"" + del_hit['_id'] + "\" } }\n" |
| 90 | if bulk_delete: |
| 91 | BmajIndex.es.bulk(body=bulk_delete) |
| 92 | if flush: |
| 93 | BmajIndex.es.indices.flush(index=BmajIndex.index, force=True) |
| 94 | except Exception as e: |
| 95 | if BmajIndex.skip_if_failure: |
| 96 | BmajIndex.do_index = False |
| 97 | else: |
| 98 | logging.error('Failed to delete bank index: ' + str(e)) |
| 99 | |
| 100 | @staticmethod |
| 101 | def delete_all_bank(bank_name): |
| 102 | """ |
| 103 | Delete complete index for a bank |
| 104 | """ |
| 105 | if not BmajIndex.do_index: |
| 106 | return |
| 107 | BmajIndex._bulk_delete({"bank": bank_name}, True) |
| 108 | |
| 109 | @staticmethod |
| 110 | def remove(bank_name, release): |
| 111 | """ |
| 112 | Remove a production release |
| 113 | |
| 114 | :param bank_name: Name of the bank |
| 115 | :type bank_name: str |
| 116 | :param release: production release |
| 117 | :type release: str |
| 118 | """ |
| 119 | if not BmajIndex.do_index: |
| 120 | return |
| 121 | BmajIndex._bulk_delete({"release": release, "bank": bank_name}) |
| 122 | """ |
| 123 | try: |
| 124 | query = { |
| 125 | "query" : { |
| 126 | "term" : {"release" : release, "bank": bank_name} |
| 127 | } |
| 128 | } |
| 129 | BmajIndex.es.delete_by_query(index=BmajIndex.index, body=query) |
| 130 | except Exception as e: |
| 131 | logging.error('Index:Remove:'+bank_name+'_'+str(release)+':Exception:'+str(e)) |
| 132 | if BmajIndex.skip_if_failure: |
| 133 | BmajIndex.do_index = False |
| 134 | """ |
| 135 | |
| 136 | @staticmethod |
| 137 | def search(query): |
| 138 | if not BmajIndex.do_index: |
| 139 | return None |
| 140 | res = BmajIndex.es.search(index=BmajIndex.index, |
| 141 | doc_type='production', |
| 142 | search_type="query_then_fetch", |
| 143 | body=query) |
| 144 | return res['hits']['hits'] |
| 145 | |
| 146 | @staticmethod |
| 147 | def searchq(query, size=1000): |
| 148 | """ |
| 149 | Lucene syntax search |
| 150 | |
| 151 | :param query: Lucene search string |
| 152 | :type query: str |
| 153 | :param size: number of results |
| 154 | :type size: int |
| 155 | :return: list of matches |
| 156 | """ |
| 157 | if not BmajIndex.do_index: |
| 158 | return None |
| 159 | res = BmajIndex.es.search(index=BmajIndex.index, doc_type='production', q=query, size=size) |
| 160 | return res['hits']['hits'] |
| 161 | |
| 162 | @staticmethod |
| 163 | def add_stat(stat_id, stat): |
| 164 | """ |
| 165 | Add some statistics, must contain release and bank properties. |
| 166 | """ |
| 167 | if not BmajIndex.do_index: |
| 168 | return |
| 169 | if stat['release'] is None or stat['bank'] is None: |
| 170 | return False |
| 171 | try: |
| 172 | BmajIndex.es.index(index=BmajIndex.index, doc_type='releasestats', id=stat_id, body=stat) |
| 173 | except Exception: |
| 174 | if BmajIndex.skip_if_failure: |
| 175 | BmajIndex.do_index = False |
| 176 | else: |
| 177 | return False |
| 178 | return True |
| 179 | |
| 180 | @staticmethod |
| 181 | def add(bank_name, prod, flush=False): |
| 182 | """ |
| 183 | Index a production release |
| 184 | |
| 185 | :param bank_name: Name of the bank |
| 186 | :type bank_name: str |
| 187 | :param prod: session release object |
| 188 | :type prod: dict |
| 189 | :param flush: Force flushing |
| 190 | :type flush: bool |
| 191 | """ |
| 192 | if not BmajIndex.do_index: |
| 193 | return |
| 194 | obj = copy.deepcopy(prod) |
| 195 | if obj['release'] is None: |
| 196 | return |
| 197 | obj['bank'] = bank_name |
| 198 | formats = obj['formats'] |
| 199 | try: |
| 200 | for fkey, fvalue in formats.items(): |
| 201 | for elt in fvalue: |
| 202 | elt['format'] = fkey |
| 203 | elt['bank'] = bank_name |
| 204 | elt['release'] = obj['release'] |
| 205 | if 'status' in obj: |
| 206 | elt['status'] = obj['status'] |
| 207 | BmajIndex.es.index(index=BmajIndex.index, doc_type='production', body=elt) |
| 208 | if flush: |
| 209 | BmajIndex.es.indices.flush(index=BmajIndex.index, force=True) |
| 210 | except Exception as e: |
| 211 | logging.error('Index:Add:' + bank_name + '_' + str(obj['release']) + ':Exception:' + str(e)) |
| 212 | if BmajIndex.skip_if_failure: |
| 213 | BmajIndex.do_index = False |