From 448a968058c850fb15c0bb8c799a4b95cafad1ed Mon Sep 17 00:00:00 2001
From: Reinder Kraaij <kraaij@astron.nl>
Date: Mon, 20 Nov 2023 15:22:45 +0100
Subject: [PATCH] Add SHow Draft without blueprint

---
 .../routes/Scheduling/SchedulingUnitList.js   | 60 +++++++++++++------
 .../src/services/schedule.service.js          | 10 +++-
 2 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/SAS/TMSS/frontend/tmss_webapp/src/routes/Scheduling/SchedulingUnitList.js b/SAS/TMSS/frontend/tmss_webapp/src/routes/Scheduling/SchedulingUnitList.js
index 7e6246baf1b..f7a053899b2 100644
--- a/SAS/TMSS/frontend/tmss_webapp/src/routes/Scheduling/SchedulingUnitList.js
+++ b/SAS/TMSS/frontend/tmss_webapp/src/routes/Scheduling/SchedulingUnitList.js
@@ -125,9 +125,10 @@ class SchedulingUnitList extends Component{
         this.getUIAttr();
         this.STATUS_BEFORE_SCHEDULED = ['defining', 'defined', 'schedulable'];  // Statuses before scheduled to get station_group
         this.mainStationGroups = {};
-        this.suTypeList = [{name: 'Draft'}, {name: 'Blueprint'}, {name: 'Constraints'}]
+        this.suTypeList = [{name: 'Draft'}, {name: 'Drafts Without BluePrint'},{name: 'Blueprint'}, {name: 'Constraints'}]
         this.totalPage = 0;
         this.statusList = [];
+        this.definitionCache = [];
         this.state = {
             showSpinner: false,
             suType: this.suUIAttr['listType'] || "Draft",
@@ -430,11 +431,15 @@ class SchedulingUnitList extends Component{
         return `${observation.length}/${pipeline.length}/${ingest.length}`;
     }
 
-    changesutype() {
-        let queryUrl = this.state.suType;
-        if (this.state.suType.toLowerCase() === 'blueprint_schedule') {
+    changesutype(type = this.state.suType) {
+        
+        let queryUrl = type;
+        if (type.toLowerCase() === 'blueprint_schedule') {
             return queryUrl = 'blueprint'
         }
+        if (type=="Draft without BluePrint") {
+            return queryUrl ="draft"
+        }
         return queryUrl.toLowerCase();
     }
 
@@ -589,12 +594,28 @@ class SchedulingUnitList extends Component{
     /**
      * Get server side filter column details form API
      */
+
+
+    
     async getFilterColumns(type) {
         const columnDefinitionToBeRemove = ['status', 'unschedulable_reason', 'workflowStatus', 'results_accepted', 'on_sky_start_time', 'on_sky_stop_time', 'process_start_time', 'process_stop_time'];
         //For Constraint
         const constColDefToBeRemove = ['observation_strategy_template_name', 'duration', 'observation_strategy_template_id', 'observation_strategy_template_description', 'on_sky_start_time', 'on_sky_stop_time', 'process_start_time', 'process_stop_time', 'task_content',
         'target_observation_sap', 'do_cancel', 'created_at', 'updated_at', 'project_rank','rank', 'priority_queue', 'output_pinned', 'draft', 'targetName','targetAngle1','targetAngle2','targetRef'];
-        const suFilters = await ScheduleService.getSchedulingUnitFilterDefinition(type);
+        let  suFilters
+
+        if (!this.definitionCache[type]) { // don't send out time after time a request to get the schema :)
+            this.definitionCache[type] = "loading";
+            suFilters = await ScheduleService.getSchedulingUnitFilterDefinition(type);
+            this.definitionCache[type]=suFilters;
+        }
+        else     {
+            if (this.definitionCache[type]=="loading")   {
+                return
+            }
+            suFilters = this.definitionCache[type];
+        }
+
         this.columnMap = [];
         let tmpDefaulColumns = _.cloneDeep(this.state.defaultcolumns[0]);
         let tmpOptionalColumns = _.cloneDeep(this.state.optionalcolumns[0]);
@@ -623,7 +644,7 @@ class SchedulingUnitList extends Component{
             }
             //Remove columns which is not relavent for Draft
             //this.removeArrayIndex(tmpColumnOrders, columnOrderToBeRemove);
-            if(type === 'draft' || type === 'constraints') {
+            if(type.startsWith('draft') || type === 'constraints') {
                 this.removeColumns(tmpDefaulColumns, [...columnDefinitionToBeRemove, ...this.minDistance]);//Blueprint columns to be remove
                 this.removeColumns(tmpOptionalColumns, [...columnDefinitionToBeRemove, ...this.minDistance]);
             }
@@ -762,7 +783,7 @@ class SchedulingUnitList extends Component{
                     {this.affectedSU.length > 0 && 
                     <div>
                         <div style={{marginTop: '1em'}}>
-                        <span>Scheduling unit {this.state.suType.toLowerCase() === "draft"?"blueprint":"draft"}(s) shown below will be affected. Do you want to continue? </span>
+                        <span>Scheduling unit {this.state.suType.toLowerCase().startsWith("draft")?"blueprint":"draft"}(s) shown below will be affected. Do you want to continue? </span>
                         <DataTable value={this.affectedSU} resizableColumns columnResizeMode="expand" className="card" style={{paddingLeft: '0em'}}>
                             <Column field="id" header="Id"></Column>
                             <Column field="name" header="Name"></Column>
@@ -808,9 +829,10 @@ class SchedulingUnitList extends Component{
             let scheduleunits = [];
             let expand,fields;
             let params=suType.toLowerCase();
-            if(suType.toLowerCase() === 'draft'){
+            if(suType.toLowerCase().startsWith('draft')){
                 expand=this.SU_DRAFT_EXPAND;
-                fields=this.SU_DRAFT_FIELDS;              
+                fields=this.SU_DRAFT_FIELDS;      
+                        
             }else if(suType.toLowerCase() === 'blueprint'){
                 expand=this.SU_BLUEPRINT_EXPAND;
                 fields=this.SU_BLUEPRINT_FIELDS;   
@@ -818,7 +840,11 @@ class SchedulingUnitList extends Component{
                 expand=this.SU_BPCONSTRAINTS_EXPAND;
                 fields=this.SU_BPCONSTRAINTS_FIELDS;
                 params='blueprint';
-            }                
+            } 
+            
+            if (suType=="drafts without blueprint") {
+                filterQry = filterQry + "&blueprinted=false" 
+            }
             let response = await ScheduleService.getSchedulingUnitsExpandWithFilter(params.toLowerCase(), expand, filterQry, orderBy, limit, offset, fields.join(","));    
             if (response && response.data) {
                 this.totalPage = response.data.count;
@@ -833,7 +859,7 @@ class SchedulingUnitList extends Component{
             }
             let projectSUs = _.uniqBy(_.map(scheduleunits, function(scheduleunit) {
                                         return {
-                                            project: suType.toLowerCase() === 'draft'? scheduleunit.scheduling_set.project.name: scheduleunit.draft.scheduling_set.project.name,
+                                            project: suType.toLowerCase().startsWith( 'draft')? scheduleunit.scheduling_set.project.name: scheduleunit.draft.scheduling_set.project.name,
                                             id: scheduleunit.id
                                         }
                                     }),'project');
@@ -861,7 +887,7 @@ class SchedulingUnitList extends Component{
                 copyFailedTasks: _.filter(projectSUs, {copyFailedTasks: true}).length === projectSUs.length,
             }
 
-            if ( suType.toLowerCase() === 'draft') {
+            if ( suType.toLowerCase().startsWith('draft')) {
                 for( const scheduleunit  of scheduleunits){
                     let updatedSU = {};
                     if (!this.props.project || (this.props.project && this.props.project === scheduleunit.scheduling_set.project.name)) {
@@ -1694,7 +1720,7 @@ class SchedulingUnitList extends Component{
         this.pageUpdated = true;
         this.filterQry = '';
         this.orderBy = '';
-        await this.setState({suType: type, scheduleunit: [], loadingStatus:true});
+        this.setState({suType: type, scheduleunit: [], loadingStatus:true});
         let sortData = UtilService.localStore({ type: 'get', key: this.lsKeySortColumn });
         if (sortData) {
             let columnDetails = _.find(this.state.columnMap, {displayName:sortData[0]?sortData[0].id:sortData.id});
@@ -1702,8 +1728,8 @@ class SchedulingUnitList extends Component{
                 this.orderBy = 'ordering='+((sortData[0]?sortData[0].desc:sortData.desc)?'-':'')+columnDetails.orgField;
             }
         }
-        this.getSchedulingUnitList(false,this.changesutype(),this.filterQry, this.orderBy, this.limit, this.offset);
-        this.storeUIAttr("listType", type.replace(" ","_"));
+        this.getSchedulingUnitList(false,this.changesutype(type),this.filterQry, this.orderBy, this.limit, this.offset);
+        this.storeUIAttr("listType", type.replaceAll(" ","_"));
     }
 
     /**
@@ -1967,7 +1993,7 @@ class SchedulingUnitList extends Component{
         e.preventDefault();
         if (this.selectedRows.length > 0) {
             const suBlueprintList = _.filter(this.selectedRows, (schedulingUnit) => { return schedulingUnit.type.toLowerCase() === "blueprint"});
-            const suDraftsList = _.filter(this.selectedRows, (schedulingUnit) => { return schedulingUnit.type.toLowerCase() === "draft"});
+            const suDraftsList = _.filter(this.selectedRows, (schedulingUnit) => { return schedulingUnit.type.toLowerCase().startsWith("draft")});
             const hasDrafts = suDraftsList.length > 0 ? true : false;
             const hasBlueprint = suBlueprintList.length > 0 ? true : false;
             if (hasBlueprint && !hasDrafts) {
@@ -2591,7 +2617,7 @@ class SchedulingUnitList extends Component{
      */
     addTargetColumns(scheduleunit) {
         scheduleunit['priority_queue'] = scheduleunit.priority_queue_value;
-        scheduleunit.taskDetails = scheduleunit.type==="Draft"?scheduleunit.task_drafts:scheduleunit.task_blueprints;
+        scheduleunit.taskDetails = scheduleunit.type?.startsWith("Draft")?scheduleunit.task_drafts:scheduleunit.task_blueprints;
         const targetObserv =  scheduleunit.taskDetails ? scheduleunit.taskDetails.find(task => task.specifications_template.type_value==='observation' && task.specifications_doc.station_configuration?.SAPs) : null;
         scheduleunit['priority_queue'] = scheduleunit.priority_queue_value;
         if (targetObserv) {
diff --git a/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js b/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js
index a47b4668166..df3f61cfee1 100644
--- a/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js
+++ b/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js
@@ -809,16 +809,22 @@ const ScheduleService = {
             return false;
         }
     },
-    getSchedulingUnitFilterDefinition: async function (type) {
+    
+       getSchedulingUnitFilterDefinition: async function (type) {
+        
         if (type === "constraints") {
             type = "blueprint";
         }
+
+        if (type.startsWith("draft"))  { type = "draft";  }
+
         let res = [];
         try {
             res = await axios.options(`/api/scheduling_unit_${type}_extended/`);
         } catch (error) {
             console.error('[schedule.services.getSchedulingUnitFilterDefinition]', error);
         }
+        
         return res;
     },
     /* get All Task Relation */
@@ -901,6 +907,8 @@ const ScheduleService = {
         if (type === "constraints") {
             type = "blueprint";
         }
+        if (type.startsWith("draft")) type="draft";
+
         let response = null;
         try {
             let api = `/api/scheduling_unit_${type}${fields ? '' : '_extended'}/?`;
-- 
GitLab