This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4_ingest_elk.py
132 lines (103 loc) · 3.26 KB
/
4_ingest_elk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests, json, os
from elasticsearch import Elasticsearch
import configparser
import datetime
import sys
import pandas as pd
import dataset as dataset_lib
config = configparser.ConfigParser()
config.read('./config.ini')
db = dataset_lib.connect("sqlite:///orchestrator.db")
if len(sys.argv) > 1:
daytoprocess = sys.argv[1]
else:
daytoprocess = str(datetime.datetime.today()).split()[0]
url_es = config['ELK']['HOST']
port_es = config['ELK']['PORT']
user_es = config['ELK']['USERNAME']
pwd_es = config['ELK']['PASSWORD']
es = Elasticsearch(
[url_es],
port=port_es,
http_auth=(user_es, pwd_es)
)
cats = ['siret',
'commune',
'date',
'url',
'latitude_wgs',
'json_geojson',
'booleen',
'code_commune_insee',
'latlon_wgs',
'code_departement',
'latitude_l93',
'adresse',
'code_postal',
'tel_fr',
'email',
'year',
'code_region',
'sexe',
'siren',
'code_waldec',
'longitude_l93',
'pays',
'departement',
'uai',
'region',
'iso_country_code',
'date_fr',
'insee_ape700',
'code_rna',
'code_fantoir',
'datetime_iso',
'longitude_wgs',
'insee_canton',
'csp_insee',
'jour_de_la_semaine',
'code_csp_insee',
'twitter']
with open("/srv/datamanufactory/data-workflow/csv-detective-results/"+daytoprocess+".json", "r") as filo:
jsonfile = json.load(filo)
table = db["checks"]
for i in range(len(jsonfile)):
el = []
jsonfile[i][1]['dataset_id'] = jsonfile[i][0].split("---")[0]
jsonfile[i][1]['resource_id'] = jsonfile[i][0].split("---")[1].split('.')[0]
print(i,end=" - ")
print(jsonfile[i][1]['resource_id'])
existing = table.find_one(dataset_id=jsonfile[i][1]['dataset_id'], resource_id=jsonfile[i][1]['resource_id'], order_by='-created_at')
jsonfile[i][1]['title'] = existing['resource_title']
jsonfile[i][1]['resource_url'] = existing['url']
# a corriger :
jsonfile[i][1]['dataset_title'] = existing['dataset_title']
if "columns_ml" in jsonfile[i][1]:
jsonfile[i][1].pop("columns_ml")
print("columns ml removed")
if "columns" in jsonfile[i][1]:
arr2 = []
for key in jsonfile[i][1]['columns']:
arr = []
for m in (range(len(jsonfile[i][1]['columns'][key]))):
if((float(jsonfile[i][1]['columns'][key][m]['score_ml']) < 0.1) & (float(jsonfile[i][1]['columns'][key][m]['score_rb']) < 0.1)):
arr.append(m)
if(len(arr) == len(jsonfile[i][1]['columns'][key])):
arr2.append(key)
else:
arr.sort(reverse=True)
if(arr is not None):
for a in arr:
jsonfile[i][1]['columns'][key].pop(a)
arr2.sort(reverse=True)
if(arr2 is not None):
for a in arr2:
jsonfile[i][1]['columns'].pop(a)
el = jsonfile[i][1]
el['ingested_date'] = daytoprocess
docket_content = json.dumps(el)
print(docket_content)
#es.delete_by_query(index='csvresource', body={"query": {"bool": {"must": [{ "match": { "dataset_id": el['dataset_id']}}, { "match": { "resource_id": el['resource_id']}}]}, "must_not" : [{ "match" : { "ingested_date" : "tototo" }}]}})
es.index(index='csvmetadtgv', ignore=400, body=json.loads(docket_content))
print("------")
print("import into ELK ok")