Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
eb3c5261
Unverified
Commit
eb3c5261
authored
7 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
Add chunking to re-humanize-names!
parent
8ecdc61c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/models/humanization.clj
+29
-16
29 additions, 16 deletions
src/metabase/models/humanization.clj
with
29 additions
and
16 deletions
src/metabase/models/humanization.clj
+
29
−
16
View file @
eb3c5261
...
...
@@ -32,22 +32,22 @@
mainly for the internal implementation):
(humanization-strategy :advanced)
(name->human-readable-name \"cooltoucans\") ;-> \"Cool Toucans\"
(name->human-readable-name \"cooltoucans\")
;-> \"Cool Toucans\"
;; this is the same as:
(name->human-readable-name (humanization-strategy) \"cooltoucans\") ;-> \"Cool Toucans\"
;; specifiy a different strategy:
(name->human-readable-name :none \"cooltoucans\") ;-> \"cooltoucans\""
(name->human-readable-name :none \"cooltoucans\")
;-> \"cooltoucans\""
{
:arglists
'
([
s
]
[
strategy
s
])}
(
fn
([
_
]
(
keyword
(
humanization-strategy
)))
([
strategy
_
]
(
keyword
strategy
))))
;; :advanced is the default implementation
;; :advanced is the default implementation
; splits words with cost-based fn
(
defmethod
name->human-readable-name
:advanced
([
s
]
(
name->human-readable-name
:advanced
s
))
([
_,
^
String
s
]
;; explode string on hyphens, underscores, spaces, and camelCase
(
when
(
seq
s
)
;; explode string on spaces, underscores, hyphens, and camelCase
(
str/join
" "
(
for
[
part
(
str/split
s
#
"[-_\s]+|(?<=[a-z])(?=[A-Z])"
)
:when
(
not
(
str/blank?
part
))
word
(
dedupe
(
flatten
(
infer-spaces
part
)))]
...
...
@@ -78,19 +78,32 @@
(
nil?
(
some
#
(
=
display-name
(
%
internal-name
))
(
vals
(
methods
name->human-readable-name
)))))
(
defn-
re-humanize-names!
[
model
]
(
doseq
[{
id
:id,
internal-name
:name,
display-name
:display_name
}
(
db/select
[
model
:id
:name
:display_name
])
:let
[
new-display-name
(
name->human-readable-name
internal-name
)]
:when
(
and
(
not=
display-name
new-display-name
)
(
not
(
custom-display-name?
internal-name
display-name
)))]
(
log/info
(
format
"Updating display name for %s '%s': '%s' -> '%s'"
(
name
model
)
internal-name
display-name
new-display-name
))
(
db/update!
model
id
:display_name
new-display-name
)))
(
defn-
re-humanize-names!
"Update all non-custom display names of all instances of `model` (e.g. Table or Field). To prevent explosions on very
large instances this is done in chunks of 10000 IDs at a time"
;; TODO - once Toucan has the new streaming SELECT functions we can use those instead of manually chunking
;; TODO - Alternatively, just make this chunking pattern generic and add to Toucan. Could be useful elsewhere!
([
model
]
(
re-humanize-names!
model
0
))
([
model
starting-id
]
(
let
[
chunk-size
10000
]
(
when-let
[
instances
(
seq
(
db/select
[
model
:id
:name
:display_name
]
:id
[
:>=
starting-id
]
{
:limit
chunk-size
}))]
(
doseq
[{
id
:id,
internal-name
:name,
display-name
:display_name
}
instances
:let
[
new-display-name
(
name->human-readable-name
internal-name
)]
:when
(
and
(
not=
display-name
new-display-name
)
(
not
(
custom-display-name?
internal-name
display-name
)))]
(
log/info
(
format
"Updating display name for %s '%s': '%s' -> '%s'"
(
name
model
)
internal-name
display-name
new-display-name
))
(
db/update!
model
id
:display_name
new-display-name
))
;; if we got a full chunk, do the next chunk, starting at one past the ID of the last object
(
when
(
>=
(
count
instances
)
chunk-size
)
(
recur
model
(
inc
(
:id
(
last
instances
)))))))))
(
defn-
re-humanize-table-and-field-names!
"Update the display names of all
t
ables in the database using new values obtained from
the (obstensibly toggled
implementation of) `name->human-readable-name`."
"Update the
non-custom
display names of all
T
ables
& Fields
in the database using new values obtained from
the (obstensibly swapped
implementation of) `name->human-readable-name`."
[]
(
re-humanize-names!
'Table
)
(
re-humanize-names!
'Field
))
...
...
@@ -104,7 +117,7 @@
;; ok, now set the new value
(
setting/set-string!
:humanization-strategy
(
name
new-strategy
))
;; now rehumanize all the Tables and Fields using the new strategy.
;; TODO: do this in a background thread because it is potentially slow?
;; TODO:
Should we
do this in a background thread because it is potentially slow?
(
log/info
(
format
"Now using %s table name humanization."
new-strategy
))
(
re-humanize-table-and-field-names!
))
...
...
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