// Helper to get sites for autocomplete const getAutoCompleteSites = (answers, lando, input = null) => { if (!_.isEmpty(pantheonSites)) { return lando.Promise.resolve(pantheonSites).filter(site => _.startsWith(site.name, input)); } else { const api = new PantheonApiClient(answers['pantheon-auth'], lando.log); return api.auth().then(() => api.getSites().map(site => ({name: site.name, value: site.name}))).then(sites => { pantheonSites = sites; return pantheonSites; }); }; }
/* * Post new meta data for metrics. */ api.post('/metrics/v2/:id', handler((req, res) => { return Promise.map(plugins, plugin => { const reporter = new plugin.Reporter(config[plugin.config]); return reporter.ping() .then(() => reporter.report(_.merge({}, req.body, {instance: req.params.id}))) .then(() => log.info('Reported to %s', plugin.name)) .then(() => reporter.close()); }); }));
async crawlZhihuList(listURL: string, crawlerDto: CrawlerDto) { const jsonResult = await axios.get(listURL); if (jsonResult.data && jsonResult.data.data) { await bluebird.map(jsonResult.data.data, (articleItem) => { return this.crawlPage(articleItem.url, crawlerDto); }, { concurrency: 20 }); } }
Promise.map(action.devices, async (deviceSelector) => { try { const device = self.stateManager.get('device', deviceSelector); const deviceFeature = getDeviceFeature( device, DEVICE_FEATURE_CATEGORIES.LIGHT, DEVICE_FEATURE_TYPES.LIGHT.BINARY, ); await self.device.setValue(device, deviceFeature, 1); } catch (e) { logger.warn(e); } })
/** * @description Delete all CalDAV calendars and events. * @param {Object} userId - Gladys user to clean up. * @returns {Promise} Resolving with client connected. * @example * cleanUp(user.id) */ async function cleanUp(userId) { logger.info(`Start cleaning CalDAV data for user: ${userId}`); const gladysCalendars = await this.gladys.calendar.get(userId, { serviceId: this.serviceId }); logger.info(`${gladysCalendars.length} calendars to clean`); await Promise.map(gladysCalendars, (calendar) => this.gladys.calendar.destroy(calendar.selector), { concurrency: 2 }); }
/* * Helper to load in all app plugins */ const loadPlugins = (app, lando) => Promise.resolve(app.plugins.registry) // Filter out .filter(plugin => _.has(plugin, 'app')) // LOADEM! .map(plugin => app.plugins.load(plugin, plugin.app, app, lando)) // Remove any naughty shit .map(plugin => _.pick(plugin.data, ['config', 'composeData', 'env', 'labels'])) // Merge minotaur .each(result => _.merge(app, result))
/** * @description Poll all devices of one frequency. * @param {number} pollFrequency - The frequency in milliseconds. * @returns {function()} Return a function. * @example * pollAll(60000); */ function pollAll(pollFrequency) { return async () => { if (this.devicesByPollFrequency[pollFrequency]) { return Promise.map(this.devicesByPollFrequency[pollFrequency], (device) => this.poll(device)); } return Promise.resolve(); }; }
async crawlJuejinList(listURL: string, crawlerDto: CrawlerDto) { let postData; try { postData = JSON.parse(crawlerDto.postData); } catch (err) { throw new MyHttpException({ errorCode: ErrorCode.ParamsError.CODE, }); } const jsonResult = await axios.post(listURL, postData, { headers: { 'X-Agent': 'Juejin/Web', }, }); if (jsonResult.data && jsonResult.data.data) { await bluebird.map(jsonResult.data.data.articleFeed.items.edges, (articleItem) => { return this.crawlPage(articleItem.node.originalUrl, crawlerDto); }, { concurrency: 20 }); } }
/** * @description Execute an array of array of action. * @param {Object} self - Reference to the SceneManager. * @param {Object} actions - An array of array of actions from the db. * @param {Object} scope - The scope passed to all actions. * @returns {Promise} Resolve if the action was executed with success. * @example * executeActions(this, actions, {}); */ async function executeActions(self, actions, scope) { // first array level should be executed in serie await Promise.mapSeries(actions, async (parallelActions, columnIndex) => { // then, second level is executed in parallel await Promise.map(parallelActions, async (action, rowIndex) => { await executeAction(self, action, scope, columnIndex, rowIndex); }); }); return null; }
Promise.map(action.devices, async (deviceSelector) => { try { const device = self.stateManager.get('device', deviceSelector); const deviceFeature = getDeviceFeature( device, DEVICE_FEATURE_CATEGORIES.SWITCH, DEVICE_FEATURE_TYPES.SWITCH.BINARY, ); await self.device.setValue(device, deviceFeature, 1); } catch (e) { logger.warn(e); } })
/* * Post new meta data for metrics. */ api.post('/metrics/v2/:id', handler((req, res) => { return Promise.map(plugins, plugin => { const reporter = new plugin.Reporter(config[plugin.config]); return reporter.ping() .then(() => reporter.report(_.merge({}, req.body, {instance: req.params.id}))) .then(() => log.info('Reported to %s', plugin.name)) .then(() => reporter.close()); }); }));
/** * @description Delete all CalDAV calendars and events. * @param {Object} userId - Gladys user to clean up. * @returns {Promise} Resolving with client connected. * @example * cleanUp(user.id) */ async function cleanUp(userId) { logger.info(`Start cleaning CalDAV data for user: ${userId}`); const gladysCalendars = await this.gladys.calendar.get(userId, { serviceId: this.serviceId }); logger.info(`${gladysCalendars.length} calendars to clean`); await Promise.map(gladysCalendars, (calendar) => this.gladys.calendar.destroy(calendar.selector), { concurrency: 2 }); }
/** * @description Delete all CalDAV calendars and events. * @param {Object} userId - Gladys user to clean up. * @returns {Promise} Resolving with client connected. * @example * cleanUp(user.id) */ async function cleanUp(userId) { logger.info(`Start cleaning CalDAV data for user: ${userId}`); const gladysCalendars = await this.gladys.calendar.get(userId, { serviceId: this.serviceId }); logger.info(`${gladysCalendars.length} calendars to clean`); await Promise.map(gladysCalendars, (calendar) => this.gladys.calendar.destroy(calendar.selector), { concurrency: 2 }); }
Promise.map(action.devices, async (deviceSelector) => { try { const device = self.stateManager.get('device', deviceSelector); const deviceFeature = getDeviceFeature( device, DEVICE_FEATURE_CATEGORIES.LIGHT, DEVICE_FEATURE_TYPES.LIGHT.BINARY, ); await self.device.setValue(device, deviceFeature, 0); } catch (e) { logger.warn(e); } })
Promise.map(action.devices, async (deviceSelector) => { try { const device = self.stateManager.get('device', deviceSelector); const deviceFeature = getDeviceFeature( device, DEVICE_FEATURE_CATEGORIES.SWITCH, DEVICE_FEATURE_TYPES.SWITCH.BINARY, ); await self.device.setValue(device, deviceFeature, 0); } catch (e) { logger.warn(e); } })