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
206e4e45
Commit
206e4e45
authored
11 months ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Add db_utils package with a function for absolute date string query
parent
682383b2
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
db_utils/db_utils.go
+81
-0
81 additions, 0 deletions
db_utils/db_utils.go
with
81 additions
and
0 deletions
db_utils/db_utils.go
0 → 100644
+
81
−
0
View file @
206e4e45
package
db_utils
import
(
"fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/date_utils"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils"
"regexp"
"strings"
"time"
)
// Special query cases
const
(
LastXHours
string
=
"^last
\\
d+ hour(s)?$"
LastDay
string
=
"^last day$"
LastXDays
string
=
"^last
\\
d+ day(s)?$"
LastMonth
string
=
"^last month$"
Yesterday
string
=
"^yesterday$"
)
func
checkAbsoluteRegex
(
regex
string
,
value
string
)
bool
{
matchesRegex
,
err
:=
regexp
.
Match
(
regex
,
[]
byte
(
value
))
if
matchesRegex
&&
err
==
nil
{
return
true
}
return
false
}
func
pullAbsoluteValue
(
value
string
)
int64
{
reg
,
err
:=
regexp
.
Compile
(
"[a-zA-Z]+"
)
if
err
!=
nil
{
return
0
}
value
=
reg
.
ReplaceAllString
(
value
,
""
)
value
=
strings
.
TrimSpace
(
value
)
val
,
_
:=
number_utils
.
StringToInt64
(
value
)
return
val
}
func
AbsoluteDateStringQuery
(
absolute
string
)
string
{
var
value
int64
timeNow
:=
date_utils
.
CurrentDate
()
if
checkAbsoluteRegex
(
LastXHours
,
absolute
)
{
value
=
pullAbsoluteValue
(
absolute
)
since
:=
timeNow
.
Add
(
time
.
Duration
(
-
value
)
*
time
.
Hour
)
return
fmt
.
Sprintf
(
" >= '%v'"
,
date_utils
.
DateDBFormattedString
(
since
))
}
if
checkAbsoluteRegex
(
LastDay
,
absolute
)
{
startDay
:=
time
.
Date
(
timeNow
.
Year
(),
timeNow
.
Month
(),
timeNow
.
Day
(),
0
,
0
,
0
,
0
,
date_utils
.
CurrentLocation
())
endDay
:=
time
.
Date
(
timeNow
.
Year
(),
timeNow
.
Month
(),
timeNow
.
Day
(),
23
,
59
,
59
,
999999999
,
date_utils
.
CurrentLocation
())
return
fmt
.
Sprintf
(
" BETWEEN '%v' AND '%v'"
,
date_utils
.
DateDBFormattedString
(
startDay
),
date_utils
.
DateDBFormattedString
(
endDay
))
}
if
checkAbsoluteRegex
(
LastXDays
,
absolute
)
{
value
=
pullAbsoluteValue
(
absolute
)
since
:=
timeNow
.
AddDate
(
0
,
0
,
-
int
(
value
))
return
fmt
.
Sprintf
(
" >= '%v'"
,
date_utils
.
DateDBFormattedString
(
since
))
}
if
checkAbsoluteRegex
(
LastMonth
,
absolute
)
{
firstOfMonth
:=
time
.
Date
(
timeNow
.
Year
(),
timeNow
.
Month
(),
1
,
0
,
0
,
0
,
0
,
date_utils
.
CurrentLocation
())
lastDay
:=
firstOfMonth
.
AddDate
(
0
,
1
,
-
1
)
lastOfMonth
:=
time
.
Date
(
lastDay
.
Year
(),
lastDay
.
Month
(),
lastDay
.
Day
(),
23
,
59
,
59
,
999999999
,
date_utils
.
CurrentLocation
())
return
fmt
.
Sprintf
(
" BETWEEN '%v' AND '%v'"
,
date_utils
.
DateDBFormattedString
(
firstOfMonth
),
date_utils
.
DateDBFormattedString
(
lastOfMonth
))
}
if
checkAbsoluteRegex
(
Yesterday
,
absolute
)
{
yesterday
:=
timeNow
.
AddDate
(
0
,
0
,
-
1
)
startDay
:=
time
.
Date
(
yesterday
.
Year
(),
yesterday
.
Month
(),
yesterday
.
Day
(),
0
,
0
,
0
,
0
,
date_utils
.
CurrentLocation
())
endDay
:=
time
.
Date
(
yesterday
.
Year
(),
yesterday
.
Month
(),
yesterday
.
Day
(),
23
,
59
,
59
,
999999999
,
date_utils
.
CurrentLocation
())
return
fmt
.
Sprintf
(
" BETWEEN '%v' AND '%v'"
,
date_utils
.
DateDBFormattedString
(
startDay
),
date_utils
.
DateDBFormattedString
(
endDay
))
}
// Not matched, so try to match it literally (It'll probably break and return nothing, but that's good)
return
fmt
.
Sprintf
(
" = '%v'"
,
absolute
)
}
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