Skip to content

Commit 71450d7

Browse files
committed
make an api stub for non-backend mode
1 parent ad92da8 commit 71450d7

File tree

13 files changed

+494
-35
lines changed

13 files changed

+494
-35
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ This project was generated with [Angular CLI](https://github.com/angular/angular
66

77
## Install dependencies
88

9-
Run `npm install`. **Important! Please use `npm` not `yarn`!**
9+
Run `yarn install`.
1010

1111
## Development server
1212

13-
Run `ng serve` for a dev server. Navigate to `http://localhost:3000/`. The app will automatically reload if you change any of the source files.
13+
Run `yarn start` for a dev server. Navigate to `http://localhost:3000/`. The app will automatically reload if you change any of the source files.
14+
15+
### If you use app with backend support, please use
16+
```
17+
yarn run serve:backend
18+
```
1419

1520
## Code scaffolding
1621

1722
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
1823

1924
## Build
2025

21-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build.
26+
Run `yarn build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build.
2227

2328
## Further help
2429

angular.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@
9191
"with": "src/environments/environment.hmr.ts"
9292
}
9393
]
94+
},
95+
"backend": {
96+
"fileReplacements": [
97+
{
98+
"replace": "src/environments/environment.ts",
99+
"with": "src/environments/environment.backend.ts"
100+
}
101+
]
94102
}
95103
}
96104
},
@@ -107,6 +115,9 @@
107115
"hmr": {
108116
"hmr": true,
109117
"browserTarget": "Sing:build:hmr"
118+
},
119+
"backend": {
120+
"browserTarget": "Sing:build:backend"
110121
}
111122
}
112123
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"install": "napa",
77
"ng": "ng",
88
"start": "ng serve",
9+
"start:backend": "ng serve --configuration backend",
910
"hmr": "ng serve --configuration hmr",
1011
"build": "ng build --prod",
1112
"test": "ng test",

src/app/app.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@angular/core';
2+
import {environment} from '../environments/environment';
23

34
declare let jQuery: any;
45

@@ -12,6 +13,8 @@ export class AppConfig {
1213
name: 'sing',
1314
title: 'Sing Dashboard App with Angular 8.0 support by Flatlogic',
1415
version: '4.0.0',
16+
remote: 'https://flatlogic-node-backend.herokuapp.com',
17+
isBackend: environment.backend,
1518
hostApi,
1619
portApi,
1720
baseURLApi,

src/app/pages/analytics/analytics.service.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {EventEmitter, Injectable} from '@angular/core';
22
import {HttpClient} from '@angular/common/http';
3+
import {AppConfig} from '../../app.config';
4+
import mock from './mock';
35

46
@Injectable()
57
export class AnalyticsService {
@@ -10,15 +12,30 @@ export class AnalyticsService {
1012
_mainChart: any = [];
1113
_isReceiving: any = false;
1214

15+
config: any;
16+
1317
onReceiveDataSuccess: EventEmitter<boolean> = new EventEmitter();
1418

15-
constructor(private http: HttpClient) {}
19+
constructor(
20+
private http: HttpClient,
21+
appConfig: AppConfig
22+
) {
23+
this.config = appConfig.getConfig();
24+
}
1625

1726
receiveDataRequest() {
1827
this.receivingData();
19-
this.http.get('/analytics').subscribe(data => {
20-
this.receiveDataSuccess(data);
21-
});
28+
if (!this.config.isBackend) {
29+
new Promise((resolve) => {
30+
resolve(mock.backendData);
31+
}).then(data => {
32+
this.receiveDataSuccess(data);
33+
});
34+
} else {
35+
this.http.get('/analytics').subscribe(data => {
36+
this.receiveDataSuccess(data);
37+
});
38+
}
2239
}
2340

2441
receiveDataSuccess(payload) {

src/app/pages/analytics/mock.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,125 @@ export default {
156156
status: 'Sent',
157157
},
158158
],
159+
backendData: {
160+
visits: {
161+
count: 4.332,
162+
logins: 830,
163+
sign_out_pct: 0.5,
164+
rate_pct: 4.5
165+
},
166+
performance: {
167+
sdk: {
168+
this_period_pct: 60,
169+
last_period_pct: 30,
170+
},
171+
integration: {
172+
this_period_pct: 40,
173+
last_period_pct: 55,
174+
}
175+
},
176+
server: {
177+
1: {
178+
pct: 60,
179+
temp: 37,
180+
frequency: 3.3
181+
},
182+
2: {
183+
pct: 54,
184+
temp: 31,
185+
frequency: 3.3
186+
}
187+
},
188+
revenue: getRevenueData(),
189+
mainChart: getMainChartData()
190+
}
159191
};
192+
193+
function getRevenueData() {
194+
const data = [];
195+
const seriesCount = 3;
196+
const accessories = ['SMX', 'Direct', 'Networks'];
197+
198+
for (let i = 0; i < seriesCount; i += 1) {
199+
data.push({
200+
label: accessories[i],
201+
data: Math.floor(Math.random() * 100) + 1,
202+
});
203+
}
204+
205+
return data;
206+
}
207+
208+
function getMainChartData() {
209+
function generateRandomPicks(minPoint, maxPoint, picksAmount, xMax) {
210+
let x = 0;
211+
let y = 0;
212+
const result = [];
213+
const xStep = 1;
214+
const smoothness = 0.3;
215+
const pointsPerPick = Math.ceil(xMax / ((picksAmount * 2) + 1) / 2);
216+
217+
const maxValues = [];
218+
const minValues = [];
219+
220+
for (let i = 0; i < picksAmount; i += 1) {
221+
const minResult = minPoint + Math.random();
222+
const maxResult = maxPoint - Math.random();
223+
224+
minValues.push(minResult);
225+
maxValues.push(maxResult);
226+
}
227+
228+
let localMax = maxValues.shift(0);
229+
let localMin = 0;
230+
let yStep = parseFloat(((localMax - localMin) / pointsPerPick).toFixed(2));
231+
232+
for (let j = 0; j < Math.ceil(xMax); j += 1) {
233+
result.push([x, y]);
234+
235+
if ((y + yStep >= localMax) || (y + yStep <= localMin)) {
236+
y += yStep * smoothness;
237+
} else if ((result[result.length - 1][1] === localMax) || (result[result.length - 1][1] === localMin)) {
238+
y += yStep * smoothness;
239+
} else {
240+
y += yStep;
241+
}
242+
243+
if (y > localMax) {
244+
y = localMax;
245+
} else if (y < localMin) {
246+
y = localMin;
247+
}
248+
249+
if (y === localMin) {
250+
localMax = maxValues.shift(0) || localMax;
251+
252+
const share = (localMax - localMin) / localMax;
253+
const p = share > 0.5 ? Math.round(pointsPerPick * 1.2) : Math.round(pointsPerPick * share);
254+
255+
yStep = parseFloat(((localMax - localMin) / p).toFixed(2));
256+
yStep *= Math.abs(yStep);
257+
}
258+
259+
if (y === localMax) {
260+
localMin = minValues.shift(0) || localMin;
261+
262+
const share = (localMax - localMin) / localMax;
263+
const p = share > 0.5 ? Math.round(pointsPerPick * 1.5) : Math.round(pointsPerPick * 0.5);
264+
265+
yStep = parseFloat(((localMax - localMin) / p).toFixed(2));
266+
yStep *= -1;
267+
}
268+
269+
x += xStep;
270+
}
271+
272+
return result;
273+
}
274+
275+
const d1 = generateRandomPicks(0.2, 3, 4, 90);
276+
const d2 = generateRandomPicks(0.4, 3.8, 4, 90);
277+
const d3 = generateRandomPicks(0.2, 4.2, 3, 90);
278+
279+
return [d1, d2, d3];
280+
}

0 commit comments

Comments
 (0)