Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mock-cheops
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
chephren
mock-cheops
Commits
b07f5434
Unverified
Commit
b07f5434
authored
1 year ago
by
REIG Julien
Browse files
Options
Downloads
Patches
Plain Diff
add deleted to resources
parent
b512456d
No related branches found
No related tags found
1 merge request
!14
add deleted to resources
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
models/resource.py
+12
-5
12 additions, 5 deletions
models/resource.py
utils/database_utils.py
+11
-7
11 additions, 7 deletions
utils/database_utils.py
with
23 additions
and
12 deletions
models/resource.py
+
12
−
5
View file @
b07f5434
from
consts.numerical_values
import
HEXADECIMAL_WORD_LENGTH
from
utils.time_utils
import
current_milli_time
from
utils.hex_generator
import
generate_hexadecimal
from
utils.time_utils
import
current_milli_time
from
.command
import
Command
...
...
@@ -13,22 +13,26 @@ class Resource:
self
.
last_update
=
current_milli_time
()
self
.
commands
=
[]
if
commands
is
None
else
commands
self
.
commands_count
=
len
(
self
.
commands
)
self
.
deleted
=
False
def
__str__
(
self
):
return
f
"
Resource(id=
{
self
.
id
}
, name=
{
self
.
name
}
, last_update=
{
self
.
last_update
}
, commands=
{
self
.
commands
}
)
"
return
f
"
Resource(id=
{
self
.
id
}
, name=
{
self
.
name
}
, last_update=
{
self
.
last_update
}
, commands=
{
self
.
commands
}
, commands_count=
{
self
.
commands_count
}
, deleted=
{
self
.
deleted
}
)
"
def
__repr__
(
self
):
return
self
.
__str__
()
def
add_command
(
self
,
command
):
def
add_command
(
self
,
command
:
Command
):
if
(
command
.
command
==
"
DELETE
"
):
self
.
deleted
=
True
self
.
commands
.
append
(
command
)
self
.
commands_count
=
len
(
self
.
commands
)
@staticmethod
def
from_tuple
(
data
,
commands
:
list
[
Command
]
=
None
,
commands_count
:
int
=
None
):
def
from_tuple
(
data
,
commands
:
list
[
Command
]
=
None
,
commands_count
:
int
=
None
,
deleted
:
bool
=
False
):
resource
=
Resource
(
data
[
1
])
resource
.
id
=
data
[
0
]
resource
.
last_update
=
data
[
2
]
resource
.
deleted
=
deleted
if
(
commands
is
not
None
):
resource
.
commands
=
commands
resource
.
commands_count
=
len
(
resource
.
commands
)
...
...
@@ -37,10 +41,11 @@ class Resource:
return
resource
@staticmethod
def
from_full_dict
(
data
):
def
from_full_dict
(
data
:
dict
):
resource
=
Resource
(
data
[
"
name
"
])
resource
.
id
=
data
[
"
id
"
]
resource
.
last_update
=
data
[
"
lastUpdate
"
]
resource
.
deleted
=
data
.
get
(
"
deleted
"
,
False
)
resource
.
commands
=
[
Command
.
from_full_dict
(
command
)
for
command
in
data
[
"
commands
"
]]
resource
.
commands_count
=
len
(
resource
.
commands
)
...
...
@@ -54,6 +59,7 @@ class Resource:
"
id
"
:
self
.
id
,
"
name
"
:
self
.
name
,
"
lastUpdate
"
:
self
.
last_update
,
"
deleted
"
:
self
.
deleted
,
"
commands
"
:
[
command
.
to_dict
()
for
command
in
self
.
commands
]
}
...
...
@@ -62,5 +68,6 @@ class Resource:
"
id
"
:
self
.
id
,
"
name
"
:
self
.
name
,
"
lastUpdate
"
:
self
.
last_update
,
"
deleted
"
:
self
.
deleted
,
"
commandsCount
"
:
self
.
commands_count
}
This diff is collapsed.
Click to expand it.
utils/database_utils.py
+
11
−
7
View file @
b07f5434
...
...
@@ -65,9 +65,9 @@ def get_resources(with_commands: bool = False, page: int = 1, page_size=20) -> O
resources
:
list
[
tuple
]
=
cursor
.
fetchall
()
cursor
.
close
()
if
with_commands
:
return
[
Resource
.
from_tuple
(
resource
,
commands
=
get_commands
(
resource
[
0
]))
for
resource
in
resources
]
return
[
Resource
.
from_tuple
(
resource
,
commands
=
get_commands
(
resource
[
0
])
,
deleted
=
resource_id_is_deleted
(
resource
[
0
])
)
for
resource
in
resources
]
return
[
Resource
.
from_tuple
(
resource
,
commands_count
=
get_commands_count
(
resource
[
0
]))
for
resource
in
resources
]
return
[
Resource
.
from_tuple
(
resource
,
commands_count
=
get_commands_count
(
resource
[
0
])
,
deleted
=
resource_id_is_deleted
(
resource
[
0
])
)
for
resource
in
resources
]
except
Exception
as
e
:
print
(
e
,
file
=
sys
.
stderr
)
return
None
...
...
@@ -96,20 +96,21 @@ def get_resource(id: str, with_commands: bool = False) -> Optional[Resource]:
cursor
.
close
()
if
resource
is
None
:
return
None
deleted
=
resource_id_is_deleted
(
id
)
if
with_commands
:
return
Resource
.
from_tuple
(
resource
,
commands
=
get_commands
(
id
))
return
Resource
.
from_tuple
(
resource
,
commands_count
=
get_commands_count
(
id
))
return
Resource
.
from_tuple
(
resource
,
commands
=
get_commands
(
id
)
,
deleted
=
deleted
)
return
Resource
.
from_tuple
(
resource
,
commands_count
=
get_commands_count
(
id
)
,
deleted
=
deleted
)
except
Exception
as
e
:
print
(
e
,
file
=
sys
.
stderr
)
return
None
def
resource_is_deleted
(
resource
:
Resource
)
->
bool
:
def
resource_id_is_deleted
(
resource_id
:
int
)
->
bool
:
with
sqlite3
.
connect
(
db_file
)
as
db_conn
:
try
:
cursor
=
db_conn
.
cursor
()
cursor
.
execute
(
"
SELECT command FROM commands WHERE resource_id = ? ORDER BY create_date DESC LIMIT 1
"
,
(
resource
.
id
,))
"
SELECT command FROM commands WHERE resource_id = ? ORDER BY create_date DESC LIMIT 1
"
,
(
resource
_
id
,))
command
=
cursor
.
fetchone
()
cursor
.
close
()
return
command
[
0
]
==
"
DELETE
"
...
...
@@ -117,6 +118,9 @@ def resource_is_deleted(resource: Resource) -> bool:
print
(
e
,
file
=
sys
.
stderr
)
return
True
def
resource_is_deleted
(
resource
:
Resource
)
->
bool
:
return
resource_id_is_deleted
(
resource
.
id
)
def
insert_command
(
resource_id
:
str
,
command
:
Command
)
->
bool
:
with
sqlite3
.
connect
(
db_file
)
as
db_conn
:
...
...
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