Skip to content

Commit

Permalink
fix: pr change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
10ko committed Dec 19, 2019
1 parent e4ae827 commit 202453c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 63 deletions.
41 changes: 28 additions & 13 deletions garden-service/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import uuidv4 from "uuid/v4"
import segmentClient = require("analytics-node")
import { platform, release } from "os"
import ci = require("ci-info")

import { flatten } from "lodash"
import { globalConfigKeys, AnalyticsGlobalConfig, GlobalConfigStore } from "../config-store"
import { getPackageVersion } from "../util/util"
import { SEGMENT_PROD_API_KEY, SEGMENT_DEV_API_KEY } from "../constants"
Expand All @@ -22,7 +22,6 @@ import uuid from "uuid"
import { Garden } from "../garden"
import { Events, EventName } from "../events"
import { AnalyticsType } from "./analytics-types"
import { TestConfig } from "../config/test"
import dedent from "dedent"

const API_KEY = process.env.ANALYTICS_DEV ? SEGMENT_DEV_API_KEY : SEGMENT_PROD_API_KEY
Expand Down Expand Up @@ -111,7 +110,7 @@ export class AnalyticsHandler {
private systemConfig: SystemInfo
private isCI = ci.isCI
private sessionId = uuid.v4()
private garden: Garden
protected garden: Garden
private projectMetadata

private constructor(garden: Garden, log: LogEntry) {
Expand All @@ -135,7 +134,14 @@ export class AnalyticsHandler {

static async init(garden: Garden, log: LogEntry) {
if (!AnalyticsHandler.instance) {
AnalyticsHandler.instance = await new AnalyticsHandler(garden, log).initialize()
AnalyticsHandler.instance = await new AnalyticsHandler(garden, log).factory()
} else {
/**
* This init is called from within the do while loop in the cli
* If the instance is already present it means a restart happened and we need to
* refresh the garden instance and event listeners.
*/
await AnalyticsHandler.refreshGarden(garden)
}
return AnalyticsHandler.instance
}
Expand All @@ -149,23 +155,23 @@ export class AnalyticsHandler {

/**
* A private initialization function which returns an initialized Analytics object, ready to be used.
* This function will load global update it if needed.
* This function will load the globalConfigStore and update it if needed.
* The globalConfigStore contains info about optIn, first run, machine info, etc.,
* This method always needs to be called after instantiation.
*
* @returns
* @memberof AnalyticsHandler
*/
private async initialize() {
private async factory() {
const globalConf = await this.globalConfigStore.get()
this.globalConfig = {
...this.globalConfig,
...globalConf.analytics,
}

const vcs = new GitHandler(process.cwd(), [])
const originName = await vcs.getOriginName()
this.projectId = originName ? hasha(await vcs.getOriginName(), { algorithm: "sha256" }) : "unset"
const originName = await vcs.getOriginName(this.log)
this.projectId = originName ? hasha(originName, { algorithm: "sha256" }) : "unset"

if (this.globalConfig.firstRun || this.globalConfig.showOptInMessage) {
if (!this.isCI) {
Expand All @@ -174,7 +180,7 @@ export class AnalyticsHandler {
dedent`
Thanks for installing Garden! We work hard to provide you with the best experience we can.
We collect some anonymized usage data while you use Garden. If you'd like to know more about what we collect
or you'd like to to opt-out, please read more at https://github.com/garden-io/garden/blob/master/README.md#Analytics`
or if you'd like to opt out of telemetry, please read more at https://github.com/garden-io/garden/blob/master/README.md#Analytics`
)
}

Expand Down Expand Up @@ -215,6 +221,13 @@ export class AnalyticsHandler {
}
}

static async refreshGarden(garden: Garden) {
AnalyticsHandler.instance.garden = garden
AnalyticsHandler.instance.garden.events.onAny((name, payload) =>
AnalyticsHandler.instance.processEvent(name, payload)
)
}

/**
* Typeguard to check wether we can process or not an event
*/
Expand All @@ -238,16 +251,16 @@ export class AnalyticsHandler {
private async generateProjectMetadata() {
const configGraph = await this.garden.getConfigGraph(this.log)
const modules = await configGraph.getModules()
const modulesTypes = [...new Set(modules.map((m) => m.type))]
const moduleTypes = [...new Set(modules.map((m) => m.type))]

const tasks = await configGraph.getTasks()
const services = await configGraph.getServices()
const tests = modules.map((m) => m.testConfigs)
const numberOfTests = ([] as TestConfig[]).concat(...tests).length
const numberOfTests = flatten(tests).length

return {
numberOfModules: modules.length,
modulesTypes,
moduleTypes,
numberOfTasks: tasks.length,
numberOfServices: services.length,
numberOfTests,
Expand Down Expand Up @@ -385,11 +398,13 @@ export class AnalyticsHandler {
* @returns
* @memberof AnalyticsHandler
*/
trackModuleConfigError(moduleType: string) {
trackModuleConfigError(name: string, moduleType: string) {
const moduleName = hasha(name, { algorithm: "sha256" })
return this.track(<AnalyticsEvent>{
type: AnalyticsType.MODULE_CONFIG_ERROR,
properties: <AnalyticsConfigErrorProperties>{
...this.getBasicAnalyticsProperties(),
moduleName,
moduleType,
},
})
Expand Down
30 changes: 12 additions & 18 deletions garden-service/src/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ export class TaskGraph {
private resultCache: ResultCache
private opQueue: PQueue

private batchId: string

constructor(private garden: Garden, private log: LogEntry) {
this.roots = new TaskNodeMap()
this.index = new TaskNodeMap()
Expand All @@ -90,12 +88,11 @@ export class TaskGraph {
this.resultCache = new ResultCache()
this.opQueue = new PQueue({ concurrency: 1 })
this.logEntryMap = {}
this.batchId = uuid.v4()
}

async process(tasks: BaseTask[], opts?: ProcessTasksOpts): Promise<TaskResults> {
// We generate a new batchId
this.batchId = uuid.v4()
const batchId = uuid.v4()

for (const t of tasks) {
this.latestTasks[t.getKey()] = t
Expand All @@ -112,7 +109,7 @@ export class TaskGraph {
// to return the latest result for each requested task.
const resultKeys = tasks.map((t) => t.getKey())

const results = await this.opQueue.add(() => this.processTasksInternal(tasksToProcess, resultKeys, opts))
const results = await this.opQueue.add(() => this.processTasksInternal(batchId, tasksToProcess, resultKeys, opts))

if (opts && opts.throwOnError) {
const failed = Object.entries(results).filter(([_, result]) => result && result.error)
Expand Down Expand Up @@ -153,13 +150,13 @@ export class TaskGraph {
this.roots.setNodes(newRootNodes)
}

private async addTask(task: BaseTask) {
private async addTask(batchId: string, task: BaseTask) {
await this.addNodeWithDependencies(task)
this.rebuild()
if (this.index.getNode(task)) {
this.garden.events.emit("taskPending", {
addedAt: new Date(),
batchId: this.batchId,
batchId,
key: task.getKey(),
name: task.getName(),
type: task.type,
Expand Down Expand Up @@ -198,14 +195,15 @@ export class TaskGraph {
* Process the graph until it's complete.
*/
private async processTasksInternal(
batchId: string,
tasks: BaseTask[],
resultKeys: string[],
opts?: ProcessTasksOpts
): Promise<TaskResults> {
const { concurrencyLimit = defaultTaskConcurrency } = opts || {}

for (const task of tasks) {
await this.addTask(this.latestTasks[task.getKey()])
await this.addTask(batchId, this.latestTasks[task.getKey()])
}

this.log.silly("")
Expand Down Expand Up @@ -261,18 +259,18 @@ export class TaskGraph {
type,
key,
startedAt: new Date(),
batchId: this.batchId,
batchId,
version: task.version,
})
result = await node.process(dependencyResults, this.batchId)
result = await node.process(dependencyResults, batchId)

this.garden.events.emit("taskComplete", result)
} catch (error) {
success = false
result = { type, description, key, name, error, completedAt: new Date(), batchId: this.batchId }
result = { type, description, key, name, error, completedAt: new Date(), batchId }
this.garden.events.emit("taskError", result)
this.logTaskError(node, error)
this.cancelDependants(node)
this.cancelDependants(batchId, node)
} finally {
// We know the result got assigned in either the try or catch clause
results[key] = result!
Expand Down Expand Up @@ -339,7 +337,7 @@ export class TaskGraph {
}

// Recursively remove node's dependants, without removing node.
private cancelDependants(node: TaskNode) {
private cancelDependants(batchId: string, node: TaskNode) {
const cancelledAt = new Date()
for (const dependant of this.getDependants(node)) {
this.logTaskComplete(dependant, false)
Expand All @@ -348,7 +346,7 @@ export class TaskGraph {
key: dependant.getKey(),
name: dependant.task.getName(),
type: dependant.getType(),
batchId: this.batchId,
batchId,
})
this.remove(dependant)
}
Expand Down Expand Up @@ -419,10 +417,6 @@ export class TaskGraph {

this.log.error({ msg, error })
}

getBatchId() {
return this.batchId
}
}

function getIndexId(task: BaseTask) {
Expand Down
5 changes: 1 addition & 4 deletions garden-service/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { deline } from "../util/string"
import { splitLast, exec } from "../util/util"
import { LogEntry } from "../logger/log-entry"
import parseGitConfig from "parse-git-config"
import { Logger } from "../logger/logger"

export function getCommitIdFromRefList(refList: string[]): string {
try {
Expand Down Expand Up @@ -387,11 +386,9 @@ export class GitHandler extends VcsHandler {
return submodules
}

async getOriginName() {
async getOriginName(log: LogEntry) {
const cwd = process.cwd()
const log = Logger.getInstance().placeholder()
const git = this.gitCli(log, cwd)

return (await git("config", "--get", "remote.origin.url"))[0]
}
}
Loading

0 comments on commit 202453c

Please sign in to comment.