simplify graph data loops

pull/17/head
HardiReady 2017-11-04 15:58:48 +01:00
parent 4ea1239a75
commit e263ce050a
2 changed files with 74 additions and 58 deletions

View File

@ -26,7 +26,7 @@
<div class="pull-left " style="padding-left: 150px; padding-top:15px"> <div class="pull-left " style="padding-left: 150px; padding-top:15px">
<a class="btn btn-default" style="margin: 20px" target="_blank" onclick="window.open(this.href, 'logfileWindow', <a class="btn btn-default" style="margin: 20px" target="_blank" onclick="window.open(this.href, 'logfileWindow',
'left=20,top=20,width=1100,height=600,toolbar=1,resizable=0'); return false;" 'left=20,top=20,width=1380,height=600,toolbar=1,resizable=0'); return false;"
href="resource/logs/{{war._id}}/clean.log"> href="resource/logs/{{war._id}}/clean.log">
Logfile anzeigen</a> Logfile anzeigen</a>
<form class="form-group"> <form class="form-group">
@ -118,7 +118,6 @@
<div class="chart-select-group btn-group" (click)="selectChart()"> <div class="chart-select-group btn-group" (click)="selectChart()">
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelPoints}}">{{labelPoints}}</label> <label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelPoints}}">{{labelPoints}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelFlag}}">{{labelFlag}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelBudget}}">{{labelBudget}}</label> <label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelBudget}}">{{labelBudget}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelKill}}">{{labelKill}}</label> <label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelKill}}">{{labelKill}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelFriendlyFire}}">{{labelFriendlyFire}}</label> <label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelFriendlyFire}}">{{labelFriendlyFire}}</label>
@ -127,6 +126,7 @@
btnRadio="{{labelStabilize}}">{{labelStabilize}}</label> btnRadio="{{labelStabilize}}">{{labelStabilize}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" <label class="btn btn-default" [(ngModel)]="chartSelectModel"
btnRadio="{{labelTransport}}">{{labelTransport}}</label> btnRadio="{{labelTransport}}">{{labelTransport}}</label>
<label class="btn btn-default" [(ngModel)]="chartSelectModel" btnRadio="{{labelFlag}}">{{labelFlag}}</label>
</div> </div>
<div *ngIf="showLineChart" class="chart-container"> <div *ngIf="showLineChart" class="chart-container">

View File

@ -63,7 +63,7 @@ export class WarDetailComponent {
labelRevive = 'Revive'; labelRevive = 'Revive';
labelStabilize = 'Stabilisiert'; labelStabilize = 'Stabilisiert';
labelFlag = 'Flaggenbesitz'; labelFlag = 'Flaggenbesitz';
lineChartLabel = this.labelPoints; lineChartLabel: string = this.labelPoints;
showLineChart = true; showLineChart = true;
stepCurve = d3.curveStepAfter; stepCurve = d3.curveStepAfter;
@ -184,34 +184,16 @@ export class WarDetailComponent {
return; return;
} }
this.tmpPointData = ChartUtils.getMultiDataArray('NATO', 'CSAT'); this.initializeTempCollections();
this.tmpBudgetData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpKillData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpFrienlyFireData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpTransportData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpReviveData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpStabilizeData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpFlagCaptureData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
[this.tmpKillData, this.tmpFrienlyFireData, this.tmpReviveData, this.tmpStabilizeData, this.tmpTransportData].forEach(tmp => {
[0, 1].forEach(index => {
tmp[index].series.push(ChartUtils.getSeriesEntry(this.startDateObj, 0));
})
});
this.logsService.getFullLog(this.war._id).subscribe((data) => { this.logsService.getFullLog(this.war._id).subscribe((data) => {
this.logData = data; this.logData = data;
this.initPointData(); this.initPointData();
this.initBudgetData(); this.initBudgetData();
this.initKillData(); this.initKillData();
this.initRevive(); this.initRevive();
this.initTransportData(); this.initTransportData();
this.initFlagHoldData(); this.initFlagHoldData();
this.addFinalTimeDataEntriesAndPopulate(new Date(this.war.endDate)); this.addFinalTimeDataEntriesAndPopulate(new Date(this.war.endDate));
@ -228,65 +210,69 @@ export class WarDetailComponent {
} }
initBudgetData() { initBudgetData() {
this.tmpBudgetData[0].series.push(ChartUtils.getSeriesEntry(this.startDateObj, this.war.budgetBlufor)); this.logData.budget.forEach(budgetEntry => {
this.tmpBudgetData[1].series.push(ChartUtils.getSeriesEntry(this.startDateObj, this.war.budgetOpfor)); const budgetEntryDate = new Date(budgetEntry.time);
// This adds an entry for both fractions on every event
for (let i = 0; i < this.logData.budget.length; i++) {
const budgetEntry = this.logData.budget[i];
const fractionChange = budgetEntry.fraction === 'BLUFOR' ? 0 : 1; const fractionChange = budgetEntry.fraction === 'BLUFOR' ? 0 : 1;
const fractionOld = budgetEntry.fraction !== 'BLUFOR' ? 0 : 1; const fractionOld = budgetEntry.fraction !== 'BLUFOR' ? 0 : 1;
if (WarDetailComponent.isTwoMinutesAhead(budgetEntry, this.tmpBudgetData, i, this.logData.budget.length)) { if (WarDetailComponent.isTwoMinutesAhead(budgetEntryDate, this.tmpBudgetData)) {
this.tmpBudgetData[fractionChange].series.push(ChartUtils.getSeriesEntry(new Date(budgetEntry.time), budgetEntry.newBudget)); this.tmpBudgetData[fractionChange].series.push(ChartUtils.getSeriesEntry(new Date(budgetEntry.time), budgetEntry.newBudget));
this.tmpBudgetData[fractionOld].series.push(ChartUtils.getSeriesEntry(new Date(budgetEntry.time), this.tmpBudgetData[fractionOld].series.push(ChartUtils.getSeriesEntry(new Date(budgetEntry.time),
this.tmpBudgetData[fractionOld].series[this.tmpBudgetData[fractionOld].series.length - 1].value)); this.tmpBudgetData[fractionOld].series[this.tmpBudgetData[fractionOld].series.length - 1].value));
} }
} });
this.tmpBudgetData[0].series.push(ChartUtils.getSeriesEntry(new Date(this.war.endDate), this.war.endBudgetBlufor));
this.tmpBudgetData[1].series.push(ChartUtils.getSeriesEntry(new Date(this.war.endDate), this.war.endBudgetOpfor));
} }
initKillData() { initKillData() {
let killCountBlufor = 0, killCountOpfor = 0, ffKillCountBlufor = 0, ffKillCountOpfor = 0 let killCountBlufor = 0, killCountOpfor = 0, ffKillCountBlufor = 0, ffKillCountOpfor = 0;
for (let i = 0; i < this.logData.kill.length; i++) {
const killEntry = this.logData.kill[i]; for (const {killEntry, index} of this.logData.kill.map((killEntry, index) => ({killEntry, index}))) {
const killEntryDate = new Date(killEntry.time);
if (killEntry.friendlyFire === false) { if (killEntry.friendlyFire === false) {
if (killEntry.fraction === 'BLUFOR') { if (killEntry.fraction === 'BLUFOR') {
killCountBlufor++; killCountBlufor++;
} else { }
if (killEntry.fraction === 'OPFOR') {
killCountOpfor++; killCountOpfor++;
} }
if (WarDetailComponent.isTwoMinutesAhead(killEntry, this.tmpKillData, i, this.logData.kill.length)) { if (WarDetailComponent.isTwoMinutesAhead(killEntryDate, this.tmpKillData)) {
this.tmpKillData[0].series.push(ChartUtils.getSeriesEntry(new Date(killEntry.time), killCountBlufor)); this.tmpKillData[0].series.push(ChartUtils.getSeriesEntry(killEntryDate, killCountBlufor));
this.tmpKillData[1].series.push(ChartUtils.getSeriesEntry(new Date(killEntry.time), killCountOpfor)); this.tmpKillData[1].series.push(ChartUtils.getSeriesEntry(killEntryDate, killCountOpfor));
} }
} else { } else {
if (killEntry.fraction === 'BLUFOR') { if (killEntry.fraction === 'BLUFOR') {
ffKillCountBlufor++; ffKillCountBlufor++;
} else { }
if (killEntry.fraction === 'OPFOR') {
ffKillCountOpfor++; ffKillCountOpfor++;
} }
if (WarDetailComponent.isTwoMinutesAhead(killEntry, this.tmpFrienlyFireData, i, this.logData.kill.length)) { if (WarDetailComponent.isTwoMinutesAhead(killEntryDate, this.tmpFrienlyFireData)) {
this.tmpFrienlyFireData[0].series.push(ChartUtils.getSeriesEntry(new Date(killEntry.time), ffKillCountBlufor)); this.tmpFrienlyFireData[0].series.push(ChartUtils.getSeriesEntry(killEntryDate, ffKillCountBlufor));
this.tmpFrienlyFireData[1].series.push(ChartUtils.getSeriesEntry(new Date(killEntry.time), ffKillCountOpfor)); this.tmpFrienlyFireData[1].series.push(ChartUtils.getSeriesEntry(killEntryDate, ffKillCountOpfor));
} }
} }
if (index === this.logData.kill.length - 1) {
this.tmpKillData[0].series.push(ChartUtils.getSeriesEntry(killEntryDate, killCountBlufor));
this.tmpKillData[1].series.push(ChartUtils.getSeriesEntry(killEntryDate, killCountOpfor));
this.tmpFrienlyFireData[0].series.push(ChartUtils.getSeriesEntry(killEntryDate, ffKillCountBlufor));
this.tmpFrienlyFireData[1].series.push(ChartUtils.getSeriesEntry(killEntryDate, ffKillCountOpfor));
}
} }
} }
initRevive() { initRevive() {
let reviveCountBlufor = 0, reviveCountOpfor = 0, stabilizeCountBlufor = 0, stabilizeCountOpfor = 0; let reviveCountBlufor = 0, reviveCountOpfor = 0, stabilizeCountBlufor = 0, stabilizeCountOpfor = 0;
for (let i = 0; i < this.logData.revive.length; i++) { for (const {reviveEntry, index} of this.logData.revive.map((reviveEntry, index) => ({reviveEntry, index}))) {
const reviveEntry = this.logData.revive[i]; const reviveEntryDate = new Date(reviveEntry.time);
if (reviveEntry.stabilized === false) { if (reviveEntry.stabilized === false) {
if (reviveEntry.fraction === 'BLUFOR') { if (reviveEntry.fraction === 'BLUFOR') {
reviveCountBlufor++; reviveCountBlufor++;
} else { } else {
reviveCountOpfor++; reviveCountOpfor++;
} }
if (WarDetailComponent.isTwoMinutesAhead(reviveEntry, this.tmpReviveData, i, this.logData.revive.length)) { if (WarDetailComponent.isTwoMinutesAhead(reviveEntryDate, this.tmpReviveData)) {
this.tmpReviveData[0].series.push(ChartUtils.getSeriesEntry(new Date(reviveEntry.time), reviveCountBlufor)); this.tmpReviveData[0].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, reviveCountBlufor));
this.tmpReviveData[1].series.push(ChartUtils.getSeriesEntry(new Date(reviveEntry.time), reviveCountOpfor)); this.tmpReviveData[1].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, reviveCountOpfor));
} }
} else { } else {
if (reviveEntry.fraction === 'BLUFOR') { if (reviveEntry.fraction === 'BLUFOR') {
@ -294,26 +280,35 @@ export class WarDetailComponent {
} else { } else {
stabilizeCountOpfor++; stabilizeCountOpfor++;
} }
if (WarDetailComponent.isTwoMinutesAhead(reviveEntry, this.tmpStabilizeData, i, this.logData.revive.length)) { if (WarDetailComponent.isTwoMinutesAhead(reviveEntryDate, this.tmpStabilizeData)) {
this.tmpStabilizeData[0].series.push(ChartUtils.getSeriesEntry(new Date(reviveEntry.time), stabilizeCountBlufor)); this.tmpStabilizeData[0].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, stabilizeCountBlufor));
this.tmpStabilizeData[1].series.push(ChartUtils.getSeriesEntry(new Date(reviveEntry.time), stabilizeCountOpfor)); this.tmpStabilizeData[1].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, stabilizeCountOpfor));
} }
} }
if (index === this.logData.revive.length - 1) {
this.tmpReviveData[0].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, reviveCountBlufor));
this.tmpReviveData[1].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, reviveCountOpfor));
this.tmpStabilizeData[0].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, stabilizeCountBlufor));
this.tmpStabilizeData[1].series.push(ChartUtils.getSeriesEntry(reviveEntryDate, stabilizeCountOpfor));
}
} }
} }
initTransportData() { initTransportData() {
let transportCountBlufor = 0, transportCountOpfor = 0; let transportCountBlufor = 0, transportCountOpfor = 0;
for (let i = 0; i < this.logData.transport.length; i++) { for (const {transportEntry, index} of this.logData.transport.map((transportEntry, index) => ({
const transportEntry = this.logData.transport[i]; transportEntry,
index
}))) {
const transportEntryDate = new Date(transportEntry.time);
if (transportEntry.fraction === 'BLUFOR') { if (transportEntry.fraction === 'BLUFOR') {
transportCountBlufor++; transportCountBlufor++;
} else { } else {
transportCountOpfor++; transportCountOpfor++;
} }
if (WarDetailComponent.isTwoMinutesAhead(transportEntry, this.tmpTransportData, i, this.logData.transport.length)) { if (WarDetailComponent.isTwoMinutesAhead(transportEntryDate, this.tmpTransportData) || index === this.logData.transport.length - 1) {
this.tmpTransportData[0].series.push(ChartUtils.getSeriesEntry(new Date(transportEntry.time), transportCountBlufor)); this.tmpTransportData[0].series.push(ChartUtils.getSeriesEntry(transportEntryDate, transportCountBlufor));
this.tmpTransportData[1].series.push(ChartUtils.getSeriesEntry(new Date(transportEntry.time), transportCountOpfor)); this.tmpTransportData[1].series.push(ChartUtils.getSeriesEntry(transportEntryDate, transportCountOpfor));
} }
} }
} }
@ -336,12 +331,31 @@ export class WarDetailComponent {
}); });
} }
private static isTwoMinutesAhead(entry: any, tmpData: any, index: number, logDataLength: number): boolean { private static isTwoMinutesAhead(entryDate: Date, tmpData: any): boolean {
return new Date(entry.time).getTime() >= tmpData[0].series[tmpData[0].series.length - 1].name.getTime() + (1.5 * 60000) || index === logDataLength - 1 return entryDate.getTime() >= tmpData[0].series[tmpData[0].series.length - 1].name.getTime() + (1.5 * 60000)
}
initializeTempCollections() {
this.tmpPointData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpBudgetData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpKillData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpFrienlyFireData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpTransportData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpReviveData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpStabilizeData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
this.tmpFlagCaptureData = ChartUtils.getMultiDataArray('NATO', 'CSAT');
[this.tmpKillData, this.tmpFrienlyFireData, this.tmpReviveData, this.tmpStabilizeData, this.tmpTransportData].forEach(tmp => {
[0, 1].forEach(index => {
tmp[index].series.push(ChartUtils.getSeriesEntry(this.startDateObj, 0));
})
});
this.tmpBudgetData[0].series.push(ChartUtils.getSeriesEntry(this.startDateObj, this.war.budgetBlufor));
this.tmpBudgetData[1].series.push(ChartUtils.getSeriesEntry(this.startDateObj, this.war.budgetOpfor));
} }
addFinalTimeDataEntriesAndPopulate(endDate) { addFinalTimeDataEntriesAndPopulate(endDate) {
[this.tmpPointData, this.tmpBudgetData, this.tmpTransportData, this.tmpReviveData, this.tmpStabilizeData, [this.tmpPointData, this.tmpTransportData, this.tmpReviveData, this.tmpStabilizeData,
this.tmpKillData, this.tmpFrienlyFireData, this.tmpFlagCaptureData].forEach(tmp => { this.tmpKillData, this.tmpFrienlyFireData, this.tmpFlagCaptureData].forEach(tmp => {
for (let j in [0, 1]) { for (let j in [0, 1]) {
if (tmp[j].series[tmp[j].series.length - 1].name < endDate) { if (tmp[j].series[tmp[j].series.length - 1].name < endDate) {
@ -349,6 +363,8 @@ export class WarDetailComponent {
} }
} }
}); });
this.tmpBudgetData[0].series.push(ChartUtils.getSeriesEntry(endDate, this.war.endBudgetBlufor));
this.tmpBudgetData[1].series.push(ChartUtils.getSeriesEntry(endDate, this.war.endBudgetOpfor));
this.showLineChart = true; this.showLineChart = true;
this.lineChartLabel = this.labelPoints; this.lineChartLabel = this.labelPoints;