|
37 | 37 | from re import sub |
38 | 38 | from json import loads, dump, dumps |
39 | 39 |
|
40 | | -from qiita_db.util import create_nested_path |
| 40 | +from qiita_db.util import create_nested_path, retrieve_resource_data |
| 41 | +from qiita_db.util import resource_allocation_plot |
41 | 42 | from qiita_core.qiita_settings import qiita_config, r_client |
42 | 43 | from qiita_core.configuration_manager import ConfigurationManager |
43 | 44 | import qiita_db as qdb |
44 | 45 |
|
| 46 | +# global constant list used in resource_allocation_page |
| 47 | +COLUMNS = [ |
| 48 | + "sName", "sVersion", "cID", "cName", "processing_job_id", |
| 49 | + "parameters", "samples", "columns", "input_size", "extra_info", |
| 50 | + "MaxRSSRaw", "ElapsedRaw", "Start", "node_name", "node_model"] |
| 51 | + |
45 | 52 |
|
46 | 53 | def _get_data_fpids(constructor, object_id): |
47 | 54 | """Small function for getting filepath IDS associated with data object |
@@ -546,3 +553,101 @@ def generate_plugin_releases(): |
546 | 553 | # important to "flush" variables to avoid errors |
547 | 554 | r_client.delete(redis_key) |
548 | 555 | f(redis_key, v) |
| 556 | + |
| 557 | + |
| 558 | +def get_software_commands(active): |
| 559 | + software_list = [s for s in qdb.software.Software.iter(active=active)] |
| 560 | + software_commands = defaultdict(lambda: defaultdict(list)) |
| 561 | + |
| 562 | + for software in software_list: |
| 563 | + sname = software.name |
| 564 | + sversion = software.version |
| 565 | + commands = software.commands |
| 566 | + |
| 567 | + for command in commands: |
| 568 | + software_commands[sname][sversion].append(command.name) |
| 569 | + software_commands[sname] = dict(software_commands[sname]) |
| 570 | + |
| 571 | + return dict(software_commands) |
| 572 | + |
| 573 | + |
| 574 | +def update_resource_allocation_redis(active=True): |
| 575 | + """Updates redis with plots and information about current software. |
| 576 | +
|
| 577 | + Parameters |
| 578 | + ---------- |
| 579 | + active: boolean, optional |
| 580 | + Defaults to True. Should only be False when testing. |
| 581 | +
|
| 582 | + """ |
| 583 | + time = datetime.now().strftime('%m-%d-%y') |
| 584 | + scommands = get_software_commands(active) |
| 585 | + redis_key = 'resources:commands' |
| 586 | + r_client.set(redis_key, str(scommands)) |
| 587 | + |
| 588 | + for sname, versions in scommands.items(): |
| 589 | + for version, commands in versions.items(): |
| 590 | + for cname in commands: |
| 591 | + col_name = "samples * columns" |
| 592 | + df = retrieve_resource_data(cname, sname, version, COLUMNS) |
| 593 | + if len(df) == 0: |
| 594 | + continue |
| 595 | + |
| 596 | + fig, axs = resource_allocation_plot(df, cname, sname, col_name) |
| 597 | + titles = [0, 0] |
| 598 | + images = [0, 0] |
| 599 | + |
| 600 | + # Splitting 1 image plot into 2 separate for better layout. |
| 601 | + for i, ax in enumerate(axs): |
| 602 | + titles[i] = ax.get_title() |
| 603 | + ax.set_title("") |
| 604 | + # new_fig, new_ax – copy with either only memory plot or |
| 605 | + # only time |
| 606 | + new_fig = plt.figure() |
| 607 | + new_ax = new_fig.add_subplot(111) |
| 608 | + |
| 609 | + scatter_data = ax.collections[0] |
| 610 | + new_ax.scatter(scatter_data.get_offsets()[:, 0], |
| 611 | + scatter_data.get_offsets()[:, 1], |
| 612 | + s=scatter_data.get_sizes(), label="data") |
| 613 | + |
| 614 | + line = ax.lines[0] |
| 615 | + new_ax.plot(line.get_xdata(), line.get_ydata(), |
| 616 | + linewidth=1, color='orange') |
| 617 | + |
| 618 | + if len(ax.collections) > 1: |
| 619 | + failure_data = ax.collections[1] |
| 620 | + new_ax.scatter(failure_data.get_offsets()[:, 0], |
| 621 | + failure_data.get_offsets()[:, 1], |
| 622 | + color='red', s=3, label="failures") |
| 623 | + |
| 624 | + new_ax.set_xscale('log') |
| 625 | + new_ax.set_yscale('log') |
| 626 | + new_ax.set_xlabel(ax.get_xlabel()) |
| 627 | + new_ax.set_ylabel(ax.get_ylabel()) |
| 628 | + new_ax.legend(loc='upper left') |
| 629 | + |
| 630 | + new_fig.tight_layout() |
| 631 | + plot = BytesIO() |
| 632 | + new_fig.savefig(plot, format='png') |
| 633 | + plot.seek(0) |
| 634 | + img = 'data:image/png;base64,' + quote( |
| 635 | + b64encode(plot.getvalue()).decode('ascii')) |
| 636 | + images[i] = img |
| 637 | + plt.close(new_fig) |
| 638 | + plt.close(fig) |
| 639 | + |
| 640 | + # SID, CID, col_name |
| 641 | + values = [ |
| 642 | + ("img_mem", images[0], r_client.set), |
| 643 | + ("img_time", images[1], r_client.set), |
| 644 | + ('time', time, r_client.set), |
| 645 | + ("title_mem", titles[0], r_client.set), |
| 646 | + ("title_time", titles[1], r_client.set) |
| 647 | + ] |
| 648 | + |
| 649 | + for k, v, f in values: |
| 650 | + redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % ( |
| 651 | + cname, sname, version, col_name, k) |
| 652 | + r_client.delete(redis_key) |
| 653 | + f(redis_key, v) |
0 commit comments