|
| 1 | +# !/usr/bin/env python |
| 2 | +""" |
| 3 | +SEED Platform (TM), Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors. |
| 4 | +See also https://github.com/SEED-platform/seed/blob/main/LICENSE.md |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +from collections import Counter |
| 9 | + |
| 10 | +from celery import chain, shared_task |
| 11 | +from django.db.models import F |
| 12 | + |
| 13 | +from seed.analysis_pipelines.pipeline import ( |
| 14 | + AnalysisPipeline, |
| 15 | + AnalysisPipelineError, |
| 16 | + analysis_pipeline_task, |
| 17 | + task_create_analysis_property_views, |
| 18 | +) |
| 19 | +from seed.models import Analysis, AnalysisMessage, AnalysisPropertyView, Column, Element, PropertyView |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class HVACMetricsPipeline(AnalysisPipeline): |
| 25 | + def _prepare_analysis(self, property_view_ids, start_analysis=True): |
| 26 | + # if theres not elements, just exit |
| 27 | + views = PropertyView.objects.filter(id__in=property_view_ids) |
| 28 | + if not Element.objects.filter(property__in=views.values_list("property", flat=True)).exists(): |
| 29 | + AnalysisMessage.log_and_create( |
| 30 | + logger=logger, |
| 31 | + type_=AnalysisMessage.ERROR, |
| 32 | + analysis_id=self._analysis_id, |
| 33 | + analysis_property_view_id=None, |
| 34 | + user_message="None of the selected properties have elements, which are requied for this analysis.", |
| 35 | + debug_message="", |
| 36 | + ) |
| 37 | + analysis = Analysis.objects.get(id=self._analysis_id) |
| 38 | + analysis.status = Analysis.FAILED |
| 39 | + analysis.save() |
| 40 | + raise AnalysisPipelineError("None of the selected properties have elements, which are requied for this analysis.") |
| 41 | + |
| 42 | + progress_data = self.get_progress_data() |
| 43 | + progress_data.total = 3 |
| 44 | + progress_data.save() |
| 45 | + |
| 46 | + chain( |
| 47 | + task_create_analysis_property_views.si(self._analysis_id, property_view_ids), |
| 48 | + _finish_preparation.s(self._analysis_id), |
| 49 | + _run_analysis.s(self._analysis_id), |
| 50 | + ).apply_async() |
| 51 | + |
| 52 | + def _start_analysis(self): |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +@shared_task(bind=True) |
| 57 | +@analysis_pipeline_task(Analysis.CREATING) |
| 58 | +def _finish_preparation(self, analysis_view_ids_by_property_view_id, analysis_id): |
| 59 | + pipeline = HVACMetricsPipeline(analysis_id) |
| 60 | + pipeline.set_analysis_status_to_ready("Ready to run HVAC Metrics analysis") |
| 61 | + |
| 62 | + # here is where errors would be filtered out |
| 63 | + |
| 64 | + return list(analysis_view_ids_by_property_view_id.values()) |
| 65 | + |
| 66 | + |
| 67 | +@shared_task(bind=True) |
| 68 | +@analysis_pipeline_task(Analysis.READY) |
| 69 | +def _run_analysis(self, analysis_property_view_ids, analysis_id): |
| 70 | + pipeline = HVACMetricsPipeline(analysis_id) |
| 71 | + progress_data = pipeline.set_analysis_status_to_running() |
| 72 | + progress_data.step("Generating Numbers") |
| 73 | + analysis = Analysis.objects.get(id=analysis_id) |
| 74 | + |
| 75 | + # get/create relevant columns |
| 76 | + existing_columns = _create_analysis_columns(analysis) |
| 77 | + |
| 78 | + analysis_property_views = AnalysisPropertyView.objects.filter(id__in=analysis_property_view_ids) |
| 79 | + property_views_by_apv_id = AnalysisPropertyView.get_property_views(analysis_property_views) |
| 80 | + for analysis_property_view in analysis_property_views: |
| 81 | + # get property view and its elements |
| 82 | + property_view = property_views_by_apv_id[analysis_property_view.id] |
| 83 | + elements = Element.objects.filter(property=property_view.property) |
| 84 | + |
| 85 | + # do calculations |
| 86 | + cooling_caps = elements.annotate(cooling_cap=F("extra_data__Nominal Cooling Cap. (Tons)")).values_list("cooling_cap", flat=True) |
| 87 | + total_cooling_cap = sum([c for c in cooling_caps if c is not None]) |
| 88 | + refrigeration_on_types = elements.annotate(refrigeration_on_type=F("extra_data__Refrigeration on Type")).values_list( |
| 89 | + "refrigeration_on_type", flat=True |
| 90 | + ) |
| 91 | + most_common_refrigeration_on_type = Counter(refrigeration_on_types).most_common(1)[0][0] if refrigeration_on_types else None |
| 92 | + |
| 93 | + # update the analysis_property_view |
| 94 | + analysis_property_view.parsed_results = { |
| 95 | + "Total Nominal Cooling Cap. (Tons)": total_cooling_cap, |
| 96 | + "Most Common Refrigeration On Type": most_common_refrigeration_on_type, |
| 97 | + } |
| 98 | + analysis_property_view.save() |
| 99 | + |
| 100 | + # write to property columns |
| 101 | + if "total_nominal_cooling_cap" in existing_columns: |
| 102 | + property_view.state.extra_data.update({"total_nominal_cooling_cap": total_cooling_cap}) |
| 103 | + if "most_common_refrigeration_on_type" in existing_columns: |
| 104 | + property_view.state.extra_data.update({"most_common_refrigeration_on_type": most_common_refrigeration_on_type}) |
| 105 | + |
| 106 | + property_view.state.save() |
| 107 | + |
| 108 | + # all done! |
| 109 | + pipeline.set_analysis_status_to_completed() |
| 110 | + |
| 111 | + |
| 112 | +def _create_analysis_columns(analysis): |
| 113 | + existing_columns = [] |
| 114 | + column_meta = [ |
| 115 | + { |
| 116 | + "column_name": "total_nominal_cooling_cap", |
| 117 | + "display_name": "Total Nominal Cooling Cap.", |
| 118 | + "description": "created by HVAC Metric analysis", |
| 119 | + }, |
| 120 | + { |
| 121 | + "column_name": "most_common_refrigeration_on_type", |
| 122 | + "display_name": "Most Common Refrigeration On Type", |
| 123 | + "description": "created by HVAC Metric analysis", |
| 124 | + }, |
| 125 | + ] |
| 126 | + |
| 127 | + for col in column_meta: |
| 128 | + try: |
| 129 | + Column.objects.get( |
| 130 | + column_name=col["column_name"], |
| 131 | + organization=analysis.organization, |
| 132 | + table_name="PropertyState", |
| 133 | + ) |
| 134 | + existing_columns.append(col["column_name"]) |
| 135 | + except Exception: |
| 136 | + if analysis.can_create(): |
| 137 | + column = Column.objects.create( |
| 138 | + is_extra_data=True, |
| 139 | + column_name=col["column_name"], |
| 140 | + organization=analysis.organization, |
| 141 | + table_name="PropertyState", |
| 142 | + ) |
| 143 | + column.display_name = col["display_name"] |
| 144 | + column.column_description = col["description"] |
| 145 | + column.save() |
| 146 | + existing_columns.append(col["column_name"]) |
| 147 | + |
| 148 | + return existing_columns |
0 commit comments