-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e88826
commit 4665d61
Showing
3 changed files
with
257 additions
and
5 deletions.
There are no files selected for viewing
82 changes: 77 additions & 5 deletions
82
backend/src/main/java/de/htwg_konstanz/mobilelearning/helper/Analytics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,81 @@ | ||
package de.htwg_konstanz.mobilelearning.helper; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
public class Analytics { | ||
public Float avg; | ||
public Float min; | ||
public Float max; | ||
public Float median; | ||
public Float count; | ||
public Double avg; | ||
public Double min; | ||
public Double max; | ||
public Double median; | ||
public Integer count; | ||
|
||
public Analytics() { | ||
this.avg = 0.0; | ||
this.min = 0.0; | ||
this.max = 0.0; | ||
this.median = 0.0; | ||
this.count = 0; | ||
} | ||
|
||
public void update(List<String> values) { | ||
|
||
// reset values | ||
if (values == null || values.size() == 0) { | ||
this.avg = 0.0; | ||
this.min = 0.0; | ||
this.max = 0.0; | ||
this.median = 0.0; | ||
this.count = 0; | ||
return; | ||
} | ||
|
||
// convert values to double | ||
List<Double> valuesAsDouble = new ArrayList<Double>(); | ||
values.forEach(valueAsString -> { | ||
try { | ||
valuesAsDouble.add(Double.parseDouble(valueAsString)); | ||
} catch (NumberFormatException e) { | ||
// ignore | ||
} | ||
}); | ||
|
||
// if everything is a string, update the count and return | ||
if (valuesAsDouble.size() == 0) { | ||
this.count = values.size(); | ||
return; | ||
} | ||
|
||
// update | ||
this.count = values.size(); | ||
this.min = valuesAsDouble.stream().mapToDouble(Double::doubleValue).min().orElse(0.0); | ||
this.max = valuesAsDouble.stream().mapToDouble(Double::doubleValue).max().orElse(0.0); | ||
this.avg = valuesAsDouble.stream().mapToDouble(Double::doubleValue).average().orElse(0.0); | ||
if (valuesAsDouble.size() % 2 == 0) { | ||
this.median = (valuesAsDouble.get(valuesAsDouble.size() / 2) + valuesAsDouble.get(valuesAsDouble.size() / 2 - 1)) / 2; | ||
} else { | ||
this.median = valuesAsDouble.get(valuesAsDouble.size() / 2); | ||
} | ||
} | ||
|
||
public Double getAvg() { | ||
return this.avg; | ||
} | ||
|
||
public Double getMin() { | ||
return this.min; | ||
} | ||
|
||
public Double getMax() { | ||
return this.max; | ||
} | ||
|
||
public Double getMedian() { | ||
return this.median; | ||
} | ||
|
||
public Integer getCount() { | ||
return this.count; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters