Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
feat(reports): hook up reports
Browse files Browse the repository at this point in the history
- refactor $scope vars into controller fields
- support expand and text parameters for export-table directive
- reword # of files to # Requests

Closes #891
  • Loading branch information
Christine Yu committed Jun 25, 2015
1 parent 1e63a22 commit 9b9c2dd
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 352 deletions.
6 changes: 4 additions & 2 deletions app/scripts/components/tables/tables.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ module ngApp.components.tables.controllers {
endpoint: string;
size: number;
fields: string[];
text: string;
expand: string[];
}

interface IExportTableController {
Expand Down Expand Up @@ -284,8 +286,8 @@ module ngApp.components.tables.controllers {
if (this.$window.URL && this.$window.URL.createObjectURL) {
var params = {
filters: filters,
fields: fieldsAndExpand.fields.join(),
expand: fieldsAndExpand.expand.join(),
fields: fieldsAndExpand.fields.concat(this.$scope.fields || []).join(),
expand: fieldsAndExpand.expand.concat(this.$scope.expand || []).join(),
attachment: true,
format: fileType,
size: this.$scope.size
Expand Down
6 changes: 4 additions & 2 deletions app/scripts/components/tables/tables.directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module ngApp.components.tables.directives {
}

var defaults = _.cloneDeep($scope.headings);

$scope.headings = $scope.saved.length ?
_.map($scope.saved, s => _.merge(_.find($scope.headings, {id: s.id}), s)) :
$scope.headings;
Expand All @@ -52,9 +52,11 @@ module ngApp.components.tables.directives {
return {
restrict: "EA",
scope: {
text: "@",
size: "@",
headings: "=",
endpoint: "@"
endpoint: "@",
expand: "="
},
replace: true,
templateUrl: "components/tables/templates/export-table.html",
Expand Down
15 changes: 10 additions & 5 deletions app/scripts/components/tables/templates/export-table.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<div class="btn-group" data-dropdown>
<button type="button" class="btn btn-default dropdown-toggle"
data-tooltip="Export Table"
data-tooltip-popup-delay=1000
<button data-ng-if="!text" type="button" class="btn btn-default dropdown-toggle"
data-tooltip="Export Table"
data-tooltip-popup-delay=1000
data-tooltip-append-to-body="true"
dropdown-toggle aria-expanded="false">
<span class="fa fa-table"></span>
data-dropdown-toggle aria-expanded="false">
<span class="fa fa-table"></span>
</button>
<button data-ng-if="text" type="button" class="btn btn-default dropdown-toggle"
data-dropdown-toggle
data-aria-expanded="false">
{{ text }}
</button>
<ul class="dropdown-menu pull-right" role="menu">
<li>
Expand Down
20 changes: 18 additions & 2 deletions app/scripts/reports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ module ngApp.reports {
import IReportsService = ngApp.reports.services.IReportsService;
import IReport = ngApp.reports.models.IReport;

var reportServiceExpand = [
"data_access",
"data_subtypes",
"tags",
"countries",
"data_formats",
"experimental_strategies",
"platforms",
"user_access_types",
"data_types",
"centers"
];

/* @ngInject */
function reportsConfig($stateProvider: ng.ui.IStateProvider) {
$stateProvider.state("reports", {
Expand All @@ -11,7 +24,9 @@ module ngApp.reports {
templateUrl: "reports/templates/reports.html",
resolve: {
reports: (ReportsService: IReportsService) => {
return ReportsService.getReports();
return ReportsService.getReports({
expand: reportServiceExpand
});
}
}
});
Expand All @@ -22,5 +37,6 @@ module ngApp.reports {
"reports.controller",
"ui.router.state"
])
.value("reportServiceExpand", reportServiceExpand)
.config(reportsConfig);
}
}
214 changes: 61 additions & 153 deletions app/scripts/reports/reports.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,181 +2,89 @@ module ngApp.reports.controllers {
import ICoreService = ngApp.core.services.ICoreService;
import IReports = ngApp.reports.models.IReports;
import IReport = ngApp.reports.models.IReport;
import IReportsService = ngApp.reports.services.IReportsService;
import IProjectsService = ngApp.projects.services.IProjectsService;
import ILocationService = ngApp.components.location.services.ILocationService;

export interface IReportsController {
}
export interface IReportController {
report: IReport;
reports: IReports;
byProject: any;
byDisease: any;
byProgram: any;
byDataType: any;
bySubtype: any;
byStrat: any;
byDataAccess: any;
byUserType: any;
byLocation: any;
}

class ReportsController implements IReportsController {
reports: IReports;
byProject: any;
byDisease: any;
byProgram: any;
byDataType: any;
bySubtype: any;
byStrat: any;
byDataAccess: any;
byUserType: any;
byLocation: any;

/* @ngInject */
constructor(private CoreService: ICoreService,
$scope: ng.IScope,
ReportsService: IReportsService,
ProjectsService: IProjectsService,
$timeout: ng.ITimeoutService,
private LocationService: ILocationService,
private config: IGDCConfig,
private $modal: any,
private $q: ng.IQService,
private Restangular: restangular.IProvider,
private $window: ng.IWindowService,
private UserService: IUserService,
$filter: ng.IFilterService,
ReportsGithutColumns,
ReportsGithut) {

CoreService.setPageTitle("Reports");

/**
* TODO: refactor with app/scripts/components/tables/tables.controllers.ts#L119
*/
$scope.reportExport = function a(){

var url = LocationService.getHref();
var abort = $q.defer();
var modalInstance = $modal.open({
templateUrl: "components/tables/templates/export-modal.html",
controller: "ExportTableModalController as etmc",
backdrop: 'static'
});

var fileType = 'JSON';
var endpoint = 'reports/data-download-statistics';

if ($window.URL && $window.URL.createObjectURL) {
var params = {
attachment: true,
format: fileType,
size: 20
};

Restangular.all(endpoint)
.withHttpConfig({
timeout: abort.promise,
responseType: "blob"
})
.get('', params).then((file) => {
var url = $window.URL.createObjectURL(file);
var a = $window.document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", endpoint + "." +
$window.moment().format() + "." +
fileType.toLowerCase());
$window.document.body.appendChild(a);

$timeout(() => {
a.click();
modalInstance.close({cancel: true});
$window.document.body.removeChild(a);
},777);
});
} else {
this.LocationService.setHref(this.config.api + "/" +
this.$scope.endpoint +
"?attachment=true&format=" + fileType +
"&fields=" + this.$scope.fields.join() +
"&size=" + this.$scope.size +
"&filters=" + JSON.stringify(filters));
}
modalInstance.result.then((data) => {
if (data.cancel) {
if (abort) {
abort.resolve();
} else {
this.LocationService.setHref(url);
}
}
});
}

ReportsService.getReports().then(function(reports){
var data = reports.hits.map(function(a){
a.file_size = a.size;
a.file_count = a.count;
return a;
});

$timeout(function(){
var githut = ReportsGithut(data);

constructor(public reports: IReports, private CoreService: ICoreService,
public $scope: ng.IScope, private $timeout: ng.ITimeoutService,
private ReportsGithutColumns, private ReportsGithut, public reportServiceExpand: string[]) {

CoreService.setPageTitle("Reports");

var dataNoZeros = _.reject(reports.hits, (hit) => { return hit.count === 0 && hit.size ===0; });
this.byProject = this.dataNest("project_id").entries(dataNoZeros);
this.byDisease = this.dataNest("disease_type").entries(dataNoZeros);
this.byProgram = this.dataNest("program").entries(dataNoZeros);
this.byDataType = this.dataNest("data_type").entries(this.reduceBy(dataNoZeros, "data_types"));
this.bySubtype = this.dataNest("data_subtype").entries(this.reduceBy(dataNoZeros, "data_subtypes"));
this.byStrat = this.dataNest("experimental_strategy").entries(this.reduceBy(dataNoZeros, "experimental_strategies"));
this.byDataAccess = this.dataNest("access").entries(this.reduceBy(dataNoZeros, "data_access"));
this.byUserType = this.dataNest("user_access_type").entries(this.reduceBy(dataNoZeros, "user_access_types"));
this.byLocation = this.dataNest("country").entries(this.reduceBy(dataNoZeros, "countries"));

$timeout(() => {
var githut = ReportsGithut(dataNoZeros);
$scope.githutData = githut.data;
$scope.githutConfig = githut.config;

}, 500);


$scope.byProject = dataNest('project_id').entries(data);
$scope.byDisease = dataNest('disease_type').entries(data);
$scope.byProgram = dataNest('program').entries(data);

$scope.byDataType = dataNest('data_type').entries(data.reduce(function(a,b){
a = a.concat(b.data_types);
return a;
},[]));

$scope.bySubtype = dataNest('data_subtype').entries(data.reduce(function(a,b){
a = a.concat(b.data_subtypes);
return a;
},[]));

$scope.byStrat = dataNest('experimental_strategy').entries(data.reduce(function(a,b){
a = a.concat(b.experimental_strategies);
return a;
},[]));

$scope.byDataAccess = dataNest('access').entries(data.reduce(function(a,b){
a = a.concat(b.data_access);
return a;
},[]));

$scope.byUserType = dataNest('user_access_type').entries(data.reduce(function(a,b){
a = a.concat(b.user_access_types);
return a;
},[]));

$scope.byLocation = dataNest('country').entries(data.reduce(function(a,b){
a = a.concat(b.countries);
return a;
},[]));


function dataNest(key){
return d3.nest()
.key(function(d){return d[key]})
.rollup(function(d){
return {
file_count:d3.sum(d.map(function(x){return x.count})),
file_size:d3.sum(d.map(function(x){return x.size})),
project_name:d[0].project_name
}
})
.sortValues(function(a,b){return a.file_count - b.file_count});
}
});
}
}

class ReportController implements IReportController {

/* @ngInject */
constructor(public report: IReport, private CoreService: ICoreService, $timeout) {
CoreService.setPageTitle("Report", report.id);
dataNest(key: string): any {
return d3.nest()
.key(function(d){return d[key]})
.rollup(function(d){
return {
file_count: d3.sum(d.map(function(x){return x.count})),
file_size: d3.sum(d.map(function(x){return x.size})),
project_name:d[0].disease_type
}
})
.sortValues(function(a,b){return a.file_count - b.file_count});
}

reduceBy(data: any, key: string): any {
return _.reduce(data, (result, datum) => {
if (datum[key]) {
result = result.concat(datum[key]);
}
return result;
}, []);
}

}

angular
.module("reports.controller", [
"reports.services",
"core.services",
'reports.githut.config'
"reports.githut.config"
])
.controller("ReportController", ReportController)
.controller("ReportsController", ReportsController);
}

Loading

0 comments on commit 9b9c2dd

Please sign in to comment.