1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # ==================================================================== # |
---|
5 | # ssh readonly@prodiguer-test-db.ipsl.upmc.fr # |
---|
6 | # psql -U prodiguer_db_user prodiguer # |
---|
7 | # ==================================================================== # |
---|
8 | |
---|
9 | |
---|
10 | # This must come first |
---|
11 | from __future__ import print_function, unicode_literals, division |
---|
12 | |
---|
13 | # Standard library imports |
---|
14 | import os |
---|
15 | import glob |
---|
16 | import psycopg2 |
---|
17 | import psycopg2.extras |
---|
18 | |
---|
19 | # Application library imports |
---|
20 | |
---|
21 | |
---|
22 | ######################################## |
---|
23 | def connect_db(db_host, db_name, db_user): |
---|
24 | |
---|
25 | conn_string = "host={} dbname={} user={}".format( |
---|
26 | db_host, db_name, db_user |
---|
27 | ) |
---|
28 | |
---|
29 | # Print the connection string we will use to connect |
---|
30 | print("Connecting to database\n -> {}".format(conn_string)) |
---|
31 | |
---|
32 | # Get a connection, if a connect cannot be made an exception will be |
---|
33 | # raised here |
---|
34 | try : |
---|
35 | conn = psycopg2.connect(conn_string) |
---|
36 | except Exception as rc: |
---|
37 | print("Problem with DB connect:\n{}".format(rc)) |
---|
38 | exit(1) |
---|
39 | |
---|
40 | # conn.cursor will return a cursor object, you can use this cursor |
---|
41 | # to perform queries |
---|
42 | # cursor = conn.cursor() |
---|
43 | cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) |
---|
44 | print("Connected!\n") |
---|
45 | |
---|
46 | return conn, cursor |
---|
47 | |
---|
48 | |
---|
49 | ######################################## |
---|
50 | def select_db(cursor, request): |
---|
51 | |
---|
52 | # Execute query |
---|
53 | try: |
---|
54 | cursor.execute(request) |
---|
55 | except Exception as rc: |
---|
56 | print("Problem with select:\n{}".format(rc)) |
---|
57 | exit(1) |
---|
58 | |
---|
59 | # # retrieve the records from the database |
---|
60 | # records = cursor.fetchall() |
---|
61 | |
---|
62 | # # print out the records using pretty print |
---|
63 | # # note that the NAMES of the columns are not shown, instead just indexes. |
---|
64 | # # for most people this isn't very useful so we'll show you how to return |
---|
65 | # # columns as a dictionary (hash) in the next example. |
---|
66 | # pprint(records) |
---|
67 | |
---|
68 | |
---|
69 | ######################################## |
---|
70 | def insert_db(cursor, request): |
---|
71 | """ |
---|
72 | """ |
---|
73 | |
---|
74 | # Execute query |
---|
75 | try: |
---|
76 | cursor.execute(request) |
---|
77 | except Exception as rc: |
---|
78 | print("Problem with insert:\n{}".format(rc)) |
---|
79 | exit(1) |
---|
80 | |
---|
81 | |
---|
82 | ######################################## |
---|
83 | def commit_db(conn): |
---|
84 | """ |
---|
85 | """ |
---|
86 | |
---|
87 | # Execute query |
---|
88 | try: |
---|
89 | conn.commit() |
---|
90 | except Exception as rc: |
---|
91 | print("Problem with commit:\n{}".format(rc)) |
---|
92 | exit(1) |
---|
93 | |
---|
94 | |
---|
95 | ######################################## |
---|
96 | def parse_input_cpt(filename, project, center): |
---|
97 | |
---|
98 | if center == "idris": |
---|
99 | return parse_idris_cpt(filename, project) |
---|
100 | elif center == "tgcc": |
---|
101 | return parse_tgcc_cpt(filename, project) |
---|
102 | else: |
---|
103 | print("Unknown center {}".format(center)) |
---|
104 | exit() |
---|
105 | |
---|
106 | |
---|
107 | ######################################## |
---|
108 | def parse_tgcc_cpt(filename, project): |
---|
109 | |
---|
110 | import datetime as dt |
---|
111 | |
---|
112 | consos = set() |
---|
113 | |
---|
114 | with open(filename, "r") as filein: |
---|
115 | # Skip lines until we find project name. |
---|
116 | # Then extract script date |
---|
117 | for ligne in filein: |
---|
118 | if project["name"] in ligne: |
---|
119 | date = ligne.split()[-1] |
---|
120 | date = dt.datetime.strptime( |
---|
121 | "{} {}".format(date), "%d-%m-%Y" |
---|
122 | ) |
---|
123 | project["machine"] = ligne.split()[5] |
---|
124 | project["nodes"] = ligne.split()[6] |
---|
125 | break |
---|
126 | |
---|
127 | # Skip next two lines : first is blank and second is login titles |
---|
128 | for _ in xrange(1): |
---|
129 | next(filein) |
---|
130 | |
---|
131 | # Login list, until blank line |
---|
132 | for ligne in filein: |
---|
133 | if not ligne.strip(): |
---|
134 | break |
---|
135 | login, conso = ligne.split() |
---|
136 | # logins[login] = float(conso) |
---|
137 | try: |
---|
138 | conso = float(conso) |
---|
139 | except ValueError: |
---|
140 | conso = float("nan") |
---|
141 | consos.add((login, conso)) |
---|
142 | |
---|
143 | # Skip until we find consumed time (hours) |
---|
144 | for ligne in filein: |
---|
145 | if "Total" in ligne: |
---|
146 | total = float(ligne.split()[-1]) |
---|
147 | break |
---|
148 | |
---|
149 | # Skip until we find allocated time (hours) |
---|
150 | for ligne in filein: |
---|
151 | if "Allocated" in ligne: |
---|
152 | alloc = float(ligne.split()[-1]) |
---|
153 | break |
---|
154 | |
---|
155 | # Skip until we find theoratical use (%) |
---|
156 | for ligne in filein: |
---|
157 | if "Suggested use at this time" in ligne: |
---|
158 | utheo = float(ligne.split()[-1].strip("%")) |
---|
159 | break |
---|
160 | |
---|
161 | # Skip until we find real use (%) |
---|
162 | for ligne in filein: |
---|
163 | if "Real use at this time" in ligne: |
---|
164 | ureal = float(ligne.split()[-1].strip("%")) |
---|
165 | break |
---|
166 | |
---|
167 | # Skip until we find deadline |
---|
168 | for ligne in filein: |
---|
169 | if "Project deadline" in ligne: |
---|
170 | deadline = ligne.split()[-1] |
---|
171 | break |
---|
172 | |
---|
173 | # return project, logins, today, total, utheo, ureal |
---|
174 | return date, alloc, consos |
---|
175 | |
---|
176 | |
---|
177 | ######################################## |
---|
178 | def parse_idris_cpt(filename, project): |
---|
179 | |
---|
180 | import datetime as dt |
---|
181 | |
---|
182 | # print(filename) |
---|
183 | |
---|
184 | with open(filename, "r") as filein: |
---|
185 | lignes = filein.readlines() |
---|
186 | |
---|
187 | consos = set() |
---|
188 | |
---|
189 | for ligne in lignes: |
---|
190 | if "mise a jour" in ligne: |
---|
191 | jour, heure = ligne.strip().split()[-2:] |
---|
192 | elif "Heures" in ligne: |
---|
193 | alloc = int(ligne.strip().split()[-1]) |
---|
194 | elif "Totaux" in ligne: |
---|
195 | conso = float(ligne.strip().split()[1]) |
---|
196 | consos.add(("total", conso)) |
---|
197 | elif project in ligne: |
---|
198 | login = ligne.strip().split()[0] |
---|
199 | try: |
---|
200 | conso = float(ligne.strip().split()[2]) |
---|
201 | except ValueError: |
---|
202 | conso = float("nan") |
---|
203 | consos.add((login, conso)) |
---|
204 | |
---|
205 | date = dt.datetime.strptime( |
---|
206 | "{} {}".format(jour, heure), "%d/%m/%Y %H:%M" |
---|
207 | ) |
---|
208 | |
---|
209 | # return jour, heure, alloc, consos |
---|
210 | return date, alloc, consos |
---|
211 | |
---|
212 | |
---|
213 | ######################################## |
---|
214 | def find_input_files(data_dir, pattern, date_range): |
---|
215 | """ |
---|
216 | """ |
---|
217 | # os.chdir(DATA_DIR) |
---|
218 | # pattern = "compta_*.dat" |
---|
219 | file_list = glob.glob(os.path.join(data_dir, pattern)) |
---|
220 | # if file_list: |
---|
221 | # res = sorted(file_list) |
---|
222 | # else: |
---|
223 | # res = None |
---|
224 | # os.chdir(SUBMIT_DIR) |
---|
225 | |
---|
226 | if date_range: |
---|
227 | res = [] |
---|
228 | for filename in file_list: |
---|
229 | file_date = os.path.basename(filename).split("_")[1] |
---|
230 | if file_date >= min(date_range) and \ |
---|
231 | file_date <= max(date_range): |
---|
232 | res.append(filename) |
---|
233 | else: |
---|
234 | res = file_list |
---|
235 | |
---|
236 | return set(res) |
---|
237 | |
---|
238 | |
---|
239 | ######################################## |
---|
240 | if __name__ == "__main__": |
---|
241 | pass |
---|