PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/searchcode/app/service/route/AdminRouteService.java

https://github.com/boyter/searchcode-server
Java | 577 lines | 476 code | 89 blank | 12 comment | 33 complexity | d161d6f1b5fa9a771e84089c06d2508e MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Boyter Online Services
  3. *
  4. * Use of this software is governed by the Fair Source License included
  5. * in the LICENSE.TXT file, but will be eventually open under GNU General Public License Version 3
  6. * see the README.md for when this clause will take effect
  7. *
  8. * Version 1.3.15
  9. */
  10. package com.searchcode.app.service.route;
  11. import com.google.gson.Gson;
  12. import com.searchcode.app.App;
  13. import com.searchcode.app.config.Values;
  14. import com.searchcode.app.dao.Data;
  15. import com.searchcode.app.dao.IRepo;
  16. import com.searchcode.app.dto.Source;
  17. import com.searchcode.app.dto.Version;
  18. import com.searchcode.app.model.RepoResult;
  19. import com.searchcode.app.model.ValidatorResult;
  20. import com.searchcode.app.service.*;
  21. import com.searchcode.app.service.index.IIndexService;
  22. import com.searchcode.app.util.LoggerWrapper;
  23. import com.searchcode.app.util.Properties;
  24. import com.searchcode.app.util.RepositorySource;
  25. import org.apache.commons.lang3.StringUtils;
  26. import spark.Request;
  27. import spark.Response;
  28. import java.net.URI;
  29. import java.net.http.HttpClient;
  30. import java.net.http.HttpRequest;
  31. import java.net.http.HttpResponse;
  32. import java.time.Instant;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. public class AdminRouteService {
  36. private final IRepo repo;
  37. private final Data data;
  38. private final JobService jobService;
  39. private final DataService dataService;
  40. private final IIndexService indexService;
  41. private final StatsService statsService;
  42. private final ValidatorService validatorService;
  43. private final RepositorySource repositorySource;
  44. private final LoggerWrapper logger;
  45. private final Gson gson;
  46. public AdminRouteService() {
  47. this(Singleton.getRepo(),
  48. Singleton.getData(),
  49. Singleton.getJobService(),
  50. Singleton.getDataService(),
  51. Singleton.getIndexService(),
  52. Singleton.getStatsService(),
  53. Singleton.getValidatorService(),
  54. Singleton.getRepositorySource(),
  55. Singleton.getLogger());
  56. }
  57. public AdminRouteService(IRepo repo, Data data, JobService jobService, DataService dataService, IIndexService indexService, StatsService statsService, ValidatorService validatorService, RepositorySource repositorySource, LoggerWrapper loggerWrapper) {
  58. this.repo = repo;
  59. this.data = data;
  60. this.jobService = jobService;
  61. this.dataService = dataService;
  62. this.indexService = indexService;
  63. this.statsService = statsService;
  64. this.validatorService = validatorService;
  65. this.repositorySource = repositorySource;
  66. this.logger = loggerWrapper;
  67. this.gson = new Gson();
  68. }
  69. public String getStat(Request request, Response response) {
  70. if (request.queryParams().contains("statname")) {
  71. String statName = request.queryParams("statname");
  72. return this.getStat(statName);
  73. }
  74. return Values.EMPTYSTRING;
  75. }
  76. public String checkIndexStatus(Request request, Response response) {
  77. if (request.queryParams().contains("reponame")) {
  78. String reponame = request.queryParams("reponame");
  79. Optional<RepoResult> repoResult = this.repo.getRepoByName(reponame);
  80. String indexStatus = repoResult.map(x -> x.getData().indexStatus).orElse(Values.EMPTYSTRING);
  81. if ("success".equals(indexStatus)) {
  82. return "Indexed ✓";
  83. }
  84. if (this.dataService.getPersistentDelete().contains(reponame)) {
  85. return "Pending Delete";
  86. }
  87. if (Singleton.getRunningIndexRepoJobs().keySet().contains(reponame)) {
  88. return "Indexing...";
  89. }
  90. return "Queued";
  91. }
  92. return Values.EMPTYSTRING;
  93. }
  94. public Map<String, Object> adminPage(Request request, Response response) {
  95. Map<String, Object> map = new HashMap<>();
  96. // Put all properties here
  97. map.put(Values.SQLITE_FILE, Properties.getProperties().getProperty(Values.SQLITE_FILE, Values.DEFAULT_SQLITE_FILE));
  98. map.put(Values.SERVER_PORT, Properties.getProperties().getProperty(Values.SERVER_PORT, Values.DEFAULT_SERVER_PORT));
  99. map.put(Values.REPOSITORYLOCATION, Properties.getProperties().getProperty(Values.REPOSITORYLOCATION, Values.DEFAULTREPOSITORYLOCATION));
  100. map.put(Values.INDEXLOCATION, Properties.getProperties().getProperty(Values.INDEXLOCATION, Values.DEFAULTINDEXLOCATION));
  101. map.put(Values.FACETSLOCATION, Properties.getProperties().getProperty(Values.FACETSLOCATION, Values.DEFAULTFACETSLOCATION));
  102. map.put(Values.CHECKREPOCHANGES, Properties.getProperties().getProperty(Values.CHECKREPOCHANGES, Values.DEFAULTCHECKREPOCHANGES));
  103. map.put(Values.CHECKFILEREPOCHANGES, Properties.getProperties().getProperty(Values.CHECKFILEREPOCHANGES, Values.DEFAULTCHECKFILEREPOCHANGES));
  104. map.put(Values.ONLYLOCALHOST, Properties.getProperties().getProperty(Values.ONLYLOCALHOST, Values.DEFAULTONLYLOCALHOST));
  105. map.put(Values.LOWMEMORY, Properties.getProperties().getProperty(Values.LOWMEMORY, Values.DEFAULTLOWMEMORY));
  106. map.put(Values.SPELLINGCORRECTORSIZE, Properties.getProperties().getProperty(Values.SPELLINGCORRECTORSIZE, Values.DEFAULTSPELLINGCORRECTORSIZE));
  107. map.put(Values.USESYSTEMGIT, Properties.getProperties().getProperty(Values.USESYSTEMGIT, Values.DEFAULTUSESYSTEMGIT));
  108. map.put(Values.GITBINARYPATH, Properties.getProperties().getProperty(Values.GITBINARYPATH, Values.DEFAULTGITBINARYPATH));
  109. map.put(Values.APIENABLED, Properties.getProperties().getProperty(Values.APIENABLED, Values.DEFAULTAPIENABLED));
  110. map.put(Values.APIKEYAUTH, Properties.getProperties().getProperty(Values.APIKEYAUTH, Values.DEFAULTAPIKEYAUTH));
  111. map.put(Values.SVNBINARYPATH, Properties.getProperties().getProperty(Values.SVNBINARYPATH, Values.DEFAULTSVNBINARYPATH));
  112. map.put(Values.SVNENABLED, Properties.getProperties().getProperty(Values.SVNENABLED, Values.DEFAULTSVNENABLED));
  113. map.put(Values.MAXDOCUMENTQUEUESIZE, Properties.getProperties().getProperty(Values.MAXDOCUMENTQUEUESIZE, Values.DEFAULTMAXDOCUMENTQUEUESIZE));
  114. map.put(Values.MAXDOCUMENTQUEUELINESIZE, Properties.getProperties().getProperty(Values.MAXDOCUMENTQUEUELINESIZE, Values.DEFAULTMAXDOCUMENTQUEUELINESIZE));
  115. map.put(Values.MAXFILELINEDEPTH, Properties.getProperties().getProperty(Values.MAXFILELINEDEPTH, Values.DEFAULTMAXFILELINEDEPTH));
  116. map.put(Values.OWASPDATABASELOCATION, Properties.getProperties().getProperty(Values.OWASPDATABASELOCATION, Values.DEFAULTOWASPDATABASELOCATION));
  117. map.put(Values.HIGHLIGHT_LINE_LIMIT, Properties.getProperties().getProperty(Values.HIGHLIGHT_LINE_LIMIT, Values.DEFAULT_HIGHLIGHT_LINE_LIMIT));
  118. map.put(Values.BINARY_WHITE_LIST, Properties.getProperties().getProperty(Values.BINARY_WHITE_LIST, Values.DEFAULT_BINARY_WHITE_LIST));
  119. map.put(Values.BINARY_BLACK_LIST, Properties.getProperties().getProperty(Values.BINARY_BLACK_LIST, Values.DEFAULT_BINARY_BLACK_LIST));
  120. map.put(Values.DIRECTORY_BLACK_LIST, Properties.getProperties().getProperty(Values.DIRECTORY_BLACK_LIST, Values.DEFAULT_DIRECTORY_BLACK_LIST));
  121. map.put(Values.NUMBER_GIT_PROCESSORS, Properties.getProperties().getProperty(Values.NUMBER_GIT_PROCESSORS, Values.DEFAULT_NUMBER_GIT_PROCESSORS));
  122. map.put(Values.NUMBER_SVN_PROCESSORS, Properties.getProperties().getProperty(Values.NUMBER_SVN_PROCESSORS, Values.DEFAULT_NUMBER_SVN_PROCESSORS));
  123. map.put(Values.NUMBER_FILE_PROCESSORS, Properties.getProperties().getProperty(Values.NUMBER_FILE_PROCESSORS, Values.DEFAULT_NUMBER_FILE_PROCESSORS));
  124. map.put(Values.AND_MATCH, Properties.getProperties().getProperty(Values.AND_MATCH, Values.DEFAULT_AND_MATCH));
  125. map.put(Values.LOG_INDEXED, Properties.getProperties().getProperty(Values.LOG_INDEXED, Values.DEFAULT_LOG_INDEXED));
  126. map.put(Values.TRASH_LOCATION, Properties.getProperties().getProperty(Values.TRASH_LOCATION, Values.DEFAULT_TRASH_LOCATION));
  127. map.put(Values.FOLLOW_LINKS, Properties.getProperties().getProperty(Values.FOLLOW_LINKS, Values.DEFAULT_FOLLOW_LINKS));
  128. map.put(Values.DEEP_GUESS_FILES, Properties.getProperties().getProperty(Values.DEEP_GUESS_FILES, Values.DEFAULT_DEEP_GUESS_FILES));
  129. map.put(Values.HOST_NAME, Properties.getProperties().getProperty(Values.HOST_NAME, Values.DEFAULT_HOST_NAME));
  130. map.put(Values.INDEX_ALL_FIELDS, Properties.getProperties().getProperty(Values.INDEX_ALL_FIELDS, Values.DEFAULT_INDEX_ALL_FIELDS));
  131. map.put(Values.INDEXTIME, Properties.getProperties().getProperty(Values.INDEXTIME, Values.DEFAULTINDEXTIME));
  132. map.put("repoCount", this.getStat("repoCount"));
  133. map.put("numDocs", this.getStat("numDocs"));
  134. map.put("numSearches", this.getStat("searchcount"));
  135. map.put("uptime", this.getStat("uptime"));
  136. map.put("loadAverage", this.getStat("loadAverage"));
  137. map.put("memoryUsage", this.getStat("memoryUsage"));
  138. map.put("currentdatetime", this.getStat("servertime"));
  139. map.put("spellingCount", this.getStat("spellingCount"));
  140. map.put("runningJobs", this.getStat("runningJobs"));
  141. map.put("threads", this.getStat("threads"));
  142. map.put("paused", this.getStat("paused"));
  143. map.put("repoQueueSize", this.getStat("repoqueuesize"));
  144. map.put("dataValues", this.data.getAllData());
  145. map.put("sysArch", this.statsService.getArch());
  146. map.put("sysVersion", this.statsService.getOsVersion());
  147. map.put("processorCount", this.statsService.getProcessorCount());
  148. map.put("deletionQueue", this.dataService.getPersistentDelete().size());
  149. map.put("version", App.VERSION);
  150. map.put("logoImage", CommonRouteService.getLogo());
  151. map.put("isCommunity", App.IS_COMMUNITY);
  152. map.put("index_paused", this.indexService.shouldPause(IIndexService.JobType.REPO_PARSER) ? "paused" : "running");
  153. map.put(Values.EMBED, this.data.getDataByName(Values.EMBED, Values.EMPTYSTRING));
  154. return map;
  155. }
  156. public Map<String, Object> adminRepo(Request request, Response response) {
  157. Map<String, Object> map = new HashMap<>();
  158. int repoCount = this.repo.getRepoCount();
  159. String offSet = request.queryParams("offset");
  160. String searchQuery = request.queryParams("q");
  161. int indexOffset = 0;
  162. if (offSet != null) {
  163. try {
  164. indexOffset = Integer.parseInt(offSet);
  165. if (indexOffset > repoCount || indexOffset < 0) {
  166. indexOffset = 0;
  167. }
  168. } catch (NumberFormatException ex) {
  169. indexOffset = 0;
  170. }
  171. }
  172. if (searchQuery != null) {
  173. map.put("repoResults", this.repo.searchRepo(searchQuery));
  174. } else {
  175. map.put("repoResults", this.repo.getPagedRepo(indexOffset, 100));
  176. }
  177. map.put("searchQuery", searchQuery);
  178. map.put("hasPrevious", indexOffset > 0);
  179. map.put("hasNext", (indexOffset + 100) < repoCount);
  180. map.put("previousOffset", Values.EMPTYSTRING + (indexOffset - 100));
  181. map.put("nextOffset", Values.EMPTYSTRING + (indexOffset + 100));
  182. map.put("repoCount", this.getStat("repoCount"));
  183. map.put("logoImage", CommonRouteService.getLogo());
  184. map.put("isCommunity", App.IS_COMMUNITY);
  185. map.put(Values.EMBED, this.data.getDataByName(Values.EMBED, Values.EMPTYSTRING));
  186. map.put("repositorySource", this.repositorySource.loadDatabase().stream().map(Source::getName).collect(Collectors.toList()));
  187. return map;
  188. }
  189. public Map<String, Object> adminGetRepo(Request request, Response response) {
  190. var map = new HashMap<String, Object>();
  191. var repoName = request.params(":reponame");
  192. var repository = this.repo.getRepoByName(repoName);
  193. repository.ifPresent(x -> map.put("repoResult", x));
  194. map.put("logoImage", CommonRouteService.getLogo());
  195. map.put("isCommunity", App.IS_COMMUNITY);
  196. map.put("repoCount", this.getStat("repoCount"));
  197. map.put(Values.EMBED, this.data.getDataByName(Values.EMBED, Values.EMPTYSTRING));
  198. map.put("repositorySource", this.repositorySource.loadDatabase().stream().map(Source::getName).collect(Collectors.toList()));
  199. return map;
  200. }
  201. public Map<String, Object> adminApi(Request request, Response response) {
  202. var map = new HashMap<String, Object>();
  203. var api = Singleton.getApi();
  204. map.put("apiKeys", api.getAllApi());
  205. var apiEnabled = Boolean.parseBoolean(Properties.getProperties().getProperty("api_enabled", "false"));
  206. var apiAuth = Boolean.parseBoolean(Properties.getProperties().getProperty("api_key_authentication", "true"));
  207. map.put("apiAuthentication", apiEnabled && apiAuth);
  208. map.put("logoImage", CommonRouteService.getLogo());
  209. map.put("isCommunity", App.IS_COMMUNITY);
  210. map.put(Values.EMBED, this.data.getDataByName(Values.EMBED, Values.EMPTYSTRING));
  211. map.put("repoCount", this.getStat("repoCount"));
  212. return map;
  213. }
  214. public Map<String, Object> adminSettings(Request request, Response response) {
  215. var highlighters = "agate,androidstudio,arta,ascetic,atelier-cave.dark,atelier-cave.light,atelier-dune.dark,atelier-dune.light,atelier-estuary.dark,atelier-estuary.light,atelier-forest.dark,atelier-forest.light,atelier-heath.dark,atelier-heath.light,atelier-lakeside.dark,atelier-lakeside.light,atelier-plateau.dark,atelier-plateau.light,atelier-savanna.dark,atelier-savanna.light,atelier-seaside.dark,atelier-seaside.light,atelier-sulphurpool.dark,atelier-sulphurpool.light,brown_paper,codepen-embed,color-brewer,dark,darkula,default,docco,far,foundation,github-gist,github,googlecode,grayscale,hopscotch,hybrid,idea,ir_black,kimbie.dark,kimbie.light,magula,mono-blue,monokai,monokai_sublime,obsidian,paraiso.dark,paraiso.light,pojoaque,railscasts,rainbow,school_book,solarized_dark,solarized_light,sunburst,tomorrow-night-blue,tomorrow-night-bright,tomorrow-night-eighties,tomorrow-night,tomorrow,vs,xcode,zenburn".split(",");
  216. var map = new HashMap<String, Object>();
  217. map.put("logoImage", CommonRouteService.getLogo());
  218. map.put("syntaxHighlighter", CommonRouteService.getSyntaxHighlighter());
  219. map.put("averageSalary", Values.EMPTYSTRING + (int) CommonRouteService.getAverageSalary());
  220. map.put("matchLines", Values.EMPTYSTRING + (int) CommonRouteService.getMatchLines());
  221. map.put("maxLineDepth", Values.EMPTYSTRING + (int) CommonRouteService.getMaxLineDepth());
  222. map.put("minifiedLength", Values.EMPTYSTRING + (int) CommonRouteService.getMinifiedLength());
  223. map.put("owaspenabled", CommonRouteService.owaspAdvisoriesEnabled());
  224. map.put("backoffValue", CommonRouteService.getBackoffValue());
  225. map.put("embed", CommonRouteService.getEmbed());
  226. map.put("isCommunity", App.IS_COMMUNITY);
  227. map.put("highlighters", highlighters);
  228. map.put("repoCount", this.getStat("repoCount"));
  229. return map;
  230. }
  231. public Map<String, Object> adminLogs(Request request, Response response) {
  232. var map = new HashMap<String, Object>();
  233. var level = Properties.getProperties().getOrDefault("log_level", "SEVERE").toString().toUpperCase();
  234. if (request.queryParams().contains("level") && !request.queryParams("level").trim().equals(Values.EMPTYSTRING)) {
  235. level = request.queryParams("level").trim().toUpperCase();
  236. }
  237. String logs;
  238. switch (level) {
  239. case "INFO":
  240. logs = this.getStat("infologs");
  241. break;
  242. case "WARNING":
  243. logs = this.getStat("warninglogs");
  244. break;
  245. case "ALL":
  246. logs = this.getStat("alllogs");
  247. break;
  248. case "SEARCH":
  249. logs = this.getStat("searchlogs");
  250. break;
  251. case "API":
  252. logs = this.getStat("apiLogs");
  253. break;
  254. case "FINE":
  255. logs = this.getStat("finelogs");
  256. break;
  257. case "SEVERE":
  258. default:
  259. logs = this.getStat("severelogs");
  260. break;
  261. }
  262. map.put("level", level);
  263. map.put("logs", logs);
  264. map.put("logoImage", CommonRouteService.getLogo());
  265. map.put("isCommunity", App.IS_COMMUNITY);
  266. map.put(Values.EMBED, this.data.getDataByName(Values.EMBED, Values.EMPTYSTRING));
  267. map.put("repoCount", this.getStat("repoCount"));
  268. return map;
  269. }
  270. public void postSettings(Request request, Response response) {
  271. var logo = request.queryParams("logo").trim();
  272. var syntaxHighlighter = request.queryParams("syntaxhighligher");
  273. var embed = request.queryParams("embed").trim();
  274. try {
  275. var averageSalary = Double.parseDouble(request.queryParams("averagesalary"));
  276. this.data.saveData(Values.AVERAGESALARY, "" + (int) averageSalary);
  277. } catch (NumberFormatException ex) {
  278. this.data.saveData(Values.AVERAGESALARY, Values.DEFAULTAVERAGESALARY);
  279. }
  280. try {
  281. var averageSalary = Double.parseDouble(request.queryParams("matchlines"));
  282. this.data.saveData(Values.MATCHLINES, "" + (int) averageSalary);
  283. } catch (NumberFormatException ex) {
  284. this.data.saveData(Values.MATCHLINES, Values.DEFAULTMATCHLINES);
  285. }
  286. try {
  287. var averageSalary = Double.parseDouble(request.queryParams("maxlinedepth"));
  288. this.data.saveData(Values.MAXLINEDEPTH, "" + (int) averageSalary);
  289. } catch (NumberFormatException ex) {
  290. this.data.saveData(Values.MAXLINEDEPTH, Values.DEFAULTMAXLINEDEPTH);
  291. }
  292. try {
  293. var minifiedLength = Double.parseDouble(request.queryParams("minifiedlength"));
  294. this.data.saveData(Values.MINIFIEDLENGTH, "" + (int) minifiedLength);
  295. } catch (NumberFormatException ex) {
  296. this.data.saveData(Values.MINIFIEDLENGTH, Values.DEFAULTMINIFIEDLENGTH);
  297. }
  298. try {
  299. var backoffValue = Double.parseDouble(request.queryParams("backoffValue"));
  300. this.data.saveData(Values.BACKOFFVALUE, "" + backoffValue);
  301. } catch (NumberFormatException ex) {
  302. this.data.saveData(Values.BACKOFFVALUE, Values.DEFAULTBACKOFFVALUE);
  303. }
  304. var owaspadvisories = Boolean.parseBoolean(request.queryParams("owaspadvisories"));
  305. this.data.saveData(Values.OWASPENABLED, "" + owaspadvisories);
  306. this.data.saveData(Values.LOGO, logo);
  307. this.data.saveData(Values.SYNTAXHIGHLIGHTER, syntaxHighlighter);
  308. this.data.saveData(Values.EMBED, embed);
  309. }
  310. public List<ValidatorResult> postBulk(Request request, Response response) {
  311. var repos = request.queryParams("repos");
  312. var repolines = repos.split("\\r?\\n");
  313. var validatorResults = new ArrayList<ValidatorResult>();
  314. for (String line : repolines) {
  315. var repoparams = line.split(",", -1);
  316. if (repoparams.length == 7) {
  317. var branch = repoparams[6].trim();
  318. if (branch.equals(Values.EMPTYSTRING)) {
  319. branch = "master";
  320. }
  321. var scm = repoparams[1].trim().toLowerCase();
  322. if (scm.equals(Values.EMPTYSTRING)) {
  323. scm = "git";
  324. }
  325. RepoResult repoResult = new RepoResult()
  326. .setRowId(-1)
  327. .setName(repoparams[0])
  328. .setScm(scm)
  329. .setUrl(repoparams[2])
  330. .setUsername(repoparams[3])
  331. .setPassword(repoparams[4])
  332. .setSource(repoparams[5])
  333. .setBranch(branch)
  334. .setData("{}");
  335. var validate = this.validatorService.validate(repoResult, false);
  336. if (validate.isValid) {
  337. this.repo.saveRepo(repoResult);
  338. this.jobService.forceEnqueue(repoResult);
  339. } else {
  340. validate.setLine(line);
  341. validatorResults.add(validate);
  342. }
  343. } else {
  344. ValidatorResult validate = new ValidatorResult(false, "Incorrect number of elements: " + line);
  345. validate.setLine(line);
  346. validatorResults.add(validate);
  347. }
  348. }
  349. return validatorResults;
  350. }
  351. // TODO split out so we don't need the ignoreDuplicates param
  352. public ValidatorResult postRepo(Request request, Response response, boolean ignoreDuplicates) {
  353. var reponames = request.queryParamsValues("reponame");
  354. var reposcms = request.queryParamsValues("reposcm");
  355. var repourls = request.queryParamsValues("repourl");
  356. var repousername = request.queryParamsValues("repousername");
  357. var repopassword = request.queryParamsValues("repopassword");
  358. var reposource = request.queryParamsValues("reposource");
  359. var repobranch = request.queryParamsValues("repobranch");
  360. // Additional
  361. var source = request.queryParamsValues("source");
  362. var sourceuser = request.queryParamsValues("sourceuser");
  363. var sourceproject = request.queryParamsValues("sourceproject");
  364. var validate = new ValidatorResult(true, Values.EMPTYSTRING);
  365. for (var i = 0; i < reponames.length; i++) {
  366. var branch = repobranch[i].trim();
  367. if (branch.equals(Values.EMPTYSTRING)) {
  368. branch = "master";
  369. }
  370. var repoResult = new RepoResult()
  371. .setRowId(-1)
  372. .setName(reponames[i])
  373. .setScm(reposcms[i])
  374. .setUrl(repourls[i])
  375. .setUsername(repousername[i])
  376. .setPassword(repopassword[i])
  377. .setSource(reposource[i])
  378. .setBranch(branch)
  379. .setData("{}");
  380. validate = this.validatorService.validate(repoResult, ignoreDuplicates);
  381. repoResult.getData().source = source[i];
  382. repoResult.getData().user = sourceuser[i];
  383. repoResult.getData().project = sourceproject[i];
  384. if (!validate.isValid) {
  385. validate.setRepoResult(repoResult);
  386. return validate;
  387. }
  388. this.repo.saveRepo(repoResult);
  389. var repoByUrl = this.repo.getRepoByUrl(repourls[i]);
  390. repoByUrl.ifPresent(this.jobService::forceEnqueue);
  391. }
  392. return validate;
  393. }
  394. public void deleteRepo(Request request, Response response) {
  395. var repoName = request.queryParams("repoName");
  396. var repoResult = this.repo.getRepoByName(repoName);
  397. repoResult.ifPresent(x -> this.dataService.addToPersistentDelete(x.getName()));
  398. }
  399. public void reindexRepo(Request request, Response response) {
  400. var repoName = request.queryParams("repoName");
  401. var repoResult = this.repo.getRepoByName(repoName);
  402. repoResult.ifPresent(x -> {
  403. x.getData().jobRunTime = Instant.parse("1800-01-01T00:00:00.000Z");
  404. this.logger.info("392be2e4::resetting Job Run Time due to reindex request for repository " + x.getName());
  405. this.repo.saveRepo(x);
  406. this.jobService.forceEnqueue(x);
  407. });
  408. }
  409. public String checkVersion() {
  410. Version version;
  411. try {
  412. var httpClient = HttpClient.newBuilder().build();
  413. var request = HttpRequest.newBuilder()
  414. .uri(URI.create("https://searchcodeserver.com/version.json"))
  415. .GET()
  416. .build();
  417. var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
  418. version = this.gson.fromJson(response.body(), Version.class);
  419. } catch (Exception ex) {
  420. return "Unable to determine if running the latest version. Check https://searchcodeserver.com/pricing.html for the latest release.";
  421. }
  422. if (App.VERSION.equals(version.getVersion())) {
  423. return "Your searchcode server version " + App.VERSION + " is the latest.";
  424. } else {
  425. return "Your searchcode server version " + App.VERSION + " instance is out of date. The latest version is " + version.getVersion() + ".";
  426. }
  427. }
  428. public String getStat(String statName) {
  429. if (statName == null) {
  430. return Values.EMPTYSTRING;
  431. }
  432. switch (statName.toLowerCase()) {
  433. case "memoryusage":
  434. return this.statsService.getMemoryUsage(Values.LINE_BREAK);
  435. case "loadaverage":
  436. return this.statsService.getLoadAverage();
  437. case "uptime":
  438. return this.statsService.getUpTime();
  439. case "searchcount":
  440. return Values.EMPTYSTRING + this.statsService.getSearchCount();
  441. case "runningjobs":
  442. var collect = Singleton.getRunningIndexRepoJobs().keySet().stream()
  443. .filter(x -> Singleton.getRunningIndexRepoJobs().get(x) != null)
  444. .map(x -> x + " <small>(" + (Singleton.getHelpers().getCurrentTimeSeconds() - Singleton.getRunningIndexRepoJobs().get(x).startTime) + "s)</small>")
  445. .collect(Collectors.joining(Values.LINE_BREAK));
  446. return collect + Values.NBSP;
  447. case "spellingcount":
  448. return Values.EMPTYSTRING + Singleton.getSpellingCorrector().getWordCount();
  449. case "repocount":
  450. return Values.EMPTYSTRING + this.repo.getRepoCount();
  451. case "numdocs":
  452. return Values.EMPTYSTRING + this.indexService.getIndexedDocumentCount();
  453. case "servertime":
  454. return new Date().toString();
  455. case "deletionqueue":
  456. return Values.EMPTYSTRING + Singleton.getDataService().getPersistentDelete().size();
  457. case "alllogs":
  458. return StringUtils.join(this.logger.getAllLogs(), System.lineSeparator());
  459. case "infologs":
  460. return StringUtils.join(this.logger.getInfoLogs(), System.lineSeparator());
  461. case "severelogs":
  462. return StringUtils.join(this.logger.getSevereLogs(), System.lineSeparator());
  463. case "searchlogs":
  464. return StringUtils.join(this.logger.getSearchLogs(), System.lineSeparator());
  465. case "apilogs":
  466. return StringUtils.join(this.logger.getApiLogs(), System.lineSeparator());
  467. case "threads":
  468. return Values.EMPTYSTRING + java.lang.Thread.activeCount();
  469. case "repoqueuesize":
  470. return Values.EMPTYSTRING + (Singleton.getUniqueGitRepoQueue().size() + Singleton.getUniqueSvnRepoQueue().size() + Singleton.getUniqueFileRepoQueue().size());
  471. case "parserpaused":
  472. return this.indexService.shouldPause(IIndexService.JobType.REPO_PARSER) ? Values.PAUSED : Values.RUNNING;
  473. case "adderpaused":
  474. return this.indexService.shouldPause(IIndexService.JobType.REPO_ADDER) ? Values.PAUSED : Values.RUNNING;
  475. case "indexreadlocation":
  476. return this.indexService.getProperty("index_read_location");
  477. case "indexwritelocation":
  478. return this.indexService.getProperty("index_write_location");
  479. case "facetwritelocation":
  480. return this.indexService.getProperty("facet_write_location");
  481. case "codeindexlinescount":
  482. return Values.EMPTYSTRING + this.indexService.getCodeIndexLinesCount();
  483. case "codeindexqueuesize":
  484. return Values.EMPTYSTRING + Singleton.getCodeIndexQueue().size();
  485. }
  486. return Values.EMPTYSTRING;
  487. }
  488. }