99namespace Controller \Api ;
1010
1111use Controller ;
12+ use lib \Config ;
1213use Model \Pathfinder ;
1314
1415class System extends Controller \AccessController {
@@ -39,7 +40,7 @@ public function graphData(\Base $f3){
3940 $ cacheResponse = false ;
4041
4142 // number of log entries in each table per system (24 = 24h)
42- $ logEntryCount = 24 ;
43+ $ logEntryCount = Pathfinder \AbstractSystemApiBasicModel:: DATA_COLUMN_COUNT ;
4344
4445 $ ttl = 60 * 10 ;
4546
@@ -51,47 +52,47 @@ public function graphData(\Base $f3){
5152 'factionKills ' => 'SystemFactionKillModel '
5253 ];
5354
55+ $ exists = false ;
56+
5457 foreach ($ systemIds as $ systemId ){
5558 $ cacheKey = $ this ->getSystemGraphCacheKey ($ systemId );
56- if ( ! $ f3 ->exists ($ cacheKey , $ graphData )){
59+ if (! $ exists = $ f3 ->exists ($ cacheKey , $ graphData )){
5760 $ graphData = [];
5861 $ cacheSystem = false ;
5962
60- foreach ($ logTables as $ label => $ ModelClass ){
61- $ systemLogModel = Pathfinder \AbstractPathfinderModel ::getNew ($ ModelClass );
63+ foreach ($ logTables as $ label => $ className ){
64+ $ systemLogModel = Pathfinder \AbstractSystemApiBasicModel ::getNew ($ className );
6265 $ systemLogExists = false ;
6366
6467 // 10min cache (could be up to 1h cache time)
6568 $ systemLogModel ->getByForeignKey ('systemId ' , $ systemId );
66- if ( ! $ systemLogModel ->dry () ){
69+ if ($ systemLogModel ->valid () ){
6770 $ systemLogExists = true ;
6871 $ cacheSystem = true ;
6972 $ cacheResponse = true ;
7073 }
7174
75+ $ systemLogData = $ systemLogModel ->getData ();
76+
7277 // podKills share graph with shipKills -> skip
7378 if ($ label != 'podKills ' ){
7479 $ graphData [$ label ]['logExists ' ] = $ systemLogExists ;
80+ $ graphData [$ label ]['updated ' ] = $ systemLogData ->updated ;
7581 }
7682
77- $ counter = 0 ;
78- for ( $ i = $ logEntryCount ; $ i >= 1 ; $ i --){
79- $ column = 'value ' . $ i ;
80- $ value = $ systemLogExists ? $ systemLogModel ->$ column : 0 ;
81-
82- // ship and pod kills should be merged into one table
83+ $ logValueCount = range (0 , $ logEntryCount - 1 );
84+ foreach ($ logValueCount as $ i ){
8385 if ($ label == 'podKills ' ){
84- $ graphData ['shipKills ' ]['data ' ][$ counter ]['z ' ] = $ value ;
86+ $ graphData ['shipKills ' ]['data ' ][$ i ]['z ' ] = $ systemLogData -> values [ $ i ] ;
8587 }else {
86- $ dataSet = [
87- 'x ' => ($ i - 1 ) . 'h ' ,
88- 'y ' => $ value
88+ $ graphData [ $ label ][ ' data ' ][] = [
89+ 'x ' => ($ logEntryCount - $ i - 1 ) . 'h ' ,
90+ 'y ' => $ systemLogData -> values [ $ i ]
8991 ];
90- $ graphData [$ label ]['data ' ][] = $ dataSet ;
9192 }
92- $ counter ++;
9393 }
9494 }
95+
9596 if ($ cacheSystem ){
9697 $ f3 ->set ($ cacheKey , $ graphData , $ ttl );
9798 }
@@ -104,7 +105,7 @@ public function graphData(\Base $f3){
104105
105106 if ($ cacheResponse ){
106107 // send client cache header
107- $ f3 ->expire ($ ttl );
108+ $ f3 ->expire (Config:: ttlLeft ( $ exists , $ ttl) );
108109 }
109110
110111 echo json_encode ($ graphsData );
0 commit comments