interface.jsx 45 KB

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