|
| 1 | +import sys |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import yaml |
| 5 | +import json |
| 6 | + |
| 7 | +from psycopg2 import connect |
| 8 | +from psycopg2.extras import RealDictCursor |
| 9 | +from elasticsearch import Elasticsearch, helpers |
| 10 | + |
| 11 | +from reporting import count_from_db, data_from_db, get_extras, data_to_es |
| 12 | + |
| 13 | + |
| 14 | +def init_es_index(index_name): |
| 15 | + |
| 16 | + with open("{}.json".format(index_name)) as f: |
| 17 | + config = json.load(f) |
| 18 | + |
| 19 | + es.indices.delete( |
| 20 | + index=index_name, |
| 21 | + ignore_unavailable=True |
| 22 | + ) |
| 23 | + |
| 24 | + es.indices.create( |
| 25 | + index=index_name, |
| 26 | + body=config |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +parser = argparse.ArgumentParser() |
| 31 | +parser.add_argument( |
| 32 | + "--config", |
| 33 | + default="settings.yaml", |
| 34 | + help="Config file location." |
| 35 | +) |
| 36 | +parser.add_argument( |
| 37 | + "--days", |
| 38 | + type=int, |
| 39 | + default=1, |
| 40 | + help="Populate past days of data." |
| 41 | +) |
| 42 | +parser.add_argument( |
| 43 | + "--rebuild", |
| 44 | + action="store_true", |
| 45 | + help="Delete and create index." |
| 46 | +) |
| 47 | + |
| 48 | +args = parser.parse_args() |
| 49 | + |
| 50 | +if os.path.isfile(args.config): |
| 51 | + with open(args.config) as f: |
| 52 | + settings = yaml.load(f, Loader=yaml.Loader) |
| 53 | +else: |
| 54 | + sys.exit("Can't find settings.") |
| 55 | + |
| 56 | +try: |
| 57 | + con = connect( |
| 58 | + host=settings["database"]["host"], |
| 59 | + port=settings["database"]["port"], |
| 60 | + user=settings["database"]["username"], |
| 61 | + password=settings["database"]["password"], |
| 62 | + database=settings["database"]["database"] |
| 63 | + ) |
| 64 | +except Exception: |
| 65 | + sys.exit("Can't connect to the database.") |
| 66 | + |
| 67 | +try: |
| 68 | + es_host = "{}:{}".format( |
| 69 | + settings["elasticsearch"]["host"], |
| 70 | + settings["elasticsearch"]["port"] |
| 71 | + ) |
| 72 | + es = Elasticsearch([es_host]) |
| 73 | +except Exception: |
| 74 | + con.close() |
| 75 | + sys.exit("Can't connect to the Elasticsearch.") |
| 76 | + |
| 77 | +cur = con.cursor(cursor_factory=RealDictCursor) |
| 78 | + |
| 79 | +if args.rebuild: |
| 80 | + print("Rebuild index.") |
| 81 | + init_es_index(settings["index"]["name"]) |
| 82 | + |
| 83 | +start = 0 |
| 84 | +to_go = 1 |
| 85 | +cache = {} |
| 86 | + |
| 87 | +while to_go > 0: |
| 88 | + |
| 89 | + to_go = count_from_db(cur, args.days, start) |
| 90 | + print( |
| 91 | + "{:,} datafileobjects to index, {:,} datasets cached" |
| 92 | + .format(to_go, len(cache)) |
| 93 | + ) |
| 94 | + |
| 95 | + if to_go > 0: |
| 96 | + |
| 97 | + rows = data_from_db(cur, args.days, start, settings["index"]["limit"]) |
| 98 | + |
| 99 | + dataset_ids = list(set([row["dataset_id"] for row in rows])) |
| 100 | + extra_ds_ids = [] |
| 101 | + for ds_id in dataset_ids: |
| 102 | + if ds_id not in cache: |
| 103 | + extra_ds_ids.append(ds_id) |
| 104 | + if len(extra_ds_ids) != 0: |
| 105 | + extras = get_extras(cur, extra_ds_ids) |
| 106 | + for k in extras: |
| 107 | + cache[k] = extras[k] |
| 108 | + |
| 109 | + data = [] |
| 110 | + for row in rows: |
| 111 | + if row["dfo_id"] > start: |
| 112 | + start = row["dfo_id"] |
| 113 | + ds_id = row["dataset_id"] |
| 114 | + if ds_id in cache: |
| 115 | + data.append({**row, **cache[ds_id]}) |
| 116 | + else: |
| 117 | + data.append(row) |
| 118 | + |
| 119 | + helpers.bulk(es, data_to_es(settings["index"]["name"], data)) |
| 120 | + |
| 121 | +print("Completed.") |
| 122 | +cur.close() |
| 123 | +con.close() |
0 commit comments