Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
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
Container 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
Bob Public Utils
bobgroup-go-utils
Commits
83f3c398
Commit
83f3c398
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Add Get() method to time series
parent
8b95a285
No related branches found
No related tags found
1 merge request
!14
Resolve "Return unique id for search documents"
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
search/time_series.go
+61
-4
61 additions, 4 deletions
search/time_series.go
with
61 additions
and
4 deletions
search/time_series.go
+
61
−
4
View file @
83f3c398
...
...
@@ -59,6 +59,7 @@ type timeSeries struct {
createdDates
map
[
string
]
bool
searchResponseBodyType
reflect
.
Type
getResponseBodyType
reflect
.
Type
}
//purpose:
...
...
@@ -132,6 +133,18 @@ func (w *writer) TimeSeries(name string, tmpl interface{}) (TimeSeries, error) {
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed to make search response type for time-series"
)
}
//define get response type
//similar to GetResponseBody
ts
.
getResponseBodyType
,
err
=
reflection
.
CloneType
(
reflect
.
TypeOf
(
GetResponseBody
{}),
map
[
string
]
reflect
.
Type
{
"._source"
:
ts
.
dataType
,
})
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed to make get response type for time-series"
)
}
w
.
timeSeriesByName
[
name
]
=
ts
return
ts
,
nil
}
...
...
@@ -452,12 +465,56 @@ func (ts *timeSeries) Search(query Query, limit int64) (docs map[string]interfac
docs
=
map
[
string
]
interface
{}{}
for
i
:=
0
;
i
<
hits
.
Len
();
i
++
{
hit
:=
hits
.
Index
(
i
)
index
:=
hit
.
Field
(
0
)
.
Interface
()
.
(
string
)
//HitDoc.Index
id
:=
hit
.
Field
(
2
)
.
Interface
()
.
(
string
)
//HitDoc.ID
docs
[
id
]
=
hit
.
Field
(
4
)
.
Interface
()
//HitDoc.Source
docs
[
index
+
"/"
+
id
]
=
hit
.
Field
(
4
)
.
Interface
()
//HitDoc.Source
}
return
docs
,
hitsTotalValue
.
Interface
()
.
(
int
),
nil
}
func
(
ts
*
timeSeries
)
Get
(
id
string
)
(
interface
{},
error
)
{
return
nil
,
errors
.
Errorf
(
"NYI"
)
func
(
ds
*
timeSeries
)
Get
(
id
string
)
(
doc
interface
{},
err
error
)
{
if
ds
==
nil
{
return
nil
,
errors
.
Errorf
(
"document store == nil"
)
}
get
:=
opensearchapi
.
GetRequest
{
Index
:
ds
.
name
,
DocumentType
:
"_doc"
,
DocumentID
:
id
,
}
getResponse
,
err
:=
get
.
Do
(
context
.
Background
(),
ds
.
w
.
client
)
if
err
!=
nil
{
err
=
errors
.
Wrapf
(
err
,
"failed to get document"
)
return
}
switch
getResponse
.
StatusCode
{
case
http
.
StatusOK
:
default
:
resBody
,
_
:=
ioutil
.
ReadAll
(
getResponse
.
Body
)
err
=
errors
.
Errorf
(
"Get failed with HTTP status %v: %s"
,
getResponse
.
StatusCode
,
string
(
resBody
))
return
}
resBodyPtrValue
:=
reflect
.
New
(
ds
.
getResponseBodyType
)
if
err
=
json
.
NewDecoder
(
getResponse
.
Body
)
.
Decode
(
resBodyPtrValue
.
Interface
());
err
!=
nil
{
err
=
errors
.
Wrapf
(
err
,
"cannot decode get response body"
)
return
}
foundVar
,
err
:=
reflection
.
Get
(
resBodyPtrValue
,
".found"
)
if
err
!=
nil
{
err
=
errors
.
Wrapf
(
err
,
"cannot get found value"
)
return
}
if
found
,
ok
:=
foundVar
.
Interface
()
.
(
bool
);
!
ok
||
!
found
{
return
nil
,
nil
//not found
}
//found
source
,
err
:=
reflection
.
Get
(
resBodyPtrValue
,
"._source"
)
if
err
!=
nil
{
err
=
errors
.
Wrapf
(
err
,
"cannot get document from get response"
)
return
}
return
source
.
Interface
(),
nil
}
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