@@ -512,58 +512,71 @@ def save(self, result=None):
512512 # upsert values
513513 self .memDB .series_service .upsert_values (values )
514514 # save new annotations
515- self .add_annotations (self .memDB .annotation_list )
515+ if len (self .memDB .annotation_list > 0 ):
516+ self .add_annotations (self .memDB .annotation_list )
517+ return result
516518 except Exception as e :
517519 logger .error ("Exception encountered while saving: {}" .format (e ))
518- return result
520+ return None
519521
520522 def save_existing (self , result ):
521523 result = self .save (result )
522524 return result
523525
524526 def save_appending (self , result , overwrite = True ):
525- values = self .memDB .getDataValuesDF ()
526-
527- # get value count
528- vc = result .ValueCount
529- # set in df
530- values ["resultid" ] = result .ResultID
531-
532- # count = overlap calc
533- count = self .overlapcalc (result , values , overwrite )
534- # set value count = res.vc+valuecount-count
535- valuecount = result .ValueCount + vc - count
536- # update result
537- self .updateResult (result , valuecount )
538- # insert values
539- self .memDB .series_service .upsert_values (values )
540- # save new annotations
541- self .add_annotations (self .memDB .annotation_list )
542-
543- return result
544-
545- def save_as (self , variable , method , proc_level , action , action_by ):
546- #save as new series
547- values = self .memDB .getDataValuesDF ()
548- # get all annotations for series
549- annolist = self .memDB .series_service .get_annotations_by_result (str (values ["resultid" ][0 ]))
550- annolist ['valueid' ]= None
527+ try :
551528
552- # create series
553- result = self .getResult (variable , method , proc_level , action , action_by )
529+ values = self .memDB .getDataValuesDF ()
554530
555- # set in df
556- values [ "resultid" ] = result .ResultID
557- # insert values
558- self . memDB . series_service . insert_values ( values )
531+ # get value count
532+ vc = result .ValueCount
533+ # set in df
534+ values [ "resultid" ] = result . ResultID
559535
560- # save all annotations
561- frames = [self .memDB .annotation_list , annolist ]
562- annolist = pd .concat (frames )
563- self .add_annotations (annolist )
536+ # count = overlap calc
537+ count = self .overlapcalc (result , values , overwrite )
538+ # set value count = res.vc+valuecount-count
539+ valuecount = result .ValueCount + vc - count
540+ # update result
541+ self .updateResult (result , valuecount )
542+ # insert values
543+ self .memDB .series_service .upsert_values (values )
544+ # save new annotations
545+ if len (self .memDB .annotation_list > 0 ):
546+ self .add_annotations (self .memDB .annotation_list )
547+ return result
548+ except Exception as e :
549+ logger .error ("Exception encountered while performing a save as: {}" .format (e ))
550+ return None
564551
552+ def save_as (self , variable , method , proc_level , action , action_by ):
565553
566- return result
554+ try :
555+ #save as new series
556+ values = self .memDB .getDataValuesDF ()
557+ # get all annotations for series
558+ annolist = self .memDB .series_service .get_annotations_by_result (str (values ["resultid" ][0 ]))
559+ annolist ['valueid' ]= None
560+
561+ # create series
562+ result = self .getResult (variable , method , proc_level , action , action_by )
563+
564+ # set in df
565+ values ["resultid" ] = result .ResultID
566+ # insert values
567+ self .memDB .series_service .insert_values (values )
568+
569+ #combine all of the annotations new annotations with the existing
570+ frames = [self .memDB .annotation_list , annolist ]
571+ annolist = pd .concat (frames )
572+ # save all annotations
573+ if len (annolist > 0 ):
574+ self .add_annotations (annolist )
575+
576+ return result
577+ except Exception as e :
578+ logger .error ("Exception encountered while performing a save as: {}" .format (e ))
579+ return None
567580
568581 def getResult (self , var , meth , proc , action , action_by ):
569582 values = self .memDB .getDataValuesDF ()
@@ -602,17 +615,19 @@ def getResult(self, var, meth, proc, action, action_by):
602615 newaction .MethodID = action .MethodID
603616 newaction .ActionTypeCV = "Derivation"
604617
605- print newaction
618+ print "creating an action"
606619 newaction = self .memDB .series_service .create .createAction (newaction ) # it times out. find out why
607620 print newaction
608621
609622
610623 # create Actionby done
624+ print "creating an actionby"
611625 action_by .ActionID = newaction .ActionID
612626 action_by = self .memDB .series_service .create .createActionby (action_by )
613627 print action_by
614628
615629
630+ print "creating a feature_action"
616631 # create FeatureAction (using current sampling feature id)
617632 sampling_feature = result .FeatureActionObj .SamplingFeatureObj
618633 self .memDB .series_service .read ._session .expunge (result .FeatureActionObj .SamplingFeatureObj )
@@ -625,6 +640,7 @@ def getResult(self, var, meth, proc, action, action_by):
625640 feature_action = self .memDB .series_service .create .createFeatureAction (feature_action )
626641 print feature_action
627642
643+ print "creating a result"
628644 # create TimeSeriesResult - this should also contain all of the stuff for the Result
629645 time , offset = self .get_current_time_and_utcoffset ()
630646
@@ -674,9 +690,9 @@ def overlapcalc(self, result, values, overwrite):
674690 #is there any overlap
675691 dbend = result .FeatureActionObj .ActionObj .EndDateTime
676692 dfstart = datetime .datetime .strptime (str (np .min (values ["valuedatetime" ])), form )
677- overlap = dbend >= dfstart
693+ overlap = dbend >= dfstart
678694 #number of overlapping values
679- overlapdf = values [(values ["valuedatetime" ]<= dfstart ) & (values ["valuedatetime" ]>= dbend )]
695+ overlapdf = values [(values ["valuedatetime" ] <= dfstart ) & (values ["valuedatetime" ] >= dbend )]
680696 count = len (overlapdf )
681697 #if not overwrite. remove any overlapping values from df
682698 if overlap :
@@ -710,6 +726,7 @@ def add_annotations(self, annolist):
710726 mynewdf = newdf [["valueid_y" ,"annotationid" ]]
711727 mynewdf .columns = ["ValueID" , "AnnotationID" ]
712728
729+
713730 # save df to db
714731 self .memDB .series_service .add_annotations (mynewdf )
715732
0 commit comments