diff --git a/api/src/engine/connectors/exareme/converters.ts b/api/src/engine/connectors/exareme/converters.ts index ce9dd731dae295d1ccffa03e20da3feef30263cd..09af624a974cdbef0be6453904fdc617006a6de1 100644 --- a/api/src/engine/connectors/exareme/converters.ts +++ b/api/src/engine/connectors/exareme/converters.ts @@ -4,15 +4,22 @@ import { Experiment } from 'src/engine/models/experiment/experiment.model'; import { ExperimentCreateInput } from 'src/engine/models/experiment/input/experiment-create.input'; import { Group } from 'src/engine/models/group.model'; import { ResultUnion } from 'src/engine/models/result/common/result-union.model'; +import { + GroupResult, + GroupsResult, +} from 'src/engine/models/result/groups-result.model'; import { RawResult } from 'src/engine/models/result/raw-result.model'; -import { TableResult } from 'src/engine/models/result/table-result.model'; import { Variable } from 'src/engine/models/variable.model'; import { Entity } from './interfaces/entity.interface'; import { ExperimentData } from './interfaces/experiment/experiment.interface'; import { ResultExperiment } from './interfaces/experiment/result-experiment.interface'; import { Hierarchy } from './interfaces/hierarchy.interface'; import { VariableEntity } from './interfaces/variable-entity.interface'; -import { transformToExperiment, transientToTable } from './transformations'; +import { + descriptiveModelToTables, + descriptiveSingleToTables, + transformToExperiment, +} from './transformations'; export const dataToGroup = (data: Hierarchy): Group => { return { @@ -80,8 +87,24 @@ export const experimentInputToData = (data: ExperimentCreateInput) => { export const descriptiveDataToTableResult = ( data: ResultExperiment, -): TableResult[] => { - return transientToTable.evaluate(data); +): GroupsResult[] => { + const result = new GroupsResult(); + + result.groups = [ + new GroupResult({ + name: 'Single', + results: descriptiveSingleToTables.evaluate(data), + }), + ]; + + result.groups.push( + new GroupResult({ + name: 'Model', + results: descriptiveModelToTables.evaluate(data), + }), + ); + + return [result]; }; export const dataToExperiment = (data: ExperimentData): Experiment => { @@ -98,6 +121,8 @@ export const dataToExperiment = (data: ExperimentData): Experiment => { .flat() : []; + console.log(exp.results); + return exp; }; diff --git a/api/src/engine/connectors/exareme/transformations.ts b/api/src/engine/connectors/exareme/transformations.ts index 4f574db08df84e0023b924ec987809d025e6e4e1..14dacfa33f1cc1528c6c4f46a90b13f4ae67d2b2 100644 --- a/api/src/engine/connectors/exareme/transformations.ts +++ b/api/src/engine/connectors/exareme/transformations.ts @@ -34,7 +34,50 @@ export const transformToExperiment = jsonata(` ) `); -export const transientToTable = jsonata(` +export const descriptiveModelToTables = jsonata(` +( + $e := function($x, $r) {($x != null) ? $x : ($r ? $r : '')}; + + $fn := function($o, $prefix) { + $type($o) = 'object' ? + $each($o, function($v, $k) {( + $type($v) = 'object' ? { $k: $v.count & ' (' & $v.percentage & '%)' } : { + $k: $v + } + )}) ~> $merge() + : {} + }; + + $vars := $count(data.single.*)-1; + $varName := $keys(data.single.*); + $model := data.model; + + [[0..$vars].( + $i := $; + $ks := $keys($model.*.data.*[$i][$type($) = 'object']); + { + 'name': $varName[$i], + 'headers': $append("", $keys($$.data.model)).{ + 'name': $, + 'type': 'string' + }, + 'data': [ + [$varName[$i], $model.*.($e(num_total))], + ['Datapoints', $model.*.($e(num_datapoints))], + ['Nulls', $model.*.($e(num_nulls))], + $model.*.data.($fn($.*[$i])) ~> $reduce(function($a, $b) { + $map($ks, function($k) {( + { + $k : [$e($lookup($a,$k), "No data"), $e($lookup($b,$k), "No data")] + } + )}) ~> $merge() + }) ~> $each(function($v, $k) {$append($k,$v)}) + ] + } + )] +)`); + +export const descriptiveSingleToTables = jsonata(` ( $e := function($x, $r) {($x != null) ? $x : ($r ? $r : '')}; @@ -52,7 +95,6 @@ export const transientToTable = jsonata(` $.single.*@$p#$i.( $ks := $keys($p.*.data[$type($) = 'object']); { - 'groupBy' : 'single', 'name': $keys(%)[$i], 'headers': $append("", $keys(*)).{ 'name': $, diff --git a/api/src/engine/models/result/groups-result.model.ts b/api/src/engine/models/result/groups-result.model.ts index fb11ba57403c420f7c3a5be903fdd4d9e8ee6686..e3de47038bb69b09615831c8c5dac1cf456d93df 100644 --- a/api/src/engine/models/result/groups-result.model.ts +++ b/api/src/engine/models/result/groups-result.model.ts @@ -4,6 +4,10 @@ import { Result } from './common/result.model'; @ObjectType() export class GroupResult { + public constructor(init?: Partial<GroupResult>) { + Object.assign(this, init); + } + @Field() name: string; diff --git a/api/src/schema.gql b/api/src/schema.gql index a3b02c8a942a8f5a0f0354bae1a0dc6998766bfe..0f1a08208f5ba53e6378446eb9b3db77733a71ca 100644 --- a/api/src/schema.gql +++ b/api/src/schema.gql @@ -34,6 +34,33 @@ type Domain { rootGroup: Group! } +type GroupResult { + name: String! + results: [ResultUnion!]! +} + +union ResultUnion = TableResult | RawResult | GroupsResult + +type TableResult { + name: String! + data: [[String!]!]! + headers: [Header!]! +} + +type RawResult { + data: JSONObject! + listMax: [String!]! +} + +""" +The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). +""" +scalar JSONObject @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf") + +type GroupsResult { + groups: [GroupResult!]! +} + type Header { name: String! type: String! @@ -68,24 +95,6 @@ type Experiment { name: String! } -union ResultUnion = TableResult | RawResult - -type TableResult { - name: String! - data: [[String!]!]! - headers: [Header!]! -} - -type RawResult { - data: JSONObject! - listMax: [String!]! -} - -""" -The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). -""" -scalar JSONObject @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf") - type PartialExperiment { uuid: String author: String