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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
934029e0
Commit
934029e0
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Delete old file
parent
9b648955
No related branches found
No related tags found
1 merge request
!6
Search package improvements to retrieve documents with text searches from OpenSearch
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
search/writer_test.go
+0
-121
0 additions, 121 deletions
search/writer_test.go
with
0 additions
and
121 deletions
search/writer_test.go
deleted
100644 → 0
+
0
−
121
View file @
9b648955
package
search_test
import
(
"fmt"
"math/rand"
"sort"
"testing"
"time"
"gitlab.com/uafrica/go-utils/logger"
"gitlab.com/uafrica/go-utils/search"
)
func
TestLocalWriter
(
t
*
testing
.
T
)
{
test
(
t
,
search
.
Config
{
Addresses
:
[]
string
{
"https://localhost:9200"
},
})
}
func
TestDevWriter
(
t
*
testing
.
T
)
{
test
(
t
,
search
.
Config
{
Addresses
:
[]
string
{
"https://search-uafrica-v3-api-logs-fefgiypvmb3sg5wqohgsbqnzvq.af-south-1.es.amazonaws.com/"
},
//from AWS Console OpenSearch Service > Domains > uafrica-v3-api-logs > General Information: Domain Endpoints
Username
:
"uafrica"
,
Password
:
"Aiz}a4ee"
,
})
}
func
test
(
t
*
testing
.
T
,
c
search
.
Config
)
{
logger
.
SetGlobalFormat
(
logger
.
NewConsole
())
logger
.
SetGlobalLevel
(
logger
.
LevelDebug
)
a
,
err
:=
search
.
New
(
c
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to create writer: %+v"
,
err
)
}
indexName
:=
"go-utils-audit-test"
ts
,
err
:=
a
.
TimeSeries
(
indexName
,
testStruct
{})
if
err
!=
nil
{
t
.
Fatalf
(
"failed to create time series: %+v"
,
err
)
}
//write N records
methods
:=
[]
string
{
"GET"
,
"POST"
,
"GET"
,
"PATCH"
,
"GET"
,
"GET"
,
"DELETE"
,
"GET"
,
"GET"
}
//more gets than others
paths
:=
[]
string
{
"/users"
,
"/orders"
,
"/accounts"
,
"/shipment"
,
"/rates"
,
"/accounts"
,
"/shipment"
,
"/rates"
,
"/accounts"
,
"/shipment"
,
"/rates"
,
"/accounts"
,
"/shipment"
,
"/rates"
}
N
:=
100
testTime
:=
time
.
Now
()
.
Add
(
-
time
.
Hour
*
time
.
Duration
(
N
))
for
i
:=
0
;
i
<
N
;
i
++
{
testTime
=
testTime
.
Add
(
time
.
Duration
(
float64
(
rand
.
Intn
(
100
))
/
60.0
*
float64
(
time
.
Hour
)))
method
:=
methods
[
i
%
len
(
methods
)]
path
:=
paths
[
i
%
len
(
paths
)]
if
err
:=
ts
.
Write
(
testTime
,
testTime
.
Add
(
-
time
.
Duration
(
float64
(
time
.
Second
)
*
(
float64
(
rand
.
Intn
(
100
))
/
100.0
+
0.1
))),
testStruct
{
TimeSeriesHeader
:
search
.
TimeSeriesHeader
{},
Test1
:
fmt
.
Sprintf
(
"%d"
,
i
+
1
),
//1,2,3,...
Test2
:
fmt
.
Sprintf
(
"ACC_%05d"
,
93
+
i
%
7
),
//ACC_00093..ACC00100
Test3
:
i
%
3
+
8
,
//8,9, or 10
HTTP
:
httpData
{
Method
:
method
,
Path
:
path
,
},
HTTPMethod
:
method
,
HTTPPath
:
path
,
});
err
!=
nil
{
t
.
Fatalf
(
"failed to add doc: %+v"
,
err
)
}
}
docs
,
totalCount
,
err
:=
ts
.
Search
(
10
)
if
err
!=
nil
{
t
.
Errorf
(
"failed to search: %+v"
,
err
)
}
else
{
t
.
Logf
(
"search: %d: %+v"
,
totalCount
,
docs
)
}
oldList
,
err
:=
a
.
DelOldTimeSeries
(
indexName
,
2
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to del old: %+v"
,
err
)
}
sort
.
Slice
(
oldList
,
func
(
i
,
j
int
)
bool
{
return
oldList
[
i
]
<
oldList
[
j
]
})
t
.
Logf
(
"Deleted %d old series"
,
len
(
oldList
))
//indexes deleted depends on current time, so not verifying
// if len(oldList) != 2 || oldList[0] != "go-utils-audit-test-20211029" || oldList[1] != "go-utils-audit-test-20211030" {
// t.Fatalf("Did not delete expected indices")
// }
t
.
Logf
(
"Done"
)
}
type
testStruct
struct
{
search
.
TimeSeriesHeader
Test1
string
`json:"test1"`
Test2
string
`json:"test2"`
Test3
int
`json:"test3"`
HTTP
httpData
`json:"http"`
HTTPMethod
string
`json:"http_method" search:"keyword"`
HTTPPath
string
`json:"http_path" search:"keyword"`
}
type
httpData
struct
{
Method
string
`json:"method" search:"keyword"`
Path
string
`json:"path" search:"keyword"`
}
func
TestOlderThan
(
t
*
testing
.
T
)
{
local
:=
time
.
Now
()
.
Location
()
//time now for test (using a fixed value in SAST)
t0
,
_
:=
time
.
ParseInLocation
(
"2006-01-02 15:04:05"
,
"2021-10-20 14:15:16"
,
local
)
t
.
Logf
(
"t0=%s"
,
t0
)
//threshold is 2 days older, applying at midnight in location SAST
olderThanDays
:=
2
t
.
Logf
(
"n=%d"
,
olderThanDays
)
t1
:=
time
.
Date
(
t0
.
Year
(),
t0
.
Month
(),
t0
.
Day
(),
0
,
0
,
0
,
0
,
local
)
t
.
Logf
(
"t1=%s"
,
t1
)
t1
=
t1
.
Add
(
-
time
.
Hour
*
24
*
time
.
Duration
(
olderThanDays
))
t
.
Logf
(
"Threshold = %s"
,
t1
)
}
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