Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Connect statistics to database #263

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Plugins/RawCMS.Plugins.Core/Controllers/CRUDController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,28 @@ public RestMessage<bool> Delete(string collection, string id)
}
return response;
}

//
[HttpGet("{collection}/count")]
public RestMessage<long> Count(string collection)
{
RestMessage<long> response = new RestMessage<long>(0);
try
{
var result = service.Count(collection);
response.Data = result;
}catch(Exception ex)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = ex.Message,
});
}

return response;
}
}
}
10 changes: 5 additions & 5 deletions Plugins/RawCMS.Plugins.Core/UI/components/dashboard/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const _DashboardDef = async () => {
totalRecordsNum: function () {
const quotasObj = optionalChain(() => this.info.recordQuotas);
if (quotasObj === undefined) {
return undefined;
return 0;
}

return Object.keys(quotasObj)
Expand All @@ -28,10 +28,10 @@ const _DashboardDef = async () => {
});
const labels = [];
const data = [];
Object.keys(quotasObj).forEach(x => {
labels.push(x);
data.push(quotasObj[x]);
});
for (const index in quotasObj) {
labels.push(index);
data.push(quotasObj[index]);
}

return { data, labels };
}
Expand Down
35 changes: 16 additions & 19 deletions Plugins/RawCMS.Plugins.Core/UI/services/dashboard.service.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { sleep } from "/app/utils/time.utils.js";
import { BaseApiService } from "/app/common/shared/services/base-api-service.js";
import { BaseCrudService } from '/app/common/shared/services/base-crud-service.js';

class DashboardService extends BaseApiService {
class DashboardService extends BaseCrudService {
constructor() {
super();
super({ basePath: "/system/admin/_schema" });
}

async getDashboardInfo() {
// FIXME: For now we use mock data

await sleep(5000);

const quota = {
TEST: Math.floor(Math.random() * 100),
Items1: Math.floor(Math.random() * 100),
Items2: Math.floor(Math.random() * 100),
Items3: Math.floor(Math.random() * 100),
Items4: Math.floor(Math.random() * 100),
Items5: Math.floor(Math.random() * 100),
Items6: Math.floor(Math.random() * 100)
};

this._basePath = '/system/admin/_schema';
let recordCount = await this.count();
let collections = await this.getAll();
let quota = {};
for await (const item of collections) {
let collName = item.CollectionName;
this._basePath = '/api/CRUD/' + collName;
let collCount = await this.count();
quota[collName] = collCount;
}

return {
recordQuotas: quota,
entitiesNum: Object.keys(quota).length,
lastWeekCallsNum: Math.floor(Math.random() * 500)
entitiesNum: recordCount
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ const _SimplePieChart = {
return res;
},
sortedData: function () {
//let data = optionalChain(() => [...this.context.data], {
// fallbackValue: []
//}).sort((a, b) => a - b);

//if (this.options.lowerIsBetter) {
// data = data.reverse();
//}
let data = optionalChain(() => [...this.context.data], {
fallbackValue: []
}).sort((a, b) => a - b);

if (this.options.lowerIsBetter) {
data = data.reverse();
}
})

return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ export class BaseCrudService extends mix(BaseApiService, ICrudService) {

return res.data.data;
}

async count() {

const res = await this._apiClient.get(`${this._basePath}/count`);
if (!this._checkGenericError(res)) {
return false;
}

return res.data.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export class ICrudService {
async delete(id) {
throw new Error(`Please Provide an implementation for ${this.delete.name}`);
}

async count(obj) {
throw new Error(`Please Provide an implementation for ${this.count.name}`);
}
}
2 changes: 1 addition & 1 deletion RawCMS.Library/Service/CRUDService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public JObject Get(string collection, string id, List<string> expando = null)
return output;
}

public long Count(string collection, string query)
public long Count(string collection, string query = null)
{
FilterDefinition<BsonDocument> filter = FilterDefinition<BsonDocument>.Empty;
if (!string.IsNullOrWhiteSpace(query))
Expand Down