负责的项目有时候会被项目经理找来催,原来又没有及时的去搜索CQ。。。
于是这两天自己实现了一下能自动执行CQ查询的脚本,同时还能降查询结果发送到我的邮箱。分享一下吧:
简单说明一下:
1)其实是两个比较独立的的功能拼凑到一块实现的, 一个功能是CQ的登陆和查询, 看函数logonDMS()和DoQuery(); 一个功能是发送email,看函数sendmail()。
2)是用CQ的perl接口实现的,执行的时候不能用原生的perl执行, 必须用CQ安装目录下的CQPerl.exe 来执行。
好了,直接上code吧, 为了“保密”, 所有个人信息的地方都更换了, 理解的吧哈。
###basic version
use CQPerlExt;
use Net::SMTP;
use LWP::Simple qw(get);
use File::Path;
use Net::SMTP;
our $sessionObj;
our $querydef;
our $dms_summary;
our $dms_query1 = "Personal Queries/projectA"; #替换了我的实际的query文件名称和位置哈
our $dms_query2 = "Personal Queries/projectB"; #替换了我的实际的query文件名称和位置哈
#usage: CQPerl.exe this_script.pl
#precondition: define queries such as $dms_query1, $dms_query2, then call them by DoQuery($dms_query1); and DoQuery($dms_query2);
sub logonDMS{
print "DMS login begin.\n";
$sessionObj = CQSession::Build();
$sessionObj->UserLogon( "name", "pass", ‘database1‘, ""); #替换了实际的用户名(name)密码(pass)和实际的数据库名称(database1),请根据环境自行更正
print "DMS login successfully.\n";
}
sub DoQuery{ #arg = dms_query
my ($query) = @_;
print "do query begin for $query \n";
my $workspace = $sessionObj->GetWorkSpace();
my $querydef = $workspace->GetQueryDef($query);
my $resultset = $sessionObj->BuildResultSet($querydef);
$resultset->EnableRecordCount();
$resultset->Execute();
$num_rows = $resultset->GetRecordCount();
print "number of DMSs: $num_rows \n";
if ($num_rows <=0) #这里是返回了,因为没有查询到任何结果
{
$dms_summary .= ‘<br/><br/><H1 style="color:red">‘.‘You have no DMS need handle under query:<br/>‘.$query.‘<br/><H1/>‘;
return;
}
$status = $resultset->MoveNext();
$dms_summary .= ‘<table border="1">‘;
while($status == $CQPerlExt::CQ_SUCCESS){
print $resultset->GetColumnValue(2); print "\t";# ‘1‘ often means internal id, so identify your desired columns from ‘2‘
$dms_summary .= ‘<tr><td>‘.$resultset->GetColumnValue(2).‘<td/>‘;
print $resultset->GetColumnValue(4); print "\n";
$dms_summary .= ‘<td>‘.$resultset->GetColumnValue(4).‘<td/></tr>‘;
$status = $resultset->MoveNext();
}
print "do query finish.\n";
$dms_summary .= ‘</table>‘;
print $dms_summary;
}
sub sendmail{
my $smtp = Net::SMTP->new(‘smtpserverxx.company.com‘) or die "Cannot connect to server"; #哈哈, 替换了我的smtp的服务器
$smtp->mail(‘[email protected]‘); #替换了我的邮箱地址哦
$smtp->to(‘[email protected]‘);
$smtp->data();
$smtp->datasend("To: myname\@xxx.com\n"); #测试用,还是发到自己邮箱
$smtp->datasend("From: myname\@xxx.com\n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: CQ test\n");
$smtp->datasend("\n");
$smtp->datasend($dms_summary);
$smtp->dataend();
$smtp->quit;
}
##################body ######
logonDMS();
DoQuery($dms_query1);
DoQuery($dms_query2);
CQSession::Unbuild($sessionObj);
sendmail();
[email protected] at 20150121 night