Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
5b4827fe
Commit
5b4827fe
authored
Jul 24, 2018
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document site statistics API
parent
32d96d48
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
0 deletions
+21
-0
app/models/site_statistic.rb
app/models/site_statistic.rb
+21
-0
No files found.
app/models/site_statistic.rb
View file @
5b4827fe
...
@@ -5,18 +5,28 @@ class SiteStatistic < ActiveRecord::Base
...
@@ -5,18 +5,28 @@ class SiteStatistic < ActiveRecord::Base
COUNTER_ATTRIBUTES
=
%w(repositories_count wikis_count)
.
freeze
COUNTER_ATTRIBUTES
=
%w(repositories_count wikis_count)
.
freeze
REQUIRED_SCHEMA_VERSION
=
20180629153018
REQUIRED_SCHEMA_VERSION
=
20180629153018
# Tracks specific attribute
#
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
def
self
.
track
(
raw_attribute
)
def
self
.
track
(
raw_attribute
)
with_statistics_available
(
raw_attribute
)
do
|
attribute
|
with_statistics_available
(
raw_attribute
)
do
|
attribute
|
SiteStatistic
.
update_all
([
"
#{
attribute
}
=
#{
attribute
}
+1"
])
SiteStatistic
.
update_all
([
"
#{
attribute
}
=
#{
attribute
}
+1"
])
end
end
end
end
# Untracks specific attribute
#
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
def
self
.
untrack
(
raw_attribute
)
def
self
.
untrack
(
raw_attribute
)
with_statistics_available
(
raw_attribute
)
do
|
attribute
|
with_statistics_available
(
raw_attribute
)
do
|
attribute
|
SiteStatistic
.
update_all
([
"
#{
attribute
}
=
#{
attribute
}
-1 WHERE
#{
attribute
}
> 0"
])
SiteStatistic
.
update_all
([
"
#{
attribute
}
=
#{
attribute
}
-1 WHERE
#{
attribute
}
> 0"
])
end
end
end
end
# Wrapper for track/untrack operations with basic validations and enforced requirements
#
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
# @yield [String] attribute quoted to be used inside SQL / Arel query
def
self
.
with_statistics_available
(
raw_attribute
)
def
self
.
with_statistics_available
(
raw_attribute
)
unless
raw_attribute
.
in?
(
COUNTER_ATTRIBUTES
)
unless
raw_attribute
.
in?
(
COUNTER_ATTRIBUTES
)
raise
ArgumentError
,
"Invalid attribute: '
#{
raw_attribute
}
' to '
#{
caller_locations
(
1
,
1
)[
0
].
label
}
' method. "
\
raise
ArgumentError
,
"Invalid attribute: '
#{
raw_attribute
}
' to '
#{
caller_locations
(
1
,
1
)[
0
].
label
}
' method. "
\
...
@@ -33,6 +43,9 @@ class SiteStatistic < ActiveRecord::Base
...
@@ -33,6 +43,9 @@ class SiteStatistic < ActiveRecord::Base
yield
(
attribute
)
yield
(
attribute
)
end
end
# Returns a site statistic record with tracked information
#
# @return [SiteStatistic] record with tracked information
def
self
.
fetch
def
self
.
fetch
SiteStatistic
.
transaction
(
requires_new:
true
)
do
SiteStatistic
.
transaction
(
requires_new:
true
)
do
SiteStatistic
.
first_or_create!
SiteStatistic
.
first_or_create!
...
@@ -41,10 +54,18 @@ class SiteStatistic < ActiveRecord::Base
...
@@ -41,10 +54,18 @@ class SiteStatistic < ActiveRecord::Base
retry
retry
end
end
# Return whether required schema change is available
#
# This is needed in order to degrade gracefully when testing schema migrations
#
# @return [Boolean] whether schema is available
def
self
.
available?
def
self
.
available?
@available_flag
||=
ActiveRecord
::
Migrator
.
current_version
>=
REQUIRED_SCHEMA_VERSION
@available_flag
||=
ActiveRecord
::
Migrator
.
current_version
>=
REQUIRED_SCHEMA_VERSION
end
end
# Resets cached column information
#
# This is called during schema migration specs, in order to reset internal cache state
def
self
.
reset_column_information
def
self
.
reset_column_information
@available_flag
=
nil
@available_flag
=
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment