1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2019 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #ifndef AVOGADRO_QTPLUGINS_CALCULATIONSUBMITTER_H
18 #define AVOGADRO_QTPLUGINS_CALCULATIONSUBMITTER_H
19 
20 #include <QSharedPointer>
21 #include <QVariant>
22 #include <QVariantMap>
23 
24 class QNetworkAccessManager;
25 class QNetworkReply;
26 
27 namespace Avogadro {
28 namespace QtPlugins {
29 
30 class CalculationSubmitter : public QObject
31 {
32   Q_OBJECT
33 
34 public:
35   explicit CalculationSubmitter(QSharedPointer<QNetworkAccessManager> manager,
36                                 const QString& girderUrl,
37                                 const QString& girderToken,
38                                 QObject* parent = nullptr);
39   ~CalculationSubmitter() override;
40 
setMoleculeCjson(const QString & cjson)41   void setMoleculeCjson(const QString& cjson) { m_moleculeCjson = cjson; }
setContainerName(const QString & name)42   void setContainerName(const QString& name) { m_containerName = name; }
setImageName(const QString & name)43   void setImageName(const QString& name) { m_imageName = name; }
setInputParameters(const QVariantMap & m)44   void setInputParameters(const QVariantMap& m) { m_inputParameters = m; }
45 
46   void start();
47 
48 signals:
49   // The results will contain "calculationId" if the calculation has
50   // already been done before. The results will contain "taskFlowId"
51   // if a new calculation was submitted.
52   void finished(const QVariantMap& results);
53   void error(const QString& errorMessage, QNetworkReply* error = nullptr);
54 
55 private slots:
56   void uploadMolecule();
57   void finishUploadMolecule(const QVariant& results);
58 
59   void uploadGeometry();
60   void finishUploadGeometry(const QVariant& results);
61 
62   void fetchCalculation();
63   void finishFetchCalculation(const QVariant& results);
64 
65   void fetchCluster();
66   void finishFetchCluster(const QVariant& results);
67 
68   void fetchOrCreateQueue();
69   void finishFetchOrCreateQueue(const QVariant& results);
70 
71   void createQueue();
72   void finishCreateQueue(const QVariant& results);
73 
74   void createPendingCalculation();
75   void finishCreatePendingCalculation(const QVariant& results);
76 
77   void createTaskFlow();
78   void finishCreateTaskFlow(const QVariant& results);
79 
80   void addTaskFlowToQueue();
81   void finishAddTaskFlowToQueue(const QVariant& results);
82 
83   void popQueue();
84   void finishPopQueue(const QVariant& results);
85 
86   void handleError(const QString& msg, QNetworkReply* networkReply);
87 
88 private:
89   QString m_girderUrl = "http://localhost:8080/api/v1";
90   QString m_girderToken;
91 
92   // These should be set before starting
93   QString m_moleculeCjson;
94   QString m_containerName;
95   QString m_imageName;
96   QVariantMap m_inputParameters;
97 
98   // These will be set during the process
99   QString m_moleculeId;
100   QString m_geometryId;
101   QString m_pendingCalculationId;
102   QString m_clusterId;
103   QString m_queueId;
104   QString m_taskFlowId;
105 
106   QSharedPointer<QNetworkAccessManager> m_networkManager;
107 };
108 
109 } // namespace QtPlugins
110 } // namespace Avogadro
111 
112 #endif // AVOGADRO_QTPLUGINS_CALCULATIONSUBMITTER_H
113