-
Notifications
You must be signed in to change notification settings - Fork 0
slowtiles module #1
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
Draft
Akasch
wants to merge
3
commits into
master
Choose a base branch
from
feature_slowtiles
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| # custom files | ||
| db.sqlite | ||
| settings.py | ||
|
|
||
| .idea | ||
|
|
||
| # Byte-compiled / optimized / DLL files | ||
| __pycache__/ | ||
|
|
||
This file contains hidden or 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,3 @@ | ||
| django | ||
| mercantile | ||
| geojson |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,6 @@ | ||
| from django.contrib import admin | ||
|
|
||
| # Register your models here. | ||
| from .models import SlowTiles | ||
|
|
||
| admin.site.register(SlowTiles) |
This file contains hidden or 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,32 @@ | ||
| # Generated by Django 2.2.1 on 2020-02-09 20:16 | ||
|
|
||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ('mapping', '0006_auto_20181226_1555'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='SlowTiles', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('z', models.IntegerField()), | ||
| ('x', models.IntegerField()), | ||
| ('y', models.IntegerField()), | ||
| ('count', models.IntegerField(default=1)), | ||
| ('created', models.DateTimeField(auto_now_add=True)), | ||
| ('updated', models.DateTimeField(auto_now=True)), | ||
| ('server', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mapping.Server')), | ||
| ], | ||
| options={ | ||
| 'unique_together': {('z', 'x', 'y')}, | ||
| }, | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or 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,39 @@ | ||
| from django.db import models, transaction | ||
| from tileservermapping.mapping.models import Server | ||
|
|
||
|
|
||
| class SlowTiles(models.Model): | ||
| z = models.IntegerField(null=False) | ||
| x = models.IntegerField(null=False) | ||
| y = models.IntegerField(null=False) | ||
|
|
||
| server = models.ForeignKey(Server, on_delete=models.CASCADE) | ||
|
|
||
| count = models.IntegerField(default=1) | ||
| created = models.DateTimeField(auto_now_add=True) | ||
| updated = models.DateTimeField(auto_now=True) | ||
|
|
||
| class Meta: | ||
| unique_together = ['z', 'x', 'y'] | ||
|
|
||
| def __str__(self): | ||
| return "<SlowTile: {}/{}/{} c: {}>".format(self.z, self.x, self.y, self.count) | ||
|
|
||
| @staticmethod | ||
| def insert(z, x, y): | ||
| """ | ||
| inserts a tile z/x/y into the slow tile list, or increase the counter on an existing tile | ||
| :return: the current counter on this tile, or -1 if no tile server is responsible | ||
| """ | ||
| server = Server.get_server(z, x, y) | ||
| if server == Server.objects.get(z=0, x=0, y=0): | ||
| # we can not do anything on the external instance | ||
| return -1 | ||
| with transaction.atomic(): | ||
| try: | ||
| tile = SlowTiles.objects.get(z=z, x=x, y=y) | ||
| tile.count += 1 | ||
| except models.ObjectDoesNotExist: | ||
| tile = SlowTiles(z=z, x=x, y=y, server=server) | ||
| tile.save() | ||
| return tile.count |
This file contains hidden or 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,8 @@ | ||
| from django.urls import path | ||
| from .views import insert_tile, get_server_list, upload_tile | ||
|
|
||
| urlpatterns = [ | ||
| path('add/<int:z>/<int:x>/<int:y>', insert_tile, name="insert_tile"), | ||
| path('server_list/<int:z>/<int:x>/<int:y>', get_server_list, name="server_list"), | ||
| path('upload_tile/<int:z>/<int:x>/<int:y>.<slug:format>', upload_tile, name='upload_tile'), | ||
| ] |
This file contains hidden or 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,46 @@ | ||
| from pathlib import Path | ||
|
|
||
| from django.conf import settings | ||
| from django.http import HttpResponse | ||
| from django.views.decorators.csrf import csrf_exempt | ||
| from django.views.decorators.http import require_POST | ||
|
|
||
| from tileservermapping.mapping.models import Server | ||
| from tileservermapping.slowtiles.models import SlowTiles | ||
|
|
||
|
|
||
| def insert_tile(request, z, x, y): | ||
| tile_count = SlowTiles.insert(z, x, y) | ||
| if tile_count < 0: | ||
| return HttpResponse("No server responsible", content_type='text', status=404) | ||
| return HttpResponse("Added: {}/{}/{} Count: {}\n".format(z, x, y, tile_count), content_type='text') | ||
|
|
||
|
|
||
| def get_server_list(request, z, x, y): | ||
| server = Server.get_server(z, x, y) | ||
| tiles = SlowTiles.objects.filter(server=server, count__gte=5) | ||
| answer = "" | ||
| for tile in tiles: | ||
| answer += "{}/{}/{}\n".format(tile.z, tile.x, tile.y) | ||
| return HttpResponse(answer, content_type='text') | ||
|
|
||
|
|
||
| def handle_uploaded_file(path, f): | ||
| with open(path, 'wb') as destination: | ||
| for chunk in f.chunks(): | ||
| destination.write(chunk) | ||
|
|
||
| @require_POST | ||
| @csrf_exempt | ||
| def upload_tile(request, z, x, y, format): | ||
| # TODO auth oth server | ||
| # TODO check if tile in server | ||
| if not SlowTiles.objects.filter(x=x, y=y, z=z, count__gte=5).count(): | ||
| return HttpResponse('Tile should not be static\n', content_type="text", status=403) | ||
| path = Path(settings.TILE_WEBSERVER_DIR) / str(z) / str(x) | ||
| if not path.exists(): | ||
| path.mkdir(parents=True) | ||
| file = path / "{}.{}".format(y, format) | ||
| existed = file.exists() | ||
| handle_uploaded_file(file, request.FILES['tile']) | ||
| return HttpResponse('Updated\n' if existed else 'OK\n', content_type='text') | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
auth oth server?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be authenticate the server, because at the moment all servers having access to the api can upload files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be fixed by using #8 and adding the checks here to