From 54bb0c68d2b649f9a991e7beb1b86394ee3a9286 Mon Sep 17 00:00:00 2001
From: NithyaSanthanam <nithya.s@matriotsolutions.com>
Date: Fri, 6 Aug 2021 19:31:38 +0530
Subject: [PATCH] TMSS-893: Created default template in QA Reporting

---
 .../src/routes/Workflow/qa.reporting.js       |   7 +-
 .../routes/Workflow/qa.reporting.template.js  | 133 ++++++++++++++++++
 .../src/services/schedule.service.js          |  11 ++
 3 files changed, 149 insertions(+), 2 deletions(-)
 create mode 100644 SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.template.js

diff --git a/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.js b/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.js
index e31a93ea7bc..0001046f29a 100644
--- a/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.js
+++ b/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.js
@@ -5,6 +5,7 @@ import 'suneditor/dist/css/suneditor.min.css'; // Import Sun Editor's CSS File
 import { Dropdown } from 'primereact/dropdown';
 import WorkflowService from '../../services/workflow.service';
 import { Checkbox } from 'primereact/checkbox';
+import QaReportingTemplate from './qa.reporting.template';
 
 //import katex from 'katex' // for mathematical operations on sun editor this component should be added
 //import 'katex/dist/katex.min.css'
@@ -23,6 +24,8 @@ class QAreporting extends Component{
     }
 
     async componentDidMount() {
+       let defaultTemplate = await QaReportingTemplate.getReportTemplate(this.props.id)
+       this.setState({defaultTemplate : defaultTemplate})
         if (this.props.readOnly) {
             const QAreportingresponse = await WorkflowService.getQAReportingTo(this.props.process.qa_reporting_to);
             this.setState({
@@ -89,9 +92,9 @@ class QAreporting extends Component{
                         </>}
                     </label>
                     <div className="col-lg-12 col-md-12 col-sm-12"></div>
-                    {!this.props.readOnly && <SunEditor setDefaultStyle="min-height: 250px; height: auto" enableToolbar={true}  disabled={this.props.readOnly}
+                    {!this.props.readOnly && <SunEditor setDefaultStyle="min-height: 250px; height: auto; font-size: 24px; font: SansSerif " enableToolbar={true}  disabled={this.props.readOnly}
                         onChange={ this.handleChange }
-                        setContents={this.state.content}
+                        setContents={this.state.defaultTemplate}
                         setOptions={{
                             buttonList: [
                                 ['undo', 'redo', 'bold', 'underline', 'fontColor', 'table', 'link', 'image', 'video', 'italic', 'strike', 'subscript',
diff --git a/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.template.js b/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.template.js
new file mode 100644
index 00000000000..6abfeee7e3e
--- /dev/null
+++ b/SAS/TMSS/frontend/tmss_webapp/src/routes/Workflow/qa.reporting.template.js
@@ -0,0 +1,133 @@
+import ScheduleService from "../../services/schedule.service";
+import UIConstants from '../../utils/ui.constants';
+import moment from 'moment';
+import TaskService from "../../services/task.service";
+
+const QaReportingTemplate = {
+    getReportTemplate : async function(id) {
+        const SUB = await ScheduleService.getSchedulingUnitExtended('blueprint', id);
+        const cycle = await ScheduleService.getCycleNamebyProject(SUB.scheduling_set_id)
+        let cycleName = cycle.project.cycles[0].name
+        let tasks = SUB.task_blueprints;
+        let project_id =  SUB.scheduling_set_object.project_id
+
+        let observations = []
+    if(tasks){
+        for(let task in tasks){
+            let observation = {
+                pipeline: []
+            }
+            if(tasks[task].task_type === "observation"){
+                const taskSuccessor = await TaskService.getTaskSuccessors('blueprint', tasks[task].id)
+                console.log(taskSuccessor)
+                taskSuccessor.forEach(successor => {
+                    if(successor.task_type === "pipeline"){
+                        observation.pipeline.push(successor.id);
+                    }
+                    
+                });
+                let subtasks = tasks[task].subtasks;
+                subtasks.forEach(subtask => {
+                    if(subtask.subtask_type === 'observation'){
+                        console.log(tasks[task])
+                        observation.controlId = subtask.id
+                        observation.scheduleUnitId =  tasks[task].scheduling_unit_blueprint_id
+                        observation.taskId = tasks[task].id
+                        observation.project =  project_id
+                        observation.antenna = subtask.specifications_doc.stations.antenna_set
+                        observation.filter = subtask.specifications_doc.stations.filter
+                        observation.startTime = moment(subtask.start_time, moment.ISO_8601).format(UIConstants.CALENDAR_DATETIME_FORMAT)
+                        observation.stopTime = moment(subtask.stop_time, moment.ISO_8601).format(UIConstants.CALENDAR_DATETIME_FORMAT)
+                        observation.targetname = tasks[task].specifications_doc.SAPs ? tasks[task].specifications_doc.SAPs[0].name: ''
+                        observation.subbands = tasks[task].specifications_doc.SAPs ? tasks[task].specifications_doc.SAPs[0].subbands.length: ''
+                        observation.dataproducts = subtask.output_dataproducts.length
+                        observations.push(observation)
+                    }
+                });
+                
+            }
+        }
+    }
+    let tableBody = ''
+
+    observations.forEach(observation => {
+        tableBody = tableBody+`
+        <tr>
+            <td>${observation.controlId}</td>
+            <td>${observation.scheduleUnitId}</td>
+            <td>${observation.taskId}</td>
+            <td>${observation.pipeline.join(",")}</td>
+            <td>${observation.project}</td>
+            <td>${observation.targetname}</td>
+            <td><a href='https://proxy.lofar.eu/inspect/HTML/${observation.controlId}/Stations/station_beamlets.html'>BST</a></td>
+            <td>90%</td>
+            <td>${observation.antenna}</td>
+            <td>${observation.filter}</td>
+            <td>${observation.startTime}</td>
+            <td>${observation.stopTime}</td>
+            <td>${observation.subbands}</td>
+            <td>${observation.dataproducts}</td>
+            <td><a href='https://proxy.lofar.eu/inspect/${observation.controlId}/rtcp-${observation.controlId}.parset'>parset</a></td>
+        </tr>
+            `
+            
+
+    });
+    
+    return(
+        `<p>Subject- Observation failed</p>
+        <p>Dear Colleague,</p>
+        <p> The following message contains information regarding a LOFAR ${cycleName} project for which you are listed as the contact author.
+        Please forward this information to your collaborators.</p>
+        <p> We would like to inform you that observations related to your LOFAR ${cycleName} project have been performed. Please find detailed information below.</p>
+        <h3>General notes:</h3>
+        <p> The validation plots linked below remains online for 3 weeks from the date of observation. After that, they are compressed and transferred to offline storage. users can request access to them by submitting a ticket through the <a href='https://support.astron.nl/sdchelpdesk'>helpdesk</a>.
+        <h3>Observations:</h3>
+        <table>
+            <thead>
+                <th>Observation ID</th>
+                <th> Scheduling Unit ID</th>
+                <th> Task ID</th>
+                <th> Pipeline ID</th>
+                <th> Project</th>
+                <th> Target Name</th>
+                <th> Station plots</th>
+                <th> Completeness</th>
+                <th> Antenna Set</th>
+                <th> Filter</th>
+                <th> Start Time</th>
+                <th> End time</th>
+                <th> Number of Sub-bands</th>
+                <th> Number of files</th>
+                <th> Parset</th>
+            </thead>
+            <tbody>
+                ${tableBody}
+            </tbody>
+        </table>
+        <br/>
+        <h3>Performance of the system:</h3>
+        <h3>Data recording:</h3>
+        <h3>Data processing:</h3>
+        <h3>Archiving:</h3>
+        <h3 style='display:inline'>Remarks:</h3><p style= "display:inline; position: relative"> Please analyse the validation plots at 
+        <a href='https://proxy.lofar.eu/inspect/HTML/'>https://proxy.lofar.eu/inspect/HTML/</a> within 24 hours after this notification and submit your 
+        findings using the form at <a href="http://localhost:3000/schedulingunit/${id}/workflow">http://localhost:3000/schedulingunit/${id}/workflow </a>.
+        After this time window has passed, we will assume that your judgement is that the observation was successful and we will complete the actions described above ourselves to support your run.</p>
+        <br/>
+        <p>From the moment the data are made available to you at the LTA you have four weeks to check their quality and to report any problems to the 
+        Observatory through the helpdesk. After this time window has passed, no requests for re-observations will be considered.</p>
+
+        <h3>Actions:</h3>
+        <p>If you need any further clarification, please do not hesitate to contact us through the SDC helpdesk at <a href='https://support.astron.nl/sdchelpdesk'>https://support.astron.nl/sdchelpdesk</a>, specifying your project code in the subject.
+
+        <p>Best regards,</p>
+        <p>TO name (Telescope Operations)</p>
+        <p>SDCO name (Science Data center Operations)</p>`
+       
+    )
+    }
+    
+};
+
+export default QaReportingTemplate;
\ No newline at end of file
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 8a8d9dffdb4..173d9898924 100644
--- a/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js
+++ b/SAS/TMSS/frontend/tmss_webapp/src/services/schedule.service.js
@@ -1027,6 +1027,17 @@ const ScheduleService = {
             return null
         }
     },
+    getCycleNamebyProject: async function (id) {
+        let response = {};
+        try {
+            let url = `/api/scheduling_set/${id}/?expand=project,project.cycles&fields=project.cycles.name,name`;
+            response = await axios.get(url);
+        } catch (error) {
+            console.error('[schedule.services.updateSchedulingUnit]',"Mistake", error);
+            return null
+        }
+        return response.data
+    } 
 
 }
 
-- 
GitLab