Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
blendb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pdts20
blendb
Commits
783b05d8
Commit
783b05d8
authored
Apr 25, 2019
by
Felipe Wu
Browse files
Options
Downloads
Patches
Plain Diff
[007] Finalizado saida no estilo padrao
Signed-off-by:
Felipe Wu
<
felipeshiwu@gmail.com
>
parent
0ce1a94d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/adapter/elasticsearch.ts
+72
-21
72 additions, 21 deletions
src/adapter/elasticsearch.ts
with
72 additions
and
21 deletions
src/adapter/elasticsearch.ts
+
72
−
21
View file @
783b05d8
...
...
@@ -12,6 +12,17 @@ const elasticsearch = require('elasticsearch');
/**
* Adapter which connects with a Elasticsearch database.
*/
interface
ViewOperation
{
opcode
?:
number
;
values
?:
any
[];
clauses
?:
any
[];
sort
?:
any
[];
origin
?:
boolean
;
operation
?:
object
;
id
?:
string
;
name
?:
string
;
}
export
class
ElasticsearchAdapter
extends
Adapter
{
/** Information used to connect with a PostgreSQL database. */
private
client
:
Client
;
...
...
@@ -57,7 +68,6 @@ export class ElasticsearchAdapter extends Adapter {
let
partialQuery
=
this
.
translateAggs
(
view
,
0
);
Object
.
assign
(
query
,
{
aggs
:
partialQuery
});
//console.log(JSON.stringify(view, null, 2));
return
query
;
}
...
...
@@ -66,42 +76,32 @@ export class ElasticsearchAdapter extends Adapter {
* In other words perform a SELECT query.
* @param view - "Location" from all data should be read.
* @param cb - Callback function which contains the data read.
* @param cb.error - Error information when the method fails.
* @param cb.result - Data got from view.
*/
public
getDataFromView
(
view
:
View
,
cb
:
(
error
:
Error
,
result
?:
any
[])
=>
void
):
void
{
const
query
=
this
.
getQueryFromView
(
view
);
if
(
view
.
clauses
.
length
>
0
||
view
.
dimensions
.
length
==
1
)
{
this
.
executeQuery
(
view
.
operation
.
values
[
0
].
operation
.
values
[
0
].
name
,
query
,
cb
);
}
else
{
this
.
executeQuery
(
view
.
operation
.
values
[
0
].
name
,
query
,
cb
);
}
let
partialView
=
view
.
operation
;
const
index
=
this
.
findIndex
(
partialView
);
this
.
executeQuery
(
index
,
view
,
query
,
cb
);
}
/**
* Asynchronously executes a query and get its result.
* @param query - Query (S
Q
L format) to be executed.
* @param query - Query (
Elasticsearch D
SL format) to be executed.
* @param cb - Callback function which contains the data read.
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
private
executeQuery
(
index
:
string
,
query
:
object
,
cb
:
(
error
:
Error
,
result
?:
any
[])
=>
void
):
void
{
private
executeQuery
(
index
:
string
,
view
:
View
,
query
:
object
,
cb
:
(
error
:
Error
,
result
?:
any
[])
=>
void
):
void
{
this
.
client
.
search
({
index
:
index
.
replace
(
/
\"
/g
,
''
),
body
:
query
},(
err
,
result
)
=>
{
cb
(
err
,
[
result
]);
let
displayResult
=
this
.
formateResult
(
view
,
[
result
],
0
);
cb
(
err
,
displayResult
);
})
}
/**
* Asynchronously insert one register into a given Source.
* @param source - Insertion "location".
* @param cb - Callback function which contains the query result.
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
public
insertIntoSource
(
source
:
Source
,
data
:
any
[],
cb
:
(
err
:
Error
,
result
?:
any
[])
=>
void
):
void
{
}
...
...
@@ -123,6 +123,18 @@ export class ElasticsearchAdapter extends Adapter {
}
/** Elasticsearch, unlike the others adapter, need to find its index to do the query,
* so need to use aliasAsName in the config.yaml and run the recursive function
* below to find the index.
*/
private
findIndex
(
operation
:
ViewOperation
):
string
{
if
(
operation
.
values
[
0
].
origin
==
true
)
{
return
operation
.
values
[
0
].
name
;
}
let
index
=
this
.
findIndex
(
operation
.
values
[
0
].
operation
);
return
index
;
}
private
translateAggs
(
view
:
View
,
numDimensions
:
number
):
object
{
if
(
numDimensions
==
view
.
dimensions
.
length
)
{
let
func
=
""
;
...
...
@@ -130,7 +142,7 @@ export class ElasticsearchAdapter extends Adapter {
let
partialQuery
=
{};
for
(
let
i
=
0
;
i
<
view
.
metrics
.
length
;
i
++
)
{
func
=
this
.
getAggregateFunction
(
view
.
metrics
[
i
].
aggregation
);
aggrName
=
view
.
metrics
[
i
].
name
.
toLowerCase
()
;
aggrName
=
view
.
metrics
[
i
].
name
;
Object
.
assign
(
partialQuery
,
{
[
aggrName
]:
{[
func
]:
{
field
:
view
.
metrics
[
i
].
name
}}});
}
return
partialQuery
;
...
...
@@ -138,7 +150,7 @@ export class ElasticsearchAdapter extends Adapter {
let
returnedQuery
=
this
.
translateAggs
(
view
,
numDimensions
+
1
);
if
(
view
.
dimensions
.
length
>
0
)
{
let
group_by
=
"
group_by_
"
+
view
.
dimensions
[
numDimensions
].
name
;
let
group_by
=
view
.
dimensions
[
numDimensions
].
name
;
let
dim
=
view
.
dimensions
[
numDimensions
].
name
;
if
(
view
.
dimensions
[
numDimensions
].
dataType
<
3
)
{
dim
=
dim
;
...
...
@@ -158,11 +170,50 @@ export class ElasticsearchAdapter extends Adapter {
Object
.
assign
(
aggregation
[
group_by
],
{
aggs
:
returnedQuery
});
if
(
numDimensions
==
view
.
dimensions
.
length
-
1
&&
view
.
sort
.
length
>
0
)
{
let
aggrName
=
view
.
sort
[
0
].
name
.
toLowerCase
()
;
let
aggrName
=
view
.
sort
[
0
].
name
;
Object
.
assign
(
aggregation
[
group_by
].
terms
,
{
order
:
{[
aggrName
]
:
"
asc
"
}});
}
return
aggregation
;
}
}
private
formateResult
(
view
:
View
,
result
:
any
[],
numDimensions
:
number
):
any
[]
{
let
dimensionsResult
=
{};
let
back
=
[];
let
resultArray
:
any
[]
=
[];
if
(
numDimensions
==
0
&&
view
.
dimensions
.
length
>
0
)
{
let
firstDim
=
view
.
dimensions
[
0
].
name
;
let
resultArray
:
any
[]
=
[];
for
(
let
i
=
0
;
i
<
result
[
0
].
aggregations
[
firstDim
].
buckets
.
length
;
i
++
)
{
back
=
this
.
formateResult
(
view
,
[
result
[
0
].
aggregations
[
firstDim
].
buckets
[
i
]],
1
);
for
(
let
j
=
0
;
j
<
back
.
length
;
j
++
)
{
Object
.
assign
(
back
[
j
],
{[
firstDim
]:
result
[
0
].
aggregations
[
firstDim
].
buckets
[
i
].
key
});
}
resultArray
=
resultArray
.
concat
(
back
);
}
return
resultArray
;
}
if
(
numDimensions
==
view
.
dimensions
.
length
)
{
let
metricsResult
=
{};
let
findPrefix
=
result
[
0
];
if
(
view
.
dimensions
.
length
==
0
)
{
findPrefix
=
result
[
0
].
aggregations
;
}
for
(
let
i
=
0
;
i
<
view
.
metrics
.
length
;
i
++
)
{
let
met
=
view
.
metrics
[
i
].
name
;
Object
.
assign
(
metricsResult
,
{[
met
]:
findPrefix
[
met
].
value
});
}
return
[
metricsResult
];
}
let
nDim
=
view
.
dimensions
[
numDimensions
].
name
;
for
(
let
i
=
0
;
i
<
result
[
0
][
nDim
].
buckets
.
length
;
i
++
)
{
back
=
this
.
formateResult
(
view
,
[
result
[
0
][
nDim
].
buckets
[
i
]],
numDimensions
+
1
);
for
(
let
j
=
0
;
j
<
back
.
length
;
j
++
)
{
Object
.
assign
(
back
[
j
],
{[
nDim
]:
result
[
0
][
nDim
].
buckets
[
i
].
key
});
}
resultArray
=
resultArray
.
concat
(
back
);
}
return
resultArray
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment