Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
fix sockjs redis issues: use pks for channel names
Browse files Browse the repository at this point in the history
boundary effects has been tested, need to check forestry as well
  • Loading branch information
alee committed Feb 17, 2015
1 parent 598e3a6 commit 781dda0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 64 deletions.
1 change: 1 addition & 0 deletions vcweb/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ def notify_participants(self, message, group=None, notify_experimenter=False):
channel = RedisPubSub.get_participant_broadcast_channel(self.pk)
else:
channel = RedisPubSub.get_participant_group_channel(group.pk)
logger.debug("notifying participants in channel %s", channel)
redis_client.publish(channel, message)
if notify_experimenter:
redis_client.publish(RedisPubSub.get_experimenter_channel(self.pk), message)
Expand Down
6 changes: 4 additions & 2 deletions vcweb/core/templates/includes/participant.events.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
auth_token: "{{ request.user.participant.authentication_token }}",
email: "{{ request.user.email }}",
experiment_id: {{experiment.pk|default:-1}},
{% if participant_group_relationship %}
participant_group: "{{participant_group_relationship.group|default:-1}}",
{% if group %}
group_id: "{{ group.pk }}",
{% elif participant_group_relationship %}
group_id: "{{ participant_group_relationship.group.pk }}",
{% endif %}
message: payload
});
Expand Down
87 changes: 31 additions & 56 deletions vcweb/experiment/bound/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from django.contrib import messages
from django.db import transaction
from django.http import Http404
from django.shortcuts import render, get_object_or_404, redirect

from vcweb.core.decorators import group_required
Expand All @@ -25,23 +24,21 @@
@group_required(PermissionGroup.participant, PermissionGroup.demo_participant)
def participate(request, experiment_id=None):
participant = request.user.participant
logger.debug(
"handling participate request for %s and experiment %s", participant, experiment_id)
experiment = get_object_or_404(Experiment.objects.select_related('experiment_metadata', 'experiment_configuration'),
pk=experiment_id)
logger.debug("handling participate request for %s and experiment %s", participant, experiment_id)
experiment = get_object_or_404(Experiment.objects.select_related('experiment_metadata',
'experiment_configuration'),
pk=experiment_id,
experiment_metadata=get_experiment_metadata())
if experiment.is_active:
pgr = experiment.get_participant_group_relationship(participant)
if experiment.experiment_metadata != get_experiment_metadata():
raise Http404
return render(request, experiment.participant_template, {
'experiment': experiment,
'participant_experiment_relationship': experiment.get_participant_experiment_relationship(participant),
'participant_group_relationship': pgr,
'group': pgr.group,
'experimentModelJson': dumps(get_view_model_dict(experiment, pgr)),
})
else:
messages.info(
request, '%s has not been activated yet. Please wait until the experimenter activates the experiment and try again.' % experiment)
messages.info(request, '%s has not been activated yet. Please try again later.' % experiment)
return redirect('core:dashboard')


