Added a bunch of stuff
This commit is contained in:
118
src/services/data.service.ts
Normal file
118
src/services/data.service.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import Papa, { ParseRemoteConfig, ParseResult } from 'papaparse';
|
||||
import { Milestone } from '../models/milestone';
|
||||
import { Column } from '../enums/column';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DataService {
|
||||
private _data: Milestone[] = [];
|
||||
|
||||
private counties = new Subject<string[]>();
|
||||
readonly Counties = this.counties.asObservable();
|
||||
|
||||
private schools = new Subject<string[]>();
|
||||
readonly Schools = this.schools.asObservable();
|
||||
|
||||
private years = new Subject<number[]>();
|
||||
readonly Years = this.years.asObservable();
|
||||
|
||||
private grades = new Subject<number[]>();
|
||||
readonly Grades = this.grades.asObservable();
|
||||
|
||||
constructor() {
|
||||
this.load();
|
||||
}
|
||||
|
||||
private load(): void {
|
||||
let files: { url: string; year: number; grade: number }[] = [];
|
||||
for (let year = 2015; year < 2024; ++year) {
|
||||
if (year !== 2020) {
|
||||
for (let grade = 3; grade < 9; ++grade) {
|
||||
files.push({
|
||||
url: `assets/data/${year}-${grade}.csv`,
|
||||
year: year,
|
||||
grade: grade,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let count = files.length;
|
||||
files.forEach((file: { url: string; year: number; grade: number }) => {
|
||||
Papa.parse(file.url, {
|
||||
download: true,
|
||||
header: false,
|
||||
complete: (results: ParseResult<Record<string, any>>) => {
|
||||
(results?.data ?? []).forEach(
|
||||
(record: Record<string, any>, index: number) => {
|
||||
if (index < 3) return;
|
||||
this._data.push(
|
||||
new Milestone({
|
||||
Year: file.year,
|
||||
Grade: file.grade,
|
||||
County: record[Column.County],
|
||||
School: record[Column.School],
|
||||
ELA: {
|
||||
Count: record[Column.ELA_Count],
|
||||
Mean: record[Column.ELA_Mean],
|
||||
L0: record[Column.ELA_0],
|
||||
L1: record[Column.ELA_1],
|
||||
L2: record[Column.ELA_2],
|
||||
L3: record[Column.ELA_3],
|
||||
},
|
||||
Math: {
|
||||
Count: record[Column.Math_Count],
|
||||
Mean: record[Column.Math_Mean],
|
||||
L0: record[Column.Math_0],
|
||||
L1: record[Column.Math_1],
|
||||
L2: record[Column.Math_2],
|
||||
L3: record[Column.Math_3],
|
||||
},
|
||||
Science: {
|
||||
Count: record[Column.Sci_Count],
|
||||
Mean: record[Column.Sci_Mean],
|
||||
L0: record[Column.Sci_0],
|
||||
L1: record[Column.Sci_1],
|
||||
L2: record[Column.Sci_2],
|
||||
L3: record[Column.Sci_3],
|
||||
},
|
||||
Social: {
|
||||
Count: record[Column.Soc_Count],
|
||||
Mean: record[Column.Soc_Mean],
|
||||
L0: record[Column.Soc_0],
|
||||
L1: record[Column.Soc_1],
|
||||
L2: record[Column.Soc_2],
|
||||
L3: record[Column.Soc_3],
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
);
|
||||
--count;
|
||||
if (count === 0) this.loadComplete();
|
||||
},
|
||||
error: (error: any) => {
|
||||
console.error(error);
|
||||
--count;
|
||||
if (count === 0) this.loadComplete();
|
||||
},
|
||||
} as ParseRemoteConfig);
|
||||
});
|
||||
}
|
||||
|
||||
private loadComplete(): void {
|
||||
this.counties.next(
|
||||
[...new Set(this._data.map((data: Milestone) => data.County))].sort()
|
||||
);
|
||||
this.schools.next(
|
||||
[...new Set(this._data.map((data: Milestone) => data.School))].sort()
|
||||
);
|
||||
this.years.next(
|
||||
[...new Set(this._data.map((data: Milestone) => data.Year))].sort()
|
||||
);
|
||||
this.grades.next(
|
||||
[...new Set(this._data.map((data: Milestone) => data.Grade))].sort()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user