1+ <?php
2+ /**
3+ * Copyright 2018 OpenStack Foundation
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ **/
14+
15+ /**
16+ * Class SangriaAucMetricsRestfullApi
17+ */
18+ final class SangriaAucMetricsRestfullApi extends AbstractRestfulJsonApi
19+ {
20+
21+ /**
22+ * @var IAUCMetricRepository
23+ */
24+ private $ repository ;
25+
26+ /**
27+ * SangriaAucMetricsRestfullApi constructor.
28+ * @param IAUCMetricRepository $repository
29+ */
30+ public function __construct (IAUCMetricRepository $ repository )
31+ {
32+ parent ::__construct ();
33+ $ this ->repository = $ repository ;
34+ }
35+
36+ /**
37+ * @return bool
38+ */
39+ public function authorize (){
40+ //check permissions
41+ if (!Permission::check ("SANGRIA_ACCESS " ))
42+ return false ;
43+ return true ;
44+ }
45+
46+ static $ url_handlers = [
47+ ' user-miss-matches ' => 'handleUserMissMatches ' ,
48+ 'GET ' => 'getMetrics ' ,
49+ 'GET csv ' => 'exportMetricToCSV ' ,
50+ ];
51+
52+ static $ allowed_actions = [
53+ 'handleUserMissMatches ' ,
54+ 'getMetrics ' ,
55+ 'exportMetricToCSV ' ,
56+ ];
57+
58+ /**
59+ * @param SS_HTTPRequest $request
60+ * @return mixed|SS_HTTPResponse
61+ */
62+ function handleUserMissMatches (SS_HTTPRequest $ request ){
63+ $ api = SangriaAucMetricsUserMissMatchesRestfullApi::create ();
64+ return $ api ->handleRequest ($ request , DataModel::inst ());
65+ }
66+
67+ /**
68+ * @param SS_HTTPRequest $request
69+ * @return SS_HTTPResponse
70+ */
71+ function getMetrics (SS_HTTPRequest $ request ){
72+ try
73+ {
74+ $ query_string = $ request ->getVars ();
75+ $ page = intval ((isset ($ query_string ['page ' ])) ? Convert::raw2sql ($ query_string ['page ' ]) : 0 );
76+ $ page_size = intval ((isset ($ query_string ['page_size ' ])) ? Convert::raw2sql ($ query_string ['page_size ' ]) : 25 );
77+
78+ // zero mean showing all ...
79+ if ($ page_size == 0 ) $ page_size = PHP_INT_MAX ;
80+
81+ $ order = (isset ($ query_string ['order ' ])) ? Convert::raw2sql ($ query_string ['order ' ]) : '' ;
82+ $ filter = (isset ($ query_string ['filter ' ])) ? Convert::raw2sql ($ query_string ['filter ' ]) : '' ;
83+ $ search_term = (isset ($ query_string ['search_term ' ])) ? Convert::raw2sql ($ query_string ['search_term ' ]) : '' ;
84+ $ type = (isset ($ query_string ['type ' ])) ? Convert::raw2sql ($ query_string ['type ' ]) : '' ;
85+
86+ list ($ list , $ count ) = $ this ->repository ->getAllByPage
87+ (
88+ $ page ,
89+ $ page_size ,
90+ $ order ,
91+ $ search_term ,
92+ $ type ,
93+ $ filter
94+ );
95+
96+ $ items = [];
97+ foreach ($ list as $ item ){
98+ $ expires_epoch = new DateTime ($ item ->Expires );
99+ $ created_epoch = new DateTime ($ item ->Created );
100+ $ items [] =
101+ [
102+ 'id ' => intval ($ item ->ID ),
103+ 'identifier ' => $ item ->Identifier ,
104+ 'value ' => $ item ->Value ,
105+ 'value_description ' => $ item ->ValueDescription ,
106+ 'created ' => $ created_epoch ->format ("Y-m-d h:i:s A " ),
107+ 'expires ' => $ expires_epoch ->format ("Y-m-d h:i:s A " ),
108+ 'member_id ' => intval ($ item ->FoundationMember ()->ID ),
109+ 'member_full_name ' => $ item ->FoundationMember ()->FullName ,
110+ 'member_email ' => $ item ->FoundationMember ()->Email ,
111+ ];
112+ }
113+
114+ return $ this ->ok (array ('items ' => $ items , 'count ' => $ count ));
115+ }
116+ catch (NotFoundEntityException $ ex2 )
117+ {
118+ SS_Log::log ($ ex2 ->getMessage (), SS_Log::WARN );
119+ return $ this ->notFound ($ ex2 ->getMessage ());
120+ }
121+ catch (Exception $ ex )
122+ {
123+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
124+ return $ this ->serverError ();
125+ }
126+ }
127+
128+ /**
129+ * @param SS_HTTPRequest $request
130+ */
131+ function exportMetricToCSV (SS_HTTPRequest $ request ){
132+
133+ $ query_string = $ request ->getVars ();
134+ $ order = (isset ($ query_string ['order ' ])) ? Convert::raw2sql ($ query_string ['order ' ]) : '' ;
135+ $ search_term = (isset ($ query_string ['search_term ' ])) ? Convert::raw2sql ($ query_string ['search_term ' ]) : '' ;
136+ $ type = (isset ($ query_string ['type ' ])) ? Convert::raw2sql ($ query_string ['type ' ]) : '' ;
137+ $ filter = (isset ($ query_string ['filter ' ])) ? Convert::raw2sql ($ query_string ['filter ' ]) : '' ;
138+
139+ list ($ list , $ count ) = $ this ->repository ->getAllByPage
140+ (
141+ 1 ,
142+ PHP_INT_MAX ,
143+ $ order ,
144+ $ search_term ,
145+ $ type ,
146+ $ filter
147+ );
148+
149+ $ filename = "AUCMetrics " . date ('Ymd ' ) . ".csv " ;
150+
151+ $ items = [];
152+ foreach ($ list as $ item ){
153+ $ expires_epoch = new DateTime ($ item ->Expires );
154+ $ created_epoch = new DateTime ($ item ->Created );
155+ $ items [] =
156+ [
157+ 'id ' => intval ($ item ->ID ),
158+ 'identifier ' => $ item ->Identifier ,
159+ 'value ' => $ item ->Value ,
160+ 'value_description ' => $ item ->ValueDescription ,
161+ 'created ' => $ created_epoch ->format ("Y-m-d h:i:s A " ),
162+ 'expires ' => $ expires_epoch ->format ("Y-m-d h:i:s A " ),
163+ 'member_id ' => intval ($ item ->FoundationMember ()->ID ),
164+ 'member_full_name ' => $ item ->FoundationMember ()->FullName ,
165+ 'member_email ' => $ item ->FoundationMember ()->Email ,
166+ ];
167+ }
168+
169+ return CSVExporter::getInstance ()->export ($ filename , $ items , ', ' );
170+ }
171+ }
0 commit comments