@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorConfigCacheController
4 extends PhabricatorConfigServicesController {
5
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
8
9
10 $purge_button = id(new PHUIButtonView())
11 ->setText(pht('Purge Caches'))
12 ->setHref('/config/cache/purge/')
13 ->setTag('a')
14 ->setWorkflow(true)
15 ->setIcon('fa-exclamation-triangle');
16
17 $title = pht('Cache Status');
18 $header = $this->buildHeaderView($title, $purge_button);
19
20 $code_box = $this->renderCodeBox();
21 $data_box = $this->renderDataBox();
22
23 $page = array(
24 $code_box,
25 $data_box,
26 );
27
28 $crumbs = $this->newCrumbs()
29 ->addTextCrumb($title);
30
31 $content = id(new PHUITwoColumnView())
32 ->setHeader($header)
33 ->setFooter($page);
34
35 $nav = $this->newNavigation('cache');
36
37 return $this->newPage()
38 ->setTitle($title)
39 ->setCrumbs($crumbs)
40 ->setNavigation($nav)
41 ->appendChild($content);
42 }
43
44 private function renderCodeBox() {
45 $cache = PhabricatorOpcodeCacheSpec::getActiveCacheSpec();
46 $properties = new PHUIPropertyListView();
47 $this->renderCommonProperties($properties, $cache);
48 return $this->buildConfigBoxView(pht('Opcode Cache'), $properties);
49 }
50
51 private function renderDataBox() {
52 $cache = PhabricatorDataCacheSpec::getActiveCacheSpec();
53
54 $properties = new PHUIPropertyListView();
55
56 $this->renderCommonProperties($properties, $cache);
57
58 $table = null;
59 if ($cache->getName() !== null) {
60 $total_memory = $cache->getTotalMemory();
61
62 $summary = $cache->getCacheSummary();
63 $summary = isort($summary, 'total');
64 $summary = array_reverse($summary, true);
65
66 $rows = array();
67 foreach ($summary as $key => $info) {
68 $rows[] = array(
69 $key,
70 pht('%s', new PhutilNumber($info['count'])),
71 phutil_format_bytes($info['max']),
72 phutil_format_bytes($info['total']),
73 sprintf('%.1f%%', (100 * ($info['total'] / $total_memory))),
74 );
75 }
76
77 $table = id(new AphrontTableView($rows))
78 ->setHeaders(
79 array(
80 pht('Pattern'),
81 pht('Count'),
82 pht('Largest'),
83 pht('Total'),
84 pht('Usage'),
85 ))
86 ->setColumnClasses(
87 array(
88 'wide',
89 'n',
90 'n',
91 'n',
92 'n',
93 ));
94
95 $table = $this->buildConfigBoxView(pht('Cache Storage'), $table);
96 }
97
98 $properties = $this->buildConfigBoxView(pht('Data Cache'), $properties);
99
100 return array($properties, $table);
101 }
102
103 private function renderCommonProperties(
104 PHUIPropertyListView $properties,
105 PhabricatorCacheSpec $cache) {
106
107 if ($cache->getName() !== null) {
108 $name = $this->renderYes($cache->getName());
109 } else {
110 $name = $this->renderNo(pht('None'));
111 }
112 $properties->addProperty(pht('Cache'), $name);
113
114 if ($cache->getIsEnabled()) {
115 $enabled = $this->renderYes(pht('Enabled'));
116 } else {
117 $enabled = $this->renderNo(pht('Not Enabled'));
118 }
119 $properties->addProperty(pht('Enabled'), $enabled);
120
121 $version = $cache->getVersion();
122 if ($version) {
123 $properties->addProperty(pht('Version'), $this->renderInfo($version));
124 }
125
126 if ($cache->getName() === null) {
127 return;
128 }
129
130 $mem_total = $cache->getTotalMemory();
131 $mem_used = $cache->getUsedMemory();
132
133 if ($mem_total) {
134 $percent = 100 * ($mem_used / $mem_total);
135
136 $properties->addProperty(
137 pht('Memory Usage'),
138 pht(
139 '%s of %s',
140 phutil_tag('strong', array(), sprintf('%.1f%%', $percent)),
141 phutil_format_bytes($mem_total)));
142 }
143
144 $entry_count = $cache->getEntryCount();
145 if ($entry_count !== null) {
146 $properties->addProperty(
147 pht('Cache Entries'),
148 pht('%s', new PhutilNumber($entry_count)));
149 }
150
151 }
152
153 private function renderYes($info) {
154 return array(
155 id(new PHUIIconView())->setIcon('fa-check', 'green'),
156 ' ',
157 $info,
158 );
159 }
160
161 private function renderNo($info) {
162 return array(
163 id(new PHUIIconView())->setIcon('fa-times-circle', 'red'),
164 ' ',
165 $info,
166 );
167 }
168
169 private function renderInfo($info) {
170 return array(
171 id(new PHUIIconView())->setIcon('fa-info-circle', 'grey'),
172 ' ',
173 $info,
174 );
175 }
176
177}