-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from wobcom/feature/performance2
Splitted, multi-threaded and rewamped some queries to speed up perfor…
- Loading branch information
Showing
25 changed files
with
597 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,7 @@ cosmo.yml | |
.direnv | ||
leaf*.json | ||
|
||
cosmo_data.yaml | ||
machines/test0001/* | ||
machines/** | ||
|
||
.coverage | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import time | ||
from urllib.parse import urljoin | ||
|
||
import requests | ||
|
||
from cosmo import log | ||
from cosmo.clients.netbox_v4 import NetboxV4Strategy | ||
|
||
|
||
class NetboxClient: | ||
def __init__(self, url, token): | ||
self.url = url | ||
self.token = token | ||
|
||
self.version = self.query_version() | ||
|
||
if self.version.startswith("4."): | ||
log.info("Using version 4.x strategy...") | ||
self.child_client = NetboxV4Strategy(url, token) | ||
else: | ||
raise Exception("Unknown Version") | ||
|
||
def query_version(self): | ||
r = requests.get( | ||
urljoin(self.url, "/api/status/"), | ||
headers={ | ||
"Authorization": f"Token {self.token}", | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
if r.status_code != 200: | ||
raise Exception("Error querying api: " + r.text) | ||
|
||
json = r.json() | ||
return json['netbox-version'] | ||
|
||
def get_data(self, device_config): | ||
start_time = time.perf_counter() | ||
data = self.child_client.get_data(device_config) | ||
end_time = time.perf_counter() | ||
diff_time = end_time - start_time | ||
log.info(f"Data fetching took {round(diff_time, 2)} s...") | ||
|
||
return data | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from urllib.parse import urlencode, urljoin | ||
|
||
import requests | ||
|
||
|
||
class NetboxAPIClient: | ||
def __init__(self, url, token): | ||
self.url = url | ||
self.token = token | ||
|
||
def query(self, query): | ||
r = requests.post( | ||
urljoin(self.url, "/graphql/"), | ||
json={"query": query}, | ||
headers={ | ||
"Authorization": f"Token {self.token}", | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
if r.status_code != 200: | ||
raise Exception("Error querying api: " + r.text) | ||
|
||
json = r.json() | ||
|
||
if 'errors' in json: | ||
for e in json['errors']: | ||
print(e) | ||
|
||
return json | ||
|
||
def query_rest(self, path, queries): | ||
q = urlencode(queries, doseq=True) | ||
url = urljoin(self.url, path) + f"?{q}" | ||
|
||
return_array = list() | ||
|
||
while url is not None: | ||
r = requests.get( | ||
url, | ||
headers={ | ||
"Authorization": f"Token {self.token}", | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
|
||
if r.status_code != 200: | ||
raise Exception("Error querying api: " + r.text) | ||
|
||
data = r.json() | ||
|
||
url = data['next'] | ||
return_array.extend(data['results']) | ||
|
||
return return_array |
Oops, something went wrong.