Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
surykatka
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
1
Merge Requests
1
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
nexedi
surykatka
Commits
25f6d7d5
Commit
25f6d7d5
authored
Jan 09, 2020
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP: store http request elapsed time
parent
0e6c5291
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
7 deletions
+47
-7
src/surykatka/bot.py
src/surykatka/bot.py
+1
-0
src/surykatka/db.py
src/surykatka/db.py
+11
-2
src/surykatka/http.py
src/surykatka/http.py
+35
-5
No files found.
src/surykatka/bot.py
View file @
25f6d7d5
...
...
@@ -284,6 +284,7 @@ class WebBot:
result_dict
[
"http_query"
].
append
(
{
"status_code"
:
network_change
[
"status_code"
],
"total_seconds"
:
network_change
[
"total_seconds"
],
"url"
:
network_change
[
"url"
],
"ip"
:
network_change
[
"ip"
],
"date"
:
rfc822
(
network_change
[
"status"
]),
...
...
src/surykatka/db.py
View file @
25f6d7d5
...
...
@@ -20,6 +20,7 @@
import
peewee
from
playhouse.sqlite_ext
import
SqliteExtDatabase
import
datetime
from
playhouse.migrate
import
migrate
,
SqliteMigrator
class
LogDB
:
...
...
@@ -114,6 +115,7 @@ class LogDB:
ip
=
peewee
.
TextField
()
url
=
peewee
.
TextField
()
status_code
=
peewee
.
IntegerField
()
total_seconds
=
peewee
.
FloatField
()
class
Meta
:
primary_key
=
peewee
.
CompositeKey
(
"status"
,
"ip"
,
"url"
)
...
...
@@ -129,7 +131,7 @@ class LogDB:
def
createTables
(
self
):
# http://www.sqlite.org/pragma.html#pragma_user_version
db_version
=
self
.
_db
.
pragma
(
"user_version"
)
expected_version
=
2
expected_version
=
3
if
db_version
!=
expected_version
:
with
self
.
_db
.
transaction
():
...
...
@@ -150,7 +152,14 @@ class LogDB:
# version 1 without SSL support
self
.
_db
.
create_tables
([
self
.
SslChange
])
else
:
if
(
0
<
db_version
)
and
(
db_version
<=
2
):
# version 2 without the http total_seconds column
migrator
=
SqliteMigrator
(
self
.
_db
)
migrate
(
migrator
.
add_column
(
"HttpCodeChange"
,
"total_seconds"
)
)
if
db_version
>=
expected_version
:
raise
ValueError
(
"Can not downgrade SQLite DB"
)
self
.
_db
.
pragma
(
"user_version"
,
expected_version
)
...
...
src/surykatka/http.py
View file @
25f6d7d5
...
...
@@ -78,7 +78,6 @@ def request(url, timeout=TIMEOUT, headers=None, session=requests, version=0):
except
requests
.
exceptions
.
TooManyRedirects
:
response
=
requests
.
models
.
Response
()
response
.
status_code
=
520
return
response
...
...
@@ -106,7 +105,20 @@ def reportHttp(db, ip=None, url=None):
return
query
def
logHttpStatus
(
db
,
ip
,
url
,
code
,
status_id
):
def
calculateSpeedRange
(
total_seconds
):
# Prevent updating the DB by defining acceptable speed range
if
total_seconds
==
0
:
# error cases
return
"BAD"
elif
0.5
<
total_seconds
:
return
"MODERATE"
elif
0.2
<
total_seconds
:
return
"FAST"
else
:
return
"SLOW"
def
logHttpStatus
(
db
,
ip
,
url
,
code
,
total_seconds
,
status_id
):
with
db
.
_db
.
atomic
():
try
:
...
...
@@ -115,9 +127,20 @@ def logHttpStatus(db, ip, url, code, status_id):
except
db
.
HttpCodeChange
.
DoesNotExist
:
previous_entry
=
None
if
(
previous_entry
is
None
)
or
(
previous_entry
.
status_code
!=
code
):
if
(
(
previous_entry
is
None
)
or
(
previous_entry
.
status_code
!=
code
)
or
(
calculateSpeedRange
(
previous_entry
.
total_seconds
)
!=
calculateSpeedRange
(
total_seconds
)
)
):
previous_entry
=
db
.
HttpCodeChange
.
create
(
status
=
status_id
,
ip
=
ip
,
url
=
url
,
status_code
=
code
status
=
status_id
,
ip
=
ip
,
url
=
url
,
status_code
=
code
,
total_seconds
=
total_seconds
,
)
return
previous_entry
.
status_id
...
...
@@ -146,4 +169,11 @@ def checkHttpStatus(db, status_id, url, ip, bot_version, timeout=TIMEOUT):
response
=
request
(
ip_url
,
headers
=
{
"Host"
:
hostname
},
version
=
bot_version
,
**
request_kw
)
logHttpStatus
(
db
,
ip
,
url
,
response
.
status_code
,
status_id
)
logHttpStatus
(
db
,
ip
,
url
,
response
.
status_code
,
response
.
elapsed
.
total_seconds
(),
status_id
,
)
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