Ejemplo n.º 1
0
 /// <summary>
 /// Checks if worker exists in list of workers
 /// </summary>
 /// <param name="workerId">A WorkerId</param>
 /// <returns>True if worker exists, false otherwise</returns>
 private bool WorkerExists(string workerId)
 {
     using (var ctx = new TurkRContext())
     {
         return ctx.Workers.Any(w => w.WorkerId == workerId);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Fires on connection with the hub
        /// </summary>
        public override Task OnConnected()
        {
            using(var ctx = new TurkRContext())
            {
                var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
                if(client == null)
                {
                    // Collect client info
                    var httpContext = Context.Request.GetHttpContext();
                    string ip = httpContext.Request.ServerVariables["REMOTE_ADDR"];
                    string browser = httpContext.Request.Browser.Browser + " " + httpContext.Request.Browser.Version;
                    var transport = Context.QueryString["transport"];
                    var page = Context.QueryString["page"];
                    var hubClient = Context.QueryString["client"];

                    bool isMonitor = false;
                    if(!string.IsNullOrEmpty(hubClient))
                    {
                        if (hubClient == "monitor") isMonitor = true;
                    }

                    if(isMonitor)
                    {
                        Groups.Add(Context.ConnectionId, "monitor");
                    }
                    else
                    {
                        Groups.Add(Context.ConnectionId, "client");
                    }

                    var newClient = new Client
                    {
                        ConnectionId = Context.ConnectionId,
                        IsConnected = true,
                        IsMonitor = isMonitor,
                        Ip = ip,
                        Transport = transport,
                        UserAgent = browser,
                        StartDate = DateTime.Now,
                        EndDate = DateTime.Now
                    };

                    ctx.Clients.Add(newClient);
                    ctx.SaveChanges();
                }
                else
                {
                    client.IsConnected = true;
                    client.StartDate = DateTime.Now;
                    ctx.SaveChanges();
                }
            }

            HubUpdate();
            return base.OnConnected();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Pushes an update to the dashboard
 /// </summary>
 public void HubUpdate()
 {
     using (var ctx = new TurkRContext())
     {
         var monitors = ctx.Clients.Count(c => c.IsConnected && c.IsMonitor);
         var waitingClients = ctx.Clients.Count(c => c.IsConnected && !c.IsMonitor);
         var workingClients = 0; //ctx.Clients.Count(c => c.IsConnected && !c.IsMonitor);
         var dashboardData = new { MonitorCount = monitors, WaitingCount = waitingClients, WorkingCount = workingClients };
         Clients.Group("monitor").updateDashboard(dashboardData);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Binds the GridView to the data context
        /// </summary>
        private void BindDetailsView(string workerId)
        {
            using (var ctx = new TurkRContext())
            {
                var workers = ctx.Workers.Where(w => w.WorkerId == workerId);
                if (workers.Count() > 0)
                {

                    ProfileDetailsView.DataSource = workers.ToList();
                    ProfileDetailsView.DataBind();
                }
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Approves all pending assignments of a hit
 /// </summary>
 /// <param name="hitId">A HitId</param>
 /// <param name="reason">Reason or feedback for the worker</param>
 public void ApproveAllAssignments(int hitId, string reason)
 {
     using (var ctx = new TurkRContext())
     {
         var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
         if (hit != null)
         {
             SimpleClient myClient = new SimpleClient(_mturkConfig);
             var assignments = myClient.GetAllAssignmentsForHIT(hit.AmtHitId);
             foreach (var a in assignments)
             {
                 if (a.AssignmentStatus == AssignmentStatus.Submitted)
                 {
                     myClient.ApproveAssignment(a.AssignmentId, reason);
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Signup of worker to a session
 /// </summary>
 /// <param name="sessionId"></param>
 /// <param name="workerId"></param>
 private void Signup(int sessionId, string workerId)
 {
     using(var ctx = new TurkRContext())
     {
         var session = ctx.Sessions.FirstOrDefault(s => s.SessionId == sessionId);
         if(session != null)
         {
             var worker = ctx.Workers.FirstOrDefault(w => w.WorkerId == workerId);
             if(worker != null)
             {
                 var a = new Appointment { SessionId = session.SessionId, WorkerId = worker.WorkerId };
                 ctx.Appointments.Add(a);
                 ctx.SaveChanges();
             }
         }
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Signout of a worker from a session
 /// </summary>
 /// <param name="sessionId"></param>
 /// <param name="workerId"></param>
 private void Signout(int sessionId, string workerId)
 {
     using (var ctx = new TurkRContext())
     {
         var appointment = ctx.Appointments.FirstOrDefault(a => a.SessionId == sessionId & a.WorkerId == workerId);
         if(appointment != null)
         {
             ctx.Appointments.Remove(appointment);
             ctx.SaveChanges();
         }
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Checks if the guid of a worker exists in list of workers, returns workerId if found
 /// </summary>
 /// <param name="id">A GUID as id</param>
 /// <param name="workerId">Out workerId</param>
 /// <returns>True if worker exists, false otherwise</returns>
 private bool GuidExists(string id, out string workerId)
 {
     using (var ctx = new TurkRContext())
     {
         workerId = "";
         var worker = ctx.Workers.FirstOrDefault(w => w.WorkerGuid == id);
         if(worker != null)
         {
             workerId = worker.WorkerId;
             return true;
         }
         return false;
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Binds the GridView to the data context
        /// </summary>
        private void BindGridView()
        {
            using (var ctx = new TurkRContext())
            {
                //DataSourceID="SessionsDataSource"

                var sesssions = ctx.Sessions.Where(s => s.StartTime > DateTime.UtcNow);
                SessionsGridView.DataSource = sesssions.ToList();
                SessionsGridView.DataBind();
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Returns a list of Appointsments a worker has signed up
 /// </summary>
 /// <param name="workerId">A WorkerId</param>
 /// <returns>List of Appointments</returns>
 private List<Appointment> AppointmentsByWorker(string workerId)
 {
     using (var ctx = new TurkRContext())
     {
         return ctx.Appointments.Where(a => a.WorkerId == workerId).ToList();
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Fires on disconnection with the hub
 /// </summary>
 public override Task OnDisconnected()
 {
     using (var ctx = new TurkRContext())
     {
         var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
         if (client != null)
         {
             if(client.IsMonitor)
             {
                 //client.EndDate = DateTime.Now;
                 //client.IsConnected = false;
                 ctx.Clients.Remove(client);
                 ctx.SaveChanges();
             }
             else
             {
                 //client.EndDate = DateTime.Now;
                 //client.IsConnected = false;
                 ctx.Clients.Remove(client);
                 ctx.SaveChanges();
             }
         }
     }
     HubUpdate();
     return base.OnDisconnected();
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Extends number of assignments and expiration time
 /// </summary>
 /// <param name="hitId">The HitId</param>
 /// <param name="assignmentIncrement">Number of additional assignments.</param>
 /// <param name="expirationIncrement">Number of additional seconds to expiry.</param>
 public void ExtendHit(int hitId, int assignmentIncrement, long expirationIncrement)
 {
     using (var ctx = new TurkRContext())
     {
         var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
         if (hit != null)
         {
             SimpleClient myClient = new SimpleClient(_mturkConfig);
             myClient.ExtendHIT(hit.AmtHitId, assignmentIncrement, expirationIncrement);
         }
     }
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Forces expiration of a HIT
 /// </summary>
 /// <param name="hitId">A HitId</param>
 public void ExpireHit(int hitId)
 {
     using (var ctx = new TurkRContext())
     {
         var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
         if (hit != null)
         {
             SimpleClient myClient = new SimpleClient(_mturkConfig);
             myClient.ForceExpireHIT(hit.AmtHitId);
         }
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Create a HIT from a stored template.
        /// </summary>
        /// <param name="hitId">The Hit Id</param>
        public void CreateHit(int hitId)
        {
            using (var ctx = new TurkRContext())
            {
                var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
                if (hit != null)
                {
                    SimpleClient myClient = new SimpleClient(_mturkConfig);

                    // Setup qualifications
                    List<QualificationRequirement> qualifications = new List<QualificationRequirement>();

                    // First register the HitType
                    string hitTypeId = myClient.RegisterHITType(hit.Title, hit.Description, (long)hit.AutoApprovalDelay, (long)hit.Duration, (decimal)hit.Reward, hit.Keywords, qualifications);

                    // Define the external question
                    ExternalQuestion eq = new ExternalQuestion();
                    eq.ExternalURL = "https://www.descil.ethz.ch/apps/turkr/start/" + hit.HitCode;
                    eq.FrameHeight = "800";

                    // More definitions
                    string requesterAnnotation = hit.HitCode;
                    string[] responseGroup = null;

                    // Creat the HIT
                    var amtHit = myClient.CreateHIT(hitTypeId, hit.Title, hit.Description, hit.Keywords, eq, (decimal)hit.Reward, (long)hit.Duration, (long)hit.AutoApprovalDelay, (long)hit.Lifetime, hit.Assignments, requesterAnnotation, qualifications, responseGroup);

                    hit.AmtHitId = amtHit.HITId;
                    hit.CreationTime = DateTime.UtcNow;
                    ctx.SaveChanges();
                }
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Fires on reconnection with the hub
 /// </summary>
 public override Task OnReconnected()
 {
     using (var ctx = new TurkRContext())
     {
         var client = ctx.Clients.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
         if (client != null)
         {
             client.StartDate = DateTime.Now;
             client.IsConnected = true;
             ctx.SaveChanges();
         }
     }
     HubUpdate();
     return base.OnReconnected();
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Download HIT info from AMT
 /// </summary>
 /// <param name="hitId">The hitId</param>
 /// <returns>A HitInfo object</returns>
 public AmtHit GetHit(int hitId)
 {
     using(var ctx = new TurkRContext())
     {
         var hit = ctx.Hits.FirstOrDefault(h => h.HitId == hitId);
         if(hit != null)
         {
             // Get data from AMT
             return GetHit(hit.AmtHitId);
         }
     }
     // Return empty HitInfo;
     return new AmtHit();
 }
Ejemplo n.º 17
0
        public TurkRServiceResponse Session(string id)
        {
            using (var ctx = new TurkRContext())
            {
                var nextSession = ctx.Sessions.FirstOrDefault(s => s.Project.ProjectCode == id & s.IsActive);
                if(nextSession != null)
                {
                    // Generate session view
                    var sessionView = new SessionView(nextSession);

                    // Return coordinates to client
                    return new TurkRServiceResponse { Operation = "Session", Id = id, Result = sessionView, IsValid = true };
                }
                else
                {
                    return NotValid(new TurkRServiceRequest { Operation = "Session", Id = id });
                }
            }
        }