From 1e26b07ce82936f3301c67b3385e1e8082f7830c Mon Sep 17 00:00:00 2001 From: stevereis <stevereis93@gmail.com> Date: Wed, 6 Oct 2021 14:14:12 +0200 Subject: [PATCH] Added model with single tableresult --- .../engine/connectors/exareme/converters.ts | 33 +++++++++++-- .../connectors/exareme/transformations.ts | 46 ++++++++++++++++++- .../models/result/groups-result.model.ts | 4 ++ api/src/schema.gql | 45 ++++++++++-------- 4 files changed, 104 insertions(+), 24 deletions(-) diff --git a/api/src/engine/connectors/exareme/converters.ts b/api/src/engine/connectors/exareme/converters.ts index ce9dd73..09af624 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 4f574db..14dacfa 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 fb11ba5..e3de470 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 a3b02c8..0f1a082 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 -- GitLab