File: pgaJob.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (397 lines) | stat: -rw-r--r-- 10,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgaJob.h - PostgreSQL Agent Job
//
//////////////////////////////////////////////////////////////////////////

// wxWindows headers
#include <wx/wx.h>

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "frm/frmMain.h"
#include "schema/pgObject.h"
#include "schema/pgCollection.h"
#include "schema/pgDatabase.h"
#include "agent/pgaJob.h"
#include "agent/pgaStep.h"
#include "agent/pgaSchedule.h"

pgaJob::pgaJob(const wxString &newName)
	: pgServerObject(jobFactory, newName)
{
}

wxString pgaJob::GetTranslatedMessage(int kindOfMessage) const
{
	wxString message = wxEmptyString;

	switch (kindOfMessage)
	{
		case RETRIEVINGDETAILS:
			message = _("Retrieving details on pgAgent job");
			break;
		case REFRESHINGDETAILS:
			message = _("Refreshing pgAgent job");
			break;
		case PROPERTIESREPORT:
			message = _("pgAgent job properties report");
			break;
		case PROPERTIES:
			message = _("pgAgent job properties");
			break;
		case DDLREPORT:
			message = _("pgAgent job DDL report");
			break;
		case DEPENDENCIESREPORT:
			message = _("pgAgent job dependencies report");
			break;
		case DEPENDENCIES:
			message = _("pgAgent job dependencies");
			break;
		case DEPENDENTSREPORT:
			message = _("pgAgent job dependents report");
			break;
		case DEPENDENTS:
			message = _("pgAgent job dependents");
			break;
		case DROPEXCLUDINGDEPS:
			message = wxString::Format(_("Are you sure you wish to drop job \"%s\"?"),
			                           GetFullIdentifier().c_str());
			break;
		case DROPTITLE:
			message = _("Drop job?");
			break;
	}

	if (!message.IsEmpty() && !(kindOfMessage == DROPEXCLUDINGDEPS || kindOfMessage == DROPTITLE))
		message += wxT(" - ") + GetName();

	return message;
}

int pgaJob::GetIconId()
{
	if (GetEnabled())
		return jobFactory.GetIconId();
	else
		return jobFactory.GetDisabledId();
}


wxMenu *pgaJob::GetNewMenu()
{
	wxMenu *menu = pgObject::GetNewMenu();
	if (1) // check priv.
	{
		stepFactory.AppendMenu(menu);
		scheduleFactory.AppendMenu(menu);
	}
	return menu;
}


bool pgaJob::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
	return GetConnection()->ExecuteVoid(wxT("DELETE FROM pgagent.pga_job WHERE jobid=") + NumToStr(GetRecId()));
}


void pgaJob::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
	if (!expandedKids)
	{
		expandedKids = true;

		browser->RemoveDummyChild(this);

		// Log
		wxLogInfo(wxT("Adding child objects to Job."));

		browser->AppendCollection(this, scheduleFactory);
		browser->AppendCollection(this, stepFactory);
	}

	if (properties)
	{
		CreateListColumns(properties);

		properties->AppendItem(_("Name"), GetName());
		properties->AppendItem(_("ID"), GetRecId());
		properties->AppendYesNoItem(_("Enabled"), GetEnabled());
		properties->AppendItem(_("Host agent"), GetHostAgent());
		properties->AppendItem(_("Job class"), GetJobclass());
		properties->AppendItem(_("Created"), GetCreated());
		properties->AppendItem(_("Changed"), GetChanged());
		properties->AppendItem(_("Next run"), GetNextrun());
		properties->AppendItem(_("Last run"), GetLastrun());
		properties->AppendItem(_("Last result"), GetLastresult());
		if (!GetCurrentAgent().IsEmpty())
			properties->AppendItem(_("Running at"), GetCurrentAgent());
		else
			properties->AppendItem(_("Running at"), _("Not currently running"));

		properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
	}
}



