|
7 | 7 |
|
8 | 8 | from tzlocal import get_localzone |
9 | 9 | from sqlalchemy import desc, text |
10 | | -from faas_scheduler.models import ScriptLog |
| 10 | +from faas_scheduler.models import ( |
| 11 | + ScriptLog, |
| 12 | + UserRunScriptStatistics, |
| 13 | + OrgRunScriptStatistics, |
| 14 | + DTableRunScriptStatistics, |
| 15 | +) |
11 | 16 |
|
12 | 17 | import sys |
13 | 18 |
|
@@ -197,66 +202,134 @@ def call_faas_func(script_url, temp_api_token, context_data, script_id=None): |
197 | 202 | return None |
198 | 203 |
|
199 | 204 |
|
200 | | -def update_statistics(db_session, dtable_uuid, owner, org_id, spend_time): |
201 | | - if not spend_time: |
202 | | - return |
203 | | - username = owner |
204 | | - |
205 | | - # dtable_run_script_statistcis |
206 | | - sqls = [ |
207 | | - """ |
208 | | - INSERT INTO dtable_run_script_statistics(dtable_uuid, run_date, total_run_count, total_run_time, update_at) VALUES |
209 | | - (:dtable_uuid, :run_date, 1, :spend_time, :update_at) |
210 | | - ON DUPLICATE KEY UPDATE |
211 | | - total_run_count=total_run_count+1, |
212 | | - total_run_time=total_run_time+:spend_time, |
213 | | - update_at=:update_at; |
214 | | - """ |
215 | | - ] |
| 205 | +def update_stats_run_count(db_session, dtable_uuid, owner, org_id): |
| 206 | + run_date = datetime.today().strftime("%Y-%m-%d") |
| 207 | + try: |
| 208 | + dtable_stats = ( |
| 209 | + db_session.query(DTableRunScriptStatistics) |
| 210 | + .filter_by(dtable_uuid=dtable_uuid, run_date=run_date) |
| 211 | + .first() |
| 212 | + ) |
| 213 | + if not dtable_stats: |
| 214 | + dtable_stats = DTableRunScriptStatistics( |
| 215 | + dtable_uuid=dtable_uuid, |
| 216 | + run_date=run_date, |
| 217 | + total_run_count=1, |
| 218 | + total_run_time=0, |
| 219 | + update_at=datetime.now(), |
| 220 | + ) |
| 221 | + db_session.add(dtable_stats) |
| 222 | + else: |
| 223 | + dtable_stats.total_run_count += 1 |
| 224 | + dtable_stats.update_at = datetime.now() |
| 225 | + if org_id == -1: |
| 226 | + if "@seafile_group" not in owner: |
| 227 | + user_stats = ( |
| 228 | + db_session.query(UserRunScriptStatistics) |
| 229 | + .filter_by(username=owner, run_date=run_date) |
| 230 | + .first() |
| 231 | + ) |
| 232 | + if not user_stats: |
| 233 | + user_stats = UserRunScriptStatistics( |
| 234 | + username=owner, |
| 235 | + run_date=run_date, |
| 236 | + total_run_count=1, |
| 237 | + total_run_time=0, |
| 238 | + update_at=datetime.now(), |
| 239 | + ) |
| 240 | + db_session.add(user_stats) |
| 241 | + else: |
| 242 | + user_stats.total_run_count += 1 |
| 243 | + user_stats.update_at = datetime.now() |
| 244 | + else: |
| 245 | + org_stats = ( |
| 246 | + db_session.query(OrgRunScriptStatistics) |
| 247 | + .filter_by(org_id=org_id, run_date=run_date) |
| 248 | + .first() |
| 249 | + ) |
| 250 | + if not org_stats: |
| 251 | + org_stats = OrgRunScriptStatistics( |
| 252 | + org_id=org_id, |
| 253 | + run_date=run_date, |
| 254 | + total_run_count=1, |
| 255 | + total_run_time=0, |
| 256 | + update_at=datetime.now(), |
| 257 | + ) |
| 258 | + db_session.add(org_stats) |
| 259 | + else: |
| 260 | + org_stats.total_run_count += 1 |
| 261 | + org_stats.update_at = datetime.now() |
| 262 | + db_session.commit() |
| 263 | + except Exception as e: |
| 264 | + logger.exception( |
| 265 | + f"update stats for org_id {org_id} owner {owner} dtable {dtable_uuid} run count error {e}" |
| 266 | + ) |
216 | 267 |
|
217 | | - # org_run_script_statistics |
218 | | - if org_id and org_id != -1: |
219 | | - sqls += [ |
220 | | - """ |
221 | | - INSERT INTO org_run_script_statistics(org_id, run_date, total_run_count, total_run_time, update_at) VALUES |
222 | | - (:org_id, :run_date, 1, :spend_time, :update_at) |
223 | | - ON DUPLICATE KEY UPDATE |
224 | | - total_run_count=total_run_count+1, |
225 | | - total_run_time=total_run_time+:spend_time, |
226 | | - update_at=:update_at; |
227 | | - """ |
228 | | - ] |
229 | | - |
230 | | - # user_run_script_statistics |
231 | | - if "@seafile_group" not in username: |
232 | | - sqls += [ |
233 | | - """ |
234 | | - INSERT INTO user_run_script_statistics(username, org_id, run_date, total_run_count, total_run_time, update_at) VALUES |
235 | | - (:username, :org_id, :run_date, 1, :spend_time, :update_at) |
236 | | - ON DUPLICATE KEY UPDATE |
237 | | - org_id=:org_id, |
238 | | - total_run_count=total_run_count+1, |
239 | | - total_run_time=total_run_time+:spend_time, |
240 | | - update_at=:update_at; |
241 | | - """ |
242 | | - ] |
243 | 268 |
|
| 269 | +def update_stats_run_time(db_session, dtable_uuid, owner, org_id, spend_time): |
| 270 | + if not spend_time: |
| 271 | + return |
| 272 | + run_date = datetime.today().strftime("%Y-%m-%d") |
244 | 273 | try: |
245 | | - for sql in sqls: |
246 | | - db_session.execute( |
247 | | - text(sql), |
248 | | - { |
249 | | - "dtable_uuid": dtable_uuid, |
250 | | - "username": username, |
251 | | - "org_id": org_id, |
252 | | - "run_date": datetime.today(), |
253 | | - "spend_time": spend_time, |
254 | | - "update_at": datetime.now(), |
255 | | - }, |
| 274 | + dtable_stats = ( |
| 275 | + db_session.query(DTableRunScriptStatistics) |
| 276 | + .filter_by(dtable_uuid=dtable_uuid, run_date=run_date) |
| 277 | + .first() |
| 278 | + ) |
| 279 | + if not dtable_stats: |
| 280 | + dtable_stats = DTableRunScriptStatistics( |
| 281 | + dtable_uuid=dtable_uuid, |
| 282 | + run_date=run_date, |
| 283 | + total_run_count=1, |
| 284 | + total_run_time=spend_time, |
| 285 | + update_at=datetime.now(), |
| 286 | + ) |
| 287 | + db_session.add(dtable_stats) |
| 288 | + else: |
| 289 | + dtable_stats.total_run_time += spend_time |
| 290 | + dtable_stats.update_at = datetime.now() |
| 291 | + if org_id == -1: |
| 292 | + if "@seafile_group" not in owner: |
| 293 | + user_stats = ( |
| 294 | + db_session.query(UserRunScriptStatistics) |
| 295 | + .filter_by(username=owner, run_date=run_date) |
| 296 | + .first() |
| 297 | + ) |
| 298 | + if not user_stats: |
| 299 | + user_stats = UserRunScriptStatistics( |
| 300 | + username=owner, |
| 301 | + run_date=run_date, |
| 302 | + total_run_count=1, |
| 303 | + total_run_time=spend_time, |
| 304 | + update_at=datetime.now(), |
| 305 | + ) |
| 306 | + db_session.add(user_stats) |
| 307 | + else: |
| 308 | + user_stats.total_run_time += spend_time |
| 309 | + user_stats.update_at = datetime.now() |
| 310 | + else: |
| 311 | + org_stats = ( |
| 312 | + db_session.query(OrgRunScriptStatistics) |
| 313 | + .filter_by(org_id=org_id, run_date=run_date) |
| 314 | + .first() |
256 | 315 | ) |
| 316 | + if not org_stats: |
| 317 | + org_stats = OrgRunScriptStatistics( |
| 318 | + org_id=org_id, |
| 319 | + run_date=run_date, |
| 320 | + total_run_count=1, |
| 321 | + total_run_time=spend_time, |
| 322 | + update_at=datetime.now(), |
| 323 | + ) |
| 324 | + db_session.add(org_stats) |
| 325 | + else: |
| 326 | + org_stats.total_run_time += spend_time |
| 327 | + org_stats.update_at = datetime.now() |
257 | 328 | db_session.commit() |
258 | 329 | except Exception as e: |
259 | | - logger.exception("update statistics sql error: %s", e) |
| 330 | + logger.exception( |
| 331 | + f"update stats for org_id {org_id} owner {owner} dtable {dtable_uuid} run time error {e}" |
| 332 | + ) |
260 | 333 |
|
261 | 334 |
|
262 | 335 | # required to get "script logs" in dtable-web |
@@ -387,6 +460,8 @@ def add_script( |
387 | 460 | db_session.add(script) |
388 | 461 | db_session.commit() |
389 | 462 |
|
| 463 | + update_stats_run_count(db_session, dtable_uuid, owner, org_id) |
| 464 | + |
390 | 465 | return script |
391 | 466 |
|
392 | 467 |
|
@@ -435,7 +510,7 @@ def hook_update_script( |
435 | 510 | update_script( |
436 | 511 | db_session, script, success, return_code, output, started_at, finished_at |
437 | 512 | ) |
438 | | - update_statistics( |
| 513 | + update_stats_run_time( |
439 | 514 | db_session, script.dtable_uuid, script.owner, script.org_id, spend_time |
440 | 515 | ) |
441 | 516 |
|
|
0 commit comments