Return affiliation, library name, and comment in the lanes_for json
[htsworkflow.git] / htsworkflow / frontend / experiments / experiments.py
index 7e6c7343faec43204ef0640f99cb2d85d4ea2ef4..83b541437603197923c741550e5ca7da44dddf88 100755 (executable)
@@ -13,10 +13,10 @@ from django.core.exceptions import ObjectDoesNotExist
 from django.core.mail import send_mail, mail_admins
 from django.http import HttpResponse, Http404
 
-from htsworkflow.frontend import settings
-from htsworkflow.frontend.experiments.models import FlowCell, DataRun
-from htsworkflow.frontend.samples.models import Library
 from htsworkflow.frontend.auth import require_api_key
+from htsworkflow.frontend import settings
+from htsworkflow.frontend.experiments.models import FlowCell, DataRun, Lane
+from htsworkflow.frontend.samples.models import Library, HTSUser
 
 def flowcell_information(flowcell_id):
     """
@@ -39,14 +39,20 @@ def flowcell_information(flowcell_id):
             'library_name': lane.library.library_name,
             'library_id': lane.library.id,
             'library_species': lane.library.library_species.scientific_name,
-            'pM': float(lane.pM),
-            'read_length': fc.read_length
+            'pM': unicode(lane.pM),
+            'read_length': lane.flowcell.read_length
         }
+
+    if fc.control_lane is None:
+        control_lane = None
+    else:
+        control_lane = int(fc.control_lane)
+        
     info = {
         'advanced_run': fc.advanced_run,
         'cluster_station_id': fc.cluster_station_id,
         'cluster_station': fc.cluster_station.name,
-        'control_lane': int(fc.control_lane),
+        'control_lane': control_lane,
         # 'datarun_set': how should this be represented?,
         'flowcell_id': fc.flowcell_id,
         'id': fc.id,
@@ -74,7 +80,48 @@ def flowcell_json(request, fc_id):
     
     fc_json = json.dumps(fc_dict)
     return HttpResponse(fc_json, mimetype = 'application/json')
+
+def lanes_for(username=None):
+    """
+    Given a user id try to return recent lanes as a list of dictionaries
+    """
+    query = {}
+    if username is not None:
+        user = HTSUser.objects.get(username=username)        
+        query.update({'library__affiliations__users__id': user.id})
+        
+    lanes = Lane.objects.filter(**query).order_by('-flowcell__run_date')
+
     
+    result = []
+    for l in lanes:
+        affiliations = l.library.affiliations.all()
+        affiliations_list = [(a.id, a.name) for a in affiliations]
+        result.append({ 'flowcell': l.flowcell.flowcell_id,
+                        'run_date': l.flowcell.run_date.isoformat(),
+                        'lane_number': l.lane_number,
+                        'library': l.library.id,
+                        'library_name': l.library.library_name,
+                        'comment': l.comment,
+                        'affiliations': affiliations_list})
+    return result
+
+def lanes_for_json(request, username):
+    """
+    Format lanes for a user
+    """
+    require_api_key(request)
+
+    try:
+        result = lanes_for(username)
+    except ObjectDoesNotExist, e:
+        raise Http404
+    
+    #convert query set to python structure
+    
+    result_json = json.dumps(result)
+    return HttpResponse(result_json, mimetype='application/json')
+                 
 def updStatus(request):
     output=''
     user = 'none'
@@ -256,10 +303,26 @@ def estimateFlowcellDuration(flowcell):
     sequencing_time = timedelta(0, cycles * sequencing_seconds_per_cycle)
     analysis_time = timedelta(0, cycles * pipeline_seconds_per_cycle)
     estimate_mid = sequencing_time + analysis_time
+
+    return estimate_mid
+
+def estimateFlowcellTimeRemaining(flowcell):
+    estimate_mid = estimateFlowcellDuration(flowcell)
+    
+    # offset for how long we've been running
+    running_time = datetime.now() - flowcell.run_date
+    estimate_mid -= running_time
+
+    return estimate_mid
+
+def roundToDays(estimate):
+    """
+    Given a time estimate round up and down in days
+    """
     # floor estimate_mid
-    estimate_low = timedelta(estimate_mid.days, 0)
+    estimate_low = timedelta(estimate.days, 0)
     # floor estimate_mid and add a day
-    estimate_high = timedelta(estimate_mid.days+1, 0)
+    estimate_high = timedelta(estimate.days+1, 0)
     
     return (estimate_low, estimate_high)