pgObject *pgaJob::Refresh(ctlTree *browser, const wxTreeItemId item)
{
	pgObject *job = 0;

	pgObject *obj = browser->GetObject(browser->GetItemParent(item));
	if (obj && obj->IsCollection())
		job = jobFactory.CreateObjects((pgCollection *)obj, 0, wxT("\n   WHERE j.jobid=") + NumToStr(GetRecId()));

	return job;
}



pgObject *pgaJobFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
	pgaJob *job = 0;

	pgSet *jobs = collection->GetConnection()->ExecuteSet(
	                  wxT("SELECT j.*, cl.*, ag.*, sub.jlgstatus AS joblastresult ")
	                  wxT("  FROM pgagent.pga_job j JOIN")
	                  wxT("  pgagent.pga_jobclass cl ON cl.jclid=jobjclid LEFT OUTER JOIN")
	                  wxT("  pgagent.pga_jobagent ag ON ag.jagpid=jobagentid LEFT OUTER JOIN")
	                  wxT("  (SELECT DISTINCT ON (jlgjobid) jlgstatus, jlgjobid")
	                  wxT("   FROM pgagent.pga_joblog")
	                  wxT("  ORDER BY jlgjobid, jlgid desc) sub ON sub.jlgjobid = j.jobid ")
	                  + restriction +
	                  wxT("ORDER BY jobname;"));

	if (jobs)
	{
		while (!jobs->Eof())
		{
			wxString status;
			if (jobs->GetVal(wxT("joblastresult")) == wxT("r"))
				status = _("Running");
			else if (jobs->GetVal(wxT("joblastresult")) == wxT("s"))
				status = _("Successful");
			else if (jobs->GetVal(wxT("joblastresult")) == wxT("f"))
				status = _("Failed");
			else if (jobs->GetVal(wxT("joblastresult")) == wxT("d"))
				status = _("Aborted");
			else if (jobs->GetVal(wxT("joblastresult")) == wxT("i"))
				status = _("No steps");
			else
				status = _("Unknown");

			job = new pgaJob(jobs->GetVal(wxT("jobname")));
			job->iSetServer(collection->GetServer());
			job->iSetRecId(jobs->GetLong(wxT("jobid")));
			job->iSetComment(jobs->GetVal(wxT("jobdesc")));

			job->iSetEnabled(jobs->GetBool(wxT("jobenabled")));
			job->iSetJobclass(jobs->GetVal(wxT("jclname")));
			job->iSetHostAgent(jobs->GetVal(wxT("jobhostagent")));
			job->iSetCreated(jobs->GetDateTime(wxT("jobcreated")));
			job->iSetChanged(jobs->GetDateTime(wxT("jobchanged")));
			job->iSetNextrun(jobs->GetDateTime(wxT("jobnextrun")));
			job->iSetLastrun(jobs->GetDateTime(wxT("joblastrun")));
			job->iSetLastresult(status);
			job->iSetCurrentAgent(jobs->GetVal(wxT("jagstation")));

			if (browser)
			{
				browser->AppendObject(collection, job);
				jobs->MoveNext();
			}
			else
				break;
		}

		delete jobs;
	}
	return job;
}

