Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[scripts]
serve = "python -m whois"
"init-db" = "python helpers/db_create.py"

[dev-packages]
pytest = "*"
freezegun = "*"
Expand All @@ -15,6 +19,7 @@ Flask = "*"
Flask-Login = "*"
python-dateutil = "*"
peewee = "*"
apscheduler = "*"

[requires]
python_version = "3.6"
35 changes: 28 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@

- Dependencies

```bash
```shell
pipenv install
```

- Create database

```bash
```shell
pipenv run python helpers/db_create.py
# or
pipenv run init-db
```

## Running

```bash
```shell
pipenv run python -m whois
# or
pipenv run serve
```

## Deployment

```bash
```shell
docker-compose build
# first run, later it should just connect to existing db
docker-compose run web python3 helpers/db_create.py
Expand Down
12 changes: 12 additions & 0 deletions whois/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,15 @@ def update_or_create(cls, mac_address, last_seen, hostname=None):
res.last_seen = last_seen
res.hostname = hostname
res.save()


class History(pw.Model):
"""History of user activity in HS"""

datetime = pw.DateTimeField()
user_count = pw.IntegerField()
unknown_device_count = pw.IntegerField()
known_device_count = pw.IntegerField()

class Meta:
database = db
33 changes: 33 additions & 0 deletions whois/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logging
import atexit
from datetime import datetime

from apscheduler.schedulers.background import BackgroundScheduler as Scheduler
from whois.settings import log_frequency_time
from whois.database import History, Device
from whois.helpers import owners_from_devices, filter_hidden, unclaimed_devices

logger = logging.getLogger(__name__)

logger.info("Created BackgroundScheduler")
cron = Scheduler(daemon=True)
cron.start()


@cron.scheduled_job(**log_frequency_time)
def log_presence():
logger = logging.getLogger(__name__)
logger.info("logging presence to history")

now = datetime.now()
recent = Device.get_recent(**log_frequency_time)
visible_devices = filter_hidden(recent)
uc = len(filter_hidden(owners_from_devices(visible_devices)))
udc = len(unclaimed_devices(visible_devices))
ndc = len(visible_devices) - udc
History.create(
datetime=now, user_count=uc, unknown_device_count=udc, known_device_count=ndc
)


atexit.register(lambda: cron.shutdown(wait=False))
1 change: 1 addition & 0 deletions whois/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
device_flags = {1: "hidden", 2: "new", 4: "infrastructure", 8: "esp", 16: "laptop"}

recent_time = {"minutes": 20}
log_frequency_time = {"hours": 1, "minutes": 0, "seconds": 0}

# production
ip_mask = "192.168.88.1-255"
Expand Down
Binary file added whois/static/favicon.ico
Binary file not shown.
3 changes: 2 additions & 1 deletion whois/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<!-- Bootstrap -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link href="{{ url_for('static', filename='bootstrap.min.css') }}"
rel="stylesheet">

Expand All @@ -31,12 +32,12 @@
{% block header %}
<nav class="nav">
<a class="navbar-brand" href="/">{{ self.title() }}</a>
<a class="nav-link" href="/">status</a>
{% if current_user.is_anonymous %}
<a class="nav-link" href="/login">login</a>
<a class="nav-link" href="/register">register</a>
{% endif %}
{% if current_user.is_authenticated %}
<a class="nav-link" href="/">devices</a>
<a class="nav-link" href="/profile">profile</a>
<a class="nav-link" href="/logout">logout
({{ current_user.username }})</a>
Expand Down
3 changes: 2 additions & 1 deletion whois/templates/landing.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
{% endif %}
<div class="card text-black">
<div class="card-body">
Komenda na Slacku <kbd>/ktohakuje</kbd>
Komenda na Slacku <kbd>/ktohakuje</kbd><br>
Więcej instrukcji na <a href="https://wiki.hs3.pl/projekty/whois">stronie wiki</a>
</div>
</div>
</div>
Expand Down