Skip to content

Commit

Permalink
feat(reports): "sum" query for advanced reports (#1919)
Browse files Browse the repository at this point in the history
closes #1705
  • Loading branch information
TheSlimvReal authored Jul 6, 2023
1 parent 34fee07 commit b0e2a4d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
71 changes: 46 additions & 25 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { School } from "../../child-dev-project/schools/model/school";
import { ChildSchoolRelation } from "../../child-dev-project/children/model/childSchoolRelation";
import { EventNote } from "../../child-dev-project/attendance/model/event-note";
import { defaultDateFilters } from "../filter/date-range-filter/date-range-filter-panel/date-range-filter-panel.component";
import { EducationalMaterial } from "../../child-dev-project/children/educational-material/model/educational-material";

// prettier-ignore
export const defaultJsonConfig = {
Expand Down Expand Up @@ -224,21 +225,21 @@ export const defaultJsonConfig = {
}
],
"exportConfig": [
{ "label": "event_id", "query": "_id" },
{ "label": "date", "query": "date" },
{ "label": "event title", "query": "subject" },
{ "label": "event type", "query": "category" },
{ "label": "event description", "query": "text" },
{"label": "event_id", "query": "_id"},
{"label": "date", "query": "date"},
{"label": "event title", "query": "subject"},
{"label": "event type", "query": "category"},
{"label": "event description", "query": "text"},
{
"query": ":getAttendanceArray(true)",
"subQueries": [
{
"query": ".participant:toEntities(Child)",
"subQueries": [
{ "label": "participant_id", "query": "_id" },
{ "label": "participant", "query": "name" },
{ "label": "gender", "query": "gender" },
{ "label": "religion", "query": "religion" },
{"label": "participant_id", "query": "_id"},
{"label": "participant", "query": "name"},
{"label": "gender", "query": "gender"},
{"label": "religion", "query": "religion"},
]
},
{
Expand All @@ -248,8 +249,8 @@ export const defaultJsonConfig = {
{
"query": ".school:toEntities(School)",
"subQueries": [
{ "label": "school_name", "query": "name" },
{ "label": "school_id", "query": "entityId" }
{"label": "school_name", "query": "name"},
{"label": "school_id", "query": "entityId"}
]
}
],
Expand Down Expand Up @@ -627,7 +628,7 @@ export const defaultJsonConfig = {
"config": {
"rightSide": {
"entityType": School.ENTITY_TYPE,
"availableFilters": [{ "id": "language" }],
"availableFilters": [{"id": "language"}],
},
}
}
Expand Down Expand Up @@ -691,11 +692,11 @@ export const defaultJsonConfig = {
component: "HistoricalDataComponent",
config: [
"date",
{ id: "isMotivatedDuringClass", visibleFrom: "lg" },
{ id: "isParticipatingInClass", visibleFrom: "lg" },
{ id: "isInteractingWithOthers", visibleFrom: "lg" },
{ id: "doesHomework", visibleFrom: "lg" },
{ id: "asksQuestions", visibleFrom: "lg" },
{id: "isMotivatedDuringClass", visibleFrom: "lg"},
{id: "isParticipatingInClass", visibleFrom: "lg"},
{id: "isInteractingWithOthers", visibleFrom: "lg"},
{id: "doesHomework", visibleFrom: "lg"},
{id: "asksQuestions", visibleFrom: "lg"},
]
}
]
Expand Down Expand Up @@ -740,9 +741,9 @@ export const defaultJsonConfig = {
},
],
"exportConfig": [
{ label: "Title", query: "title" },
{ label: "Type", query: "type" },
{ label: "Assigned users", query: "assignedTo" }
{label: "Title", query: "title"},
{label: "Type", query: "type"},
{label: "Assigned users", query: "assignedTo"}
]
}
},
Expand Down Expand Up @@ -857,7 +858,7 @@ export const defaultJsonConfig = {
"aggregationDefinitions": [
{
"query": `${EventNote.ENTITY_TYPE}:toArray[* date >= ? & date <= ?]`,
groupBy: { label: "Type", property: "category" },
groupBy: {label: "Type", property: "category"},
"subQueries": [
{
query: ":getAttendanceArray:getAttendanceReport",
Expand Down Expand Up @@ -888,6 +889,26 @@ export const defaultJsonConfig = {
},
],
},
{
"title": $localize`:Name of a report:Materials Distributed`,
"mode": "exporting",
"aggregationDefinitions": [
{
"query": `${EducationalMaterial.ENTITY_TYPE}:toArray[*date >= ? & date <= ?]`,
"groupBy": {label: "Type", property: "materialType"},
"subQueries": [
{
"label": "Number of events of handing out",
"query": `.materialAmount:count`
},
{
"label": "Total Items",
"query": `.materialAmount:sum`
},
]
},
]
}
]
}
},
Expand Down Expand Up @@ -1059,10 +1080,10 @@ export const defaultJsonConfig = {
config: {
rightSide: {
entityType: School.ENTITY_TYPE,
prefilter: { "privateSchool": true },
availableFilters: [{ "id": "language" }],
prefilter: {"privateSchool": true},
availableFilters: [{"id": "language"}],
},
leftSide: { entityType: Child.ENTITY_TYPE },
leftSide: {entityType: Child.ENTITY_TYPE},
}
},
"appConfig:matching-entities": {
Expand All @@ -1089,7 +1110,7 @@ export const defaultJsonConfig = {
"entity": "Todo",
"columns": ["deadline", "subject", "assignedTo", "startDate", "relatedEntities"],
"filters": [
{ "id": "assignedTo" },
{"id": "assignedTo"},

{
"id": "due-status",
Expand Down
14 changes: 14 additions & 0 deletions src/app/core/export/query.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,20 @@ describe("QueryService", () => {
expect(res).toBe(1);
});

it("should allow to sum values", () => {
const data = [
{ a: 1, b: 2 },
{ a: "4" }, // also allows strings
{ b: 5 }, // not existing as 0
{ a: -2 }, // allows negative
{ a: "three" }, // skips invalid
];

const res = service.queryData("a:sum", undefined, undefined, data);

expect(res).toBe(3);
});

function queryData(query: string, from?: Date, to?: Date, data?: any) {
return service
.cacheRequiredData(query, from, to)
Expand Down
14 changes: 14 additions & 0 deletions src/app/core/export/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class QueryService {
toArray: this.toArray,
unique: this.unique,
count: this.count,
sum: this.sum,
addPrefix: this.addPrefix,
toEntities: this.toEntities.bind(this),
getRelated: this.getRelated.bind(this),
Expand Down Expand Up @@ -233,6 +234,19 @@ export class QueryService {
return data ? data.length : 0;
}

/**
* Returns the (integer) sum of the provided array.
* It can also handle integers in strings, e.g. "3"
* @param data and integer array
* @private
*/
private sum(data: any[]): number {
return data.reduce((res, cur) => {
const parsed = Number.parseInt(cur);
return Number.isNaN(parsed) ? res : res + parsed;
}, 0);
}

/**
* Turns a list of ids (with the entity prefix) into a list of entities
* @param ids the array of ids with entity prefix
Expand Down

0 comments on commit b0e2a4d

Please sign in to comment.