mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
chore: batch mode for sample data script
This commit is contained in:
parent
1a10a4fb21
commit
6576837396
@ -9,7 +9,6 @@ from datetime import datetime, timedelta
|
|||||||
from typing import List, Union, Callable
|
from typing import List, Union, Callable
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from tqdm import tqdm
|
|
||||||
|
|
||||||
MACHINE = "devmachine"
|
MACHINE = "devmachine"
|
||||||
UA = 'wakatime/13.0.7 (Linux-4.15.0-91-generic-x86_64-with-glibc2.4) Python3.8.0.final.0 generator/1.42.1 generator-wakatime/4.0.0'
|
UA = 'wakatime/13.0.7 (Linux-4.15.0-91-generic-x86_64-with-glibc2.4) Python3.8.0.final.0 generator/1.42.1 generator-wakatime/4.0.0'
|
||||||
@ -53,6 +52,7 @@ class ConfigParams:
|
|||||||
self.n_projects = 0
|
self.n_projects = 0
|
||||||
self.offset = 0
|
self.offset = 0
|
||||||
self.seed = 0
|
self.seed = 0
|
||||||
|
self.batch = False
|
||||||
|
|
||||||
|
|
||||||
def generate_data(n: int, n_projects: int = 5, n_past_hours: int = 24) -> List[Heartbeat]:
|
def generate_data(n: int, n_projects: int = 5, n_past_hours: int = 24) -> List[Heartbeat]:
|
||||||
@ -86,21 +86,21 @@ def generate_data(n: int, n_projects: int = 5, n_past_hours: int = 24) -> List[H
|
|||||||
def post_data_sync(data: List[Heartbeat], url: str, api_key: str):
|
def post_data_sync(data: List[Heartbeat], url: str, api_key: str):
|
||||||
encoded_key: str = str(base64.b64encode(api_key.encode('utf-8')), 'utf-8')
|
encoded_key: str = str(base64.b64encode(api_key.encode('utf-8')), 'utf-8')
|
||||||
|
|
||||||
for h in data:
|
r = requests.post(url, json=[h.__dict__ for h in data], headers={
|
||||||
r = requests.post(url, json=[h.__dict__], headers={
|
'User-Agent': UA,
|
||||||
'User-Agent': UA,
|
'Authorization': f'Basic {encoded_key}',
|
||||||
'Authorization': f'Basic {encoded_key}',
|
'X-Machine-Name': MACHINE,
|
||||||
'X-Machine-Name': MACHINE,
|
})
|
||||||
})
|
if r.status_code != 201:
|
||||||
if r.status_code != 201:
|
print(r.text)
|
||||||
print(r.text)
|
sys.exit(1)
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def make_gui(callback: Callable[[ConfigParams, Callable[[int], None]], None]) -> ('QApplication', 'QWidget'):
|
def make_gui(callback: Callable[[ConfigParams, Callable[[int], None]], None]) -> ('QApplication', 'QWidget'):
|
||||||
|
# https://doc.qt.io/qt-5/qtwidgets-module.html
|
||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt
|
||||||
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QHBoxLayout, QVBoxLayout, QGroupBox, QLabel, \
|
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QHBoxLayout, QVBoxLayout, QGroupBox, QLabel, \
|
||||||
QLineEdit, QSpinBox, QProgressBar, QPushButton
|
QLineEdit, QSpinBox, QProgressBar, QPushButton, QCheckBox
|
||||||
|
|
||||||
# Main app
|
# Main app
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
@ -153,10 +153,14 @@ def make_gui(callback: Callable[[ConfigParams, Callable[[int], None]], None]) ->
|
|||||||
seed_input.setMaximum(2147483647)
|
seed_input.setMaximum(2147483647)
|
||||||
seed_input.setValue(1337)
|
seed_input.setValue(1337)
|
||||||
|
|
||||||
|
batch_checkbox = QCheckBox('Batch Mode')
|
||||||
|
batch_checkbox.setTristate(False)
|
||||||
|
|
||||||
form_layout_2.addRow(heartbeats_input_label, heartbeats_input)
|
form_layout_2.addRow(heartbeats_input_label, heartbeats_input)
|
||||||
form_layout_2.addRow(projects_input_label, projects_input)
|
form_layout_2.addRow(projects_input_label, projects_input)
|
||||||
form_layout_2.addRow(offset_input_label, offset_input)
|
form_layout_2.addRow(offset_input_label, offset_input)
|
||||||
form_layout_2.addRow(seed_input_label, seed_input)
|
form_layout_2.addRow(seed_input_label, seed_input)
|
||||||
|
form_layout_2.addRow(batch_checkbox)
|
||||||
|
|
||||||
# Bottom controls
|
# Bottom controls
|
||||||
bottom_layout = QHBoxLayout()
|
bottom_layout = QHBoxLayout()
|
||||||
@ -195,6 +199,7 @@ def make_gui(callback: Callable[[ConfigParams, Callable[[int], None]], None]) ->
|
|||||||
params.n_projects = projects_input.value()
|
params.n_projects = projects_input.value()
|
||||||
params.offset = offset_input.value()
|
params.offset = offset_input.value()
|
||||||
params.seed = seed_input.value()
|
params.seed = seed_input.value()
|
||||||
|
params.batch = batch_checkbox.isChecked()
|
||||||
return params
|
return params
|
||||||
|
|
||||||
def update_progress(inc=1):
|
def update_progress(inc=1):
|
||||||
@ -231,6 +236,7 @@ def parse_arguments():
|
|||||||
help='negative time offset in hours from now for to be used as an interval within which to generate heartbeats for')
|
help='negative time offset in hours from now for to be used as an interval within which to generate heartbeats for')
|
||||||
parser.add_argument('-s', '--seed', type=int, default=2020,
|
parser.add_argument('-s', '--seed', type=int, default=2020,
|
||||||
help='a seed for initializing the pseudo-random number generator')
|
help='a seed for initializing the pseudo-random number generator')
|
||||||
|
parser.add_argument('-b', '--batch', default=False, help='batch mode (push all heartbeats at once)', action='store_true')
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@ -242,6 +248,7 @@ def args_to_params(parsed_args: argparse.Namespace) -> (ConfigParams, bool):
|
|||||||
params.seed = parsed_args.seed
|
params.seed = parsed_args.seed
|
||||||
params.api_url = parsed_args.url
|
params.api_url = parsed_args.url
|
||||||
params.api_key = parsed_args.apikey
|
params.api_key = parsed_args.apikey
|
||||||
|
params.batch = parsed_args.batch
|
||||||
return params, not parsed_args.headless
|
return params, not parsed_args.headless
|
||||||
|
|
||||||
|
|
||||||
@ -258,9 +265,14 @@ def run(params: ConfigParams, update_progress: Callable[[int], None]):
|
|||||||
params.offset * -1 if params.offset < 0 else params.offset
|
params.offset * -1 if params.offset < 0 else params.offset
|
||||||
)
|
)
|
||||||
|
|
||||||
for d in data:
|
# batch-mode won't work when using sqlite backend
|
||||||
post_data_sync([d], f'{params.api_url}/heartbeats', params.api_key)
|
if params.batch:
|
||||||
update_progress(1)
|
post_data_sync(data, f'{params.api_url}/heartbeats', params.api_key)
|
||||||
|
update_progress(len(data))
|
||||||
|
else:
|
||||||
|
for d in data:
|
||||||
|
post_data_sync([d], f'{params.api_url}/heartbeats', params.api_key)
|
||||||
|
update_progress(len(data))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -270,5 +282,7 @@ if __name__ == '__main__':
|
|||||||
window.show()
|
window.show()
|
||||||
app.exec()
|
app.exec()
|
||||||
else:
|
else:
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
pbar = tqdm(total=params.n)
|
pbar = tqdm(total=params.n)
|
||||||
run(params, pbar.update)
|
run(params, pbar.update)
|
||||||
|
@ -70,12 +70,8 @@
|
|||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Fancy statistics and plots</li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Fancy statistics and plots</li>
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Cool badges for readmes</li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Cool badges for readmes</li>
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Intuitive REST API</li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Intuitive REST API</li>
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Compatible with <a href="https://wakatime.com" target="_blank"
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Compatible with <a href="https://wakatime.com" target="_blank" rel="noopener noreferrer" class="underline">Wakatime</a></li>
|
||||||
rel="noopener noreferrer" class="underline">Wakatime</a></li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> <a href="https://prometheus.io" target="_blank" rel="noopener noreferrer" class="underline">Prometheus</a> metrics</li>
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> <a href="https://prometheus.io" target="_blank" rel="noopener noreferrer"
|
|
||||||
class="underline">Prometheus</a> metrics via <a
|
|
||||||
href="https://github.com/MacroPower/wakatime_exporter" target="_blank"
|
|
||||||
rel="noopener noreferrer" class="underline">exporter</a></li>
|
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Lightning fast</li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Lightning fast</li>
|
||||||
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Self-hosted</li>
|
<li><span class="iconify inline text-green-700" data-icon="ant-design:check-square-filled"></span> Self-hosted</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
Loading…
Reference in New Issue
Block a user