Skip to content

Commit

Permalink
Add distributed counter example
Browse files Browse the repository at this point in the history
  • Loading branch information
billyrrr committed Nov 23, 2019
1 parent 442a4e8 commit 4b1ca69
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
78 changes: 78 additions & 0 deletions examples/distributed_counter_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from flask_boiler.context import Context as CTX
from flask_boiler.schema import Schema
from flask_boiler.fields import Integer
from flask_boiler.domain_model import DomainModel
from flask_boiler.view_mediator_dav import ViewMediatorDAV
from flask_boiler.view_model import ViewModel


class ShardSchema(Schema):

count = Integer()


class Shard(DomainModel):

class Meta:
schema_cls = ShardSchema
collection_id = "CounterAShards"


class CounterViewSchema(Schema):

total_count = Integer(dump_only=True)


class CounterView(ViewModel):

class Meta:
schema_cls = CounterViewSchema

def __init__(self, *args, **kwargs):
doc_ref = CTX.db.collection("counters").document("counter_0")
super().__init__(*args, doc_ref=doc_ref, **kwargs)
self.shards = dict()

@property
def total_count(self):
return sum(v.count for _, v in self.shards.items())

def set_shard(self, sid, shard):
self.shards[sid] = shard

def get_vm_update_callback(self, dm_cls, *args, **kwargs) :

if dm_cls == Shard:
def callback(vm: CounterView, dm: Shard):
vm.set_shard(dm.doc_id, dm)
return callback
else:
return super().get_vm_update_callback(dm_cls, *args, **kwargs)


class CounterMediator(ViewMediatorDAV):

def __init__(self, shard_size, *args, **kwargs):
super().__init__(*args, view_model_cls=CounterView, **kwargs)
self.shard_size = shard_size
self.view_model = None

@classmethod
def notify(cls, obj):
obj.save()

def start(self):

struct = dict()

for i in range(self.shard_size):
doc_id = str(i)
shard = Shard.new(doc_id=doc_id, )
shard.save()
struct[shard.doc_id] = (Shard, doc_id)

self.view_model = self.view_model_cls.get(
f_notify=self.notify,
struct_d=struct,
once=False)

46 changes: 46 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
from google.cloud.firestore import Increment

from examples.meeting_room.tests.test_view_model import *
from examples.meeting_room.tests.test_dav import *

from examples import distributed_counter_example as dc
from .fixtures import CTX


def test_start(CTX):
"""
TODO: add teardown steps to delete documents generated in firestore
:param CTX:
:return:
"""

mediator = dc.CounterMediator(
mutation_cls=None,
shard_size=2
)

mediator.start()

time.sleep(3)

doc_ref = CTX.db.collection("counters").document("counter_0")

assert doc_ref.get().to_dict() == {
"doc_ref": "counters/counter_0",
"obj_type": "CounterView",
"totalCount": 0
}

CTX.db.collection("Shard").document("0").set(
document_data={
"count": Increment(1),
},
merge=True
)

time.sleep(1)

assert doc_ref.get().to_dict() == {
"doc_ref": "counters/counter_0",
"obj_type": "CounterView",
"totalCount": 1
}

0 comments on commit 4b1ca69

Please sign in to comment.