Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Roque
slapos.core
Commits
c713396f
Commit
c713396f
authored
Sep 22, 2017
by
Hardik Juneja
Committed by
Rafael Monnerat
Sep 26, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move Consumption report utils to a new class so it can be reused
parent
ec632b51
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
61 deletions
+86
-61
slapos/collect/reporter.py
slapos/collect/reporter.py
+67
-61
slapos/tests/collect.py
slapos/tests/collect.py
+19
-0
No files found.
slapos/collect/reporter.py
View file @
c713396f
...
...
@@ -128,13 +128,78 @@ def compressLogFolder(log_directory):
finally
:
os
.
chdir
(
initial_folder
)
class
ConsumptionReport
(
object
):
class
ConsumptionReportBase
(
object
):
def
__init__
(
self
,
db
):
self
.
db
=
db
def
getPartitionCPULoadAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"SUM(cpu_percent)"
,
where
=
"partition = '%s'"
%
partition_id
)
cpu_percent_sum
=
zip
(
*
query_result_cursor
)
if
len
(
cpu_percent_sum
)
and
cpu_percent_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
sample_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
sample_amount
)
and
len
(
cpu_percent_sum
):
return
cpu_percent_sum
[
0
][
0
]
/
sample_amount
[
0
][
0
]
def
getPartitionUsedMemoryAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"SUM(memory_rss)"
,
where
=
"partition = '%s'"
%
partition_id
)
memory_sum
=
zip
(
*
query_result_cursor
)
if
len
(
memory_sum
)
and
memory_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
sample_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
sample_amount
)
and
len
(
memory_sum
):
return
memory_sum
[
0
][
0
]
/
sample_amount
[
0
][
0
]
def
getPartitionDiskUsedAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"folder"
,
date_scope
,
columns
=
"SUM(disk_used)"
,
where
=
"partition = '%s'"
%
partition_id
)
disk_used_sum
=
zip
(
*
query_result_cursor
)
if
len
(
disk_used_sum
)
and
disk_used_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"folder"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
collect_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
collect_amount
)
and
len
(
disk_used_sum
):
return
disk_used_sum
[
0
][
0
]
/
collect_amount
[
0
][
0
]
class
ConsumptionReport
(
ConsumptionReportBase
):
def
__init__
(
self
,
database
,
computer_id
,
location
,
user_list
):
self
.
computer_id
=
computer_id
self
.
db
=
database
self
.
user_list
=
user_list
self
.
location
=
location
ConsumptionReportBase
.
__init__
(
self
.
db
)
def
buildXMLReport
(
self
,
date_scope
):
...
...
@@ -257,65 +322,6 @@ class ConsumptionReport(object):
self
.
db
.
close
()
return
zer
def
_getPartitionCPULoadAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"SUM(cpu_percent)"
,
where
=
"partition = '%s'"
%
partition_id
)
cpu_percent_sum
=
zip
(
*
query_result_cursor
)
if
len
(
cpu_percent_sum
)
and
cpu_percent_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
sample_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
sample_amount
)
and
len
(
cpu_percent_sum
):
return
cpu_percent_sum
[
0
][
0
]
/
sample_amount
[
0
][
0
]
def
_getPartitionUsedMemoryAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"SUM(memory_rss)"
,
where
=
"partition = '%s'"
%
partition_id
)
memory_sum
=
zip
(
*
query_result_cursor
)
if
len
(
memory_sum
)
and
memory_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"user"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
sample_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
sample_amount
)
and
len
(
memory_sum
):
return
memory_sum
[
0
][
0
]
/
sample_amount
[
0
][
0
]
def
_getPartitionDiskUsedAverage
(
self
,
partition_id
,
date_scope
):
self
.
db
.
connect
()
query_result_cursor
=
self
.
db
.
select
(
"folder"
,
date_scope
,
columns
=
"SUM(disk_used)"
,
where
=
"partition = '%s'"
%
partition_id
)
disk_used_sum
=
zip
(
*
query_result_cursor
)
if
len
(
disk_used_sum
)
and
disk_used_sum
[
0
][
0
]
is
None
:
return
query_result_cursor
=
self
.
db
.
select
(
"folder"
,
date_scope
,
columns
=
"COUNT(DISTINCT time)"
,
where
=
"partition = '%s'"
%
partition_id
)
collect_amount
=
zip
(
*
query_result_cursor
)
self
.
db
.
close
()
if
len
(
collect_amount
)
and
len
(
disk_used_sum
):
return
disk_used_sum
[
0
][
0
]
/
collect_amount
[
0
][
0
]
class
Journal
(
object
):
def
__init__
(
self
):
...
...
slapos/tests/collect.py
View file @
c713396f
...
...
@@ -101,6 +101,25 @@ class TestCollectDatabase(unittest.TestCase):
self
.
assertTrue
(
os
.
path
.
exists
(
"%s/collector.db"
%
self
.
instance_root
))
def
test_database_select
(
self
):
def
_fake_execute
(
sql
):
return
sql
database
=
db
.
Database
(
self
.
instance_root
)
database
.
connect
()
original_execute
=
database
.
_execute
try
:
database
.
_execute
=
_fake_execute
self
.
assertEquals
(
"SELECT * FROM user "
,
database
.
select
(
'user'
))
self
.
assertEquals
(
"SELECT DATE FROM user WHERE date = '0.1' AND time=
\
"
00:02
\
"
"
,
database
.
select
(
'user'
,
0.1
,
'DATE'
,
'time="00:02"'
))
self
.
assertEquals
(
"SELECT DATE FROM user WHERE date = '0.1' GROUP BY time ORDER BY date"
,
database
.
select
(
'user'
,
0.1
,
'DATE'
,
order
=
'date'
,
group
=
'time'
))
self
.
assertEquals
(
"SELECT DATE FROM user WHERE date = '0.1' limit 1"
,
database
.
select
(
'user'
,
0.1
,
'DATE'
,
limit
=
1
))
finally
:
database
.
_execute
=
original_execute
database
.
close
()
def
test_insert_user_snapshot
(
self
):
database
=
db
.
Database
(
self
.
instance_root
)
...
...
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