Skip to content

Commit

Permalink
feat(logs): always includes total count in EditLogs API, then display…
Browse files Browse the repository at this point in the history
… it in isari-log-table (fixes #457)
  • Loading branch information
naholyr committed Sep 25, 2017
1 parent 6be1e27 commit 4340ebc
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 30 deletions.
4 changes: 2 additions & 2 deletions client/src/app/isari-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class IsariDataService {
.map(vals => keyBy(vals, 'value')),
])

.map(([logs, schema, roles, accessMonitorings]) => {
.map(([{ count, results: logs }, schema, roles, accessMonitorings]) => {
logs = (<any[]>logs).map(log => {

// if query accessMonitoring, keep only diff for this accessMonitoring #433
Expand Down Expand Up @@ -164,7 +164,7 @@ export class IsariDataService {
}))
return log;
});
return logs;
return { count, logs };
});
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/app/isari-editor/isari-editor.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<isari-log-table *ngIf="displayHistory"
style="width:100%"
[feature]="feature"
[logs]="logs$ | async"
[data]="logs$ | async"
[labs]="labs$ | async"
[options]="options"
[hideItemCol]="true"
Expand Down
7 changes: 5 additions & 2 deletions client/src/app/isari-editor/isari-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export class IsariEditorComponent implements OnInit {
.getHistory(this.feature, this.options, lang)
.combineLatest(this.details$)
})
.map(([logs, details]) => {
.map(([{count, logs}, details]) => {
this.labs$ = this.isariDataService.getForeignLabel('Organization', uniq(flattenDeep(logs.map(log => log.who.roles.map(role => role.lab)))))
.map(labs => keyBy(labs, 'id'));

Expand All @@ -306,7 +306,10 @@ export class IsariEditorComponent implements OnInit {
diff: log.diff.filter(diff => diff.path[0] === this.options['path'])
}));

return logs.map(log => Object.assign({}, log, { _open: details }));
return {
count,
logs: logs.map(log => Object.assign({}, log, { _open: details }))
};
});
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/app/isari-logs/isari-logs.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<isari-log-table
[feature]="feature"
[logs]="logs$ | async"
[data]="logs$ | async"
[labs]="labs$ | async"
[options]="options"
(onDownloadCSV)="downloadCSV($event)"
Expand Down
7 changes: 5 additions & 2 deletions client/src/app/isari-logs/isari-logs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class IsariLogsComponent implements OnInit {
.getHistory(this.feature, this.options, lang)
.combineLatest(this.details$)
})
.map(([logs, details]) => {
.map(([{count, logs}, details]) => {
this.labs$ = this.isariDataService.getForeignLabel('Organization', uniq(flattenDeep(logs.map(log => log.who.roles.map(role => role.lab)))))
.map(labs => keyBy(labs, 'id'));

Expand All @@ -62,7 +62,10 @@ export class IsariLogsComponent implements OnInit {
diff: log.diff.filter(diff => diff.path[0] === this.options['path'])
}));

return logs.map(log => Object.assign({}, log, { _open: details }));
return {
count,
logs: logs.map(log => Object.assign({}, log, { _open: details }))
};
});
}

Expand Down
7 changes: 4 additions & 3 deletions client/src/app/log-table/log-table.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@
</tr>
</thead>

<tbody *ngIf="logs; else loading">
<tr *ngIf="!logs.length">
<tbody *ngIf="data; else loading">
<tr *ngIf="!data.logs.length">
<td [colSpan]="hideItemCol ? 5 : 6" style="text-align:center;">{{ 'no result' | translate }}</td>
</tr>
<ng-template ngFor let-log [ngForOf]="logs" let-i="index">
<ng-template ngFor let-log [ngForOf]="data.logs" let-i="index">
<tr [class.open1]="log._open" [class.odd]="i % 2 === 0">

