Skip to content

Commit

Permalink
filter report by ali
Browse files Browse the repository at this point in the history
  • Loading branch information
haneslinger committed Apr 30, 2024
1 parent 9433699 commit 46911dd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
36 changes: 34 additions & 2 deletions seed/static/seed/js/controllers/inventory_reports_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,33 @@ angular.module('BE.seed.controller.inventory_reports', []).controller('inventory
'$sce',
'$translate',
'$uibModal',
'ah_service',
'access_level_tree',
'user_service',
// eslint-disable-next-line func-names
function ($scope, $log, $stateParams, spinner_utility, inventory_reports_service, simple_modal_service, columns, cycles, organization_payload, urls, $sce, $translate, $uibModal) {
function ($scope, $log, $stateParams, spinner_utility, inventory_reports_service, simple_modal_service, columns, cycles, organization_payload, urls, $sce, $translate, $uibModal, ah_service, access_level_tree, user_service) {

Check warning on line 28 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

This line has a length of 226. Maximum allowed is 200
const org_id = organization_payload.organization.id;
const base_storage_key = `report.${org_id}`;
console.log(user_service.get_access_level_instance())

Check failure on line 31 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Missing semicolon

$scope.access_level_tree = access_level_tree.access_level_tree;
$scope.level_names = access_level_tree.access_level_names;
$scope.level_name_index = null;
$scope.potential_level_instances = [];
$scope.access_level_instance_id = null;
$scope.users_access_level_instance_id = user_service.get_access_level_instance().id;


Check failure on line 40 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

More than 1 blank line not allowed
const access_level_instances_by_depth = ah_service.calculate_access_level_instances_by_depth($scope.access_level_tree);
// cannot select parents alis
const [users_depth, ali] = Object.entries(access_level_instances_by_depth).find(([_, x]) => x.length === 1 && x[0].id == $scope.users_access_level_instance_id)

Check failure on line 43 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

'ali' is assigned a value but never used

Check failure on line 43 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

'_' is defined but never used

Check failure on line 43 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Expected '===' and instead saw '=='

Check failure on line 43 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Missing semicolon
$scope.level_names = access_level_tree.access_level_names.slice(users_depth-1);

Check failure on line 44 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Operator '-' must be spaced

$scope.change_selected_level_index = () => {
const new_level_instance_depth = parseInt($scope.level_name_index, 10) + parseInt(users_depth, 10);
$scope.potential_level_instances = access_level_instances_by_depth[new_level_instance_depth];
$scope.access_level_instance_id = null;
};

const pretty_unit = (pint_spec) => {
const mappings = {
Expand Down Expand Up @@ -137,11 +160,18 @@ angular.module('BE.seed.controller.inventory_reports', []).controller('inventory

const localStorageXAxisKey = `${base_storage_key}.xaxis`;
const localStorageYAxisKey = `${base_storage_key}.yaxis`;
const localStorageALIndex = `${base_storage_key}.ALIndex`;
const localStorageALIID = `${base_storage_key}.ALIID`;

// Currently selected x and y variables - check local storage first, otherwise initialize to first choice
$scope.xAxisSelectedItem = JSON.parse(localStorage.getItem(localStorageXAxisKey)) || $scope.xAxisVars[0];
$scope.yAxisSelectedItem = JSON.parse(localStorage.getItem(localStorageYAxisKey)) || $scope.yAxisVars[0];

$scope.level_name_index = JSON.parse(localStorage.getItem(localStorageALIndex)) || "0";

Check failure on line 170 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Strings must use singlequote
const new_level_instance_depth = parseInt($scope.level_name_index, 10) + parseInt(users_depth, 10);
$scope.potential_level_instances = access_level_instances_by_depth[new_level_instance_depth]

Check failure on line 172 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Missing semicolon
$scope.access_level_instance_id = JSON.parse(localStorage.getItem(localStorageALIID)) || parseInt($scope.users_access_level_instance_id, 0);

Check failure on line 173 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

Invalid radix parameter, must be an integer between 2 and 36

// Chart data
$scope.chartData = [];
$scope.aggChartData = [];
Expand Down Expand Up @@ -393,7 +423,7 @@ angular.module('BE.seed.controller.inventory_reports', []).controller('inventory
$scope.chartIsLoading = true;

inventory_reports_service
.get_report_data(xVar, yVar, $scope.selected_cycles)
.get_report_data(xVar, yVar, $scope.selected_cycles, $scope.access_level_instance_id)
.then(
(data) => {
data = data.data;
Expand Down Expand Up @@ -502,6 +532,8 @@ angular.module('BE.seed.controller.inventory_reports', []).controller('inventory
localStorage.setItem(localStorageXAxisKey, JSON.stringify($scope.xAxisSelectedItem));
localStorage.setItem(localStorageYAxisKey, JSON.stringify($scope.yAxisSelectedItem));
localStorage.setItem(localStorageSelectedCycles, JSON.stringify($scope.selected_cycles));
localStorage.setItem(localStorageALIndex, JSON.stringify($scope.level_name_index));
localStorage.setItem(localStorageALIID, JSON.stringify($scope.access_level_instance_id));
}

/* Generate an array of color objects to be used as part of chart configuration
Expand Down
10 changes: 9 additions & 1 deletion seed/static/seed/js/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,15 @@ SEED_app.config([
const organization_id = user_service.get_organization().id;
return organization_service.get_organization(organization_id);
}
]
],
access_level_tree: [
'organization_service',
'user_service',
(organization_service, user_service) => {
const organization_id = user_service.get_organization().id;
return organization_service.get_organization_access_level_tree(organization_id);
}
],
}
})
.state({
Expand Down
3 changes: 2 additions & 1 deletion seed/static/seed/js/services/inventory_reports_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ angular.module('BE.seed.service.inventory_reports', []).factory('inventory_repor
]
}
*/
const get_report_data = (xVar, yVar, cycle_ids) => {
const get_report_data = (xVar, yVar, cycle_ids, access_level_instance_id) => {
// Error checks
if (_.some([xVar, yVar, cycle_ids], _.isNil)) {
$log.error('#inventory_reports_service.get_report_data(): null parameter');
Expand All @@ -47,6 +47,7 @@ angular.module('BE.seed.service.inventory_reports', []).factory('inventory_repor
params: {
x_var: xVar,
y_var: yVar,
access_level_instance_id,
cycle_ids
}
})
Expand Down
20 changes: 19 additions & 1 deletion seed/static/seed/partials/inventory_reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ <h2 style="font-size: 18px"><i class="fa-solid fa-bar-chart pull-left"></i> {$::
</div>
<div class="form-inline pull-right chart-inputs">
<form class="form-inline pull-right chart-inputs" role="form" style="display: flex">

<div style="display: flex; flex-direction: column" class="right-bar">
<div class="form-group">
<label class="control-label col-sm-3" translate>Access Level:</label>
<div class="col-sm-8">
<select class="form-control" ng-model="level_name_index" ng-change="change_selected_level_index()"
ng-options="i as name for (i, name) in level_names"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" translate>Access Level Instance:</label>
<div class="col-sm-8">
<select class="form-control" ng-model="access_level_instance_id"
ng-options="potential_level_instance.id as potential_level_instance.name for potential_level_instance in potential_level_instances"></select>
</div>
</div>
</div>

<div style="display: flex; flex-direction: column" class="right-bar">
<div class="form-group">
<label for="select-cycle">{$:: 'Cycles to Include' | translate $}:</label>
Expand Down Expand Up @@ -48,7 +66,7 @@ <h2 style="font-size: 18px"><i class="fa-solid fa-bar-chart pull-left"></i> {$::
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" ng-click="updateChartData()" translate>Update Charts</button>
<button type="submit" class="btn btn-primary" ng-disabled="access_level_instance_id == null" ng-click="updateChartData()" translate>Update Charts</button>
</div>
</form>
</div>
Expand Down
10 changes: 10 additions & 0 deletions seed/tests/test_organization_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ def test_report(self):
resp = self.client.get(url, content_type="application/json")
assert resp.json()["data"]["property_counts"][0]["num_properties"] == 1

# root users can choose
self.login_as_root_member()
resp = self.client.get(url + "&access_level_instance_id=" + str(self.child_level_instance.pk), content_type="application/json")
assert resp.json()["data"]["property_counts"][0]["num_properties"] == 0

# child user cannot choose
self.login_as_child_member()
resp = self.client.get(url + "&access_level_instance_id=" + str(self.root_level_instance.pk), content_type="application/json")
assert resp.status_code == 400

def test_report_aggregated(self):
url = reverse("api:v3:organizations-report-aggregated", args=[self.org.pk])
url += f"?x_var=building_count&y_var=gross_floor_area&cycle_ids={Cycle.objects.first().id}"
Expand Down
17 changes: 15 additions & 2 deletions seed/views/v3/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,26 @@ def get_raw_report_data(self, organization_id, access_level_instance, cycles, x_
@action(detail=True, methods=["GET"])
def report(self, request, pk=None):
"""Retrieve a summary report for charting x vs y"""
access_level_instance = AccessLevelInstance.objects.get(pk=self.request.access_level_instance_id)
params = {
"x_var": request.query_params.get("x_var", None),
"y_var": request.query_params.get("y_var", None),
"access_level_instance_id": request.query_params.get("access_level_instance_id", None),
"cycle_ids": request.query_params.getlist("cycle_ids", None),
}

user_ali = AccessLevelInstance.objects.get(pk=self.request.access_level_instance_id)
if params["access_level_instance_id"] is None:
ali = user_ali
else:
try:
selected_ali = AccessLevelInstance.objects.get(pk=params["access_level_instance_id"])
if not (selected_ali == user_ali or selected_ali.is_descendant_of(user_ali)):
raise AccessLevelInstance.DoesNotExist
except (AccessLevelInstance.DoesNotExist, AssertionError):
return Response({"status": "error", "message": "No such ali"}, status=status.HTTP_400_BAD_REQUEST)
else:
ali = selected_ali

excepted_params = ["x_var", "y_var", "cycle_ids"]
missing_params = [p for p in excepted_params if p not in params]
if missing_params:
Expand All @@ -841,7 +854,7 @@ def report(self, request, pk=None):
)

cycles = Cycle.objects.filter(id__in=params["cycle_ids"])
data = self.get_raw_report_data(pk, access_level_instance, cycles, params["x_var"], params["y_var"])
data = self.get_raw_report_data(pk, ali, cycles, params["x_var"], params["y_var"])
data = {
"chart_data": functools.reduce(operator.iadd, [d["chart_data"] for d in data], []),
"property_counts": [d["property_counts"] for d in data],
Expand Down

0 comments on commit 46911dd

Please sign in to comment.