~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to pgadmin/agent/pgaJob.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2011-06-07 23:03:54 UTC
  • mfrom: (1.3.1 upstream) (13 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110607230354-3td4j9y71u4ahcvj
Tags: 1.14.0~beta1-1
* New upstream development release, adding Build-Depends on
  postgresql-server-dev-all >= 117~.
* Add Build-Depends on quilt, (un)patch to debian/rules and patch for fixing
  the include for kwlist.h in pgadmin/db/keywords.c.
* Add pg_config --includedir-server output to CPPFLAGS.
* Remove unrecognized configure options: --with-wx-config,
  --with-pgsql-include, --enable-gtk2, --enable-unicode.
* Clean up manually the files that are left behind after the broken
  distclean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//////////////////////////////////////////////////////////////////////////
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
 
// 
5
 
// Copyright (C) 2002 - 2010, The pgAdmin Development Team
 
4
//
 
5
// Copyright (C) 2002 - 2011, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// pgaJob.h - PostgreSQL Agent Job
23
23
#include "agent/pgaStep.h"
24
24
#include "agent/pgaSchedule.h"
25
25
 
26
 
pgaJob::pgaJob(const wxString& newName)
27
 
: pgServerObject(jobFactory, newName)
28
 
{
 
26
pgaJob::pgaJob(const wxString &newName)
 
27
        : pgServerObject(jobFactory, newName)
 
28
{
 
29
}
 
30
 
 
31
wxString pgaJob::GetTranslatedMessage(int kindOfMessage) const
 
32
{
 
33
        wxString message = wxEmptyString;
 
34
 
 
35
        switch (kindOfMessage)
 
36
        {
 
37
                case RETRIEVINGDETAILS:
 
38
                        message = _("Retrieving details on pgAgent job");
 
39
                        break;
 
40
                case REFRESHINGDETAILS:
 
41
                        message = _("Refreshing pgAgent job");
 
42
                        break;
 
43
                case PROPERTIESREPORT:
 
44
                        message = _("pgAgent job properties report");
 
45
                        break;
 
46
                case PROPERTIES:
 
47
                        message = _("pgAgent job properties");
 
48
                        break;
 
49
                case DEPENDENCIESREPORT:
 
50
                        message = _("pgAgent job dependencies report");
 
51
                        break;
 
52
                case DEPENDENCIES:
 
53
                        message = _("pgAgent job dependencies");
 
54
                        break;
 
55
                case DEPENDENTSREPORT:
 
56
                        message = _("pgAgent job dependents report");
 
57
                        break;
 
58
                case DEPENDENTS:
 
59
                        message = _("pgAgent job dependents");
 
60
                        break;
 
61
        }
 
62
 
 
63
        if (!message.IsEmpty())
 
64
                message += wxT(" ") + GetName();
 
65
 
 
66
        return message;
29
67
}
30
68
 
31
69
int pgaJob::GetIconId()
32
70
{
33
 
    if (GetEnabled())
34
 
        return jobFactory.GetIconId();
35
 
    else
36
 
        return jobFactory.GetDisabledId();
 
71
        if (GetEnabled())
 
72
                return jobFactory.GetIconId();
 
73
        else
 
74
                return jobFactory.GetDisabledId();
37
75
}
38
76
 
39
77
 
40
78
wxMenu *pgaJob::GetNewMenu()
41
79
{
42
 
    wxMenu *menu=pgObject::GetNewMenu();
43
 
    if (1) // check priv.
44
 
    {
45
 
        stepFactory.AppendMenu(menu);
46
 
        scheduleFactory.AppendMenu(menu);
47
 
    }
48
 
    return menu;
 
80
        wxMenu *menu = pgObject::GetNewMenu();
 
81
        if (1) // check priv.
 
82
        {
 
83
                stepFactory.AppendMenu(menu);
 
84
                scheduleFactory.AppendMenu(menu);
 
85
        }
 
86
        return menu;
49
87
}
50
88
 
51
89
 
52
90
bool pgaJob::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
53
91
{
54
 
    return GetConnection()->ExecuteVoid(wxT("DELETE FROM pgagent.pga_job WHERE jobid=") + NumToStr(GetRecId()));
 
92
        return GetConnection()->ExecuteVoid(wxT("DELETE FROM pgagent.pga_job WHERE jobid=") + NumToStr(GetRecId()));
55
93
}
56
94
 
57
95
 
58
96
void pgaJob::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
59
97
{
60
 
    if (!expandedKids)
61
 
    {
62
 
        expandedKids=true;
63
 
 
64
 
        browser->RemoveDummyChild(this);
65
 
 
66
 
        // Log
67
 
        wxLogInfo(wxT("Adding child objects to Job."));
68
 
 
69
 
        browser->AppendCollection(this, scheduleFactory);
70
 
        browser->AppendCollection(this, stepFactory);
71
 
    }
72
 
 
73
 
    if (properties)
74
 
    {
75
 
        CreateListColumns(properties);
76
 
 
77
 
        properties->AppendItem(_("Name"), GetName());
78
 
        properties->AppendItem(_("ID"), GetRecId());
79
 
        properties->AppendItem(_("Enabled"), GetEnabled());
 
98
        if (!expandedKids)
 
99
        {
 
100
                expandedKids = true;
 
101
 
 
102
                browser->RemoveDummyChild(this);
 
103
 
 
104
                // Log
 
105
                wxLogInfo(wxT("Adding child objects to Job."));
 
106
 
 
107
                browser->AppendCollection(this, scheduleFactory);
 
108
                browser->AppendCollection(this, stepFactory);
 
109
        }
 
110
 
 
111
        if (properties)
 
112
        {
 
113
                CreateListColumns(properties);
 
114
 
 
115
                properties->AppendItem(_("Name"), GetName());
 
116
                properties->AppendItem(_("ID"), GetRecId());
 
117
                properties->AppendYesNoItem(_("Enabled"), GetEnabled());
80
118
                properties->AppendItem(_("Host agent"), GetHostAgent());
81
 
        properties->AppendItem(_("Job class"), GetJobclass());
82
 
        properties->AppendItem(_("Created"), GetCreated());
83
 
        properties->AppendItem(_("Changed"), GetChanged());
84
 
        properties->AppendItem(_("Next run"), GetNextrun());
85
 
        properties->AppendItem(_("Last run"), GetLastrun());
86
 
        properties->AppendItem(_("Last result"), GetLastresult());
 
119
                properties->AppendItem(_("Job class"), GetJobclass());
 
120
                properties->AppendItem(_("Created"), GetCreated());
 
121
                properties->AppendItem(_("Changed"), GetChanged());
 
122
                properties->AppendItem(_("Next run"), GetNextrun());
 
123
                properties->AppendItem(_("Last run"), GetLastrun());
 
124
                properties->AppendItem(_("Last result"), GetLastresult());
87
125
                if (!GetCurrentAgent().IsEmpty())
88
126
                        properties->AppendItem(_("Running at"), GetCurrentAgent());
89
127
                else
90
128
                        properties->AppendItem(_("Running at"), _("Not currently running"));
91
129
 
92
 
        properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
93
 
    }
 
130
                properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
 
131
        }
94
132
}
95
133
 
96
134
 
97
135
 
98
136
pgObject *pgaJob::Refresh(ctlTree *browser, const wxTreeItemId item)
99
137
{
100
 
    pgObject *job=0;
101
 
 
102
 
    pgObject *obj=browser->GetObject(browser->GetItemParent(item));
103
 
    if (obj->IsCollection())
104
 
        job = jobFactory.CreateObjects((pgCollection*)obj, 0, wxT("\n   WHERE j.jobid=") + NumToStr(GetRecId()));
105
 
 
106
 
    return job;
 
138
        pgObject *job = 0;
 
139
 
 
140
        pgObject *obj = browser->GetObject(browser->GetItemParent(item));
 
141
        if (obj->IsCollection())
 
142
                job = jobFactory.CreateObjects((pgCollection *)obj, 0, wxT("\n   WHERE j.jobid=") + NumToStr(GetRecId()));
 
143
 
 
144
        return job;
107
145
}
108
146
 
109
147
 
110
148
 
111
149
pgObject *pgaJobFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
112
150
{
113
 
    pgaJob *job=0;
114
 
 
115
 
    pgSet *jobs= collection->GetConnection()->ExecuteSet(
116
 
       wxT("SELECT j.*, cl.*, ag.*, sub.jlgstatus AS joblastresult ")
117
 
       wxT("  FROM pgagent.pga_job j JOIN")
118
 
       wxT("  pgagent.pga_jobclass cl ON cl.jclid=jobjclid LEFT OUTER JOIN")
119
 
       wxT("  pgagent.pga_jobagent ag ON ag.jagpid=jobagentid LEFT OUTER JOIN")
120
 
       wxT("  (SELECT DISTINCT ON (jlgjobid) jlgstatus, jlgjobid")
121
 
       wxT("   FROM pgagent.pga_joblog")
122
 
       wxT("  ORDER BY jlgjobid, jlgid desc) sub ON sub.jlgjobid = j.jobid ")
123
 
       + restriction +
124
 
       wxT("ORDER BY jobname;"));
125
 
 
126
 
    if (jobs)
127
 
    {
128
 
        while (!jobs->Eof())
129
 
        {
 
151
        pgaJob *job = 0;
 
152
 
 
153
        pgSet *jobs = collection->GetConnection()->ExecuteSet(
 
154
                          wxT("SELECT j.*, cl.*, ag.*, sub.jlgstatus AS joblastresult ")
 
155
                          wxT("  FROM pgagent.pga_job j JOIN")
 
156
                          wxT("  pgagent.pga_jobclass cl ON cl.jclid=jobjclid LEFT OUTER JOIN")
 
157
                          wxT("  pgagent.pga_jobagent ag ON ag.jagpid=jobagentid LEFT OUTER JOIN")
 
158
                          wxT("  (SELECT DISTINCT ON (jlgjobid) jlgstatus, jlgjobid")
 
159
                          wxT("   FROM pgagent.pga_joblog")
 
160
                          wxT("  ORDER BY jlgjobid, jlgid desc) sub ON sub.jlgjobid = j.jobid ")
 
161
                          + restriction +
 
162
                          wxT("ORDER BY jobname;"));
 
163
 
 
164
        if (jobs)
 
165
        {
 
166
                while (!jobs->Eof())
 
167
                {
130
168
                        wxString status;
131
169
                        if (jobs->GetVal(wxT("joblastresult")) == wxT("r"))
132
 
                status = _("Running");
133
 
                    else if (jobs->GetVal(wxT("joblastresult")) == wxT("s"))
134
 
                status = _("Successful");
 
170
                                status = _("Running");
 
171
                        else if (jobs->GetVal(wxT("joblastresult")) == wxT("s"))
 
172
                                status = _("Successful");
135
173
                        else if (jobs->GetVal(wxT("joblastresult")) == wxT("f"))
136
 
                status = _("Failed");
 
174
                                status = _("Failed");
137
175
                        else if (jobs->GetVal(wxT("joblastresult")) == wxT("d"))
138
 
                status = _("Aborted");
139
 
                else if (jobs->GetVal(wxT("joblastresult")) == wxT("i"))
140
 
                status = _("No steps");
 
176
                                status = _("Aborted");
 
177
                        else if (jobs->GetVal(wxT("joblastresult")) == wxT("i"))
 
178
                                status = _("No steps");
141
179
                        else
142
 
                status = _("Unknown");
143
 
 
144
 
            job = new pgaJob(jobs->GetVal(wxT("jobname")));
145
 
            job->iSetServer(collection->GetServer());
146
 
            job->iSetRecId(jobs->GetLong(wxT("jobid")));
147
 
            job->iSetComment(jobs->GetVal(wxT("jobdesc")));
148
 
 
149
 
            job->iSetEnabled(jobs->GetBool(wxT("jobenabled")));
150
 
            job->iSetJobclass(jobs->GetVal(wxT("jclname")));
 
180
                                status = _("Unknown");
 
181
 
 
182
                        job = new pgaJob(jobs->GetVal(wxT("jobname")));
 
183
                        job->iSetServer(collection->GetServer());
 
184
                        job->iSetRecId(jobs->GetLong(wxT("jobid")));
 
185
                        job->iSetComment(jobs->GetVal(wxT("jobdesc")));
 
186
 
 
187
                        job->iSetEnabled(jobs->GetBool(wxT("jobenabled")));
 
188
                        job->iSetJobclass(jobs->GetVal(wxT("jclname")));
151
189
                        job->iSetHostAgent(jobs->GetVal(wxT("jobhostagent")));
152
 
            job->iSetCreated(jobs->GetDateTime(wxT("jobcreated")));
153
 
            job->iSetChanged(jobs->GetDateTime(wxT("jobchanged")));
154
 
            job->iSetNextrun(jobs->GetDateTime(wxT("jobnextrun")));
155
 
            job->iSetLastrun(jobs->GetDateTime(wxT("joblastrun")));
156
 
            job->iSetLastresult(status);
 
190
                        job->iSetCreated(jobs->GetDateTime(wxT("jobcreated")));
 
191
                        job->iSetChanged(jobs->GetDateTime(wxT("jobchanged")));
 
192
                        job->iSetNextrun(jobs->GetDateTime(wxT("jobnextrun")));
 
193
                        job->iSetLastrun(jobs->GetDateTime(wxT("joblastrun")));
 
194
                        job->iSetLastresult(status);
157
195
                        job->iSetCurrentAgent(jobs->GetVal(wxT("jagstation")));
158
196
 
159
 
            if (browser)
160
 
            {
161
 
                browser->AppendObject(collection, job);
 
197
                        if (browser)
 
198
                        {
 
199
                                browser->AppendObject(collection, job);
162
200
                                jobs->MoveNext();
163
 
            }
164
 
            else
165
 
                break;
166
 
        }
 
201
                        }
 
202
                        else
 
203
                                break;
 
204
                }
167
205
 
168
206
                delete jobs;
169
 
    }
170
 
    return job;
 
207
        }
 
208
        return job;
171
209
}
172
210
 
173
211
void pgaJob::ShowStatistics(frmMain *form, ctlListView *statistics)
174
212
{
175
 
    wxString sql =
176
 
        wxT("SELECT jlgid")
177
 
                     wxT(", jlgstatus")
178
 
             wxT(", jlgstart")
179
 
             wxT(", jlgduration")
180
 
                         wxT(", (jlgstart + jlgduration) AS endtime")
181
 
             wxT("  FROM pgagent.pga_joblog\n")
182
 
             wxT(" WHERE jlgjobid = ") + NumToStr(GetRecId()) +
183
 
                         wxT(" ORDER BY jlgstart DESC") +
184
 
                         wxT(" LIMIT ") + NumToStr(settings->GetMaxRows());
185
 
 
186
 
    if (statistics)
187
 
    {
188
 
        wxLogInfo(wxT("Displaying statistics for job %s"), GetFullIdentifier().c_str());
189
 
 
190
 
        // Add the statistics view columns
 
213
        wxString sql =
 
214
            wxT("SELECT jlgid")
 
215
            wxT(", jlgstatus")
 
216
            wxT(", jlgstart")
 
217
            wxT(", jlgduration")
 
218
            wxT(", (jlgstart + jlgduration) AS endtime")
 
219
            wxT("  FROM pgagent.pga_joblog\n")
 
220
            wxT(" WHERE jlgjobid = ") + NumToStr(GetRecId()) +
 
221
            wxT(" ORDER BY jlgstart DESC") +
 
222
            wxT(" LIMIT ") + NumToStr(settings->GetMaxRows());
 
223
 
 
224
        if (statistics)
 
225
        {
 
226
                wxLogInfo(wxT("Displaying statistics for job %s"), GetFullIdentifier().c_str());
 
227
 
 
228
                // Add the statistics view columns
191
229
                statistics->ClearAll();
192
230
                statistics->AddColumn(_("Run"), 50);
193
 
        statistics->AddColumn(_("Status"), 60);
 
231
                statistics->AddColumn(_("Status"), 60);
194
232
                statistics->AddColumn(_("Start time"), 95);
195
233
                statistics->AddColumn(_("End time"), 95);
196
234
                statistics->AddColumn(_("Duration"), 70);
197
235
 
198
 
        pgSet *stats = GetConnection()->ExecuteSet(sql);
 
236
                pgSet *stats = GetConnection()->ExecuteSet(sql);
199
237
                wxString status;
200
238
                wxDateTime startTime;
201
239
                wxDateTime endTime;
202
240
 
203
 
        if (stats)
204
 
        {
205
 
            while (!stats->Eof())
206
 
            {
 
241
                if (stats)
 
242
                {
 
243
                        while (!stats->Eof())
 
244
                        {
207
245
                                if (stats->GetVal(1) == wxT("r"))
208
 
                    status = _("Running");
 
246
                                        status = _("Running");
209
247
                                else if (stats->GetVal(1) == wxT("s"))
210
 
                    status = _("Successful");
 
248
                                        status = _("Successful");
211
249
                                else if (stats->GetVal(1) == wxT("f"))
212
 
                    status = _("Failed");
 
250
                                        status = _("Failed");
213
251
                                else if (stats->GetVal(1) == wxT("d"))
214
 
                    status = _("Aborted");
 
252
                                        status = _("Aborted");
215
253
                                else if (stats->GetVal(1) == wxT("i"))
216
 
                    status = _("No steps");
 
254
                                        status = _("No steps");
217
255
                                else
218
 
                    status = _("Unknown");
 
256
                                        status = _("Unknown");
219
257
 
220
258
                                startTime.ParseDateTime(stats->GetVal(2));
221
259
                                endTime.ParseDateTime(stats->GetVal(4));
222
260
 
223
 
                long pos=statistics->AppendItem(stats->GetVal(0), status, startTime.Format());
 
261
                                long pos = statistics->AppendItem(stats->GetVal(0), status, startTime.Format());
224
262
                                if (stats->GetVal(4).Length() > 0)
225
 
                    statistics->SetItem(pos, 3, endTime.Format());
 
263
                                        statistics->SetItem(pos, 3, endTime.Format());
226
264
                                statistics->SetItem(pos, 4, stats->GetVal(3));
227
265
 
228
266
                                stats->MoveNext();
229
 
            }
230
 
            delete stats;
231
 
        }
232
 
    }
 
267
                        }
 
268
                        delete stats;
 
269
                }
 
270
        }
233
271
}
234
272
 
235
273
bool pgaJob::RunNow()
236
274
{
237
 
    if (!GetConnection()->ExecuteVoid(wxT("UPDATE pgagent.pga_job SET jobnextrun = now() WHERE jobid=") + NumToStr(GetRecId())))
238
 
        return false;
 
275
        if (!GetConnection()->ExecuteVoid(wxT("UPDATE pgagent.pga_job SET jobnextrun = now() WHERE jobid=") + NumToStr(GetRecId())))
 
276
                return false;
239
277
 
240
 
    return true;
 
278
        return true;
241
279
}
242
280
 
243
 
pgaJobObject::pgaJobObject(pgaJob *_job, pgaFactory &factory, const wxString& newName)
244
 
: pgServerObject(factory, newName)
 
281
pgaJobObject::pgaJobObject(pgaJob *_job, pgaFactory &factory, const wxString &newName)
 
282
        : pgServerObject(factory, newName)
245
283
{
246
 
    job=_job;
247
 
    server=job->GetServer();
 
284
        job = _job;
 
285
        server = job->GetServer();
248
286
}
249
287
 
250
288
 
251
289
pgaJobObjCollection::pgaJobObjCollection(pgaFactory *factory, pgaJob *_job)
252
 
: pgServerObjCollection(factory, _job->GetServer())
 
290
        : pgServerObjCollection(factory, _job->GetServer())
253
291
{
254
 
    job = _job;
 
292
        job = _job;
255
293
}
256
294
 
257
295
 
258
296
bool pgaJobObjCollection::CanCreate()
259
297
{
260
 
    return job->CanCreate();
 
298
        return job->CanCreate();
261
299
}
262
300
 
263
301
 
264
302
pgCollection *pgaJobObjFactory::CreateCollection(pgObject *obj)
265
303
{
266
 
    return new pgaJobObjCollection(GetCollectionFactory(), (pgaJob*)obj);
267
 
}
268
 
 
269
 
 
270
 
 
271
 
#include "images/job.xpm"
272
 
#include "images/jobs.xpm"
273
 
#include "images/jobdisabled.xpm"
274
 
 
275
 
pgaJobFactory::pgaJobFactory() 
276
 
: pgServerObjFactory(__("pgAgent Job"), __("New Job"), __("Create a new Job."), job_xpm)
277
 
{
278
 
    metaType = PGM_JOB;
279
 
    disabledId = addIcon(jobdisabled_xpm);
 
304
        return new pgaJobObjCollection(GetCollectionFactory(), (pgaJob *)obj);
 
305
}
 
306
 
 
307
 
 
308
wxString pgaJobObjCollection::GetTranslatedMessage(int kindOfMessage) const
 
309
{
 
310
        wxString message = wxEmptyString;
 
311
 
 
312
        switch (kindOfMessage)
 
313
        {
 
314
                case RETRIEVINGDETAILS:
 
315
                        message = _("Retrieving details on pgAgent jobs");
 
316
                        break;
 
317
                case REFRESHINGDETAILS:
 
318
                        message = _("Refreshing pgAgent jobs");
 
319
                        break;
 
320
        }
 
321
 
 
322
        return message;
 
323
}
 
324
 
 
325
/////////////////////////////
 
326
 
 
327
 
 
328
#include "images/job.pngc"
 
329
#include "images/jobs.pngc"
 
330
#include "images/jobdisabled.pngc"
 
331
 
 
332
pgaJobFactory::pgaJobFactory()
 
333
        : pgServerObjFactory(__("pgAgent Job"), __("New Job"), __("Create a new Job."), job_png_img)
 
334
{
 
335
        metaType = PGM_JOB;
 
336
        disabledId = addIcon(jobdisabled_png_img);
280
337
}
281
338
 
282
339
 
283
340
pgaJobFactory jobFactory;
284
 
static pgaCollectionFactory cf(&jobFactory, __("Jobs"), jobs_xpm);
 
341
static pgaCollectionFactory cf(&jobFactory, __("Jobs"), jobs_png_img);
285
342
 
286
343
runNowFactory::runNowFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
287
344
{
288
 
    mnu->Append(id, _("&Run now"), _("Reschedule the job to run now."));
 
345
        mnu->Append(id, _("&Run now"), _("Reschedule the job to run now."));
289
346
}
290
347
 
291
348
 
292
349
wxWindow *runNowFactory::StartDialog(frmMain *form, pgObject *obj)
293
350
{
294
 
    if (!((pgaJob *)(obj))->RunNow())
295
 
        wxLogError(_("Failed to reschedule the job."));
296
 
 
297
 
    form->Refresh(obj);
298
 
 
299
 
    return 0;
 
351
        if (!((pgaJob *)(obj))->RunNow())
 
352
        {
 
353
                wxLogError(_("Failed to reschedule the job."));
 
354
        }
 
355
 
 
356
        form->Refresh(obj);
 
357
 
 
358
        return 0;
300
359
}
301
360
 
302
361
 
303
362
bool runNowFactory::CheckEnable(pgObject *obj)
304
363
{
305
 
    if (obj)
306
 
    {
307
 
        if (obj->GetMetaType() == PGM_JOB && !obj->IsCollection())
308
 
            return true;
309
 
    }
310
 
    return false;
 
364
        if (obj)
 
365
        {
 
366
                if (obj->GetMetaType() == PGM_JOB && !obj->IsCollection())
 
367
                        return true;
 
368
        }
 
369
        return false;
311
370
}