<td>{{ log.date | date:'yyyy-MM-dd HH:mm' }}</td>
Expand Down Expand Up @@ -171,6 +171,7 @@
<div class="pagination">
<ul>
<li><a md-button (click)="navigatePrev()"><md-icon>navigate_before</md-icon></a>
<li *ngIf="data"><span>{{ firstIndex }}-{{ lastIndex }} / {{ data.count }}</span></li>
<li><a md-button (click)="navigateNext()"><md-icon>navigate_next</md-icon></a>
</ul>

Expand Down
24 changes: 20 additions & 4 deletions client/src/app/log-table/log-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export class LogTableComponent implements OnInit, OnChanges {
fields: any[];
accessMonitorings: any[];
limits: number[] = [3, 5, 10, 20, 50, 100, 200];
firstIndex: number;
lastIndex: number;

filterForm: FormGroup;

@Input() logs: any[] | null;
@Input() data: {count: number, logs: any[]} | null;
@Input() labs: any[];
@Input() feature: string;
@Input() options: EditLogApiOptions;
Expand Down Expand Up @@ -70,9 +72,14 @@ export class LogTableComponent implements OnInit, OnChanges {
this.emitOptions(Object.assign({}, filters, { skip: 0 }));
});

this.computeIndices();
}

ngOnChanges(changes: SimpleChanges) {
if (changes['data']) {
this.computeIndices();
}

if (changes['feature']) {

// people autocomplete (whoID)
Expand Down Expand Up @@ -109,6 +116,15 @@ export class LogTableComponent implements OnInit, OnChanges {

}

computeIndices() {
if (!this.data) {
this.firstIndex = this.lastIndex = null;
} else {
this.firstIndex = this.options.skip + 1;
this.lastIndex = this.firstIndex + this.data.logs.length - 1;
}
}

navigatePrev() {
if (this.options.skip === 0) return;
this.emitOptions(Object.assign(this.options, {
Expand All @@ -117,7 +133,7 @@ export class LogTableComponent implements OnInit, OnChanges {
}

navigateNext() {
if (this.logs.length === 0) return;
if (this.data.logs.length === 0) return;
//@TODO handle end via count query
this.emitOptions(Object.assign(this.options, {
skip: this.options.skip + this.options.limit
Expand All @@ -133,11 +149,11 @@ export class LogTableComponent implements OnInit, OnChanges {
}

downloadCSV() {
this.onDownloadCSV.emit(this.logs);
this.onDownloadCSV.emit(this.data.logs);
}

private emitOptions(options) {
this.logs = null;
this.data = null;
this.onOptionsChange.emit(Object.assign({}, this.options, options));
}

Expand Down
29 changes: 14 additions & 15 deletions server/routes/editLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,22 @@ function getEditLog(req, res){
{'$sort':{date:-1}}
]

//count
if (query.count)
aggregationPipeline.push({
'$group': {
'_id' : null,
'count' : {$sum : 1}
}
})

// skip and limit
if (!query.count && query.skip)
aggregationPipeline.push({'$skip':+query.skip})
if (!query.count && query.limit)
aggregationPipeline.push({'$limit':+query.limit})
const resultsPipeline = aggregationPipeline
.concat(query.skip ? [{'$skip':+query.skip}] : [])
.concat(query.limit ? [{'$limit':+query.limit}] : [])

// count
const countPipeline = aggregationPipeline
.concat([{'$group': { '_id' : null, 'count' : {$sum : 1} }}])

const countP = EditLog.aggregate(countPipeline)
.then(([ { count } ]) => count)
const resultsP = EditLog.aggregate(resultsPipeline)
.then(data => formatEdits(data, model, !canViewConfidential))

return EditLog.aggregate(aggregationPipeline)
.then(data => query.count ? data[0] : formatEdits(data, model, !canViewConfidential))
return Promise.all([ countP, resultsP ])
.then(([ count, results ]) => ({ count, results }))
})

return editsP
Expand Down

0 comments on commit 4340ebc

Please sign in to comment.