333 lines
8.1 KiB
TypeScript
333 lines
8.1 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
||
import { MatDialog } from '@angular/material/dialog';
|
||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||
import { DialogComponent } from '../dialog/dialog.component';
|
||
import { Rayconnect } from '../../../../services/rayconnect/rayconnect.service';
|
||
|
||
import { Chart } from 'chart.js'
|
||
import { FormControl } from '@angular/forms';
|
||
|
||
interface University {
|
||
value: string;
|
||
viewValue: string;
|
||
}
|
||
|
||
interface UniversityGroup {
|
||
disabled?: boolean;
|
||
name: string;
|
||
value: string;
|
||
universities: University[];
|
||
}
|
||
|
||
@Component({
|
||
selector: 'app-app',
|
||
templateUrl: './app.component.html',
|
||
styleUrls: ['./app.component.scss']
|
||
})
|
||
export class AppComponent implements OnInit {
|
||
|
||
universityControl = new FormControl();
|
||
grade = ["کاردانی"]
|
||
shift = ["روزانه"]
|
||
entry = ["مهر"]
|
||
majors = []
|
||
majorList = []
|
||
|
||
univeristygroups: UniversityGroup[] = [
|
||
|
||
{
|
||
name: 'مرکزی',
|
||
value: "markazi",
|
||
universities: [
|
||
{ value: 'athar', viewValue: 'اطهر' },
|
||
{ value: 'amirkabir', viewValue: 'امیر کبیر' },
|
||
{ value: 'khomein', viewValue: 'دکتر حسابی خمین' },
|
||
{ value: 'saveh', viewValue: 'مرکز پسران ساوه' }
|
||
|
||
]
|
||
}
|
||
];
|
||
public list: { _id: string, name: string }[] = [];
|
||
options = {
|
||
responsive: true,
|
||
|
||
}
|
||
precolor = {}
|
||
data: any = []
|
||
|
||
|
||
constructor(private dialog: MatDialog, private rayconnect: Rayconnect, private snackbar: MatSnackBar) { }
|
||
|
||
ngOnInit(): void {
|
||
this.fetch();
|
||
|
||
Chart.defaults.global.defaultFontFamily = "Font"
|
||
|
||
this.universityControl.setValue("all")
|
||
|
||
this.subscribe()
|
||
|
||
let cachedMajorColor = []
|
||
let cachedMajorLabel = []
|
||
|
||
this.rayconnect.Query({
|
||
scope: "kateb",
|
||
address: "volunteers/stats/live",
|
||
method: "LIVE",
|
||
}, (res => {
|
||
res['data']['majors'] = res['data']['majors'].sort(function (a, b) { return b.count - a.count });
|
||
res['data']['waiting'] = res['data']['waiting'].sort(function (a, b) { return b.count - a.count });
|
||
res['data']['entry'] = res['data']['entry'].sort(function (a, b) { return b.count - a.count });
|
||
res['data']['steps'] = res['data']['steps'].sort(function (a, b) { return b.count - a.count });
|
||
res['data']['shift'] = res['data']['shift'].sort(function (a, b) { return b.count - a.count });
|
||
res['data']['grade'] = res['data']['grade'].sort(function (a, b) { return b.count - a.count });
|
||
|
||
|
||
console.log(res['data'])
|
||
|
||
|
||
if(this.majors.length == 0){
|
||
this.majorList = res['data']['majors']
|
||
}
|
||
let cachedMajorColor = this.RandomColor(res['data']['majors'].length, 'majors');
|
||
let cachedMajorLabel = res['data']['majors'].map((major) => {
|
||
return major._id
|
||
})
|
||
|
||
|
||
|
||
|
||
|
||
|
||
this.buildMajorsStats(res['data']['majors'], res['data']['waiting'], res['data']['entry'], res['data']['steps'], res['data']['grade'], res['data']['shift'], cachedMajorColor, cachedMajorLabel)
|
||
|
||
|
||
|
||
|
||
|
||
}));
|
||
|
||
}
|
||
|
||
subscribe() {
|
||
this.rayconnect.RequestBack({
|
||
scope: 'kateb',
|
||
uniqueID: 'profile',
|
||
TokenID: '*',
|
||
address: 'volunteers/stats/subscribe',
|
||
info: {
|
||
method: 'SUB',
|
||
data: {
|
||
|
||
grade: this.grade,
|
||
shift: this.shift,
|
||
entry: this.entry,
|
||
major: this.majors
|
||
}
|
||
}
|
||
}).then(res => {
|
||
if (res['data']['status'] == true) { }
|
||
})
|
||
}
|
||
RandomColor(len, id) {
|
||
|
||
if (this.precolor[id]) return this.precolor[id]
|
||
|
||
let colors = []
|
||
|
||
for (let i = 0; i < len; i++) {
|
||
|
||
colors.push('#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6)
|
||
)
|
||
}
|
||
|
||
this.precolor[id] = colors
|
||
return colors
|
||
}
|
||
|
||
buildMajorsStats(majors, waiting, entry, steps, grades, shift, colors, labels) {
|
||
|
||
|
||
this.data[0] = {
|
||
labels: labels,
|
||
type: "doughnut",
|
||
datasets: [{
|
||
label: 'تعداد ثبت نامی به تفکیک رشته',
|
||
data: majors.map((major) => {
|
||
return major.count
|
||
}),
|
||
backgroundColor: colors,
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
|
||
this.data[1] = {
|
||
labels: waiting.map((w) => {
|
||
switch (w._id) {
|
||
case "wait":
|
||
return "در انتظار تایید"
|
||
case "no":
|
||
return "فاقد وضعیت"
|
||
case "resolve":
|
||
return "تایید شده"
|
||
case "reject":
|
||
return "رد شده"
|
||
}
|
||
}),
|
||
type: "bar",
|
||
datasets: [{
|
||
label: 'وضعیت تایید افراد',
|
||
data: waiting.map((w) => {
|
||
return w.count
|
||
}),
|
||
backgroundColor: this.RandomColor(waiting.length, "waiting"),
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
|
||
this.data[2] = {
|
||
labels: entry.map((w) => {
|
||
return w._id
|
||
}),
|
||
type: "pie",
|
||
datasets: [{
|
||
label: 'امار ورودی ها',
|
||
data: entry.map((w) => {
|
||
return w.count
|
||
}),
|
||
backgroundColor: this.RandomColor(entry.length, "entry"),
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
|
||
this.data[3] = {
|
||
labels: steps.map((w) => {
|
||
return w._id
|
||
}),
|
||
type: "pie",
|
||
datasets: [{
|
||
label: 'وضعیت افراد در مراحل',
|
||
data: steps.map((w) => {
|
||
return w.count
|
||
}),
|
||
backgroundColor: this.RandomColor(steps.length, "steps"),
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
|
||
this.data[4] = {
|
||
labels: grades.map((w) => {
|
||
return w._id
|
||
}),
|
||
type: "pie",
|
||
datasets: [{
|
||
label: 'آمار مقطع کاربران',
|
||
data: grades.map((w) => {
|
||
return w.count
|
||
}),
|
||
backgroundColor: this.RandomColor(grades.length, "grades"),
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
|
||
this.data[5] = {
|
||
labels: shift.map((w) => {
|
||
return w._id
|
||
}),
|
||
type: "pie",
|
||
datasets: [{
|
||
label: 'آمار کاربران (شیفت)',
|
||
data: shift.map((w) => {
|
||
return w.count
|
||
}),
|
||
backgroundColor: this.RandomColor(shift.length, "shift"),
|
||
borderWidth: 1
|
||
}]
|
||
}
|
||
/* this.data[3] = {
|
||
labels: majors.map((major) => {
|
||
return major.name
|
||
}),
|
||
type: "doughnut",
|
||
datasets: [{
|
||
label: 'آمار مراجعات حضوری بر اساس رشته',
|
||
data: majors.map(() => {
|
||
return Math.floor(Math.random() * 6) + 1
|
||
}),
|
||
backgroundColor: this.RandomColor(majors.length),
|
||
borderWidth: 1
|
||
}]
|
||
|
||
|
||
}
|
||
*/
|
||
|
||
/* let labels = []
|
||
let m = moment()
|
||
for (let index = 0; index < 10; index++) {
|
||
m = m.add(1, 'd')
|
||
labels.push(m.format('jYYYY/jM/jD'));
|
||
|
||
}
|
||
/* this.data[4] = {
|
||
labels: labels,
|
||
type: "line",
|
||
datasets: [{
|
||
label: 'آمار مراجعات حضوری بر اساس تاریخ',
|
||
data: labels.map((da) => {
|
||
return Math.floor(Math.random() * 6) + 1;
|
||
}),
|
||
borderColor: 'rgb(75, 192, 192)',
|
||
lineTension: 0.1,
|
||
borderWidth: 1
|
||
}]
|
||
|
||
|
||
} */
|
||
|
||
|
||
}
|
||
fetch() {
|
||
this.rayconnect.RequestBack({
|
||
scope: 'kateb',
|
||
uniqueID: 'profile',
|
||
TokenID: '*',
|
||
address: 'major/list',
|
||
info: {
|
||
method: 'LIST',
|
||
data: {}
|
||
}
|
||
}).then(res => {
|
||
if (res['data']['status'] == true) return;
|
||
})
|
||
}
|
||
|
||
delete(id: string) {
|
||
this.rayconnect.RequestBack({
|
||
scope: 'kateb',
|
||
uniqueID: 'profile',
|
||
TokenID: '*',
|
||
address: 'major/delete',
|
||
info: {
|
||
method: 'DELETE',
|
||
data: { id }
|
||
}
|
||
}).then(res => {
|
||
this.snackbar.open(res['data']['message'], 'باشه', { duration: 3000, direction: 'rtl' });
|
||
if (res['data']['status'] == true) {
|
||
let index = this.list.findIndex(item => item._id == id);
|
||
this.list.splice(index, 1);
|
||
}
|
||
})
|
||
}
|
||
|
||
openDialog(data) {
|
||
this.dialog.open(DialogComponent, { data, autoFocus: false, disableClose: true }).afterClosed().subscribe((result: { name: string, _id: string }) => {
|
||
if (!result) return;
|
||
let index = this.list.findIndex(item => item._id == result._id);
|
||
if (index < 0) this.list.push(result);
|
||
else this.list[index] = result;
|
||
})
|
||
}
|
||
}
|