|
| 1 | +// Copyright 2018 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Liubov Yaronskaya ([email protected]) |
| 3 | +// Author: Anatoly Kislov ([email protected]) |
| 4 | + |
| 5 | +package app.extractors |
| 6 | + |
| 7 | +import app.BuildConfig |
| 8 | +import app.Logger |
| 9 | +import app.model.Classifier |
| 10 | +import app.model.LibraryMeta |
| 11 | +import app.utils.FileHelper |
| 12 | +import org.apache.http.client.methods.HttpGet |
| 13 | +import org.apache.http.impl.client.HttpClientBuilder |
| 14 | +import java.io.FileOutputStream |
| 15 | + |
| 16 | +class ClassifierManager { |
| 17 | + companion object { |
| 18 | + private const val CLASSIFIERS_DIR = "classifiers" |
| 19 | + private const val DATA_EXT = ".pb" |
| 20 | + private const val LIBS_META_DIR = ClassifierManager.CLASSIFIERS_DIR |
| 21 | + private const val LIBS_META_FILENAME = "libraries_meta.pb" |
| 22 | + } |
| 23 | + |
| 24 | + val cache = hashMapOf<String, Classifier>() |
| 25 | + val libsMeta = getLibraryMeta() |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns libraries used in a line. |
| 29 | + */ |
| 30 | + fun estimate(line: List<String>, libraries: List<String>): List<String> { |
| 31 | + return libraries.filter { libId -> |
| 32 | + if (!cache.containsKey(libId)) { |
| 33 | + // Library not downloaded from cloud storage. |
| 34 | + if (FileHelper.notExists(libId + DATA_EXT, CLASSIFIERS_DIR)) { |
| 35 | + Logger.info { "Downloading $libId classifier" } |
| 36 | + downloadClassifier(libId) |
| 37 | + Logger.info { "Finished downloading $libId classifier" } |
| 38 | + } |
| 39 | + |
| 40 | + // Library not loaded from local storage. |
| 41 | + Logger.info { "Loading $libId evaluator" } |
| 42 | + loadClassifier(libId) |
| 43 | + Logger.info { "$libId evaluator ready" } |
| 44 | + } |
| 45 | + |
| 46 | + // Check line for usage of a library. |
| 47 | + val prediction = cache[libId]!!.evaluate(line) |
| 48 | + // Prediction based on two classes. |
| 49 | + val prob = prediction[cache[libId]!!.libraries.indexOf(libId)] |
| 50 | + // Libraries with no imports. |
| 51 | + if (libId == "rb.rails") { |
| 52 | + prob > 0.8 |
| 53 | + } else { |
| 54 | + prob > 0.5 |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Downloads libraries from cloud. |
| 61 | + */ |
| 62 | + private fun downloadClassifier(libId: String) { |
| 63 | + val file = FileHelper.getFile(libId + DATA_EXT, CLASSIFIERS_DIR) |
| 64 | + val langId = libId.split('.')[0] |
| 65 | + val url = "${BuildConfig.LIBRARY_MODELS_URL}$langId/$libId$DATA_EXT" |
| 66 | + val builder = HttpClientBuilder.create() |
| 67 | + val client = builder.build() |
| 68 | + try { |
| 69 | + client.execute(HttpGet(url)).use { response -> |
| 70 | + val entity = response.entity |
| 71 | + if (entity != null) { |
| 72 | + FileOutputStream(file).use { outstream -> |
| 73 | + entity.writeTo(outstream) |
| 74 | + outstream.flush() |
| 75 | + outstream.close() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + } catch (e: Exception) { |
| 81 | + Logger.error(e, "Failed to download $libId classifier") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Loads libraries from local storage to cache. |
| 87 | + */ |
| 88 | + private fun loadClassifier(libId: String) { |
| 89 | + val bytesArray = FileHelper.getFile(libId + DATA_EXT, CLASSIFIERS_DIR) |
| 90 | + .readBytes() |
| 91 | + cache[libId] = Classifier(bytesArray) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Downloads libraries meta data from cloud. |
| 96 | + */ |
| 97 | + private fun downloadLibrariesMeta() { |
| 98 | + val file = FileHelper.getFile(LIBS_META_FILENAME, LIBS_META_DIR) |
| 99 | + val url = BuildConfig.LIBRARY_MODELS_URL + LIBS_META_FILENAME |
| 100 | + val builder = HttpClientBuilder.create() |
| 101 | + val client = builder.build() |
| 102 | + try { |
| 103 | + client.execute(HttpGet(url)).use { response -> |
| 104 | + val entity = response.entity |
| 105 | + if (entity != null) { |
| 106 | + FileOutputStream(file).use { outstream -> |
| 107 | + entity.writeTo(outstream) |
| 108 | + outstream.flush() |
| 109 | + outstream.close() |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } catch (e: Exception) { |
| 114 | + Logger.error(e, "Failed to download $LIBS_META_FILENAME") |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Loads libraries meta data from local storage. |
| 120 | + */ |
| 121 | + private fun getLibraryMeta(): LibraryMeta { |
| 122 | + Logger.info { "Downloading $LIBS_META_FILENAME" } |
| 123 | + downloadLibrariesMeta() |
| 124 | + Logger.info { "Finished downloading $LIBS_META_FILENAME" } |
| 125 | + |
| 126 | + val bytesArray = FileHelper.getFile(LIBS_META_FILENAME, |
| 127 | + LIBS_META_DIR).readBytes() |
| 128 | + return LibraryMeta(bytesArray) |
| 129 | + } |
| 130 | +} |
0 commit comments