Skip to content

Commit

Permalink
Merge pull request lega0208#467 from lega0208/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lega0208 authored Jul 18, 2024
2 parents 0a08e35 + ae7c672 commit d4beac4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
5 changes: 4 additions & 1 deletion libs/api/custom-reports/src/lib/custom-reports.strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '@dua-upd/types-common';
import {
arrayToDictionary,
chunkMap,
dateRangeToGranularity,
} from '@dua-upd/utils-common';
import type { ReportDataPoint } from './custom-reports.service';
Expand Down Expand Up @@ -101,10 +102,12 @@ export function decomposeConfig(config: ReportConfig<Date>) {
urls,
}) as AAQueryConfig;

const chunkedUrls = chunkMap(urls, (url) => url, 50);

const queries = dateRanges.flatMap((dateRange) =>
!grouped && breakdownDimension
? urls.map((url) => toQueryConfig(dateRange, [url]))
: [toQueryConfig(dateRange, urls)],
: chunkedUrls.map((url) => toQueryConfig(dateRange, url)),
);

return queries.map((query) => ({
Expand Down
24 changes: 18 additions & 6 deletions libs/api/query/src/lib/query.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DbService } from '@dua-upd/db';
import { DbService, Page } from '@dua-upd/db';
import { Injectable } from '@nestjs/common';
import { Model, Types, isValidObjectId } from 'mongoose';
import type { DbQuery } from '@dua-upd/types-common';
import dayjs from 'dayjs';

@Injectable()
export class QueryService {
Expand All @@ -10,6 +11,11 @@ export class QueryService {
async getData(serializedQueries: { [key: string]: string }) {
const results: { [key: string]: unknown } = {};

const queryDateRange = {
start: dayjs().subtract(1, 'week').startOf('week').toDate(),
end: dayjs().subtract(1, 'week').endOf('week').toDate(),
};

for (const key in serializedQueries) {
const query: DbQuery[keyof DbQuery] = JSON.parse(
atob(serializedQueries[key]),
Expand All @@ -26,11 +32,17 @@ export class QueryService {
// large queries on this collection can cripple the db, so we'll limit the results like this for now
const limit = collection === 'pageMetrics' ? 1000 : undefined;

results[key] = await collectionModel
.find(filter, project, { limit })
.sort(sort)
.lean()
.exec();
results[key] =
collection === 'pages'
? await this.db.views.pageVisits.getVisitsWithPageData(
queryDateRange,
collectionModel as Model<Page>,
)
: await collectionModel
.find(filter, project, { limit })
.sort(sort)
.lean()
.exec();
}

return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[rows]="displayRows"
[rowHover]="true"
[showCurrentPageReport]="true"
[customSort]="true"
[rowsPerPageOptions]="[5, 10, 25, 50, 100]"
[loading]="loading"
styleClass="p-datatable-striped p-datatable-sm border"
Expand All @@ -19,6 +20,7 @@
[sortOrder]="sortOrder === 'desc' ? -1 : 1"
dataKey="_id"
(selectionChange)="onSelectionChange($event)"
(sortFunction)="customSort($event)"
[(selection)]="selectedRows"
>
<ng-template *ngIf="filter || captionTitle || exports" pTemplate="caption">
Expand Down
43 changes: 39 additions & 4 deletions libs/upd/components/src/lib/data-table/data-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Table } from 'primeng/table';
import type { ColumnConfig } from '@dua-upd/types-common';
import type { SelectedNode } from '../filter-table/filter-table.component';
import { toGroupedColumnSelect } from '@dua-upd/upd/utils';
import { SortEvent } from 'primeng/api';
import { isNullish } from '@dua-upd/utils-common';

@Component({
selector: 'upd-data-table',
Expand All @@ -40,7 +42,7 @@ export class DataTableComponent<T extends object> {
@Input() placeholderText = 'dt_search_keyword';
@Input() selectedNodes: SelectedNode[] = [];
@Input() allowHeaderWrap = false;

node: SelectedNode | null = null;

@Output() rowSelectionChanged = new EventEmitter<T[]>();
Expand All @@ -64,9 +66,13 @@ export class DataTableComponent<T extends object> {
);

selectedColumnsSynced = computed(() => {
const selectedColumnFields = this.selectedColumns().map(({ field }) => field);
const selectedColumnFields = this.selectedColumns().map(
({ field }) => field,
);

return this.cols().filter((col) => selectedColumnFields.includes(col.field));
return this.cols().filter((col) =>
selectedColumnFields.includes(col.field),
);
});

searchFields = computed(() =>
Expand All @@ -91,10 +97,14 @@ export class DataTableComponent<T extends object> {

lang = this.i18n.currentLang;

isSorted: boolean | null = null;

selectableCols = computed(() => {
const cols = this.cols()
.filter((col: ColumnConfig) => !col.frozen)
.sort((a: ColumnConfig, b: ColumnConfig) => a.header.localeCompare(b.header));
.sort((a: ColumnConfig, b: ColumnConfig) =>
a.header.localeCompare(b.header),
);

if (this.groupedColumnSelection()) {
return toGroupedColumnSelect(cols);
Expand Down Expand Up @@ -162,4 +172,29 @@ export class DataTableComponent<T extends object> {

this.selectedColumns.set(selectedColumns);
}

customSort(event: SortEvent) {
event.data?.sort((a, b) => {
a = a[event.field as keyof typeof a];
b = b[event.field as keyof typeof b];

const order = event.order === 1 ? 1 : -1;

// if a is nullish, it goes to the end
if (isNullish(a) || a === '') {
return 1;
}

// if b is nullish, it goes to the end
if (isNullish(b) || b === '') {
return -1;
}

if (typeof a === 'string') {
return a.localeCompare(b) * order;
}

return (a - b) * order;
});
}
}
1 change: 1 addition & 0 deletions libs/utils-common/src/lib/utils-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export function chunkMap<T, ReturnT>(
mapFunc: (val: T[]) => ReturnT,
chunkSize: number,
): ReturnT[] {
array = array.slice();
const chunks = [];

while (array.length) {
Expand Down

0 comments on commit d4beac4

Please sign in to comment.