void pgaJob::ShowStatistics(frmMain *form, ctlListView *statistics)
{
	wxString sql =
	    wxT("SELECT jlgid")
	    wxT(", jlgstatus")
	    wxT(", jlgstart")
	    wxT(", jlgduration")
	    wxT(", (jlgstart + jlgduration) AS endtime")
	    wxT("  FROM pgagent.pga_joblog\n")
	    wxT(" WHERE jlgjobid = ") + NumToStr(GetRecId()) +
	    wxT(" ORDER BY jlgstart DESC") +
	    wxT(" LIMIT ") + NumToStr(settings->GetMaxRows());

	if (statistics)
	{
		wxLogInfo(wxT("Displaying statistics for job %s"), GetFullIdentifier().c_str());

		// Add the statistics view columns
		statistics->ClearAll();
		statistics->AddColumn(_("Run"), 50);
		statistics->AddColumn(_("Status"), 60);
		statistics->AddColumn(_("Start time"), 95);
		statistics->AddColumn(_("End time"), 95);
		statistics->AddColumn(_("Duration"), 70);

		pgSet *stats = GetConnection()->ExecuteSet(sql);
		wxString status;
		wxDateTime startTime;
		wxDateTime endTime;

		if (stats)
		{
			while (!stats->Eof())
			{
				if (stats->GetVal(1) == wxT("r"))
					status = _("Running");
				else if (stats->GetVal(1) == wxT("s"))
					status = _("Successful");
				else if (stats->GetVal(1) == wxT("f"))
					status = _("Failed");
				else if (stats->GetVal(1) == wxT("d"))
					status = _("Aborted");
				else if (stats->GetVal(1) == wxT("i"))
					status = _("No steps");
				else
					status = _("Unknown");

				startTime.ParseDateTime(stats->GetVal(2));
				endTime.ParseDateTime(stats->GetVal(4));

				long pos = statistics->AppendItem(stats->GetVal(0), status, startTime.Format());
				if (stats->GetVal(4).Length() > 0)
					statistics->SetItem(pos, 3, endTime.Format());
				statistics->SetItem(pos, 4, stats->GetVal(3));

				stats->MoveNext();
			}
			delete stats;
		}
	}
}

bool pgaJob::RunNow()
{
	if (!GetConnection()->ExecuteVoid(wxT("UPDATE pgagent.pga_job SET jobnextrun = now() WHERE jobid=") + NumToStr(GetRecId())))
		return false;

	return true;
}


pgaJobCollection::pgaJobCollection(pgaFactory *factory, pgServer *sv)
	: pgServerObjCollection(factory, sv)
{
}


wxString pgaJobCollection::GetTranslatedMessage(int kindOfMessage) const
{
	wxString message = wxEmptyString;

	switch (kindOfMessage)
	{
		case RETRIEVINGDETAILS:
			message = _("Retrieving details on pgAgent jobs");
			break;
		case REFRESHINGDETAILS:
			message = _("Refreshing pgAgent jobs");
			break;
		case OBJECTSLISTREPORT:
			message = _("pgAgent jobs list report");
			break;
	}

	return message;
}


pgaJobObject::pgaJobObject(pgaJob *_job, pgaFactory &factory, const wxString &newName)
	: pgServerObject(factory, newName)
{
	job = _job;
	server = job->GetServer();
}


pgaJobObjCollection::pgaJobObjCollection(pgaFactory *factory, pgaJob *_job)
	: pgServerObjCollection(factory, _job->GetServer())
{
	job = _job;
}


bool pgaJobObjCollection::CanCreate()
{
	return job->CanCreate();
}


pgCollection *pgaJobObjFactory::CreateCollection(pgObject *obj)
{
	return new pgaJobObjCollection(GetCollectionFactory(), (pgaJob *)obj);
}


/////////////////////////////


#include "images/job.pngc"
#include "images/jobs.pngc"
#include "images/jobdisabled.pngc"

pgaJobFactory::pgaJobFactory()
	: pgServerObjFactory(__("pgAgent Job"), __("New Job"), __("Create a new Job."), job_png_img)
{
	metaType = PGM_JOB;
	disabledId = addIcon(jobdisabled_png_img);
}


pgCollection *pgaJobFactory::CreateCollection(pgObject *obj)
{
	return new pgaJobCollection(GetCollectionFactory(), (pgServer *)obj);
}


pgaJobFactory jobFactory;
static pgaCollectionFactory cf(&jobFactory, __("Jobs"), jobs_png_img);

runNowFactory::runNowFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
	mnu->Append(id, _("&Run now"), _("Reschedule the job to run now."));
}


wxWindow *runNowFactory::StartDialog(frmMain *form, pgObject *obj)
{
	if (!((pgaJob *)(obj))->RunNow())
	{
		wxLogError(_("Failed to reschedule the job."));
	}

	form->Refresh(obj);

	return 0;
}


bool runNowFactory::CheckEnable(pgObject *obj)
{
	if (obj)
	{
		if (obj->GetMetaType() == PGM_JOB && !obj->IsCollection())
			return true;
	}
	return false;
}