Index: trunk/fundraiser-statistics/fundraiser-scripts/classes/DataLoader.py |
— | — | @@ -16,24 +16,32 @@ |
17 | 17 | |
18 | 18 | |
19 | 19 | import sys |
20 | | -sys.path.append('../') |
| 20 | +# sys.path.append('../') |
21 | 21 | |
22 | 22 | import MySQLdb |
23 | 23 | import math |
24 | 24 | import datetime |
25 | 25 | import re # regular expression matching |
26 | 26 | |
27 | | -import miner_help as mh |
28 | | -import QueryData as QD |
29 | | -import TimestampProcessor as TP |
| 27 | +import Fundraiser_Tools.miner_help as mh |
| 28 | +import Fundraiser_Tools.classes.QueryData as QD |
| 29 | +import Fundraiser_Tools.classes.TimestampProcessor as TP |
30 | 30 | |
31 | 31 | """ |
32 | 32 | |
33 | | - CLASS :: DataLoader |
| 33 | + BASE CLASS :: DataLoader |
34 | 34 | |
| 35 | + This is the base class for data handling functionality of metrics. |
| 36 | + |
35 | 37 | METHODS: |
36 | | - init_db - |
37 | | - close_db - |
| 38 | + init_db |
| 39 | + close_db |
| 40 | + compose_key |
| 41 | + include_keys |
| 42 | + exclude_keys |
| 43 | + get_sql_filename_for_query |
| 44 | + |
| 45 | + |
38 | 46 | """ |
39 | 47 | class DataLoader(object): |
40 | 48 | |
— | — | @@ -179,9 +187,6 @@ |
180 | 188 | self._query_names_['campaign'] = 'report_campaign_metrics_minutely' |
181 | 189 | self._query_names_['campaign_total'] = 'report_campaign_metrics_minutely_total' |
182 | 190 | |
183 | | - def get_sql_filename_for_query(self, query_type): |
184 | | - return self._query_names_[query_type] |
185 | | - |
186 | 191 | """ |
187 | 192 | <DESCRIPTION> |
188 | 193 | |
— | — | @@ -300,6 +305,7 @@ |
301 | 306 | |
302 | 307 | return [metrics, times] |
303 | 308 | |
| 309 | + |
304 | 310 | """ |
305 | 311 | |
306 | 312 | |
— | — | @@ -344,7 +350,18 @@ |
345 | 351 | |
346 | 352 | return [metrics, times] |
347 | 353 | |
| 354 | + |
| 355 | +""" |
| 356 | + |
| 357 | + CLASS :: BannerLPReportingLoader |
348 | 358 | |
| 359 | + This dataloader handles reporting on banners and landing pages. |
| 360 | + |
| 361 | + METHODS: |
| 362 | + run_query |
| 363 | + |
| 364 | + |
| 365 | +""" |
349 | 366 | class BannerLPReportingLoader(DataLoader): |
350 | 367 | |
351 | 368 | def run_query(self): |
— | — | @@ -437,8 +454,94 @@ |
438 | 455 | # return the metric values at each time |
439 | 456 | return [metrics_1, metrics_2, times_indices] |
440 | 457 | |
| 458 | + |
| 459 | +""" |
| 460 | + |
| 461 | + CLASS :: CampaignReportingLoader |
441 | 462 | |
| 463 | + This dataloader handles reporting on utm_campaigns. |
| 464 | + |
| 465 | + METHODS: |
| 466 | + run_query |
| 467 | + |
| 468 | + |
| 469 | +""" |
| 470 | +class CampaignReportingLoader(DataLoader): |
| 471 | + |
| 472 | + def __init__(self): |
| 473 | + self._query_names_['totals'] = 'report_campaign_totals' |
| 474 | + self._query_names_['times'] = 'report_campaign_times' |
| 475 | + |
| 476 | + """ |
| 477 | + !! MODIFY -- use python reflection !! ... maybe |
| 478 | + |
| 479 | + This method is retrieving campaign names |
| 480 | + |
| 481 | + delegates the procesing to different methods |
| 482 | + |
| 483 | + """ |
| 484 | + def run_query(self, query_type, params): |
| 485 | + |
| 486 | + self.init_db() |
| 487 | + |
| 488 | + if query_type == 'totals': |
| 489 | + data = self.query_totals(query_type, params) |
| 490 | + |
| 491 | + self.close_db() |
| 492 | + |
| 493 | + return data |
| 494 | + |
| 495 | + """ |
| 496 | + |
| 497 | + Handle queries from "report_campaign_totals" |
| 498 | + |
| 499 | + """ |
| 500 | + def query_totals(self, query_type, params): |
| 501 | + |
| 502 | + """ Resolve parameters """ |
| 503 | + metric_name = params['metric_name'] |
| 504 | + start_time = params['start_time'] |
| 505 | + end_time = params['end_time'] |
| 506 | + |
| 507 | + query_name = self.get_sql_filename_for_query(query_type) |
| 508 | + |
| 509 | + """ Load the SQL File & Format """ |
| 510 | + filename = self._sql_path_+ query_name + '.sql' |
| 511 | + sql_stmnt = mh.read_sql(filename) |
| 512 | + sql_stmnt = QD.format_query(query_name, sql_stmnt, [start_time, end_time]) |
| 513 | + |
| 514 | + """ Get Indexes into Query """ |
| 515 | + key_index = QD.get_key_index(query_name) |
| 516 | + metric_index = QD.get_metric_index(query_name, metric_name) |
| 517 | + |
| 518 | + data = mh.AutoVivification() |
| 519 | + |
| 520 | + """ Compose the data for each separate donor pipeline artifact """ |
| 521 | + try: |
| 522 | + |
| 523 | + self._cur_.execute(sql_stmnt) |
| 524 | + |
| 525 | + results = self._cur_.fetchall() |
| 526 | + |
| 527 | + for row in results: |
| 528 | + |
| 529 | + key_name = row[key_index] |
| 530 | + data[key_name] = float(row[metric_index]) |
| 531 | + |
| 532 | + |
| 533 | + except Exception as inst: |
| 534 | + print type(inst) # the exception instance |
| 535 | + print inst.args # arguments stored in .args |
| 536 | + print inst # __str__ allows args to printed directly |
| 537 | + |
| 538 | + self._db_.rollback() |
| 539 | + sys.exit(0) |
442 | 540 | |
| 541 | + |
| 542 | + return data |
| 543 | + |
| 544 | + |
| 545 | + |
443 | 546 | """ |
444 | 547 | |
445 | 548 | CLASS :: TTestLoaderHelp |