interface.jsx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. class Interface extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. realtime: this.getCookie(),
  6. resetting: false,
  7. opstate: props.opstate
  8. }
  9. this.polling = false;
  10. this.isSecure = (window.location.protocol === 'https:');
  11. if (this.getCookie()) {
  12. this.startTimer();
  13. }
  14. }
  15. startTimer = () => {
  16. this.setState({realtime: true})
  17. this.polling = setInterval(() => {
  18. this.setState({fetching: true, resetting: false});
  19. axios.get(window.location.pathname, {time: Date.now()})
  20. .then((response) => {
  21. this.setState({opstate: response.data});
  22. });
  23. }, this.props.realtimeRefresh * 1000);
  24. }
  25. stopTimer = () => {
  26. this.setState({realtime: false, resetting: false})
  27. clearInterval(this.polling)
  28. }
  29. realtimeHandler = () => {
  30. const realtime = !this.state.realtime;
  31. if (!realtime) {
  32. this.stopTimer();
  33. this.removeCookie();
  34. } else {
  35. this.startTimer();
  36. this.setCookie();
  37. }
  38. }
  39. resetHandler = () => {
  40. if (this.state.realtime) {
  41. this.setState({resetting: true});
  42. axios.get(window.location.pathname, {params: {reset: 1}})
  43. .then((response) => {
  44. console.log('success: ', response.data);
  45. });
  46. } else {
  47. window.location.href = '?reset=1';
  48. }
  49. }
  50. setCookie = () => {
  51. let d = new Date();
  52. d.setTime(d.getTime() + (this.props.cookie.ttl * 86400000));
  53. document.cookie = `${this.props.cookie.name}=true;expires=${d.toUTCString()};path=/${this.isSecure ? ';secure' : ''}`;
  54. }
  55. removeCookie = () => {
  56. document.cookie = `${this.props.cookie.name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/${this.isSecure ? ';secure' : ''}`;
  57. }
  58. getCookie = () => {
  59. const v = document.cookie.match(`(^|;) ?${this.props.cookie.name}=([^;]*)(;|$)`);
  60. return v ? !!v[2] : false;
  61. };
  62. render() {
  63. const { opstate, realtimeRefresh, ...otherProps } = this.props;
  64. return (
  65. <>
  66. <header>
  67. <MainNavigation {...otherProps}
  68. opstate={this.state.opstate}
  69. realtime={this.state.realtime}
  70. resetting={this.state.resetting}
  71. realtimeHandler={this.realtimeHandler}
  72. resetHandler={this.resetHandler}
  73. />
  74. </header>
  75. <Footer version={this.props.opstate.version.gui} />
  76. </>
  77. );
  78. }
  79. }
  80. function MainNavigation(props) {
  81. return (
  82. <nav className="main-nav">
  83. <Tabs>
  84. <div label="Overview" tabId="overview" tabIndex={1}>
  85. <OverviewCounts
  86. overview={props.opstate.overview}
  87. highlight={props.highlight}
  88. useCharts={props.useCharts}
  89. />
  90. <div id="info" className="tab-content-overview-info">
  91. <GeneralInfo
  92. start={props.opstate.overview && props.opstate.overview.readable.start_time || null}
  93. reset={props.opstate.overview && props.opstate.overview.readable.last_restart_time || null}
  94. version={props.opstate.version}
  95. />
  96. <Directives
  97. directives={props.opstate.directives}
  98. />
  99. <Functions
  100. functions={props.opstate.functions}
  101. />
  102. </div>
  103. </div>
  104. {
  105. props.allow.filelist &&
  106. <div label="Cached" tabId="cached" tabIndex={2}>
  107. <CachedFiles
  108. perPageLimit={props.perPageLimit}
  109. allFiles={props.opstate.files}
  110. searchTerm={props.searchTerm}
  111. debounceRate={props.debounceRate}
  112. allow={{fileList: props.allow.filelist, invalidate: props.allow.invalidate}}
  113. realtime={props.realtime}
  114. />
  115. </div>
  116. }
  117. {
  118. (props.allow.filelist && props.opstate.blacklist.length &&
  119. <div label="Ignored" tabId="ignored" tabIndex={3}>
  120. <IgnoredFiles
  121. perPageLimit={props.perPageLimit}
  122. allFiles={props.opstate.blacklist}
  123. allow={{fileList: props.allow.filelist }}
  124. />
  125. </div>)
  126. }
  127. {
  128. (props.allow.filelist && props.opstate.preload.length &&
  129. <div label="Preloaded" tabId="preloaded" tabIndex={4}>
  130. <PreloadedFiles
  131. perPageLimit={props.perPageLimit}
  132. allFiles={props.opstate.preload}
  133. allow={{fileList: props.allow.filelist }}
  134. />
  135. </div>)
  136. }
  137. {
  138. props.allow.reset &&
  139. <div label="Reset cache" tabId="resetCache"
  140. className={`nav-tab-link-reset${props.resetting ? ' is-resetting pulse' : ''}`}
  141. handler={props.resetHandler}
  142. tabIndex={5}
  143. ></div>
  144. }
  145. {
  146. props.allow.realtime &&
  147. <div label={`${props.realtime ? 'Disable' : 'Enable'} real-time update`} tabId="toggleRealtime"
  148. className={`nav-tab-link-realtime${props.realtime ? ' live-update pulse' : ''}`}
  149. handler={props.realtimeHandler}
  150. tabIndex={6}
  151. ></div>
  152. }
  153. </Tabs>
  154. </nav>
  155. );
  156. }
  157. class Tabs extends React.Component {
  158. constructor(props) {
  159. super(props);
  160. this.state = {
  161. activeTab: this.props.children[0].props.label,
  162. };
  163. }
  164. onClickTabItem = (tab) => {
  165. this.setState({ activeTab: tab });
  166. }
  167. render() {
  168. const {
  169. onClickTabItem,
  170. state: { activeTab }
  171. } = this;
  172. const children = this.props.children.filter(Boolean);
  173. return (
  174. <>
  175. <ul className="nav-tab-list">
  176. {children.map((child) => {
  177. const { tabId, label, className, handler, tabIndex } = child.props;
  178. return (
  179. <Tab
  180. activeTab={activeTab}
  181. key={tabId}
  182. label={label}
  183. onClick={handler || onClickTabItem}
  184. className={className}
  185. tabIndex={tabIndex}
  186. tabId={tabId}
  187. />
  188. );
  189. })}
  190. </ul>
  191. <div className="tab-content">
  192. {children.map((child) => (
  193. <div key={child.props.label}
  194. style={{ display: child.props.label === activeTab ? 'block' : 'none' }}
  195. id={`${child.props.tabId}-content`}
  196. >
  197. {child.props.children}
  198. </div>
  199. ))}
  200. </div>
  201. </>
  202. );
  203. }
  204. }
  205. class Tab extends React.Component {
  206. onClick = () => {
  207. const { label, onClick } = this.props;
  208. onClick(label);
  209. }
  210. render() {
  211. const {
  212. onClick,
  213. props: { activeTab, label, tabIndex, tabId },
  214. } = this;
  215. let className = 'nav-tab';
  216. if (this.props.className) {
  217. className += ` ${this.props.className}`;
  218. }
  219. if (activeTab === label) {
  220. className += ' active';
  221. }
  222. return (
  223. <li className={className}
  224. onClick={onClick}
  225. tabIndex={tabIndex}
  226. role="tab"
  227. aria-controls={`${tabId}-content`}
  228. >{label}</li>
  229. );
  230. }
  231. }
  232. function OverviewCounts(props) {
  233. if (props.overview === false) {
  234. return (
  235. <p class="file-cache-only">
  236. You have <i>opcache.file_cache_only</i> turned on. As a result, the memory information is not available. Statistics and file list may also not be returned by <i>opcache_get_statistics()</i>.
  237. </p>
  238. );
  239. }
  240. const graphList = [
  241. {id: 'memoryUsageCanvas', title: 'memory', show: props.highlight.memory, value: props.overview.used_memory_percentage},
  242. {id: 'hitRateCanvas', title: 'hit rate', show: props.highlight.hits, value: props.overview.hit_rate_percentage},
  243. {id: 'keyUsageCanvas', title: 'keys', show: props.highlight.keys, value: props.overview.used_key_percentage},
  244. {id: 'jitUsageCanvas', title: 'jit buffer', show: props.highlight.jit, value: props.overview.jit_buffer_used_percentage}
  245. ];
  246. return (
  247. <div id="counts" className="tab-content-overview-counts">
  248. {graphList.map((graph) => {
  249. if (!graph.show) {
  250. return null;
  251. }
  252. return (
  253. <div className="widget-panel" key={graph.id}>
  254. <h3 className="widget-header">{graph.title}</h3>
  255. <UsageGraph charts={props.useCharts} value={graph.value} gaugeId={graph.id} />
  256. </div>
  257. );
  258. })}
  259. <MemoryUsagePanel
  260. total={props.overview.readable.total_memory}
  261. used={props.overview.readable.used_memory}
  262. free={props.overview.readable.free_memory}
  263. wasted={props.overview.readable.wasted_memory}
  264. preload={props.overview.readable.preload_memory || null}
  265. wastedPercent={props.overview.wasted_percentage}
  266. jitBuffer={props.overview.readable.jit_buffer_size || null}
  267. jitBufferFree={props.overview.readable.jit_buffer_free || null}
  268. jitBufferFreePercentage={props.overview.jit_buffer_used_percentage || null}
  269. />
  270. <StatisticsPanel
  271. num_cached_scripts={props.overview.readable.num_cached_scripts}
  272. hits={props.overview.readable.hits}
  273. misses={props.overview.readable.misses}
  274. blacklist_miss={props.overview.readable.blacklist_miss}
  275. num_cached_keys={props.overview.readable.num_cached_keys}
  276. max_cached_keys={props.overview.readable.max_cached_keys}
  277. />
  278. {props.overview.readable.interned &&
  279. <InternedStringsPanel
  280. buffer_size={props.overview.readable.interned.buffer_size}
  281. strings_used_memory={props.overview.readable.interned.strings_used_memory}
  282. strings_free_memory={props.overview.readable.interned.strings_free_memory}
  283. number_of_strings={props.overview.readable.interned.number_of_strings}
  284. />
  285. }
  286. </div>
  287. );
  288. }
  289. function GeneralInfo(props) {
  290. return (
  291. <table className="tables general-info-table">
  292. <thead>
  293. <tr><th colSpan="2">General info</th></tr>
  294. </thead>
  295. <tbody>
  296. <tr><td>Zend OPcache</td><td>{props.version.version}</td></tr>
  297. <tr><td>PHP</td><td>{props.version.php}</td></tr>
  298. <tr><td>Host</td><td>{props.version.host}</td></tr>
  299. <tr><td>Server Software</td><td>{props.version.server}</td></tr>
  300. { props.start ? <tr><td>Start time</td><td>{props.start}</td></tr> : null }
  301. { props.reset ? <tr><td>Last reset</td><td>{props.reset}</td></tr> : null }
  302. </tbody>
  303. </table>
  304. );
  305. }
  306. function Directives(props) {
  307. let directiveList = (directive) => {
  308. return (
  309. <ul className="directive-list">{
  310. directive.v.map((item, key) => {
  311. return Array.isArray(item)
  312. ? <li key={"sublist_" + key}>{directiveList({v:item})}</li>
  313. : <li key={key}>{item}</li>
  314. })
  315. }</ul>
  316. );
  317. };
  318. let directiveNodes = props.directives.map(function(directive) {
  319. let map = { 'opcache.':'', '_':' ' };
  320. let dShow = directive.k.replace(/opcache\.|_/gi, function(matched){
  321. return map[matched];
  322. });
  323. let vShow;
  324. if (directive.v === true || directive.v === false) {
  325. vShow = React.createElement('i', {}, directive.v.toString());
  326. } else if (directive.v === '') {
  327. vShow = React.createElement('i', {}, 'no value');
  328. } else {
  329. if (Array.isArray(directive.v)) {
  330. vShow = directiveList(directive);
  331. } else {
  332. vShow = directive.v;
  333. }
  334. }
  335. return (
  336. <tr key={directive.k}>
  337. <td title={'View ' + directive.k + ' manual entry'}><a href={'https://php.net/manual/en/opcache.configuration.php#ini.'
  338. + (directive.k).replace(/_/g,'-')} target="_blank">{dShow}</a></td>
  339. <td>{vShow}</td>
  340. </tr>
  341. );
  342. });
  343. return (
  344. <table className="tables directives-table">
  345. <thead><tr><th colSpan="2">Directives</th></tr></thead>
  346. <tbody>{directiveNodes}</tbody>
  347. </table>
  348. );
  349. }
  350. function Functions(props) {
  351. return (
  352. <div id="functions">
  353. <table className="tables">
  354. <thead><tr><th>Available functions</th></tr></thead>
  355. <tbody>
  356. {props.functions.map(f =>
  357. <tr key={f}><td><a href={"https://php.net/"+f} title="View manual page" target="_blank">{f}</a></td></tr>
  358. )}
  359. </tbody>
  360. </table>
  361. </div>
  362. );
  363. }
  364. function UsageGraph(props) {
  365. const percentage = Math.round(((3.6 * props.value)/360)*100);
  366. return (props.charts
  367. ? <ReactCustomizableProgressbar
  368. progress={percentage}
  369. radius={100}
  370. strokeWidth={30}
  371. trackStrokeWidth={30}
  372. strokeColor={getComputedStyle(document.documentElement).getPropertyValue('--opcache-gui-graph-track-fill-color') || "#6CA6EF"}
  373. trackStrokeColor={getComputedStyle(document.documentElement).getPropertyValue('--opcache-gui-graph-track-background-color') || "#CCC"}
  374. gaugeId={props.gaugeId}
  375. />
  376. : <p className="widget-value"><span className="large">{percentage}</span><span>%</span></p>
  377. );
  378. }
  379. /**
  380. * This component is from <https://github.com/martyan/react-customizable-progressbar/>
  381. * MIT License (MIT), Copyright (c) 2019 Martin Juzl
  382. */
  383. class ReactCustomizableProgressbar extends React.Component {
  384. constructor(props) {
  385. super(props);
  386. this.state = {
  387. animationInited: false
  388. };
  389. }
  390. componentDidMount() {
  391. const { initialAnimation, initialAnimationDelay } = this.props
  392. if (initialAnimation)
  393. setTimeout(this.initAnimation, initialAnimationDelay)
  394. }
  395. initAnimation = () => {
  396. this.setState({ animationInited: true })
  397. }
  398. getProgress = () => {
  399. const { initialAnimation, progress } = this.props
  400. const { animationInited } = this.state
  401. return initialAnimation && !animationInited ? 0 : progress
  402. }
  403. getStrokeDashoffset = strokeLength => {
  404. const { counterClockwise, inverse, steps } = this.props
  405. const progress = this.getProgress()
  406. const progressLength = (strokeLength / steps) * (steps - progress)
  407. if (inverse) return counterClockwise ? 0 : progressLength - strokeLength
  408. return counterClockwise ? -1 * progressLength : progressLength
  409. }
  410. getStrokeDashArray = (strokeLength, circumference) => {
  411. const { counterClockwise, inverse, steps } = this.props
  412. const progress = this.getProgress()
  413. const progressLength = (strokeLength / steps) * (steps - progress)
  414. if (inverse) return `${progressLength}, ${circumference}`
  415. return counterClockwise
  416. ? `${strokeLength * (progress / 100)}, ${circumference}`
  417. : `${strokeLength}, ${circumference}`
  418. }
  419. getTrackStrokeDashArray = (strokeLength, circumference) => {
  420. const { initialAnimation } = this.props
  421. const { animationInited } = this.state
  422. if (initialAnimation && !animationInited) return `0, ${circumference}`
  423. return `${strokeLength}, ${circumference}`
  424. }
  425. getExtendedWidth = () => {
  426. const {
  427. strokeWidth,
  428. pointerRadius,
  429. pointerStrokeWidth,
  430. trackStrokeWidth
  431. } = this.props
  432. const pointerWidth = pointerRadius + pointerStrokeWidth
  433. if (pointerWidth > strokeWidth && pointerWidth > trackStrokeWidth) return pointerWidth * 2
  434. else if (strokeWidth > trackStrokeWidth) return strokeWidth * 2
  435. else return trackStrokeWidth * 2
  436. }
  437. getPointerAngle = () => {
  438. const { cut, counterClockwise, steps } = this.props
  439. const progress = this.getProgress()
  440. return counterClockwise
  441. ? ((360 - cut) / steps) * (steps - progress)
  442. : ((360 - cut) / steps) * progress
  443. }
  444. render() {
  445. const {
  446. radius,
  447. pointerRadius,
  448. pointerStrokeWidth,
  449. pointerFillColor,
  450. pointerStrokeColor,
  451. fillColor,
  452. trackStrokeWidth,
  453. trackStrokeColor,
  454. trackStrokeLinecap,
  455. strokeColor,
  456. strokeWidth,
  457. strokeLinecap,
  458. rotate,
  459. cut,
  460. trackTransition,
  461. transition,
  462. progress
  463. } = this.props
  464. const d = 2 * radius
  465. const width = d + this.getExtendedWidth()
  466. const circumference = 2 * Math.PI * radius
  467. const strokeLength = (circumference / 360) * (360 - cut)
  468. return (
  469. <figure
  470. className={`graph-widget`}
  471. style={{width: `${width || 250}px`}}
  472. data-value={progress}
  473. id={this.props.guageId}
  474. >
  475. <svg width={width} height={width}
  476. viewBox={`0 0 ${width} ${width}`}
  477. style={{ transform: `rotate(${rotate}deg)` }}
  478. >
  479. {trackStrokeWidth > 0 && (
  480. <circle
  481. cx={width / 2}
  482. cy={width / 2}
  483. r={radius}
  484. fill="none"
  485. stroke={trackStrokeColor}
  486. strokeWidth={trackStrokeWidth}
  487. strokeDasharray={this.getTrackStrokeDashArray(
  488. strokeLength,
  489. circumference
  490. )}
  491. strokeLinecap={trackStrokeLinecap}
  492. style={{ transition: trackTransition }}
  493. />
  494. )}
  495. {strokeWidth > 0 && (
  496. <circle
  497. cx={width / 2}
  498. cy={width / 2}
  499. r={radius}
  500. fill={fillColor}
  501. stroke={strokeColor}
  502. strokeWidth={strokeWidth}
  503. strokeDasharray={this.getStrokeDashArray(
  504. strokeLength,
  505. circumference
  506. )}
  507. strokeDashoffset={this.getStrokeDashoffset(
  508. strokeLength
  509. )}
  510. strokeLinecap={strokeLinecap}
  511. style={{ transition }}
  512. />
  513. )}
  514. {pointerRadius > 0 && (
  515. <circle
  516. cx={d}
  517. cy="50%"
  518. r={pointerRadius}
  519. fill={pointerFillColor}
  520. stroke={pointerStrokeColor}
  521. strokeWidth={pointerStrokeWidth}
  522. style={{
  523. transformOrigin: '50% 50%',
  524. transform: `rotate(${this.getPointerAngle()}deg) translate(${this.getExtendedWidth() /
  525. 2}px)`,
  526. transition
  527. }}
  528. />
  529. )}
  530. </svg>
  531. <figcaption className={`widget-value`}>
  532. {progress}%
  533. </figcaption>
  534. </figure>
  535. )
  536. }
  537. }
  538. ReactCustomizableProgressbar.defaultProps = {
  539. radius: 100,
  540. progress: 0,
  541. steps: 100,
  542. cut: 0,
  543. rotate: -90,
  544. strokeWidth: 20,
  545. strokeColor: 'indianred',
  546. fillColor: 'none',
  547. strokeLinecap: 'round',
  548. transition: '.3s ease',
  549. pointerRadius: 0,
  550. pointerStrokeWidth: 20,
  551. pointerStrokeColor: 'indianred',
  552. pointerFillColor: 'white',
  553. trackStrokeColor: '#e6e6e6',
  554. trackStrokeWidth: 20,
  555. trackStrokeLinecap: 'round',
  556. trackTransition: '.3s ease',
  557. counterClockwise: false,
  558. inverse: false,
  559. initialAnimation: false,
  560. initialAnimationDelay: 0
  561. };
  562. function MemoryUsagePanel(props) {
  563. return (
  564. <div className="widget-panel">
  565. <h3 className="widget-header">memory usage</h3>
  566. <div className="widget-value widget-info">
  567. <p><b>total memory:</b> {props.total}</p>
  568. <p><b>used memory:</b> {props.used}</p>
  569. <p><b>free memory:</b> {props.free}</p>
  570. { props.preload && <p><b>preload memory:</b> {props.preload}</p> }
  571. <p><b>wasted memory:</b> {props.wasted} ({props.wastedPercent}%)</p>
  572. { props.jitBuffer && <p><b>jit buffer:</b> {props.jitBuffer}</p> }
  573. { props.jitBufferFree && <p><b>jit buffer free:</b> {props.jitBufferFree} ({100 - props.jitBufferFreePercentage}%)</p> }
  574. </div>
  575. </div>
  576. );
  577. }
  578. function StatisticsPanel(props) {
  579. return (
  580. <div className="widget-panel">
  581. <h3 className="widget-header">opcache statistics</h3>
  582. <div className="widget-value widget-info">
  583. <p><b>number of cached files:</b> {props.num_cached_scripts}</p>
  584. <p><b>number of hits:</b> {props.hits}</p>
  585. <p><b>number of misses:</b> {props.misses}</p>
  586. <p><b>blacklist misses:</b> {props.blacklist_miss}</p>
  587. <p><b>number of cached keys:</b> {props.num_cached_keys}</p>
  588. <p><b>max cached keys:</b> {props.max_cached_keys}</p>
  589. </div>
  590. </div>
  591. );
  592. }
  593. function InternedStringsPanel(props) {
  594. return (
  595. <div className="widget-panel">
  596. <h3 className="widget-header">interned strings usage</h3>
  597. <div className="widget-value widget-info">
  598. <p><b>buffer size:</b> {props.buffer_size}</p>
  599. <p><b>used memory:</b> {props.strings_used_memory}</p>
  600. <p><b>free memory:</b> {props.strings_free_memory}</p>
  601. <p><b>number of strings:</b> {props.number_of_strings}</p>
  602. </div>
  603. </div>
  604. );
  605. }
  606. class CachedFiles extends React.Component {
  607. constructor(props) {
  608. super(props);
  609. this.doPagination = (typeof props.perPageLimit === "number"
  610. && props.perPageLimit > 0
  611. );
  612. this.state = {
  613. currentPage: 1,
  614. searchTerm: props.searchTerm,
  615. refreshPagination: 0,
  616. sortBy: `last_used_timestamp`,
  617. sortDir: `desc`
  618. }
  619. }
  620. setSearchTerm = debounce(searchTerm => {
  621. this.setState({
  622. searchTerm,
  623. refreshPagination: !(this.state.refreshPagination)
  624. });
  625. }, this.props.debounceRate);
  626. onPageChanged = currentPage => {
  627. this.setState({ currentPage });
  628. }
  629. handleInvalidate = e => {
  630. e.preventDefault();
  631. if (this.props.realtime) {
  632. axios.get(window.location.pathname, {params: { invalidate_searched: this.state.searchTerm }})
  633. .then((response) => {
  634. console.log('success: ' , response.data);
  635. });
  636. } else {
  637. window.location.href = e.currentTarget.href;
  638. }
  639. }
  640. changeSort = e => {
  641. this.setState({ [e.target.name]: e.target.value });
  642. }
  643. compareValues = (key, order = 'asc') => {
  644. return function innerSort(a, b) {
  645. if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
  646. return 0;
  647. }
  648. const varA = (typeof a[key] === 'string') ? a[key].toUpperCase() : a[key];
  649. const varB = (typeof b[key] === 'string') ? b[key].toUpperCase() : b[key];
  650. let comparison = 0;
  651. if (varA > varB) {
  652. comparison = 1;
  653. } else if (varA < varB) {
  654. comparison = -1;
  655. }
  656. return (
  657. (order === 'desc') ? (comparison * -1) : comparison
  658. );
  659. };
  660. }
  661. render() {
  662. if (!this.props.allow.fileList) {
  663. return null;
  664. }
  665. if (this.props.allFiles.length === 0) {
  666. return <p>No files have been cached or you have <i>opcache.file_cache_only</i> turned on</p>;
  667. }
  668. const { searchTerm, currentPage } = this.state;
  669. const offset = (currentPage - 1) * this.props.perPageLimit;
  670. const filesInSearch = (searchTerm
  671. ? this.props.allFiles.filter(file => {
  672. return !(file.full_path.indexOf(searchTerm) === -1);
  673. })
  674. : this.props.allFiles
  675. );
  676. filesInSearch.sort(this.compareValues(this.state.sortBy, this.state.sortDir));
  677. const filesInPage = (this.doPagination
  678. ? filesInSearch.slice(offset, offset + this.props.perPageLimit)
  679. : filesInSearch
  680. );
  681. const allFilesTotal = this.props.allFiles.length;
  682. const showingTotal = filesInSearch.length;
  683. return (
  684. <div>
  685. <form action="#">
  686. <label htmlFor="frmFilter">Start typing to filter on script path</label><br/>
  687. <input type="text" name="filter" id="frmFilter" className="file-filter" onChange={e => {this.setSearchTerm(e.target.value)}} />
  688. </form>
  689. <h3>{allFilesTotal} files cached{showingTotal !== allFilesTotal && `, ${showingTotal} showing due to filter '${this.state.searchTerm}'`}</h3>
  690. { this.props.allow.invalidate && this.state.searchTerm && showingTotal !== allFilesTotal &&
  691. <p><a href={`?invalidate_searched=${encodeURIComponent(this.state.searchTerm)}`} onClick={this.handleInvalidate}>Invalidate all matching files</a></p>
  692. }
  693. <div className="paginate-filter">
  694. {this.doPagination && <Pagination
  695. totalRecords={filesInSearch.length}
  696. pageLimit={this.props.perPageLimit}
  697. pageNeighbours={2}
  698. onPageChanged={this.onPageChanged}
  699. refresh={this.state.refreshPagination}
  700. />}
  701. <nav className="filter" aria-label="Sort order">
  702. <select name="sortBy" onChange={this.changeSort} value={this.state.sortBy}>
  703. <option value="last_used_timestamp">Last used</option>
  704. <option value="last_modified">Last modified</option>
  705. <option value="full_path">Path</option>
  706. <option value="hits">Number of hits</option>
  707. <option value="memory_consumption">Memory consumption</option>
  708. </select>
  709. <select name="sortDir" onChange={this.changeSort} value={this.state.sortDir}>
  710. <option value="desc">Descending</option>
  711. <option value="asc">Ascending</option>
  712. </select>
  713. </nav>
  714. </div>
  715. <table className="tables cached-list-table">
  716. <thead>
  717. <tr>
  718. <th>Script</th>
  719. </tr>
  720. </thead>
  721. <tbody>
  722. {filesInPage.map((file, index) => {
  723. return <CachedFile
  724. key={file.full_path}
  725. canInvalidate={this.props.allow.invalidate}
  726. realtime={this.props.realtime}
  727. {...file}
  728. />
  729. })}
  730. </tbody>
  731. </table>
  732. </div>
  733. );
  734. }
  735. }
  736. class CachedFile extends React.Component {
  737. handleInvalidate = e => {
  738. e.preventDefault();
  739. if (this.props.realtime) {
  740. axios.get(window.location.pathname, {params: { invalidate: e.currentTarget.getAttribute('data-file') }})
  741. .then((response) => {
  742. console.log('success: ' , response.data);
  743. });
  744. } else {
  745. window.location.href = e.currentTarget.href;
  746. }
  747. }
  748. render() {
  749. return (
  750. <tr data-path={this.props.full_path.toLowerCase()}>
  751. <td>
  752. <span className="file-pathname">{this.props.full_path}</span>
  753. <span className="file-metainfo">
  754. <b>hits: </b><span>{this.props.readable.hits}, </span>
  755. <b>memory: </b><span>{this.props.readable.memory_consumption}, </span>
  756. { this.props.last_modified && <><b>last modified: </b><span>{this.props.last_modified}, </span></> }
  757. <b>last used: </b><span>{this.props.last_used}</span>
  758. </span>
  759. { !this.props.timestamp && <span className="invalid file-metainfo"> - has been invalidated</span> }
  760. { this.props.canInvalidate && <span>,&nbsp;<a className="file-metainfo"
  761. href={'?invalidate=' + this.props.full_path} data-file={this.props.full_path}
  762. onClick={this.handleInvalidate}>force file invalidation</a></span> }
  763. </td>
  764. </tr>
  765. );
  766. }
  767. }
  768. class IgnoredFiles extends React.Component {
  769. constructor(props) {
  770. super(props);
  771. this.doPagination = (typeof props.perPageLimit === "number"
  772. && props.perPageLimit > 0
  773. );
  774. this.state = {
  775. currentPage: 1,
  776. refreshPagination: 0
  777. }
  778. }
  779. onPageChanged = currentPage => {
  780. this.setState({ currentPage });
  781. }
  782. render() {
  783. if (!this.props.allow.fileList) {
  784. return null;
  785. }
  786. if (this.props.allFiles.length === 0) {
  787. return <p>No files have been ignored via <i>opcache.blacklist_filename</i></p>;
  788. }
  789. const { currentPage } = this.state;
  790. const offset = (currentPage - 1) * this.props.perPageLimit;
  791. const filesInPage = (this.doPagination
  792. ? this.props.allFiles.slice(offset, offset + this.props.perPageLimit)
  793. : this.props.allFiles
  794. );
  795. const allFilesTotal = this.props.allFiles.length;
  796. return (
  797. <div>
  798. <h3>{allFilesTotal} ignore file locations</h3>
  799. {this.doPagination && <Pagination
  800. totalRecords={allFilesTotal}
  801. pageLimit={this.props.perPageLimit}
  802. pageNeighbours={2}
  803. onPageChanged={this.onPageChanged}
  804. refresh={this.state.refreshPagination}
  805. />}
  806. <table className="tables ignored-list-table">
  807. <thead><tr><th>Path</th></tr></thead>
  808. <tbody>
  809. {filesInPage.map((file, index) => {
  810. return <tr key={file}><td>{file}</td></tr>
  811. })}
  812. </tbody>
  813. </table>
  814. </div>
  815. );
  816. }
  817. }
  818. class PreloadedFiles extends React.Component {
  819. constructor(props) {
  820. super(props);
  821. this.doPagination = (typeof props.perPageLimit === "number"
  822. && props.perPageLimit > 0
  823. );
  824. this.state = {
  825. currentPage: 1,
  826. refreshPagination: 0
  827. }
  828. }
  829. onPageChanged = currentPage => {
  830. this.setState({ currentPage });
  831. }
  832. render() {
  833. if (!this.props.allow.fileList) {
  834. return null;
  835. }
  836. if (this.props.allFiles.length === 0) {
  837. return <p>No files have been preloaded <i>opcache.preload</i></p>;
  838. }
  839. const { currentPage } = this.state;
  840. const offset = (currentPage - 1) * this.props.perPageLimit;
  841. const filesInPage = (this.doPagination
  842. ? this.props.allFiles.slice(offset, offset + this.props.perPageLimit)
  843. : this.props.allFiles
  844. );
  845. const allFilesTotal = this.props.allFiles.length;
  846. return (
  847. <div>
  848. <h3>{allFilesTotal} preloaded files</h3>
  849. {this.doPagination && <Pagination
  850. totalRecords={allFilesTotal}
  851. pageLimit={this.props.perPageLimit}
  852. pageNeighbours={2}
  853. onPageChanged={this.onPageChanged}
  854. refresh={this.state.refreshPagination}
  855. />}
  856. <table className="tables preload-list-table">
  857. <thead><tr><th>Path</th></tr></thead>
  858. <tbody>
  859. {filesInPage.map((file, index) => {
  860. return <tr key={file}><td>{file}</td></tr>
  861. })}
  862. </tbody>
  863. </table>
  864. </div>
  865. );
  866. }
  867. }
  868. class Pagination extends React.Component {
  869. constructor(props) {
  870. super(props);
  871. this.state = { currentPage: 1 };
  872. this.pageNeighbours =
  873. typeof props.pageNeighbours === "number"
  874. ? Math.max(0, Math.min(props.pageNeighbours, 2))
  875. : 0;
  876. }
  877. componentDidMount() {
  878. this.gotoPage(1);
  879. }
  880. componentDidUpdate(props) {
  881. const { refresh } = this.props;
  882. if (props.refresh !== refresh) {
  883. this.gotoPage(1);
  884. }
  885. }
  886. gotoPage = page => {
  887. const { onPageChanged = f => f } = this.props;
  888. const currentPage = Math.max(0, Math.min(page, this.totalPages()));
  889. this.setState({ currentPage }, () => onPageChanged(currentPage));
  890. };
  891. totalPages = () => {
  892. return Math.ceil(this.props.totalRecords / this.props.pageLimit);
  893. }
  894. handleClick = (page, evt) => {
  895. evt.preventDefault();
  896. this.gotoPage(page);
  897. };
  898. handleJumpLeft = evt => {
  899. evt.preventDefault();
  900. this.gotoPage(this.state.currentPage - this.pageNeighbours * 2 - 1);
  901. };
  902. handleJumpRight = evt => {
  903. evt.preventDefault();
  904. this.gotoPage(this.state.currentPage + this.pageNeighbours * 2 + 1);
  905. };
  906. handleMoveLeft = evt => {
  907. evt.preventDefault();
  908. this.gotoPage(this.state.currentPage - 1);
  909. };
  910. handleMoveRight = evt => {
  911. evt.preventDefault();
  912. this.gotoPage(this.state.currentPage + 1);
  913. };
  914. range = (from, to, step = 1) => {
  915. let i = from;
  916. const range = [];
  917. while (i <= to) {
  918. range.push(i);
  919. i += step;
  920. }
  921. return range;
  922. }
  923. fetchPageNumbers = () => {
  924. const totalPages = this.totalPages();
  925. const pageNeighbours = this.pageNeighbours;
  926. const totalNumbers = this.pageNeighbours * 2 + 3;
  927. const totalBlocks = totalNumbers + 2;
  928. if (totalPages > totalBlocks) {
  929. let pages = [];
  930. const leftBound = this.state.currentPage - pageNeighbours;
  931. const rightBound = this.state.currentPage + pageNeighbours;
  932. const beforeLastPage = totalPages - 1;
  933. const startPage = leftBound > 2 ? leftBound : 2;
  934. const endPage = rightBound < beforeLastPage ? rightBound : beforeLastPage;
  935. pages = this.range(startPage, endPage);
  936. const pagesCount = pages.length;
  937. const singleSpillOffset = totalNumbers - pagesCount - 1;
  938. const leftSpill = startPage > 2;
  939. const rightSpill = endPage < beforeLastPage;
  940. const leftSpillPage = "LEFT";
  941. const rightSpillPage = "RIGHT";
  942. if (leftSpill && !rightSpill) {
  943. const extraPages = this.range(startPage - singleSpillOffset, startPage - 1);
  944. pages = [leftSpillPage, ...extraPages, ...pages];
  945. } else if (!leftSpill && rightSpill) {
  946. const extraPages = this.range(endPage + 1, endPage + singleSpillOffset);
  947. pages = [...pages, ...extraPages, rightSpillPage];
  948. } else if (leftSpill && rightSpill) {
  949. pages = [leftSpillPage, ...pages, rightSpillPage];
  950. }
  951. return [1, ...pages, totalPages];
  952. }
  953. return this.range(1, totalPages);
  954. };
  955. render() {
  956. if (!this.props.totalRecords || this.totalPages() === 1) {
  957. return null
  958. }
  959. const { currentPage } = this.state;
  960. const pages = this.fetchPageNumbers();
  961. return (
  962. <nav aria-label="File list pagination">
  963. <ul className="pagination">
  964. {pages.map((page, index) => {
  965. if (page === "LEFT") {
  966. return (
  967. <React.Fragment key={index}>
  968. <li className="page-item arrow">
  969. <a className="page-link" href="#" aria-label="Previous" onClick={this.handleJumpLeft}>
  970. <span aria-hidden="true">↞</span>
  971. <span className="sr-only">Jump back</span>
  972. </a>
  973. </li>
  974. <li className="page-item arrow">
  975. <a className="page-link" href="#" aria-label="Previous" onClick={this.handleMoveLeft}>
  976. <span aria-hidden="true">⇠</span>
  977. <span className="sr-only">Previous page</span>
  978. </a>
  979. </li>
  980. </React.Fragment>
  981. );
  982. }
  983. if (page === "RIGHT") {
  984. return (
  985. <React.Fragment key={index}>
  986. <li className="page-item arrow">
  987. <a className="page-link" href="#" aria-label="Next" onClick={this.handleMoveRight}>
  988. <span aria-hidden="true">⇢</span>
  989. <span className="sr-only">Next page</span>
  990. </a>
  991. </li>
  992. <li className="page-item arrow">
  993. <a className="page-link" href="#" aria-label="Next" onClick={this.handleJumpRight}>
  994. <span aria-hidden="true">↠</span>
  995. <span className="sr-only">Jump forward</span>
  996. </a>
  997. </li>
  998. </React.Fragment>
  999. );
  1000. }
  1001. return (
  1002. <li key={index} className="page-item">
  1003. <a className={`page-link${currentPage === page ? " active" : ""}`} href="#" onClick={e => this.handleClick(page, e)}>
  1004. {page}
  1005. </a>
  1006. </li>
  1007. );
  1008. })}
  1009. </ul>
  1010. </nav>
  1011. );
  1012. }
  1013. }
  1014. function Footer(props) {
  1015. return (
  1016. <footer className="main-footer">
  1017. <a className="github-link" href="https://github.com/amnuts/opcache-gui"
  1018. target="_blank"
  1019. title="opcache-gui (currently version {props.version}) on GitHub"
  1020. >https://github.com/amnuts/opcache-gui - version {props.version}</a>
  1021. </footer>
  1022. );
  1023. }
  1024. function debounce(func, wait, immediate) {
  1025. let timeout;
  1026. wait = wait || 250;
  1027. return function() {
  1028. let context = this, args = arguments;
  1029. let later = function() {
  1030. timeout = null;
  1031. if (!immediate) {
  1032. func.apply(context, args);
  1033. }
  1034. };
  1035. let callNow = immediate && !timeout;
  1036. clearTimeout(timeout);
  1037. timeout = setTimeout(later, wait);
  1038. if (callNow) {
  1039. func.apply(context, args);
  1040. }
  1041. };
  1042. }