Skip to content

Commit 236df67

Browse files
authored
Merge pull request #29 from aliyun/power_sql
project sql api support power sql
2 parents 1e58c30 + b13739b commit 236df67

File tree

5 files changed

+332
-4
lines changed

5 files changed

+332
-4
lines changed

Aliyun/Log/Client.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ public function getProjectLogsJson(Aliyun_Log_Models_GetProjectLogsRequest $requ
660660
$params = array ();
661661
if ($request->getQuery () !== null)
662662
$params ['query'] = $request->getQuery ();
663+
if ($request -> getPowerSql() != null)
664+
$params ["powerSql"] = $request -> getPowerSql()? 'true' : 'false';
663665
$project = $request->getProject () !== null ? $request->getProject () : '';
664666
$resource = "/logs";
665667
list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
@@ -709,7 +711,43 @@ public function executeLogStoreSql(Aliyun_Log_Models_LogStoreSqlRequest $request
709711
$resp = $this->parseToJson ( $resp, $requestId );
710712
return new Aliyun_Log_Models_LogStoreSqlResponse($resp, $header);
711713
}
712-
714+
/**
715+
* exeucte project sql.
716+
* Unsuccessful opertaion will cause an Aliyun_Log_Exception.
717+
*
718+
* @param Aliyun_Log_Models_ProjectSqlRequest $request the GetLogs request parameters class.
719+
* @throws Aliyun_Log_Exception
720+
* @return array(json body, http header)
721+
*/
722+
public function executeProjectSqlJson(Aliyun_Log_Models_ProjectSqlRequest $request) {
723+
$headers = array ();
724+
$params = array ();
725+
if ($request->getQuery () !== null)
726+
$params ['query'] = $request->getQuery ();
727+
if ($request -> getPowerSql() != null)
728+
$params ["powerSql"] = $request -> getPowerSql()? 'true' : 'false';
729+
$project = $request->getProject () !== null ? $request->getProject () : '';
730+
$resource = "/logs";
731+
list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
732+
$requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
733+
$resp = $this->parseToJson ( $resp, $requestId );
734+
return array($resp, $header);
735+
//return new Aliyun_Log_Models_GetLogsResponse ( $resp, $header );
736+
}
737+
/**
738+
* Get logs from Log service.
739+
* Unsuccessful opertaion will cause an Aliyun_Log_Exception.
740+
*
741+
* @param Aliyun_Log_Models_GetProjectLogsRequest $request the GetLogs request parameters class.
742+
* @throws Aliyun_Log_Exception
743+
* @return Aliyun_Log_Models_GetLogsResponse
744+
*/
745+
public function executeProjectSql(Aliyun_Log_Models_ProjectSqlRequest $request) {
746+
$ret = $this->executeProjectSqlJson($request);
747+
$resp = $ret[0];
748+
$header = $ret[1];
749+
return new Aliyun_Log_Models_ProjectSqlResponse ( $resp, $header );
750+
}
713751
/**
714752
* create sql instance api
715753
* Unsuccessful opertaion will cause an Aliyun_Log_Exception.

Aliyun/Log/Models/Request/GetProjectLogsRequest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ class Aliyun_Log_Models_GetProjectLogsRequest extends Aliyun_Log_Models_Request
1717
* @var string user defined query
1818
*/
1919
private $query;
20-
20+
/**
21+
* @var bool if power sql is true, then the query will be run with powered instance, which can handle large amountof data
22+
*/
23+
private $powerSql;
24+
2125

2226
/**
2327
* Aliyun_Log_Models_GetProjectLogsRequest Constructor
2428
* @param string $query
2529
* user defined query
2630
*/
27-
public function __construct($project = null, $query = null ) {
31+
public function __construct($project = null, $query = null ,$powerSql= null) {
2832
parent::__construct ( $project );
2933

3034
$this->query = $query;
35+
$this->powerSql = $powerSql;
3136
}
3237

3338

@@ -40,6 +45,26 @@ public function __construct($project = null, $query = null ) {
4045
public function getQuery() {
4146
return $this->query;
4247
}
43-
48+
49+
/**
50+
* Get request powerSql flag
51+
*
52+
* @reutnr bool powerSql flag
53+
*/
54+
public function getPowerSql() {
55+
return $this -> powerSql;
56+
}
57+
58+
/**
59+
* Set request powerSql flag
60+
*
61+
* @param bool $powerSql
62+
* powerSql flag
63+
*
64+
*/
65+
public function setPowerSql($powerSql)
66+
{
67+
$this -> powerSql = $powerSql;
68+
}
4469

4570
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright (C) Alibaba Cloud Computing
4+
* All rights reserved
5+
*/
6+
7+
require_once realpath(dirname(__FILE__) . '/Request.php');
8+
9+
/**
10+
* The request used to execute power sql by a query from log service.
11+
*
12+
* @author log service dev
13+
*/
14+
class Aliyun_Log_Models_ProjectSqlRequest extends Aliyun_Log_Models_Request {
15+
16+
/**
17+
* @var string user defined query
18+
*/
19+
private $query;
20+
21+
/**
22+
* @var bool if power sql is true, then the query will be run with powered instance, which can handle large amountof data
23+
*/
24+
private $powerSql;
25+
26+
/**
27+
* Aliyun_Log_Models_ProjectSqlRequest Constructor
28+
* @param string $query
29+
* user defined query
30+
*/
31+
public function __construct($project = null, $query = null,$powerSql= null) {
32+
parent::__construct ( $project );
33+
34+
$this->query = $query;
35+
$this->powerSql = $powerSql;
36+
}
37+
38+
39+
40+
/**
41+
* Get user defined query
42+
*
43+
* @return string user defined query
44+
*/
45+
public function getQuery() {
46+
return $this->query;
47+
}
48+
49+
50+
/**
51+
* Get request powerSql flag
52+
*
53+
* @reutnr bool powerSql flag
54+
*/
55+
public function getPowerSql() {
56+
return $this -> powerSql;
57+
}
58+
59+
/**
60+
* Set request powerSql flag
61+
*
62+
* @param bool $powerSql
63+
* powerSql flag
64+
*
65+
*/
66+
public function setPowerSql($powerSql)
67+
{
68+
$this -> powerSql = $powerSql;
69+
}
70+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright (C) Alibaba Cloud Computing
4+
* All rights reserved
5+
*/
6+
7+
require_once realpath(dirname(__FILE__) . '/Response.php');
8+
require_once realpath(dirname(__FILE__) . '/QueriedLog.php');
9+
10+
/**
11+
* The response of the execute sql API from log service.
12+
*
13+
* @author log service dev
14+
*/
15+
class Aliyun_Log_Models_ProjectSqlResponse extends Aliyun_Log_Models_Response {
16+
17+
/**
18+
* @var integer log number
19+
*/
20+
private $count;
21+
22+
/**
23+
* @var string logs query status(Complete or InComplete)
24+
*/
25+
private $progress;
26+
27+
/**
28+
* @var array Aliyun_Log_Models_QueriedLog array, all log data
29+
*/
30+
private $logs;
31+
32+
/**
33+
* @var rows proccesed in this request
34+
*/
35+
private $processedRows;
36+
37+
/**
38+
* @var execution latency in milliseconds
39+
*/
40+
private $elapsedMilli;
41+
42+
/**
43+
* @var used cpu sec for this request
44+
*/
45+
private $cpuSec;
46+
47+
/**
48+
* @var used cpu core number for this request
49+
*/
50+
private $cpuCores;
51+
52+
53+
/**
54+
* Aliyun_Log_Models_GetLogsResponse constructor
55+
*
56+
* @param array $resp
57+
* GetLogs HTTP response body
58+
* @param array $header
59+
* GetLogs HTTP response header
60+
*/
61+
public function __construct($resp, $header) {
62+
parent::__construct ( $header );
63+
$this->count = $header['x-log-count'];
64+
$this->progress = $header ['x-log-progress'];
65+
$this->processedRows = $header['x-log-processed-rows'];
66+
$this->elapsedMilli = $header['x-log-elapsed-millisecond'];
67+
$this->cpuSec = $header['x-log-cpu-sec'];
68+
$this->cpuCores = $header['x-log-cpu-cores'];
69+
$this->logs = array ();
70+
foreach ( $resp as $data ) {
71+
$contents = $data;
72+
$time = $data ['__time__'];
73+
$source = $data ['__source__'];
74+
unset ( $contents ['__time__'] );
75+
unset ( $contents ['__source__'] );
76+
$this->logs [] = new Aliyun_Log_Models_QueriedLog ( $time, $source, $contents );
77+
}
78+
}
79+
80+
/**
81+
* Get log number from the response
82+
*
83+
* @return integer log number
84+
*/
85+
public function getCount() {
86+
return $this->count;
87+
}
88+
89+
/**
90+
* Check if the get logs query is completed
91+
*
92+
* @return bool true if this logs query is completed
93+
*/
94+
public function isCompleted() {
95+
return $this->progress == 'Complete';
96+
}
97+
98+
/**
99+
* Get all logs from the response
100+
*
101+
* @return array Aliyun_Log_Models_QueriedLog array, all log data
102+
*/
103+
public function getLogs() {
104+
return $this->logs;
105+
}
106+
107+
/**
108+
* get proccesedRows
109+
*/
110+
public function getProcessedRows()
111+
{
112+
return $this ->processedRows;
113+
}
114+
115+
/**
116+
* get elapsedMilli
117+
*/
118+
public function getElapsedMilli()
119+
{
120+
return $this -> elapsedMilli;
121+
}
122+
123+
/**
124+
* get cpuSec
125+
*/
126+
public function getCpuSec()
127+
{
128+
return $this->cpuSec;
129+
}
130+
131+
/**
132+
* get cpuCores
133+
*/
134+
public function getCpuCores()
135+
{
136+
return $this-> cpuCores;
137+
}
138+
}
139+

sample/sample.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,60 @@ function getLogsWithPowerSql(Aliyun_Log_Client $client, $project, $logstore) {
107107
logVarDump($ex);
108108
}
109109
}
110+
function getProjectLogsWithPowerSql(Aliyun_Log_Client $client, $project) {
111+
$query = " select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())";
112+
$request = new Aliyun_Log_Models_GetProjectLogsRequest($project, $query, True);
113+
114+
try {
115+
$response = $client->getProjectLogs($request);
116+
#$response = $client->getProjectLogs($request);
117+
foreach($response -> getLogs() as $log)
118+
{
119+
print $log -> getTime()."\t";
120+
foreach($log -> getContents() as $key => $value){
121+
print $key.":".$value."\t";
122+
}
123+
print "\n";
124+
}
125+
print "proccesedRows:".$response -> getProcessedRows()."\n";
126+
print "elapsedMilli:".$response -> getElapsedMilli()."\n";
127+
print "cpuSec:".$response -> getCpuSec()."\n";
128+
print "cpuCores:".$response -> getCpuCores()."\n";
129+
print "requestId:".$response ->getRequestId()."\n";
130+
131+
} catch (Aliyun_Log_Exception $ex) {
132+
logVarDump($ex);
133+
} catch (Exception $ex) {
134+
logVarDump($ex);
135+
}
136+
}
137+
function executeProjectSqlWithPowerSql(Aliyun_Log_Client $client, $project) {
138+
$query = " select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())";
139+
$request = new Aliyun_Log_Models_ProjectSqlRequest($project, $query, True);
140+
141+
try {
142+
$response = $client->executeProjectSql($request);
143+
#$response = $client->getProjectLogs($request);
144+
foreach($response -> getLogs() as $log)
145+
{
146+
print $log -> getTime()."\t";
147+
foreach($log -> getContents() as $key => $value){
148+
print $key.":".$value."\t";
149+
}
150+
print "\n";
151+
}
152+
print "proccesedRows:".$response -> getProcessedRows()."\n";
153+
print "elapsedMilli:".$response -> getElapsedMilli()."\n";
154+
print "cpuSec:".$response -> getCpuSec()."\n";
155+
print "cpuCores:".$response -> getCpuCores()."\n";
156+
print "requestId:".$response ->getRequestId()."\n";
157+
158+
} catch (Aliyun_Log_Exception $ex) {
159+
logVarDump($ex);
160+
} catch (Exception $ex) {
161+
logVarDump($ex);
162+
}
163+
}
110164
function crudSqlInstance(Aliyun_Log_Client $client,$project){
111165
$res = $client -> createSqlInstance($project,1000);
112166
logVarDump($res);
@@ -285,5 +339,7 @@ function logVarDump($expression){
285339
#listTopics($client, $project, $logstore);
286340
#getHistograms($client, $project, $logstore);
287341
#getLogs($client, $project, $logstore);
342+
executeProjectSqlWithPowerSql($client,$project);
343+
getProjectLogsWithPowerSql($client,$project);
288344
getLogsWithPowerSql($client, $project, $logstore);
289345
crudSqlInstance($client,$project);

0 commit comments

Comments
 (0)