Skip to content

Commit d5102ae

Browse files
committed
Merge branch 'log-pagination' into 'activity-logging'
polydep-17: Log pagination See merge request !8
2 parents 20bc756 + b8ad7ea commit d5102ae

File tree

9 files changed

+267
-22
lines changed

9 files changed

+267
-22
lines changed

PolyDeploy/Clients/Core/src/js/services/EventLogDataService.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55

66
// GET
77
// Browse Events.
8-
function browseEvents(pageIndex, pageSize, eventType, severity) {
8+
function browseEvents(options) {
99

1010
var queryParameters = {};
1111

12-
if (pageIndex) {
13-
queryParameters['pageIndex'] = pageIndex;
14-
}
12+
if (options) {
13+
if (options.pageIndex) {
14+
queryParameters['pageIndex'] = options.pageIndex;
15+
}
1516

16-
if (pageSize) {
17-
queryParameters['pageSize'] = pageSize;
18-
}
17+
if (options.pageSize) {
18+
queryParameters['pageSize'] = options.pageSize;
19+
}
1920

20-
if (eventType) {
21-
queryParameters['eventType'] = eventType;
22-
}
21+
if (options.eventType) {
22+
queryParameters['eventType'] = options.eventType;
23+
}
2324

24-
if (severity) {
25-
queryParameters['severity'] = severity;
25+
if (options.severity) {
26+
queryParameters['severity'] = options.severity;
27+
}
2628
}
2729

2830
var queryString = buildQueryString(queryParameters);
@@ -37,7 +39,7 @@
3739
}
3840

3941
// GET
40-
// Get event types..
42+
// Get event types.
4143
function getEventTypes() {
4244

4345
// Make request.

PolyDeploy/Clients/Manage/src/css/main.less

+38
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@
88

99
#cantarus-poly-deploy {
1010

11+
.can-pagination {
12+
position: relative;
13+
height: 20px;
1114

15+
.prev, .next {
16+
position: absolute;
17+
height: 100%;
18+
width: 20px;
19+
top: 0;
20+
text-align: center;
21+
line-height: 30px;
22+
cursor: pointer;
23+
}
1224

25+
.prev {
26+
left: 0;
27+
}
28+
29+
.next {
30+
right: 0;
31+
}
32+
33+
.pages {
34+
list-style: none;
35+
margin: 0 20px;
36+
text-align: center;
37+
38+
li {
39+
display: inline-block;
40+
line-height: 20px;
41+
padding: 0 5px;
42+
margin: 0 5px;
43+
cursor: pointer;
44+
45+
&.current {
46+
font-weight: bold;
47+
}
48+
}
49+
}
50+
}
1351
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,103 @@
11
module.exports = ['$scope', 'EventLogDataService',
22
function ($scope, EventLogDataService) {
33

4-
// Load events.
5-
loadEvents();
4+
// Defaults.
5+
var options = {
6+
pageIndex: 0,
7+
pageSize: 10,
8+
eventType: undefined,
9+
severity: undefined
10+
};
11+
12+
// Initialise scope variables.
13+
$scope.currentPage = 1;
14+
$scope.pageCount = 1;
15+
$scope.eventLogs = [];
16+
17+
// Add functions to scope.
18+
$scope.hasNextPage = hasNextPage;
19+
$scope.hasPrevPage = hasPrevPage;
20+
$scope.nextPage = nextPage;
21+
$scope.prevPage = prevPage;
22+
$scope.loadPage = loadPage;
23+
$scope.getPages = getPages;
24+
25+
// Fetch first page.
26+
fetchEvents(options);
627

728
// Fetch events.
8-
function loadEvents() {
29+
function fetchEvents(opts) {
30+
31+
// Load events based on options.
32+
EventLogDataService.browseEvents(opts).then(
33+
function (response) {
934

10-
// No pagination yet.
11-
EventLogDataService.browseEvents(0, 9999).then(
12-
function (eventLogs) {
35+
// Place the events on the scope.
36+
$scope.eventLogs = response.Data;
1337

14-
// Pop them on the scope.
15-
$scope.eventLogs = eventLogs;
38+
// Place page count on scope.
39+
$scope.pageCount = response.Pagination.Pages;
1640

1741
});
1842
}
1943

44+
// Is there a next page?
45+
function hasNextPage() {
46+
return $scope.currentPage < $scope.pageCount;
47+
}
48+
49+
// Is there a previous page?
50+
function hasPrevPage() {
51+
return $scope.currentPage > 1;
52+
}
53+
54+
// Load the next page.
55+
function nextPage() {
56+
57+
// Is there a next page?
58+
if (hasNextPage()) {
59+
60+
// Yes, increment and fetch events.
61+
$scope.currentPage++;
62+
63+
options.pageIndex = $scope.currentPage - 1;
64+
65+
fetchEvents(options);
66+
}
67+
}
68+
69+
// Load the next page.
70+
function prevPage() {
71+
72+
// Is there a previous page?
73+
if (hasPrevPage()) {
74+
75+
// Yes, decrement and fetch events.
76+
$scope.currentPage--;
77+
78+
options.pageIndex = $scope.currentPage - 1;
79+
80+
fetchEvents(options);
81+
}
82+
}
83+
84+
// Load specified page.
85+
function loadPage(pageNo) {
86+
87+
// Page exists?
88+
if (pageNo > 0 && pageNo <= $scope.pageCount) {
89+
90+
$scope.currentPage = pageNo;
91+
92+
options.pageIndex = pageNo - 1;
93+
94+
fetchEvents(options);
95+
}
96+
}
97+
98+
// Get array for pagination.
99+
function getPages(number) {
100+
return new Array(number);
101+
}
102+
20103
}];

PolyDeploy/Clients/Manage/src/js/templates/events.html

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ <h3 class="panel-title">Event Logs</h3>
2626
</tr>
2727
</tbody>
2828
</table>
29+
30+
<!-- Pagination -->
31+
<div class="can-pagination">
32+
<span class="prev"
33+
ng-click="prevPage()"
34+
ng-disable="hasPrevPage()"><</span>
35+
<ul class="pages">
36+
<li ng-repeat="i in getPages(pageCount) track by $index"
37+
ng-click="loadPage($index+1)"
38+
ng-class="{ current: $index+1 === currentPage }">{{ $index+1 }}</li>
39+
</ul>
40+
<span class="next"
41+
ng-click="nextPage()"
42+
ng-disable="hasNextPage()">></span>
43+
</div>
44+
<!-- /Pagination -->
2945
</div>
3046
</div>
3147
</div>

PolyDeploy/Components/DataAccess/DataControllers/EventLogDataController.cs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DotNetNuke.Data;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67

78
namespace Cantarus.Modules.PolyDeploy.Components.DataAccess.DataControllers
89
{
@@ -97,6 +98,14 @@ public IEnumerable<string> GetEventTypes()
9798
}
9899
}
99100

101+
public int EventCount()
102+
{
103+
using (IDataContext context = DataContext.Instance())
104+
{
105+
return context.ExecuteQuery<int>(System.Data.CommandType.Text, "SELECT COUNT(*) FROM [dbo].[Cantarus_PolyDeploy_EventLogs]", null).FirstOrDefault();
106+
}
107+
}
108+
100109
#endregion
101110
}
102111
}

PolyDeploy/Components/Logging/EventLogManager.cs

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public static IEnumerable<string> GetEventTypes()
1919
return LogDC.GetEventTypes();
2020
}
2121

22+
public static int EventCount()
23+
{
24+
return LogDC.EventCount();
25+
}
26+
2227
public static void Log(string eventType, EventLogSeverity severity, string message = null, Exception ex = null)
2328
{
2429
// TODO: Internal logging switched on?

PolyDeploy/Components/WebAPI/EventLogController.cs

+88-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
using Cantarus.Modules.PolyDeploy.Components.Logging;
33
using Cantarus.Modules.PolyDeploy.Components.WebAPI.ActionFilters;
44
using DotNetNuke.Web.Api;
5+
using Newtonsoft.Json;
6+
using System;
57
using System.Collections.Generic;
68
using System.Linq;
79
using System.Net;
810
using System.Net.Http;
11+
using System.Text;
912
using System.Web.Http;
13+
using System.Web.Script.Serialization;
1014

1115
namespace Cantarus.Modules.PolyDeploy.Components.WebAPI
1216
{
@@ -27,9 +31,92 @@ public HttpResponseMessage Browse(int pageIndex = 0, int pageSize = 30, string e
2731
actualSeverity = (EventLogSeverity)severity;
2832
}
2933

34+
// Get event logs.
3035
List<EventLog> eventLogs = EventLogManager.Browse(pageIndex, pageSize, eventType, actualSeverity).ToList();
3136

32-
return Request.CreateResponse(HttpStatusCode.OK, eventLogs);
37+
// Work out pagination details.
38+
int rowCount = EventLogManager.EventCount();
39+
int pageCount = (int)Math.Ceiling((double)(rowCount / pageSize));
40+
41+
// Start building meta.
42+
Dictionary<string, dynamic> pagination = new Dictionary<string, dynamic>();
43+
44+
// Add basics.
45+
pagination.Add("Records", rowCount);
46+
pagination.Add("Pages", pageCount);
47+
pagination.Add("CurrentPage", pageIndex);
48+
49+
// Build navigation.
50+
Dictionary<string, string> navigation = new Dictionary<string, string>();
51+
52+
// Parameters passed in not changed by pagination.
53+
string fixedParams = "";
54+
55+
// Page size.
56+
if (pageSize != 30)
57+
{
58+
fixedParams += string.Format("pageSize={0}", pageSize);
59+
}
60+
61+
// Event type.
62+
if (eventType != null)
63+
{
64+
fixedParams += string.Format("eventType={0}", eventType);
65+
}
66+
67+
// Severity.
68+
if (severity != -1)
69+
{
70+
fixedParams += string.Format("eventType={0}", severity);
71+
}
72+
73+
// Is there a next page?
74+
if (pageIndex < pageCount)
75+
{
76+
string nextLink = string.Format("Browse?pageIndex={0}", pageIndex + 1);
77+
78+
if (!string.IsNullOrEmpty(fixedParams))
79+
{
80+
nextLink = string.Format("{0}&{1}", nextLink, fixedParams);
81+
}
82+
83+
navigation.Add("Next", nextLink);
84+
}
85+
86+
// Is there a previous page?
87+
if (pageIndex > 0)
88+
{
89+
string prevLink = string.Format("Browse?pageIndex={0}", pageIndex - 1);
90+
91+
if (!string.IsNullOrEmpty(fixedParams))
92+
{
93+
prevLink = string.Format("{0}&{1}", prevLink, fixedParams);
94+
}
95+
96+
navigation.Add("Previous", prevLink);
97+
}
98+
99+
// Add navigation.
100+
pagination.Add("Navigation", navigation);
101+
102+
Dictionary<string, dynamic> payload = new Dictionary<string, dynamic>();
103+
104+
payload.Add("Data", eventLogs);
105+
payload.Add("Pagination", pagination);
106+
107+
string json = JsonConvert.SerializeObject(payload);
108+
109+
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
110+
111+
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
112+
113+
return response;
114+
}
115+
116+
[HttpGet]
117+
public HttpResponseMessage Count()
118+
{
119+
return Request.CreateResponse(HttpStatusCode.OK, EventLogManager.EventCount());
33120
}
34121

35122
[HttpGet]

PolyDeploy/PolyDeploy.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
<HintPath>..\packages\DotNetNuke_Community_DotNetNuke.Web.Client.07.00.00\lib\DotNetNuke.Web.Client.dll</HintPath>
7171
<Private>False</Private>
7272
</Reference>
73+
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
74+
<HintPath>..\packages\DotNetNuke_Community_Newtonsoft.Json.07.00.00\lib\Newtonsoft.Json.dll</HintPath>
75+
<Private>False</Private>
76+
</Reference>
7377
<Reference Include="System" />
7478
<Reference Include="System.Data" />
7579
<Reference Include="System.Data.DataSetExtensions" />

PolyDeploy/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<package id="DotNetNuke_Community_DotNetNuke" version="07.00.00" targetFramework="net45" />
44
<package id="DotNetNuke_Community_DotNetNuke.Web" version="07.00.00" targetFramework="net45" />
55
<package id="DotNetNuke_Community_DotNetNuke.Web.Client" version="07.00.00" targetFramework="net45" />
6+
<package id="DotNetNuke_Community_Newtonsoft.Json" version="07.00.00" targetFramework="net45" />
67
<package id="DotNetNuke_Community_System.Net.Http" version="07.00.00" targetFramework="net45" />
78
<package id="DotNetNuke_Community_System.Net.Http.Formatting" version="07.00.00" targetFramework="net45" />
89
<package id="DotNetNuke_Community_System.Web.Http" version="07.00.00" targetFramework="net45" />

0 commit comments

Comments
 (0)