Expand All @@ -51,16 +48,14 @@ def submit_harvest_decision(request, experiment_id=None):
experiment = get_object_or_404(Experiment, pk=experiment_id)
if form.is_valid():
participant_group_id = form.cleaned_data['participant_group_id']
pgr = get_object_or_404(
ParticipantGroupRelationship, pk=participant_group_id)
pgr = get_object_or_404(ParticipantGroupRelationship, pk=participant_group_id)
harvest_decision = form.cleaned_data['integer_decision']
submitted = form.cleaned_data['submitted']
logger.debug("pgr %s harvested %s - final submission? %s",
pgr, harvest_decision, submitted)
with transaction.atomic():
round_data = experiment.current_round_data
set_harvest_decision(
pgr, harvest_decision, round_data, submitted=submitted)
set_harvest_decision(pgr, harvest_decision, round_data, submitted=submitted)
message = "%s harvested %s trees"
experiment.log(message % (pgr.participant, harvest_decision))
response_dict = {
Expand All @@ -80,8 +75,7 @@ def submit_harvest_decision(request, experiment_id=None):
def get_view_model(request, experiment_id=None):
experiment = get_object_or_404(Experiment.objects.select_related('experiment_metadata', 'experiment_configuration'),
pk=experiment_id)
pgr = experiment.get_participant_group_relationship(
request.user.participant)
pgr = experiment.get_participant_group_relationship(request.user.participant)
return JsonResponse(get_view_model_dict(experiment, pgr))


Expand Down Expand Up @@ -140,8 +134,7 @@ def get_view_model_dict(experiment, participant_group_relationship, **kwargs):
current_round = experiment.current_round
current_round_data = experiment.current_round_data
previous_round = experiment.previous_round
previous_round_data = experiment.get_round_data(
round_configuration=previous_round, previous_round=True)
previous_round_data = experiment.get_round_data(round_configuration=previous_round, previous_round=True)
experiment_model_dict = experiment.to_dict(
include_round_data=False, default_value_dict=experiment_model_defaults)

Expand All @@ -156,28 +149,24 @@ def get_view_model_dict(experiment, participant_group_relationship, **kwargs):
experiment_model_dict['isPracticeRound'] = current_round.is_practice_round
# FIXME: only show the tour on the first practice round, this is a bit brittle. better setup might be to have a
# dedicated boolean flag on RoundConfiguration?
experiment_model_dict[
'showTour'] = current_round.is_practice_round and not previous_round.is_practice_round
experiment_model_dict['showTour'] = current_round.is_practice_round and not previous_round.is_practice_round
# instructions round parameters
if current_round.is_instructions_round:
experiment_model_dict['isInstructionsRound'] = True
experiment_model_dict['participantsPerGroup'] = ec.max_group_size
experiment_model_dict['regrowthRate'] = regrowth_rate
experiment_model_dict[
'initialResourceLevel'] = get_initial_resource_level(current_round)
experiment_model_dict['initialResourceLevel'] = get_initial_resource_level(current_round)
if current_round.is_playable_round:
experiment_model_dict['chatEnabled'] = current_round.chat_enabled

if current_round.is_debriefing_round:
experiment_model_dict['totalHarvest'] = get_total_harvest(participant_group_relationship,
current_round.session_id)
if experiment.is_last_round:
(session_one_storage, session_two_storage) = get_final_session_storage_queryset(experiment,
participant_group_relationship.participant)
experiment_model_dict[
'sessionOneStorage'] = session_one_storage.int_value
experiment_model_dict[
'sessionTwoStorage'] = session_two_storage.int_value
(session_one_storage, session_two_storage) = get_final_session_storage_queryset(
experiment, participant_group_relationship.participant)
experiment_model_dict['sessionOneStorage'] = session_one_storage.int_value
experiment_model_dict['sessionTwoStorage'] = session_two_storage.int_value

if current_round.is_survey_enabled:
query_parameters = urlencode({
Expand All @@ -189,19 +178,14 @@ def get_view_model_dict(experiment, participant_group_relationship, **kwargs):
separator = '?'
if separator in survey_url:
separator = '&'
experiment_model_dict['surveyUrl'] = "{0}{1}{2}".format(
current_round.survey_url, separator, query_parameters)
experiment_model_dict['surveyUrl'] = "{0}{1}{2}".format(current_round.survey_url, separator, query_parameters)
experiment_model_dict['isSurveyEnabled'] = True
experiment_model_dict[
'surveyCompleted'] = participant_group_relationship.survey_completed
logger.debug("survey was enabled, setting survey url to %s",
experiment_model_dict['surveyUrl'])
experiment_model_dict['surveyCompleted'] = participant_group_relationship.survey_completed
logger.debug("survey was enabled, setting survey url to %s", experiment_model_dict['surveyUrl'])

# participant data
experiment_model_dict[
'participantNumber'] = participant_group_relationship.participant_number
experiment_model_dict[
'participantGroupId'] = participant_group_relationship.pk
experiment_model_dict['participantNumber'] = participant_group_relationship.participant_number
experiment_model_dict['participantGroupId'] = participant_group_relationship.pk
# FIXME: these should only need to be added for playable rounds but KO gets unhappy when we switch templates from
# instructions rounds to practice rounds.
own_group = participant_group_relationship.group
Expand All @@ -211,16 +195,11 @@ def get_view_model_dict(experiment, participant_group_relationship, **kwargs):
participant_group_relationship)
experiment_model_dict.update(own_data)
experiment_model_dict['playerData'] = player_data
experiment_model_dict['averageHarvest'] = get_average_harvest(
own_group, previous_round_data)
experiment_model_dict['averageStorage'] = get_average_storage(
own_group, current_round_data)
regrowth = experiment_model_dict['regrowth'] = get_regrowth_dv(
own_group, current_round_data).value
c = Counter(
map(itemgetter('alive'), experiment_model_dict['playerData']))
experiment_model_dict['numberAlive'] = "%s out of %s" % (
c[True], sum(c.values()))
experiment_model_dict['averageHarvest'] = get_average_harvest(own_group, previous_round_data)
experiment_model_dict['averageStorage'] = get_average_storage(own_group, current_round_data)
regrowth = experiment_model_dict['regrowth'] = get_regrowth_dv(own_group, current_round_data).value
c = Counter(map(itemgetter('alive'), experiment_model_dict['playerData']))
experiment_model_dict['numberAlive'] = "%s out of %s" % (c[True], sum(c.values()))
# FIXME: refactor duplication between myGroup and otherGroup data
# loading
experiment_model_dict['myGroup'] = {
Expand All @@ -238,24 +217,20 @@ def get_view_model_dict(experiment, participant_group_relationship, **kwargs):
# participant group data parameters are only needed if this round is a
# data round or the previous round was a data round
if previous_round.is_playable_round or current_round.is_playable_round:
harvest_decision = get_harvest_decision_dv(
participant_group_relationship, current_round_data)
harvest_decision = get_harvest_decision_dv(participant_group_relationship, current_round_data)
experiment_model_dict['submitted'] = harvest_decision.submitted
if harvest_decision.submitted:
# user has already submit a harvest decision this round
experiment_model_dict[
'harvestDecision'] = harvest_decision.int_value
experiment_model_dict['harvestDecision'] = harvest_decision.int_value
logger.debug("already submitted, setting harvest decision to %s",
experiment_model_dict['harvestDecision'])

experiment_model_dict['chatMessages'] = [
cm.to_dict() for cm in ChatMessage.objects.for_group(own_group)]
experiment_model_dict['chatMessages'] = [cm.to_dict() for cm in ChatMessage.objects.for_group(own_group)]
if can_observe_other_group(current_round):
experiment_model_dict['canObserveOtherGroup'] = True
other_group = own_group.get_related_group()
number_alive = get_number_alive(other_group, current_round_data)
resource_level = get_resource_level(
other_group, current_round_data)
resource_level = get_resource_level(other_group, current_round_data)
regrowth = get_regrowth_dv(other_group, current_round_data).value
experiment_model_dict['otherGroup'] = {
'regrowth': regrowth,
Expand Down
12 changes: 6 additions & 6 deletions vcweb/sockjs-redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ class ParticipantConnection(RedisSockJSConnection):
"""

def initialize(self, message_dict):
self.group = message_dict['participant_group']
self.experiment = message_dict['experiment_id']
self.group_id = message_dict['group_id']
self.experiment_id = message_dict['experiment_id']

@property
def redis_channels(self):
return (RedisPubSub.get_participant_broadcast_channel(self.experiment),
RedisPubSub.get_participant_group_channel(self.group))
return (RedisPubSub.get_participant_broadcast_channel(self.experiment_id),
RedisPubSub.get_participant_group_channel(self.group_id))


class ExperimenterConnection(RedisSockJSConnection):
Expand All @@ -146,11 +146,11 @@ class ExperimenterConnection(RedisSockJSConnection):
"""

def initialize(self, message_dict):
self.experiment = message_dict['experiment_id']
self.experiment_id = message_dict['experiment_id']

@property
def redis_channels(self):
return [RedisPubSub.get_experimenter_channel(self.experiment)]
return [RedisPubSub.get_experimenter_channel(self.experiment_id)]


def main(argv=None):
Expand Down

0 comments on commit 781dda0

Please sign in to comment.