|
| 1 | +from django.db import models, transaction |
| 2 | +from tileservermapping.mapping.models import Server |
| 3 | + |
| 4 | + |
| 5 | +class SlowTiles(models.Model): |
| 6 | + z = models.IntegerField(null=False) |
| 7 | + x = models.IntegerField(null=False) |
| 8 | + y = models.IntegerField(null=False) |
| 9 | + |
| 10 | + server = models.ForeignKey(Server, on_delete=models.CASCADE) |
| 11 | + |
| 12 | + count = models.IntegerField(default=1) |
| 13 | + created = models.DateTimeField(auto_now_add=True) |
| 14 | + updated = models.DateTimeField(auto_now=True) |
| 15 | + |
| 16 | + class Meta: |
| 17 | + unique_together = ['z', 'x', 'y'] |
| 18 | + |
| 19 | + def __str__(self): |
| 20 | + return "<SlowTile: {}/{}/{} c: {}>".format(self.z, self.x, self.y, self.count) |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def insert(z, x, y): |
| 24 | + """ |
| 25 | + inserts a tile z/x/y into the slow tile list, or increase the counter on an existing tile |
| 26 | + :return: the current counter on this tile, or -1 if no tile server is responsible |
| 27 | + """ |
| 28 | + server = Server.get_server(z, x, y) |
| 29 | + if server == Server.objects.get(z=0, x=0, y=0): |
| 30 | + # we can not do anything on the external instance |
| 31 | + return -1 |
| 32 | + with transaction.atomic(): |
| 33 | + try: |
| 34 | + tile = SlowTiles.objects.get(z=z, x=x, y=y) |
| 35 | + tile.count += 1 |
| 36 | + except models.ObjectDoesNotExist: |
| 37 | + tile = SlowTiles(z=z, x=x, y=y, server=server) |
| 38 | + tile.save() |
| 39 | + return tile.count |
0 commit comments