| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  | import { ChangeDetectionStrategy, Component, Inject, Input, LOCALE_ID, OnInit, HostBinding } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  | import { EChartsOption} from 'echarts'; | 
					
						
							|  |  |  | import { Observable } from 'rxjs'; | 
					
						
							|  |  |  | import { map, share, startWith, switchMap, tap } from 'rxjs/operators'; | 
					
						
							| 
									
										
										
										
											2022-09-21 17:23:45 +02:00
										 |  |  | import { ApiService } from '../../services/api.service'; | 
					
						
							|  |  |  | import { SeoService } from '../../services/seo.service'; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  | import { formatNumber } from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2022-11-28 11:55:23 +09:00
										 |  |  | import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; | 
					
						
							| 
									
										
										
										
											2022-09-21 17:23:45 +02:00
										 |  |  | import { StorageService } from '../../services/storage.service'; | 
					
						
							|  |  |  | import { MiningService } from '../../services/mining.service'; | 
					
						
							| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  | import { ActivatedRoute } from '@angular/router'; | 
					
						
							| 
									
										
										
										
											2022-09-21 17:23:45 +02:00
										 |  |  | import { download, formatterXAxis } from '../../shared/graphs.utils'; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'app-block-sizes-weights-graph', | 
					
						
							|  |  |  |   templateUrl: './block-sizes-weights-graph.component.html', | 
					
						
							|  |  |  |   styleUrls: ['./block-sizes-weights-graph.component.scss'], | 
					
						
							|  |  |  |   styles: [`
 | 
					
						
							|  |  |  |     .loadingGraphs { | 
					
						
							|  |  |  |       position: absolute; | 
					
						
							|  |  |  |       top: 50%; | 
					
						
							|  |  |  |       left: calc(50% - 15px); | 
					
						
							|  |  |  |       z-index: 100; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   `],
 | 
					
						
							|  |  |  |   changeDetection: ChangeDetectionStrategy.OnPush, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | export class BlockSizesWeightsGraphComponent implements OnInit { | 
					
						
							|  |  |  |   @Input() right: number | string = 45; | 
					
						
							|  |  |  |   @Input() left: number | string = 75; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   miningWindowPreference: string; | 
					
						
							| 
									
										
										
										
											2022-11-28 11:55:23 +09:00
										 |  |  |   radioGroupForm: UntypedFormGroup; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   chartOptions: EChartsOption = {}; | 
					
						
							|  |  |  |   chartInitOptions = { | 
					
						
							|  |  |  |     renderer: 'svg', | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @HostBinding('attr.dir') dir = 'ltr'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   blockSizesWeightsObservable$: Observable<any>; | 
					
						
							|  |  |  |   isLoading = true; | 
					
						
							|  |  |  |   formatNumber = formatNumber; | 
					
						
							|  |  |  |   timespan = ''; | 
					
						
							|  |  |  |   chartInstance: any = undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							|  |  |  |     @Inject(LOCALE_ID) public locale: string, | 
					
						
							|  |  |  |     private seoService: SeoService, | 
					
						
							|  |  |  |     private apiService: ApiService, | 
					
						
							| 
									
										
										
										
											2022-11-28 11:55:23 +09:00
										 |  |  |     private formBuilder: UntypedFormBuilder, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |     private storageService: StorageService, | 
					
						
							|  |  |  |     private miningService: MiningService, | 
					
						
							| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  |     private route: ActivatedRoute, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |   ) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngOnInit(): void { | 
					
						
							|  |  |  |     let firstRun = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 17:05:07 +04:00
										 |  |  |     this.seoService.setTitle($localize`:@@56fa1cd221491b6478998679cba2dc8d55ba330d:Block Sizes and Weights`); | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |     this.miningWindowPreference = this.miningService.getDefaultTimespan('24h'); | 
					
						
							|  |  |  |     this.radioGroupForm = this.formBuilder.group({ dateSpan: this.miningWindowPreference }); | 
					
						
							|  |  |  |     this.radioGroupForm.controls.dateSpan.setValue(this.miningWindowPreference); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  |     this.route | 
					
						
							|  |  |  |       .fragment | 
					
						
							|  |  |  |       .subscribe((fragment) => { | 
					
						
							|  |  |  |         if (['24h', '3d', '1w', '1m', '3m', '6m', '1y', '2y', '3y', 'all'].indexOf(fragment) > -1) { | 
					
						
							| 
									
										
										
										
											2022-06-23 15:30:42 +02:00
										 |  |  |           this.radioGroupForm.controls.dateSpan.setValue(fragment, { emitEvent: false }); | 
					
						
							| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |     this.blockSizesWeightsObservable$ = this.radioGroupForm.get('dateSpan').valueChanges | 
					
						
							|  |  |  |       .pipe( | 
					
						
							| 
									
										
										
										
											2022-06-15 11:26:58 +02:00
										 |  |  |         startWith(this.radioGroupForm.controls.dateSpan.value), | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |         switchMap((timespan) => { | 
					
						
							|  |  |  |           this.timespan = timespan; | 
					
						
							|  |  |  |           if (!firstRun) { | 
					
						
							|  |  |  |             this.storageService.setValue('miningWindowPreference', timespan); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           firstRun = false; | 
					
						
							|  |  |  |           this.miningWindowPreference = timespan; | 
					
						
							|  |  |  |           this.isLoading = true; | 
					
						
							|  |  |  |           return this.apiService.getHistoricalBlockSizesAndWeights$(timespan) | 
					
						
							|  |  |  |             .pipe( | 
					
						
							|  |  |  |               tap((response) => { | 
					
						
							|  |  |  |                 const data = response.body; | 
					
						
							|  |  |  |                 this.prepareChartOptions({ | 
					
						
							| 
									
										
										
										
											2022-05-20 09:44:29 +02:00
										 |  |  |                   sizes: data.sizes.map(val => [val.timestamp * 1000, val.avgSize / 1000000, val.avgHeight]), | 
					
						
							|  |  |  |                   weights: data.weights.map(val => [val.timestamp * 1000, val.avgWeight / 1000000, val.avgHeight]), | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |                 }); | 
					
						
							|  |  |  |                 this.isLoading = false; | 
					
						
							|  |  |  |               }), | 
					
						
							|  |  |  |               map((response) => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                   blockCount: parseInt(response.headers.get('x-total-count'), 10), | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |               }), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         share() | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   prepareChartOptions(data) { | 
					
						
							|  |  |  |     let title: object; | 
					
						
							|  |  |  |     if (data.sizes.length === 0) { | 
					
						
							|  |  |  |       title = { | 
					
						
							|  |  |  |         textStyle: { | 
					
						
							|  |  |  |           color: 'grey', | 
					
						
							|  |  |  |           fontSize: 15 | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2022-06-10 23:29:27 +02:00
										 |  |  |         text: $localize`:@@23555386d8af1ff73f297e89dd4af3f4689fb9dd:Indexing blocks`, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |         left: 'center', | 
					
						
							|  |  |  |         top: 'center' | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.chartOptions = { | 
					
						
							|  |  |  |       title: title, | 
					
						
							|  |  |  |       animation: false, | 
					
						
							|  |  |  |       color: [ | 
					
						
							|  |  |  |         '#FDD835', | 
					
						
							|  |  |  |         '#D81B60', | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       grid: { | 
					
						
							|  |  |  |         top: 30, | 
					
						
							|  |  |  |         bottom: 70, | 
					
						
							|  |  |  |         right: this.right, | 
					
						
							|  |  |  |         left: this.left, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       tooltip: { | 
					
						
							|  |  |  |         show: !this.isMobile(), | 
					
						
							|  |  |  |         trigger: 'axis', | 
					
						
							|  |  |  |         axisPointer: { | 
					
						
							|  |  |  |           type: 'line' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         backgroundColor: 'rgba(17, 19, 31, 1)', | 
					
						
							|  |  |  |         borderRadius: 4, | 
					
						
							|  |  |  |         shadowColor: 'rgba(0, 0, 0, 0.5)', | 
					
						
							|  |  |  |         textStyle: { | 
					
						
							|  |  |  |           color: '#b1b1b1', | 
					
						
							|  |  |  |           align: 'left', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         borderColor: '#000', | 
					
						
							|  |  |  |         formatter: (ticks) => { | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |           let tooltip = `<b style="color: white; margin-left: 2px">${formatterXAxis(this.locale, this.timespan, parseInt(ticks[0].axisValue, 10))}</b><br>`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           for (const tick of ticks) { | 
					
						
							|  |  |  |             if (tick.seriesIndex === 0) { // Size
 | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |               tooltip += `${tick.marker} ${tick.seriesName}: ${formatNumber(tick.data[1], this.locale, '1.2-2')} MB`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |             } else if (tick.seriesIndex === 1) { // Weight
 | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |               tooltip += `${tick.marker} ${tick.seriesName}: ${formatNumber(tick.data[1], this.locale, '1.2-2')} MWU`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |             tooltip += `<br>`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (['24h', '3d'].includes(this.timespan)) { | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |             tooltip += `<small>` + $localize`At block: ${ticks[0].data[2]}` + `</small>`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2022-06-10 23:34:13 +02:00
										 |  |  |             tooltip += `<small>` + $localize`Around block: ${ticks[0].data[2]}` + `</small>`; | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return tooltip; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       xAxis: data.sizes.length === 0 ? undefined : { | 
					
						
							|  |  |  |         type: 'time', | 
					
						
							|  |  |  |         splitNumber: this.isMobile() ? 5 : 10, | 
					
						
							|  |  |  |         axisLabel: { | 
					
						
							|  |  |  |           hideOverlap: true, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       legend: data.sizes.length === 0 ? undefined : { | 
					
						
							|  |  |  |         padding: 10, | 
					
						
							|  |  |  |         data: [ | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2022-05-18 03:06:31 +04:00
										 |  |  |             name: $localize`:@@7faaaa08f56427999f3be41df1093ce4089bbd75:Size`, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |             inactiveColor: 'rgb(110, 112, 121)', | 
					
						
							|  |  |  |             textStyle: { | 
					
						
							|  |  |  |               color: 'white', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             icon: 'roundRect', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2022-05-18 03:06:31 +04:00
										 |  |  |             name: $localize`:@@919f2fd60a898850c24b1584362bbf18a4628bcb:Weight`, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |             inactiveColor: 'rgb(110, 112, 121)', | 
					
						
							|  |  |  |             textStyle: { | 
					
						
							|  |  |  |               color: 'white', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             icon: 'roundRect', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         selected: JSON.parse(this.storageService.getValue('sizes_weights_legend'))  ?? { | 
					
						
							|  |  |  |           'Size': true, | 
					
						
							|  |  |  |           'Weight': true, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       yAxis: data.sizes.length === 0 ? undefined : [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           type: 'value', | 
					
						
							|  |  |  |           position: 'left', | 
					
						
							|  |  |  |           min: (value) => { | 
					
						
							|  |  |  |             return value.min * 0.9; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           axisLabel: { | 
					
						
							|  |  |  |             color: 'rgb(110, 112, 121)', | 
					
						
							|  |  |  |             formatter: (val) => { | 
					
						
							|  |  |  |               return `${Math.round(val * 100) / 100} MWU`; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           splitLine: { | 
					
						
							|  |  |  |             lineStyle: { | 
					
						
							|  |  |  |               type: 'dotted', | 
					
						
							|  |  |  |               color: '#ffffff66', | 
					
						
							|  |  |  |               opacity: 0.25, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       series: data.sizes.length === 0 ? [] : [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           zlevel: 1, | 
					
						
							| 
									
										
										
										
											2022-05-18 03:06:31 +04:00
										 |  |  |           name: $localize`:@@7faaaa08f56427999f3be41df1093ce4089bbd75:Size`, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |           showSymbol: false, | 
					
						
							|  |  |  |           symbol: 'none', | 
					
						
							|  |  |  |           data: data.sizes, | 
					
						
							|  |  |  |           type: 'line', | 
					
						
							|  |  |  |           lineStyle: { | 
					
						
							|  |  |  |             width: 2, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           markLine: { | 
					
						
							|  |  |  |             silent: true, | 
					
						
							|  |  |  |             symbol: 'none', | 
					
						
							|  |  |  |             lineStyle: { | 
					
						
							|  |  |  |               type: 'solid', | 
					
						
							|  |  |  |               color: '#ffffff66', | 
					
						
							|  |  |  |               opacity: 1, | 
					
						
							|  |  |  |               width: 1, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             data: [{ | 
					
						
							|  |  |  |               yAxis: 1, | 
					
						
							|  |  |  |               label: { | 
					
						
							|  |  |  |                 position: 'end', | 
					
						
							|  |  |  |                 show: true, | 
					
						
							|  |  |  |                 color: '#ffffff', | 
					
						
							|  |  |  |                 formatter: `1 MB` | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             }], | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           zlevel: 1, | 
					
						
							|  |  |  |           yAxisIndex: 0, | 
					
						
							| 
									
										
										
										
											2022-05-18 03:06:31 +04:00
										 |  |  |           name: $localize`:@@919f2fd60a898850c24b1584362bbf18a4628bcb:Weight`, | 
					
						
							| 
									
										
										
										
											2022-05-10 16:40:56 +02:00
										 |  |  |           showSymbol: false, | 
					
						
							|  |  |  |           symbol: 'none', | 
					
						
							|  |  |  |           data: data.weights, | 
					
						
							|  |  |  |           type: 'line', | 
					
						
							|  |  |  |           lineStyle: { | 
					
						
							|  |  |  |             width: 2, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       dataZoom: [{ | 
					
						
							|  |  |  |         type: 'inside', | 
					
						
							|  |  |  |         realtime: true, | 
					
						
							|  |  |  |         zoomLock: true, | 
					
						
							|  |  |  |         maxSpan: 100, | 
					
						
							|  |  |  |         minSpan: 5, | 
					
						
							|  |  |  |         moveOnMouseMove: false, | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         showDetail: false, | 
					
						
							|  |  |  |         show: true, | 
					
						
							|  |  |  |         type: 'slider', | 
					
						
							|  |  |  |         brushSelect: false, | 
					
						
							|  |  |  |         realtime: true, | 
					
						
							|  |  |  |         left: 20, | 
					
						
							|  |  |  |         right: 15, | 
					
						
							|  |  |  |         selectedDataBackground: { | 
					
						
							|  |  |  |           lineStyle: { | 
					
						
							|  |  |  |             color: '#fff', | 
					
						
							|  |  |  |             opacity: 0.45, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           areaStyle: { | 
					
						
							|  |  |  |             opacity: 0, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }], | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onChartInit(ec) { | 
					
						
							|  |  |  |     if (this.chartInstance !== undefined) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.chartInstance = ec; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.chartInstance.on('legendselectchanged', (e) => { | 
					
						
							|  |  |  |       this.storageService.setValue('sizes_weights_legend', JSON.stringify(e.selected)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   isMobile() { | 
					
						
							|  |  |  |     return (window.innerWidth <= 767.98); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onSaveChart() { | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							|  |  |  |     const prevBottom = this.chartOptions.grid.bottom; | 
					
						
							|  |  |  |     const now = new Date(); | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							|  |  |  |     this.chartOptions.grid.bottom = 40; | 
					
						
							|  |  |  |     this.chartOptions.backgroundColor = '#11131f'; | 
					
						
							|  |  |  |     this.chartInstance.setOption(this.chartOptions); | 
					
						
							|  |  |  |     download(this.chartInstance.getDataURL({ | 
					
						
							|  |  |  |       pixelRatio: 2, | 
					
						
							|  |  |  |       excludeComponents: ['dataZoom'], | 
					
						
							|  |  |  |     }), `block-sizes-weights-${this.timespan}-${Math.round(now.getTime() / 1000)}.svg`); | 
					
						
							|  |  |  |     // @ts-ignore
 | 
					
						
							|  |  |  |     this.chartOptions.grid.bottom = prevBottom; | 
					
						
							|  |  |  |     this.chartOptions.backgroundColor = 'none'; | 
					
						
							|  |  |  |     this.chartInstance.setOption(this.chartOptions); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |