file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
39k
suffix
large_stringlengths
0
36.1k
middle
large_stringlengths
0
29.4k
fim_type
large_stringclasses
4 values
index.ts
/*! * Copyright 2019 Kopano and its contributors * * Use of this source code is governed by a MIT license * that can be found in the LICENSE.txt file. * * @author Kopano <https://kopano.com> * @license MIT * @preserve */ import { API, Glue, Controller, ICallData, IInitData, IEmbeddOptions, IEnableOptions, IPayload, IReadyData, } from './glue'; import { getGlueParameter, queueMicroTask, setGlueParameter, } from './utils'; /** * Embeds the provided url into the provided container by creating an iframe * which gets appended as child to the provided container. * * @param url URL of the app to be embedded with Glue. * @param container DOM node where the iframe element is appended as child. * @param options Embedded options. */ async function embed(url: string, container: Element, options?: IEmbeddOptions): Promise<Glue> { const state: { glue?: Glue; beforeInitResolve?: (value?: unknown) => void; beforeInitReject?: (reason?: unknown) => void; retryTimer?: ReturnType<typeof setTimeout>; } = {}; return new Promise((resolve, reject) => { // Add default option values and ensure options. options = { timeout: 5000, sandboxRestrictions: 'allow-forms allow-popups allow-popups-to-escape-sandbox allow-scripts allow-same-origin', featurePolicy: 'animations; autoplay; camera; encrypted-media; fullscreen; geolocation; microphone; speaker; vr', ...options, } const src = new URL(url, window.location.href); const origin = options.origin ? options.origin : src.origin; const features = options.features; const mode = options.mode ? options.mode : ''; // Create glue controller. const controller = new Controller({ origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'init': { if (state.retryTimer) { clearTimeout(state.retryTimer); } const data = message.data as IInitData; const reply: IInitData = { features: features ? Object.keys(features) : [], }; const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (data.features) { data.features.forEach(action => { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } }); } state.glue = controller.Glue({api, mode}); if (options && options.onBeforeInit) { const p = new Promise((resolve, reject) => { state.beforeInitResolve = resolve; state.beforeInitReject = reject; }); if (!state.beforeInitResolve || !state.beforeInitReject) { throw new Error('glue init promise error'); } try { const action = options.onBeforeInit(state.glue, p); if (action) { if (!data.features || !data.features.includes(action)) { state.beforeInitReject(new Error(`unsupported action: ${action}`)); } else { reply.action = action; } } } catch (err) { reject(new Error(`onInit failed: ${err}`)); return; } } return reply; } case 'ready': { if (!state.glue) { throw new Error('failed to glue: no state'); } //const glue = controller.Glue(state.api); const data = message.data as IReadyData; if (options && options.onBeforeInit && state.beforeInitResolve && state.beforeInitReject) { if (data.ready) { await state.beforeInitResolve(data.data); } else { await state.beforeInitReject(data.data); } resolve(state.glue); } else { if (data.ready) { resolve(state.glue); } else { if (data.error) { throw new Error(`failed to glue: ${data.data}`) } else { reject(state.glue); } } } break; } case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (embed) unknown message type: ${message.type}`); } }, }); // Create iframe. const ownerDocument = container.ownerDocument !== null ? container.ownerDocument : document; const frame = ownerDocument.createElement('iframe'); if (options && options.className) { frame.className = options.className; } if (options.sandboxRestrictions) { frame.setAttribute('sandbox', options.sandboxRestrictions); } if (options.featurePolicy) { frame.setAttribute('allow', options.featurePolicy); } if (options && options.attributes) { Object.entries(options.attributes).forEach(([key, value]) => { frame.setAttribute(key, value); }); } // Prepare URL and set it to element. setGlueParameter(src, 'mode', mode); if (origin !== window.origin) { // Cross origin, add glue origin hash parameter to allow white list // checks on the other end. setGlueParameter(src, 'origin', origin); } frame.setAttribute('src', src.toString()); // Append iframe with timeout and retry. const append = (): void => { // Inject iframe and attach glue. container.appendChild(frame); if (!frame.contentWindow) { throw new Error('new frame has no contentWindow'); } controller.attach(frame.contentWindow); } const retry = (): void => { controller.detach(); container.removeChild(frame); setTimeout(() => { append(); }, 1000); // NOTE(longsleep): Retry time hardcoded - is it needed to have a configuration? } frame.addEventListener('load', () => { if (state.glue)
if (options && options.timeout) { state.retryTimer = setTimeout(() => { if (!state.glue) { retry(); } }, options.timeout); } else { reject(new Error('glue timeout')); } }); append(); }); } /** * Enables glue for the provided sourceWindow with options. * * @param sourceWindow Window element of the app which is using Glue embed. * @param options Enable options. */ async function enable(sourceWindow?: Window, options?: IEnableOptions): Promise<Glue> { return new Promise((resolve, reject) => { if (!sourceWindow) { sourceWindow = window.parent; } // Get glue mode. const mode = getGlueParameter('mode'); if (sourceWindow === self || mode === null) { // Return empty Glue API if we are self, or glue mode is not set. It // this means Glue is not active. resolve(new Glue({})); return; } // Add default option values and ensure options. options = { timeout: 5000, ...options, }; // Validate origin. const expectedOrigin = getGlueParameter('origin'); if (expectedOrigin) { if (expectedOrigin !== window.origin) { // Validate white list if cross origin. if (!options || !options.origins || !options.origins.includes('expectedOrigin')) { throw new Error('glue origin is not allowed'); } } } // Create glue controller. const features = options.features; const controller = new Controller({ origin: expectedOrigin ? expectedOrigin : window.origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (enable) unknown message type: ${message.type}`) } }, }); // Attach glue. controller.attach(sourceWindow); // Start timeout. let failed = false; const timer = setTimeout(() => { failed = true; reject(new Error('glue timeout')); }, options.timeout); // Start initialization. queueMicroTask(() => { const request: IInitData = { features: features ? Object.keys(features) : [], mode, } controller.postMessage('init', request).then(async (initData?: IInitData): Promise<void> => { clearTimeout(timer); if (failed) { // Do nothing when flagged failed. return; } if (!initData || initData.error) { // TODO(longsleep): Initialization failed. What now? reject(new Error(`glue init received error: ${initData ? initData.error : 'no data'}`)); return; } const readyData: IReadyData = { ready: true, } // Create API action handlers. const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (initData.features) { for (const action of initData.features) { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } } } // Create glue. const glue = controller.Glue({api, mode}); // Trigger onInit hook. if (options && options.onInit) { try { options.onInit(glue, initData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Trigger initial requested action. if (readyData.ready && initData.action) { // Trigger action, this action is set when initializing and it // is triggered before the app reports ready. if (features && features[initData.action]) { const handler = features[initData.action]; try { const result = await handler(); if (result !== undefined) { readyData.data = result; } } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } else { readyData.ready = false; readyData.error = true; readyData.data = new Error(`unknown glue action: ${initData.action}`); } } // Trigger beforeReady hook. if (options && options.onBeforeReady) { try { options.onBeforeReady(glue, readyData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Reply with redy result. controller.postMessage('ready', readyData).then((): void => { resolve(glue); }); }).catch((reason: unknown)=> { throw new Error(`glue init failed: ${reason}`); }); }); }); } export { Glue, Controller, embed, enable, } export default Glue;
{ delete state.glue; }
conditional_block
index.ts
/*! * Copyright 2019 Kopano and its contributors * * Use of this source code is governed by a MIT license * that can be found in the LICENSE.txt file. * * @author Kopano <https://kopano.com> * @license MIT * @preserve */ import { API, Glue, Controller, ICallData, IInitData, IEmbeddOptions, IEnableOptions, IPayload, IReadyData, } from './glue'; import { getGlueParameter, queueMicroTask, setGlueParameter, } from './utils'; /** * Embeds the provided url into the provided container by creating an iframe * which gets appended as child to the provided container. * * @param url URL of the app to be embedded with Glue. * @param container DOM node where the iframe element is appended as child. * @param options Embedded options. */ async function
(url: string, container: Element, options?: IEmbeddOptions): Promise<Glue> { const state: { glue?: Glue; beforeInitResolve?: (value?: unknown) => void; beforeInitReject?: (reason?: unknown) => void; retryTimer?: ReturnType<typeof setTimeout>; } = {}; return new Promise((resolve, reject) => { // Add default option values and ensure options. options = { timeout: 5000, sandboxRestrictions: 'allow-forms allow-popups allow-popups-to-escape-sandbox allow-scripts allow-same-origin', featurePolicy: 'animations; autoplay; camera; encrypted-media; fullscreen; geolocation; microphone; speaker; vr', ...options, } const src = new URL(url, window.location.href); const origin = options.origin ? options.origin : src.origin; const features = options.features; const mode = options.mode ? options.mode : ''; // Create glue controller. const controller = new Controller({ origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'init': { if (state.retryTimer) { clearTimeout(state.retryTimer); } const data = message.data as IInitData; const reply: IInitData = { features: features ? Object.keys(features) : [], }; const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (data.features) { data.features.forEach(action => { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } }); } state.glue = controller.Glue({api, mode}); if (options && options.onBeforeInit) { const p = new Promise((resolve, reject) => { state.beforeInitResolve = resolve; state.beforeInitReject = reject; }); if (!state.beforeInitResolve || !state.beforeInitReject) { throw new Error('glue init promise error'); } try { const action = options.onBeforeInit(state.glue, p); if (action) { if (!data.features || !data.features.includes(action)) { state.beforeInitReject(new Error(`unsupported action: ${action}`)); } else { reply.action = action; } } } catch (err) { reject(new Error(`onInit failed: ${err}`)); return; } } return reply; } case 'ready': { if (!state.glue) { throw new Error('failed to glue: no state'); } //const glue = controller.Glue(state.api); const data = message.data as IReadyData; if (options && options.onBeforeInit && state.beforeInitResolve && state.beforeInitReject) { if (data.ready) { await state.beforeInitResolve(data.data); } else { await state.beforeInitReject(data.data); } resolve(state.glue); } else { if (data.ready) { resolve(state.glue); } else { if (data.error) { throw new Error(`failed to glue: ${data.data}`) } else { reject(state.glue); } } } break; } case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (embed) unknown message type: ${message.type}`); } }, }); // Create iframe. const ownerDocument = container.ownerDocument !== null ? container.ownerDocument : document; const frame = ownerDocument.createElement('iframe'); if (options && options.className) { frame.className = options.className; } if (options.sandboxRestrictions) { frame.setAttribute('sandbox', options.sandboxRestrictions); } if (options.featurePolicy) { frame.setAttribute('allow', options.featurePolicy); } if (options && options.attributes) { Object.entries(options.attributes).forEach(([key, value]) => { frame.setAttribute(key, value); }); } // Prepare URL and set it to element. setGlueParameter(src, 'mode', mode); if (origin !== window.origin) { // Cross origin, add glue origin hash parameter to allow white list // checks on the other end. setGlueParameter(src, 'origin', origin); } frame.setAttribute('src', src.toString()); // Append iframe with timeout and retry. const append = (): void => { // Inject iframe and attach glue. container.appendChild(frame); if (!frame.contentWindow) { throw new Error('new frame has no contentWindow'); } controller.attach(frame.contentWindow); } const retry = (): void => { controller.detach(); container.removeChild(frame); setTimeout(() => { append(); }, 1000); // NOTE(longsleep): Retry time hardcoded - is it needed to have a configuration? } frame.addEventListener('load', () => { if (state.glue) { delete state.glue; } if (options && options.timeout) { state.retryTimer = setTimeout(() => { if (!state.glue) { retry(); } }, options.timeout); } else { reject(new Error('glue timeout')); } }); append(); }); } /** * Enables glue for the provided sourceWindow with options. * * @param sourceWindow Window element of the app which is using Glue embed. * @param options Enable options. */ async function enable(sourceWindow?: Window, options?: IEnableOptions): Promise<Glue> { return new Promise((resolve, reject) => { if (!sourceWindow) { sourceWindow = window.parent; } // Get glue mode. const mode = getGlueParameter('mode'); if (sourceWindow === self || mode === null) { // Return empty Glue API if we are self, or glue mode is not set. It // this means Glue is not active. resolve(new Glue({})); return; } // Add default option values and ensure options. options = { timeout: 5000, ...options, }; // Validate origin. const expectedOrigin = getGlueParameter('origin'); if (expectedOrigin) { if (expectedOrigin !== window.origin) { // Validate white list if cross origin. if (!options || !options.origins || !options.origins.includes('expectedOrigin')) { throw new Error('glue origin is not allowed'); } } } // Create glue controller. const features = options.features; const controller = new Controller({ origin: expectedOrigin ? expectedOrigin : window.origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (enable) unknown message type: ${message.type}`) } }, }); // Attach glue. controller.attach(sourceWindow); // Start timeout. let failed = false; const timer = setTimeout(() => { failed = true; reject(new Error('glue timeout')); }, options.timeout); // Start initialization. queueMicroTask(() => { const request: IInitData = { features: features ? Object.keys(features) : [], mode, } controller.postMessage('init', request).then(async (initData?: IInitData): Promise<void> => { clearTimeout(timer); if (failed) { // Do nothing when flagged failed. return; } if (!initData || initData.error) { // TODO(longsleep): Initialization failed. What now? reject(new Error(`glue init received error: ${initData ? initData.error : 'no data'}`)); return; } const readyData: IReadyData = { ready: true, } // Create API action handlers. const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (initData.features) { for (const action of initData.features) { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } } } // Create glue. const glue = controller.Glue({api, mode}); // Trigger onInit hook. if (options && options.onInit) { try { options.onInit(glue, initData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Trigger initial requested action. if (readyData.ready && initData.action) { // Trigger action, this action is set when initializing and it // is triggered before the app reports ready. if (features && features[initData.action]) { const handler = features[initData.action]; try { const result = await handler(); if (result !== undefined) { readyData.data = result; } } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } else { readyData.ready = false; readyData.error = true; readyData.data = new Error(`unknown glue action: ${initData.action}`); } } // Trigger beforeReady hook. if (options && options.onBeforeReady) { try { options.onBeforeReady(glue, readyData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Reply with redy result. controller.postMessage('ready', readyData).then((): void => { resolve(glue); }); }).catch((reason: unknown)=> { throw new Error(`glue init failed: ${reason}`); }); }); }); } export { Glue, Controller, embed, enable, } export default Glue;
embed
identifier_name
index.ts
/*! * Copyright 2019 Kopano and its contributors * * Use of this source code is governed by a MIT license * that can be found in the LICENSE.txt file. * * @author Kopano <https://kopano.com> * @license MIT * @preserve */ import { API, Glue, Controller, ICallData, IInitData, IEmbeddOptions, IEnableOptions, IPayload, IReadyData, } from './glue'; import { getGlueParameter, queueMicroTask, setGlueParameter, } from './utils'; /** * Embeds the provided url into the provided container by creating an iframe * which gets appended as child to the provided container. * * @param url URL of the app to be embedded with Glue. * @param container DOM node where the iframe element is appended as child. * @param options Embedded options. */ async function embed(url: string, container: Element, options?: IEmbeddOptions): Promise<Glue> { const state: { glue?: Glue; beforeInitResolve?: (value?: unknown) => void; beforeInitReject?: (reason?: unknown) => void; retryTimer?: ReturnType<typeof setTimeout>; } = {}; return new Promise((resolve, reject) => { // Add default option values and ensure options. options = { timeout: 5000, sandboxRestrictions: 'allow-forms allow-popups allow-popups-to-escape-sandbox allow-scripts allow-same-origin', featurePolicy: 'animations; autoplay; camera; encrypted-media; fullscreen; geolocation; microphone; speaker; vr', ...options, } const src = new URL(url, window.location.href); const origin = options.origin ? options.origin : src.origin; const features = options.features; const mode = options.mode ? options.mode : ''; // Create glue controller. const controller = new Controller({ origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'init': { if (state.retryTimer) { clearTimeout(state.retryTimer); } const data = message.data as IInitData; const reply: IInitData = { features: features ? Object.keys(features) : [], }; const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (data.features) { data.features.forEach(action => { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } }); } state.glue = controller.Glue({api, mode}); if (options && options.onBeforeInit) { const p = new Promise((resolve, reject) => { state.beforeInitResolve = resolve; state.beforeInitReject = reject; }); if (!state.beforeInitResolve || !state.beforeInitReject) { throw new Error('glue init promise error'); } try { const action = options.onBeforeInit(state.glue, p); if (action) { if (!data.features || !data.features.includes(action)) { state.beforeInitReject(new Error(`unsupported action: ${action}`)); } else { reply.action = action; } } } catch (err) { reject(new Error(`onInit failed: ${err}`)); return; } } return reply; } case 'ready': { if (!state.glue) { throw new Error('failed to glue: no state'); } //const glue = controller.Glue(state.api); const data = message.data as IReadyData; if (options && options.onBeforeInit && state.beforeInitResolve && state.beforeInitReject) { if (data.ready) { await state.beforeInitResolve(data.data); } else { await state.beforeInitReject(data.data); } resolve(state.glue); } else { if (data.ready) { resolve(state.glue); } else { if (data.error) { throw new Error(`failed to glue: ${data.data}`) } else { reject(state.glue); } } } break; } case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (embed) unknown message type: ${message.type}`); } }, }); // Create iframe. const ownerDocument = container.ownerDocument !== null ? container.ownerDocument : document; const frame = ownerDocument.createElement('iframe'); if (options && options.className) { frame.className = options.className; } if (options.sandboxRestrictions) { frame.setAttribute('sandbox', options.sandboxRestrictions); } if (options.featurePolicy) { frame.setAttribute('allow', options.featurePolicy); } if (options && options.attributes) { Object.entries(options.attributes).forEach(([key, value]) => { frame.setAttribute(key, value); }); } // Prepare URL and set it to element. setGlueParameter(src, 'mode', mode); if (origin !== window.origin) { // Cross origin, add glue origin hash parameter to allow white list // checks on the other end. setGlueParameter(src, 'origin', origin); } frame.setAttribute('src', src.toString()); // Append iframe with timeout and retry. const append = (): void => { // Inject iframe and attach glue. container.appendChild(frame); if (!frame.contentWindow) { throw new Error('new frame has no contentWindow'); } controller.attach(frame.contentWindow); } const retry = (): void => { controller.detach(); container.removeChild(frame); setTimeout(() => { append();
} frame.addEventListener('load', () => { if (state.glue) { delete state.glue; } if (options && options.timeout) { state.retryTimer = setTimeout(() => { if (!state.glue) { retry(); } }, options.timeout); } else { reject(new Error('glue timeout')); } }); append(); }); } /** * Enables glue for the provided sourceWindow with options. * * @param sourceWindow Window element of the app which is using Glue embed. * @param options Enable options. */ async function enable(sourceWindow?: Window, options?: IEnableOptions): Promise<Glue> { return new Promise((resolve, reject) => { if (!sourceWindow) { sourceWindow = window.parent; } // Get glue mode. const mode = getGlueParameter('mode'); if (sourceWindow === self || mode === null) { // Return empty Glue API if we are self, or glue mode is not set. It // this means Glue is not active. resolve(new Glue({})); return; } // Add default option values and ensure options. options = { timeout: 5000, ...options, }; // Validate origin. const expectedOrigin = getGlueParameter('origin'); if (expectedOrigin) { if (expectedOrigin !== window.origin) { // Validate white list if cross origin. if (!options || !options.origins || !options.origins.includes('expectedOrigin')) { throw new Error('glue origin is not allowed'); } } } // Create glue controller. const features = options.features; const controller = new Controller({ origin: expectedOrigin ? expectedOrigin : window.origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (enable) unknown message type: ${message.type}`) } }, }); // Attach glue. controller.attach(sourceWindow); // Start timeout. let failed = false; const timer = setTimeout(() => { failed = true; reject(new Error('glue timeout')); }, options.timeout); // Start initialization. queueMicroTask(() => { const request: IInitData = { features: features ? Object.keys(features) : [], mode, } controller.postMessage('init', request).then(async (initData?: IInitData): Promise<void> => { clearTimeout(timer); if (failed) { // Do nothing when flagged failed. return; } if (!initData || initData.error) { // TODO(longsleep): Initialization failed. What now? reject(new Error(`glue init received error: ${initData ? initData.error : 'no data'}`)); return; } const readyData: IReadyData = { ready: true, } // Create API action handlers. const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (initData.features) { for (const action of initData.features) { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } } } // Create glue. const glue = controller.Glue({api, mode}); // Trigger onInit hook. if (options && options.onInit) { try { options.onInit(glue, initData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Trigger initial requested action. if (readyData.ready && initData.action) { // Trigger action, this action is set when initializing and it // is triggered before the app reports ready. if (features && features[initData.action]) { const handler = features[initData.action]; try { const result = await handler(); if (result !== undefined) { readyData.data = result; } } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } else { readyData.ready = false; readyData.error = true; readyData.data = new Error(`unknown glue action: ${initData.action}`); } } // Trigger beforeReady hook. if (options && options.onBeforeReady) { try { options.onBeforeReady(glue, readyData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Reply with redy result. controller.postMessage('ready', readyData).then((): void => { resolve(glue); }); }).catch((reason: unknown)=> { throw new Error(`glue init failed: ${reason}`); }); }); }); } export { Glue, Controller, embed, enable, } export default Glue;
}, 1000); // NOTE(longsleep): Retry time hardcoded - is it needed to have a configuration?
random_line_split
index.ts
/*! * Copyright 2019 Kopano and its contributors * * Use of this source code is governed by a MIT license * that can be found in the LICENSE.txt file. * * @author Kopano <https://kopano.com> * @license MIT * @preserve */ import { API, Glue, Controller, ICallData, IInitData, IEmbeddOptions, IEnableOptions, IPayload, IReadyData, } from './glue'; import { getGlueParameter, queueMicroTask, setGlueParameter, } from './utils'; /** * Embeds the provided url into the provided container by creating an iframe * which gets appended as child to the provided container. * * @param url URL of the app to be embedded with Glue. * @param container DOM node where the iframe element is appended as child. * @param options Embedded options. */ async function embed(url: string, container: Element, options?: IEmbeddOptions): Promise<Glue> { const state: { glue?: Glue; beforeInitResolve?: (value?: unknown) => void; beforeInitReject?: (reason?: unknown) => void; retryTimer?: ReturnType<typeof setTimeout>; } = {}; return new Promise((resolve, reject) => { // Add default option values and ensure options. options = { timeout: 5000, sandboxRestrictions: 'allow-forms allow-popups allow-popups-to-escape-sandbox allow-scripts allow-same-origin', featurePolicy: 'animations; autoplay; camera; encrypted-media; fullscreen; geolocation; microphone; speaker; vr', ...options, } const src = new URL(url, window.location.href); const origin = options.origin ? options.origin : src.origin; const features = options.features; const mode = options.mode ? options.mode : ''; // Create glue controller. const controller = new Controller({ origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'init': { if (state.retryTimer) { clearTimeout(state.retryTimer); } const data = message.data as IInitData; const reply: IInitData = { features: features ? Object.keys(features) : [], }; const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (data.features) { data.features.forEach(action => { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } }); } state.glue = controller.Glue({api, mode}); if (options && options.onBeforeInit) { const p = new Promise((resolve, reject) => { state.beforeInitResolve = resolve; state.beforeInitReject = reject; }); if (!state.beforeInitResolve || !state.beforeInitReject) { throw new Error('glue init promise error'); } try { const action = options.onBeforeInit(state.glue, p); if (action) { if (!data.features || !data.features.includes(action)) { state.beforeInitReject(new Error(`unsupported action: ${action}`)); } else { reply.action = action; } } } catch (err) { reject(new Error(`onInit failed: ${err}`)); return; } } return reply; } case 'ready': { if (!state.glue) { throw new Error('failed to glue: no state'); } //const glue = controller.Glue(state.api); const data = message.data as IReadyData; if (options && options.onBeforeInit && state.beforeInitResolve && state.beforeInitReject) { if (data.ready) { await state.beforeInitResolve(data.data); } else { await state.beforeInitReject(data.data); } resolve(state.glue); } else { if (data.ready) { resolve(state.glue); } else { if (data.error) { throw new Error(`failed to glue: ${data.data}`) } else { reject(state.glue); } } } break; } case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (embed) unknown message type: ${message.type}`); } }, }); // Create iframe. const ownerDocument = container.ownerDocument !== null ? container.ownerDocument : document; const frame = ownerDocument.createElement('iframe'); if (options && options.className) { frame.className = options.className; } if (options.sandboxRestrictions) { frame.setAttribute('sandbox', options.sandboxRestrictions); } if (options.featurePolicy) { frame.setAttribute('allow', options.featurePolicy); } if (options && options.attributes) { Object.entries(options.attributes).forEach(([key, value]) => { frame.setAttribute(key, value); }); } // Prepare URL and set it to element. setGlueParameter(src, 'mode', mode); if (origin !== window.origin) { // Cross origin, add glue origin hash parameter to allow white list // checks on the other end. setGlueParameter(src, 'origin', origin); } frame.setAttribute('src', src.toString()); // Append iframe with timeout and retry. const append = (): void => { // Inject iframe and attach glue. container.appendChild(frame); if (!frame.contentWindow) { throw new Error('new frame has no contentWindow'); } controller.attach(frame.contentWindow); } const retry = (): void => { controller.detach(); container.removeChild(frame); setTimeout(() => { append(); }, 1000); // NOTE(longsleep): Retry time hardcoded - is it needed to have a configuration? } frame.addEventListener('load', () => { if (state.glue) { delete state.glue; } if (options && options.timeout) { state.retryTimer = setTimeout(() => { if (!state.glue) { retry(); } }, options.timeout); } else { reject(new Error('glue timeout')); } }); append(); }); } /** * Enables glue for the provided sourceWindow with options. * * @param sourceWindow Window element of the app which is using Glue embed. * @param options Enable options. */ async function enable(sourceWindow?: Window, options?: IEnableOptions): Promise<Glue>
export { Glue, Controller, embed, enable, } export default Glue;
{ return new Promise((resolve, reject) => { if (!sourceWindow) { sourceWindow = window.parent; } // Get glue mode. const mode = getGlueParameter('mode'); if (sourceWindow === self || mode === null) { // Return empty Glue API if we are self, or glue mode is not set. It // this means Glue is not active. resolve(new Glue({})); return; } // Add default option values and ensure options. options = { timeout: 5000, ...options, }; // Validate origin. const expectedOrigin = getGlueParameter('origin'); if (expectedOrigin) { if (expectedOrigin !== window.origin) { // Validate white list if cross origin. if (!options || !options.origins || !options.origins.includes('expectedOrigin')) { throw new Error('glue origin is not allowed'); } } } // Create glue controller. const features = options.features; const controller = new Controller({ origin: expectedOrigin ? expectedOrigin : window.origin, handler: async (message: IPayload): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ switch (message.type) { case 'call': { const data = message.data as ICallData; const handler = features ? features[data.action] : null; if (!handler) { throw new Error(`unknown action: ${data.action}`); } const args = data.args ? data.args : []; return handler(...args); } default: console.debug(`glue (enable) unknown message type: ${message.type}`) } }, }); // Attach glue. controller.attach(sourceWindow); // Start timeout. let failed = false; const timer = setTimeout(() => { failed = true; reject(new Error('glue timeout')); }, options.timeout); // Start initialization. queueMicroTask(() => { const request: IInitData = { features: features ? Object.keys(features) : [], mode, } controller.postMessage('init', request).then(async (initData?: IInitData): Promise<void> => { clearTimeout(timer); if (failed) { // Do nothing when flagged failed. return; } if (!initData || initData.error) { // TODO(longsleep): Initialization failed. What now? reject(new Error(`glue init received error: ${initData ? initData.error : 'no data'}`)); return; } const readyData: IReadyData = { ready: true, } // Create API action handlers. const api = {} as API<{[key: string]: (...args: unknown[]) => Promise<any>}>; /* eslint-disable-line @typescript-eslint/no-explicit-any */ if (initData.features) { for (const action of initData.features) { api[action] = (...args: unknown[]): Promise<any> => { /* eslint-disable-line @typescript-eslint/no-explicit-any */ return controller.callAction(action, args); } } } // Create glue. const glue = controller.Glue({api, mode}); // Trigger onInit hook. if (options && options.onInit) { try { options.onInit(glue, initData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Trigger initial requested action. if (readyData.ready && initData.action) { // Trigger action, this action is set when initializing and it // is triggered before the app reports ready. if (features && features[initData.action]) { const handler = features[initData.action]; try { const result = await handler(); if (result !== undefined) { readyData.data = result; } } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } else { readyData.ready = false; readyData.error = true; readyData.data = new Error(`unknown glue action: ${initData.action}`); } } // Trigger beforeReady hook. if (options && options.onBeforeReady) { try { options.onBeforeReady(glue, readyData); } catch(e) { readyData.ready = false; readyData.error = true; readyData.data = e; } } // Reply with redy result. controller.postMessage('ready', readyData).then((): void => { resolve(glue); }); }).catch((reason: unknown)=> { throw new Error(`glue init failed: ${reason}`); }); }); }); }
identifier_body
day_14.rs
//! This is my solution for [Advent of Code - Day 14](https://adventofcode.com/2020/day/14) - //! _Docking Data_ //! //! This was themed around bitwise operations. The challenge was mostly parsing the puzzle //! description into the bitwise operations needed. This was the first time I needed an Either //! implementation rather than just using an enum as I needed to be able to store the current Mask //! in a variable that is explicitly a Mask rather than an Instruction that could be either a Mask //! or a Mem. use std::fs; use regex::Regex; use im::{HashMap, HashSet}; use either::Either; use either::Either::*; /// The entry point for running the solutions with the 'real' puzzle input. /// /// - The puzzle input is expected to be at `<project_root>/res/day-14-input` /// - It is expected this will be called by [`super::main()`] when the user elects to run day 14. pub fn run() { let contents = fs::read_to_string("res/day-14-input").expect("Failed to read file"); let memory = run_program_v1(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v1 is: {}", sum); let memory = run_program_v2(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v2 is: {}", sum); } /// Representing an input line that overwrites the current bitmask, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mask { mask: usize, data: usize } /// Represents an input line that updates the current memory values, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mem { address: usize, value: usize } /// Parse a line from the puzzle input into structured data /// /// A line will be of one of the two following formats: /// * `mask = 000000000000000000000000000000X1001X` /// * `mem[8] = 11` /// /// ## Masks /// For both parts of the puzzle the mask has two uses, where the character is a `0 `or `1` it /// should be treated a raw data that will in someway override other input, and `X` will be used as /// the mask. It is easier to store this as two bitmaps, one for the data and one for the mask, as /// these are used separately. /// /// ## Memory Updates /// Whilst the two parts use the mask to modify where/what actually gets written `mem[8] = 11` /// should be interpreted as address = 8, value = 11. /// /// # Examples from Tests /// ``` /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111111111111, /// data: 0b000000000000000000000000000000000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") /// ); /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111110111101, /// data: 0b000000000000000000000000000001000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") /// ); /// /// assert_eq!( /// Right(Mem { address: 8, value: 11 }), /// parse_line("mem[8] = 11") /// ); /// assert_eq!( /// Right(Mem { address: 7, value: 101 }), /// parse_line("mem[7] = 101") /// ); /// assert_eq!( /// Right(Mem { address: 8, value: 0 }), /// parse_line("mem[8] = 0") /// ); /// ``` fn parse_line(line: &str) -> Either<Mask, Mem> { let mut parts = line.split(" = "); let inst = parts.next().expect("Invalid line"); let value = parts.next().expect("Invalid line"); if inst == "mask" { let (mask, data) = value.chars().fold( (0usize, 0usize), |(mask, data), char| ( mask << 1 | if char == 'X' { 1 } else { 0 }, data << 1 | if char == '1' { 1 } else { 0 } ), ); Left(Mask { mask, data }) } else { let re = Regex::new(r"^mem\[(\d+)]$").unwrap(); match re.captures(inst) { Some(cap) => Right(Mem { address: cap.get(1).unwrap().as_str().parse::<usize>().unwrap(), value: value.parse::<usize>().unwrap(), }), None => panic!("Invalid line") } } } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 1 protocol /// /// > The current bitmask is applied to values immediately before they are written to memory: a 0 or /// > 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value /// > unchanged. /// /// # Example from Tests /// ``` /// let mut expected: HashMap<usize, usize> = HashMap::new(); /// /// let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; /// /// expected.insert(8, 73); /// assert_eq!(expected, run_program_v1(program_1)); /// /// let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X /// mem[8] = 11 /// mem[7] = 101 /// mem[8] = 0"; /// /// expected.insert(7, 101); /// expected.insert(8, 64); /// let memory = run_program_v1(program_2); /// /// assert_eq!(expected, memory); /// /// assert_eq!(165usize, sum_memory(memory)); /// ``` fn run_program_v1(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => { memory.insert( address, value & current_mask.mask | current_mask.data, ); } } } return memory; } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 2 protocol. /// /// > Immediately before a value is written to memory, each bit in the bitmask modifies the /// > corresponding bit of the destination memory address in the following way: /// > - If the bitmask bit is 0, the corresponding memory address bit is unchanged. /// > - If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1. /// > - If the bitmask bit is X, the corresponding memory address bit is floating. /// > /// > A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, /// > this means the floating bits will take on all possible values, potentially causing many memory /// > addresses to be written all at once! /// /// The set of addresses a mask will write to is given by [`explode_addresses`] /// /// # Example from Tests /// ``` /// let program = "mask = 000000000000000000000000000000X1001X /// mem[42] = 100 /// mask = 00000000000000000000000000000000X0XX /// mem[26] = 1"; /// /// let memory = run_program_v2(program); /// assert_eq!(208usize, sum_memory(memory)); /// ``` fn run_program_v2(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => for address in explode_addresses(&current_mask, address) { memory.insert(address, value); }, } } return memory; } /// Because floating bits can take on any value, this returns all the addresses that a given mask /// applied to the input address refers to. /// /// 1. The base address is the address where all the `X` values in the mask are `0`. Additionally /// bits where the mask data is 1 all should be 1 for all addresses in the final output i.e. /// `(input | mask.data) & !mask.mask` /// 2. Iterate through the bits, and where the mask is `X` add an additional address to each of the /// existing combinations for the address where that bit is `1` rather than `0`, so the set /// doubles in size each time we encounter an `X`. With some boiler plate as the existing set /// can't be appended to as it's being iterated. /// /// # Examples from Tests /// ``` /// let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &Mask { /// mask: 0b000000000000000000000000000000100001, /// data: 0b000000000000000000000000000000010010, /// }, /// 42, /// ) /// ); /// /// let expected: HashSet<usize> = /// vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) /// .into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &parse_line("mask = 00000000000000000000000000000000X0XX") /// .expect_left("Failed to parse as mask"), /// 26, /// ) /// ); /// ``` fn explode_addresses(mask: &Mask, input: usize) -> HashSet<usize> { let mut addresses = HashSet::new(); addresses.insert((input | mask.data) & !mask.mask); for i in 0..36 { if (1 << i) & mask.mask != 0 {
for &address in addresses.iter() { new_addresses.insert(address | (1 << i)); } for &new_address in new_addresses.iter() { addresses.insert(new_address); }; } } addresses } /// Sum a memory snapshot /// /// Both puzzle parts finally sum all the memory registers into a single number as the expected /// answer. Extracted into a function to avoid repetition. fn sum_memory(memory: HashMap<usize, usize>) -> usize { memory.iter().map(|(_, v)| *v).sum() } #[cfg(test)] mod tests { use day_14::{parse_line, Mask, Mem, run_program_v1, sum_memory, explode_addresses, run_program_v2}; use either::Either::*; use im::{HashMap, HashSet}; //noinspection SpellCheckingInspection #[test] fn can_parse() { assert_eq!( Left(Mask { mask: 0b111111111111111111111111111111111111, data: 0b000000000000000000000000000000000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") ); assert_eq!( Left(Mask { mask: 0b111111111111111111111111111110111101, data: 0b000000000000000000000000000001000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") ); assert_eq!( Right(Mem { address: 8, value: 11 }), parse_line("mem[8] = 11") ); assert_eq!( Right(Mem { address: 7, value: 101 }), parse_line("mem[7] = 101") ); assert_eq!( Right(Mem { address: 8, value: 0 }), parse_line("mem[8] = 0") ); } //noinspection SpellCheckingInspection #[test] fn can_run_program_v1() { let mut expected: HashMap<usize, usize> = HashMap::new(); let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; expected.insert(8, 73); assert_eq!(expected, run_program_v1(program_1)); let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X mem[8] = 11 mem[7] = 101 mem[8] = 0"; expected.insert(7, 101); expected.insert(8, 64); let memory = run_program_v1(program_2); assert_eq!(expected, memory); assert_eq!(165usize, sum_memory(memory)); } #[test] fn can_explode_addresses() { let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); assert_eq!( expected, explode_addresses( &Mask { mask: 0b000000000000000000000000000000100001, data: 0b000000000000000000000000000000010010, }, 42, ) ); let expected: HashSet<usize> = vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) .into_iter().collect(); assert_eq!( expected, explode_addresses( &parse_line("mask = 00000000000000000000000000000000X0XX") .expect_left("Failed to parse as mask"), 26, ) ); } #[test] fn can_run_program_v2() { let program = "mask = 000000000000000000000000000000X1001X mem[42] = 100 mask = 00000000000000000000000000000000X0XX mem[26] = 1"; let memory = run_program_v2(program); assert_eq!(208usize, sum_memory(memory)); } }
let mut new_addresses = HashSet::new();
random_line_split
day_14.rs
//! This is my solution for [Advent of Code - Day 14](https://adventofcode.com/2020/day/14) - //! _Docking Data_ //! //! This was themed around bitwise operations. The challenge was mostly parsing the puzzle //! description into the bitwise operations needed. This was the first time I needed an Either //! implementation rather than just using an enum as I needed to be able to store the current Mask //! in a variable that is explicitly a Mask rather than an Instruction that could be either a Mask //! or a Mem. use std::fs; use regex::Regex; use im::{HashMap, HashSet}; use either::Either; use either::Either::*; /// The entry point for running the solutions with the 'real' puzzle input. /// /// - The puzzle input is expected to be at `<project_root>/res/day-14-input` /// - It is expected this will be called by [`super::main()`] when the user elects to run day 14. pub fn run() { let contents = fs::read_to_string("res/day-14-input").expect("Failed to read file"); let memory = run_program_v1(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v1 is: {}", sum); let memory = run_program_v2(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v2 is: {}", sum); } /// Representing an input line that overwrites the current bitmask, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mask { mask: usize, data: usize } /// Represents an input line that updates the current memory values, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mem { address: usize, value: usize } /// Parse a line from the puzzle input into structured data /// /// A line will be of one of the two following formats: /// * `mask = 000000000000000000000000000000X1001X` /// * `mem[8] = 11` /// /// ## Masks /// For both parts of the puzzle the mask has two uses, where the character is a `0 `or `1` it /// should be treated a raw data that will in someway override other input, and `X` will be used as /// the mask. It is easier to store this as two bitmaps, one for the data and one for the mask, as /// these are used separately. /// /// ## Memory Updates /// Whilst the two parts use the mask to modify where/what actually gets written `mem[8] = 11` /// should be interpreted as address = 8, value = 11. /// /// # Examples from Tests /// ``` /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111111111111, /// data: 0b000000000000000000000000000000000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") /// ); /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111110111101, /// data: 0b000000000000000000000000000001000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") /// ); /// /// assert_eq!( /// Right(Mem { address: 8, value: 11 }), /// parse_line("mem[8] = 11") /// ); /// assert_eq!( /// Right(Mem { address: 7, value: 101 }), /// parse_line("mem[7] = 101") /// ); /// assert_eq!( /// Right(Mem { address: 8, value: 0 }), /// parse_line("mem[8] = 0") /// ); /// ``` fn parse_line(line: &str) -> Either<Mask, Mem> { let mut parts = line.split(" = "); let inst = parts.next().expect("Invalid line"); let value = parts.next().expect("Invalid line"); if inst == "mask" { let (mask, data) = value.chars().fold( (0usize, 0usize), |(mask, data), char| ( mask << 1 | if char == 'X' { 1 } else { 0 }, data << 1 | if char == '1' { 1 } else { 0 } ), ); Left(Mask { mask, data }) } else { let re = Regex::new(r"^mem\[(\d+)]$").unwrap(); match re.captures(inst) { Some(cap) => Right(Mem { address: cap.get(1).unwrap().as_str().parse::<usize>().unwrap(), value: value.parse::<usize>().unwrap(), }), None => panic!("Invalid line") } } } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 1 protocol /// /// > The current bitmask is applied to values immediately before they are written to memory: a 0 or /// > 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value /// > unchanged. /// /// # Example from Tests /// ``` /// let mut expected: HashMap<usize, usize> = HashMap::new(); /// /// let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; /// /// expected.insert(8, 73); /// assert_eq!(expected, run_program_v1(program_1)); /// /// let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X /// mem[8] = 11 /// mem[7] = 101 /// mem[8] = 0"; /// /// expected.insert(7, 101); /// expected.insert(8, 64); /// let memory = run_program_v1(program_2); /// /// assert_eq!(expected, memory); /// /// assert_eq!(165usize, sum_memory(memory)); /// ``` fn run_program_v1(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => { memory.insert( address, value & current_mask.mask | current_mask.data, ); } } } return memory; } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 2 protocol. /// /// > Immediately before a value is written to memory, each bit in the bitmask modifies the /// > corresponding bit of the destination memory address in the following way: /// > - If the bitmask bit is 0, the corresponding memory address bit is unchanged. /// > - If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1. /// > - If the bitmask bit is X, the corresponding memory address bit is floating. /// > /// > A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, /// > this means the floating bits will take on all possible values, potentially causing many memory /// > addresses to be written all at once! /// /// The set of addresses a mask will write to is given by [`explode_addresses`] /// /// # Example from Tests /// ``` /// let program = "mask = 000000000000000000000000000000X1001X /// mem[42] = 100 /// mask = 00000000000000000000000000000000X0XX /// mem[26] = 1"; /// /// let memory = run_program_v2(program); /// assert_eq!(208usize, sum_memory(memory)); /// ``` fn run_program_v2(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => for address in explode_addresses(&current_mask, address) { memory.insert(address, value); }, } } return memory; } /// Because floating bits can take on any value, this returns all the addresses that a given mask /// applied to the input address refers to. /// /// 1. The base address is the address where all the `X` values in the mask are `0`. Additionally /// bits where the mask data is 1 all should be 1 for all addresses in the final output i.e. /// `(input | mask.data) & !mask.mask` /// 2. Iterate through the bits, and where the mask is `X` add an additional address to each of the /// existing combinations for the address where that bit is `1` rather than `0`, so the set /// doubles in size each time we encounter an `X`. With some boiler plate as the existing set /// can't be appended to as it's being iterated. /// /// # Examples from Tests /// ``` /// let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &Mask { /// mask: 0b000000000000000000000000000000100001, /// data: 0b000000000000000000000000000000010010, /// }, /// 42, /// ) /// ); /// /// let expected: HashSet<usize> = /// vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) /// .into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &parse_line("mask = 00000000000000000000000000000000X0XX") /// .expect_left("Failed to parse as mask"), /// 26, /// ) /// ); /// ``` fn explode_addresses(mask: &Mask, input: usize) -> HashSet<usize> { let mut addresses = HashSet::new(); addresses.insert((input | mask.data) & !mask.mask); for i in 0..36 { if (1 << i) & mask.mask != 0 { let mut new_addresses = HashSet::new(); for &address in addresses.iter() { new_addresses.insert(address | (1 << i)); } for &new_address in new_addresses.iter() { addresses.insert(new_address); }; } } addresses } /// Sum a memory snapshot /// /// Both puzzle parts finally sum all the memory registers into a single number as the expected /// answer. Extracted into a function to avoid repetition. fn sum_memory(memory: HashMap<usize, usize>) -> usize { memory.iter().map(|(_, v)| *v).sum() } #[cfg(test)] mod tests { use day_14::{parse_line, Mask, Mem, run_program_v1, sum_memory, explode_addresses, run_program_v2}; use either::Either::*; use im::{HashMap, HashSet}; //noinspection SpellCheckingInspection #[test] fn can_parse() { assert_eq!( Left(Mask { mask: 0b111111111111111111111111111111111111, data: 0b000000000000000000000000000000000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") ); assert_eq!( Left(Mask { mask: 0b111111111111111111111111111110111101, data: 0b000000000000000000000000000001000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") ); assert_eq!( Right(Mem { address: 8, value: 11 }), parse_line("mem[8] = 11") ); assert_eq!( Right(Mem { address: 7, value: 101 }), parse_line("mem[7] = 101") ); assert_eq!( Right(Mem { address: 8, value: 0 }), parse_line("mem[8] = 0") ); } //noinspection SpellCheckingInspection #[test] fn can_run_program_v1()
#[test] fn can_explode_addresses() { let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); assert_eq!( expected, explode_addresses( &Mask { mask: 0b000000000000000000000000000000100001, data: 0b000000000000000000000000000000010010, }, 42, ) ); let expected: HashSet<usize> = vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) .into_iter().collect(); assert_eq!( expected, explode_addresses( &parse_line("mask = 00000000000000000000000000000000X0XX") .expect_left("Failed to parse as mask"), 26, ) ); } #[test] fn can_run_program_v2() { let program = "mask = 000000000000000000000000000000X1001X mem[42] = 100 mask = 00000000000000000000000000000000X0XX mem[26] = 1"; let memory = run_program_v2(program); assert_eq!(208usize, sum_memory(memory)); } }
{ let mut expected: HashMap<usize, usize> = HashMap::new(); let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; expected.insert(8, 73); assert_eq!(expected, run_program_v1(program_1)); let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X mem[8] = 11 mem[7] = 101 mem[8] = 0"; expected.insert(7, 101); expected.insert(8, 64); let memory = run_program_v1(program_2); assert_eq!(expected, memory); assert_eq!(165usize, sum_memory(memory)); }
identifier_body
day_14.rs
//! This is my solution for [Advent of Code - Day 14](https://adventofcode.com/2020/day/14) - //! _Docking Data_ //! //! This was themed around bitwise operations. The challenge was mostly parsing the puzzle //! description into the bitwise operations needed. This was the first time I needed an Either //! implementation rather than just using an enum as I needed to be able to store the current Mask //! in a variable that is explicitly a Mask rather than an Instruction that could be either a Mask //! or a Mem. use std::fs; use regex::Regex; use im::{HashMap, HashSet}; use either::Either; use either::Either::*; /// The entry point for running the solutions with the 'real' puzzle input. /// /// - The puzzle input is expected to be at `<project_root>/res/day-14-input` /// - It is expected this will be called by [`super::main()`] when the user elects to run day 14. pub fn run() { let contents = fs::read_to_string("res/day-14-input").expect("Failed to read file"); let memory = run_program_v1(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v1 is: {}", sum); let memory = run_program_v2(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v2 is: {}", sum); } /// Representing an input line that overwrites the current bitmask, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mask { mask: usize, data: usize } /// Represents an input line that updates the current memory values, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mem { address: usize, value: usize } /// Parse a line from the puzzle input into structured data /// /// A line will be of one of the two following formats: /// * `mask = 000000000000000000000000000000X1001X` /// * `mem[8] = 11` /// /// ## Masks /// For both parts of the puzzle the mask has two uses, where the character is a `0 `or `1` it /// should be treated a raw data that will in someway override other input, and `X` will be used as /// the mask. It is easier to store this as two bitmaps, one for the data and one for the mask, as /// these are used separately. /// /// ## Memory Updates /// Whilst the two parts use the mask to modify where/what actually gets written `mem[8] = 11` /// should be interpreted as address = 8, value = 11. /// /// # Examples from Tests /// ``` /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111111111111, /// data: 0b000000000000000000000000000000000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") /// ); /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111110111101, /// data: 0b000000000000000000000000000001000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") /// ); /// /// assert_eq!( /// Right(Mem { address: 8, value: 11 }), /// parse_line("mem[8] = 11") /// ); /// assert_eq!( /// Right(Mem { address: 7, value: 101 }), /// parse_line("mem[7] = 101") /// ); /// assert_eq!( /// Right(Mem { address: 8, value: 0 }), /// parse_line("mem[8] = 0") /// ); /// ``` fn
(line: &str) -> Either<Mask, Mem> { let mut parts = line.split(" = "); let inst = parts.next().expect("Invalid line"); let value = parts.next().expect("Invalid line"); if inst == "mask" { let (mask, data) = value.chars().fold( (0usize, 0usize), |(mask, data), char| ( mask << 1 | if char == 'X' { 1 } else { 0 }, data << 1 | if char == '1' { 1 } else { 0 } ), ); Left(Mask { mask, data }) } else { let re = Regex::new(r"^mem\[(\d+)]$").unwrap(); match re.captures(inst) { Some(cap) => Right(Mem { address: cap.get(1).unwrap().as_str().parse::<usize>().unwrap(), value: value.parse::<usize>().unwrap(), }), None => panic!("Invalid line") } } } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 1 protocol /// /// > The current bitmask is applied to values immediately before they are written to memory: a 0 or /// > 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value /// > unchanged. /// /// # Example from Tests /// ``` /// let mut expected: HashMap<usize, usize> = HashMap::new(); /// /// let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; /// /// expected.insert(8, 73); /// assert_eq!(expected, run_program_v1(program_1)); /// /// let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X /// mem[8] = 11 /// mem[7] = 101 /// mem[8] = 0"; /// /// expected.insert(7, 101); /// expected.insert(8, 64); /// let memory = run_program_v1(program_2); /// /// assert_eq!(expected, memory); /// /// assert_eq!(165usize, sum_memory(memory)); /// ``` fn run_program_v1(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => { memory.insert( address, value & current_mask.mask | current_mask.data, ); } } } return memory; } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 2 protocol. /// /// > Immediately before a value is written to memory, each bit in the bitmask modifies the /// > corresponding bit of the destination memory address in the following way: /// > - If the bitmask bit is 0, the corresponding memory address bit is unchanged. /// > - If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1. /// > - If the bitmask bit is X, the corresponding memory address bit is floating. /// > /// > A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, /// > this means the floating bits will take on all possible values, potentially causing many memory /// > addresses to be written all at once! /// /// The set of addresses a mask will write to is given by [`explode_addresses`] /// /// # Example from Tests /// ``` /// let program = "mask = 000000000000000000000000000000X1001X /// mem[42] = 100 /// mask = 00000000000000000000000000000000X0XX /// mem[26] = 1"; /// /// let memory = run_program_v2(program); /// assert_eq!(208usize, sum_memory(memory)); /// ``` fn run_program_v2(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => for address in explode_addresses(&current_mask, address) { memory.insert(address, value); }, } } return memory; } /// Because floating bits can take on any value, this returns all the addresses that a given mask /// applied to the input address refers to. /// /// 1. The base address is the address where all the `X` values in the mask are `0`. Additionally /// bits where the mask data is 1 all should be 1 for all addresses in the final output i.e. /// `(input | mask.data) & !mask.mask` /// 2. Iterate through the bits, and where the mask is `X` add an additional address to each of the /// existing combinations for the address where that bit is `1` rather than `0`, so the set /// doubles in size each time we encounter an `X`. With some boiler plate as the existing set /// can't be appended to as it's being iterated. /// /// # Examples from Tests /// ``` /// let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &Mask { /// mask: 0b000000000000000000000000000000100001, /// data: 0b000000000000000000000000000000010010, /// }, /// 42, /// ) /// ); /// /// let expected: HashSet<usize> = /// vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) /// .into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &parse_line("mask = 00000000000000000000000000000000X0XX") /// .expect_left("Failed to parse as mask"), /// 26, /// ) /// ); /// ``` fn explode_addresses(mask: &Mask, input: usize) -> HashSet<usize> { let mut addresses = HashSet::new(); addresses.insert((input | mask.data) & !mask.mask); for i in 0..36 { if (1 << i) & mask.mask != 0 { let mut new_addresses = HashSet::new(); for &address in addresses.iter() { new_addresses.insert(address | (1 << i)); } for &new_address in new_addresses.iter() { addresses.insert(new_address); }; } } addresses } /// Sum a memory snapshot /// /// Both puzzle parts finally sum all the memory registers into a single number as the expected /// answer. Extracted into a function to avoid repetition. fn sum_memory(memory: HashMap<usize, usize>) -> usize { memory.iter().map(|(_, v)| *v).sum() } #[cfg(test)] mod tests { use day_14::{parse_line, Mask, Mem, run_program_v1, sum_memory, explode_addresses, run_program_v2}; use either::Either::*; use im::{HashMap, HashSet}; //noinspection SpellCheckingInspection #[test] fn can_parse() { assert_eq!( Left(Mask { mask: 0b111111111111111111111111111111111111, data: 0b000000000000000000000000000000000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") ); assert_eq!( Left(Mask { mask: 0b111111111111111111111111111110111101, data: 0b000000000000000000000000000001000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") ); assert_eq!( Right(Mem { address: 8, value: 11 }), parse_line("mem[8] = 11") ); assert_eq!( Right(Mem { address: 7, value: 101 }), parse_line("mem[7] = 101") ); assert_eq!( Right(Mem { address: 8, value: 0 }), parse_line("mem[8] = 0") ); } //noinspection SpellCheckingInspection #[test] fn can_run_program_v1() { let mut expected: HashMap<usize, usize> = HashMap::new(); let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; expected.insert(8, 73); assert_eq!(expected, run_program_v1(program_1)); let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X mem[8] = 11 mem[7] = 101 mem[8] = 0"; expected.insert(7, 101); expected.insert(8, 64); let memory = run_program_v1(program_2); assert_eq!(expected, memory); assert_eq!(165usize, sum_memory(memory)); } #[test] fn can_explode_addresses() { let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); assert_eq!( expected, explode_addresses( &Mask { mask: 0b000000000000000000000000000000100001, data: 0b000000000000000000000000000000010010, }, 42, ) ); let expected: HashSet<usize> = vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) .into_iter().collect(); assert_eq!( expected, explode_addresses( &parse_line("mask = 00000000000000000000000000000000X0XX") .expect_left("Failed to parse as mask"), 26, ) ); } #[test] fn can_run_program_v2() { let program = "mask = 000000000000000000000000000000X1001X mem[42] = 100 mask = 00000000000000000000000000000000X0XX mem[26] = 1"; let memory = run_program_v2(program); assert_eq!(208usize, sum_memory(memory)); } }
parse_line
identifier_name
day_14.rs
//! This is my solution for [Advent of Code - Day 14](https://adventofcode.com/2020/day/14) - //! _Docking Data_ //! //! This was themed around bitwise operations. The challenge was mostly parsing the puzzle //! description into the bitwise operations needed. This was the first time I needed an Either //! implementation rather than just using an enum as I needed to be able to store the current Mask //! in a variable that is explicitly a Mask rather than an Instruction that could be either a Mask //! or a Mem. use std::fs; use regex::Regex; use im::{HashMap, HashSet}; use either::Either; use either::Either::*; /// The entry point for running the solutions with the 'real' puzzle input. /// /// - The puzzle input is expected to be at `<project_root>/res/day-14-input` /// - It is expected this will be called by [`super::main()`] when the user elects to run day 14. pub fn run() { let contents = fs::read_to_string("res/day-14-input").expect("Failed to read file"); let memory = run_program_v1(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v1 is: {}", sum); let memory = run_program_v2(contents.as_str()); let sum = sum_memory(memory); println!("The sum of memory values after running the program v2 is: {}", sum); } /// Representing an input line that overwrites the current bitmask, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mask { mask: usize, data: usize } /// Represents an input line that updates the current memory values, see [`parse_line`]. #[derive(Debug, Eq, PartialEq)] struct Mem { address: usize, value: usize } /// Parse a line from the puzzle input into structured data /// /// A line will be of one of the two following formats: /// * `mask = 000000000000000000000000000000X1001X` /// * `mem[8] = 11` /// /// ## Masks /// For both parts of the puzzle the mask has two uses, where the character is a `0 `or `1` it /// should be treated a raw data that will in someway override other input, and `X` will be used as /// the mask. It is easier to store this as two bitmaps, one for the data and one for the mask, as /// these are used separately. /// /// ## Memory Updates /// Whilst the two parts use the mask to modify where/what actually gets written `mem[8] = 11` /// should be interpreted as address = 8, value = 11. /// /// # Examples from Tests /// ``` /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111111111111, /// data: 0b000000000000000000000000000000000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") /// ); /// assert_eq!( /// Left(Mask { /// mask: 0b111111111111111111111111111110111101, /// data: 0b000000000000000000000000000001000000, /// }), /// parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") /// ); /// /// assert_eq!( /// Right(Mem { address: 8, value: 11 }), /// parse_line("mem[8] = 11") /// ); /// assert_eq!( /// Right(Mem { address: 7, value: 101 }), /// parse_line("mem[7] = 101") /// ); /// assert_eq!( /// Right(Mem { address: 8, value: 0 }), /// parse_line("mem[8] = 0") /// ); /// ``` fn parse_line(line: &str) -> Either<Mask, Mem> { let mut parts = line.split(" = "); let inst = parts.next().expect("Invalid line"); let value = parts.next().expect("Invalid line"); if inst == "mask"
else { let re = Regex::new(r"^mem\[(\d+)]$").unwrap(); match re.captures(inst) { Some(cap) => Right(Mem { address: cap.get(1).unwrap().as_str().parse::<usize>().unwrap(), value: value.parse::<usize>().unwrap(), }), None => panic!("Invalid line") } } } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 1 protocol /// /// > The current bitmask is applied to values immediately before they are written to memory: a 0 or /// > 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value /// > unchanged. /// /// # Example from Tests /// ``` /// let mut expected: HashMap<usize, usize> = HashMap::new(); /// /// let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; /// /// expected.insert(8, 73); /// assert_eq!(expected, run_program_v1(program_1)); /// /// let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X /// mem[8] = 11 /// mem[7] = 101 /// mem[8] = 0"; /// /// expected.insert(7, 101); /// expected.insert(8, 64); /// let memory = run_program_v1(program_2); /// /// assert_eq!(expected, memory); /// /// assert_eq!(165usize, sum_memory(memory)); /// ``` fn run_program_v1(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => { memory.insert( address, value & current_mask.mask | current_mask.data, ); } } } return memory; } /// Takes the string input and returns the memory state after that has been interpreted using the /// part 2 protocol. /// /// > Immediately before a value is written to memory, each bit in the bitmask modifies the /// > corresponding bit of the destination memory address in the following way: /// > - If the bitmask bit is 0, the corresponding memory address bit is unchanged. /// > - If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1. /// > - If the bitmask bit is X, the corresponding memory address bit is floating. /// > /// > A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, /// > this means the floating bits will take on all possible values, potentially causing many memory /// > addresses to be written all at once! /// /// The set of addresses a mask will write to is given by [`explode_addresses`] /// /// # Example from Tests /// ``` /// let program = "mask = 000000000000000000000000000000X1001X /// mem[42] = 100 /// mask = 00000000000000000000000000000000X0XX /// mem[26] = 1"; /// /// let memory = run_program_v2(program); /// assert_eq!(208usize, sum_memory(memory)); /// ``` fn run_program_v2(program: &str) -> HashMap<usize, usize> { let mut memory = HashMap::new(); let mut current_mask = Mask { mask: 0, data: 0 }; for line in program.lines() { match parse_line(line) { Left(Mask { mask, data }) => current_mask = Mask { mask, data }, Right(Mem { address, value }) => for address in explode_addresses(&current_mask, address) { memory.insert(address, value); }, } } return memory; } /// Because floating bits can take on any value, this returns all the addresses that a given mask /// applied to the input address refers to. /// /// 1. The base address is the address where all the `X` values in the mask are `0`. Additionally /// bits where the mask data is 1 all should be 1 for all addresses in the final output i.e. /// `(input | mask.data) & !mask.mask` /// 2. Iterate through the bits, and where the mask is `X` add an additional address to each of the /// existing combinations for the address where that bit is `1` rather than `0`, so the set /// doubles in size each time we encounter an `X`. With some boiler plate as the existing set /// can't be appended to as it's being iterated. /// /// # Examples from Tests /// ``` /// let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &Mask { /// mask: 0b000000000000000000000000000000100001, /// data: 0b000000000000000000000000000000010010, /// }, /// 42, /// ) /// ); /// /// let expected: HashSet<usize> = /// vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) /// .into_iter().collect(); /// assert_eq!( /// expected, /// explode_addresses( /// &parse_line("mask = 00000000000000000000000000000000X0XX") /// .expect_left("Failed to parse as mask"), /// 26, /// ) /// ); /// ``` fn explode_addresses(mask: &Mask, input: usize) -> HashSet<usize> { let mut addresses = HashSet::new(); addresses.insert((input | mask.data) & !mask.mask); for i in 0..36 { if (1 << i) & mask.mask != 0 { let mut new_addresses = HashSet::new(); for &address in addresses.iter() { new_addresses.insert(address | (1 << i)); } for &new_address in new_addresses.iter() { addresses.insert(new_address); }; } } addresses } /// Sum a memory snapshot /// /// Both puzzle parts finally sum all the memory registers into a single number as the expected /// answer. Extracted into a function to avoid repetition. fn sum_memory(memory: HashMap<usize, usize>) -> usize { memory.iter().map(|(_, v)| *v).sum() } #[cfg(test)] mod tests { use day_14::{parse_line, Mask, Mem, run_program_v1, sum_memory, explode_addresses, run_program_v2}; use either::Either::*; use im::{HashMap, HashSet}; //noinspection SpellCheckingInspection #[test] fn can_parse() { assert_eq!( Left(Mask { mask: 0b111111111111111111111111111111111111, data: 0b000000000000000000000000000000000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") ); assert_eq!( Left(Mask { mask: 0b111111111111111111111111111110111101, data: 0b000000000000000000000000000001000000, }), parse_line("mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X") ); assert_eq!( Right(Mem { address: 8, value: 11 }), parse_line("mem[8] = 11") ); assert_eq!( Right(Mem { address: 7, value: 101 }), parse_line("mem[7] = 101") ); assert_eq!( Right(Mem { address: 8, value: 0 }), parse_line("mem[8] = 0") ); } //noinspection SpellCheckingInspection #[test] fn can_run_program_v1() { let mut expected: HashMap<usize, usize> = HashMap::new(); let program_1 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11"; expected.insert(8, 73); assert_eq!(expected, run_program_v1(program_1)); let program_2 = "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X mem[8] = 11 mem[7] = 101 mem[8] = 0"; expected.insert(7, 101); expected.insert(8, 64); let memory = run_program_v1(program_2); assert_eq!(expected, memory); assert_eq!(165usize, sum_memory(memory)); } #[test] fn can_explode_addresses() { let expected: HashSet<usize> = vec!(26usize, 27usize, 58usize, 59usize).into_iter().collect(); assert_eq!( expected, explode_addresses( &Mask { mask: 0b000000000000000000000000000000100001, data: 0b000000000000000000000000000000010010, }, 42, ) ); let expected: HashSet<usize> = vec!(16usize, 17usize, 18usize, 19usize, 24usize, 25usize, 26usize, 27usize) .into_iter().collect(); assert_eq!( expected, explode_addresses( &parse_line("mask = 00000000000000000000000000000000X0XX") .expect_left("Failed to parse as mask"), 26, ) ); } #[test] fn can_run_program_v2() { let program = "mask = 000000000000000000000000000000X1001X mem[42] = 100 mask = 00000000000000000000000000000000X0XX mem[26] = 1"; let memory = run_program_v2(program); assert_eq!(208usize, sum_memory(memory)); } }
{ let (mask, data) = value.chars().fold( (0usize, 0usize), |(mask, data), char| ( mask << 1 | if char == 'X' { 1 } else { 0 }, data << 1 | if char == '1' { 1 } else { 0 } ), ); Left(Mask { mask, data }) }
conditional_block
schedule.rs
use crate::{ borrow::{Exclusive, RefMut}, command::CommandBuffer, resource::ResourceTypeId, storage::ComponentTypeId, world::World, }; use bit_set::BitSet; use itertools::izip; use std::iter::repeat; use std::{ collections::{HashMap, HashSet}, sync::atomic::{AtomicUsize, Ordering}, }; #[cfg(feature = "par-iter")] use rayon::prelude::*; /// Empty trait which defines a `System` as schedulable by the dispatcher - this requires that the /// type is both `Send` and `Sync`. /// /// This is automatically implemented for all types that implement `Runnable` which meet the requirements. pub trait Schedulable: Runnable + Send + Sync {} impl<T> Schedulable for T where T: Runnable + Send + Sync {} /// Describes which archetypes a system declares access to. pub enum ArchetypeAccess { /// All archetypes. All, /// Some archetypes. Some(BitSet), } impl ArchetypeAccess { pub fn is_disjoint(&self, other: &ArchetypeAccess) -> bool { match self { Self::All => false, Self::Some(mine) => match other { Self::All => false, Self::Some(theirs) => mine.is_disjoint(theirs), }, } } } /// Trait describing a schedulable type. This is implemented by `System` pub trait Runnable { fn name(&self) -> &str; fn reads(&self) -> (&[ResourceTypeId], &[ComponentTypeId]); fn writes(&self) -> (&[ResourceTypeId], &[ComponentTypeId]); fn prepare(&mut self, world: &World); fn accesses_archetypes(&self) -> &ArchetypeAccess; fn run(&self, world: &World); fn dispose(self: Box<Self>, world: &mut World); fn command_buffer_mut(&self) -> RefMut<Exclusive, CommandBuffer>; } /// Stages represent discrete steps of a game's loop, such as "start", "update", "draw", "end", etc. /// Stages have a defined execution order. /// /// Systems run within a stage, and commit any buffered changes to the ecs at the end of a stage /// (which may or may not be the stage within which they run, but cannot be an earlier stage). trait Stage: Copy + PartialOrd + Ord + PartialEq + Eq {} /// Executes all systems that are to be run within a single given stage. pub struct StageExecutor<'a> { systems: &'a mut [Box<dyn Schedulable>], #[cfg(feature = "par-iter")] pool: &'a rayon::ThreadPool, #[cfg(feature = "par-iter")] static_dependants: Vec<Vec<usize>>, #[cfg(feature = "par-iter")] dynamic_dependants: Vec<Vec<usize>>, #[cfg(feature = "par-iter")] static_dependency_counts: Vec<AtomicUsize>, #[cfg(feature = "par-iter")] awaiting: Vec<AtomicUsize>, } impl<'a> StageExecutor<'a> { #[cfg(not(feature = "par-iter"))] pub fn new(systems: &'a mut [Box<dyn Schedulable>]) -> Self { Self { systems } } /// Constructs a new executor for all systems to be run in a single stage. /// /// Systems are provided in the order in which side-effects (e.g. writes to resources or entities) /// are to be observed. #[cfg(feature = "par-iter")] #[allow(clippy::cognitive_complexity)] // TODO: we should break this up pub fn new(systems: &'a mut [Box<dyn Schedulable>], pool: &'a rayon::ThreadPool) -> Self { if systems.len() > 1 { let mut static_dependency_counts = Vec::with_capacity(systems.len()); let mut static_dependants: Vec<Vec<_>> = repeat(Vec::with_capacity(64)).take(systems.len()).collect(); let mut dynamic_dependants: Vec<Vec<_>> = repeat(Vec::with_capacity(64)).take(systems.len()).collect(); let mut resource_last_mutated = HashMap::<ResourceTypeId, usize>::with_capacity(64); let mut resource_last_read = HashMap::<ResourceTypeId, usize>::with_capacity(64);
let mut component_mutated = HashMap::<ComponentTypeId, Vec<usize>>::with_capacity(64); for (i, system) in systems.iter().enumerate() { log::debug!("Building dependency: {}", system.name()); let (read_res, read_comp) = system.reads(); let (write_res, write_comp) = system.writes(); // find resource access dependencies let mut dependencies = HashSet::with_capacity(64); for res in read_res { log::trace!("Read resource: {:?}", res); if let Some(n) = resource_last_mutated.get(res) { dependencies.insert(*n); } resource_last_read.insert(*res, i); } for res in write_res { log::trace!("Write resource: {:?}", res); // Writes have to be exclusive, so we are dependent on reads too if let Some(n) = resource_last_read.get(res) { log::trace!("Added dep: {:?}", n); dependencies.insert(*n); } if let Some(n) = resource_last_mutated.get(res) { log::trace!("Added dep: {:?}", n); dependencies.insert(*n); } resource_last_mutated.insert(*res, i); } static_dependency_counts.push(AtomicUsize::from(dependencies.len())); log::debug!("dependencies: {:?}", dependencies); for dep in dependencies { log::debug!("static_dependants.push: {:?}", dep); static_dependants[dep].push(i); } // find component access dependencies let mut comp_dependencies = HashSet::new(); for comp in read_comp { if let Some(ns) = component_mutated.get(comp) { for n in ns { comp_dependencies.insert(*n); } } } for comp in write_comp { if let Some(ns) = component_mutated.get(comp) { for n in ns { comp_dependencies.insert(*n); } } component_mutated .entry(*comp) .or_insert_with(Vec::new) .push(i); } log::debug!("comp_dependencies: {:?}", &comp_dependencies); for dep in comp_dependencies { dynamic_dependants[dep].push(i); } } if log::log_enabled!(log::Level::Debug) { log::debug!("static_dependants: {:?}", static_dependants); log::debug!("dynamic_dependants: {:?}", dynamic_dependants); } let mut awaiting = Vec::with_capacity(systems.len()); systems .iter() .for_each(|_| awaiting.push(AtomicUsize::new(0))); Self { pool, awaiting, static_dependants, dynamic_dependants, static_dependency_counts, systems, } } else { Self { pool, awaiting: Vec::with_capacity(0), static_dependants: Vec::with_capacity(0), dynamic_dependants: Vec::with_capacity(0), static_dependency_counts: Vec::with_capacity(0), systems, } } } /// This is a linear executor which just runs the system in their given order. /// /// Only enabled with par-iter is disabled #[cfg(not(feature = "par-iter"))] pub fn execute(&mut self, world: &mut World) { self.systems.iter_mut().for_each(|system| { system.run(world); }); // Flush the command buffers of all the systems self.systems.iter().for_each(|system| { system.command_buffer_mut().write(world); }); } /// Executes this stage. Execution is recursively conducted in a draining fashion. Systems are /// ordered based on 1. their resource access, and then 2. their insertion order. systems are /// executed in the pool provided at construction, and this function does not return until all /// systems in this stage have completed. #[cfg(feature = "par-iter")] pub fn execute(&mut self, world: &mut World) { log::trace!("execute"); rayon::join( || {}, || { match self.systems.len() { 1 => { log::trace!("Single system, just run it"); self.systems[0].run(world); } _ => { log::trace!("Begin pool execution"); let systems = &mut self.systems; let static_dependency_counts = &self.static_dependency_counts; let awaiting = &mut self.awaiting; // prepare all systems - archetype filters are pre-executed here systems.par_iter_mut().for_each(|sys| sys.prepare(world)); // determine dynamic dependencies izip!( systems.iter(), self.static_dependants.iter_mut(), self.dynamic_dependants.iter_mut() ) .par_bridge() .for_each(|(sys, static_dep, dyn_dep)| { let archetypes = sys.accesses_archetypes(); for i in (0..dyn_dep.len()).rev() { let dep = dyn_dep[i]; let other = &systems[dep]; // if the archetype sets intersect, // then we can move the dynamic dependant into the static dependants set if !other.accesses_archetypes().is_disjoint(archetypes) { static_dep.push(dep); dyn_dep.swap_remove(i); static_dependency_counts[dep].fetch_add(1, Ordering::Relaxed); } } }); // initialize dependency tracking for (i, count) in static_dependency_counts.iter().enumerate() { awaiting[i].store(count.load(Ordering::Relaxed), Ordering::Relaxed); } log::trace!("Initialized awaiting: {:?}", awaiting); let awaiting = &self.awaiting; // execute all systems with no outstanding dependencies (0..systems.len()) .into_par_iter() .filter(|i| awaiting[*i].load(Ordering::SeqCst) == 0) .for_each(|i| { self.run_recursive(i, world); }); } } }, ); // Flush the command buffers of all the systems self.systems.iter().for_each(|system| { system.command_buffer_mut().write(world); }); } /// Recursively execute through the generated depedency cascade and exhaust it. #[cfg(feature = "par-iter")] fn run_recursive(&self, i: usize, world: &World) { log::trace!("run_recursive: {}", i); self.systems[i].run(world); self.static_dependants[i].par_iter().for_each(|dep| { match self.awaiting[*dep].compare_exchange( 1, std::usize::MAX, Ordering::Relaxed, Ordering::Relaxed, ) { Ok(_) => { self.run_recursive(*dep, world); } Err(_) => { self.awaiting[*dep].fetch_sub(1, Ordering::Relaxed); } } }); } }
random_line_split
schedule.rs
use crate::{ borrow::{Exclusive, RefMut}, command::CommandBuffer, resource::ResourceTypeId, storage::ComponentTypeId, world::World, }; use bit_set::BitSet; use itertools::izip; use std::iter::repeat; use std::{ collections::{HashMap, HashSet}, sync::atomic::{AtomicUsize, Ordering}, }; #[cfg(feature = "par-iter")] use rayon::prelude::*; /// Empty trait which defines a `System` as schedulable by the dispatcher - this requires that the /// type is both `Send` and `Sync`. /// /// This is automatically implemented for all types that implement `Runnable` which meet the requirements. pub trait Schedulable: Runnable + Send + Sync {} impl<T> Schedulable for T where T: Runnable + Send + Sync {} /// Describes which archetypes a system declares access to. pub enum ArchetypeAccess { /// All archetypes. All, /// Some archetypes. Some(BitSet), } impl ArchetypeAccess { pub fn is_disjoint(&self, other: &ArchetypeAccess) -> bool { match self { Self::All => false, Self::Some(mine) => match other { Self::All => false, Self::Some(theirs) => mine.is_disjoint(theirs), }, } } } /// Trait describing a schedulable type. This is implemented by `System` pub trait Runnable { fn name(&self) -> &str; fn reads(&self) -> (&[ResourceTypeId], &[ComponentTypeId]); fn writes(&self) -> (&[ResourceTypeId], &[ComponentTypeId]); fn prepare(&mut self, world: &World); fn accesses_archetypes(&self) -> &ArchetypeAccess; fn run(&self, world: &World); fn dispose(self: Box<Self>, world: &mut World); fn command_buffer_mut(&self) -> RefMut<Exclusive, CommandBuffer>; } /// Stages represent discrete steps of a game's loop, such as "start", "update", "draw", "end", etc. /// Stages have a defined execution order. /// /// Systems run within a stage, and commit any buffered changes to the ecs at the end of a stage /// (which may or may not be the stage within which they run, but cannot be an earlier stage). trait Stage: Copy + PartialOrd + Ord + PartialEq + Eq {} /// Executes all systems that are to be run within a single given stage. pub struct StageExecutor<'a> { systems: &'a mut [Box<dyn Schedulable>], #[cfg(feature = "par-iter")] pool: &'a rayon::ThreadPool, #[cfg(feature = "par-iter")] static_dependants: Vec<Vec<usize>>, #[cfg(feature = "par-iter")] dynamic_dependants: Vec<Vec<usize>>, #[cfg(feature = "par-iter")] static_dependency_counts: Vec<AtomicUsize>, #[cfg(feature = "par-iter")] awaiting: Vec<AtomicUsize>, } impl<'a> StageExecutor<'a> { #[cfg(not(feature = "par-iter"))] pub fn new(systems: &'a mut [Box<dyn Schedulable>]) -> Self { Self { systems } } /// Constructs a new executor for all systems to be run in a single stage. /// /// Systems are provided in the order in which side-effects (e.g. writes to resources or entities) /// are to be observed. #[cfg(feature = "par-iter")] #[allow(clippy::cognitive_complexity)] // TODO: we should break this up pub fn new(systems: &'a mut [Box<dyn Schedulable>], pool: &'a rayon::ThreadPool) -> Self { if systems.len() > 1 { let mut static_dependency_counts = Vec::with_capacity(systems.len()); let mut static_dependants: Vec<Vec<_>> = repeat(Vec::with_capacity(64)).take(systems.len()).collect(); let mut dynamic_dependants: Vec<Vec<_>> = repeat(Vec::with_capacity(64)).take(systems.len()).collect(); let mut resource_last_mutated = HashMap::<ResourceTypeId, usize>::with_capacity(64); let mut resource_last_read = HashMap::<ResourceTypeId, usize>::with_capacity(64); let mut component_mutated = HashMap::<ComponentTypeId, Vec<usize>>::with_capacity(64); for (i, system) in systems.iter().enumerate() { log::debug!("Building dependency: {}", system.name()); let (read_res, read_comp) = system.reads(); let (write_res, write_comp) = system.writes(); // find resource access dependencies let mut dependencies = HashSet::with_capacity(64); for res in read_res { log::trace!("Read resource: {:?}", res); if let Some(n) = resource_last_mutated.get(res) { dependencies.insert(*n); } resource_last_read.insert(*res, i); } for res in write_res { log::trace!("Write resource: {:?}", res); // Writes have to be exclusive, so we are dependent on reads too if let Some(n) = resource_last_read.get(res) { log::trace!("Added dep: {:?}", n); dependencies.insert(*n); } if let Some(n) = resource_last_mutated.get(res) { log::trace!("Added dep: {:?}", n); dependencies.insert(*n); } resource_last_mutated.insert(*res, i); } static_dependency_counts.push(AtomicUsize::from(dependencies.len())); log::debug!("dependencies: {:?}", dependencies); for dep in dependencies { log::debug!("static_dependants.push: {:?}", dep); static_dependants[dep].push(i); } // find component access dependencies let mut comp_dependencies = HashSet::new(); for comp in read_comp { if let Some(ns) = component_mutated.get(comp) { for n in ns { comp_dependencies.insert(*n); } } } for comp in write_comp { if let Some(ns) = component_mutated.get(comp) { for n in ns { comp_dependencies.insert(*n); } } component_mutated .entry(*comp) .or_insert_with(Vec::new) .push(i); } log::debug!("comp_dependencies: {:?}", &comp_dependencies); for dep in comp_dependencies { dynamic_dependants[dep].push(i); } } if log::log_enabled!(log::Level::Debug) { log::debug!("static_dependants: {:?}", static_dependants); log::debug!("dynamic_dependants: {:?}", dynamic_dependants); } let mut awaiting = Vec::with_capacity(systems.len()); systems .iter() .for_each(|_| awaiting.push(AtomicUsize::new(0))); Self { pool, awaiting, static_dependants, dynamic_dependants, static_dependency_counts, systems, } } else { Self { pool, awaiting: Vec::with_capacity(0), static_dependants: Vec::with_capacity(0), dynamic_dependants: Vec::with_capacity(0), static_dependency_counts: Vec::with_capacity(0), systems, } } } /// This is a linear executor which just runs the system in their given order. /// /// Only enabled with par-iter is disabled #[cfg(not(feature = "par-iter"))] pub fn
(&mut self, world: &mut World) { self.systems.iter_mut().for_each(|system| { system.run(world); }); // Flush the command buffers of all the systems self.systems.iter().for_each(|system| { system.command_buffer_mut().write(world); }); } /// Executes this stage. Execution is recursively conducted in a draining fashion. Systems are /// ordered based on 1. their resource access, and then 2. their insertion order. systems are /// executed in the pool provided at construction, and this function does not return until all /// systems in this stage have completed. #[cfg(feature = "par-iter")] pub fn execute(&mut self, world: &mut World) { log::trace!("execute"); rayon::join( || {}, || { match self.systems.len() { 1 => { log::trace!("Single system, just run it"); self.systems[0].run(world); } _ => { log::trace!("Begin pool execution"); let systems = &mut self.systems; let static_dependency_counts = &self.static_dependency_counts; let awaiting = &mut self.awaiting; // prepare all systems - archetype filters are pre-executed here systems.par_iter_mut().for_each(|sys| sys.prepare(world)); // determine dynamic dependencies izip!( systems.iter(), self.static_dependants.iter_mut(), self.dynamic_dependants.iter_mut() ) .par_bridge() .for_each(|(sys, static_dep, dyn_dep)| { let archetypes = sys.accesses_archetypes(); for i in (0..dyn_dep.len()).rev() { let dep = dyn_dep[i]; let other = &systems[dep]; // if the archetype sets intersect, // then we can move the dynamic dependant into the static dependants set if !other.accesses_archetypes().is_disjoint(archetypes) { static_dep.push(dep); dyn_dep.swap_remove(i); static_dependency_counts[dep].fetch_add(1, Ordering::Relaxed); } } }); // initialize dependency tracking for (i, count) in static_dependency_counts.iter().enumerate() { awaiting[i].store(count.load(Ordering::Relaxed), Ordering::Relaxed); } log::trace!("Initialized awaiting: {:?}", awaiting); let awaiting = &self.awaiting; // execute all systems with no outstanding dependencies (0..systems.len()) .into_par_iter() .filter(|i| awaiting[*i].load(Ordering::SeqCst) == 0) .for_each(|i| { self.run_recursive(i, world); }); } } }, ); // Flush the command buffers of all the systems self.systems.iter().for_each(|system| { system.command_buffer_mut().write(world); }); } /// Recursively execute through the generated depedency cascade and exhaust it. #[cfg(feature = "par-iter")] fn run_recursive(&self, i: usize, world: &World) { log::trace!("run_recursive: {}", i); self.systems[i].run(world); self.static_dependants[i].par_iter().for_each(|dep| { match self.awaiting[*dep].compare_exchange( 1, std::usize::MAX, Ordering::Relaxed, Ordering::Relaxed, ) { Ok(_) => { self.run_recursive(*dep, world); } Err(_) => { self.awaiting[*dep].fetch_sub(1, Ordering::Relaxed); } } }); } }
execute
identifier_name
edge.rs
use delaunator::{Point, Triangulation}; use itertools::Itertools; use std::collections::HashMap; #[derive(Clone, Copy)] pub struct Edge { pub vertex1: usize, pub vertex2: usize, pub edge_type1: EdgeType, pub edge_type2: EdgeType, pub active: bool, pub length: f64, } #[derive(PartialEq, Copy, Clone)] pub enum EdgeType { Long, Short, Medium, Empty, } impl Edge { pub fn other(&self, index: usize) -> usize { if self.vertex1 == index { return self.vertex2; } self.vertex1 } pub fn edge_type(&self, perspective: usize) -> EdgeType { if self.vertex1 == perspective { return self.edge_type1; } self.edge_type2 } pub fn new(v1: usize, v2: usize, t1: EdgeType, t2: EdgeType, length: f64) -> Edge { Edge { vertex1: v1, vertex2: v2, edge_type1: t1, edge_type2: t2, active: true, length, } } } impl PartialEq for Edge { fn eq(&self, other: &Self) -> bool { (self.vertex1 == other.vertex1 && self.vertex2 == other.vertex2) || (self.vertex1 == other.vertex2 && self.vertex2 == other.vertex1) } } #[derive(Clone)] pub struct Graph { pub verticies: Vec<Vertex>, pub active_edges: Vec<Edge>, pub mean_std_deviation: f64, } pub trait FindLabel { fn find_label_for(&self, vertex: &Vertex) -> Option<usize>; fn find_label_for_index(&self, index: usize) -> Option<usize>; } pub trait Reasign { fn reasign(&mut self, from: usize, to: usize); } impl Graph { fn edge_is_active(&self, e: usize) -> bool { self.active_edges[e].active } pub fn is_short(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length < from.local_mean - self.mean_std_deviation } pub fn is_long(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length > from.local_mean + self.mean_std_deviation } pub fn calculate_type(&self, from: &Vertex, to: &Vertex) -> EdgeType { if self.is_long(from, to) { return EdgeType::Long; } if self.is_short(from, to) { return EdgeType::Short; } EdgeType::Medium } pub fn filter_edges(&self) -> Graph { let mut result = self.clone(); for edge in result.active_edges.iter_mut() { if edge.edge_type1 != EdgeType::Medium && edge.edge_type2 != EdgeType::Medium { edge.active = false; } } result } fn build_connected_component(&mut self, vertex_index: usize, label: usize)
pub fn calculate_connected_components(&mut self) { let mut cc_index = 1; while let Some(v) = self .verticies .iter_mut() .position(|x| !x.edges.is_empty() && x.label == 0) { self.build_connected_component(v, cc_index); cc_index += 1; } let groups = self.calculate_cc_sizes(); for (label, size) in groups { if size == 1 { for v in 0..self.verticies.len() { if self.verticies[v].label == label { self.verticies[v].label = 0; break; } } } } } fn calculate_cc_sizes(&self) -> HashMap<usize, usize> { let mut cc_sizes: HashMap<usize, usize> = HashMap::new(); for vertex in &self.verticies { *cc_sizes.entry(vertex.label).or_insert(0) += 1; } cc_sizes } fn reassign( &mut self, vertex_index: usize, label: usize, cc_sizes: &mut HashMap<usize, usize>, ) { if self.verticies[vertex_index].label != label { *cc_sizes .get_mut(&self.verticies[vertex_index].label) .unwrap() -= 1; *cc_sizes.get_mut(&label).unwrap() += 1; let vertex = &mut self.verticies[vertex_index]; vertex.label = label; for e in 0..vertex.edges.len() { let edge = self.verticies[vertex_index].edges[e]; let other = self.active_edges[edge].other(vertex_index); if self.active_edges[edge].edge_type(vertex_index) == EdgeType::Short && self.verticies[other].label == label { self.active_edges[edge].active = true; } if self.verticies[other].label != label { self.active_edges[edge].active = false; } } } } pub fn restore_edges(&mut self) { struct LabelReference { size: usize, label: usize, edge_index: usize, }; let mut cc_sizes = self.calculate_cc_sizes(); let mut reassign_map: HashMap<usize, usize> = HashMap::new(); for i in 0..self.verticies.len() { let short_edges: Vec<&Edge> = self.verticies[i] .edges .iter() .filter(|e| self.active_edges[**e].edge_type(i) == EdgeType::Short) .map(|x| &self.active_edges[*x]) .collect(); let label = self.verticies[i].label; let mut possible_labels: Vec<LabelReference> = vec![]; for (i, e) in short_edges.iter().enumerate() { let other_label = self.verticies[e.other(i)].label; if other_label != 0 && label != other_label { let other_size = cc_sizes[&other_label]; if matches!( possible_labels.iter_mut().find(|x| x.label == other_label), None ) { possible_labels.push(LabelReference { size: other_size, label: other_label, edge_index: i, }) } } } if let Some(best_label) = possible_labels.iter().max_by_key(|x| x.size) { if best_label.label != label { *reassign_map.entry(i).or_insert(0) = best_label.label; } } } for (vertex, label) in reassign_map { self.reassign(vertex, label, &mut cc_sizes); } for i in 0..self.verticies.len() { for &edge in self.verticies[i].edges.iter() { if self.active_edges[edge].edge_type(i) == EdgeType::Short && self.verticies[self.active_edges[edge].other(i)].label == self.verticies[i].label { self.active_edges[edge].active = true; } } } } fn recalculate_k_neighbourhood(&mut self, vertex_index: usize) { let mut edge_count: usize = 0; let mut edge_sum = 0.0; for &edge1 in self.verticies[vertex_index].edges.iter() { if self.edge_is_active(edge1) { let other = self.active_edges[edge1].other(vertex_index); for &edge2 in self.verticies[other].edges.iter() { if self.edge_is_active(edge2) { edge_sum += self.active_edges[edge2].length; edge_count += 1; } } } } self.verticies[vertex_index].local_mean = edge_sum / edge_count as f64; self.verticies[vertex_index].local_std_dev = local_std_deviation(vertex_index, &self.active_edges, &self.verticies) } pub fn recalculate_mean_with_k_neighbourhood(&mut self) { for v in 0..self.verticies.len() { self.recalculate_k_neighbourhood(v); self.verticies[v].label = 0; } self.mean_std_deviation = self.mean_std_deviation(); for v in 0..self.verticies.len() { for e in 0..self.verticies[v].edges.len() { let other = self.active_edges[self.verticies[v].edges[e]].other(v); for e2 in 0..self.verticies[other].edges.len() { let is_long = self.active_edges[self.verticies[other].edges[e2]].length > self.verticies[v].local_mean + self.mean_std_deviation; if is_long { self.active_edges[self.verticies[other].edges[e2]].active = false; self.mean_std_deviation = self.mean_std_deviation(); self.verticies[v].local_std_dev = local_std_deviation(v, &self.active_edges, &self.verticies); } } } } } fn mean_std_deviation(&self) -> f64 { self.verticies .iter() .fold(0.0, |acc, v| acc + v.local_std_dev) / self.verticies.len() as f64 } } #[derive(Clone, Debug)] pub struct Vertex { index: usize, pub point: Point, local_mean: f64, local_std_dev: f64, edges: Vec<usize>, pub label: usize, } impl Vertex { fn new(index: usize, point: Point, edges: Vec<usize>) -> Vertex { Vertex { index, point, local_mean: 0.0, local_std_dev: 0.0, edges, label: 0, } } } pub trait ToGraph { fn to_graph(&self, points: &[Point]) -> Graph; } impl ToGraph for Triangulation { fn to_graph(&self, points: &[Point]) -> Graph { let all_edges = all_edges(self, points); let mut verticies: Vec<Vertex> = vec![]; for (i, p) in points.iter().enumerate() { let vertex = Vertex::new(i, Point { x: p.x, y: p.y }, neighborhood(i, &all_edges)); verticies.push(vertex); } for v in verticies.iter_mut() { v.local_mean = local_mean(&all_edges, &v.edges) } for i in 0..points.len() { verticies[i].local_std_dev = local_std_deviation(i, &all_edges, &verticies); } let mut result = Graph { verticies, mean_std_deviation: 0.0, active_edges: all_edges, }; result.mean_std_deviation = result.mean_std_deviation(); for i in 0..result.active_edges.len() { result.active_edges[i].edge_type1 = result.calculate_type( &result.verticies[result.active_edges[i].vertex1], &result.verticies[result.active_edges[i].vertex2], ); result.active_edges[i].edge_type2 = result.calculate_type( &result.verticies[result.active_edges[i].vertex2], &result.verticies[result.active_edges[i].vertex1], ); } result } } fn distance(p1: &Vertex, p2: &Vertex) -> f64 { ((p1.point.x - p2.point.x).powi(2) + (p1.point.y - p2.point.y).powi(2)).sqrt() } fn distance_point(p1: &Point, p2: &Point) -> f64 { ((p1.x - p2.x).powi(2) + (p1.y - p2.y).powi(2)).sqrt() } pub fn all_edges(graph: &Triangulation, points: &[Point]) -> Vec<Edge> { let mut result: Vec<Edge> = vec![]; for t in graph.triangles.iter().batching(|it| match it.next() { None => None, Some(x) => match it.next() { None => None, Some(y) => match it.next() { None => None, Some(z) => Some((*x, *y, *z)), }, }, }) { let e1 = Edge::new( t.0, t.1, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.0], &points[t.1]), ); let e2 = Edge::new( t.1, t.2, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.1], &points[t.2]), ); let e3 = Edge::new( t.2, t.0, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.2], &points[t.0]), ); if !result.contains(&e1) { result.push(e1); } if !result.contains(&e2) { result.push(e2); } if !result.contains(&e3) { result.push(e3); } } result } pub fn edges(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { let mut result: Vec<usize> = vec![]; for (i, edge) in all_edges.iter().enumerate() { if edge.vertex1 == point_index || edge.vertex2 == point_index { result.push(i); } } result } fn neighborhood(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { edges(point_index, all_edges) } pub fn local_mean(edges: &[Edge], edge_indicies: &[usize]) -> f64 { let mut result = 0.0; for index in edge_indicies { result += edges[*index].length; } result / (edge_indicies.len() as f64) } fn local_std_deviation(point_index: usize, edges: &[Edge], points: &[Vertex]) -> f64 { let mut result = 0.0; let current_vertex = &points[point_index]; for edge in current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) { result += (current_vertex.local_mean - edge.length).powi(2) / (current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) .count() as f64); } result.sqrt() }
{ if self.verticies[vertex_index].label != label { self.verticies[vertex_index].label = label; for i in 0..self.verticies[vertex_index].edges.len() { let edge_index = self.verticies[vertex_index].edges[i]; if self.edge_is_active(edge_index) && self.verticies[self.active_edges[edge_index].other(vertex_index)].label == 0 { self.build_connected_component( self.active_edges[edge_index].other(vertex_index), label, ); } } } }
identifier_body
edge.rs
use delaunator::{Point, Triangulation}; use itertools::Itertools; use std::collections::HashMap; #[derive(Clone, Copy)] pub struct Edge { pub vertex1: usize, pub vertex2: usize, pub edge_type1: EdgeType, pub edge_type2: EdgeType, pub active: bool, pub length: f64, } #[derive(PartialEq, Copy, Clone)] pub enum EdgeType { Long, Short, Medium, Empty, } impl Edge { pub fn other(&self, index: usize) -> usize { if self.vertex1 == index { return self.vertex2; } self.vertex1 } pub fn edge_type(&self, perspective: usize) -> EdgeType { if self.vertex1 == perspective { return self.edge_type1; } self.edge_type2 } pub fn new(v1: usize, v2: usize, t1: EdgeType, t2: EdgeType, length: f64) -> Edge { Edge { vertex1: v1, vertex2: v2, edge_type1: t1, edge_type2: t2, active: true, length, } } } impl PartialEq for Edge { fn eq(&self, other: &Self) -> bool { (self.vertex1 == other.vertex1 && self.vertex2 == other.vertex2) || (self.vertex1 == other.vertex2 && self.vertex2 == other.vertex1) } } #[derive(Clone)] pub struct Graph { pub verticies: Vec<Vertex>, pub active_edges: Vec<Edge>, pub mean_std_deviation: f64, } pub trait FindLabel { fn find_label_for(&self, vertex: &Vertex) -> Option<usize>; fn find_label_for_index(&self, index: usize) -> Option<usize>; } pub trait Reasign { fn reasign(&mut self, from: usize, to: usize); } impl Graph { fn edge_is_active(&self, e: usize) -> bool { self.active_edges[e].active } pub fn is_short(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length < from.local_mean - self.mean_std_deviation } pub fn is_long(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length > from.local_mean + self.mean_std_deviation } pub fn calculate_type(&self, from: &Vertex, to: &Vertex) -> EdgeType { if self.is_long(from, to) { return EdgeType::Long; } if self.is_short(from, to) { return EdgeType::Short; } EdgeType::Medium } pub fn filter_edges(&self) -> Graph { let mut result = self.clone(); for edge in result.active_edges.iter_mut() { if edge.edge_type1 != EdgeType::Medium && edge.edge_type2 != EdgeType::Medium { edge.active = false; } } result } fn build_connected_component(&mut self, vertex_index: usize, label: usize) { if self.verticies[vertex_index].label != label { self.verticies[vertex_index].label = label; for i in 0..self.verticies[vertex_index].edges.len() { let edge_index = self.verticies[vertex_index].edges[i]; if self.edge_is_active(edge_index) && self.verticies[self.active_edges[edge_index].other(vertex_index)].label == 0 { self.build_connected_component( self.active_edges[edge_index].other(vertex_index), label, ); } } } } pub fn calculate_connected_components(&mut self) { let mut cc_index = 1; while let Some(v) = self .verticies .iter_mut() .position(|x| !x.edges.is_empty() && x.label == 0) { self.build_connected_component(v, cc_index); cc_index += 1; } let groups = self.calculate_cc_sizes(); for (label, size) in groups { if size == 1 { for v in 0..self.verticies.len() { if self.verticies[v].label == label { self.verticies[v].label = 0; break; } } } } } fn calculate_cc_sizes(&self) -> HashMap<usize, usize> { let mut cc_sizes: HashMap<usize, usize> = HashMap::new(); for vertex in &self.verticies { *cc_sizes.entry(vertex.label).or_insert(0) += 1; } cc_sizes } fn reassign( &mut self, vertex_index: usize, label: usize, cc_sizes: &mut HashMap<usize, usize>, ) { if self.verticies[vertex_index].label != label { *cc_sizes .get_mut(&self.verticies[vertex_index].label) .unwrap() -= 1; *cc_sizes.get_mut(&label).unwrap() += 1; let vertex = &mut self.verticies[vertex_index]; vertex.label = label; for e in 0..vertex.edges.len() { let edge = self.verticies[vertex_index].edges[e]; let other = self.active_edges[edge].other(vertex_index); if self.active_edges[edge].edge_type(vertex_index) == EdgeType::Short && self.verticies[other].label == label {
} } } } pub fn restore_edges(&mut self) { struct LabelReference { size: usize, label: usize, edge_index: usize, }; let mut cc_sizes = self.calculate_cc_sizes(); let mut reassign_map: HashMap<usize, usize> = HashMap::new(); for i in 0..self.verticies.len() { let short_edges: Vec<&Edge> = self.verticies[i] .edges .iter() .filter(|e| self.active_edges[**e].edge_type(i) == EdgeType::Short) .map(|x| &self.active_edges[*x]) .collect(); let label = self.verticies[i].label; let mut possible_labels: Vec<LabelReference> = vec![]; for (i, e) in short_edges.iter().enumerate() { let other_label = self.verticies[e.other(i)].label; if other_label != 0 && label != other_label { let other_size = cc_sizes[&other_label]; if matches!( possible_labels.iter_mut().find(|x| x.label == other_label), None ) { possible_labels.push(LabelReference { size: other_size, label: other_label, edge_index: i, }) } } } if let Some(best_label) = possible_labels.iter().max_by_key(|x| x.size) { if best_label.label != label { *reassign_map.entry(i).or_insert(0) = best_label.label; } } } for (vertex, label) in reassign_map { self.reassign(vertex, label, &mut cc_sizes); } for i in 0..self.verticies.len() { for &edge in self.verticies[i].edges.iter() { if self.active_edges[edge].edge_type(i) == EdgeType::Short && self.verticies[self.active_edges[edge].other(i)].label == self.verticies[i].label { self.active_edges[edge].active = true; } } } } fn recalculate_k_neighbourhood(&mut self, vertex_index: usize) { let mut edge_count: usize = 0; let mut edge_sum = 0.0; for &edge1 in self.verticies[vertex_index].edges.iter() { if self.edge_is_active(edge1) { let other = self.active_edges[edge1].other(vertex_index); for &edge2 in self.verticies[other].edges.iter() { if self.edge_is_active(edge2) { edge_sum += self.active_edges[edge2].length; edge_count += 1; } } } } self.verticies[vertex_index].local_mean = edge_sum / edge_count as f64; self.verticies[vertex_index].local_std_dev = local_std_deviation(vertex_index, &self.active_edges, &self.verticies) } pub fn recalculate_mean_with_k_neighbourhood(&mut self) { for v in 0..self.verticies.len() { self.recalculate_k_neighbourhood(v); self.verticies[v].label = 0; } self.mean_std_deviation = self.mean_std_deviation(); for v in 0..self.verticies.len() { for e in 0..self.verticies[v].edges.len() { let other = self.active_edges[self.verticies[v].edges[e]].other(v); for e2 in 0..self.verticies[other].edges.len() { let is_long = self.active_edges[self.verticies[other].edges[e2]].length > self.verticies[v].local_mean + self.mean_std_deviation; if is_long { self.active_edges[self.verticies[other].edges[e2]].active = false; self.mean_std_deviation = self.mean_std_deviation(); self.verticies[v].local_std_dev = local_std_deviation(v, &self.active_edges, &self.verticies); } } } } } fn mean_std_deviation(&self) -> f64 { self.verticies .iter() .fold(0.0, |acc, v| acc + v.local_std_dev) / self.verticies.len() as f64 } } #[derive(Clone, Debug)] pub struct Vertex { index: usize, pub point: Point, local_mean: f64, local_std_dev: f64, edges: Vec<usize>, pub label: usize, } impl Vertex { fn new(index: usize, point: Point, edges: Vec<usize>) -> Vertex { Vertex { index, point, local_mean: 0.0, local_std_dev: 0.0, edges, label: 0, } } } pub trait ToGraph { fn to_graph(&self, points: &[Point]) -> Graph; } impl ToGraph for Triangulation { fn to_graph(&self, points: &[Point]) -> Graph { let all_edges = all_edges(self, points); let mut verticies: Vec<Vertex> = vec![]; for (i, p) in points.iter().enumerate() { let vertex = Vertex::new(i, Point { x: p.x, y: p.y }, neighborhood(i, &all_edges)); verticies.push(vertex); } for v in verticies.iter_mut() { v.local_mean = local_mean(&all_edges, &v.edges) } for i in 0..points.len() { verticies[i].local_std_dev = local_std_deviation(i, &all_edges, &verticies); } let mut result = Graph { verticies, mean_std_deviation: 0.0, active_edges: all_edges, }; result.mean_std_deviation = result.mean_std_deviation(); for i in 0..result.active_edges.len() { result.active_edges[i].edge_type1 = result.calculate_type( &result.verticies[result.active_edges[i].vertex1], &result.verticies[result.active_edges[i].vertex2], ); result.active_edges[i].edge_type2 = result.calculate_type( &result.verticies[result.active_edges[i].vertex2], &result.verticies[result.active_edges[i].vertex1], ); } result } } fn distance(p1: &Vertex, p2: &Vertex) -> f64 { ((p1.point.x - p2.point.x).powi(2) + (p1.point.y - p2.point.y).powi(2)).sqrt() } fn distance_point(p1: &Point, p2: &Point) -> f64 { ((p1.x - p2.x).powi(2) + (p1.y - p2.y).powi(2)).sqrt() } pub fn all_edges(graph: &Triangulation, points: &[Point]) -> Vec<Edge> { let mut result: Vec<Edge> = vec![]; for t in graph.triangles.iter().batching(|it| match it.next() { None => None, Some(x) => match it.next() { None => None, Some(y) => match it.next() { None => None, Some(z) => Some((*x, *y, *z)), }, }, }) { let e1 = Edge::new( t.0, t.1, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.0], &points[t.1]), ); let e2 = Edge::new( t.1, t.2, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.1], &points[t.2]), ); let e3 = Edge::new( t.2, t.0, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.2], &points[t.0]), ); if !result.contains(&e1) { result.push(e1); } if !result.contains(&e2) { result.push(e2); } if !result.contains(&e3) { result.push(e3); } } result } pub fn edges(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { let mut result: Vec<usize> = vec![]; for (i, edge) in all_edges.iter().enumerate() { if edge.vertex1 == point_index || edge.vertex2 == point_index { result.push(i); } } result } fn neighborhood(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { edges(point_index, all_edges) } pub fn local_mean(edges: &[Edge], edge_indicies: &[usize]) -> f64 { let mut result = 0.0; for index in edge_indicies { result += edges[*index].length; } result / (edge_indicies.len() as f64) } fn local_std_deviation(point_index: usize, edges: &[Edge], points: &[Vertex]) -> f64 { let mut result = 0.0; let current_vertex = &points[point_index]; for edge in current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) { result += (current_vertex.local_mean - edge.length).powi(2) / (current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) .count() as f64); } result.sqrt() }
self.active_edges[edge].active = true; } if self.verticies[other].label != label { self.active_edges[edge].active = false;
random_line_split
edge.rs
use delaunator::{Point, Triangulation}; use itertools::Itertools; use std::collections::HashMap; #[derive(Clone, Copy)] pub struct Edge { pub vertex1: usize, pub vertex2: usize, pub edge_type1: EdgeType, pub edge_type2: EdgeType, pub active: bool, pub length: f64, } #[derive(PartialEq, Copy, Clone)] pub enum EdgeType { Long, Short, Medium, Empty, } impl Edge { pub fn other(&self, index: usize) -> usize { if self.vertex1 == index { return self.vertex2; } self.vertex1 } pub fn edge_type(&self, perspective: usize) -> EdgeType { if self.vertex1 == perspective { return self.edge_type1; } self.edge_type2 } pub fn new(v1: usize, v2: usize, t1: EdgeType, t2: EdgeType, length: f64) -> Edge { Edge { vertex1: v1, vertex2: v2, edge_type1: t1, edge_type2: t2, active: true, length, } } } impl PartialEq for Edge { fn eq(&self, other: &Self) -> bool { (self.vertex1 == other.vertex1 && self.vertex2 == other.vertex2) || (self.vertex1 == other.vertex2 && self.vertex2 == other.vertex1) } } #[derive(Clone)] pub struct Graph { pub verticies: Vec<Vertex>, pub active_edges: Vec<Edge>, pub mean_std_deviation: f64, } pub trait FindLabel { fn find_label_for(&self, vertex: &Vertex) -> Option<usize>; fn find_label_for_index(&self, index: usize) -> Option<usize>; } pub trait Reasign { fn reasign(&mut self, from: usize, to: usize); } impl Graph { fn edge_is_active(&self, e: usize) -> bool { self.active_edges[e].active } pub fn is_short(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length < from.local_mean - self.mean_std_deviation } pub fn is_long(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length > from.local_mean + self.mean_std_deviation } pub fn calculate_type(&self, from: &Vertex, to: &Vertex) -> EdgeType { if self.is_long(from, to) { return EdgeType::Long; } if self.is_short(from, to) { return EdgeType::Short; } EdgeType::Medium } pub fn filter_edges(&self) -> Graph { let mut result = self.clone(); for edge in result.active_edges.iter_mut() { if edge.edge_type1 != EdgeType::Medium && edge.edge_type2 != EdgeType::Medium { edge.active = false; } } result } fn build_connected_component(&mut self, vertex_index: usize, label: usize) { if self.verticies[vertex_index].label != label
} pub fn calculate_connected_components(&mut self) { let mut cc_index = 1; while let Some(v) = self .verticies .iter_mut() .position(|x| !x.edges.is_empty() && x.label == 0) { self.build_connected_component(v, cc_index); cc_index += 1; } let groups = self.calculate_cc_sizes(); for (label, size) in groups { if size == 1 { for v in 0..self.verticies.len() { if self.verticies[v].label == label { self.verticies[v].label = 0; break; } } } } } fn calculate_cc_sizes(&self) -> HashMap<usize, usize> { let mut cc_sizes: HashMap<usize, usize> = HashMap::new(); for vertex in &self.verticies { *cc_sizes.entry(vertex.label).or_insert(0) += 1; } cc_sizes } fn reassign( &mut self, vertex_index: usize, label: usize, cc_sizes: &mut HashMap<usize, usize>, ) { if self.verticies[vertex_index].label != label { *cc_sizes .get_mut(&self.verticies[vertex_index].label) .unwrap() -= 1; *cc_sizes.get_mut(&label).unwrap() += 1; let vertex = &mut self.verticies[vertex_index]; vertex.label = label; for e in 0..vertex.edges.len() { let edge = self.verticies[vertex_index].edges[e]; let other = self.active_edges[edge].other(vertex_index); if self.active_edges[edge].edge_type(vertex_index) == EdgeType::Short && self.verticies[other].label == label { self.active_edges[edge].active = true; } if self.verticies[other].label != label { self.active_edges[edge].active = false; } } } } pub fn restore_edges(&mut self) { struct LabelReference { size: usize, label: usize, edge_index: usize, }; let mut cc_sizes = self.calculate_cc_sizes(); let mut reassign_map: HashMap<usize, usize> = HashMap::new(); for i in 0..self.verticies.len() { let short_edges: Vec<&Edge> = self.verticies[i] .edges .iter() .filter(|e| self.active_edges[**e].edge_type(i) == EdgeType::Short) .map(|x| &self.active_edges[*x]) .collect(); let label = self.verticies[i].label; let mut possible_labels: Vec<LabelReference> = vec![]; for (i, e) in short_edges.iter().enumerate() { let other_label = self.verticies[e.other(i)].label; if other_label != 0 && label != other_label { let other_size = cc_sizes[&other_label]; if matches!( possible_labels.iter_mut().find(|x| x.label == other_label), None ) { possible_labels.push(LabelReference { size: other_size, label: other_label, edge_index: i, }) } } } if let Some(best_label) = possible_labels.iter().max_by_key(|x| x.size) { if best_label.label != label { *reassign_map.entry(i).or_insert(0) = best_label.label; } } } for (vertex, label) in reassign_map { self.reassign(vertex, label, &mut cc_sizes); } for i in 0..self.verticies.len() { for &edge in self.verticies[i].edges.iter() { if self.active_edges[edge].edge_type(i) == EdgeType::Short && self.verticies[self.active_edges[edge].other(i)].label == self.verticies[i].label { self.active_edges[edge].active = true; } } } } fn recalculate_k_neighbourhood(&mut self, vertex_index: usize) { let mut edge_count: usize = 0; let mut edge_sum = 0.0; for &edge1 in self.verticies[vertex_index].edges.iter() { if self.edge_is_active(edge1) { let other = self.active_edges[edge1].other(vertex_index); for &edge2 in self.verticies[other].edges.iter() { if self.edge_is_active(edge2) { edge_sum += self.active_edges[edge2].length; edge_count += 1; } } } } self.verticies[vertex_index].local_mean = edge_sum / edge_count as f64; self.verticies[vertex_index].local_std_dev = local_std_deviation(vertex_index, &self.active_edges, &self.verticies) } pub fn recalculate_mean_with_k_neighbourhood(&mut self) { for v in 0..self.verticies.len() { self.recalculate_k_neighbourhood(v); self.verticies[v].label = 0; } self.mean_std_deviation = self.mean_std_deviation(); for v in 0..self.verticies.len() { for e in 0..self.verticies[v].edges.len() { let other = self.active_edges[self.verticies[v].edges[e]].other(v); for e2 in 0..self.verticies[other].edges.len() { let is_long = self.active_edges[self.verticies[other].edges[e2]].length > self.verticies[v].local_mean + self.mean_std_deviation; if is_long { self.active_edges[self.verticies[other].edges[e2]].active = false; self.mean_std_deviation = self.mean_std_deviation(); self.verticies[v].local_std_dev = local_std_deviation(v, &self.active_edges, &self.verticies); } } } } } fn mean_std_deviation(&self) -> f64 { self.verticies .iter() .fold(0.0, |acc, v| acc + v.local_std_dev) / self.verticies.len() as f64 } } #[derive(Clone, Debug)] pub struct Vertex { index: usize, pub point: Point, local_mean: f64, local_std_dev: f64, edges: Vec<usize>, pub label: usize, } impl Vertex { fn new(index: usize, point: Point, edges: Vec<usize>) -> Vertex { Vertex { index, point, local_mean: 0.0, local_std_dev: 0.0, edges, label: 0, } } } pub trait ToGraph { fn to_graph(&self, points: &[Point]) -> Graph; } impl ToGraph for Triangulation { fn to_graph(&self, points: &[Point]) -> Graph { let all_edges = all_edges(self, points); let mut verticies: Vec<Vertex> = vec![]; for (i, p) in points.iter().enumerate() { let vertex = Vertex::new(i, Point { x: p.x, y: p.y }, neighborhood(i, &all_edges)); verticies.push(vertex); } for v in verticies.iter_mut() { v.local_mean = local_mean(&all_edges, &v.edges) } for i in 0..points.len() { verticies[i].local_std_dev = local_std_deviation(i, &all_edges, &verticies); } let mut result = Graph { verticies, mean_std_deviation: 0.0, active_edges: all_edges, }; result.mean_std_deviation = result.mean_std_deviation(); for i in 0..result.active_edges.len() { result.active_edges[i].edge_type1 = result.calculate_type( &result.verticies[result.active_edges[i].vertex1], &result.verticies[result.active_edges[i].vertex2], ); result.active_edges[i].edge_type2 = result.calculate_type( &result.verticies[result.active_edges[i].vertex2], &result.verticies[result.active_edges[i].vertex1], ); } result } } fn distance(p1: &Vertex, p2: &Vertex) -> f64 { ((p1.point.x - p2.point.x).powi(2) + (p1.point.y - p2.point.y).powi(2)).sqrt() } fn distance_point(p1: &Point, p2: &Point) -> f64 { ((p1.x - p2.x).powi(2) + (p1.y - p2.y).powi(2)).sqrt() } pub fn all_edges(graph: &Triangulation, points: &[Point]) -> Vec<Edge> { let mut result: Vec<Edge> = vec![]; for t in graph.triangles.iter().batching(|it| match it.next() { None => None, Some(x) => match it.next() { None => None, Some(y) => match it.next() { None => None, Some(z) => Some((*x, *y, *z)), }, }, }) { let e1 = Edge::new( t.0, t.1, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.0], &points[t.1]), ); let e2 = Edge::new( t.1, t.2, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.1], &points[t.2]), ); let e3 = Edge::new( t.2, t.0, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.2], &points[t.0]), ); if !result.contains(&e1) { result.push(e1); } if !result.contains(&e2) { result.push(e2); } if !result.contains(&e3) { result.push(e3); } } result } pub fn edges(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { let mut result: Vec<usize> = vec![]; for (i, edge) in all_edges.iter().enumerate() { if edge.vertex1 == point_index || edge.vertex2 == point_index { result.push(i); } } result } fn neighborhood(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { edges(point_index, all_edges) } pub fn local_mean(edges: &[Edge], edge_indicies: &[usize]) -> f64 { let mut result = 0.0; for index in edge_indicies { result += edges[*index].length; } result / (edge_indicies.len() as f64) } fn local_std_deviation(point_index: usize, edges: &[Edge], points: &[Vertex]) -> f64 { let mut result = 0.0; let current_vertex = &points[point_index]; for edge in current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) { result += (current_vertex.local_mean - edge.length).powi(2) / (current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) .count() as f64); } result.sqrt() }
{ self.verticies[vertex_index].label = label; for i in 0..self.verticies[vertex_index].edges.len() { let edge_index = self.verticies[vertex_index].edges[i]; if self.edge_is_active(edge_index) && self.verticies[self.active_edges[edge_index].other(vertex_index)].label == 0 { self.build_connected_component( self.active_edges[edge_index].other(vertex_index), label, ); } } }
conditional_block
edge.rs
use delaunator::{Point, Triangulation}; use itertools::Itertools; use std::collections::HashMap; #[derive(Clone, Copy)] pub struct Edge { pub vertex1: usize, pub vertex2: usize, pub edge_type1: EdgeType, pub edge_type2: EdgeType, pub active: bool, pub length: f64, } #[derive(PartialEq, Copy, Clone)] pub enum EdgeType { Long, Short, Medium, Empty, } impl Edge { pub fn other(&self, index: usize) -> usize { if self.vertex1 == index { return self.vertex2; } self.vertex1 } pub fn edge_type(&self, perspective: usize) -> EdgeType { if self.vertex1 == perspective { return self.edge_type1; } self.edge_type2 } pub fn new(v1: usize, v2: usize, t1: EdgeType, t2: EdgeType, length: f64) -> Edge { Edge { vertex1: v1, vertex2: v2, edge_type1: t1, edge_type2: t2, active: true, length, } } } impl PartialEq for Edge { fn eq(&self, other: &Self) -> bool { (self.vertex1 == other.vertex1 && self.vertex2 == other.vertex2) || (self.vertex1 == other.vertex2 && self.vertex2 == other.vertex1) } } #[derive(Clone)] pub struct Graph { pub verticies: Vec<Vertex>, pub active_edges: Vec<Edge>, pub mean_std_deviation: f64, } pub trait FindLabel { fn find_label_for(&self, vertex: &Vertex) -> Option<usize>; fn find_label_for_index(&self, index: usize) -> Option<usize>; } pub trait Reasign { fn reasign(&mut self, from: usize, to: usize); } impl Graph { fn edge_is_active(&self, e: usize) -> bool { self.active_edges[e].active } pub fn is_short(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length < from.local_mean - self.mean_std_deviation } pub fn is_long(&self, from: &Vertex, to: &Vertex) -> bool { let length = distance(from, to); length > from.local_mean + self.mean_std_deviation } pub fn calculate_type(&self, from: &Vertex, to: &Vertex) -> EdgeType { if self.is_long(from, to) { return EdgeType::Long; } if self.is_short(from, to) { return EdgeType::Short; } EdgeType::Medium } pub fn filter_edges(&self) -> Graph { let mut result = self.clone(); for edge in result.active_edges.iter_mut() { if edge.edge_type1 != EdgeType::Medium && edge.edge_type2 != EdgeType::Medium { edge.active = false; } } result } fn build_connected_component(&mut self, vertex_index: usize, label: usize) { if self.verticies[vertex_index].label != label { self.verticies[vertex_index].label = label; for i in 0..self.verticies[vertex_index].edges.len() { let edge_index = self.verticies[vertex_index].edges[i]; if self.edge_is_active(edge_index) && self.verticies[self.active_edges[edge_index].other(vertex_index)].label == 0 { self.build_connected_component( self.active_edges[edge_index].other(vertex_index), label, ); } } } } pub fn calculate_connected_components(&mut self) { let mut cc_index = 1; while let Some(v) = self .verticies .iter_mut() .position(|x| !x.edges.is_empty() && x.label == 0) { self.build_connected_component(v, cc_index); cc_index += 1; } let groups = self.calculate_cc_sizes(); for (label, size) in groups { if size == 1 { for v in 0..self.verticies.len() { if self.verticies[v].label == label { self.verticies[v].label = 0; break; } } } } } fn calculate_cc_sizes(&self) -> HashMap<usize, usize> { let mut cc_sizes: HashMap<usize, usize> = HashMap::new(); for vertex in &self.verticies { *cc_sizes.entry(vertex.label).or_insert(0) += 1; } cc_sizes } fn reassign( &mut self, vertex_index: usize, label: usize, cc_sizes: &mut HashMap<usize, usize>, ) { if self.verticies[vertex_index].label != label { *cc_sizes .get_mut(&self.verticies[vertex_index].label) .unwrap() -= 1; *cc_sizes.get_mut(&label).unwrap() += 1; let vertex = &mut self.verticies[vertex_index]; vertex.label = label; for e in 0..vertex.edges.len() { let edge = self.verticies[vertex_index].edges[e]; let other = self.active_edges[edge].other(vertex_index); if self.active_edges[edge].edge_type(vertex_index) == EdgeType::Short && self.verticies[other].label == label { self.active_edges[edge].active = true; } if self.verticies[other].label != label { self.active_edges[edge].active = false; } } } } pub fn restore_edges(&mut self) { struct
{ size: usize, label: usize, edge_index: usize, }; let mut cc_sizes = self.calculate_cc_sizes(); let mut reassign_map: HashMap<usize, usize> = HashMap::new(); for i in 0..self.verticies.len() { let short_edges: Vec<&Edge> = self.verticies[i] .edges .iter() .filter(|e| self.active_edges[**e].edge_type(i) == EdgeType::Short) .map(|x| &self.active_edges[*x]) .collect(); let label = self.verticies[i].label; let mut possible_labels: Vec<LabelReference> = vec![]; for (i, e) in short_edges.iter().enumerate() { let other_label = self.verticies[e.other(i)].label; if other_label != 0 && label != other_label { let other_size = cc_sizes[&other_label]; if matches!( possible_labels.iter_mut().find(|x| x.label == other_label), None ) { possible_labels.push(LabelReference { size: other_size, label: other_label, edge_index: i, }) } } } if let Some(best_label) = possible_labels.iter().max_by_key(|x| x.size) { if best_label.label != label { *reassign_map.entry(i).or_insert(0) = best_label.label; } } } for (vertex, label) in reassign_map { self.reassign(vertex, label, &mut cc_sizes); } for i in 0..self.verticies.len() { for &edge in self.verticies[i].edges.iter() { if self.active_edges[edge].edge_type(i) == EdgeType::Short && self.verticies[self.active_edges[edge].other(i)].label == self.verticies[i].label { self.active_edges[edge].active = true; } } } } fn recalculate_k_neighbourhood(&mut self, vertex_index: usize) { let mut edge_count: usize = 0; let mut edge_sum = 0.0; for &edge1 in self.verticies[vertex_index].edges.iter() { if self.edge_is_active(edge1) { let other = self.active_edges[edge1].other(vertex_index); for &edge2 in self.verticies[other].edges.iter() { if self.edge_is_active(edge2) { edge_sum += self.active_edges[edge2].length; edge_count += 1; } } } } self.verticies[vertex_index].local_mean = edge_sum / edge_count as f64; self.verticies[vertex_index].local_std_dev = local_std_deviation(vertex_index, &self.active_edges, &self.verticies) } pub fn recalculate_mean_with_k_neighbourhood(&mut self) { for v in 0..self.verticies.len() { self.recalculate_k_neighbourhood(v); self.verticies[v].label = 0; } self.mean_std_deviation = self.mean_std_deviation(); for v in 0..self.verticies.len() { for e in 0..self.verticies[v].edges.len() { let other = self.active_edges[self.verticies[v].edges[e]].other(v); for e2 in 0..self.verticies[other].edges.len() { let is_long = self.active_edges[self.verticies[other].edges[e2]].length > self.verticies[v].local_mean + self.mean_std_deviation; if is_long { self.active_edges[self.verticies[other].edges[e2]].active = false; self.mean_std_deviation = self.mean_std_deviation(); self.verticies[v].local_std_dev = local_std_deviation(v, &self.active_edges, &self.verticies); } } } } } fn mean_std_deviation(&self) -> f64 { self.verticies .iter() .fold(0.0, |acc, v| acc + v.local_std_dev) / self.verticies.len() as f64 } } #[derive(Clone, Debug)] pub struct Vertex { index: usize, pub point: Point, local_mean: f64, local_std_dev: f64, edges: Vec<usize>, pub label: usize, } impl Vertex { fn new(index: usize, point: Point, edges: Vec<usize>) -> Vertex { Vertex { index, point, local_mean: 0.0, local_std_dev: 0.0, edges, label: 0, } } } pub trait ToGraph { fn to_graph(&self, points: &[Point]) -> Graph; } impl ToGraph for Triangulation { fn to_graph(&self, points: &[Point]) -> Graph { let all_edges = all_edges(self, points); let mut verticies: Vec<Vertex> = vec![]; for (i, p) in points.iter().enumerate() { let vertex = Vertex::new(i, Point { x: p.x, y: p.y }, neighborhood(i, &all_edges)); verticies.push(vertex); } for v in verticies.iter_mut() { v.local_mean = local_mean(&all_edges, &v.edges) } for i in 0..points.len() { verticies[i].local_std_dev = local_std_deviation(i, &all_edges, &verticies); } let mut result = Graph { verticies, mean_std_deviation: 0.0, active_edges: all_edges, }; result.mean_std_deviation = result.mean_std_deviation(); for i in 0..result.active_edges.len() { result.active_edges[i].edge_type1 = result.calculate_type( &result.verticies[result.active_edges[i].vertex1], &result.verticies[result.active_edges[i].vertex2], ); result.active_edges[i].edge_type2 = result.calculate_type( &result.verticies[result.active_edges[i].vertex2], &result.verticies[result.active_edges[i].vertex1], ); } result } } fn distance(p1: &Vertex, p2: &Vertex) -> f64 { ((p1.point.x - p2.point.x).powi(2) + (p1.point.y - p2.point.y).powi(2)).sqrt() } fn distance_point(p1: &Point, p2: &Point) -> f64 { ((p1.x - p2.x).powi(2) + (p1.y - p2.y).powi(2)).sqrt() } pub fn all_edges(graph: &Triangulation, points: &[Point]) -> Vec<Edge> { let mut result: Vec<Edge> = vec![]; for t in graph.triangles.iter().batching(|it| match it.next() { None => None, Some(x) => match it.next() { None => None, Some(y) => match it.next() { None => None, Some(z) => Some((*x, *y, *z)), }, }, }) { let e1 = Edge::new( t.0, t.1, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.0], &points[t.1]), ); let e2 = Edge::new( t.1, t.2, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.1], &points[t.2]), ); let e3 = Edge::new( t.2, t.0, EdgeType::Empty, EdgeType::Empty, distance_point(&points[t.2], &points[t.0]), ); if !result.contains(&e1) { result.push(e1); } if !result.contains(&e2) { result.push(e2); } if !result.contains(&e3) { result.push(e3); } } result } pub fn edges(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { let mut result: Vec<usize> = vec![]; for (i, edge) in all_edges.iter().enumerate() { if edge.vertex1 == point_index || edge.vertex2 == point_index { result.push(i); } } result } fn neighborhood(point_index: usize, all_edges: &[Edge]) -> Vec<usize> { edges(point_index, all_edges) } pub fn local_mean(edges: &[Edge], edge_indicies: &[usize]) -> f64 { let mut result = 0.0; for index in edge_indicies { result += edges[*index].length; } result / (edge_indicies.len() as f64) } fn local_std_deviation(point_index: usize, edges: &[Edge], points: &[Vertex]) -> f64 { let mut result = 0.0; let current_vertex = &points[point_index]; for edge in current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) { result += (current_vertex.local_mean - edge.length).powi(2) / (current_vertex .edges .iter() .map(|x| &edges[*x]) .filter(|x| x.active) .count() as f64); } result.sqrt() }
LabelReference
identifier_name
scheme_types.py
#!/usr/bin/env python3 import fractions import sys class Env(dict): """Context Environment.""" def __init__(self, parms=(), args=(), outer=None): """Initialize the environment with specific parameters.""" self._outer = outer if isa(parms, Symbol): # (lambda x (...)) self.update({parms:list(args)}) else: require_type(len(parms)==len(args), 'expected {0}, given {1}'.format(tostr(parms),tostr(args))) self.update(zip(parms, args)) def find(self, op): """Find operator in the environment.""" # return the environment itself so that invoker can update information # in specific environment if op in self: return self if self._outer is None: raise LookupError('unbound '+op) return self._outer.find(op) class Procedure: """Class for procedure.""" def __init__(self, parms, body, env): """Initialize a procedure with specific parameters, arguments and environment.""" self.parms = parms self.body = body self.env = env class Symbol(str): """Class for symbol.""" pass def _property(attr): """Get attribute of a class. It's a closure.""" name = '_' + attr @property def prop(self): """Get the specific attribute.""" return getattr(self, name) @prop.setter def prop(self, value): """Set the attribute.""" setattr(self, name, value) if isa(self, Pair): self.update_str() return prop class Pair: """Class for pair in scheme(created by function cons).""" car = _property('car') cdr = _property('cdr') def __init__(self, car, cdr): """Construct a pair with given data.""" # be the cache for string form of this pair self._str = '' # here can't use prop to update _str # because _car and _cdr aren't constructed self._car = car self._cdr = cdr # invoke update_str() manually only here self.update_str() def _rm_outer(self, symbol): """Remove outer boundary of second part when printing.""" if isa(symbol, Pair) or isa(symbol, List): return ' ' + str(symbol)[1:-1] # deal with situation where cdr is '() if self.cdr == []: return '' return ' . ' + tostr(symbol) def update_str(self): """Format for printing.""" self._str = ''.join(['(',tostr(self.car),self._rm_outer(self.cdr),')']) def __str__(self): """Return string form.""" return self._str def __eq__(self, pair): """Compare two pairs.""" if isa(pair, list) and pair == []: return False require_type(isa(pair, Pair), "the two type can't be compared") return self.car == pair.car and self.cdr == pair.cdr class List: """Class for list.""" def __init__(self, members): """Construct a list in scheme with members in a list.""" require_type(isa(members, list), 'the parameter of list must be a list of objects') self.members = members self.pair = self._list(members) def _list(self, exprs): """Construct a list with method cons.""" require(exprs, len(exprs)!=0) result = Pair(exprs[-1], []) for i in reversed(exprs[:-1]): result = Pair(i, result) return result def __str__(self): """Format for printing.""" return str(self.pair) def __len__(self): """Length of list.""" return len(self.members) def __eq__(self, s_list): """Compare two lists.""" if isa(s_list, list) and s_list == []: return self.members == [] require_type(isa(s_list, List), "the two type can't be compared") return self.pair == s_list.pair def __getitem__(self, i): """Get member by index.""" return self.members[i] def __setitem__(self, key, val): """Set member by index.""" self.members[key] = val pair = self.pair for i in range(key): pair = pair.cdr pair.car = val def __add__(self, right): """Add scheme list with another thing.""" try: if right == []: return self except TypeError: None raise TypeError("+ can't be applied between list and "+str(type(right))) @property def car(self): """Return the first part.""" return self.pair.car @property def cdr(self): """Return the second part.""" return self.pair.cdr @car.setter def car(self, value): """Set the first element.""" self.pair.car = value @cdr.setter def cdr(self, value): """Set the second element.""" self.pair.cdr = value class Promise: """Class for lazy binding.""" def __init__(self, exprs): """Construct a promise with its content.""" self.exprs = exprs def _pair2list(pair): """Convert a pair to list.""" members = [] while pair: members.append(pair.car) pair = pair.cdr return List(members) def _list2pair(s_list): """Convert a list into pair.""" return s_list.pair def append(*values): """Append val to a list, not modifying the list.""" require(values, len(values)>=2) values = list(values) s_list = values[0] appended = values[1:] require_type(isa(s_list,List), 'the first parameter of append must be a list') last = appended.pop() members = s_list.members + appended result = Pair(members[-1], last) for i in reversed(members[:-1]): result = Pair(i, result) if _can_be_list(result): return _pair2list(result) return result def do_is(op_left, op_right): """Judge whether op_left is op_right.""" if isa(op_left, float) and isa(op_right, float): return op_left == op_right return op_left is op_right def do_sqrt(num): """Compute square root of the number.""" if num < 0: from cmath import sqrt return sqrt(num) from math import sqrt return sqrt(num) def is_list(s_list): """Judge whether it's a list.""" return isa(s_list, List) def is_pair(pair): """Judge whether it's a pair.""" return isa(pair, Pair) or is_list(pair) def _can_be_list(pair): """Judge whether a pair can be converted into a list.""" assert(isa(pair, Pair)) return str(pair).find(' . ') < 0 def _should_be_pair(s_list): """Judge whether a list should be a pair.""" assert(isa(s_list, List)) return str(s_list).find(' . ') > 0 def cons(first, second): """Construct a pair or a list if possible.""" pair = Pair(first, second) if _can_be_list(pair): pair = _pair2list(pair) return pair def list_ref(s_list, i): """Return the ith element of the list.""" require_type(isa(s_list,List), 'parameters of list-ref must be a list') return s_list[i] def list_set(s_list, i, val): """Set value in list by index.""" require_type(isa(s_list,List), 'parameters of list-set! must be a list') s_list[i] = val return None def make_list(num, val): """Construct a list filled with num numbers of value val.""" return List([val for i in range(num)]) def set_car(pair, val): """Set car of the pair.""" pair.car = val return pair def set_cdr(pair, val): """Set cdr of the pair.""" pair.cdr = val if isa(pair, Pair) and _can_be_list(pair): return _pair2list(pair) if isa(pair, List) and _should_be_pair(pair): return _list2pair(pair) return pair def get_cdr(pair): """Get cdr of a pair or list.""" result = pair.cdr if isa(result, Pair) and _can_be_list(result): return _pair2list(result) return result isa = isinstance def transform(token): """Transform token into proper form.""" if token == '#t': return True if token == '#f': return False if token[0] == '"': return bytes(token[1:-1], "utf-8").decode('unicode-escape') if token.startswith(';'): return ';' if token.startswith('#b'): return int(token[2:], 2) if token.startswith('#o'): return int(token[2:], 8) if token.startswith('#d'): return int(token[2:]) if token.startswith('#x'): return int(token[2:], 16) try: return int(token) except ValueError: try: return float(token) except ValueError: try: result = complex(token.replace('i', 'j')) # user can't write a+bj and form like i, 2i, 3i where no '+' appers if token.find('j') >= 0 or token.find('+') < 0: return Symbol(token.lower()) return result except ValueError: try: return fractions.Fraction(token) except ValueError: return Symbol(token.lower()) def tostr(token): """Convert a token into form in lisp.""" if token is True: return '#t' if token is False: return '#f' if isa(token, Symbol): return token if isa(token, str): import json return json.dumps(token) if isa(token, complex): result = str(token).replace('j', 'i') if result.find('(') < 0: return result return result[1:-1] if isa(token, list): return '(' + ' '.join(map(tostr, token)) + ')' return str(token) def require(var, condition, msg='wrong length'): """Assert if condition isn't satisfied.""" if not condition: raise SyntaxError(tostr(var)+': '+msg) def require_type(cond, msg): """Assert for TypeError.""" if not cond: raise TypeError(msg) def not_op(target): """Implementation of operator not.""" if not isa(target, bool): return False return not target def is_int(symbol): """Judge whether the symbol is an integer.""" return isa(symbol, int) def _is_real(symbol): """Judge whether the symbol is a real number.""" return isa(symbol, float) or is_int(symbol) def is_rational(symbol): """Judge whether the symbol is rational.""" return isa(symbol, fractions.Fraction) or _is_real(symbol) def is_number(symbol): """Judge whether the symbol is a number.""" return isa(symbol, complex) or is_rational(symbol) def num2str(num): """Convert number to string.""" require_type(is_number(num), 'parameter of number->string must be a number') return tostr(num) def str2num(numstr): """Convert string to number.""" require_type(isa(numstr,str), 'parameter of string->number must be a string') return transform(numstr) def quotient(left_object, right_object): """Return quotient of the two and round towards 0.""" return int(float(left_object)/right_object) def remainder(left_object, right_object): """Return left % right whose sign is the same with the left one.""" result = left_object % right_object if left_object < 0 and result > 0 or left_object > 0 and result < 0: result = result - right_object return result def display(content): """Print content.""" print(content if isa(content, str) else tostr(content)) def lcm(num1, num2): """Compute the least common multiple for two numbers.""" return num1 * num2 // fractions.gcd(num1,num2) def numerator(num): """Return numerator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of numerator must be a fraction or integer') return num.numerator def denominator(num): """Return denominator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of denominator must be a fraction or integer') return num.denominator def make_rectangular(num1, num2): """Construct complex with two numbers.""" require_type((isa(num1,int) or isa(num1,float)) and (isa(num2,int) or isa(num2,float)), 'parameters of make_rectangular must be integers or float numbers') return complex(num1, num2) def is_complex(num): """Judge whether the number is a complex.""" try: complex(num) except Exception: return False return True def str2symbol(string): """Convert a string to symbol.""" require_type(isa(string, str), 'parameter of string->symbol must be a string') if string.find('"') >= 0: string = ''.join(['|', string, '|']) return Symbol(string) def substr(string, beg, end):
def append_str(*strs): """Append strings.""" return ''.join(list(strs)) def reverse_list(s_list): """Reverse a scheme list.""" require_type(isa(s_list, List), 'parameter of reverse must be a list') new_list = s_list.members.copy() new_list.reverse() return List(new_list) def is_procedure(procedure): """Judge whether it's a procedure.""" return isa(procedure,Procedure) or isa(procedure,type(max)) or isa(procedure,type(tostr)) def is_input(port): """Judge whether the port is an input port.""" try: return port.mode == 'r' except Exception: return False def is_output(port): """Judge whether the port is an output port.""" try: return port.mode == 'w' except Exception: return False def read(in_file): """Read a line from the file.""" require_type(is_input(in_file), 'the parameter of read must be an input file') txt = in_file.readline().lower() while txt == '\n': txt = in_file.readline().lower() return txt.strip() if txt else Symbol('#!eof') def is_eof(eof): """Judge whether it's an eof.""" return eof == Symbol('#!eof') def close_input(in_file): """Close input file.""" require_type(is_input(in_file), 'the parameter must be an input file') in_file.close() def write(content, port=sys.stdout): """Write content to the port.""" require_type(is_output(port), 'the parameter of write must be an output file') if port is sys.stdout: display(content) return port.write(tostr(content)) def close_output(out_file): """Close the output file.""" require_type(is_output(out_file), 'the parameter must be an output file') out_file.close() def s_or(*args): """Logical or.""" result = False for i in args: result = result or i if result: break return result def s_and(*args): """Logical and.""" result = True for i in args: result = result and i if not result: break return result def promise_forced(promise): """Judge whether the promise has been forced.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') return promise.exprs.env.find(Symbol('already-run?'))['already-run?'] def promise_value(promise): """Return forced value in promise else raise exception.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') if promise_forced(promise): return promise.exprs.env.find(Symbol('result'))['result'] raise RuntimeError('the promise has not been forced')
"""Return substring from beg to end.""" require_type(isa(string, str), 'the first parameter of substring must be a string') if beg < 0 or end >= len(string) or beg > end: raise IndexError('the index of substring is invalid') return string[beg:end]
identifier_body
scheme_types.py
#!/usr/bin/env python3 import fractions import sys class Env(dict): """Context Environment.""" def __init__(self, parms=(), args=(), outer=None): """Initialize the environment with specific parameters.""" self._outer = outer if isa(parms, Symbol): # (lambda x (...)) self.update({parms:list(args)}) else: require_type(len(parms)==len(args), 'expected {0}, given {1}'.format(tostr(parms),tostr(args))) self.update(zip(parms, args)) def find(self, op): """Find operator in the environment.""" # return the environment itself so that invoker can update information # in specific environment if op in self: return self if self._outer is None: raise LookupError('unbound '+op) return self._outer.find(op) class Procedure: """Class for procedure.""" def __init__(self, parms, body, env): """Initialize a procedure with specific parameters, arguments and environment.""" self.parms = parms self.body = body self.env = env class Symbol(str): """Class for symbol.""" pass def _property(attr): """Get attribute of a class. It's a closure.""" name = '_' + attr @property def prop(self): """Get the specific attribute.""" return getattr(self, name) @prop.setter def prop(self, value): """Set the attribute.""" setattr(self, name, value) if isa(self, Pair): self.update_str() return prop class Pair: """Class for pair in scheme(created by function cons).""" car = _property('car') cdr = _property('cdr') def __init__(self, car, cdr): """Construct a pair with given data.""" # be the cache for string form of this pair self._str = '' # here can't use prop to update _str # because _car and _cdr aren't constructed self._car = car self._cdr = cdr # invoke update_str() manually only here self.update_str() def _rm_outer(self, symbol): """Remove outer boundary of second part when printing.""" if isa(symbol, Pair) or isa(symbol, List): return ' ' + str(symbol)[1:-1] # deal with situation where cdr is '() if self.cdr == []: return '' return ' . ' + tostr(symbol) def update_str(self): """Format for printing.""" self._str = ''.join(['(',tostr(self.car),self._rm_outer(self.cdr),')']) def __str__(self): """Return string form.""" return self._str def __eq__(self, pair): """Compare two pairs.""" if isa(pair, list) and pair == []: return False require_type(isa(pair, Pair), "the two type can't be compared") return self.car == pair.car and self.cdr == pair.cdr class List: """Class for list.""" def __init__(self, members): """Construct a list in scheme with members in a list.""" require_type(isa(members, list), 'the parameter of list must be a list of objects') self.members = members self.pair = self._list(members) def _list(self, exprs): """Construct a list with method cons.""" require(exprs, len(exprs)!=0) result = Pair(exprs[-1], []) for i in reversed(exprs[:-1]): result = Pair(i, result) return result def __str__(self): """Format for printing.""" return str(self.pair) def __len__(self): """Length of list.""" return len(self.members) def __eq__(self, s_list): """Compare two lists.""" if isa(s_list, list) and s_list == []: return self.members == [] require_type(isa(s_list, List), "the two type can't be compared") return self.pair == s_list.pair def __getitem__(self, i): """Get member by index.""" return self.members[i] def __setitem__(self, key, val): """Set member by index.""" self.members[key] = val pair = self.pair for i in range(key): pair = pair.cdr pair.car = val def __add__(self, right): """Add scheme list with another thing.""" try: if right == []: return self except TypeError: None raise TypeError("+ can't be applied between list and "+str(type(right))) @property def car(self): """Return the first part.""" return self.pair.car @property def cdr(self): """Return the second part.""" return self.pair.cdr @car.setter def car(self, value): """Set the first element.""" self.pair.car = value @cdr.setter def cdr(self, value): """Set the second element.""" self.pair.cdr = value class Promise: """Class for lazy binding.""" def __init__(self, exprs): """Construct a promise with its content.""" self.exprs = exprs def _pair2list(pair): """Convert a pair to list.""" members = [] while pair: members.append(pair.car) pair = pair.cdr return List(members) def _list2pair(s_list): """Convert a list into pair.""" return s_list.pair def append(*values): """Append val to a list, not modifying the list.""" require(values, len(values)>=2) values = list(values) s_list = values[0] appended = values[1:] require_type(isa(s_list,List), 'the first parameter of append must be a list') last = appended.pop() members = s_list.members + appended result = Pair(members[-1], last) for i in reversed(members[:-1]): result = Pair(i, result) if _can_be_list(result): return _pair2list(result)
def do_is(op_left, op_right): """Judge whether op_left is op_right.""" if isa(op_left, float) and isa(op_right, float): return op_left == op_right return op_left is op_right def do_sqrt(num): """Compute square root of the number.""" if num < 0: from cmath import sqrt return sqrt(num) from math import sqrt return sqrt(num) def is_list(s_list): """Judge whether it's a list.""" return isa(s_list, List) def is_pair(pair): """Judge whether it's a pair.""" return isa(pair, Pair) or is_list(pair) def _can_be_list(pair): """Judge whether a pair can be converted into a list.""" assert(isa(pair, Pair)) return str(pair).find(' . ') < 0 def _should_be_pair(s_list): """Judge whether a list should be a pair.""" assert(isa(s_list, List)) return str(s_list).find(' . ') > 0 def cons(first, second): """Construct a pair or a list if possible.""" pair = Pair(first, second) if _can_be_list(pair): pair = _pair2list(pair) return pair def list_ref(s_list, i): """Return the ith element of the list.""" require_type(isa(s_list,List), 'parameters of list-ref must be a list') return s_list[i] def list_set(s_list, i, val): """Set value in list by index.""" require_type(isa(s_list,List), 'parameters of list-set! must be a list') s_list[i] = val return None def make_list(num, val): """Construct a list filled with num numbers of value val.""" return List([val for i in range(num)]) def set_car(pair, val): """Set car of the pair.""" pair.car = val return pair def set_cdr(pair, val): """Set cdr of the pair.""" pair.cdr = val if isa(pair, Pair) and _can_be_list(pair): return _pair2list(pair) if isa(pair, List) and _should_be_pair(pair): return _list2pair(pair) return pair def get_cdr(pair): """Get cdr of a pair or list.""" result = pair.cdr if isa(result, Pair) and _can_be_list(result): return _pair2list(result) return result isa = isinstance def transform(token): """Transform token into proper form.""" if token == '#t': return True if token == '#f': return False if token[0] == '"': return bytes(token[1:-1], "utf-8").decode('unicode-escape') if token.startswith(';'): return ';' if token.startswith('#b'): return int(token[2:], 2) if token.startswith('#o'): return int(token[2:], 8) if token.startswith('#d'): return int(token[2:]) if token.startswith('#x'): return int(token[2:], 16) try: return int(token) except ValueError: try: return float(token) except ValueError: try: result = complex(token.replace('i', 'j')) # user can't write a+bj and form like i, 2i, 3i where no '+' appers if token.find('j') >= 0 or token.find('+') < 0: return Symbol(token.lower()) return result except ValueError: try: return fractions.Fraction(token) except ValueError: return Symbol(token.lower()) def tostr(token): """Convert a token into form in lisp.""" if token is True: return '#t' if token is False: return '#f' if isa(token, Symbol): return token if isa(token, str): import json return json.dumps(token) if isa(token, complex): result = str(token).replace('j', 'i') if result.find('(') < 0: return result return result[1:-1] if isa(token, list): return '(' + ' '.join(map(tostr, token)) + ')' return str(token) def require(var, condition, msg='wrong length'): """Assert if condition isn't satisfied.""" if not condition: raise SyntaxError(tostr(var)+': '+msg) def require_type(cond, msg): """Assert for TypeError.""" if not cond: raise TypeError(msg) def not_op(target): """Implementation of operator not.""" if not isa(target, bool): return False return not target def is_int(symbol): """Judge whether the symbol is an integer.""" return isa(symbol, int) def _is_real(symbol): """Judge whether the symbol is a real number.""" return isa(symbol, float) or is_int(symbol) def is_rational(symbol): """Judge whether the symbol is rational.""" return isa(symbol, fractions.Fraction) or _is_real(symbol) def is_number(symbol): """Judge whether the symbol is a number.""" return isa(symbol, complex) or is_rational(symbol) def num2str(num): """Convert number to string.""" require_type(is_number(num), 'parameter of number->string must be a number') return tostr(num) def str2num(numstr): """Convert string to number.""" require_type(isa(numstr,str), 'parameter of string->number must be a string') return transform(numstr) def quotient(left_object, right_object): """Return quotient of the two and round towards 0.""" return int(float(left_object)/right_object) def remainder(left_object, right_object): """Return left % right whose sign is the same with the left one.""" result = left_object % right_object if left_object < 0 and result > 0 or left_object > 0 and result < 0: result = result - right_object return result def display(content): """Print content.""" print(content if isa(content, str) else tostr(content)) def lcm(num1, num2): """Compute the least common multiple for two numbers.""" return num1 * num2 // fractions.gcd(num1,num2) def numerator(num): """Return numerator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of numerator must be a fraction or integer') return num.numerator def denominator(num): """Return denominator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of denominator must be a fraction or integer') return num.denominator def make_rectangular(num1, num2): """Construct complex with two numbers.""" require_type((isa(num1,int) or isa(num1,float)) and (isa(num2,int) or isa(num2,float)), 'parameters of make_rectangular must be integers or float numbers') return complex(num1, num2) def is_complex(num): """Judge whether the number is a complex.""" try: complex(num) except Exception: return False return True def str2symbol(string): """Convert a string to symbol.""" require_type(isa(string, str), 'parameter of string->symbol must be a string') if string.find('"') >= 0: string = ''.join(['|', string, '|']) return Symbol(string) def substr(string, beg, end): """Return substring from beg to end.""" require_type(isa(string, str), 'the first parameter of substring must be a string') if beg < 0 or end >= len(string) or beg > end: raise IndexError('the index of substring is invalid') return string[beg:end] def append_str(*strs): """Append strings.""" return ''.join(list(strs)) def reverse_list(s_list): """Reverse a scheme list.""" require_type(isa(s_list, List), 'parameter of reverse must be a list') new_list = s_list.members.copy() new_list.reverse() return List(new_list) def is_procedure(procedure): """Judge whether it's a procedure.""" return isa(procedure,Procedure) or isa(procedure,type(max)) or isa(procedure,type(tostr)) def is_input(port): """Judge whether the port is an input port.""" try: return port.mode == 'r' except Exception: return False def is_output(port): """Judge whether the port is an output port.""" try: return port.mode == 'w' except Exception: return False def read(in_file): """Read a line from the file.""" require_type(is_input(in_file), 'the parameter of read must be an input file') txt = in_file.readline().lower() while txt == '\n': txt = in_file.readline().lower() return txt.strip() if txt else Symbol('#!eof') def is_eof(eof): """Judge whether it's an eof.""" return eof == Symbol('#!eof') def close_input(in_file): """Close input file.""" require_type(is_input(in_file), 'the parameter must be an input file') in_file.close() def write(content, port=sys.stdout): """Write content to the port.""" require_type(is_output(port), 'the parameter of write must be an output file') if port is sys.stdout: display(content) return port.write(tostr(content)) def close_output(out_file): """Close the output file.""" require_type(is_output(out_file), 'the parameter must be an output file') out_file.close() def s_or(*args): """Logical or.""" result = False for i in args: result = result or i if result: break return result def s_and(*args): """Logical and.""" result = True for i in args: result = result and i if not result: break return result def promise_forced(promise): """Judge whether the promise has been forced.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') return promise.exprs.env.find(Symbol('already-run?'))['already-run?'] def promise_value(promise): """Return forced value in promise else raise exception.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') if promise_forced(promise): return promise.exprs.env.find(Symbol('result'))['result'] raise RuntimeError('the promise has not been forced')
return result
random_line_split
scheme_types.py
#!/usr/bin/env python3 import fractions import sys class Env(dict): """Context Environment.""" def __init__(self, parms=(), args=(), outer=None): """Initialize the environment with specific parameters.""" self._outer = outer if isa(parms, Symbol): # (lambda x (...)) self.update({parms:list(args)}) else: require_type(len(parms)==len(args), 'expected {0}, given {1}'.format(tostr(parms),tostr(args))) self.update(zip(parms, args)) def find(self, op): """Find operator in the environment.""" # return the environment itself so that invoker can update information # in specific environment if op in self: return self if self._outer is None: raise LookupError('unbound '+op) return self._outer.find(op) class Procedure: """Class for procedure.""" def __init__(self, parms, body, env): """Initialize a procedure with specific parameters, arguments and environment.""" self.parms = parms self.body = body self.env = env class Symbol(str): """Class for symbol.""" pass def _property(attr): """Get attribute of a class. It's a closure.""" name = '_' + attr @property def prop(self): """Get the specific attribute.""" return getattr(self, name) @prop.setter def prop(self, value): """Set the attribute.""" setattr(self, name, value) if isa(self, Pair): self.update_str() return prop class Pair: """Class for pair in scheme(created by function cons).""" car = _property('car') cdr = _property('cdr') def __init__(self, car, cdr): """Construct a pair with given data.""" # be the cache for string form of this pair self._str = '' # here can't use prop to update _str # because _car and _cdr aren't constructed self._car = car self._cdr = cdr # invoke update_str() manually only here self.update_str() def _rm_outer(self, symbol): """Remove outer boundary of second part when printing.""" if isa(symbol, Pair) or isa(symbol, List): return ' ' + str(symbol)[1:-1] # deal with situation where cdr is '() if self.cdr == []: return '' return ' . ' + tostr(symbol) def update_str(self): """Format for printing.""" self._str = ''.join(['(',tostr(self.car),self._rm_outer(self.cdr),')']) def __str__(self): """Return string form.""" return self._str def __eq__(self, pair): """Compare two pairs.""" if isa(pair, list) and pair == []: return False require_type(isa(pair, Pair), "the two type can't be compared") return self.car == pair.car and self.cdr == pair.cdr class List: """Class for list.""" def __init__(self, members): """Construct a list in scheme with members in a list.""" require_type(isa(members, list), 'the parameter of list must be a list of objects') self.members = members self.pair = self._list(members) def _list(self, exprs): """Construct a list with method cons.""" require(exprs, len(exprs)!=0) result = Pair(exprs[-1], []) for i in reversed(exprs[:-1]): result = Pair(i, result) return result def __str__(self): """Format for printing.""" return str(self.pair) def __len__(self): """Length of list.""" return len(self.members) def __eq__(self, s_list): """Compare two lists.""" if isa(s_list, list) and s_list == []: return self.members == [] require_type(isa(s_list, List), "the two type can't be compared") return self.pair == s_list.pair def __getitem__(self, i): """Get member by index.""" return self.members[i] def __setitem__(self, key, val): """Set member by index.""" self.members[key] = val pair = self.pair for i in range(key): pair = pair.cdr pair.car = val def __add__(self, right): """Add scheme list with another thing.""" try: if right == []: return self except TypeError: None raise TypeError("+ can't be applied between list and "+str(type(right))) @property def car(self): """Return the first part.""" return self.pair.car @property def cdr(self): """Return the second part.""" return self.pair.cdr @car.setter def car(self, value): """Set the first element.""" self.pair.car = value @cdr.setter def cdr(self, value): """Set the second element.""" self.pair.cdr = value class Promise: """Class for lazy binding.""" def __init__(self, exprs): """Construct a promise with its content.""" self.exprs = exprs def _pair2list(pair): """Convert a pair to list.""" members = [] while pair: members.append(pair.car) pair = pair.cdr return List(members) def _list2pair(s_list): """Convert a list into pair.""" return s_list.pair def append(*values): """Append val to a list, not modifying the list.""" require(values, len(values)>=2) values = list(values) s_list = values[0] appended = values[1:] require_type(isa(s_list,List), 'the first parameter of append must be a list') last = appended.pop() members = s_list.members + appended result = Pair(members[-1], last) for i in reversed(members[:-1]): result = Pair(i, result) if _can_be_list(result): return _pair2list(result) return result def do_is(op_left, op_right): """Judge whether op_left is op_right.""" if isa(op_left, float) and isa(op_right, float): return op_left == op_right return op_left is op_right def do_sqrt(num): """Compute square root of the number.""" if num < 0: from cmath import sqrt return sqrt(num) from math import sqrt return sqrt(num) def is_list(s_list): """Judge whether it's a list.""" return isa(s_list, List) def is_pair(pair): """Judge whether it's a pair.""" return isa(pair, Pair) or is_list(pair) def _can_be_list(pair): """Judge whether a pair can be converted into a list.""" assert(isa(pair, Pair)) return str(pair).find(' . ') < 0 def _should_be_pair(s_list): """Judge whether a list should be a pair.""" assert(isa(s_list, List)) return str(s_list).find(' . ') > 0 def cons(first, second): """Construct a pair or a list if possible.""" pair = Pair(first, second) if _can_be_list(pair): pair = _pair2list(pair) return pair def list_ref(s_list, i): """Return the ith element of the list.""" require_type(isa(s_list,List), 'parameters of list-ref must be a list') return s_list[i] def list_set(s_list, i, val): """Set value in list by index.""" require_type(isa(s_list,List), 'parameters of list-set! must be a list') s_list[i] = val return None def make_list(num, val): """Construct a list filled with num numbers of value val.""" return List([val for i in range(num)]) def set_car(pair, val): """Set car of the pair.""" pair.car = val return pair def set_cdr(pair, val): """Set cdr of the pair.""" pair.cdr = val if isa(pair, Pair) and _can_be_list(pair): return _pair2list(pair) if isa(pair, List) and _should_be_pair(pair): return _list2pair(pair) return pair def get_cdr(pair): """Get cdr of a pair or list.""" result = pair.cdr if isa(result, Pair) and _can_be_list(result): return _pair2list(result) return result isa = isinstance def transform(token): """Transform token into proper form.""" if token == '#t': return True if token == '#f': return False if token[0] == '"': return bytes(token[1:-1], "utf-8").decode('unicode-escape') if token.startswith(';'): return ';' if token.startswith('#b'): return int(token[2:], 2) if token.startswith('#o'): return int(token[2:], 8) if token.startswith('#d'): return int(token[2:]) if token.startswith('#x'): return int(token[2:], 16) try: return int(token) except ValueError: try: return float(token) except ValueError: try: result = complex(token.replace('i', 'j')) # user can't write a+bj and form like i, 2i, 3i where no '+' appers if token.find('j') >= 0 or token.find('+') < 0: return Symbol(token.lower()) return result except ValueError: try: return fractions.Fraction(token) except ValueError: return Symbol(token.lower()) def tostr(token): """Convert a token into form in lisp.""" if token is True: return '#t' if token is False: return '#f' if isa(token, Symbol): return token if isa(token, str): import json return json.dumps(token) if isa(token, complex): result = str(token).replace('j', 'i') if result.find('(') < 0: return result return result[1:-1] if isa(token, list): return '(' + ' '.join(map(tostr, token)) + ')' return str(token) def require(var, condition, msg='wrong length'): """Assert if condition isn't satisfied.""" if not condition: raise SyntaxError(tostr(var)+': '+msg) def require_type(cond, msg): """Assert for TypeError.""" if not cond: raise TypeError(msg) def not_op(target): """Implementation of operator not.""" if not isa(target, bool): return False return not target def is_int(symbol): """Judge whether the symbol is an integer.""" return isa(symbol, int) def _is_real(symbol): """Judge whether the symbol is a real number.""" return isa(symbol, float) or is_int(symbol) def is_rational(symbol): """Judge whether the symbol is rational.""" return isa(symbol, fractions.Fraction) or _is_real(symbol) def is_number(symbol): """Judge whether the symbol is a number.""" return isa(symbol, complex) or is_rational(symbol) def num2str(num): """Convert number to string.""" require_type(is_number(num), 'parameter of number->string must be a number') return tostr(num) def str2num(numstr): """Convert string to number.""" require_type(isa(numstr,str), 'parameter of string->number must be a string') return transform(numstr) def quotient(left_object, right_object): """Return quotient of the two and round towards 0.""" return int(float(left_object)/right_object) def remainder(left_object, right_object): """Return left % right whose sign is the same with the left one.""" result = left_object % right_object if left_object < 0 and result > 0 or left_object > 0 and result < 0: result = result - right_object return result def display(content): """Print content.""" print(content if isa(content, str) else tostr(content)) def lcm(num1, num2): """Compute the least common multiple for two numbers.""" return num1 * num2 // fractions.gcd(num1,num2) def numerator(num): """Return numerator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of numerator must be a fraction or integer') return num.numerator def denominator(num): """Return denominator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of denominator must be a fraction or integer') return num.denominator def make_rectangular(num1, num2): """Construct complex with two numbers.""" require_type((isa(num1,int) or isa(num1,float)) and (isa(num2,int) or isa(num2,float)), 'parameters of make_rectangular must be integers or float numbers') return complex(num1, num2) def is_complex(num): """Judge whether the number is a complex.""" try: complex(num) except Exception: return False return True def str2symbol(string): """Convert a string to symbol.""" require_type(isa(string, str), 'parameter of string->symbol must be a string') if string.find('"') >= 0: string = ''.join(['|', string, '|']) return Symbol(string) def substr(string, beg, end): """Return substring from beg to end.""" require_type(isa(string, str), 'the first parameter of substring must be a string') if beg < 0 or end >= len(string) or beg > end: raise IndexError('the index of substring is invalid') return string[beg:end] def append_str(*strs): """Append strings.""" return ''.join(list(strs)) def reverse_list(s_list): """Reverse a scheme list.""" require_type(isa(s_list, List), 'parameter of reverse must be a list') new_list = s_list.members.copy() new_list.reverse() return List(new_list) def is_procedure(procedure): """Judge whether it's a procedure.""" return isa(procedure,Procedure) or isa(procedure,type(max)) or isa(procedure,type(tostr)) def is_input(port): """Judge whether the port is an input port.""" try: return port.mode == 'r' except Exception: return False def is_output(port): """Judge whether the port is an output port.""" try: return port.mode == 'w' except Exception: return False def read(in_file): """Read a line from the file.""" require_type(is_input(in_file), 'the parameter of read must be an input file') txt = in_file.readline().lower() while txt == '\n': txt = in_file.readline().lower() return txt.strip() if txt else Symbol('#!eof') def is_eof(eof): """Judge whether it's an eof.""" return eof == Symbol('#!eof') def close_input(in_file): """Close input file.""" require_type(is_input(in_file), 'the parameter must be an input file') in_file.close() def write(content, port=sys.stdout): """Write content to the port.""" require_type(is_output(port), 'the parameter of write must be an output file') if port is sys.stdout: display(content) return port.write(tostr(content)) def close_output(out_file): """Close the output file.""" require_type(is_output(out_file), 'the parameter must be an output file') out_file.close() def
(*args): """Logical or.""" result = False for i in args: result = result or i if result: break return result def s_and(*args): """Logical and.""" result = True for i in args: result = result and i if not result: break return result def promise_forced(promise): """Judge whether the promise has been forced.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') return promise.exprs.env.find(Symbol('already-run?'))['already-run?'] def promise_value(promise): """Return forced value in promise else raise exception.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') if promise_forced(promise): return promise.exprs.env.find(Symbol('result'))['result'] raise RuntimeError('the promise has not been forced')
s_or
identifier_name
scheme_types.py
#!/usr/bin/env python3 import fractions import sys class Env(dict): """Context Environment.""" def __init__(self, parms=(), args=(), outer=None): """Initialize the environment with specific parameters.""" self._outer = outer if isa(parms, Symbol): # (lambda x (...)) self.update({parms:list(args)}) else: require_type(len(parms)==len(args), 'expected {0}, given {1}'.format(tostr(parms),tostr(args))) self.update(zip(parms, args)) def find(self, op): """Find operator in the environment.""" # return the environment itself so that invoker can update information # in specific environment if op in self: return self if self._outer is None: raise LookupError('unbound '+op) return self._outer.find(op) class Procedure: """Class for procedure.""" def __init__(self, parms, body, env): """Initialize a procedure with specific parameters, arguments and environment.""" self.parms = parms self.body = body self.env = env class Symbol(str): """Class for symbol.""" pass def _property(attr): """Get attribute of a class. It's a closure.""" name = '_' + attr @property def prop(self): """Get the specific attribute.""" return getattr(self, name) @prop.setter def prop(self, value): """Set the attribute.""" setattr(self, name, value) if isa(self, Pair): self.update_str() return prop class Pair: """Class for pair in scheme(created by function cons).""" car = _property('car') cdr = _property('cdr') def __init__(self, car, cdr): """Construct a pair with given data.""" # be the cache for string form of this pair self._str = '' # here can't use prop to update _str # because _car and _cdr aren't constructed self._car = car self._cdr = cdr # invoke update_str() manually only here self.update_str() def _rm_outer(self, symbol): """Remove outer boundary of second part when printing.""" if isa(symbol, Pair) or isa(symbol, List): return ' ' + str(symbol)[1:-1] # deal with situation where cdr is '() if self.cdr == []: return '' return ' . ' + tostr(symbol) def update_str(self): """Format for printing.""" self._str = ''.join(['(',tostr(self.car),self._rm_outer(self.cdr),')']) def __str__(self): """Return string form.""" return self._str def __eq__(self, pair): """Compare two pairs.""" if isa(pair, list) and pair == []: return False require_type(isa(pair, Pair), "the two type can't be compared") return self.car == pair.car and self.cdr == pair.cdr class List: """Class for list.""" def __init__(self, members): """Construct a list in scheme with members in a list.""" require_type(isa(members, list), 'the parameter of list must be a list of objects') self.members = members self.pair = self._list(members) def _list(self, exprs): """Construct a list with method cons.""" require(exprs, len(exprs)!=0) result = Pair(exprs[-1], []) for i in reversed(exprs[:-1]): result = Pair(i, result) return result def __str__(self): """Format for printing.""" return str(self.pair) def __len__(self): """Length of list.""" return len(self.members) def __eq__(self, s_list): """Compare two lists.""" if isa(s_list, list) and s_list == []: return self.members == [] require_type(isa(s_list, List), "the two type can't be compared") return self.pair == s_list.pair def __getitem__(self, i): """Get member by index.""" return self.members[i] def __setitem__(self, key, val): """Set member by index.""" self.members[key] = val pair = self.pair for i in range(key): pair = pair.cdr pair.car = val def __add__(self, right): """Add scheme list with another thing.""" try: if right == []: return self except TypeError: None raise TypeError("+ can't be applied between list and "+str(type(right))) @property def car(self): """Return the first part.""" return self.pair.car @property def cdr(self): """Return the second part.""" return self.pair.cdr @car.setter def car(self, value): """Set the first element.""" self.pair.car = value @cdr.setter def cdr(self, value): """Set the second element.""" self.pair.cdr = value class Promise: """Class for lazy binding.""" def __init__(self, exprs): """Construct a promise with its content.""" self.exprs = exprs def _pair2list(pair): """Convert a pair to list.""" members = [] while pair: members.append(pair.car) pair = pair.cdr return List(members) def _list2pair(s_list): """Convert a list into pair.""" return s_list.pair def append(*values): """Append val to a list, not modifying the list.""" require(values, len(values)>=2) values = list(values) s_list = values[0] appended = values[1:] require_type(isa(s_list,List), 'the first parameter of append must be a list') last = appended.pop() members = s_list.members + appended result = Pair(members[-1], last) for i in reversed(members[:-1]): result = Pair(i, result) if _can_be_list(result): return _pair2list(result) return result def do_is(op_left, op_right): """Judge whether op_left is op_right.""" if isa(op_left, float) and isa(op_right, float):
return op_left is op_right def do_sqrt(num): """Compute square root of the number.""" if num < 0: from cmath import sqrt return sqrt(num) from math import sqrt return sqrt(num) def is_list(s_list): """Judge whether it's a list.""" return isa(s_list, List) def is_pair(pair): """Judge whether it's a pair.""" return isa(pair, Pair) or is_list(pair) def _can_be_list(pair): """Judge whether a pair can be converted into a list.""" assert(isa(pair, Pair)) return str(pair).find(' . ') < 0 def _should_be_pair(s_list): """Judge whether a list should be a pair.""" assert(isa(s_list, List)) return str(s_list).find(' . ') > 0 def cons(first, second): """Construct a pair or a list if possible.""" pair = Pair(first, second) if _can_be_list(pair): pair = _pair2list(pair) return pair def list_ref(s_list, i): """Return the ith element of the list.""" require_type(isa(s_list,List), 'parameters of list-ref must be a list') return s_list[i] def list_set(s_list, i, val): """Set value in list by index.""" require_type(isa(s_list,List), 'parameters of list-set! must be a list') s_list[i] = val return None def make_list(num, val): """Construct a list filled with num numbers of value val.""" return List([val for i in range(num)]) def set_car(pair, val): """Set car of the pair.""" pair.car = val return pair def set_cdr(pair, val): """Set cdr of the pair.""" pair.cdr = val if isa(pair, Pair) and _can_be_list(pair): return _pair2list(pair) if isa(pair, List) and _should_be_pair(pair): return _list2pair(pair) return pair def get_cdr(pair): """Get cdr of a pair or list.""" result = pair.cdr if isa(result, Pair) and _can_be_list(result): return _pair2list(result) return result isa = isinstance def transform(token): """Transform token into proper form.""" if token == '#t': return True if token == '#f': return False if token[0] == '"': return bytes(token[1:-1], "utf-8").decode('unicode-escape') if token.startswith(';'): return ';' if token.startswith('#b'): return int(token[2:], 2) if token.startswith('#o'): return int(token[2:], 8) if token.startswith('#d'): return int(token[2:]) if token.startswith('#x'): return int(token[2:], 16) try: return int(token) except ValueError: try: return float(token) except ValueError: try: result = complex(token.replace('i', 'j')) # user can't write a+bj and form like i, 2i, 3i where no '+' appers if token.find('j') >= 0 or token.find('+') < 0: return Symbol(token.lower()) return result except ValueError: try: return fractions.Fraction(token) except ValueError: return Symbol(token.lower()) def tostr(token): """Convert a token into form in lisp.""" if token is True: return '#t' if token is False: return '#f' if isa(token, Symbol): return token if isa(token, str): import json return json.dumps(token) if isa(token, complex): result = str(token).replace('j', 'i') if result.find('(') < 0: return result return result[1:-1] if isa(token, list): return '(' + ' '.join(map(tostr, token)) + ')' return str(token) def require(var, condition, msg='wrong length'): """Assert if condition isn't satisfied.""" if not condition: raise SyntaxError(tostr(var)+': '+msg) def require_type(cond, msg): """Assert for TypeError.""" if not cond: raise TypeError(msg) def not_op(target): """Implementation of operator not.""" if not isa(target, bool): return False return not target def is_int(symbol): """Judge whether the symbol is an integer.""" return isa(symbol, int) def _is_real(symbol): """Judge whether the symbol is a real number.""" return isa(symbol, float) or is_int(symbol) def is_rational(symbol): """Judge whether the symbol is rational.""" return isa(symbol, fractions.Fraction) or _is_real(symbol) def is_number(symbol): """Judge whether the symbol is a number.""" return isa(symbol, complex) or is_rational(symbol) def num2str(num): """Convert number to string.""" require_type(is_number(num), 'parameter of number->string must be a number') return tostr(num) def str2num(numstr): """Convert string to number.""" require_type(isa(numstr,str), 'parameter of string->number must be a string') return transform(numstr) def quotient(left_object, right_object): """Return quotient of the two and round towards 0.""" return int(float(left_object)/right_object) def remainder(left_object, right_object): """Return left % right whose sign is the same with the left one.""" result = left_object % right_object if left_object < 0 and result > 0 or left_object > 0 and result < 0: result = result - right_object return result def display(content): """Print content.""" print(content if isa(content, str) else tostr(content)) def lcm(num1, num2): """Compute the least common multiple for two numbers.""" return num1 * num2 // fractions.gcd(num1,num2) def numerator(num): """Return numerator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of numerator must be a fraction or integer') return num.numerator def denominator(num): """Return denominator of a fraction.""" require_type(isa(num,fractions.Fraction) or isa(num,int), 'parameter of denominator must be a fraction or integer') return num.denominator def make_rectangular(num1, num2): """Construct complex with two numbers.""" require_type((isa(num1,int) or isa(num1,float)) and (isa(num2,int) or isa(num2,float)), 'parameters of make_rectangular must be integers or float numbers') return complex(num1, num2) def is_complex(num): """Judge whether the number is a complex.""" try: complex(num) except Exception: return False return True def str2symbol(string): """Convert a string to symbol.""" require_type(isa(string, str), 'parameter of string->symbol must be a string') if string.find('"') >= 0: string = ''.join(['|', string, '|']) return Symbol(string) def substr(string, beg, end): """Return substring from beg to end.""" require_type(isa(string, str), 'the first parameter of substring must be a string') if beg < 0 or end >= len(string) or beg > end: raise IndexError('the index of substring is invalid') return string[beg:end] def append_str(*strs): """Append strings.""" return ''.join(list(strs)) def reverse_list(s_list): """Reverse a scheme list.""" require_type(isa(s_list, List), 'parameter of reverse must be a list') new_list = s_list.members.copy() new_list.reverse() return List(new_list) def is_procedure(procedure): """Judge whether it's a procedure.""" return isa(procedure,Procedure) or isa(procedure,type(max)) or isa(procedure,type(tostr)) def is_input(port): """Judge whether the port is an input port.""" try: return port.mode == 'r' except Exception: return False def is_output(port): """Judge whether the port is an output port.""" try: return port.mode == 'w' except Exception: return False def read(in_file): """Read a line from the file.""" require_type(is_input(in_file), 'the parameter of read must be an input file') txt = in_file.readline().lower() while txt == '\n': txt = in_file.readline().lower() return txt.strip() if txt else Symbol('#!eof') def is_eof(eof): """Judge whether it's an eof.""" return eof == Symbol('#!eof') def close_input(in_file): """Close input file.""" require_type(is_input(in_file), 'the parameter must be an input file') in_file.close() def write(content, port=sys.stdout): """Write content to the port.""" require_type(is_output(port), 'the parameter of write must be an output file') if port is sys.stdout: display(content) return port.write(tostr(content)) def close_output(out_file): """Close the output file.""" require_type(is_output(out_file), 'the parameter must be an output file') out_file.close() def s_or(*args): """Logical or.""" result = False for i in args: result = result or i if result: break return result def s_and(*args): """Logical and.""" result = True for i in args: result = result and i if not result: break return result def promise_forced(promise): """Judge whether the promise has been forced.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') return promise.exprs.env.find(Symbol('already-run?'))['already-run?'] def promise_value(promise): """Return forced value in promise else raise exception.""" require_type(isa(promise,Promise), 'the parameter of promise_forced must be a Promise') if promise_forced(promise): return promise.exprs.env.find(Symbol('result'))['result'] raise RuntimeError('the promise has not been forced')
return op_left == op_right
conditional_block
User.ts
/*! * ISC License * * Copyright (c) 2018, Imqueue Sandbox * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ import { IMQService, expose, profile, IMessageQueue } from '@imqueue/rpc'; import * as mongoose from 'mongoose'; import { md5, isEmail } from './helpers'; import { UserObject, UserFilters, UserCarObject } from './types'; import { USER_DB, MAX_USER_CARS_COUNT } from '../config'; import { schema } from './schema'; import { ADD_CAR_LIMIT_EXCEEDED_ERROR, ADD_CAR_DUPLICATE_ERROR, ADD_USER_DUPLICATE_ERROR, INTERNAL_ERROR, INVALID_CAR_ID_ERROR, } from './errors'; /** * User service implementation */ export class User extends IMQService { private db: mongoose.Connection; private UserModel: mongoose.Model<any>; /** * Transforms given filters into mongo-specific filters object * * @param {UserFilters} filters * @return {any} */ private prepare(filters: UserFilters) { for (let filter of Object.keys(filters)) { if (~['isAdmin', 'isActive'].indexOf(filter)) { continue; } (filters as any)[filter] = { $regex: (filters as any)[filter], $options: 'i' }; } return filters; } /** * Initializes mongo database connection and user schema * * @return Promise<any> */ @profile() private async initDb(): Promise<any> { return new Promise((resolve, reject) => { mongoose.set('useCreateIndex', true); mongoose.set('useNewUrlParser', true); mongoose.connect(USER_DB); this.db = mongoose.connection; this.db.on('error', reject); this.db.once('open', resolve); this.UserModel = mongoose.model('User', schema); }); } /** * Overriding start method to inject mongodb connection establishment */ @profile() public async start(): Promise<IMessageQueue | undefined> { this.logger.log('Initializing MongoDB connection...'); await this.initDb(); return super.start(); } /** * Creates new user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async createUser(data: UserObject, fields?: string[]) { try { const user = new this.UserModel(data); await user.save(); return this.fetch(data.email, fields); } catch (err) { if (/duplicate key/.test(err)) { throw ADD_USER_DUPLICATE_ERROR; } else { throw err; } } } /** * Updates existing user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async updateUser(data: UserObject, fields?: string[]) { const _id = String(data._id); delete data._id; await this.UserModel.updateOne({ _id }, data).exec(); return this.fetch(_id, fields); } /** * Creates or updates existing user with the new data set * * @param {UserObject} data - user data fields * @param {string[]} [fields] - fields to return on success * @return {Promise<UserObject | null>} - saved user data object */ @profile() @expose() public async update( data: UserObject, fields?: string[] ): Promise<UserObject | null> { if (data.password) { data.password = md5(data.password); } if (data._id)
else { return await this.createUser(data, fields); } } /** * Returns number of cars registered for the user having given id or email * * @param {string} idOrEmail * @return {Promise<number>} */ @profile() @expose() public async carsCount(idOrEmail: string): Promise<number> { const field = isEmail(idOrEmail) ? 'email' : '_id'; const ObjectId = mongoose.Types.ObjectId; if (field === '_id') { idOrEmail = ObjectId(idOrEmail) as any; } return ((await this.UserModel.aggregate([ { $match: { [field]: idOrEmail } }, { $project: { carsCount: { $size: "$cars" } } } ]))[0] || {}).carsCount || 0 } /** * Look-ups and returns user data by either user e-mail or by user object * identifier * * @param {string} criteria - user identifier or e-mail string * @param {string[]} [fields] - fields to select and return * @return {Promise<UserObject | null>} - found user object or nothing */ @profile() @expose() public async fetch( criteria: string, fields?: string[] ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; let query: mongoose.DocumentQuery<UserObject | null, any>; if (isEmail(criteria)) { query = this.UserModel.findOne().where({ email: criteria, }); } else { query = this.UserModel.findById(ObjectId(criteria)); } if (fields && fields.length) { query.select(fields.join(' ')); } return await query.exec(); } /** * Returns number of users stored in the system and matching given criteria * * @param {UserFilters} [filters] - filter by is active criteria * @return {Promise<number>} - number of user counted */ @profile() @expose() public async count(filters?: UserFilters): Promise<number> { return await this.UserModel.count( this.prepare(filters || {} as UserFilters) ).exec(); } /** * Returns collection of users matched is active criteria. Records * can be fetched skipping given number of records and having max length * of a given limit argument * * @param {UserFilters} [filters] - is active criteria to filter user list * @param {string[]} [fields] - list of fields to be selected and returned for each found user object * @param {number} [skip] - record to start fetching from * @param {number} [limit] - selected collection max length from a starting position * @return {Promise<UserObject[]>} - collection of users found */ @profile() @expose() public async find( filters?: UserFilters, fields?: string[], skip?: number, limit?: number, ): Promise<UserObject[]> { const query = this.UserModel.find( this.prepare(filters || {} as UserFilters) ); if (fields && fields.length) { query.select(fields.join(' ')); } if (skip) { query.skip(skip); } if (limit) { query.limit(limit); } return await query.exec() as UserObject[]; } /** * Attach new car to a user * * @param {string} userId - user identifier to add car to * @param {string} carId - selected car identifier * @param {string} regNumber - car registration number * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - operation result */ @profile() @expose() public async addCar( userId: string, carId: string, regNumber: string, selectedFields?: string[], ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; const carsCount = await this.carsCount(userId); let result: any; if (carsCount >= MAX_USER_CARS_COUNT) { throw ADD_CAR_LIMIT_EXCEEDED_ERROR; } try { result = await this.UserModel.updateOne( { _id: ObjectId(userId), 'cars.regNumber': { $ne: regNumber } }, { $push: { cars: { carId, regNumber } } }, ).exec(); } catch (err) { this.logger.log('addCar() error:', err); throw INTERNAL_ERROR; } if (result && result.ok && !result.nModified) { throw ADD_CAR_DUPLICATE_ERROR; } if (!(result && result.ok && result.nModified === 1)) { this.logger.log('addCar() invalid result:', result); throw INTERNAL_ERROR; } return await this.fetch(userId, selectedFields); } /** * Removes given car from a user * * @param {string} carId - user car identifier * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - modified user object */ @profile() @expose() public async removeCar( carId: string, selectedFields?: string[], ): Promise<UserObject | null> { try { const user = await this.UserModel.findOne({ 'cars._id': mongoose.Types.ObjectId(carId) }); if (!user) { throw INVALID_CAR_ID_ERROR; } await this.UserModel.updateOne( { 'cars._id': mongoose.Types.ObjectId(carId) }, { $pull: { cars: { _id: mongoose.Types.ObjectId(carId) } } }, ).exec(); return await this.fetch(String(user._id), selectedFields); } catch (err) { this.logger.log('removeCar() error:', err); throw INTERNAL_ERROR; } } /** * Returns car object of a given user, fetched by identifier * * @param {string} userId - user identifier * @param {string} carId - car identifier * @return {Promise<UserCarObject | null>} */ @profile() @expose() public async getCar( userId: string, carId: string, ): Promise<UserCarObject | null> { return (await this.UserModel .findOne({ _id: mongoose.Types.ObjectId(userId) }) .select(['cars._id', 'cars.carId', 'cars.regNumber']) .exec() || { cars: [] }) .cars .find((car: UserCarObject) => String(car._id) === carId) || null; } }
{ return await this.updateUser(data, fields); }
conditional_block
User.ts
/*! * ISC License * * Copyright (c) 2018, Imqueue Sandbox * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ import { IMQService, expose, profile, IMessageQueue } from '@imqueue/rpc'; import * as mongoose from 'mongoose'; import { md5, isEmail } from './helpers'; import { UserObject, UserFilters, UserCarObject } from './types'; import { USER_DB, MAX_USER_CARS_COUNT } from '../config'; import { schema } from './schema'; import { ADD_CAR_LIMIT_EXCEEDED_ERROR, ADD_CAR_DUPLICATE_ERROR, ADD_USER_DUPLICATE_ERROR, INTERNAL_ERROR, INVALID_CAR_ID_ERROR, } from './errors'; /** * User service implementation */ export class User extends IMQService { private db: mongoose.Connection; private UserModel: mongoose.Model<any>; /** * Transforms given filters into mongo-specific filters object * * @param {UserFilters} filters * @return {any} */ private prepare(filters: UserFilters) { for (let filter of Object.keys(filters)) { if (~['isAdmin', 'isActive'].indexOf(filter)) { continue; } (filters as any)[filter] = { $regex: (filters as any)[filter], $options: 'i' }; } return filters; } /** * Initializes mongo database connection and user schema * * @return Promise<any> */ @profile() private async initDb(): Promise<any> { return new Promise((resolve, reject) => { mongoose.set('useCreateIndex', true); mongoose.set('useNewUrlParser', true); mongoose.connect(USER_DB); this.db = mongoose.connection; this.db.on('error', reject); this.db.once('open', resolve); this.UserModel = mongoose.model('User', schema); }); } /** * Overriding start method to inject mongodb connection establishment */ @profile() public async start(): Promise<IMessageQueue | undefined> { this.logger.log('Initializing MongoDB connection...'); await this.initDb(); return super.start(); } /** * Creates new user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async createUser(data: UserObject, fields?: string[]) { try { const user = new this.UserModel(data); await user.save(); return this.fetch(data.email, fields); } catch (err) { if (/duplicate key/.test(err)) { throw ADD_USER_DUPLICATE_ERROR; } else { throw err; } } } /** * Updates existing user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async updateUser(data: UserObject, fields?: string[]) { const _id = String(data._id); delete data._id; await this.UserModel.updateOne({ _id }, data).exec(); return this.fetch(_id, fields); } /** * Creates or updates existing user with the new data set * * @param {UserObject} data - user data fields * @param {string[]} [fields] - fields to return on success * @return {Promise<UserObject | null>} - saved user data object */ @profile() @expose() public async update( data: UserObject, fields?: string[] ): Promise<UserObject | null> { if (data.password) { data.password = md5(data.password); } if (data._id) { return await this.updateUser(data, fields); } else { return await this.createUser(data, fields); } } /** * Returns number of cars registered for the user having given id or email * * @param {string} idOrEmail * @return {Promise<number>} */ @profile() @expose() public async carsCount(idOrEmail: string): Promise<number> { const field = isEmail(idOrEmail) ? 'email' : '_id'; const ObjectId = mongoose.Types.ObjectId; if (field === '_id') { idOrEmail = ObjectId(idOrEmail) as any; } return ((await this.UserModel.aggregate([ { $match: { [field]: idOrEmail } }, { $project: { carsCount: { $size: "$cars" } } } ]))[0] || {}).carsCount || 0 } /** * Look-ups and returns user data by either user e-mail or by user object * identifier * * @param {string} criteria - user identifier or e-mail string * @param {string[]} [fields] - fields to select and return * @return {Promise<UserObject | null>} - found user object or nothing */ @profile() @expose() public async fetch( criteria: string, fields?: string[] ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; let query: mongoose.DocumentQuery<UserObject | null, any>; if (isEmail(criteria)) { query = this.UserModel.findOne().where({ email: criteria, }); } else { query = this.UserModel.findById(ObjectId(criteria)); } if (fields && fields.length) { query.select(fields.join(' ')); } return await query.exec(); } /** * Returns number of users stored in the system and matching given criteria * * @param {UserFilters} [filters] - filter by is active criteria * @return {Promise<number>} - number of user counted */ @profile() @expose() public async count(filters?: UserFilters): Promise<number> { return await this.UserModel.count( this.prepare(filters || {} as UserFilters) ).exec(); } /** * Returns collection of users matched is active criteria. Records * can be fetched skipping given number of records and having max length * of a given limit argument * * @param {UserFilters} [filters] - is active criteria to filter user list * @param {string[]} [fields] - list of fields to be selected and returned for each found user object * @param {number} [skip] - record to start fetching from * @param {number} [limit] - selected collection max length from a starting position * @return {Promise<UserObject[]>} - collection of users found */ @profile() @expose() public async find( filters?: UserFilters, fields?: string[], skip?: number, limit?: number, ): Promise<UserObject[]> { const query = this.UserModel.find( this.prepare(filters || {} as UserFilters) ); if (fields && fields.length) { query.select(fields.join(' ')); } if (skip) { query.skip(skip); } if (limit) { query.limit(limit); } return await query.exec() as UserObject[]; } /** * Attach new car to a user * * @param {string} userId - user identifier to add car to * @param {string} carId - selected car identifier * @param {string} regNumber - car registration number * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - operation result */ @profile() @expose() public async addCar( userId: string, carId: string, regNumber: string, selectedFields?: string[], ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; const carsCount = await this.carsCount(userId); let result: any; if (carsCount >= MAX_USER_CARS_COUNT) { throw ADD_CAR_LIMIT_EXCEEDED_ERROR; } try { result = await this.UserModel.updateOne( { _id: ObjectId(userId), 'cars.regNumber': { $ne: regNumber } }, { $push: { cars: { carId, regNumber } } }, ).exec(); } catch (err) { this.logger.log('addCar() error:', err); throw INTERNAL_ERROR; } if (result && result.ok && !result.nModified) { throw ADD_CAR_DUPLICATE_ERROR; } if (!(result && result.ok && result.nModified === 1)) { this.logger.log('addCar() invalid result:', result); throw INTERNAL_ERROR; } return await this.fetch(userId, selectedFields); } /** * Removes given car from a user * * @param {string} carId - user car identifier * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - modified user object */ @profile() @expose() public async removeCar( carId: string, selectedFields?: string[], ): Promise<UserObject | null> { try { const user = await this.UserModel.findOne({ 'cars._id': mongoose.Types.ObjectId(carId) }); if (!user) { throw INVALID_CAR_ID_ERROR; } await this.UserModel.updateOne( { 'cars._id': mongoose.Types.ObjectId(carId) }, { $pull: { cars: { _id: mongoose.Types.ObjectId(carId) } } }, ).exec(); return await this.fetch(String(user._id), selectedFields); } catch (err) { this.logger.log('removeCar() error:', err); throw INTERNAL_ERROR; } } /** * Returns car object of a given user, fetched by identifier *
* @param {string} carId - car identifier * @return {Promise<UserCarObject | null>} */ @profile() @expose() public async getCar( userId: string, carId: string, ): Promise<UserCarObject | null> { return (await this.UserModel .findOne({ _id: mongoose.Types.ObjectId(userId) }) .select(['cars._id', 'cars.carId', 'cars.regNumber']) .exec() || { cars: [] }) .cars .find((car: UserCarObject) => String(car._id) === carId) || null; } }
* @param {string} userId - user identifier
random_line_split
User.ts
/*! * ISC License * * Copyright (c) 2018, Imqueue Sandbox * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ import { IMQService, expose, profile, IMessageQueue } from '@imqueue/rpc'; import * as mongoose from 'mongoose'; import { md5, isEmail } from './helpers'; import { UserObject, UserFilters, UserCarObject } from './types'; import { USER_DB, MAX_USER_CARS_COUNT } from '../config'; import { schema } from './schema'; import { ADD_CAR_LIMIT_EXCEEDED_ERROR, ADD_CAR_DUPLICATE_ERROR, ADD_USER_DUPLICATE_ERROR, INTERNAL_ERROR, INVALID_CAR_ID_ERROR, } from './errors'; /** * User service implementation */ export class User extends IMQService { private db: mongoose.Connection; private UserModel: mongoose.Model<any>; /** * Transforms given filters into mongo-specific filters object * * @param {UserFilters} filters * @return {any} */ private prepare(filters: UserFilters) { for (let filter of Object.keys(filters)) { if (~['isAdmin', 'isActive'].indexOf(filter)) { continue; } (filters as any)[filter] = { $regex: (filters as any)[filter], $options: 'i' }; } return filters; } /** * Initializes mongo database connection and user schema * * @return Promise<any> */ @profile() private async initDb(): Promise<any> { return new Promise((resolve, reject) => { mongoose.set('useCreateIndex', true); mongoose.set('useNewUrlParser', true); mongoose.connect(USER_DB); this.db = mongoose.connection; this.db.on('error', reject); this.db.once('open', resolve); this.UserModel = mongoose.model('User', schema); }); } /** * Overriding start method to inject mongodb connection establishment */ @profile() public async start(): Promise<IMessageQueue | undefined> { this.logger.log('Initializing MongoDB connection...'); await this.initDb(); return super.start(); } /** * Creates new user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async createUser(data: UserObject, fields?: string[]) { try { const user = new this.UserModel(data); await user.save(); return this.fetch(data.email, fields); } catch (err) { if (/duplicate key/.test(err)) { throw ADD_USER_DUPLICATE_ERROR; } else { throw err; } } } /** * Updates existing user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async updateUser(data: UserObject, fields?: string[]) { const _id = String(data._id); delete data._id; await this.UserModel.updateOne({ _id }, data).exec(); return this.fetch(_id, fields); } /** * Creates or updates existing user with the new data set * * @param {UserObject} data - user data fields * @param {string[]} [fields] - fields to return on success * @return {Promise<UserObject | null>} - saved user data object */ @profile() @expose() public async update( data: UserObject, fields?: string[] ): Promise<UserObject | null> { if (data.password) { data.password = md5(data.password); } if (data._id) { return await this.updateUser(data, fields); } else { return await this.createUser(data, fields); } } /** * Returns number of cars registered for the user having given id or email * * @param {string} idOrEmail * @return {Promise<number>} */ @profile() @expose() public async
(idOrEmail: string): Promise<number> { const field = isEmail(idOrEmail) ? 'email' : '_id'; const ObjectId = mongoose.Types.ObjectId; if (field === '_id') { idOrEmail = ObjectId(idOrEmail) as any; } return ((await this.UserModel.aggregate([ { $match: { [field]: idOrEmail } }, { $project: { carsCount: { $size: "$cars" } } } ]))[0] || {}).carsCount || 0 } /** * Look-ups and returns user data by either user e-mail or by user object * identifier * * @param {string} criteria - user identifier or e-mail string * @param {string[]} [fields] - fields to select and return * @return {Promise<UserObject | null>} - found user object or nothing */ @profile() @expose() public async fetch( criteria: string, fields?: string[] ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; let query: mongoose.DocumentQuery<UserObject | null, any>; if (isEmail(criteria)) { query = this.UserModel.findOne().where({ email: criteria, }); } else { query = this.UserModel.findById(ObjectId(criteria)); } if (fields && fields.length) { query.select(fields.join(' ')); } return await query.exec(); } /** * Returns number of users stored in the system and matching given criteria * * @param {UserFilters} [filters] - filter by is active criteria * @return {Promise<number>} - number of user counted */ @profile() @expose() public async count(filters?: UserFilters): Promise<number> { return await this.UserModel.count( this.prepare(filters || {} as UserFilters) ).exec(); } /** * Returns collection of users matched is active criteria. Records * can be fetched skipping given number of records and having max length * of a given limit argument * * @param {UserFilters} [filters] - is active criteria to filter user list * @param {string[]} [fields] - list of fields to be selected and returned for each found user object * @param {number} [skip] - record to start fetching from * @param {number} [limit] - selected collection max length from a starting position * @return {Promise<UserObject[]>} - collection of users found */ @profile() @expose() public async find( filters?: UserFilters, fields?: string[], skip?: number, limit?: number, ): Promise<UserObject[]> { const query = this.UserModel.find( this.prepare(filters || {} as UserFilters) ); if (fields && fields.length) { query.select(fields.join(' ')); } if (skip) { query.skip(skip); } if (limit) { query.limit(limit); } return await query.exec() as UserObject[]; } /** * Attach new car to a user * * @param {string} userId - user identifier to add car to * @param {string} carId - selected car identifier * @param {string} regNumber - car registration number * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - operation result */ @profile() @expose() public async addCar( userId: string, carId: string, regNumber: string, selectedFields?: string[], ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; const carsCount = await this.carsCount(userId); let result: any; if (carsCount >= MAX_USER_CARS_COUNT) { throw ADD_CAR_LIMIT_EXCEEDED_ERROR; } try { result = await this.UserModel.updateOne( { _id: ObjectId(userId), 'cars.regNumber': { $ne: regNumber } }, { $push: { cars: { carId, regNumber } } }, ).exec(); } catch (err) { this.logger.log('addCar() error:', err); throw INTERNAL_ERROR; } if (result && result.ok && !result.nModified) { throw ADD_CAR_DUPLICATE_ERROR; } if (!(result && result.ok && result.nModified === 1)) { this.logger.log('addCar() invalid result:', result); throw INTERNAL_ERROR; } return await this.fetch(userId, selectedFields); } /** * Removes given car from a user * * @param {string} carId - user car identifier * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - modified user object */ @profile() @expose() public async removeCar( carId: string, selectedFields?: string[], ): Promise<UserObject | null> { try { const user = await this.UserModel.findOne({ 'cars._id': mongoose.Types.ObjectId(carId) }); if (!user) { throw INVALID_CAR_ID_ERROR; } await this.UserModel.updateOne( { 'cars._id': mongoose.Types.ObjectId(carId) }, { $pull: { cars: { _id: mongoose.Types.ObjectId(carId) } } }, ).exec(); return await this.fetch(String(user._id), selectedFields); } catch (err) { this.logger.log('removeCar() error:', err); throw INTERNAL_ERROR; } } /** * Returns car object of a given user, fetched by identifier * * @param {string} userId - user identifier * @param {string} carId - car identifier * @return {Promise<UserCarObject | null>} */ @profile() @expose() public async getCar( userId: string, carId: string, ): Promise<UserCarObject | null> { return (await this.UserModel .findOne({ _id: mongoose.Types.ObjectId(userId) }) .select(['cars._id', 'cars.carId', 'cars.regNumber']) .exec() || { cars: [] }) .cars .find((car: UserCarObject) => String(car._id) === carId) || null; } }
carsCount
identifier_name
User.ts
/*! * ISC License * * Copyright (c) 2018, Imqueue Sandbox * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ import { IMQService, expose, profile, IMessageQueue } from '@imqueue/rpc'; import * as mongoose from 'mongoose'; import { md5, isEmail } from './helpers'; import { UserObject, UserFilters, UserCarObject } from './types'; import { USER_DB, MAX_USER_CARS_COUNT } from '../config'; import { schema } from './schema'; import { ADD_CAR_LIMIT_EXCEEDED_ERROR, ADD_CAR_DUPLICATE_ERROR, ADD_USER_DUPLICATE_ERROR, INTERNAL_ERROR, INVALID_CAR_ID_ERROR, } from './errors'; /** * User service implementation */ export class User extends IMQService { private db: mongoose.Connection; private UserModel: mongoose.Model<any>; /** * Transforms given filters into mongo-specific filters object * * @param {UserFilters} filters * @return {any} */ private prepare(filters: UserFilters) { for (let filter of Object.keys(filters)) { if (~['isAdmin', 'isActive'].indexOf(filter)) { continue; } (filters as any)[filter] = { $regex: (filters as any)[filter], $options: 'i' }; } return filters; } /** * Initializes mongo database connection and user schema * * @return Promise<any> */ @profile() private async initDb(): Promise<any> { return new Promise((resolve, reject) => { mongoose.set('useCreateIndex', true); mongoose.set('useNewUrlParser', true); mongoose.connect(USER_DB); this.db = mongoose.connection; this.db.on('error', reject); this.db.once('open', resolve); this.UserModel = mongoose.model('User', schema); }); } /** * Overriding start method to inject mongodb connection establishment */ @profile() public async start(): Promise<IMessageQueue | undefined> { this.logger.log('Initializing MongoDB connection...'); await this.initDb(); return super.start(); } /** * Creates new user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async createUser(data: UserObject, fields?: string[]) { try { const user = new this.UserModel(data); await user.save(); return this.fetch(data.email, fields); } catch (err) { if (/duplicate key/.test(err)) { throw ADD_USER_DUPLICATE_ERROR; } else { throw err; } } } /** * Updates existing user object in a database * * @param {UserObject} data * @param {string[]} fields * @return {UserObject} */ @profile() private async updateUser(data: UserObject, fields?: string[]) { const _id = String(data._id); delete data._id; await this.UserModel.updateOne({ _id }, data).exec(); return this.fetch(_id, fields); } /** * Creates or updates existing user with the new data set * * @param {UserObject} data - user data fields * @param {string[]} [fields] - fields to return on success * @return {Promise<UserObject | null>} - saved user data object */ @profile() @expose() public async update( data: UserObject, fields?: string[] ): Promise<UserObject | null> { if (data.password) { data.password = md5(data.password); } if (data._id) { return await this.updateUser(data, fields); } else { return await this.createUser(data, fields); } } /** * Returns number of cars registered for the user having given id or email * * @param {string} idOrEmail * @return {Promise<number>} */ @profile() @expose() public async carsCount(idOrEmail: string): Promise<number> { const field = isEmail(idOrEmail) ? 'email' : '_id'; const ObjectId = mongoose.Types.ObjectId; if (field === '_id') { idOrEmail = ObjectId(idOrEmail) as any; } return ((await this.UserModel.aggregate([ { $match: { [field]: idOrEmail } }, { $project: { carsCount: { $size: "$cars" } } } ]))[0] || {}).carsCount || 0 } /** * Look-ups and returns user data by either user e-mail or by user object * identifier * * @param {string} criteria - user identifier or e-mail string * @param {string[]} [fields] - fields to select and return * @return {Promise<UserObject | null>} - found user object or nothing */ @profile() @expose() public async fetch( criteria: string, fields?: string[] ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; let query: mongoose.DocumentQuery<UserObject | null, any>; if (isEmail(criteria)) { query = this.UserModel.findOne().where({ email: criteria, }); } else { query = this.UserModel.findById(ObjectId(criteria)); } if (fields && fields.length) { query.select(fields.join(' ')); } return await query.exec(); } /** * Returns number of users stored in the system and matching given criteria * * @param {UserFilters} [filters] - filter by is active criteria * @return {Promise<number>} - number of user counted */ @profile() @expose() public async count(filters?: UserFilters): Promise<number> { return await this.UserModel.count( this.prepare(filters || {} as UserFilters) ).exec(); } /** * Returns collection of users matched is active criteria. Records * can be fetched skipping given number of records and having max length * of a given limit argument * * @param {UserFilters} [filters] - is active criteria to filter user list * @param {string[]} [fields] - list of fields to be selected and returned for each found user object * @param {number} [skip] - record to start fetching from * @param {number} [limit] - selected collection max length from a starting position * @return {Promise<UserObject[]>} - collection of users found */ @profile() @expose() public async find( filters?: UserFilters, fields?: string[], skip?: number, limit?: number, ): Promise<UserObject[]> { const query = this.UserModel.find( this.prepare(filters || {} as UserFilters) ); if (fields && fields.length) { query.select(fields.join(' ')); } if (skip) { query.skip(skip); } if (limit) { query.limit(limit); } return await query.exec() as UserObject[]; } /** * Attach new car to a user * * @param {string} userId - user identifier to add car to * @param {string} carId - selected car identifier * @param {string} regNumber - car registration number * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - operation result */ @profile() @expose() public async addCar( userId: string, carId: string, regNumber: string, selectedFields?: string[], ): Promise<UserObject | null> { const ObjectId = mongoose.Types.ObjectId; const carsCount = await this.carsCount(userId); let result: any; if (carsCount >= MAX_USER_CARS_COUNT) { throw ADD_CAR_LIMIT_EXCEEDED_ERROR; } try { result = await this.UserModel.updateOne( { _id: ObjectId(userId), 'cars.regNumber': { $ne: regNumber } }, { $push: { cars: { carId, regNumber } } }, ).exec(); } catch (err) { this.logger.log('addCar() error:', err); throw INTERNAL_ERROR; } if (result && result.ok && !result.nModified) { throw ADD_CAR_DUPLICATE_ERROR; } if (!(result && result.ok && result.nModified === 1)) { this.logger.log('addCar() invalid result:', result); throw INTERNAL_ERROR; } return await this.fetch(userId, selectedFields); } /** * Removes given car from a user * * @param {string} carId - user car identifier * @param {string[]} [selectedFields] - fields to fetch for a modified user object * @return {Promise<UserObject | null>} - modified user object */ @profile() @expose() public async removeCar( carId: string, selectedFields?: string[], ): Promise<UserObject | null> { try { const user = await this.UserModel.findOne({ 'cars._id': mongoose.Types.ObjectId(carId) }); if (!user) { throw INVALID_CAR_ID_ERROR; } await this.UserModel.updateOne( { 'cars._id': mongoose.Types.ObjectId(carId) }, { $pull: { cars: { _id: mongoose.Types.ObjectId(carId) } } }, ).exec(); return await this.fetch(String(user._id), selectedFields); } catch (err) { this.logger.log('removeCar() error:', err); throw INTERNAL_ERROR; } } /** * Returns car object of a given user, fetched by identifier * * @param {string} userId - user identifier * @param {string} carId - car identifier * @return {Promise<UserCarObject | null>} */ @profile() @expose() public async getCar( userId: string, carId: string, ): Promise<UserCarObject | null>
}
{ return (await this.UserModel .findOne({ _id: mongoose.Types.ObjectId(userId) }) .select(['cars._id', 'cars.carId', 'cars.regNumber']) .exec() || { cars: [] }) .cars .find((car: UserCarObject) => String(car._id) === carId) || null; }
identifier_body
garmin_util.rs
use anyhow::{format_err, Error}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use fitparser::Value; use flate2::{read::GzEncoder, Compression}; use futures::{Stream, TryStreamExt}; use log::{debug, error}; use num_traits::pow::Pow; use postgres_query::{query, Error as PqError}; use rand::{ distributions::{Alphanumeric, Distribution, Uniform}, thread_rng, Rng, }; use smallvec::SmallVec; use stack_string::{format_sstr, StackString}; use std::{ collections::HashSet, convert::TryInto, fs::{remove_file, File}, future::Future, io::{BufRead, BufReader, Read}, path::{Path, PathBuf}, }; use subprocess::{Exec, Redirection}; use time::{format_description::well_known::Rfc3339, macros::date, Date, Month, OffsetDateTime}; use time_tz::{timezones::db::UTC, OffsetDateTimeExt}; use tokio::time::{sleep, Duration}; use crate::common::pgpool::PgPool; pub const METERS_PER_MILE: f64 = 1609.344; pub const MARATHON_DISTANCE_M: i32 = 42195; pub const MARATHON_DISTANCE_MI: f64 = MARATHON_DISTANCE_M as f64 / METERS_PER_MILE; pub const MONTH_NAMES: [&str; 12] = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; pub const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; /// # Errors /// Return error if parsing time string fails pub fn convert_time_string(time_str: &str) -> Result<f64, Error> { let entries: SmallVec<[&str; 3]> = time_str.split(':').take(3).collect(); let (h, m, s): (i32, i32, f64) = match entries.first() { Some(h) => match entries.get(1) { Some(m) => match entries.get(2) { Some(s) => (h.parse()?, m.parse()?, s.parse()?), None => (h.parse()?, m.parse()?, 0.), }, None => (h.parse()?, 0, 0.), }, None => (0, 0, 0.), }; Ok(s + 60.0 * (f64::from(m) + 60.0 * f64::from(h))) } /// # Errors /// Return error if parsing time string fails pub fn convert_xml_local_time_to_utc(xml_local_time: &str) -> Result<OffsetDateTime, Error> { OffsetDateTime::parse(xml_local_time, &Rfc3339) .map(|x| x.to_timezone(UTC)) .map_err(Into::into) } /// # Errors /// Return error if running `md5sum` fails pub fn get_md5sum(filename: &Path) -> Result<StackString, Error> { if !Path::new("/usr/bin/md5sum").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/md5sum)" )); } let command = format_sstr!("md5sum {}", filename.to_string_lossy()); let stream = Exec::shell(command).stream_stdout()?; let reader = BufReader::new(stream); if let Some(line) = reader.lines().next() { if let Some(entry) = line?.split_whitespace().next() { return Ok(entry.into()); } } Ok("".into()) } /// # Errors /// Return error if second is negative pub fn print_h_m_s(second: f64, do_hours: bool) -> Result<StackString, Error> { let hours = (second / 3600.0) as i32; let minutes = (second / 60.0) as i32 - hours * 60; let seconds = second as i32 - minutes * 60 - hours * 3600; if (hours > 0) | ((hours == 0) & do_hours) { Ok(format_sstr!("{hours:02}:{minutes:02}:{seconds:02}")) } else if hours == 0 { Ok(format_sstr!("{minutes:02}:{seconds:02}")) } else { Err(format_err!("Negative result!")) } }
#[must_use] pub fn days_in_year(year: i32) -> i64 { (Date::from_calendar_date(year + 1, Month::January, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, Month::January, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn days_in_month(year: i32, month: u32) -> i64 { let mut y1 = year; let mut m1 = month + 1; if m1 == 13 { y1 += 1; m1 = 1; } let month: Month = (month as u8).try_into().unwrap_or(Month::January); let m1: Month = (m1 as u8).try_into().unwrap_or(Month::January); (Date::from_calendar_date(y1, m1, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, month, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn expected_calories(weight: f64, pace_min_per_mile: f64, distance: f64) -> f64 { let cal_per_mi = weight * (0.0395 + 0.003_27 * (60. / pace_min_per_mile) + 0.000_455 * (60. / pace_min_per_mile).pow(2.0) + 0.000_801 * ((weight / 154.0) * 0.425 / weight * (60. / pace_min_per_mile).pow(3.0)) * 60. / (60. / pace_min_per_mile)); cal_per_mi * distance } #[must_use] pub fn titlecase(input: &str) -> StackString { if input.is_empty() { "".into() } else { let firstchar = input[0..1].to_uppercase(); format_sstr!("{firstchar}{s}", s = &input[1..]) } } #[must_use] pub fn generate_random_string(nchar: usize) -> StackString { let mut rng = thread_rng(); Alphanumeric .sample_iter(&mut rng) .take(nchar) .map(Into::into) .collect() } #[must_use] pub fn get_file_list(path: &Path) -> Vec<PathBuf> { match path.read_dir() { Ok(it) => it .filter_map(|dir_line| match dir_line { Ok(entry) => Some(entry.path()), Err(_) => None, }) .collect(), Err(err) => { debug!("{}", err); Vec::new() } } } /// # Errors /// Return error if closure fails pub async fn exponential_retry<T, U, F>(f: T) -> Result<U, Error> where T: Fn() -> F, F: Future<Output = Result<U, Error>>, { let mut timeout: f64 = 1.0; let range = Uniform::from(0..1000); loop { match f().await { Ok(resp) => return Ok(resp), Err(err) => { sleep(Duration::from_millis((timeout * 1000.0) as u64)).await; timeout *= 4.0 * f64::from(range.sample(&mut thread_rng())) / 1000.0; if timeout >= 64.0 { return Err(err); } } } } } fn extract_zip(filename: &Path, ziptmpdir: &Path) -> Result<(), Error> { if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/unzip" )); } let command = format_sstr!( "unzip {} -d {}", filename.to_string_lossy(), ziptmpdir.to_string_lossy() ); let mut process = Exec::shell(command).stdout(Redirection::Pipe).popen()?; let exit_status = process.wait()?; if !exit_status.success() { if let Some(mut f) = process.stdout.as_ref() { let mut buf = String::new(); f.read_to_string(&mut buf)?; error!("{}", buf); } return Err(format_err!("Failed with exit status {exit_status:?}")); } Ok(()) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect( filename: &Path, ziptmpdir: &Path, ) -> Result<PathBuf, Error> { extract_zip(filename, ziptmpdir)?; let new_filename = filename .file_stem() .ok_or_else(|| format_err!("Bad filename {}", filename.to_string_lossy()))?; let new_filename = format_sstr!("{}_ACTIVITY.fit", new_filename.to_string_lossy()); let new_filename = ziptmpdir.join(new_filename); if !new_filename.exists() { return Err(format_err!("Activity file not found")); } remove_file(filename)?; Ok(new_filename) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect_multiple( filename: &Path, ziptmpdir: &Path, ) -> Result<Vec<PathBuf>, Error> { extract_zip(filename, ziptmpdir)?; if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "unzip not installed (or not present at /usr/bin/unzip" )); } let mut files = Vec::new(); for entry in ziptmpdir.read_dir()? { let entry = entry?; files.push(entry.path()); } if !files.is_empty() { remove_file(filename)?; } Ok(files) } /// # Errors /// Return error if: /// * input file does not exist /// * opening it fails /// * creating the output file fails /// * writing to the file fails pub fn gzip_file<T, U>(input_filename: T, output_filename: U) -> Result<(), Error> where T: AsRef<Path>, U: AsRef<Path>, { let input_filename = input_filename.as_ref(); let output_filename = output_filename.as_ref(); if !input_filename.exists() { return Err(format_err!("File {input_filename:?} does not exist")); } std::io::copy( &mut GzEncoder::new(File::open(input_filename)?, Compression::fast()), &mut File::create(output_filename)?, )?; Ok(()) } #[must_use] pub fn get_f64(value: &Value) -> Option<f64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp() as f64), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(f64::from(*val)) } Value::SInt8(val) => Some(f64::from(*val)), Value::SInt16(val) => Some(f64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(f64::from(*val)), Value::SInt32(val) => Some(f64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(f64::from(*val)), Value::SInt64(val) => Some(*val as f64), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as f64), Value::Float32(val) => Some(f64::from(*val)), Value::Float64(val) => Some(*val), _ => None, } } #[must_use] pub fn get_i64(value: &Value) -> Option<i64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp()), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(i64::from(*val)) } Value::SInt8(val) => Some(i64::from(*val)), Value::SInt16(val) => Some(i64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(i64::from(*val)), Value::SInt32(val) => Some(i64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(i64::from(*val)), Value::SInt64(val) => Some(*val), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as i64), Value::Float32(val) => Some(*val as i64), Value::Float64(val) => Some(*val as i64), _ => None, } } #[inline] #[must_use] pub fn get_degrees_from_semicircles(s: f64) -> f64 { s * 180.0 / (2_147_483_648.0) } /// # Errors /// Return error if db query fails pub async fn get_authorized_users(pool: &PgPool) -> Result<HashSet<StackString>, Error> { let query = query!("SELECT email FROM authorized_users"); let conn = pool.get().await?; query .query_streaming(&conn) .await? .and_then(|row| async move { let email: StackString = row.try_get(0).map_err(PqError::BeginTransaction)?; Ok(email) }) .try_collect() .await .map_err(Into::into) } /// # Errors /// Return error if db query fails pub async fn get_list_of_telegram_userids( pool: &PgPool, ) -> Result<impl Stream<Item = Result<i64, PqError>>, Error> { let query = query!( " SELECT distinct telegram_userid FROM authorized_users WHERE telegram_userid IS NOT NULL " ); let conn = pool.get().await?; query .query_streaming(&conn) .await .map(|stream| { stream.and_then(|row| async move { let telegram_id: i64 = row .try_get("telegram_userid") .map_err(PqError::BeginTransaction)?; Ok(telegram_id) }) }) .map_err(Into::into) } #[must_use] pub fn get_random_string() -> StackString { let random_bytes: SmallVec<[u8; 16]> = (0..16).map(|_| thread_rng().gen::<u8>()).collect(); URL_SAFE_NO_PAD.encode(&random_bytes).into() }
random_line_split
garmin_util.rs
use anyhow::{format_err, Error}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use fitparser::Value; use flate2::{read::GzEncoder, Compression}; use futures::{Stream, TryStreamExt}; use log::{debug, error}; use num_traits::pow::Pow; use postgres_query::{query, Error as PqError}; use rand::{ distributions::{Alphanumeric, Distribution, Uniform}, thread_rng, Rng, }; use smallvec::SmallVec; use stack_string::{format_sstr, StackString}; use std::{ collections::HashSet, convert::TryInto, fs::{remove_file, File}, future::Future, io::{BufRead, BufReader, Read}, path::{Path, PathBuf}, }; use subprocess::{Exec, Redirection}; use time::{format_description::well_known::Rfc3339, macros::date, Date, Month, OffsetDateTime}; use time_tz::{timezones::db::UTC, OffsetDateTimeExt}; use tokio::time::{sleep, Duration}; use crate::common::pgpool::PgPool; pub const METERS_PER_MILE: f64 = 1609.344; pub const MARATHON_DISTANCE_M: i32 = 42195; pub const MARATHON_DISTANCE_MI: f64 = MARATHON_DISTANCE_M as f64 / METERS_PER_MILE; pub const MONTH_NAMES: [&str; 12] = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; pub const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; /// # Errors /// Return error if parsing time string fails pub fn convert_time_string(time_str: &str) -> Result<f64, Error> { let entries: SmallVec<[&str; 3]> = time_str.split(':').take(3).collect(); let (h, m, s): (i32, i32, f64) = match entries.first() { Some(h) => match entries.get(1) { Some(m) => match entries.get(2) { Some(s) => (h.parse()?, m.parse()?, s.parse()?), None => (h.parse()?, m.parse()?, 0.), }, None => (h.parse()?, 0, 0.), }, None => (0, 0, 0.), }; Ok(s + 60.0 * (f64::from(m) + 60.0 * f64::from(h))) } /// # Errors /// Return error if parsing time string fails pub fn convert_xml_local_time_to_utc(xml_local_time: &str) -> Result<OffsetDateTime, Error> { OffsetDateTime::parse(xml_local_time, &Rfc3339) .map(|x| x.to_timezone(UTC)) .map_err(Into::into) } /// # Errors /// Return error if running `md5sum` fails pub fn get_md5sum(filename: &Path) -> Result<StackString, Error> { if !Path::new("/usr/bin/md5sum").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/md5sum)" )); } let command = format_sstr!("md5sum {}", filename.to_string_lossy()); let stream = Exec::shell(command).stream_stdout()?; let reader = BufReader::new(stream); if let Some(line) = reader.lines().next() { if let Some(entry) = line?.split_whitespace().next() { return Ok(entry.into()); } } Ok("".into()) } /// # Errors /// Return error if second is negative pub fn print_h_m_s(second: f64, do_hours: bool) -> Result<StackString, Error> { let hours = (second / 3600.0) as i32; let minutes = (second / 60.0) as i32 - hours * 60; let seconds = second as i32 - minutes * 60 - hours * 3600; if (hours > 0) | ((hours == 0) & do_hours) { Ok(format_sstr!("{hours:02}:{minutes:02}:{seconds:02}")) } else if hours == 0 { Ok(format_sstr!("{minutes:02}:{seconds:02}")) } else { Err(format_err!("Negative result!")) } } #[must_use] pub fn days_in_year(year: i32) -> i64 { (Date::from_calendar_date(year + 1, Month::January, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, Month::January, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn days_in_month(year: i32, month: u32) -> i64
#[must_use] pub fn expected_calories(weight: f64, pace_min_per_mile: f64, distance: f64) -> f64 { let cal_per_mi = weight * (0.0395 + 0.003_27 * (60. / pace_min_per_mile) + 0.000_455 * (60. / pace_min_per_mile).pow(2.0) + 0.000_801 * ((weight / 154.0) * 0.425 / weight * (60. / pace_min_per_mile).pow(3.0)) * 60. / (60. / pace_min_per_mile)); cal_per_mi * distance } #[must_use] pub fn titlecase(input: &str) -> StackString { if input.is_empty() { "".into() } else { let firstchar = input[0..1].to_uppercase(); format_sstr!("{firstchar}{s}", s = &input[1..]) } } #[must_use] pub fn generate_random_string(nchar: usize) -> StackString { let mut rng = thread_rng(); Alphanumeric .sample_iter(&mut rng) .take(nchar) .map(Into::into) .collect() } #[must_use] pub fn get_file_list(path: &Path) -> Vec<PathBuf> { match path.read_dir() { Ok(it) => it .filter_map(|dir_line| match dir_line { Ok(entry) => Some(entry.path()), Err(_) => None, }) .collect(), Err(err) => { debug!("{}", err); Vec::new() } } } /// # Errors /// Return error if closure fails pub async fn exponential_retry<T, U, F>(f: T) -> Result<U, Error> where T: Fn() -> F, F: Future<Output = Result<U, Error>>, { let mut timeout: f64 = 1.0; let range = Uniform::from(0..1000); loop { match f().await { Ok(resp) => return Ok(resp), Err(err) => { sleep(Duration::from_millis((timeout * 1000.0) as u64)).await; timeout *= 4.0 * f64::from(range.sample(&mut thread_rng())) / 1000.0; if timeout >= 64.0 { return Err(err); } } } } } fn extract_zip(filename: &Path, ziptmpdir: &Path) -> Result<(), Error> { if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/unzip" )); } let command = format_sstr!( "unzip {} -d {}", filename.to_string_lossy(), ziptmpdir.to_string_lossy() ); let mut process = Exec::shell(command).stdout(Redirection::Pipe).popen()?; let exit_status = process.wait()?; if !exit_status.success() { if let Some(mut f) = process.stdout.as_ref() { let mut buf = String::new(); f.read_to_string(&mut buf)?; error!("{}", buf); } return Err(format_err!("Failed with exit status {exit_status:?}")); } Ok(()) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect( filename: &Path, ziptmpdir: &Path, ) -> Result<PathBuf, Error> { extract_zip(filename, ziptmpdir)?; let new_filename = filename .file_stem() .ok_or_else(|| format_err!("Bad filename {}", filename.to_string_lossy()))?; let new_filename = format_sstr!("{}_ACTIVITY.fit", new_filename.to_string_lossy()); let new_filename = ziptmpdir.join(new_filename); if !new_filename.exists() { return Err(format_err!("Activity file not found")); } remove_file(filename)?; Ok(new_filename) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect_multiple( filename: &Path, ziptmpdir: &Path, ) -> Result<Vec<PathBuf>, Error> { extract_zip(filename, ziptmpdir)?; if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "unzip not installed (or not present at /usr/bin/unzip" )); } let mut files = Vec::new(); for entry in ziptmpdir.read_dir()? { let entry = entry?; files.push(entry.path()); } if !files.is_empty() { remove_file(filename)?; } Ok(files) } /// # Errors /// Return error if: /// * input file does not exist /// * opening it fails /// * creating the output file fails /// * writing to the file fails pub fn gzip_file<T, U>(input_filename: T, output_filename: U) -> Result<(), Error> where T: AsRef<Path>, U: AsRef<Path>, { let input_filename = input_filename.as_ref(); let output_filename = output_filename.as_ref(); if !input_filename.exists() { return Err(format_err!("File {input_filename:?} does not exist")); } std::io::copy( &mut GzEncoder::new(File::open(input_filename)?, Compression::fast()), &mut File::create(output_filename)?, )?; Ok(()) } #[must_use] pub fn get_f64(value: &Value) -> Option<f64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp() as f64), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(f64::from(*val)) } Value::SInt8(val) => Some(f64::from(*val)), Value::SInt16(val) => Some(f64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(f64::from(*val)), Value::SInt32(val) => Some(f64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(f64::from(*val)), Value::SInt64(val) => Some(*val as f64), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as f64), Value::Float32(val) => Some(f64::from(*val)), Value::Float64(val) => Some(*val), _ => None, } } #[must_use] pub fn get_i64(value: &Value) -> Option<i64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp()), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(i64::from(*val)) } Value::SInt8(val) => Some(i64::from(*val)), Value::SInt16(val) => Some(i64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(i64::from(*val)), Value::SInt32(val) => Some(i64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(i64::from(*val)), Value::SInt64(val) => Some(*val), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as i64), Value::Float32(val) => Some(*val as i64), Value::Float64(val) => Some(*val as i64), _ => None, } } #[inline] #[must_use] pub fn get_degrees_from_semicircles(s: f64) -> f64 { s * 180.0 / (2_147_483_648.0) } /// # Errors /// Return error if db query fails pub async fn get_authorized_users(pool: &PgPool) -> Result<HashSet<StackString>, Error> { let query = query!("SELECT email FROM authorized_users"); let conn = pool.get().await?; query .query_streaming(&conn) .await? .and_then(|row| async move { let email: StackString = row.try_get(0).map_err(PqError::BeginTransaction)?; Ok(email) }) .try_collect() .await .map_err(Into::into) } /// # Errors /// Return error if db query fails pub async fn get_list_of_telegram_userids( pool: &PgPool, ) -> Result<impl Stream<Item = Result<i64, PqError>>, Error> { let query = query!( " SELECT distinct telegram_userid FROM authorized_users WHERE telegram_userid IS NOT NULL " ); let conn = pool.get().await?; query .query_streaming(&conn) .await .map(|stream| { stream.and_then(|row| async move { let telegram_id: i64 = row .try_get("telegram_userid") .map_err(PqError::BeginTransaction)?; Ok(telegram_id) }) }) .map_err(Into::into) } #[must_use] pub fn get_random_string() -> StackString { let random_bytes: SmallVec<[u8; 16]> = (0..16).map(|_| thread_rng().gen::<u8>()).collect(); URL_SAFE_NO_PAD.encode(&random_bytes).into() }
{ let mut y1 = year; let mut m1 = month + 1; if m1 == 13 { y1 += 1; m1 = 1; } let month: Month = (month as u8).try_into().unwrap_or(Month::January); let m1: Month = (m1 as u8).try_into().unwrap_or(Month::January); (Date::from_calendar_date(y1, m1, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, month, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() }
identifier_body
garmin_util.rs
use anyhow::{format_err, Error}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use fitparser::Value; use flate2::{read::GzEncoder, Compression}; use futures::{Stream, TryStreamExt}; use log::{debug, error}; use num_traits::pow::Pow; use postgres_query::{query, Error as PqError}; use rand::{ distributions::{Alphanumeric, Distribution, Uniform}, thread_rng, Rng, }; use smallvec::SmallVec; use stack_string::{format_sstr, StackString}; use std::{ collections::HashSet, convert::TryInto, fs::{remove_file, File}, future::Future, io::{BufRead, BufReader, Read}, path::{Path, PathBuf}, }; use subprocess::{Exec, Redirection}; use time::{format_description::well_known::Rfc3339, macros::date, Date, Month, OffsetDateTime}; use time_tz::{timezones::db::UTC, OffsetDateTimeExt}; use tokio::time::{sleep, Duration}; use crate::common::pgpool::PgPool; pub const METERS_PER_MILE: f64 = 1609.344; pub const MARATHON_DISTANCE_M: i32 = 42195; pub const MARATHON_DISTANCE_MI: f64 = MARATHON_DISTANCE_M as f64 / METERS_PER_MILE; pub const MONTH_NAMES: [&str; 12] = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; pub const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; /// # Errors /// Return error if parsing time string fails pub fn convert_time_string(time_str: &str) -> Result<f64, Error> { let entries: SmallVec<[&str; 3]> = time_str.split(':').take(3).collect(); let (h, m, s): (i32, i32, f64) = match entries.first() { Some(h) => match entries.get(1) { Some(m) => match entries.get(2) { Some(s) => (h.parse()?, m.parse()?, s.parse()?), None => (h.parse()?, m.parse()?, 0.), }, None => (h.parse()?, 0, 0.), }, None => (0, 0, 0.), }; Ok(s + 60.0 * (f64::from(m) + 60.0 * f64::from(h))) } /// # Errors /// Return error if parsing time string fails pub fn convert_xml_local_time_to_utc(xml_local_time: &str) -> Result<OffsetDateTime, Error> { OffsetDateTime::parse(xml_local_time, &Rfc3339) .map(|x| x.to_timezone(UTC)) .map_err(Into::into) } /// # Errors /// Return error if running `md5sum` fails pub fn get_md5sum(filename: &Path) -> Result<StackString, Error> { if !Path::new("/usr/bin/md5sum").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/md5sum)" )); } let command = format_sstr!("md5sum {}", filename.to_string_lossy()); let stream = Exec::shell(command).stream_stdout()?; let reader = BufReader::new(stream); if let Some(line) = reader.lines().next() { if let Some(entry) = line?.split_whitespace().next() { return Ok(entry.into()); } } Ok("".into()) } /// # Errors /// Return error if second is negative pub fn print_h_m_s(second: f64, do_hours: bool) -> Result<StackString, Error> { let hours = (second / 3600.0) as i32; let minutes = (second / 60.0) as i32 - hours * 60; let seconds = second as i32 - minutes * 60 - hours * 3600; if (hours > 0) | ((hours == 0) & do_hours) { Ok(format_sstr!("{hours:02}:{minutes:02}:{seconds:02}")) } else if hours == 0 { Ok(format_sstr!("{minutes:02}:{seconds:02}")) } else { Err(format_err!("Negative result!")) } } #[must_use] pub fn days_in_year(year: i32) -> i64 { (Date::from_calendar_date(year + 1, Month::January, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, Month::January, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn days_in_month(year: i32, month: u32) -> i64 { let mut y1 = year; let mut m1 = month + 1; if m1 == 13 { y1 += 1; m1 = 1; } let month: Month = (month as u8).try_into().unwrap_or(Month::January); let m1: Month = (m1 as u8).try_into().unwrap_or(Month::January); (Date::from_calendar_date(y1, m1, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, month, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn expected_calories(weight: f64, pace_min_per_mile: f64, distance: f64) -> f64 { let cal_per_mi = weight * (0.0395 + 0.003_27 * (60. / pace_min_per_mile) + 0.000_455 * (60. / pace_min_per_mile).pow(2.0) + 0.000_801 * ((weight / 154.0) * 0.425 / weight * (60. / pace_min_per_mile).pow(3.0)) * 60. / (60. / pace_min_per_mile)); cal_per_mi * distance } #[must_use] pub fn titlecase(input: &str) -> StackString { if input.is_empty() { "".into() } else { let firstchar = input[0..1].to_uppercase(); format_sstr!("{firstchar}{s}", s = &input[1..]) } } #[must_use] pub fn generate_random_string(nchar: usize) -> StackString { let mut rng = thread_rng(); Alphanumeric .sample_iter(&mut rng) .take(nchar) .map(Into::into) .collect() } #[must_use] pub fn get_file_list(path: &Path) -> Vec<PathBuf> { match path.read_dir() { Ok(it) => it .filter_map(|dir_line| match dir_line { Ok(entry) => Some(entry.path()), Err(_) => None, }) .collect(), Err(err) => { debug!("{}", err); Vec::new() } } } /// # Errors /// Return error if closure fails pub async fn
<T, U, F>(f: T) -> Result<U, Error> where T: Fn() -> F, F: Future<Output = Result<U, Error>>, { let mut timeout: f64 = 1.0; let range = Uniform::from(0..1000); loop { match f().await { Ok(resp) => return Ok(resp), Err(err) => { sleep(Duration::from_millis((timeout * 1000.0) as u64)).await; timeout *= 4.0 * f64::from(range.sample(&mut thread_rng())) / 1000.0; if timeout >= 64.0 { return Err(err); } } } } } fn extract_zip(filename: &Path, ziptmpdir: &Path) -> Result<(), Error> { if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/unzip" )); } let command = format_sstr!( "unzip {} -d {}", filename.to_string_lossy(), ziptmpdir.to_string_lossy() ); let mut process = Exec::shell(command).stdout(Redirection::Pipe).popen()?; let exit_status = process.wait()?; if !exit_status.success() { if let Some(mut f) = process.stdout.as_ref() { let mut buf = String::new(); f.read_to_string(&mut buf)?; error!("{}", buf); } return Err(format_err!("Failed with exit status {exit_status:?}")); } Ok(()) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect( filename: &Path, ziptmpdir: &Path, ) -> Result<PathBuf, Error> { extract_zip(filename, ziptmpdir)?; let new_filename = filename .file_stem() .ok_or_else(|| format_err!("Bad filename {}", filename.to_string_lossy()))?; let new_filename = format_sstr!("{}_ACTIVITY.fit", new_filename.to_string_lossy()); let new_filename = ziptmpdir.join(new_filename); if !new_filename.exists() { return Err(format_err!("Activity file not found")); } remove_file(filename)?; Ok(new_filename) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect_multiple( filename: &Path, ziptmpdir: &Path, ) -> Result<Vec<PathBuf>, Error> { extract_zip(filename, ziptmpdir)?; if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "unzip not installed (or not present at /usr/bin/unzip" )); } let mut files = Vec::new(); for entry in ziptmpdir.read_dir()? { let entry = entry?; files.push(entry.path()); } if !files.is_empty() { remove_file(filename)?; } Ok(files) } /// # Errors /// Return error if: /// * input file does not exist /// * opening it fails /// * creating the output file fails /// * writing to the file fails pub fn gzip_file<T, U>(input_filename: T, output_filename: U) -> Result<(), Error> where T: AsRef<Path>, U: AsRef<Path>, { let input_filename = input_filename.as_ref(); let output_filename = output_filename.as_ref(); if !input_filename.exists() { return Err(format_err!("File {input_filename:?} does not exist")); } std::io::copy( &mut GzEncoder::new(File::open(input_filename)?, Compression::fast()), &mut File::create(output_filename)?, )?; Ok(()) } #[must_use] pub fn get_f64(value: &Value) -> Option<f64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp() as f64), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(f64::from(*val)) } Value::SInt8(val) => Some(f64::from(*val)), Value::SInt16(val) => Some(f64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(f64::from(*val)), Value::SInt32(val) => Some(f64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(f64::from(*val)), Value::SInt64(val) => Some(*val as f64), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as f64), Value::Float32(val) => Some(f64::from(*val)), Value::Float64(val) => Some(*val), _ => None, } } #[must_use] pub fn get_i64(value: &Value) -> Option<i64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp()), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(i64::from(*val)) } Value::SInt8(val) => Some(i64::from(*val)), Value::SInt16(val) => Some(i64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(i64::from(*val)), Value::SInt32(val) => Some(i64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(i64::from(*val)), Value::SInt64(val) => Some(*val), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as i64), Value::Float32(val) => Some(*val as i64), Value::Float64(val) => Some(*val as i64), _ => None, } } #[inline] #[must_use] pub fn get_degrees_from_semicircles(s: f64) -> f64 { s * 180.0 / (2_147_483_648.0) } /// # Errors /// Return error if db query fails pub async fn get_authorized_users(pool: &PgPool) -> Result<HashSet<StackString>, Error> { let query = query!("SELECT email FROM authorized_users"); let conn = pool.get().await?; query .query_streaming(&conn) .await? .and_then(|row| async move { let email: StackString = row.try_get(0).map_err(PqError::BeginTransaction)?; Ok(email) }) .try_collect() .await .map_err(Into::into) } /// # Errors /// Return error if db query fails pub async fn get_list_of_telegram_userids( pool: &PgPool, ) -> Result<impl Stream<Item = Result<i64, PqError>>, Error> { let query = query!( " SELECT distinct telegram_userid FROM authorized_users WHERE telegram_userid IS NOT NULL " ); let conn = pool.get().await?; query .query_streaming(&conn) .await .map(|stream| { stream.and_then(|row| async move { let telegram_id: i64 = row .try_get("telegram_userid") .map_err(PqError::BeginTransaction)?; Ok(telegram_id) }) }) .map_err(Into::into) } #[must_use] pub fn get_random_string() -> StackString { let random_bytes: SmallVec<[u8; 16]> = (0..16).map(|_| thread_rng().gen::<u8>()).collect(); URL_SAFE_NO_PAD.encode(&random_bytes).into() }
exponential_retry
identifier_name
garmin_util.rs
use anyhow::{format_err, Error}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use fitparser::Value; use flate2::{read::GzEncoder, Compression}; use futures::{Stream, TryStreamExt}; use log::{debug, error}; use num_traits::pow::Pow; use postgres_query::{query, Error as PqError}; use rand::{ distributions::{Alphanumeric, Distribution, Uniform}, thread_rng, Rng, }; use smallvec::SmallVec; use stack_string::{format_sstr, StackString}; use std::{ collections::HashSet, convert::TryInto, fs::{remove_file, File}, future::Future, io::{BufRead, BufReader, Read}, path::{Path, PathBuf}, }; use subprocess::{Exec, Redirection}; use time::{format_description::well_known::Rfc3339, macros::date, Date, Month, OffsetDateTime}; use time_tz::{timezones::db::UTC, OffsetDateTimeExt}; use tokio::time::{sleep, Duration}; use crate::common::pgpool::PgPool; pub const METERS_PER_MILE: f64 = 1609.344; pub const MARATHON_DISTANCE_M: i32 = 42195; pub const MARATHON_DISTANCE_MI: f64 = MARATHON_DISTANCE_M as f64 / METERS_PER_MILE; pub const MONTH_NAMES: [&str; 12] = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; pub const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; /// # Errors /// Return error if parsing time string fails pub fn convert_time_string(time_str: &str) -> Result<f64, Error> { let entries: SmallVec<[&str; 3]> = time_str.split(':').take(3).collect(); let (h, m, s): (i32, i32, f64) = match entries.first() { Some(h) => match entries.get(1) { Some(m) => match entries.get(2) { Some(s) => (h.parse()?, m.parse()?, s.parse()?), None => (h.parse()?, m.parse()?, 0.), }, None => (h.parse()?, 0, 0.), }, None => (0, 0, 0.), }; Ok(s + 60.0 * (f64::from(m) + 60.0 * f64::from(h))) } /// # Errors /// Return error if parsing time string fails pub fn convert_xml_local_time_to_utc(xml_local_time: &str) -> Result<OffsetDateTime, Error> { OffsetDateTime::parse(xml_local_time, &Rfc3339) .map(|x| x.to_timezone(UTC)) .map_err(Into::into) } /// # Errors /// Return error if running `md5sum` fails pub fn get_md5sum(filename: &Path) -> Result<StackString, Error> { if !Path::new("/usr/bin/md5sum").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/md5sum)" )); } let command = format_sstr!("md5sum {}", filename.to_string_lossy()); let stream = Exec::shell(command).stream_stdout()?; let reader = BufReader::new(stream); if let Some(line) = reader.lines().next() { if let Some(entry) = line?.split_whitespace().next() { return Ok(entry.into()); } } Ok("".into()) } /// # Errors /// Return error if second is negative pub fn print_h_m_s(second: f64, do_hours: bool) -> Result<StackString, Error> { let hours = (second / 3600.0) as i32; let minutes = (second / 60.0) as i32 - hours * 60; let seconds = second as i32 - minutes * 60 - hours * 3600; if (hours > 0) | ((hours == 0) & do_hours) { Ok(format_sstr!("{hours:02}:{minutes:02}:{seconds:02}")) } else if hours == 0 { Ok(format_sstr!("{minutes:02}:{seconds:02}")) } else { Err(format_err!("Negative result!")) } } #[must_use] pub fn days_in_year(year: i32) -> i64 { (Date::from_calendar_date(year + 1, Month::January, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, Month::January, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn days_in_month(year: i32, month: u32) -> i64 { let mut y1 = year; let mut m1 = month + 1; if m1 == 13 { y1 += 1; m1 = 1; } let month: Month = (month as u8).try_into().unwrap_or(Month::January); let m1: Month = (m1 as u8).try_into().unwrap_or(Month::January); (Date::from_calendar_date(y1, m1, 1).unwrap_or(date!(1969 - 01 - 01)) - Date::from_calendar_date(year, month, 1).unwrap_or(date!(1969 - 01 - 01))) .whole_days() } #[must_use] pub fn expected_calories(weight: f64, pace_min_per_mile: f64, distance: f64) -> f64 { let cal_per_mi = weight * (0.0395 + 0.003_27 * (60. / pace_min_per_mile) + 0.000_455 * (60. / pace_min_per_mile).pow(2.0) + 0.000_801 * ((weight / 154.0) * 0.425 / weight * (60. / pace_min_per_mile).pow(3.0)) * 60. / (60. / pace_min_per_mile)); cal_per_mi * distance } #[must_use] pub fn titlecase(input: &str) -> StackString { if input.is_empty() { "".into() } else { let firstchar = input[0..1].to_uppercase(); format_sstr!("{firstchar}{s}", s = &input[1..]) } } #[must_use] pub fn generate_random_string(nchar: usize) -> StackString { let mut rng = thread_rng(); Alphanumeric .sample_iter(&mut rng) .take(nchar) .map(Into::into) .collect() } #[must_use] pub fn get_file_list(path: &Path) -> Vec<PathBuf> { match path.read_dir() { Ok(it) => it .filter_map(|dir_line| match dir_line { Ok(entry) => Some(entry.path()), Err(_) => None, }) .collect(), Err(err) => { debug!("{}", err); Vec::new() } } } /// # Errors /// Return error if closure fails pub async fn exponential_retry<T, U, F>(f: T) -> Result<U, Error> where T: Fn() -> F, F: Future<Output = Result<U, Error>>, { let mut timeout: f64 = 1.0; let range = Uniform::from(0..1000); loop { match f().await { Ok(resp) => return Ok(resp), Err(err) => { sleep(Duration::from_millis((timeout * 1000.0) as u64)).await; timeout *= 4.0 * f64::from(range.sample(&mut thread_rng())) / 1000.0; if timeout >= 64.0 { return Err(err); } } } } } fn extract_zip(filename: &Path, ziptmpdir: &Path) -> Result<(), Error> { if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "md5sum not installed (or not present at /usr/bin/unzip" )); } let command = format_sstr!( "unzip {} -d {}", filename.to_string_lossy(), ziptmpdir.to_string_lossy() ); let mut process = Exec::shell(command).stdout(Redirection::Pipe).popen()?; let exit_status = process.wait()?; if !exit_status.success()
Ok(()) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect( filename: &Path, ziptmpdir: &Path, ) -> Result<PathBuf, Error> { extract_zip(filename, ziptmpdir)?; let new_filename = filename .file_stem() .ok_or_else(|| format_err!("Bad filename {}", filename.to_string_lossy()))?; let new_filename = format_sstr!("{}_ACTIVITY.fit", new_filename.to_string_lossy()); let new_filename = ziptmpdir.join(new_filename); if !new_filename.exists() { return Err(format_err!("Activity file not found")); } remove_file(filename)?; Ok(new_filename) } /// # Errors /// Return error if unzip fails pub fn extract_zip_from_garmin_connect_multiple( filename: &Path, ziptmpdir: &Path, ) -> Result<Vec<PathBuf>, Error> { extract_zip(filename, ziptmpdir)?; if !Path::new("/usr/bin/unzip").exists() { return Err(format_err!( "unzip not installed (or not present at /usr/bin/unzip" )); } let mut files = Vec::new(); for entry in ziptmpdir.read_dir()? { let entry = entry?; files.push(entry.path()); } if !files.is_empty() { remove_file(filename)?; } Ok(files) } /// # Errors /// Return error if: /// * input file does not exist /// * opening it fails /// * creating the output file fails /// * writing to the file fails pub fn gzip_file<T, U>(input_filename: T, output_filename: U) -> Result<(), Error> where T: AsRef<Path>, U: AsRef<Path>, { let input_filename = input_filename.as_ref(); let output_filename = output_filename.as_ref(); if !input_filename.exists() { return Err(format_err!("File {input_filename:?} does not exist")); } std::io::copy( &mut GzEncoder::new(File::open(input_filename)?, Compression::fast()), &mut File::create(output_filename)?, )?; Ok(()) } #[must_use] pub fn get_f64(value: &Value) -> Option<f64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp() as f64), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(f64::from(*val)) } Value::SInt8(val) => Some(f64::from(*val)), Value::SInt16(val) => Some(f64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(f64::from(*val)), Value::SInt32(val) => Some(f64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(f64::from(*val)), Value::SInt64(val) => Some(*val as f64), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as f64), Value::Float32(val) => Some(f64::from(*val)), Value::Float64(val) => Some(*val), _ => None, } } #[must_use] pub fn get_i64(value: &Value) -> Option<i64> { match value { Value::Timestamp(val) => Some(val.unix_timestamp()), Value::Byte(val) | Value::Enum(val) | Value::UInt8(val) | Value::UInt8z(val) => { Some(i64::from(*val)) } Value::SInt8(val) => Some(i64::from(*val)), Value::SInt16(val) => Some(i64::from(*val)), Value::UInt16(val) | Value::UInt16z(val) => Some(i64::from(*val)), Value::SInt32(val) => Some(i64::from(*val)), Value::UInt32(val) | Value::UInt32z(val) => Some(i64::from(*val)), Value::SInt64(val) => Some(*val), Value::UInt64(val) | Value::UInt64z(val) => Some(*val as i64), Value::Float32(val) => Some(*val as i64), Value::Float64(val) => Some(*val as i64), _ => None, } } #[inline] #[must_use] pub fn get_degrees_from_semicircles(s: f64) -> f64 { s * 180.0 / (2_147_483_648.0) } /// # Errors /// Return error if db query fails pub async fn get_authorized_users(pool: &PgPool) -> Result<HashSet<StackString>, Error> { let query = query!("SELECT email FROM authorized_users"); let conn = pool.get().await?; query .query_streaming(&conn) .await? .and_then(|row| async move { let email: StackString = row.try_get(0).map_err(PqError::BeginTransaction)?; Ok(email) }) .try_collect() .await .map_err(Into::into) } /// # Errors /// Return error if db query fails pub async fn get_list_of_telegram_userids( pool: &PgPool, ) -> Result<impl Stream<Item = Result<i64, PqError>>, Error> { let query = query!( " SELECT distinct telegram_userid FROM authorized_users WHERE telegram_userid IS NOT NULL " ); let conn = pool.get().await?; query .query_streaming(&conn) .await .map(|stream| { stream.and_then(|row| async move { let telegram_id: i64 = row .try_get("telegram_userid") .map_err(PqError::BeginTransaction)?; Ok(telegram_id) }) }) .map_err(Into::into) } #[must_use] pub fn get_random_string() -> StackString { let random_bytes: SmallVec<[u8; 16]> = (0..16).map(|_| thread_rng().gen::<u8>()).collect(); URL_SAFE_NO_PAD.encode(&random_bytes).into() }
{ if let Some(mut f) = process.stdout.as_ref() { let mut buf = String::new(); f.read_to_string(&mut buf)?; error!("{}", buf); } return Err(format_err!("Failed with exit status {exit_status:?}")); }
conditional_block
views.py
from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt import os import time from .clipmanager import ClipManager from .models import Channel, Clip, HighlightClip import shutil import traceback from datetime import datetime from clipmanager.tasks import generate_thumbnail_task, encode_video, download_clip # Create your views here. def videoplayer(request): url = request.GET.get('videourl') return render(request, 'blog/videoplayer.html', {'videourl': url}) def dashboard(response): return render(response, 'blog/base_admin.html') def index(response): if response.user is not []: user = response.user return render(response, "blog/index.html", {'user': user}) def login(response): return render(response, "blog/login.html") def submit_success(response): return render(response, 'blog/submit-success.html') def submit(response): if response.user is not []: user = response.user # print(response.POST) # <QueryDict: {'csrfmiddlewaretoken': ['TyTMLhsydMTLShkiiE2VCBLQPRjME9bbEtdDN2qK87Q7NBSETC137bbpYHXUDGae'], 'start': ['0'], 'duration': ['5'], 'author': ['jack'], 'title': ['研发日志-人工智能编程'], 'target-filename': ['长身之处.mp4'], 'customRadioInline1': ['on']}> if response.POST.get('target-filename') is not None: target_filename = response.POST.get('target-filename') author = response.POST.get('author') title = response.POST.get('title') start = response.POST.get('start') duration = response.POST.get('duration') if response.POST.get('customRadioInline1') == 'on': cliptype = 0 else: cliptype = 1 clip_used = False user_path = os.path.join('upload', user.username) user_upload_path = os.path.join('upload', user.username, 'uploaded') user_encode_path = os.path.join('upload', user.username, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) path = os.path.join(user_upload_path, target_filename) upload_complete = True encode_complete = False file_deleted = False highlight = HighlightClip() highlight.user = user highlight.author = author highlight.title = title highlight.filename = target_filename highlight.path = path highlight.start_time = start highlight.duration = duration highlight.upload_complete = upload_complete highlight.encode_complete = encode_complete highlight.file_deleted = file_deleted highlight.clip_type = cliptype highlight.clip_used = clip_used highlight.save() return HttpResponseRedirect('submit_success', {'user': user}) return render(response, "blog/submit.html", {'user': user}) def home(request): return HttpResponse('<h1>Home</h1>') def about(request): return HttpResponse('<h1>About</h1>') def ajax_macie(request): print(request.GET) if request.GET.get('action') is not None: action = request.GET.get('action') if request.GET.get('slug') is not None: slug = request.GET.get('slug') if action == 'accept': clip = Clip.objects.filter(slug=slug)[0] print('accepting ', clip.title) clip.reviewed = True clip.accepted = True clip.save() elif action == 'reject': clip = Clip.objects.filter(slug=slug)[0] print('rejecting ', clip.title) clip.reviewed = True clip.rejected = True clip.save() return HttpResponse('ok') # return render(request, 'ajax2.html') def download_videos(request): download_clip.delay() return HttpResponse('<h1>Clips Download Started</h1>') def macie(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), rejected=False, accepted=False,channel=channel).order_by('-views') return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': True}) def update_clip_views(response): clips = [] clips = Clip.objects.filter(reviewed=False, downloaded=False) clipmanager = ClipManager() count = 1 total = len(clips) print("UPDATING VIEWS") for item in clips: item = clipmanager.update_cl
1>所有直播的播放数已更新</h1>') def macie_reviewed(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=False,channel=channel) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def macie_downloaded(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=True) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def update_macie_twitch(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='week', limit=100, cursor="") return HttpResponse('<h1>每周的直播片段已更新</h1>') def update_macie_daily(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='day', limit=30, cursor="") return HttpResponse('<h1>MacieJay\'s 每天的直播片段已更新</h1>') def update_macie_all(request): clipmanager = ClipManager() try: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") except Exception as e: print(e) finally: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") return HttpResponse('<h1>MacieJay\'s 排名靠前5000的直播片段已更新</h1>') def update_download_url(request): clips = Clip.objects.all() clipmanager = ClipManager() for item in clips: clipmanager.update_clip_url(item) return HttpResponse('<h1>所有直播片段的下载地址已更新</h1>') def upload(request): return render(request, 'blog/upload.html') @csrf_exempt def upload_index(request): if request.method == 'POST': task = request.POST.get('task_id') # 获取文件的唯一标识符 chunk = request.POST.get('chunk', 0) # 获取该分片在所有分片中的序号 filename = '%s%s' % (task, chunk) # 构造该分片的唯一标识符 upload_file = request.FILES['file'] with open('upload/%s' % filename, 'wb') as f: f.write(upload_file.read()) print("upload ...") return HttpResponse('ok') @csrf_exempt def upload_complete(request): target_filename = request.GET.get('filename') # 获取上传文件的文件名 task = request.GET.get('task_id') # 获取文件的唯一标识符 user = request.GET.get('user') chunk = 0 # 分片序号 print(target_filename, task) with open('upload/%s' % target_filename, 'wb') as target_file: # 创建新文件 while True: try: filename = 'upload/%s%d' % (task, chunk) source_file = open(filename, 'rb') # 按序打开每个分片 target_file.write(source_file.read()) # 读取分片内容写入新文件 source_file.close() except IOError as msg: break chunk += 1 os.remove(filename) # 删除该分片,节约空间 time.sleep(1) # 如果用户的文件夹不存在 user_path = os.path.join('upload', user) user_upload_path = os.path.join('upload', user, 'uploaded') user_encode_path = os.path.join('upload', user, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) if os.path.exists(user_upload_path): try: if os.path.exists(os.path.join(user_upload_path, target_filename)): os.remove(os.path.join(user_upload_path, target_filename)) shutil.move(os.path.join('upload', target_filename), user_upload_path) except Exception as e: print('move_file ERROR: ', e) traceback.print_exc() time.sleep(1) return render(request, "blog/index.html", {'target_filename': target_filename, 'user_upload_path': user_upload_path}) def save_agent(f, username): # whether the existing is in tesing or validation path_valid = "validateAgent/" + username + "/" path_test = "testingAgent/" + username + "/" if os.path.exists(path_valid) or os.path.exists(path_test): return False path = 'uploadAgent/' + username + '.zip' print("start.....") with open(path, 'wb+') as destination: print("open safely") for chunk in f.chunks(): destination.write(chunk) print("submit finished!") return True
ip_view(item) print("[UPDATE CLIP VIEWS]", count, ' of ', total) if item.views > 5: item.rejected = False item.accepted = False print("[New Commer!]") if item.views < 6: item.rejected = True print(count, ' has been REJECTED') item.save() count += 1 return HttpResponse('<h
conditional_block
views.py
from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt import os import time from .clipmanager import ClipManager from .models import Channel, Clip, HighlightClip import shutil import traceback from datetime import datetime from clipmanager.tasks import generate_thumbnail_task, encode_video, download_clip # Create your views here. def videoplayer(request): url = request.GET.get('videourl') return render(request, 'blog/videoplayer.html', {'videourl': url}) def dashboard(response): return render(response, 'blog/base_admin.html') def index(response): if response.user is not []: user = response.user return render(response, "blog/index.html", {'user': user}) def login(response): return render(response, "blog/login.html") def submit_success(response): return render(response, 'blog/submit-success.html') def submit(response): if response.user is not []: user = response.user # print(response.POST) # <QueryDict: {'csrfmiddlewaretoken': ['TyTMLhsydMTLShkiiE2VCBLQPRjME9bbEtdDN2qK87Q7NBSETC137bbpYHXUDGae'], 'start': ['0'], 'duration': ['5'], 'author': ['jack'], 'title': ['研发日志-人工智能编程'], 'target-filename': ['长身之处.mp4'], 'customRadioInline1': ['on']}> if response.POST.get('target-filename') is not None: target_filename = response.POST.get('target-filename') author = response.POST.get('author') title = response.POST.get('title') start = response.POST.get('start') duration = response.POST.get('duration') if response.POST.get('customRadioInline1') == 'on': cliptype = 0 else: cliptype = 1 clip_used = False user_path = os.path.join('upload', user.username) user_upload_path = os.path.join('upload', user.username, 'uploaded') user_encode_path = os.path.join('upload', user.username, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) path = os.path.join(user_upload_path, target_filename) upload_complete = True encode_complete = False file_deleted = False highlight = HighlightClip() highlight.user = user highlight.author = author highlight.title = title highlight.filename = target_filename highlight.path = path highlight.start_time = start highlight.duration = duration highlight.upload_complete = upload_complete highlight.encode_complete = encode_complete highlight.file_deleted = file_deleted highlight.clip_type = cliptype highlight.clip_used = clip_used highlight.save() return HttpResponseRedirect('submit_success', {'user': user}) return render(response, "blog/submit.html", {'user': user}) def home(request): return HttpResponse('<h1>Home</h1>') def about(request): return HttpResponse('<h1>About</h1>') def ajax_macie(request): print(request.GET) if request.GET.get('action') is not None: action = request.GET.get('action') if request.GET.get('slug') is not None: slug = request.GET.get('slug') if action == 'accept': clip = Clip.objects.filter(slug=slug)[0] print('accepting ', clip.title) clip.reviewed = True clip.accepted = True clip.save() elif action == 'reject': clip = Clip.objects.filter(slug=slug)[0] print('rejecting ', clip.title) clip.reviewed = True clip.rejected = True clip.save() return HttpResponse('ok') # return render(request, 'ajax2.html') def download_videos(request): download_clip.delay() return HttpResponse('<h1>Clips Download Started</h1>') def macie(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), rejected=False, accepted=False,channel=channel).order_by('-views') return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': True}) def update_clip_views(response): clips = [] clips = Clip.objects.filter(reviewed=False, downloaded=False) clipmanager = ClipManager() count = 1 total = len(clips) print("UPDATING VIEWS") for item in clips: item = clipmanager.update_clip_view(item) print("[UPDATE CLIP VIEWS]", count, ' of ', total) if item.views > 5: item.rejected = False item.accepted = False print("[New Commer!]") if item.views < 6: item.rejected = True print(count, ' has been REJECTED') item.save() count += 1 return HttpResponse('<h1>所有直播的播放数已更新</h1>') def macie_reviewed(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False,
accepted=True, downloaded=False,channel=channel) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def macie_downloaded(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=True) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def update_macie_twitch(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='week', limit=100, cursor="") return HttpResponse('<h1>每周的直播片段已更新</h1>') def update_macie_daily(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='day', limit=30, cursor="") return HttpResponse('<h1>MacieJay\'s 每天的直播片段已更新</h1>') def update_macie_all(request): clipmanager = ClipManager() try: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") except Exception as e: print(e) finally: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") return HttpResponse('<h1>MacieJay\'s 排名靠前5000的直播片段已更新</h1>') def update_download_url(request): clips = Clip.objects.all() clipmanager = ClipManager() for item in clips: clipmanager.update_clip_url(item) return HttpResponse('<h1>所有直播片段的下载地址已更新</h1>') def upload(request): return render(request, 'blog/upload.html') @csrf_exempt def upload_index(request): if request.method == 'POST': task = request.POST.get('task_id') # 获取文件的唯一标识符 chunk = request.POST.get('chunk', 0) # 获取该分片在所有分片中的序号 filename = '%s%s' % (task, chunk) # 构造该分片的唯一标识符 upload_file = request.FILES['file'] with open('upload/%s' % filename, 'wb') as f: f.write(upload_file.read()) print("upload ...") return HttpResponse('ok') @csrf_exempt def upload_complete(request): target_filename = request.GET.get('filename') # 获取上传文件的文件名 task = request.GET.get('task_id') # 获取文件的唯一标识符 user = request.GET.get('user') chunk = 0 # 分片序号 print(target_filename, task) with open('upload/%s' % target_filename, 'wb') as target_file: # 创建新文件 while True: try: filename = 'upload/%s%d' % (task, chunk) source_file = open(filename, 'rb') # 按序打开每个分片 target_file.write(source_file.read()) # 读取分片内容写入新文件 source_file.close() except IOError as msg: break chunk += 1 os.remove(filename) # 删除该分片,节约空间 time.sleep(1) # 如果用户的文件夹不存在 user_path = os.path.join('upload', user) user_upload_path = os.path.join('upload', user, 'uploaded') user_encode_path = os.path.join('upload', user, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) if os.path.exists(user_upload_path): try: if os.path.exists(os.path.join(user_upload_path, target_filename)): os.remove(os.path.join(user_upload_path, target_filename)) shutil.move(os.path.join('upload', target_filename), user_upload_path) except Exception as e: print('move_file ERROR: ', e) traceback.print_exc() time.sleep(1) return render(request, "blog/index.html", {'target_filename': target_filename, 'user_upload_path': user_upload_path}) def save_agent(f, username): # whether the existing is in tesing or validation path_valid = "validateAgent/" + username + "/" path_test = "testingAgent/" + username + "/" if os.path.exists(path_valid) or os.path.exists(path_test): return False path = 'uploadAgent/' + username + '.zip' print("start.....") with open(path, 'wb+') as destination: print("open safely") for chunk in f.chunks(): destination.write(chunk) print("submit finished!") return True
random_line_split
views.py
from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt import os import time from .clipmanager import ClipManager from .models import Channel, Clip, HighlightClip import shutil import traceback from datetime import datetime from clipmanager.tasks import generate_thumbnail_task, encode_video, download_clip # Create your views here. def videoplayer(request): url = request.GET.get('videourl') return render(request, 'blog/videoplayer.html', {'videourl': url}) def dashboard(response): return render(response, 'blog/base_admin.html') def index(response): if response.user is not []: user = response.user return render(response, "blog/index.html", {'user': user}) def login(response): return render(response, "blog/login.html") def submit_success(response): return render(response, 'blog/submit-success.html') def submit(response): if response.user is not []: user = response.user # print(response.POST) # <QueryDict: {'csrfmiddlewaretoken': ['TyTMLhsydMTLShkiiE2VCBLQPRjME9bbEtdDN2qK87Q7NBSETC137bbpYHXUDGae'], 'start': ['0'], 'duration': ['5'], 'author': ['jack'], 'title': ['研发日志-人工智能编程'], 'target-filename': ['长身之处.mp4'], 'customRadioInline1': ['on']}> if response.POST.get('target-filename') is not None: target_filename = response.POST.get('target-filename') author = response.POST.get('author') title = response.POST.get('title') start = response.POST.get('start') duration = response.POST.get('duration') if response.POST.get('customRadioInline1') == 'on': cliptype = 0 else: cliptype = 1 clip_used = False user_path = os.path.join('upload', user.username) user_upload_path = os.path.join('upload', user.username, 'uploaded') user_encode_path = os.path.join('upload', user.username, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) path = os.path.join(user_upload_path, target_filename) upload_complete = True encode_complete = False file_deleted = False highlight = HighlightClip() highlight.user = user highlight.author = author highlight.title = title highlight.filename = target_filename highlight.path = path highlight.start_time = start highlight.duration = duration highlight.upload_complete = upload_complete highlight.encode_complete = encode_complete highlight.file_deleted = file_deleted highlight.clip_type = cliptype highlight.clip_used = clip_used highlight.save() return HttpResponseRedirect('submit_success', {'user': user}) return render(response, "blog/submit.html", {'user': user}) def home(request): return HttpResponse('<h1>Home</h1>') def about(request): return HttpResponse('<h1>About</h1>') def ajax_macie(request): print(request.GET) if request.GET.get('action') is not None: action = request.GET.get('action') if request.GET.get('slug') is not None: slug = request.GET.get('slug') if action == 'accept': clip = Clip.objects.filter(slug=slug)[0] print('accepting ', clip.title) clip.reviewed = True clip.accepted = True clip.save() elif action == 'reject': clip = Clip.objects.filter(slug=slug)[0] print('rejecting ', clip.title) clip.reviewed = True clip.rejected = True clip.save() return HttpResponse('ok') # return render(request, 'ajax2.html') def download_videos(request): download_clip.delay() re
clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), rejected=False, accepted=False,channel=channel).order_by('-views') return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': True}) def update_clip_views(response): clips = [] clips = Clip.objects.filter(reviewed=False, downloaded=False) clipmanager = ClipManager() count = 1 total = len(clips) print("UPDATING VIEWS") for item in clips: item = clipmanager.update_clip_view(item) print("[UPDATE CLIP VIEWS]", count, ' of ', total) if item.views > 5: item.rejected = False item.accepted = False print("[New Commer!]") if item.views < 6: item.rejected = True print(count, ' has been REJECTED') item.save() count += 1 return HttpResponse('<h1>所有直播的播放数已更新</h1>') def macie_reviewed(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=False,channel=channel) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def macie_downloaded(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=True) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def update_macie_twitch(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='week', limit=100, cursor="") return HttpResponse('<h1>每周的直播片段已更新</h1>') def update_macie_daily(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='day', limit=30, cursor="") return HttpResponse('<h1>MacieJay\'s 每天的直播片段已更新</h1>') def update_macie_all(request): clipmanager = ClipManager() try: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") except Exception as e: print(e) finally: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") return HttpResponse('<h1>MacieJay\'s 排名靠前5000的直播片段已更新</h1>') def update_download_url(request): clips = Clip.objects.all() clipmanager = ClipManager() for item in clips: clipmanager.update_clip_url(item) return HttpResponse('<h1>所有直播片段的下载地址已更新</h1>') def upload(request): return render(request, 'blog/upload.html') @csrf_exempt def upload_index(request): if request.method == 'POST': task = request.POST.get('task_id') # 获取文件的唯一标识符 chunk = request.POST.get('chunk', 0) # 获取该分片在所有分片中的序号 filename = '%s%s' % (task, chunk) # 构造该分片的唯一标识符 upload_file = request.FILES['file'] with open('upload/%s' % filename, 'wb') as f: f.write(upload_file.read()) print("upload ...") return HttpResponse('ok') @csrf_exempt def upload_complete(request): target_filename = request.GET.get('filename') # 获取上传文件的文件名 task = request.GET.get('task_id') # 获取文件的唯一标识符 user = request.GET.get('user') chunk = 0 # 分片序号 print(target_filename, task) with open('upload/%s' % target_filename, 'wb') as target_file: # 创建新文件 while True: try: filename = 'upload/%s%d' % (task, chunk) source_file = open(filename, 'rb') # 按序打开每个分片 target_file.write(source_file.read()) # 读取分片内容写入新文件 source_file.close() except IOError as msg: break chunk += 1 os.remove(filename) # 删除该分片,节约空间 time.sleep(1) # 如果用户的文件夹不存在 user_path = os.path.join('upload', user) user_upload_path = os.path.join('upload', user, 'uploaded') user_encode_path = os.path.join('upload', user, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) if os.path.exists(user_upload_path): try: if os.path.exists(os.path.join(user_upload_path, target_filename)): os.remove(os.path.join(user_upload_path, target_filename)) shutil.move(os.path.join('upload', target_filename), user_upload_path) except Exception as e: print('move_file ERROR: ', e) traceback.print_exc() time.sleep(1) return render(request, "blog/index.html", {'target_filename': target_filename, 'user_upload_path': user_upload_path}) def save_agent(f, username): # whether the existing is in tesing or validation path_valid = "validateAgent/" + username + "/" path_test = "testingAgent/" + username + "/" if os.path.exists(path_valid) or os.path.exists(path_test): return False path = 'uploadAgent/' + username + '.zip' print("start.....") with open(path, 'wb+') as destination: print("open safely") for chunk in f.chunks(): destination.write(chunk) print("submit finished!") return True
turn HttpResponse('<h1>Clips Download Started</h1>') def macie(response):
identifier_body
views.py
from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt import os import time from .clipmanager import ClipManager from .models import Channel, Clip, HighlightClip import shutil import traceback from datetime import datetime from clipmanager.tasks import generate_thumbnail_task, encode_video, download_clip # Create your views here. def videoplayer(request): url = request.GET.get('videourl') return render(request, 'blog/videoplayer.html', {'videourl': url}) def dashboard(response): return render(response, 'blog/base_admin.html') def index(response): if response.user is not []: user = response.user return render(response, "blog/index.html", {'user': user}) def login(response): return render(response, "blog/login.html") def submit_success(response): return render(response, 'blog/submit-success.html') def submit(response): if response.user is not []: user = response.user # print(response.POST) # <QueryDict: {'csrfmiddlewaretoken': ['TyTMLhsydMTLShkiiE2VCBLQPRjME9bbEtdDN2qK87Q7NBSETC137bbpYHXUDGae'], 'start': ['0'], 'duration': ['5'], 'author': ['jack'], 'title': ['研发日志-人工智能编程'], 'target-filename': ['长身之处.mp4'], 'customRadioInline1': ['on']}> if response.POST.get('target-filename') is not None: target_filename = response.POST.get('target-filename') author = response.POST.get('author') title = response.POST.get('title') start = response.POST.get('start') duration = response.POST.get('duration') if response.POST.get('customRadioInline1') == 'on': cliptype = 0 else: cliptype = 1 clip_used = False user_path = os.path.join('upload', user.username) user_upload_path = os.path.join('upload', user.username, 'uploaded') user_encode_path = os.path.join('upload', user.username, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) path = os.path.join(user_upload_path, target_filename) upload_complete = True encode_complete = False file_deleted = False highlight = HighlightClip() highlight.user = user highlight.author = author highlight.title = title highlight.filename = target_filename highlight.path = path highlight.start_time = start highlight.duration = duration highlight.upload_complete = upload_complete highlight.encode_complete = encode_complete highlight.file_deleted = file_deleted highlight.clip_type = cliptype highlight.clip_used = clip_used highlight.save() return HttpResponseRedirect('submit_success', {'user': user}) return render(response, "blog/submit.html", {'user': user}) def home(request): return HttpResponse('<h1>Home</h1>') def about(request): return HttpResponse('<h1>About</h1>') def ajax_macie(request): pri
.GET) if request.GET.get('action') is not None: action = request.GET.get('action') if request.GET.get('slug') is not None: slug = request.GET.get('slug') if action == 'accept': clip = Clip.objects.filter(slug=slug)[0] print('accepting ', clip.title) clip.reviewed = True clip.accepted = True clip.save() elif action == 'reject': clip = Clip.objects.filter(slug=slug)[0] print('rejecting ', clip.title) clip.reviewed = True clip.rejected = True clip.save() return HttpResponse('ok') # return render(request, 'ajax2.html') def download_videos(request): download_clip.delay() return HttpResponse('<h1>Clips Download Started</h1>') def macie(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), rejected=False, accepted=False,channel=channel).order_by('-views') return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': True}) def update_clip_views(response): clips = [] clips = Clip.objects.filter(reviewed=False, downloaded=False) clipmanager = ClipManager() count = 1 total = len(clips) print("UPDATING VIEWS") for item in clips: item = clipmanager.update_clip_view(item) print("[UPDATE CLIP VIEWS]", count, ' of ', total) if item.views > 5: item.rejected = False item.accepted = False print("[New Commer!]") if item.views < 6: item.rejected = True print(count, ' has been REJECTED') item.save() count += 1 return HttpResponse('<h1>所有直播的播放数已更新</h1>') def macie_reviewed(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') channel = Channel.objects.filter(name='maciejay')[0] clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=False,channel=channel) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def macie_downloaded(response): clips = [] if response.POST.get('start') is not None: start = response.POST.get('start') start = str(start) + "-00:00:00" start_datetime = datetime.strptime(start, '%m/%d/%Y-%H:%M:%S') if response.POST.get('end') is not None: end = response.POST.get('end') end = str(end) + "-23:59:59" end_datetime = datetime.strptime(end, '%m/%d/%Y-%H:%M:%S') clips = Clip.objects.filter(created_at__range=(start_datetime, end_datetime), reviewed=True, rejected=False, accepted=True, downloaded=True) return render(response, 'blog/projects-grid-cards.html', {'clips': clips, 'review': False}) def update_macie_twitch(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='week', limit=100, cursor="") return HttpResponse('<h1>每周的直播片段已更新</h1>') def update_macie_daily(request): clipmanager = ClipManager() clips = clipmanager.retrieve_clips_data(channel='maciejay', period='day', limit=30, cursor="") return HttpResponse('<h1>MacieJay\'s 每天的直播片段已更新</h1>') def update_macie_all(request): clipmanager = ClipManager() try: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") except Exception as e: print(e) finally: clips = clipmanager.retrive_clips_by_count(channel='maciejay', period='all', limit=100, total_count=3000, cursor="") return HttpResponse('<h1>MacieJay\'s 排名靠前5000的直播片段已更新</h1>') def update_download_url(request): clips = Clip.objects.all() clipmanager = ClipManager() for item in clips: clipmanager.update_clip_url(item) return HttpResponse('<h1>所有直播片段的下载地址已更新</h1>') def upload(request): return render(request, 'blog/upload.html') @csrf_exempt def upload_index(request): if request.method == 'POST': task = request.POST.get('task_id') # 获取文件的唯一标识符 chunk = request.POST.get('chunk', 0) # 获取该分片在所有分片中的序号 filename = '%s%s' % (task, chunk) # 构造该分片的唯一标识符 upload_file = request.FILES['file'] with open('upload/%s' % filename, 'wb') as f: f.write(upload_file.read()) print("upload ...") return HttpResponse('ok') @csrf_exempt def upload_complete(request): target_filename = request.GET.get('filename') # 获取上传文件的文件名 task = request.GET.get('task_id') # 获取文件的唯一标识符 user = request.GET.get('user') chunk = 0 # 分片序号 print(target_filename, task) with open('upload/%s' % target_filename, 'wb') as target_file: # 创建新文件 while True: try: filename = 'upload/%s%d' % (task, chunk) source_file = open(filename, 'rb') # 按序打开每个分片 target_file.write(source_file.read()) # 读取分片内容写入新文件 source_file.close() except IOError as msg: break chunk += 1 os.remove(filename) # 删除该分片,节约空间 time.sleep(1) # 如果用户的文件夹不存在 user_path = os.path.join('upload', user) user_upload_path = os.path.join('upload', user, 'uploaded') user_encode_path = os.path.join('upload', user, 'encoded') if os.path.exists(user_path) == False: os.mkdir(user_path) if os.path.exists(user_upload_path) == False: os.mkdir(user_upload_path) if os.path.exists(user_encode_path) == False: os.mkdir(user_encode_path) if os.path.exists(user_upload_path): try: if os.path.exists(os.path.join(user_upload_path, target_filename)): os.remove(os.path.join(user_upload_path, target_filename)) shutil.move(os.path.join('upload', target_filename), user_upload_path) except Exception as e: print('move_file ERROR: ', e) traceback.print_exc() time.sleep(1) return render(request, "blog/index.html", {'target_filename': target_filename, 'user_upload_path': user_upload_path}) def save_agent(f, username): # whether the existing is in tesing or validation path_valid = "validateAgent/" + username + "/" path_test = "testingAgent/" + username + "/" if os.path.exists(path_valid) or os.path.exists(path_test): return False path = 'uploadAgent/' + username + '.zip' print("start.....") with open(path, 'wb+') as destination: print("open safely") for chunk in f.chunks(): destination.write(chunk) print("submit finished!") return True
nt(request
identifier_name
main5.py
import tkinter from collections import namedtuple from tkinter import * from tkinter.constants import * from tkinter import ttk from tkinter.ttk import * import requests import time import datetime import gui import concurrent.futures import logging import json import asyncio logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler = logging.FileHandler("RemoteKu_mainLog.log") file_handler.setLevel(logging.ERROR) file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(stream_handler) def logger_func(orig_func): import logging formatter2 = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler2 = logging.FileHandler("RemoteKu.log") file_handler2.setFormatter(formatter2) logger.addHandler(file_handler2) def wrapper(*args, **kwargs): logger.debug("DEBUG log for Func {} with args:{} and kwargs:{}".format(orig_func, args, kwargs)) return orig_func(*args, **kwargs) return wrapper ### This is basics such as variables and holders for devices global cur_hdmi stats_counter = 30 counter = 0 running = False timing = 0 result = "NULL" msg_box_text = "" api_port = ":8060" cur_hdmi = 1 devices_listing = [] root = tkinter.Tk() root.wm_iconbitmap(default='wicon.ico') #root.tk_setPalette(background='purple', activeBackground='white', foreground='green') def toplevel_loading(devices_listing): t = 'loading...' toplevel2 = tkinter.Toplevel(root) toplevel2.title('Loading Devices...') label1 = ttk.LabelFrame(toplevel2) label1_1 = ttk.Label(label1, text=t) label1.place() label1_1.place() with open('devices.json', mode='r') as f: dev_in = json.load(f) for dev in dev_in.get('devices').items(): devices_listing.append(dev) dev_states = generate_devs(devices_listing) toplevel2.destroy() return dev_states def generate_devs(dev_in): dev_states = [] for dev in dev_in: dev_url = 'http://{}'.format(dev[1].get('ip_address')) result = pwr_status(dev_url) dev_status = (result) dev_states.append(dev_status) return vals(dev_states) dev_list = { "dadL": "http://192.168.0.111", "dadR": "http://192.168.0.203", "lrTV": "http://192.168.1.155", "sisTV": "http://192.168.1.199", "parkTV": "http://192.168.1.198" } input_list = ['InputTuner', 'InputHDMI1','InputHDMI2', 'InputHDMI3', 'InputHDMI4'] dev_grps = { "dadBOTH": [dev_list.get("dadL"), dev_list.get("dadR")] } api_calls = { "device_info": "/query/device-info", "get_apps": "/query/apps", "power_cycle": "/keypress/power", "active_app": "/query/active-app", "vol_up": "/keypress/volumeup", "vol_down": "/keypress/volumedown", "vol_mute": "/keypress/volumemute", "select": "/keypress/select", "home": "/keypress/home", "up": "/keypress/up", "down": "/keypress/down", "right": "/keypress/right", "left": "/keypress/left", "info": "/keypress/info", "input": "/keypress/inputhdmi{}".format(cur_hdmi) } def inputs(input_list): inp_vals = [] for value in input_list.values(): inp_vals.append(value) return inp_vals def dev_check(dev_list): dev_states = [] dev_states = dev_status() return vals(dev_states) def vals(dev_states): val_list = [] for value in dev_states: if value[2] != 'red': val_list.append(value[0]) return val_list @logger_func def api_post(dev, api_call): """ Function for api POST calls """ import xmltodict import pdb try: r = requests.post(dev + ':8060' + api_call, timeout=10) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE {} RETURNED: {} ".format(n.get(), str(r))) r2 = r.text response = f'{r_code} - OK' return msg_box(response) @logger_func def api_req(dev, api_call): """ Function for api GET calls """ import xmltodict import logging try: r = requests.get(dev + ':8060' + api_call, timeout=5) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE RETURNED: {} ".format(str(r))) r2 = r.text response = xmltodict.parse(r2, xml_attribs=False) return response else: response = "UnknownERR" dev.state(DISABLED) return msg_box(response) def
(dev): api_call = api_calls.get("active_app") response = api_req(dev, "get", api_call) act_app = response.get("active-app").get("app") return act_app def dev_status(): dev_states = [] for key,value in dev_list.items(): dev_url = value result = pwr_status(value) dev_status = (result) dev_states.append(dev_status) return dev_states def dev_status_exec(): dev_states = [] for key,value in dev_list.items(): dev_url = value with concurrent.futures.ProcessPoolExecutor() as executor: rslts = executor.map(pwr_status, dev_url) for r in rslts: print(r) dev_status = r dev_states.append(dev_status) return dev_states def pwr_status(dev): api_call = "/query/device-info" try: response = api_req(dev, api_call) except TimeoutError as to_err: response = "Timeout Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color if response == 'ERR': response = "Timeout2 Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color dev_info = response.get("device-info") pwr_state = dev_info.get("power-mode") if pwr_state == "Ready": pwr_status = "Sleep" pwr_color = "orange" return dev, pwr_status, pwr_color elif pwr_state == "PowerOn": pwr_status = "On" pwr_color = "green" return dev, pwr_status, pwr_color else: pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color @logger_func def input_hdmi_cycle(dev, cur_hdmi): import itertools hdmi_range = [1, 2, 3, 4] num = itertools.cycle(hdmi_range) cur_hdmi = num.__next__() response = api_post(dev, api_calls.get("input"), cur_hdmi) return response def select_dev(eventObject): device = eventObject.get() label1["text"] = "OK" return device ## Toplevel window for sending api calls apiPath_var = StringVar() apiMethod_var = StringVar() apiCall_var = StringVar() @logger_func def toplevel_apiCall(): toplevel1 = Toplevel(root) toplevel1.title('RemoteKu-Send API Call') toplevel_label = Label(toplevel1, text="This window allows user to send API calls ").pack() ## "to the current device. Provide only the path below, the URL " \ ## "and port auto-populate and the click the button to choose the " \ ## "method for the call (GET or POST). ex. http://2.2.3.2:8060/query/device-info") path_label = Label(toplevel1, text="API Path:").pack() path_entry = Entry(toplevel1, textvariable=apiPath_var).pack() get_btn = Button(toplevel1, text="GET", command=lambda:build_apiCall("GET", apiPath_var)).pack() post_btn = Button(toplevel1, text="POST", command=lambda:build_apiCall("POST", apiPath_var)).pack() close_btn = Button(toplevel1, text="Close", command=toplevel1.destroy).pack() ## return build_apiCall(apiPath_var) ##command=lambda:api_post(n.get(),api_calls.get("vol_mute")) @logger_func def build_apiCall(apiMethod, apiPath_var): dev = n.get() path = apiPath_var.get() if apiMethod == "POST": response = api_post(dev, path)#requests.post(dev + ":8060" + path) print(response) return msg_box(response) elif apiMethod == "GET": response = api_req(dev, path) print(response) return msg_box(response) else: return msg_box("ERROR") #### end toplevel 2##def toplevel_input(): ## ii = tkinter.StringVar() ## toplevel1 = tkinter.Toplevel(root) ## toplevel1.title('RemoteKu-Input Selector') ## input_combobox = ttk.Combobox(toplevel1, textvariable=ii) ## input_combobox['values'] = inputs(input_list) ## input_combobox.grid() ## toplevel1.bind('<<ComboboxSelected>>', select_input) ## return toplevel1 ## ##def select_input(eventObject): #### ii = eventObject.get() ## toplevel1.destroy() ## return ii def donothing(): pass def menu_close(): root.destroy() ############## Below is GUI definitions ##root = Tk() root.title("RemoteKu C5dev--..") root.minsize(width=100, height=70) font1 = ttk.Separator menubar = Menu(root) filemenu = Menu(menubar, tearoff = 0) filemenu.add_command(label="New", command = donothing) ##filemenu.add_command(label = "Open", command = open_file) filemenu.add_separator() filemenu.add_command(label = "Close", command = menu_close) menubar.add_cascade(label = "File", menu = filemenu) style1 = ttk.Style() style1.map("C.TButton", foreground=[('pressed', 'red'), ('active', 'blue')], background=[('pressed', '!disabled', 'black'), ('active', 'purple')] ) top = ttk.Frame(root) top.grid(columnspan=2, rowspan=2) label1 = ttk.Label(top, text='Current Device').grid(column=0, row=1, pady=2) n = tkinter.StringVar() current_dev = ttk.Combobox(top, textvariable=n) current_dev['values'] = toplevel_loading(devices_listing)#generate_devs(devices_listing) current_dev.current() current_dev.grid() top.bind('<<ComboboxSelected>>', select_dev) device = n.get() sep1 = ttk.Separator(root, orient='horizontal').grid(row=2) index = ttk.Frame(root).grid(columnspan=3, padx=0, pady=0) ##########Current Tab Config Btns etc btn1Img = PhotoImage(file='images/pwr.png') btn2Img = PhotoImage(file='images/nav_up.png') btn3Img = PhotoImage(file='images/api.png') btn4Img = PhotoImage(file='images/nav_left.png') btn5Img = PhotoImage(file='images/nav_ok.png') btn6Img = PhotoImage(file='images/nav_right.png') btn7Img = PhotoImage(file='images/mute.png') btn8Img = PhotoImage(file='images/nav_down.png') btn9Img = PhotoImage(file='images/vol_up.png') btn10Img = PhotoImage(file='images/home.png') btn11Img = PhotoImage(file='images/info.png') btn12Img = PhotoImage(file='images/vol_down.png') btn1 = ttk.Button(index, style='C.TButton', text="Pwr", image=btn1Img, command=lambda:api_post(n.get(),api_calls.get("power_cycle"))).grid(row=3, column=0) btn2 = ttk.Button(index, text=" ^ ", image=btn2Img, command=lambda:api_post(n.get(),api_calls.get("up"))).grid(row=3, column=1) btn3 = ttk.Button(index, text="API Call", image=btn3Img, command=toplevel_apiCall).grid(row=6, column=0) btn4 = ttk.Button(index, text=" < ", image=btn4Img, command=lambda:api_post(n.get(),api_calls.get("left"))).grid(row=4, column=0) btn5 = ttk.Button(index, text="Enter", image=btn5Img, command=lambda:api_post(n.get(),api_calls.get("select"))).grid(row=4, column=1) btn6 = ttk.Button(index, text=" > ", image=btn6Img, command=lambda:api_post(n.get(),api_calls.get("right"))).grid(row=4, column=2) btn7 = ttk.Button(index, text="Mute", image=btn7Img, command=lambda:api_post(n.get(),api_calls.get("vol_mute"))).grid(row=5, column=0) btn8 = ttk.Button(index, text="\/", image=btn8Img, command=lambda:api_post(n.get(),api_calls.get("down"))).grid(row=5, column=1) btn9 = ttk.Button(index, text="Vol Up", image=btn9Img, command=lambda:api_post(n.get(),api_calls.get("vol_up"))).grid(row=5, column=2) btn10 = ttk.Button(index, text="Home", image=btn10Img, command=lambda:api_post(n.get(),api_calls.get("home"))).grid(row=3, column=2) btn11= ttk.Button(index, text="Info", image=btn11Img, command=lambda:api_post(n.get(),api_calls.get("info"))).grid(row=6, column=1) btn12 = ttk.Button(index, text="Vol Dn", image=btn12Img, command=lambda:api_post(n.get(),api_calls.get("vol_down"))).grid(row=6, column=2) msg_frame1 = LabelFrame(root, text = "Message Box",) msg_initial = "Welcome" label1 = Label(msg_frame1) label1['text'] = msg_initial label1.grid() msg_frame1.grid(sticky="s", columnspan=3) def msg_box(msg_label): counter = 3 label1['text'] = msg_label return label1 root.config(menu = menubar) if __name__ == '__main__': root.mainloop()
active_app
identifier_name
main5.py
import tkinter from collections import namedtuple from tkinter import * from tkinter.constants import * from tkinter import ttk from tkinter.ttk import * import requests import time import datetime import gui import concurrent.futures import logging import json import asyncio logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
stream_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(stream_handler) def logger_func(orig_func): import logging formatter2 = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler2 = logging.FileHandler("RemoteKu.log") file_handler2.setFormatter(formatter2) logger.addHandler(file_handler2) def wrapper(*args, **kwargs): logger.debug("DEBUG log for Func {} with args:{} and kwargs:{}".format(orig_func, args, kwargs)) return orig_func(*args, **kwargs) return wrapper ### This is basics such as variables and holders for devices global cur_hdmi stats_counter = 30 counter = 0 running = False timing = 0 result = "NULL" msg_box_text = "" api_port = ":8060" cur_hdmi = 1 devices_listing = [] root = tkinter.Tk() root.wm_iconbitmap(default='wicon.ico') #root.tk_setPalette(background='purple', activeBackground='white', foreground='green') def toplevel_loading(devices_listing): t = 'loading...' toplevel2 = tkinter.Toplevel(root) toplevel2.title('Loading Devices...') label1 = ttk.LabelFrame(toplevel2) label1_1 = ttk.Label(label1, text=t) label1.place() label1_1.place() with open('devices.json', mode='r') as f: dev_in = json.load(f) for dev in dev_in.get('devices').items(): devices_listing.append(dev) dev_states = generate_devs(devices_listing) toplevel2.destroy() return dev_states def generate_devs(dev_in): dev_states = [] for dev in dev_in: dev_url = 'http://{}'.format(dev[1].get('ip_address')) result = pwr_status(dev_url) dev_status = (result) dev_states.append(dev_status) return vals(dev_states) dev_list = { "dadL": "http://192.168.0.111", "dadR": "http://192.168.0.203", "lrTV": "http://192.168.1.155", "sisTV": "http://192.168.1.199", "parkTV": "http://192.168.1.198" } input_list = ['InputTuner', 'InputHDMI1','InputHDMI2', 'InputHDMI3', 'InputHDMI4'] dev_grps = { "dadBOTH": [dev_list.get("dadL"), dev_list.get("dadR")] } api_calls = { "device_info": "/query/device-info", "get_apps": "/query/apps", "power_cycle": "/keypress/power", "active_app": "/query/active-app", "vol_up": "/keypress/volumeup", "vol_down": "/keypress/volumedown", "vol_mute": "/keypress/volumemute", "select": "/keypress/select", "home": "/keypress/home", "up": "/keypress/up", "down": "/keypress/down", "right": "/keypress/right", "left": "/keypress/left", "info": "/keypress/info", "input": "/keypress/inputhdmi{}".format(cur_hdmi) } def inputs(input_list): inp_vals = [] for value in input_list.values(): inp_vals.append(value) return inp_vals def dev_check(dev_list): dev_states = [] dev_states = dev_status() return vals(dev_states) def vals(dev_states): val_list = [] for value in dev_states: if value[2] != 'red': val_list.append(value[0]) return val_list @logger_func def api_post(dev, api_call): """ Function for api POST calls """ import xmltodict import pdb try: r = requests.post(dev + ':8060' + api_call, timeout=10) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE {} RETURNED: {} ".format(n.get(), str(r))) r2 = r.text response = f'{r_code} - OK' return msg_box(response) @logger_func def api_req(dev, api_call): """ Function for api GET calls """ import xmltodict import logging try: r = requests.get(dev + ':8060' + api_call, timeout=5) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE RETURNED: {} ".format(str(r))) r2 = r.text response = xmltodict.parse(r2, xml_attribs=False) return response else: response = "UnknownERR" dev.state(DISABLED) return msg_box(response) def active_app(dev): api_call = api_calls.get("active_app") response = api_req(dev, "get", api_call) act_app = response.get("active-app").get("app") return act_app def dev_status(): dev_states = [] for key,value in dev_list.items(): dev_url = value result = pwr_status(value) dev_status = (result) dev_states.append(dev_status) return dev_states def dev_status_exec(): dev_states = [] for key,value in dev_list.items(): dev_url = value with concurrent.futures.ProcessPoolExecutor() as executor: rslts = executor.map(pwr_status, dev_url) for r in rslts: print(r) dev_status = r dev_states.append(dev_status) return dev_states def pwr_status(dev): api_call = "/query/device-info" try: response = api_req(dev, api_call) except TimeoutError as to_err: response = "Timeout Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color if response == 'ERR': response = "Timeout2 Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color dev_info = response.get("device-info") pwr_state = dev_info.get("power-mode") if pwr_state == "Ready": pwr_status = "Sleep" pwr_color = "orange" return dev, pwr_status, pwr_color elif pwr_state == "PowerOn": pwr_status = "On" pwr_color = "green" return dev, pwr_status, pwr_color else: pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color @logger_func def input_hdmi_cycle(dev, cur_hdmi): import itertools hdmi_range = [1, 2, 3, 4] num = itertools.cycle(hdmi_range) cur_hdmi = num.__next__() response = api_post(dev, api_calls.get("input"), cur_hdmi) return response def select_dev(eventObject): device = eventObject.get() label1["text"] = "OK" return device ## Toplevel window for sending api calls apiPath_var = StringVar() apiMethod_var = StringVar() apiCall_var = StringVar() @logger_func def toplevel_apiCall(): toplevel1 = Toplevel(root) toplevel1.title('RemoteKu-Send API Call') toplevel_label = Label(toplevel1, text="This window allows user to send API calls ").pack() ## "to the current device. Provide only the path below, the URL " \ ## "and port auto-populate and the click the button to choose the " \ ## "method for the call (GET or POST). ex. http://2.2.3.2:8060/query/device-info") path_label = Label(toplevel1, text="API Path:").pack() path_entry = Entry(toplevel1, textvariable=apiPath_var).pack() get_btn = Button(toplevel1, text="GET", command=lambda:build_apiCall("GET", apiPath_var)).pack() post_btn = Button(toplevel1, text="POST", command=lambda:build_apiCall("POST", apiPath_var)).pack() close_btn = Button(toplevel1, text="Close", command=toplevel1.destroy).pack() ## return build_apiCall(apiPath_var) ##command=lambda:api_post(n.get(),api_calls.get("vol_mute")) @logger_func def build_apiCall(apiMethod, apiPath_var): dev = n.get() path = apiPath_var.get() if apiMethod == "POST": response = api_post(dev, path)#requests.post(dev + ":8060" + path) print(response) return msg_box(response) elif apiMethod == "GET": response = api_req(dev, path) print(response) return msg_box(response) else: return msg_box("ERROR") #### end toplevel 2##def toplevel_input(): ## ii = tkinter.StringVar() ## toplevel1 = tkinter.Toplevel(root) ## toplevel1.title('RemoteKu-Input Selector') ## input_combobox = ttk.Combobox(toplevel1, textvariable=ii) ## input_combobox['values'] = inputs(input_list) ## input_combobox.grid() ## toplevel1.bind('<<ComboboxSelected>>', select_input) ## return toplevel1 ## ##def select_input(eventObject): #### ii = eventObject.get() ## toplevel1.destroy() ## return ii def donothing(): pass def menu_close(): root.destroy() ############## Below is GUI definitions ##root = Tk() root.title("RemoteKu C5dev--..") root.minsize(width=100, height=70) font1 = ttk.Separator menubar = Menu(root) filemenu = Menu(menubar, tearoff = 0) filemenu.add_command(label="New", command = donothing) ##filemenu.add_command(label = "Open", command = open_file) filemenu.add_separator() filemenu.add_command(label = "Close", command = menu_close) menubar.add_cascade(label = "File", menu = filemenu) style1 = ttk.Style() style1.map("C.TButton", foreground=[('pressed', 'red'), ('active', 'blue')], background=[('pressed', '!disabled', 'black'), ('active', 'purple')] ) top = ttk.Frame(root) top.grid(columnspan=2, rowspan=2) label1 = ttk.Label(top, text='Current Device').grid(column=0, row=1, pady=2) n = tkinter.StringVar() current_dev = ttk.Combobox(top, textvariable=n) current_dev['values'] = toplevel_loading(devices_listing)#generate_devs(devices_listing) current_dev.current() current_dev.grid() top.bind('<<ComboboxSelected>>', select_dev) device = n.get() sep1 = ttk.Separator(root, orient='horizontal').grid(row=2) index = ttk.Frame(root).grid(columnspan=3, padx=0, pady=0) ##########Current Tab Config Btns etc btn1Img = PhotoImage(file='images/pwr.png') btn2Img = PhotoImage(file='images/nav_up.png') btn3Img = PhotoImage(file='images/api.png') btn4Img = PhotoImage(file='images/nav_left.png') btn5Img = PhotoImage(file='images/nav_ok.png') btn6Img = PhotoImage(file='images/nav_right.png') btn7Img = PhotoImage(file='images/mute.png') btn8Img = PhotoImage(file='images/nav_down.png') btn9Img = PhotoImage(file='images/vol_up.png') btn10Img = PhotoImage(file='images/home.png') btn11Img = PhotoImage(file='images/info.png') btn12Img = PhotoImage(file='images/vol_down.png') btn1 = ttk.Button(index, style='C.TButton', text="Pwr", image=btn1Img, command=lambda:api_post(n.get(),api_calls.get("power_cycle"))).grid(row=3, column=0) btn2 = ttk.Button(index, text=" ^ ", image=btn2Img, command=lambda:api_post(n.get(),api_calls.get("up"))).grid(row=3, column=1) btn3 = ttk.Button(index, text="API Call", image=btn3Img, command=toplevel_apiCall).grid(row=6, column=0) btn4 = ttk.Button(index, text=" < ", image=btn4Img, command=lambda:api_post(n.get(),api_calls.get("left"))).grid(row=4, column=0) btn5 = ttk.Button(index, text="Enter", image=btn5Img, command=lambda:api_post(n.get(),api_calls.get("select"))).grid(row=4, column=1) btn6 = ttk.Button(index, text=" > ", image=btn6Img, command=lambda:api_post(n.get(),api_calls.get("right"))).grid(row=4, column=2) btn7 = ttk.Button(index, text="Mute", image=btn7Img, command=lambda:api_post(n.get(),api_calls.get("vol_mute"))).grid(row=5, column=0) btn8 = ttk.Button(index, text="\/", image=btn8Img, command=lambda:api_post(n.get(),api_calls.get("down"))).grid(row=5, column=1) btn9 = ttk.Button(index, text="Vol Up", image=btn9Img, command=lambda:api_post(n.get(),api_calls.get("vol_up"))).grid(row=5, column=2) btn10 = ttk.Button(index, text="Home", image=btn10Img, command=lambda:api_post(n.get(),api_calls.get("home"))).grid(row=3, column=2) btn11= ttk.Button(index, text="Info", image=btn11Img, command=lambda:api_post(n.get(),api_calls.get("info"))).grid(row=6, column=1) btn12 = ttk.Button(index, text="Vol Dn", image=btn12Img, command=lambda:api_post(n.get(),api_calls.get("vol_down"))).grid(row=6, column=2) msg_frame1 = LabelFrame(root, text = "Message Box",) msg_initial = "Welcome" label1 = Label(msg_frame1) label1['text'] = msg_initial label1.grid() msg_frame1.grid(sticky="s", columnspan=3) def msg_box(msg_label): counter = 3 label1['text'] = msg_label return label1 root.config(menu = menubar) if __name__ == '__main__': root.mainloop()
file_handler = logging.FileHandler("RemoteKu_mainLog.log") file_handler.setLevel(logging.ERROR) file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler()
random_line_split
main5.py
import tkinter from collections import namedtuple from tkinter import * from tkinter.constants import * from tkinter import ttk from tkinter.ttk import * import requests import time import datetime import gui import concurrent.futures import logging import json import asyncio logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler = logging.FileHandler("RemoteKu_mainLog.log") file_handler.setLevel(logging.ERROR) file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(stream_handler) def logger_func(orig_func):
### This is basics such as variables and holders for devices global cur_hdmi stats_counter = 30 counter = 0 running = False timing = 0 result = "NULL" msg_box_text = "" api_port = ":8060" cur_hdmi = 1 devices_listing = [] root = tkinter.Tk() root.wm_iconbitmap(default='wicon.ico') #root.tk_setPalette(background='purple', activeBackground='white', foreground='green') def toplevel_loading(devices_listing): t = 'loading...' toplevel2 = tkinter.Toplevel(root) toplevel2.title('Loading Devices...') label1 = ttk.LabelFrame(toplevel2) label1_1 = ttk.Label(label1, text=t) label1.place() label1_1.place() with open('devices.json', mode='r') as f: dev_in = json.load(f) for dev in dev_in.get('devices').items(): devices_listing.append(dev) dev_states = generate_devs(devices_listing) toplevel2.destroy() return dev_states def generate_devs(dev_in): dev_states = [] for dev in dev_in: dev_url = 'http://{}'.format(dev[1].get('ip_address')) result = pwr_status(dev_url) dev_status = (result) dev_states.append(dev_status) return vals(dev_states) dev_list = { "dadL": "http://192.168.0.111", "dadR": "http://192.168.0.203", "lrTV": "http://192.168.1.155", "sisTV": "http://192.168.1.199", "parkTV": "http://192.168.1.198" } input_list = ['InputTuner', 'InputHDMI1','InputHDMI2', 'InputHDMI3', 'InputHDMI4'] dev_grps = { "dadBOTH": [dev_list.get("dadL"), dev_list.get("dadR")] } api_calls = { "device_info": "/query/device-info", "get_apps": "/query/apps", "power_cycle": "/keypress/power", "active_app": "/query/active-app", "vol_up": "/keypress/volumeup", "vol_down": "/keypress/volumedown", "vol_mute": "/keypress/volumemute", "select": "/keypress/select", "home": "/keypress/home", "up": "/keypress/up", "down": "/keypress/down", "right": "/keypress/right", "left": "/keypress/left", "info": "/keypress/info", "input": "/keypress/inputhdmi{}".format(cur_hdmi) } def inputs(input_list): inp_vals = [] for value in input_list.values(): inp_vals.append(value) return inp_vals def dev_check(dev_list): dev_states = [] dev_states = dev_status() return vals(dev_states) def vals(dev_states): val_list = [] for value in dev_states: if value[2] != 'red': val_list.append(value[0]) return val_list @logger_func def api_post(dev, api_call): """ Function for api POST calls """ import xmltodict import pdb try: r = requests.post(dev + ':8060' + api_call, timeout=10) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE {} RETURNED: {} ".format(n.get(), str(r))) r2 = r.text response = f'{r_code} - OK' return msg_box(response) @logger_func def api_req(dev, api_call): """ Function for api GET calls """ import xmltodict import logging try: r = requests.get(dev + ':8060' + api_call, timeout=5) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE RETURNED: {} ".format(str(r))) r2 = r.text response = xmltodict.parse(r2, xml_attribs=False) return response else: response = "UnknownERR" dev.state(DISABLED) return msg_box(response) def active_app(dev): api_call = api_calls.get("active_app") response = api_req(dev, "get", api_call) act_app = response.get("active-app").get("app") return act_app def dev_status(): dev_states = [] for key,value in dev_list.items(): dev_url = value result = pwr_status(value) dev_status = (result) dev_states.append(dev_status) return dev_states def dev_status_exec(): dev_states = [] for key,value in dev_list.items(): dev_url = value with concurrent.futures.ProcessPoolExecutor() as executor: rslts = executor.map(pwr_status, dev_url) for r in rslts: print(r) dev_status = r dev_states.append(dev_status) return dev_states def pwr_status(dev): api_call = "/query/device-info" try: response = api_req(dev, api_call) except TimeoutError as to_err: response = "Timeout Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color if response == 'ERR': response = "Timeout2 Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color dev_info = response.get("device-info") pwr_state = dev_info.get("power-mode") if pwr_state == "Ready": pwr_status = "Sleep" pwr_color = "orange" return dev, pwr_status, pwr_color elif pwr_state == "PowerOn": pwr_status = "On" pwr_color = "green" return dev, pwr_status, pwr_color else: pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color @logger_func def input_hdmi_cycle(dev, cur_hdmi): import itertools hdmi_range = [1, 2, 3, 4] num = itertools.cycle(hdmi_range) cur_hdmi = num.__next__() response = api_post(dev, api_calls.get("input"), cur_hdmi) return response def select_dev(eventObject): device = eventObject.get() label1["text"] = "OK" return device ## Toplevel window for sending api calls apiPath_var = StringVar() apiMethod_var = StringVar() apiCall_var = StringVar() @logger_func def toplevel_apiCall(): toplevel1 = Toplevel(root) toplevel1.title('RemoteKu-Send API Call') toplevel_label = Label(toplevel1, text="This window allows user to send API calls ").pack() ## "to the current device. Provide only the path below, the URL " \ ## "and port auto-populate and the click the button to choose the " \ ## "method for the call (GET or POST). ex. http://2.2.3.2:8060/query/device-info") path_label = Label(toplevel1, text="API Path:").pack() path_entry = Entry(toplevel1, textvariable=apiPath_var).pack() get_btn = Button(toplevel1, text="GET", command=lambda:build_apiCall("GET", apiPath_var)).pack() post_btn = Button(toplevel1, text="POST", command=lambda:build_apiCall("POST", apiPath_var)).pack() close_btn = Button(toplevel1, text="Close", command=toplevel1.destroy).pack() ## return build_apiCall(apiPath_var) ##command=lambda:api_post(n.get(),api_calls.get("vol_mute")) @logger_func def build_apiCall(apiMethod, apiPath_var): dev = n.get() path = apiPath_var.get() if apiMethod == "POST": response = api_post(dev, path)#requests.post(dev + ":8060" + path) print(response) return msg_box(response) elif apiMethod == "GET": response = api_req(dev, path) print(response) return msg_box(response) else: return msg_box("ERROR") #### end toplevel 2##def toplevel_input(): ## ii = tkinter.StringVar() ## toplevel1 = tkinter.Toplevel(root) ## toplevel1.title('RemoteKu-Input Selector') ## input_combobox = ttk.Combobox(toplevel1, textvariable=ii) ## input_combobox['values'] = inputs(input_list) ## input_combobox.grid() ## toplevel1.bind('<<ComboboxSelected>>', select_input) ## return toplevel1 ## ##def select_input(eventObject): #### ii = eventObject.get() ## toplevel1.destroy() ## return ii def donothing(): pass def menu_close(): root.destroy() ############## Below is GUI definitions ##root = Tk() root.title("RemoteKu C5dev--..") root.minsize(width=100, height=70) font1 = ttk.Separator menubar = Menu(root) filemenu = Menu(menubar, tearoff = 0) filemenu.add_command(label="New", command = donothing) ##filemenu.add_command(label = "Open", command = open_file) filemenu.add_separator() filemenu.add_command(label = "Close", command = menu_close) menubar.add_cascade(label = "File", menu = filemenu) style1 = ttk.Style() style1.map("C.TButton", foreground=[('pressed', 'red'), ('active', 'blue')], background=[('pressed', '!disabled', 'black'), ('active', 'purple')] ) top = ttk.Frame(root) top.grid(columnspan=2, rowspan=2) label1 = ttk.Label(top, text='Current Device').grid(column=0, row=1, pady=2) n = tkinter.StringVar() current_dev = ttk.Combobox(top, textvariable=n) current_dev['values'] = toplevel_loading(devices_listing)#generate_devs(devices_listing) current_dev.current() current_dev.grid() top.bind('<<ComboboxSelected>>', select_dev) device = n.get() sep1 = ttk.Separator(root, orient='horizontal').grid(row=2) index = ttk.Frame(root).grid(columnspan=3, padx=0, pady=0) ##########Current Tab Config Btns etc btn1Img = PhotoImage(file='images/pwr.png') btn2Img = PhotoImage(file='images/nav_up.png') btn3Img = PhotoImage(file='images/api.png') btn4Img = PhotoImage(file='images/nav_left.png') btn5Img = PhotoImage(file='images/nav_ok.png') btn6Img = PhotoImage(file='images/nav_right.png') btn7Img = PhotoImage(file='images/mute.png') btn8Img = PhotoImage(file='images/nav_down.png') btn9Img = PhotoImage(file='images/vol_up.png') btn10Img = PhotoImage(file='images/home.png') btn11Img = PhotoImage(file='images/info.png') btn12Img = PhotoImage(file='images/vol_down.png') btn1 = ttk.Button(index, style='C.TButton', text="Pwr", image=btn1Img, command=lambda:api_post(n.get(),api_calls.get("power_cycle"))).grid(row=3, column=0) btn2 = ttk.Button(index, text=" ^ ", image=btn2Img, command=lambda:api_post(n.get(),api_calls.get("up"))).grid(row=3, column=1) btn3 = ttk.Button(index, text="API Call", image=btn3Img, command=toplevel_apiCall).grid(row=6, column=0) btn4 = ttk.Button(index, text=" < ", image=btn4Img, command=lambda:api_post(n.get(),api_calls.get("left"))).grid(row=4, column=0) btn5 = ttk.Button(index, text="Enter", image=btn5Img, command=lambda:api_post(n.get(),api_calls.get("select"))).grid(row=4, column=1) btn6 = ttk.Button(index, text=" > ", image=btn6Img, command=lambda:api_post(n.get(),api_calls.get("right"))).grid(row=4, column=2) btn7 = ttk.Button(index, text="Mute", image=btn7Img, command=lambda:api_post(n.get(),api_calls.get("vol_mute"))).grid(row=5, column=0) btn8 = ttk.Button(index, text="\/", image=btn8Img, command=lambda:api_post(n.get(),api_calls.get("down"))).grid(row=5, column=1) btn9 = ttk.Button(index, text="Vol Up", image=btn9Img, command=lambda:api_post(n.get(),api_calls.get("vol_up"))).grid(row=5, column=2) btn10 = ttk.Button(index, text="Home", image=btn10Img, command=lambda:api_post(n.get(),api_calls.get("home"))).grid(row=3, column=2) btn11= ttk.Button(index, text="Info", image=btn11Img, command=lambda:api_post(n.get(),api_calls.get("info"))).grid(row=6, column=1) btn12 = ttk.Button(index, text="Vol Dn", image=btn12Img, command=lambda:api_post(n.get(),api_calls.get("vol_down"))).grid(row=6, column=2) msg_frame1 = LabelFrame(root, text = "Message Box",) msg_initial = "Welcome" label1 = Label(msg_frame1) label1['text'] = msg_initial label1.grid() msg_frame1.grid(sticky="s", columnspan=3) def msg_box(msg_label): counter = 3 label1['text'] = msg_label return label1 root.config(menu = menubar) if __name__ == '__main__': root.mainloop()
import logging formatter2 = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler2 = logging.FileHandler("RemoteKu.log") file_handler2.setFormatter(formatter2) logger.addHandler(file_handler2) def wrapper(*args, **kwargs): logger.debug("DEBUG log for Func {} with args:{} and kwargs:{}".format(orig_func, args, kwargs)) return orig_func(*args, **kwargs) return wrapper
identifier_body
main5.py
import tkinter from collections import namedtuple from tkinter import * from tkinter.constants import * from tkinter import ttk from tkinter.ttk import * import requests import time import datetime import gui import concurrent.futures import logging import json import asyncio logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler = logging.FileHandler("RemoteKu_mainLog.log") file_handler.setLevel(logging.ERROR) file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(stream_handler) def logger_func(orig_func): import logging formatter2 = logging.Formatter("%(asctime)s:%(name)s:%(message)s") file_handler2 = logging.FileHandler("RemoteKu.log") file_handler2.setFormatter(formatter2) logger.addHandler(file_handler2) def wrapper(*args, **kwargs): logger.debug("DEBUG log for Func {} with args:{} and kwargs:{}".format(orig_func, args, kwargs)) return orig_func(*args, **kwargs) return wrapper ### This is basics such as variables and holders for devices global cur_hdmi stats_counter = 30 counter = 0 running = False timing = 0 result = "NULL" msg_box_text = "" api_port = ":8060" cur_hdmi = 1 devices_listing = [] root = tkinter.Tk() root.wm_iconbitmap(default='wicon.ico') #root.tk_setPalette(background='purple', activeBackground='white', foreground='green') def toplevel_loading(devices_listing): t = 'loading...' toplevel2 = tkinter.Toplevel(root) toplevel2.title('Loading Devices...') label1 = ttk.LabelFrame(toplevel2) label1_1 = ttk.Label(label1, text=t) label1.place() label1_1.place() with open('devices.json', mode='r') as f: dev_in = json.load(f) for dev in dev_in.get('devices').items(): devices_listing.append(dev) dev_states = generate_devs(devices_listing) toplevel2.destroy() return dev_states def generate_devs(dev_in): dev_states = [] for dev in dev_in: dev_url = 'http://{}'.format(dev[1].get('ip_address')) result = pwr_status(dev_url) dev_status = (result) dev_states.append(dev_status) return vals(dev_states) dev_list = { "dadL": "http://192.168.0.111", "dadR": "http://192.168.0.203", "lrTV": "http://192.168.1.155", "sisTV": "http://192.168.1.199", "parkTV": "http://192.168.1.198" } input_list = ['InputTuner', 'InputHDMI1','InputHDMI2', 'InputHDMI3', 'InputHDMI4'] dev_grps = { "dadBOTH": [dev_list.get("dadL"), dev_list.get("dadR")] } api_calls = { "device_info": "/query/device-info", "get_apps": "/query/apps", "power_cycle": "/keypress/power", "active_app": "/query/active-app", "vol_up": "/keypress/volumeup", "vol_down": "/keypress/volumedown", "vol_mute": "/keypress/volumemute", "select": "/keypress/select", "home": "/keypress/home", "up": "/keypress/up", "down": "/keypress/down", "right": "/keypress/right", "left": "/keypress/left", "info": "/keypress/info", "input": "/keypress/inputhdmi{}".format(cur_hdmi) } def inputs(input_list): inp_vals = [] for value in input_list.values(): inp_vals.append(value) return inp_vals def dev_check(dev_list): dev_states = [] dev_states = dev_status() return vals(dev_states) def vals(dev_states): val_list = [] for value in dev_states: if value[2] != 'red': val_list.append(value[0]) return val_list @logger_func def api_post(dev, api_call): """ Function for api POST calls """ import xmltodict import pdb try: r = requests.post(dev + ':8060' + api_call, timeout=10) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200:
@logger_func def api_req(dev, api_call): """ Function for api GET calls """ import xmltodict import logging try: r = requests.get(dev + ':8060' + api_call, timeout=5) except Exception as exc: response = ["ERR", exc] return response[0] except ConnectionError as connerr: response = ["ERR", connerr] return response[0] except TimeoutError as toerr: response = ["ERR", toerr] return response[0], toerr r_code = r.status_code if r_code == 200: print("REQUEST WAS A SUCCESS. DEVICE RETURNED: {} ".format(str(r))) r2 = r.text response = xmltodict.parse(r2, xml_attribs=False) return response else: response = "UnknownERR" dev.state(DISABLED) return msg_box(response) def active_app(dev): api_call = api_calls.get("active_app") response = api_req(dev, "get", api_call) act_app = response.get("active-app").get("app") return act_app def dev_status(): dev_states = [] for key,value in dev_list.items(): dev_url = value result = pwr_status(value) dev_status = (result) dev_states.append(dev_status) return dev_states def dev_status_exec(): dev_states = [] for key,value in dev_list.items(): dev_url = value with concurrent.futures.ProcessPoolExecutor() as executor: rslts = executor.map(pwr_status, dev_url) for r in rslts: print(r) dev_status = r dev_states.append(dev_status) return dev_states def pwr_status(dev): api_call = "/query/device-info" try: response = api_req(dev, api_call) except TimeoutError as to_err: response = "Timeout Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color if response == 'ERR': response = "Timeout2 Error Occured on : {}".format(dev) pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color dev_info = response.get("device-info") pwr_state = dev_info.get("power-mode") if pwr_state == "Ready": pwr_status = "Sleep" pwr_color = "orange" return dev, pwr_status, pwr_color elif pwr_state == "PowerOn": pwr_status = "On" pwr_color = "green" return dev, pwr_status, pwr_color else: pwr_status = "Unknown" pwr_color = "red" return dev, pwr_status, pwr_color @logger_func def input_hdmi_cycle(dev, cur_hdmi): import itertools hdmi_range = [1, 2, 3, 4] num = itertools.cycle(hdmi_range) cur_hdmi = num.__next__() response = api_post(dev, api_calls.get("input"), cur_hdmi) return response def select_dev(eventObject): device = eventObject.get() label1["text"] = "OK" return device ## Toplevel window for sending api calls apiPath_var = StringVar() apiMethod_var = StringVar() apiCall_var = StringVar() @logger_func def toplevel_apiCall(): toplevel1 = Toplevel(root) toplevel1.title('RemoteKu-Send API Call') toplevel_label = Label(toplevel1, text="This window allows user to send API calls ").pack() ## "to the current device. Provide only the path below, the URL " \ ## "and port auto-populate and the click the button to choose the " \ ## "method for the call (GET or POST). ex. http://2.2.3.2:8060/query/device-info") path_label = Label(toplevel1, text="API Path:").pack() path_entry = Entry(toplevel1, textvariable=apiPath_var).pack() get_btn = Button(toplevel1, text="GET", command=lambda:build_apiCall("GET", apiPath_var)).pack() post_btn = Button(toplevel1, text="POST", command=lambda:build_apiCall("POST", apiPath_var)).pack() close_btn = Button(toplevel1, text="Close", command=toplevel1.destroy).pack() ## return build_apiCall(apiPath_var) ##command=lambda:api_post(n.get(),api_calls.get("vol_mute")) @logger_func def build_apiCall(apiMethod, apiPath_var): dev = n.get() path = apiPath_var.get() if apiMethod == "POST": response = api_post(dev, path)#requests.post(dev + ":8060" + path) print(response) return msg_box(response) elif apiMethod == "GET": response = api_req(dev, path) print(response) return msg_box(response) else: return msg_box("ERROR") #### end toplevel 2##def toplevel_input(): ## ii = tkinter.StringVar() ## toplevel1 = tkinter.Toplevel(root) ## toplevel1.title('RemoteKu-Input Selector') ## input_combobox = ttk.Combobox(toplevel1, textvariable=ii) ## input_combobox['values'] = inputs(input_list) ## input_combobox.grid() ## toplevel1.bind('<<ComboboxSelected>>', select_input) ## return toplevel1 ## ##def select_input(eventObject): #### ii = eventObject.get() ## toplevel1.destroy() ## return ii def donothing(): pass def menu_close(): root.destroy() ############## Below is GUI definitions ##root = Tk() root.title("RemoteKu C5dev--..") root.minsize(width=100, height=70) font1 = ttk.Separator menubar = Menu(root) filemenu = Menu(menubar, tearoff = 0) filemenu.add_command(label="New", command = donothing) ##filemenu.add_command(label = "Open", command = open_file) filemenu.add_separator() filemenu.add_command(label = "Close", command = menu_close) menubar.add_cascade(label = "File", menu = filemenu) style1 = ttk.Style() style1.map("C.TButton", foreground=[('pressed', 'red'), ('active', 'blue')], background=[('pressed', '!disabled', 'black'), ('active', 'purple')] ) top = ttk.Frame(root) top.grid(columnspan=2, rowspan=2) label1 = ttk.Label(top, text='Current Device').grid(column=0, row=1, pady=2) n = tkinter.StringVar() current_dev = ttk.Combobox(top, textvariable=n) current_dev['values'] = toplevel_loading(devices_listing)#generate_devs(devices_listing) current_dev.current() current_dev.grid() top.bind('<<ComboboxSelected>>', select_dev) device = n.get() sep1 = ttk.Separator(root, orient='horizontal').grid(row=2) index = ttk.Frame(root).grid(columnspan=3, padx=0, pady=0) ##########Current Tab Config Btns etc btn1Img = PhotoImage(file='images/pwr.png') btn2Img = PhotoImage(file='images/nav_up.png') btn3Img = PhotoImage(file='images/api.png') btn4Img = PhotoImage(file='images/nav_left.png') btn5Img = PhotoImage(file='images/nav_ok.png') btn6Img = PhotoImage(file='images/nav_right.png') btn7Img = PhotoImage(file='images/mute.png') btn8Img = PhotoImage(file='images/nav_down.png') btn9Img = PhotoImage(file='images/vol_up.png') btn10Img = PhotoImage(file='images/home.png') btn11Img = PhotoImage(file='images/info.png') btn12Img = PhotoImage(file='images/vol_down.png') btn1 = ttk.Button(index, style='C.TButton', text="Pwr", image=btn1Img, command=lambda:api_post(n.get(),api_calls.get("power_cycle"))).grid(row=3, column=0) btn2 = ttk.Button(index, text=" ^ ", image=btn2Img, command=lambda:api_post(n.get(),api_calls.get("up"))).grid(row=3, column=1) btn3 = ttk.Button(index, text="API Call", image=btn3Img, command=toplevel_apiCall).grid(row=6, column=0) btn4 = ttk.Button(index, text=" < ", image=btn4Img, command=lambda:api_post(n.get(),api_calls.get("left"))).grid(row=4, column=0) btn5 = ttk.Button(index, text="Enter", image=btn5Img, command=lambda:api_post(n.get(),api_calls.get("select"))).grid(row=4, column=1) btn6 = ttk.Button(index, text=" > ", image=btn6Img, command=lambda:api_post(n.get(),api_calls.get("right"))).grid(row=4, column=2) btn7 = ttk.Button(index, text="Mute", image=btn7Img, command=lambda:api_post(n.get(),api_calls.get("vol_mute"))).grid(row=5, column=0) btn8 = ttk.Button(index, text="\/", image=btn8Img, command=lambda:api_post(n.get(),api_calls.get("down"))).grid(row=5, column=1) btn9 = ttk.Button(index, text="Vol Up", image=btn9Img, command=lambda:api_post(n.get(),api_calls.get("vol_up"))).grid(row=5, column=2) btn10 = ttk.Button(index, text="Home", image=btn10Img, command=lambda:api_post(n.get(),api_calls.get("home"))).grid(row=3, column=2) btn11= ttk.Button(index, text="Info", image=btn11Img, command=lambda:api_post(n.get(),api_calls.get("info"))).grid(row=6, column=1) btn12 = ttk.Button(index, text="Vol Dn", image=btn12Img, command=lambda:api_post(n.get(),api_calls.get("vol_down"))).grid(row=6, column=2) msg_frame1 = LabelFrame(root, text = "Message Box",) msg_initial = "Welcome" label1 = Label(msg_frame1) label1['text'] = msg_initial label1.grid() msg_frame1.grid(sticky="s", columnspan=3) def msg_box(msg_label): counter = 3 label1['text'] = msg_label return label1 root.config(menu = menubar) if __name__ == '__main__': root.mainloop()
print("REQUEST WAS A SUCCESS. DEVICE {} RETURNED: {} ".format(n.get(), str(r))) r2 = r.text response = f'{r_code} - OK' return msg_box(response)
conditional_block
__init__.py
''' Data I/O package. Used to import and export data to and from TSV and JSON files. .. seealso:: File format documentation in: `Segmentation Representation \ Specification <http://nlp.chrisfournier.ca/publications/#seg_spec>`_. .. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com> ''' #=============================================================================== # Copyright (c) 2012, Chris Fournier # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #=============================================================================== import os import csv import json from .. import convert_positions_to_masses RESULTS = ['summary', 'tsv'] DEFAULT_DELIMITER = '\t' def load_tests(loader, tests, pattern): ''' A ``load_tests()`` function utilizing the default loader :func:`segeval.Utils.default_load_tests`. .. seealso:: The `load_tests protocol <http://docs.python.org/library/\ unittest.html#load-tests-protocol>`_. ''' #pylint: disable=W0613 from ..Utils import default_load_tests return default_load_tests(__file__, loader, tests) class DataIOError(Exception): ''' Indicates that an input processing error has occurred. ''' def __init__(self, message, exception): ''' Initializer. :param message: Explanation for the exception. :type message: str ''' Exception.__init__(self, message, exception) def input_linear_mass_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a linear segmentation mass TSV file. :param tsv_filename: path to the mass file containing segment mass codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type tsv_filename: str :type delimiter: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' # List version of file header = [] segment_masses = dict() # Open file csv_file = open(tsv_filename, 'rU') # Read in file try: reader = csv.reader(csv_file, delimiter=delimiter) for i, row in enumerate(reader): # Read annotators from header if i == 0: for item in row[1:]: header.append(item) # Read data else: coder = None for j, col in enumerate(row): # Skip the first col if j == 0: coder = str(col) segment_masses[coder] = list() elif j > 0: segment_masses[coder].append(int(col)) # pylint: disable=C0103 except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % tsv_filename, exception) finally: csv_file.close() return segment_masses def input_linear_positions_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a segment position TSV file. :param csv_filename: path to the mass file containing segment position codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type csv_filename: str :type delimiter: str .. deprecated:: 1.0 .. warning:: This i/o function is for legacy files only and will be removed in later versions. :returns: Segmentation mass codings. :rtype: :func:`dict` ''' coder_positions = input_linear_mass_tsv(tsv_filename, delimiter) # Convert each segment position to masses for coder, positions in coder_positions.items(): coder_positions[coder] = convert_positions_to_masses(positions) # Return return coder_positions def input_linear_mass_json(json_filename): ''' Load a segment mass JSON file. :param json_filename: path to the mass file containing segment position codings. :type json_filename: str :returns: Segmentation mass codings. :rtype: :func:`dict` .. seealso:: `JSON (JavaScript Object Notation) <http://www.json.org/>`_. ''' codings = dict() data = dict() # Open file json_file = open(json_filename, 'rU') # Read in file try: data = json.load(json_file) except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % json_filename, exception) # Check type if 'segmentation_type' in data: if data['segmentation_type'] != 'linear': raise DataIOError( 'Segmentation type \'linear\' expected, but encountered %s' % \ data['segmentation_type']) # Remove the metadata layer if 'codings' in data: data = data['codings'] else: data = data # Convert coder labels into strings for key, value in data.items(): codings[key] = value # Return return codings FILETYPE_TSV = 'tsv' FILETYPE_JSON = 'json' EXT = 'ext' FNC = 'fnc' FILETYPES = {FILETYPE_TSV : {EXT : ['.tsv', '.csv'], FNC : input_linear_mass_tsv}, FILETYPE_JSON : {EXT : ['.json', '.jsn'], FNC : input_linear_mass_json}} FILETYPES_DEFAULT = FILETYPE_JSON def load_nested_folders_dict(containing_dir, filetype): ''' Loads TSV files from a file directory structure, which reflects the directory structure in nested :func:`dict` with each directory name representing a key in these :func:`dict`. :param containing_dir: Root directory containing sub-directories which contain segmentation files. :param filetype: File type to load (e.g., json or tsv). :type containing_dir: str :type filetype: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' allowable_extensions = list(FILETYPES[filetype][EXT]) fnc_load = FILETYPES[filetype][FNC] data = dict() datafile_found = False # List of entries files = dict() dirs = dict() # For each filesystem item for name in os.listdir(containing_dir):
# If a data file was found if datafile_found: # If TSV files were found, load for name, filepath in files.items(): data[name] = fnc_load(filepath) else: # If only dirs were found, recurse for name, dirpath in dirs.items(): data[name] = load_nested_folders_dict(dirpath, filetype) return data def load_file(args): ''' Load a file or set of directories from command line arguments. :param args: Command line arguments :type args: dict :returns: The loaded values and whether a file was loaded or not. :rtype: :func:`dict`, :func:`bool` ''' values = None input_path = args['input'][0] is_file = os.path.isfile(input_path) filetype = args['format'] # Load file or dir if is_file: values = FILETYPES[filetype][FNC](input_path) values = {'item' : values} else: values = load_nested_folders_dict(input_path, filetype) return values, is_file def parser_add_file_support(parser): ''' Add support for file input and output parameters to an argument parser. :param parser: Argument parser :type parser: argparse.ArgumentParser ''' parser.add_argument('-o', '--output', type=str, nargs=1, required=False, help='Output file or directory. If not specified, a '+\ 'summary or results is printed to the console.') parser.add_argument('input', type=str, nargs=1, action='store', help='Input file or directory') parser.add_argument('-f', '--format', type=str, default=FILETYPES_DEFAULT, choices=FILETYPES.keys(), help='Input file format; default is %s' % \ FILETYPES_DEFAULT) parser.add_argument('-d', '--delimiter', type=str, default='\t', help='Delimiting character for input TSV files; '+\ 'ignored if JSON is specified, default is a tab '+\ 'character')
path = os.path.join(containing_dir, name) # Found a directory if os.path.isdir(path): dirs[name] = path # Found a file elif os.path.isfile(path): name, ext = os.path.splitext(name) if len(ext) > 0 and ext.lower() in allowable_extensions: files[name] = path datafile_found = True
conditional_block
__init__.py
''' Data I/O package. Used to import and export data to and from TSV and JSON files. .. seealso:: File format documentation in: `Segmentation Representation \ Specification <http://nlp.chrisfournier.ca/publications/#seg_spec>`_. .. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com> ''' #=============================================================================== # Copyright (c) 2012, Chris Fournier # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #=============================================================================== import os import csv import json from .. import convert_positions_to_masses RESULTS = ['summary', 'tsv'] DEFAULT_DELIMITER = '\t' def load_tests(loader, tests, pattern): ''' A ``load_tests()`` function utilizing the default loader :func:`segeval.Utils.default_load_tests`. .. seealso:: The `load_tests protocol <http://docs.python.org/library/\ unittest.html#load-tests-protocol>`_. ''' #pylint: disable=W0613 from ..Utils import default_load_tests return default_load_tests(__file__, loader, tests) class DataIOError(Exception): ''' Indicates that an input processing error has occurred. ''' def __init__(self, message, exception): ''' Initializer. :param message: Explanation for the exception. :type message: str ''' Exception.__init__(self, message, exception) def input_linear_mass_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a linear segmentation mass TSV file. :param tsv_filename: path to the mass file containing segment mass codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type tsv_filename: str :type delimiter: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' # List version of file header = [] segment_masses = dict() # Open file csv_file = open(tsv_filename, 'rU') # Read in file try: reader = csv.reader(csv_file, delimiter=delimiter) for i, row in enumerate(reader): # Read annotators from header if i == 0: for item in row[1:]: header.append(item) # Read data else: coder = None for j, col in enumerate(row): # Skip the first col if j == 0: coder = str(col) segment_masses[coder] = list() elif j > 0: segment_masses[coder].append(int(col)) # pylint: disable=C0103 except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % tsv_filename, exception) finally: csv_file.close() return segment_masses def input_linear_positions_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a segment position TSV file. :param csv_filename: path to the mass file containing segment position codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type csv_filename: str :type delimiter: str .. deprecated:: 1.0 .. warning:: This i/o function is for legacy files only and will be removed in later versions. :returns: Segmentation mass codings. :rtype: :func:`dict` '''
for coder, positions in coder_positions.items(): coder_positions[coder] = convert_positions_to_masses(positions) # Return return coder_positions def input_linear_mass_json(json_filename): ''' Load a segment mass JSON file. :param json_filename: path to the mass file containing segment position codings. :type json_filename: str :returns: Segmentation mass codings. :rtype: :func:`dict` .. seealso:: `JSON (JavaScript Object Notation) <http://www.json.org/>`_. ''' codings = dict() data = dict() # Open file json_file = open(json_filename, 'rU') # Read in file try: data = json.load(json_file) except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % json_filename, exception) # Check type if 'segmentation_type' in data: if data['segmentation_type'] != 'linear': raise DataIOError( 'Segmentation type \'linear\' expected, but encountered %s' % \ data['segmentation_type']) # Remove the metadata layer if 'codings' in data: data = data['codings'] else: data = data # Convert coder labels into strings for key, value in data.items(): codings[key] = value # Return return codings FILETYPE_TSV = 'tsv' FILETYPE_JSON = 'json' EXT = 'ext' FNC = 'fnc' FILETYPES = {FILETYPE_TSV : {EXT : ['.tsv', '.csv'], FNC : input_linear_mass_tsv}, FILETYPE_JSON : {EXT : ['.json', '.jsn'], FNC : input_linear_mass_json}} FILETYPES_DEFAULT = FILETYPE_JSON def load_nested_folders_dict(containing_dir, filetype): ''' Loads TSV files from a file directory structure, which reflects the directory structure in nested :func:`dict` with each directory name representing a key in these :func:`dict`. :param containing_dir: Root directory containing sub-directories which contain segmentation files. :param filetype: File type to load (e.g., json or tsv). :type containing_dir: str :type filetype: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' allowable_extensions = list(FILETYPES[filetype][EXT]) fnc_load = FILETYPES[filetype][FNC] data = dict() datafile_found = False # List of entries files = dict() dirs = dict() # For each filesystem item for name in os.listdir(containing_dir): path = os.path.join(containing_dir, name) # Found a directory if os.path.isdir(path): dirs[name] = path # Found a file elif os.path.isfile(path): name, ext = os.path.splitext(name) if len(ext) > 0 and ext.lower() in allowable_extensions: files[name] = path datafile_found = True # If a data file was found if datafile_found: # If TSV files were found, load for name, filepath in files.items(): data[name] = fnc_load(filepath) else: # If only dirs were found, recurse for name, dirpath in dirs.items(): data[name] = load_nested_folders_dict(dirpath, filetype) return data def load_file(args): ''' Load a file or set of directories from command line arguments. :param args: Command line arguments :type args: dict :returns: The loaded values and whether a file was loaded or not. :rtype: :func:`dict`, :func:`bool` ''' values = None input_path = args['input'][0] is_file = os.path.isfile(input_path) filetype = args['format'] # Load file or dir if is_file: values = FILETYPES[filetype][FNC](input_path) values = {'item' : values} else: values = load_nested_folders_dict(input_path, filetype) return values, is_file def parser_add_file_support(parser): ''' Add support for file input and output parameters to an argument parser. :param parser: Argument parser :type parser: argparse.ArgumentParser ''' parser.add_argument('-o', '--output', type=str, nargs=1, required=False, help='Output file or directory. If not specified, a '+\ 'summary or results is printed to the console.') parser.add_argument('input', type=str, nargs=1, action='store', help='Input file or directory') parser.add_argument('-f', '--format', type=str, default=FILETYPES_DEFAULT, choices=FILETYPES.keys(), help='Input file format; default is %s' % \ FILETYPES_DEFAULT) parser.add_argument('-d', '--delimiter', type=str, default='\t', help='Delimiting character for input TSV files; '+\ 'ignored if JSON is specified, default is a tab '+\ 'character')
coder_positions = input_linear_mass_tsv(tsv_filename, delimiter) # Convert each segment position to masses
random_line_split
__init__.py
''' Data I/O package. Used to import and export data to and from TSV and JSON files. .. seealso:: File format documentation in: `Segmentation Representation \ Specification <http://nlp.chrisfournier.ca/publications/#seg_spec>`_. .. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com> ''' #=============================================================================== # Copyright (c) 2012, Chris Fournier # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #=============================================================================== import os import csv import json from .. import convert_positions_to_masses RESULTS = ['summary', 'tsv'] DEFAULT_DELIMITER = '\t' def load_tests(loader, tests, pattern): ''' A ``load_tests()`` function utilizing the default loader :func:`segeval.Utils.default_load_tests`. .. seealso:: The `load_tests protocol <http://docs.python.org/library/\ unittest.html#load-tests-protocol>`_. ''' #pylint: disable=W0613 from ..Utils import default_load_tests return default_load_tests(__file__, loader, tests) class DataIOError(Exception): ''' Indicates that an input processing error has occurred. ''' def __init__(self, message, exception): ''' Initializer. :param message: Explanation for the exception. :type message: str ''' Exception.__init__(self, message, exception) def input_linear_mass_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a linear segmentation mass TSV file. :param tsv_filename: path to the mass file containing segment mass codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type tsv_filename: str :type delimiter: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' # List version of file header = [] segment_masses = dict() # Open file csv_file = open(tsv_filename, 'rU') # Read in file try: reader = csv.reader(csv_file, delimiter=delimiter) for i, row in enumerate(reader): # Read annotators from header if i == 0: for item in row[1:]: header.append(item) # Read data else: coder = None for j, col in enumerate(row): # Skip the first col if j == 0: coder = str(col) segment_masses[coder] = list() elif j > 0: segment_masses[coder].append(int(col)) # pylint: disable=C0103 except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % tsv_filename, exception) finally: csv_file.close() return segment_masses def input_linear_positions_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a segment position TSV file. :param csv_filename: path to the mass file containing segment position codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type csv_filename: str :type delimiter: str .. deprecated:: 1.0 .. warning:: This i/o function is for legacy files only and will be removed in later versions. :returns: Segmentation mass codings. :rtype: :func:`dict` ''' coder_positions = input_linear_mass_tsv(tsv_filename, delimiter) # Convert each segment position to masses for coder, positions in coder_positions.items(): coder_positions[coder] = convert_positions_to_masses(positions) # Return return coder_positions def input_linear_mass_json(json_filename): ''' Load a segment mass JSON file. :param json_filename: path to the mass file containing segment position codings. :type json_filename: str :returns: Segmentation mass codings. :rtype: :func:`dict` .. seealso:: `JSON (JavaScript Object Notation) <http://www.json.org/>`_. ''' codings = dict() data = dict() # Open file json_file = open(json_filename, 'rU') # Read in file try: data = json.load(json_file) except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % json_filename, exception) # Check type if 'segmentation_type' in data: if data['segmentation_type'] != 'linear': raise DataIOError( 'Segmentation type \'linear\' expected, but encountered %s' % \ data['segmentation_type']) # Remove the metadata layer if 'codings' in data: data = data['codings'] else: data = data # Convert coder labels into strings for key, value in data.items(): codings[key] = value # Return return codings FILETYPE_TSV = 'tsv' FILETYPE_JSON = 'json' EXT = 'ext' FNC = 'fnc' FILETYPES = {FILETYPE_TSV : {EXT : ['.tsv', '.csv'], FNC : input_linear_mass_tsv}, FILETYPE_JSON : {EXT : ['.json', '.jsn'], FNC : input_linear_mass_json}} FILETYPES_DEFAULT = FILETYPE_JSON def load_nested_folders_dict(containing_dir, filetype):
def load_file(args): ''' Load a file or set of directories from command line arguments. :param args: Command line arguments :type args: dict :returns: The loaded values and whether a file was loaded or not. :rtype: :func:`dict`, :func:`bool` ''' values = None input_path = args['input'][0] is_file = os.path.isfile(input_path) filetype = args['format'] # Load file or dir if is_file: values = FILETYPES[filetype][FNC](input_path) values = {'item' : values} else: values = load_nested_folders_dict(input_path, filetype) return values, is_file def parser_add_file_support(parser): ''' Add support for file input and output parameters to an argument parser. :param parser: Argument parser :type parser: argparse.ArgumentParser ''' parser.add_argument('-o', '--output', type=str, nargs=1, required=False, help='Output file or directory. If not specified, a '+\ 'summary or results is printed to the console.') parser.add_argument('input', type=str, nargs=1, action='store', help='Input file or directory') parser.add_argument('-f', '--format', type=str, default=FILETYPES_DEFAULT, choices=FILETYPES.keys(), help='Input file format; default is %s' % \ FILETYPES_DEFAULT) parser.add_argument('-d', '--delimiter', type=str, default='\t', help='Delimiting character for input TSV files; '+\ 'ignored if JSON is specified, default is a tab '+\ 'character')
''' Loads TSV files from a file directory structure, which reflects the directory structure in nested :func:`dict` with each directory name representing a key in these :func:`dict`. :param containing_dir: Root directory containing sub-directories which contain segmentation files. :param filetype: File type to load (e.g., json or tsv). :type containing_dir: str :type filetype: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' allowable_extensions = list(FILETYPES[filetype][EXT]) fnc_load = FILETYPES[filetype][FNC] data = dict() datafile_found = False # List of entries files = dict() dirs = dict() # For each filesystem item for name in os.listdir(containing_dir): path = os.path.join(containing_dir, name) # Found a directory if os.path.isdir(path): dirs[name] = path # Found a file elif os.path.isfile(path): name, ext = os.path.splitext(name) if len(ext) > 0 and ext.lower() in allowable_extensions: files[name] = path datafile_found = True # If a data file was found if datafile_found: # If TSV files were found, load for name, filepath in files.items(): data[name] = fnc_load(filepath) else: # If only dirs were found, recurse for name, dirpath in dirs.items(): data[name] = load_nested_folders_dict(dirpath, filetype) return data
identifier_body
__init__.py
''' Data I/O package. Used to import and export data to and from TSV and JSON files. .. seealso:: File format documentation in: `Segmentation Representation \ Specification <http://nlp.chrisfournier.ca/publications/#seg_spec>`_. .. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com> ''' #=============================================================================== # Copyright (c) 2012, Chris Fournier # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #=============================================================================== import os import csv import json from .. import convert_positions_to_masses RESULTS = ['summary', 'tsv'] DEFAULT_DELIMITER = '\t' def load_tests(loader, tests, pattern): ''' A ``load_tests()`` function utilizing the default loader :func:`segeval.Utils.default_load_tests`. .. seealso:: The `load_tests protocol <http://docs.python.org/library/\ unittest.html#load-tests-protocol>`_. ''' #pylint: disable=W0613 from ..Utils import default_load_tests return default_load_tests(__file__, loader, tests) class DataIOError(Exception): ''' Indicates that an input processing error has occurred. ''' def __init__(self, message, exception): ''' Initializer. :param message: Explanation for the exception. :type message: str ''' Exception.__init__(self, message, exception) def input_linear_mass_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a linear segmentation mass TSV file. :param tsv_filename: path to the mass file containing segment mass codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type tsv_filename: str :type delimiter: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' # List version of file header = [] segment_masses = dict() # Open file csv_file = open(tsv_filename, 'rU') # Read in file try: reader = csv.reader(csv_file, delimiter=delimiter) for i, row in enumerate(reader): # Read annotators from header if i == 0: for item in row[1:]: header.append(item) # Read data else: coder = None for j, col in enumerate(row): # Skip the first col if j == 0: coder = str(col) segment_masses[coder] = list() elif j > 0: segment_masses[coder].append(int(col)) # pylint: disable=C0103 except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % tsv_filename, exception) finally: csv_file.close() return segment_masses def input_linear_positions_tsv(tsv_filename, delimiter=DEFAULT_DELIMITER): ''' Load a segment position TSV file. :param csv_filename: path to the mass file containing segment position codings. :param delimiter: the delimiter used when reading a TSV file (by default, a tab, but it can also be a comma, whitespace, etc. :type csv_filename: str :type delimiter: str .. deprecated:: 1.0 .. warning:: This i/o function is for legacy files only and will be removed in later versions. :returns: Segmentation mass codings. :rtype: :func:`dict` ''' coder_positions = input_linear_mass_tsv(tsv_filename, delimiter) # Convert each segment position to masses for coder, positions in coder_positions.items(): coder_positions[coder] = convert_positions_to_masses(positions) # Return return coder_positions def input_linear_mass_json(json_filename): ''' Load a segment mass JSON file. :param json_filename: path to the mass file containing segment position codings. :type json_filename: str :returns: Segmentation mass codings. :rtype: :func:`dict` .. seealso:: `JSON (JavaScript Object Notation) <http://www.json.org/>`_. ''' codings = dict() data = dict() # Open file json_file = open(json_filename, 'rU') # Read in file try: data = json.load(json_file) except Exception as exception: raise DataIOError('Error occurred processing file: %s' \ % json_filename, exception) # Check type if 'segmentation_type' in data: if data['segmentation_type'] != 'linear': raise DataIOError( 'Segmentation type \'linear\' expected, but encountered %s' % \ data['segmentation_type']) # Remove the metadata layer if 'codings' in data: data = data['codings'] else: data = data # Convert coder labels into strings for key, value in data.items(): codings[key] = value # Return return codings FILETYPE_TSV = 'tsv' FILETYPE_JSON = 'json' EXT = 'ext' FNC = 'fnc' FILETYPES = {FILETYPE_TSV : {EXT : ['.tsv', '.csv'], FNC : input_linear_mass_tsv}, FILETYPE_JSON : {EXT : ['.json', '.jsn'], FNC : input_linear_mass_json}} FILETYPES_DEFAULT = FILETYPE_JSON def
(containing_dir, filetype): ''' Loads TSV files from a file directory structure, which reflects the directory structure in nested :func:`dict` with each directory name representing a key in these :func:`dict`. :param containing_dir: Root directory containing sub-directories which contain segmentation files. :param filetype: File type to load (e.g., json or tsv). :type containing_dir: str :type filetype: str :returns: Segmentation mass codings. :rtype: :func:`dict` ''' allowable_extensions = list(FILETYPES[filetype][EXT]) fnc_load = FILETYPES[filetype][FNC] data = dict() datafile_found = False # List of entries files = dict() dirs = dict() # For each filesystem item for name in os.listdir(containing_dir): path = os.path.join(containing_dir, name) # Found a directory if os.path.isdir(path): dirs[name] = path # Found a file elif os.path.isfile(path): name, ext = os.path.splitext(name) if len(ext) > 0 and ext.lower() in allowable_extensions: files[name] = path datafile_found = True # If a data file was found if datafile_found: # If TSV files were found, load for name, filepath in files.items(): data[name] = fnc_load(filepath) else: # If only dirs were found, recurse for name, dirpath in dirs.items(): data[name] = load_nested_folders_dict(dirpath, filetype) return data def load_file(args): ''' Load a file or set of directories from command line arguments. :param args: Command line arguments :type args: dict :returns: The loaded values and whether a file was loaded or not. :rtype: :func:`dict`, :func:`bool` ''' values = None input_path = args['input'][0] is_file = os.path.isfile(input_path) filetype = args['format'] # Load file or dir if is_file: values = FILETYPES[filetype][FNC](input_path) values = {'item' : values} else: values = load_nested_folders_dict(input_path, filetype) return values, is_file def parser_add_file_support(parser): ''' Add support for file input and output parameters to an argument parser. :param parser: Argument parser :type parser: argparse.ArgumentParser ''' parser.add_argument('-o', '--output', type=str, nargs=1, required=False, help='Output file or directory. If not specified, a '+\ 'summary or results is printed to the console.') parser.add_argument('input', type=str, nargs=1, action='store', help='Input file or directory') parser.add_argument('-f', '--format', type=str, default=FILETYPES_DEFAULT, choices=FILETYPES.keys(), help='Input file format; default is %s' % \ FILETYPES_DEFAULT) parser.add_argument('-d', '--delimiter', type=str, default='\t', help='Delimiting character for input TSV files; '+\ 'ignored if JSON is specified, default is a tab '+\ 'character')
load_nested_folders_dict
identifier_name
report.py
import numpy as np import pandas as pd from pathlib import Path import matplotlib as mpl from matplotlib import pyplot as plt plt.style.use('seaborn-muted') #from IPython import get_ipython from IPython.display import HTML, Markdown import air_cargo_problems as acp problems = ['Air Cargo Problem 1', 'Air Cargo Problem 2', 'Air Cargo Problem 3', 'Air Cargo Problem 4'] SEARCHES = ['breadth_first_search', 'depth_first_graph_search', 'uniform_cost_search', 'greedy_best_first_graph_search h_unmet_goals', 'greedy_best_first_graph_search h_pg_levelsum', 'greedy_best_first_graph_search h_pg_maxlevel', 'greedy_best_first_graph_search h_pg_setlevel', 'astar_search h_unmet_goals', 'astar_search h_pg_levelsum', 'astar_search h_pg_maxlevel', 'astar_search h_pg_setlevel'] def get_prob_specs(): Probs = [acp.air_cargo_p1(), acp.air_cargo_p2(), acp.air_cargo_p3(), acp.air_cargo_p4()] problems_specs = {'Problem': [name for name in problems], 'Air cargo problem': [i+1 for i in range(len(problems))], 'Cargos': [len(p.cargos) for p in Probs], 'Planes': [len(p.planes) for p in Probs], 'Airports': [len(p.airports) for p in Probs], 'Goal': [len(p.goal) for p in Probs]} return pd.DataFrame(problems_specs) specs = get_prob_specs() def df2tsv(df, fname, replace=False): if Path(fname).exists(): if replace: df.to_csv(fname, sep='\t') #else: # print(f'File {fname} not replaced.') return df.to_csv(fname, sep='\t') return def get_problem_data_df(file_stem, problem, raw_dir, out_dir, file_as_tsv=False, replace=False): """ Combine all processed files of a problem found in Path(data_dir) with given stem. The file to be saved to/retrieved from out_dir is passed in file_as_tsv, tab separated csv. Input example: file_stem = 'prob_2' problem = 'Air Cargo Problem 2' Output: a dataframe, saved to tsv if file_as_tsv=True and not replace; saved as file_stem+'_df.csv'. """ if file_stem is None or problem is None: print('file_stem and problem must have a value.') return t = '\t' # input/output file suffixes: sfx = ['.csv', '_df.csv'] # Try retrieving it from out_dir if not replacing it: fout = None if file_as_tsv: fout = Path(out_dir).joinpath(file_stem + sfx[1]) if fout.exists() and not replace: df = pd.read_csv(fout, sep=t) try: return df.drop('Unnamed: 0', axis=1) except KeyError: pass # else: (re)process pfiles = list(Path(raw_dir).glob(file_stem + '*')) if len(pfiles) == 0: print(f'No raw files with stem: {file_stem}')
dflist = [] for f in pfiles: df, err = get_results_df(f, problem) if df is not None: df = df.merge(specs) df['index'] = df['Searcher'].apply(lambda x: SEARCHES.index(x)+1) df['index'] = df['index'].astype(int) df.set_index('index', drop=True, inplace=True) dflist.append(df) del df else: print(f'Error from get_results_df:\n\t{err}') dfout = pd.concat(dflist, ignore_index=False) dfout.sort_index(inplace=True) if file_as_tsv: df2tsv(dfout, fout, replace=replace) return dfout def get_results_df(fname, problem): """Process csv into dataframe. """ t = '\t' # Cols to add: val_cols = ['Actions','Expansions','GoalTests','NewNodes','PlanLength','ElapsedSeconds'] err = '' df = pd.read_csv(fname, sep=t) if df.shape[0] < len(val_cols): err = f'Data for {fname.name} is incomplete.' return None, err # Rename cols: c (temp) -> Searcher df.columns = ['c', 'Searcher'] # Add new cols & reindex df = df.reindex(columns = df.columns.tolist() + val_cols) # Populate new cols according to row with search name: sr = df.loc[df.c == 'Searcher', 'Searcher'] for (idx, sr_row) in sr.items(): j = idx for c in df.columns[2:].tolist(): j += 1 if c == 'ElapsedSeconds': df.loc[idx, c] = float(df.loc[j, 'Searcher']) else: df.loc[idx, c] = int(df.loc[j, 'Searcher']) df.dropna(inplace=True) # Add a minute column: df['Minutes'] = np.round(df.ElapsedSeconds/60, 3) # Replace values of 1st col with problem name & update col name: df['c'] = problem df.rename(columns={'c': 'Problem'}, inplace=True) df.reset_index(drop=True, inplace=True) return df, '' def concat_all_dfs(dflist): """ Output combined df for complete runs, Actions>0. """ dfall = pd.concat(dflist, ignore_index=False) dfall.reset_index(drop=False, inplace=True) dfall.rename(columns={'index': 'id'}, inplace=True) # reduced drop_cols = dfall.columns[-4:-1].tolist() + ['Problem','Minutes','GoalTests'] dfa = dfall.drop(drop_cols, axis=1) del dfall # add col for function name dfa['search_fn'] = dfa.Searcher.str.partition(' ')[0] # reorder cols dfa = dfa[['Air cargo problem','id','search_fn','Searcher','Actions', 'PlanLength', 'NewNodes','Expansions','ElapsedSeconds']] # complete runs only: return dfa[dfa['Actions'].values > 0] def plans_length(dfa, which): """ dfa: frame of concatenated df1 to df4. Analysis of plan length for which in ['double', 'single']: PlanLength is double(single)-digit. """ if which == 'double': msk = dfa.PlanLength >= 10 col2 = 'Frequency where PlanLength >=10' else: msk = dfa.PlanLength < 10 col2 = 'Frequency where PlanLength <10' dfa_rows = dfa.shape[0] dfout = dfa[msk].sort_values(['PlanLength'], ascending=False) uniq_probs = dfout['Air cargo problem'].unique() n_plans = dfout.shape[0] searcher_cnt = dfout['Searcher'].value_counts() fn_cnt = dfout['search_fn'].value_counts() # get the html string: df_fn = fn_cnt.to_frame() df_fn.reset_index(drop=False, inplace=True) df_fn.columns = ['Search function', col2] df_fn_html = df_fn.to_html(index=False, justify='center') replace_str1 = ' style="text-align: center;"' replace_str2 = 'class="dataframe"' df_fn_html = df_fn_html.replace(replace_str1, '') df_fn_html = df_fn_html.replace(replace_str2, replace_str1) pct_plans = n_plans/dfa_rows top2_fn = fn_cnt[0:2].sum() pct_top2_fn = top2_fn/n_plans text = f"Out of {dfa_rows} completed searches, {pct_plans:.0%} ({n_plans}), have {which}-digit or longer PlanLength.<br>" text += f"In that subset, {top2_fn:d} ({pct_top2_fn:.0%}) involve the search functions `{fn_cnt.index[0]}` and `{fn_cnt.index[1]}`." if len(uniq_probs) < 4: text += " And this occurs only for Problems: " pro = ",".join('{}' for p in uniq_probs) +'.<br>' text += pro.format(*uniq_probs) else: text += " And this occurs for all Problems." text += "<br>" return df_fn_html, text, dfout def make_bar_plots(df_list, x_col, y_col, problems, legend_bbox=(.05, .95), to_file='', show=False, excluded=None): """ To get 2 bar plots in a row. """ import matplotlib.patches as mpatches def despine(ax): ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) a1 = df_list[0][x_col].unique().astype(int) a1 = a1[a1>0] a2 = df_list[1][x_col].unique().astype(int) a2 = a2[a2>0] assert len(a1) == len(a2) == 1 action_nums = [a1[0], a2[0]] p1 = df_list[0]['Air cargo problem'].iloc[0] p2 = df_list[1]['Air cargo problem'].iloc[0] # Seach functions names should be common to all dfs: search = df_list[0].Searcher.tolist() # Sample cmap according to categories: s_len = len(search) cmap = plt.get_cmap('viridis') m = cmap.N // s_len colors = [cmap.colors[i*m] for i in range(s_len)] fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12,5)) # Use the minutes columns for the more complex problems: if y_col == 'ElapsedSeconds': ty_col = 'Elapsed time' if p1 == 3 or p == 4: # applies to problems 3/4 y_col = 'Minutes' else: ty_col = y_col plt.title(f'{ty_col} vs. {x_col} for Problems {p1} & {p2}', y = 1.05, fontsize=14) for i, df in enumerate(df_list): ylog = False ylab = f'{y_col}' # log scale on NewNodes for df2, df3, df4: if (i == 1 or p1 == 3) and y_col == 'NewNodes': ylog = True ylab += ' (log)' axs[i].set_ylabel(ylab, fontsize=12) df[y_col].plot.bar(ax=axs[i], logy=ylog, color=colors, legend=False) t = '{}, {} = {:d}'.format(problems[i], x_col, action_nums[i]) axs[i].set_xlabel(t, fontsize=12) axs[i].set_xticks([]) despine(axs[i]) legt = 'Searchers' new_lgd = p1 == 3 and excluded is not None if new_lgd: # Modify the legend to indicate excluded searches # (bc colormap is identical to fig1/2, but some runs have no data). legt += ' (X :: excluded)' excluded_len = len(excluded) x_idx = [excluded[i][0]-1 for i in range(excluded_len)] legend_patches = [] for i, c in enumerate(colors): lab = search[i] if new_lgd: if SEARCHES.index(lab) in x_idx: lab = lab.replace(' ', ' + ') lab += ' X' else: lab = lab.replace(' ', ' + ') else: lab = lab.replace(' ', ' + ') legend_patches.append(mpatches.Patch(color=c, label=lab)) axs[1].legend(handles=legend_patches, title=legt, title_fontsize='14', fontsize='medium', bbox_to_anchor=legend_bbox, loc='upper left', labelspacing=0.6, fancybox=True) plt.tight_layout() if to_file: plt.savefig(to_file) if show: return axs def format_multiples(multi): s = '' for i in range(len(multi)): s += '{'+ str(i) +':s}, ' s = s[:-2] return '[' + s.format(*multi.values) + ']' def order_analysis(df2, df1, column_to_compare): """ df2: has the large values. """ colA_larger_values = df2[column_to_compare] colA_smaller_values = df1[column_to_compare] # orders of magnitude difference btw dfB and dfA (min, max): mag = np.round(np.log(colA_larger_values/colA_smaller_values), 0) mag.sort_values(ascending=False, inplace=True) mag_aver = int(np.round(mag.mean(), 0)) # get the indices of values above average: ma = mag[mag > mag_aver].index.tolist() # get the names of all searchers corresponding to the ma: above_multiples = (mag_aver, df2.loc[ma, 'Searcher']) return above_multiples def comparison_paragraph(df2, df1, heading, column_to_compare, return_html=False): p1 = df1.loc[0,'Problem'][-1] p2 = df2.loc[0,'Problem'][-1] order_aver, searches_above = order_analysis(df2, df1, column_to_compare) above = format_multiples(searches_above) headinglc = heading.lower() text = f"""<h3>* {heading}</h3><p style="font-size:110%;">For Problems {p1} and {p2}, """ text += f"the <i>average</i> order of magnitude difference in {headinglc} is " text += f"<b>{order_aver:d}</b>, which is surpassed by these searches: {above}.</p>" if return_html: return text else: return Markdown(text) def get_elim_candidates(df2, df1): """ For the analysis of problems 1 & 2. List the costliest searches: candidates for elimination on more complex problems. """ if df1.loc[1,'Problem']!= problems[0]: return nodes_order_av, nodes_above = order_analysis(df2, df1, 'NewNodes') time_order_av, time_above = order_analysis(df2, df1, 'ElapsedSeconds') elim_candidates = set(nodes_above[:nodes_order_av]).intersection(set(time_above[:time_order_av])) # return their 1-base index also: out = [(SEARCHES.index(c)+1, c) for c in elim_candidates] return out def paragraph_p12(candidates_tup, return_html=False): """ For displaying the analysis of problems 1 & 2. """ elim_list = "" for i, c in candidates_tup: elim_list += f"<dt><b>{i:>2}: {c}</b></dt>" text = """<h3>* Insights from Problems 1 and 2</h3><p style="font-size:110%;">""" text += """On the basis of Figures 1 and 2, which show the number of new nodes created, and the time spent by each search function, respectively, the searches that are candidates for elimination for more complex problems are those at the intersection of the average-ranked costliest sets viz new nodes creation and search time.<br>These searches are:</p><pre><dl>""" text += f"<dl>{elim_list}</dl></p></pre>" if return_html: return text else: return Markdown(text) def add_div_around_html(div_html_text, output_string=False, div_style="{width: 80%}"): """ Wrap an html code str inside a div. div_style: whatever follows style= within the <div> Behaviour with `output_string=True`: The cell is overwritten with the output string (but the cell mode is still in 'code' not 'markdown') The only thing to do is change the cell mode to Markdown. If `output_string=False`, the HTML/md output is displayed in an output cell. """ div = f"""<div style="{div_style}">{div_html_text}</div>""" if output_string: return div #get_ipython().set_next_input(div, 'markdown') else: return Markdown(div)
return
random_line_split
report.py
import numpy as np import pandas as pd from pathlib import Path import matplotlib as mpl from matplotlib import pyplot as plt plt.style.use('seaborn-muted') #from IPython import get_ipython from IPython.display import HTML, Markdown import air_cargo_problems as acp problems = ['Air Cargo Problem 1', 'Air Cargo Problem 2', 'Air Cargo Problem 3', 'Air Cargo Problem 4'] SEARCHES = ['breadth_first_search', 'depth_first_graph_search', 'uniform_cost_search', 'greedy_best_first_graph_search h_unmet_goals', 'greedy_best_first_graph_search h_pg_levelsum', 'greedy_best_first_graph_search h_pg_maxlevel', 'greedy_best_first_graph_search h_pg_setlevel', 'astar_search h_unmet_goals', 'astar_search h_pg_levelsum', 'astar_search h_pg_maxlevel', 'astar_search h_pg_setlevel'] def get_prob_specs(): Probs = [acp.air_cargo_p1(), acp.air_cargo_p2(), acp.air_cargo_p3(), acp.air_cargo_p4()] problems_specs = {'Problem': [name for name in problems], 'Air cargo problem': [i+1 for i in range(len(problems))], 'Cargos': [len(p.cargos) for p in Probs], 'Planes': [len(p.planes) for p in Probs], 'Airports': [len(p.airports) for p in Probs], 'Goal': [len(p.goal) for p in Probs]} return pd.DataFrame(problems_specs) specs = get_prob_specs() def df2tsv(df, fname, replace=False): if Path(fname).exists(): if replace: df.to_csv(fname, sep='\t') #else: # print(f'File {fname} not replaced.') return df.to_csv(fname, sep='\t') return def get_problem_data_df(file_stem, problem, raw_dir, out_dir, file_as_tsv=False, replace=False): """ Combine all processed files of a problem found in Path(data_dir) with given stem. The file to be saved to/retrieved from out_dir is passed in file_as_tsv, tab separated csv. Input example: file_stem = 'prob_2' problem = 'Air Cargo Problem 2' Output: a dataframe, saved to tsv if file_as_tsv=True and not replace; saved as file_stem+'_df.csv'. """ if file_stem is None or problem is None: print('file_stem and problem must have a value.') return t = '\t' # input/output file suffixes: sfx = ['.csv', '_df.csv'] # Try retrieving it from out_dir if not replacing it: fout = None if file_as_tsv: fout = Path(out_dir).joinpath(file_stem + sfx[1]) if fout.exists() and not replace: df = pd.read_csv(fout, sep=t) try: return df.drop('Unnamed: 0', axis=1) except KeyError: pass # else: (re)process pfiles = list(Path(raw_dir).glob(file_stem + '*')) if len(pfiles) == 0: print(f'No raw files with stem: {file_stem}') return dflist = [] for f in pfiles: df, err = get_results_df(f, problem) if df is not None:
else: print(f'Error from get_results_df:\n\t{err}') dfout = pd.concat(dflist, ignore_index=False) dfout.sort_index(inplace=True) if file_as_tsv: df2tsv(dfout, fout, replace=replace) return dfout def get_results_df(fname, problem): """Process csv into dataframe. """ t = '\t' # Cols to add: val_cols = ['Actions','Expansions','GoalTests','NewNodes','PlanLength','ElapsedSeconds'] err = '' df = pd.read_csv(fname, sep=t) if df.shape[0] < len(val_cols): err = f'Data for {fname.name} is incomplete.' return None, err # Rename cols: c (temp) -> Searcher df.columns = ['c', 'Searcher'] # Add new cols & reindex df = df.reindex(columns = df.columns.tolist() + val_cols) # Populate new cols according to row with search name: sr = df.loc[df.c == 'Searcher', 'Searcher'] for (idx, sr_row) in sr.items(): j = idx for c in df.columns[2:].tolist(): j += 1 if c == 'ElapsedSeconds': df.loc[idx, c] = float(df.loc[j, 'Searcher']) else: df.loc[idx, c] = int(df.loc[j, 'Searcher']) df.dropna(inplace=True) # Add a minute column: df['Minutes'] = np.round(df.ElapsedSeconds/60, 3) # Replace values of 1st col with problem name & update col name: df['c'] = problem df.rename(columns={'c': 'Problem'}, inplace=True) df.reset_index(drop=True, inplace=True) return df, '' def concat_all_dfs(dflist): """ Output combined df for complete runs, Actions>0. """ dfall = pd.concat(dflist, ignore_index=False) dfall.reset_index(drop=False, inplace=True) dfall.rename(columns={'index': 'id'}, inplace=True) # reduced drop_cols = dfall.columns[-4:-1].tolist() + ['Problem','Minutes','GoalTests'] dfa = dfall.drop(drop_cols, axis=1) del dfall # add col for function name dfa['search_fn'] = dfa.Searcher.str.partition(' ')[0] # reorder cols dfa = dfa[['Air cargo problem','id','search_fn','Searcher','Actions', 'PlanLength', 'NewNodes','Expansions','ElapsedSeconds']] # complete runs only: return dfa[dfa['Actions'].values > 0] def plans_length(dfa, which): """ dfa: frame of concatenated df1 to df4. Analysis of plan length for which in ['double', 'single']: PlanLength is double(single)-digit. """ if which == 'double': msk = dfa.PlanLength >= 10 col2 = 'Frequency where PlanLength >=10' else: msk = dfa.PlanLength < 10 col2 = 'Frequency where PlanLength <10' dfa_rows = dfa.shape[0] dfout = dfa[msk].sort_values(['PlanLength'], ascending=False) uniq_probs = dfout['Air cargo problem'].unique() n_plans = dfout.shape[0] searcher_cnt = dfout['Searcher'].value_counts() fn_cnt = dfout['search_fn'].value_counts() # get the html string: df_fn = fn_cnt.to_frame() df_fn.reset_index(drop=False, inplace=True) df_fn.columns = ['Search function', col2] df_fn_html = df_fn.to_html(index=False, justify='center') replace_str1 = ' style="text-align: center;"' replace_str2 = 'class="dataframe"' df_fn_html = df_fn_html.replace(replace_str1, '') df_fn_html = df_fn_html.replace(replace_str2, replace_str1) pct_plans = n_plans/dfa_rows top2_fn = fn_cnt[0:2].sum() pct_top2_fn = top2_fn/n_plans text = f"Out of {dfa_rows} completed searches, {pct_plans:.0%} ({n_plans}), have {which}-digit or longer PlanLength.<br>" text += f"In that subset, {top2_fn:d} ({pct_top2_fn:.0%}) involve the search functions `{fn_cnt.index[0]}` and `{fn_cnt.index[1]}`." if len(uniq_probs) < 4: text += " And this occurs only for Problems: " pro = ",".join('{}' for p in uniq_probs) +'.<br>' text += pro.format(*uniq_probs) else: text += " And this occurs for all Problems." text += "<br>" return df_fn_html, text, dfout def make_bar_plots(df_list, x_col, y_col, problems, legend_bbox=(.05, .95), to_file='', show=False, excluded=None): """ To get 2 bar plots in a row. """ import matplotlib.patches as mpatches def despine(ax): ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) a1 = df_list[0][x_col].unique().astype(int) a1 = a1[a1>0] a2 = df_list[1][x_col].unique().astype(int) a2 = a2[a2>0] assert len(a1) == len(a2) == 1 action_nums = [a1[0], a2[0]] p1 = df_list[0]['Air cargo problem'].iloc[0] p2 = df_list[1]['Air cargo problem'].iloc[0] # Seach functions names should be common to all dfs: search = df_list[0].Searcher.tolist() # Sample cmap according to categories: s_len = len(search) cmap = plt.get_cmap('viridis') m = cmap.N // s_len colors = [cmap.colors[i*m] for i in range(s_len)] fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12,5)) # Use the minutes columns for the more complex problems: if y_col == 'ElapsedSeconds': ty_col = 'Elapsed time' if p1 == 3 or p == 4: # applies to problems 3/4 y_col = 'Minutes' else: ty_col = y_col plt.title(f'{ty_col} vs. {x_col} for Problems {p1} & {p2}', y = 1.05, fontsize=14) for i, df in enumerate(df_list): ylog = False ylab = f'{y_col}' # log scale on NewNodes for df2, df3, df4: if (i == 1 or p1 == 3) and y_col == 'NewNodes': ylog = True ylab += ' (log)' axs[i].set_ylabel(ylab, fontsize=12) df[y_col].plot.bar(ax=axs[i], logy=ylog, color=colors, legend=False) t = '{}, {} = {:d}'.format(problems[i], x_col, action_nums[i]) axs[i].set_xlabel(t, fontsize=12) axs[i].set_xticks([]) despine(axs[i]) legt = 'Searchers' new_lgd = p1 == 3 and excluded is not None if new_lgd: # Modify the legend to indicate excluded searches # (bc colormap is identical to fig1/2, but some runs have no data). legt += ' (X :: excluded)' excluded_len = len(excluded) x_idx = [excluded[i][0]-1 for i in range(excluded_len)] legend_patches = [] for i, c in enumerate(colors): lab = search[i] if new_lgd: if SEARCHES.index(lab) in x_idx: lab = lab.replace(' ', ' + ') lab += ' X' else: lab = lab.replace(' ', ' + ') else: lab = lab.replace(' ', ' + ') legend_patches.append(mpatches.Patch(color=c, label=lab)) axs[1].legend(handles=legend_patches, title=legt, title_fontsize='14', fontsize='medium', bbox_to_anchor=legend_bbox, loc='upper left', labelspacing=0.6, fancybox=True) plt.tight_layout() if to_file: plt.savefig(to_file) if show: return axs def format_multiples(multi): s = '' for i in range(len(multi)): s += '{'+ str(i) +':s}, ' s = s[:-2] return '[' + s.format(*multi.values) + ']' def order_analysis(df2, df1, column_to_compare): """ df2: has the large values. """ colA_larger_values = df2[column_to_compare] colA_smaller_values = df1[column_to_compare] # orders of magnitude difference btw dfB and dfA (min, max): mag = np.round(np.log(colA_larger_values/colA_smaller_values), 0) mag.sort_values(ascending=False, inplace=True) mag_aver = int(np.round(mag.mean(), 0)) # get the indices of values above average: ma = mag[mag > mag_aver].index.tolist() # get the names of all searchers corresponding to the ma: above_multiples = (mag_aver, df2.loc[ma, 'Searcher']) return above_multiples def comparison_paragraph(df2, df1, heading, column_to_compare, return_html=False): p1 = df1.loc[0,'Problem'][-1] p2 = df2.loc[0,'Problem'][-1] order_aver, searches_above = order_analysis(df2, df1, column_to_compare) above = format_multiples(searches_above) headinglc = heading.lower() text = f"""<h3>* {heading}</h3><p style="font-size:110%;">For Problems {p1} and {p2}, """ text += f"the <i>average</i> order of magnitude difference in {headinglc} is " text += f"<b>{order_aver:d}</b>, which is surpassed by these searches: {above}.</p>" if return_html: return text else: return Markdown(text) def get_elim_candidates(df2, df1): """ For the analysis of problems 1 & 2. List the costliest searches: candidates for elimination on more complex problems. """ if df1.loc[1,'Problem']!= problems[0]: return nodes_order_av, nodes_above = order_analysis(df2, df1, 'NewNodes') time_order_av, time_above = order_analysis(df2, df1, 'ElapsedSeconds') elim_candidates = set(nodes_above[:nodes_order_av]).intersection(set(time_above[:time_order_av])) # return their 1-base index also: out = [(SEARCHES.index(c)+1, c) for c in elim_candidates] return out def paragraph_p12(candidates_tup, return_html=False): """ For displaying the analysis of problems 1 & 2. """ elim_list = "" for i, c in candidates_tup: elim_list += f"<dt><b>{i:>2}: {c}</b></dt>" text = """<h3>* Insights from Problems 1 and 2</h3><p style="font-size:110%;">""" text += """On the basis of Figures 1 and 2, which show the number of new nodes created, and the time spent by each search function, respectively, the searches that are candidates for elimination for more complex problems are those at the intersection of the average-ranked costliest sets viz new nodes creation and search time.<br>These searches are:</p><pre><dl>""" text += f"<dl>{elim_list}</dl></p></pre>" if return_html: return text else: return Markdown(text) def add_div_around_html(div_html_text, output_string=False, div_style="{width: 80%}"): """ Wrap an html code str inside a div. div_style: whatever follows style= within the <div> Behaviour with `output_string=True`: The cell is overwritten with the output string (but the cell mode is still in 'code' not 'markdown') The only thing to do is change the cell mode to Markdown. If `output_string=False`, the HTML/md output is displayed in an output cell. """ div = f"""<div style="{div_style}">{div_html_text}</div>""" if output_string: return div #get_ipython().set_next_input(div, 'markdown') else: return Markdown(div)
df = df.merge(specs) df['index'] = df['Searcher'].apply(lambda x: SEARCHES.index(x)+1) df['index'] = df['index'].astype(int) df.set_index('index', drop=True, inplace=True) dflist.append(df) del df
conditional_block
report.py
import numpy as np import pandas as pd from pathlib import Path import matplotlib as mpl from matplotlib import pyplot as plt plt.style.use('seaborn-muted') #from IPython import get_ipython from IPython.display import HTML, Markdown import air_cargo_problems as acp problems = ['Air Cargo Problem 1', 'Air Cargo Problem 2', 'Air Cargo Problem 3', 'Air Cargo Problem 4'] SEARCHES = ['breadth_first_search', 'depth_first_graph_search', 'uniform_cost_search', 'greedy_best_first_graph_search h_unmet_goals', 'greedy_best_first_graph_search h_pg_levelsum', 'greedy_best_first_graph_search h_pg_maxlevel', 'greedy_best_first_graph_search h_pg_setlevel', 'astar_search h_unmet_goals', 'astar_search h_pg_levelsum', 'astar_search h_pg_maxlevel', 'astar_search h_pg_setlevel'] def get_prob_specs(): Probs = [acp.air_cargo_p1(), acp.air_cargo_p2(), acp.air_cargo_p3(), acp.air_cargo_p4()] problems_specs = {'Problem': [name for name in problems], 'Air cargo problem': [i+1 for i in range(len(problems))], 'Cargos': [len(p.cargos) for p in Probs], 'Planes': [len(p.planes) for p in Probs], 'Airports': [len(p.airports) for p in Probs], 'Goal': [len(p.goal) for p in Probs]} return pd.DataFrame(problems_specs) specs = get_prob_specs() def df2tsv(df, fname, replace=False): if Path(fname).exists(): if replace: df.to_csv(fname, sep='\t') #else: # print(f'File {fname} not replaced.') return df.to_csv(fname, sep='\t') return def get_problem_data_df(file_stem, problem, raw_dir, out_dir, file_as_tsv=False, replace=False): """ Combine all processed files of a problem found in Path(data_dir) with given stem. The file to be saved to/retrieved from out_dir is passed in file_as_tsv, tab separated csv. Input example: file_stem = 'prob_2' problem = 'Air Cargo Problem 2' Output: a dataframe, saved to tsv if file_as_tsv=True and not replace; saved as file_stem+'_df.csv'. """ if file_stem is None or problem is None: print('file_stem and problem must have a value.') return t = '\t' # input/output file suffixes: sfx = ['.csv', '_df.csv'] # Try retrieving it from out_dir if not replacing it: fout = None if file_as_tsv: fout = Path(out_dir).joinpath(file_stem + sfx[1]) if fout.exists() and not replace: df = pd.read_csv(fout, sep=t) try: return df.drop('Unnamed: 0', axis=1) except KeyError: pass # else: (re)process pfiles = list(Path(raw_dir).glob(file_stem + '*')) if len(pfiles) == 0: print(f'No raw files with stem: {file_stem}') return dflist = [] for f in pfiles: df, err = get_results_df(f, problem) if df is not None: df = df.merge(specs) df['index'] = df['Searcher'].apply(lambda x: SEARCHES.index(x)+1) df['index'] = df['index'].astype(int) df.set_index('index', drop=True, inplace=True) dflist.append(df) del df else: print(f'Error from get_results_df:\n\t{err}') dfout = pd.concat(dflist, ignore_index=False) dfout.sort_index(inplace=True) if file_as_tsv: df2tsv(dfout, fout, replace=replace) return dfout def get_results_df(fname, problem): """Process csv into dataframe. """ t = '\t' # Cols to add: val_cols = ['Actions','Expansions','GoalTests','NewNodes','PlanLength','ElapsedSeconds'] err = '' df = pd.read_csv(fname, sep=t) if df.shape[0] < len(val_cols): err = f'Data for {fname.name} is incomplete.' return None, err # Rename cols: c (temp) -> Searcher df.columns = ['c', 'Searcher'] # Add new cols & reindex df = df.reindex(columns = df.columns.tolist() + val_cols) # Populate new cols according to row with search name: sr = df.loc[df.c == 'Searcher', 'Searcher'] for (idx, sr_row) in sr.items(): j = idx for c in df.columns[2:].tolist(): j += 1 if c == 'ElapsedSeconds': df.loc[idx, c] = float(df.loc[j, 'Searcher']) else: df.loc[idx, c] = int(df.loc[j, 'Searcher']) df.dropna(inplace=True) # Add a minute column: df['Minutes'] = np.round(df.ElapsedSeconds/60, 3) # Replace values of 1st col with problem name & update col name: df['c'] = problem df.rename(columns={'c': 'Problem'}, inplace=True) df.reset_index(drop=True, inplace=True) return df, '' def concat_all_dfs(dflist): """ Output combined df for complete runs, Actions>0. """ dfall = pd.concat(dflist, ignore_index=False) dfall.reset_index(drop=False, inplace=True) dfall.rename(columns={'index': 'id'}, inplace=True) # reduced drop_cols = dfall.columns[-4:-1].tolist() + ['Problem','Minutes','GoalTests'] dfa = dfall.drop(drop_cols, axis=1) del dfall # add col for function name dfa['search_fn'] = dfa.Searcher.str.partition(' ')[0] # reorder cols dfa = dfa[['Air cargo problem','id','search_fn','Searcher','Actions', 'PlanLength', 'NewNodes','Expansions','ElapsedSeconds']] # complete runs only: return dfa[dfa['Actions'].values > 0] def plans_length(dfa, which): """ dfa: frame of concatenated df1 to df4. Analysis of plan length for which in ['double', 'single']: PlanLength is double(single)-digit. """ if which == 'double': msk = dfa.PlanLength >= 10 col2 = 'Frequency where PlanLength >=10' else: msk = dfa.PlanLength < 10 col2 = 'Frequency where PlanLength <10' dfa_rows = dfa.shape[0] dfout = dfa[msk].sort_values(['PlanLength'], ascending=False) uniq_probs = dfout['Air cargo problem'].unique() n_plans = dfout.shape[0] searcher_cnt = dfout['Searcher'].value_counts() fn_cnt = dfout['search_fn'].value_counts() # get the html string: df_fn = fn_cnt.to_frame() df_fn.reset_index(drop=False, inplace=True) df_fn.columns = ['Search function', col2] df_fn_html = df_fn.to_html(index=False, justify='center') replace_str1 = ' style="text-align: center;"' replace_str2 = 'class="dataframe"' df_fn_html = df_fn_html.replace(replace_str1, '') df_fn_html = df_fn_html.replace(replace_str2, replace_str1) pct_plans = n_plans/dfa_rows top2_fn = fn_cnt[0:2].sum() pct_top2_fn = top2_fn/n_plans text = f"Out of {dfa_rows} completed searches, {pct_plans:.0%} ({n_plans}), have {which}-digit or longer PlanLength.<br>" text += f"In that subset, {top2_fn:d} ({pct_top2_fn:.0%}) involve the search functions `{fn_cnt.index[0]}` and `{fn_cnt.index[1]}`." if len(uniq_probs) < 4: text += " And this occurs only for Problems: " pro = ",".join('{}' for p in uniq_probs) +'.<br>' text += pro.format(*uniq_probs) else: text += " And this occurs for all Problems." text += "<br>" return df_fn_html, text, dfout def make_bar_plots(df_list, x_col, y_col, problems, legend_bbox=(.05, .95), to_file='', show=False, excluded=None): """ To get 2 bar plots in a row. """ import matplotlib.patches as mpatches def despine(ax): ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) a1 = df_list[0][x_col].unique().astype(int) a1 = a1[a1>0] a2 = df_list[1][x_col].unique().astype(int) a2 = a2[a2>0] assert len(a1) == len(a2) == 1 action_nums = [a1[0], a2[0]] p1 = df_list[0]['Air cargo problem'].iloc[0] p2 = df_list[1]['Air cargo problem'].iloc[0] # Seach functions names should be common to all dfs: search = df_list[0].Searcher.tolist() # Sample cmap according to categories: s_len = len(search) cmap = plt.get_cmap('viridis') m = cmap.N // s_len colors = [cmap.colors[i*m] for i in range(s_len)] fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12,5)) # Use the minutes columns for the more complex problems: if y_col == 'ElapsedSeconds': ty_col = 'Elapsed time' if p1 == 3 or p == 4: # applies to problems 3/4 y_col = 'Minutes' else: ty_col = y_col plt.title(f'{ty_col} vs. {x_col} for Problems {p1} & {p2}', y = 1.05, fontsize=14) for i, df in enumerate(df_list): ylog = False ylab = f'{y_col}' # log scale on NewNodes for df2, df3, df4: if (i == 1 or p1 == 3) and y_col == 'NewNodes': ylog = True ylab += ' (log)' axs[i].set_ylabel(ylab, fontsize=12) df[y_col].plot.bar(ax=axs[i], logy=ylog, color=colors, legend=False) t = '{}, {} = {:d}'.format(problems[i], x_col, action_nums[i]) axs[i].set_xlabel(t, fontsize=12) axs[i].set_xticks([]) despine(axs[i]) legt = 'Searchers' new_lgd = p1 == 3 and excluded is not None if new_lgd: # Modify the legend to indicate excluded searches # (bc colormap is identical to fig1/2, but some runs have no data). legt += ' (X :: excluded)' excluded_len = len(excluded) x_idx = [excluded[i][0]-1 for i in range(excluded_len)] legend_patches = [] for i, c in enumerate(colors): lab = search[i] if new_lgd: if SEARCHES.index(lab) in x_idx: lab = lab.replace(' ', ' + ') lab += ' X' else: lab = lab.replace(' ', ' + ') else: lab = lab.replace(' ', ' + ') legend_patches.append(mpatches.Patch(color=c, label=lab)) axs[1].legend(handles=legend_patches, title=legt, title_fontsize='14', fontsize='medium', bbox_to_anchor=legend_bbox, loc='upper left', labelspacing=0.6, fancybox=True) plt.tight_layout() if to_file: plt.savefig(to_file) if show: return axs def format_multiples(multi): s = '' for i in range(len(multi)): s += '{'+ str(i) +':s}, ' s = s[:-2] return '[' + s.format(*multi.values) + ']' def order_analysis(df2, df1, column_to_compare): """ df2: has the large values. """ colA_larger_values = df2[column_to_compare] colA_smaller_values = df1[column_to_compare] # orders of magnitude difference btw dfB and dfA (min, max): mag = np.round(np.log(colA_larger_values/colA_smaller_values), 0) mag.sort_values(ascending=False, inplace=True) mag_aver = int(np.round(mag.mean(), 0)) # get the indices of values above average: ma = mag[mag > mag_aver].index.tolist() # get the names of all searchers corresponding to the ma: above_multiples = (mag_aver, df2.loc[ma, 'Searcher']) return above_multiples def comparison_paragraph(df2, df1, heading, column_to_compare, return_html=False): p1 = df1.loc[0,'Problem'][-1] p2 = df2.loc[0,'Problem'][-1] order_aver, searches_above = order_analysis(df2, df1, column_to_compare) above = format_multiples(searches_above) headinglc = heading.lower() text = f"""<h3>* {heading}</h3><p style="font-size:110%;">For Problems {p1} and {p2}, """ text += f"the <i>average</i> order of magnitude difference in {headinglc} is " text += f"<b>{order_aver:d}</b>, which is surpassed by these searches: {above}.</p>" if return_html: return text else: return Markdown(text) def get_elim_candidates(df2, df1): """ For the analysis of problems 1 & 2. List the costliest searches: candidates for elimination on more complex problems. """ if df1.loc[1,'Problem']!= problems[0]: return nodes_order_av, nodes_above = order_analysis(df2, df1, 'NewNodes') time_order_av, time_above = order_analysis(df2, df1, 'ElapsedSeconds') elim_candidates = set(nodes_above[:nodes_order_av]).intersection(set(time_above[:time_order_av])) # return their 1-base index also: out = [(SEARCHES.index(c)+1, c) for c in elim_candidates] return out def paragraph_p12(candidates_tup, return_html=False): """ For displaying the analysis of problems 1 & 2. """ elim_list = "" for i, c in candidates_tup: elim_list += f"<dt><b>{i:>2}: {c}</b></dt>" text = """<h3>* Insights from Problems 1 and 2</h3><p style="font-size:110%;">""" text += """On the basis of Figures 1 and 2, which show the number of new nodes created, and the time spent by each search function, respectively, the searches that are candidates for elimination for more complex problems are those at the intersection of the average-ranked costliest sets viz new nodes creation and search time.<br>These searches are:</p><pre><dl>""" text += f"<dl>{elim_list}</dl></p></pre>" if return_html: return text else: return Markdown(text) def add_div_around_html(div_html_text, output_string=False, div_style="{width: 80%}"):
""" Wrap an html code str inside a div. div_style: whatever follows style= within the <div> Behaviour with `output_string=True`: The cell is overwritten with the output string (but the cell mode is still in 'code' not 'markdown') The only thing to do is change the cell mode to Markdown. If `output_string=False`, the HTML/md output is displayed in an output cell. """ div = f"""<div style="{div_style}">{div_html_text}</div>""" if output_string: return div #get_ipython().set_next_input(div, 'markdown') else: return Markdown(div)
identifier_body
report.py
import numpy as np import pandas as pd from pathlib import Path import matplotlib as mpl from matplotlib import pyplot as plt plt.style.use('seaborn-muted') #from IPython import get_ipython from IPython.display import HTML, Markdown import air_cargo_problems as acp problems = ['Air Cargo Problem 1', 'Air Cargo Problem 2', 'Air Cargo Problem 3', 'Air Cargo Problem 4'] SEARCHES = ['breadth_first_search', 'depth_first_graph_search', 'uniform_cost_search', 'greedy_best_first_graph_search h_unmet_goals', 'greedy_best_first_graph_search h_pg_levelsum', 'greedy_best_first_graph_search h_pg_maxlevel', 'greedy_best_first_graph_search h_pg_setlevel', 'astar_search h_unmet_goals', 'astar_search h_pg_levelsum', 'astar_search h_pg_maxlevel', 'astar_search h_pg_setlevel'] def get_prob_specs(): Probs = [acp.air_cargo_p1(), acp.air_cargo_p2(), acp.air_cargo_p3(), acp.air_cargo_p4()] problems_specs = {'Problem': [name for name in problems], 'Air cargo problem': [i+1 for i in range(len(problems))], 'Cargos': [len(p.cargos) for p in Probs], 'Planes': [len(p.planes) for p in Probs], 'Airports': [len(p.airports) for p in Probs], 'Goal': [len(p.goal) for p in Probs]} return pd.DataFrame(problems_specs) specs = get_prob_specs() def df2tsv(df, fname, replace=False): if Path(fname).exists(): if replace: df.to_csv(fname, sep='\t') #else: # print(f'File {fname} not replaced.') return df.to_csv(fname, sep='\t') return def get_problem_data_df(file_stem, problem, raw_dir, out_dir, file_as_tsv=False, replace=False): """ Combine all processed files of a problem found in Path(data_dir) with given stem. The file to be saved to/retrieved from out_dir is passed in file_as_tsv, tab separated csv. Input example: file_stem = 'prob_2' problem = 'Air Cargo Problem 2' Output: a dataframe, saved to tsv if file_as_tsv=True and not replace; saved as file_stem+'_df.csv'. """ if file_stem is None or problem is None: print('file_stem and problem must have a value.') return t = '\t' # input/output file suffixes: sfx = ['.csv', '_df.csv'] # Try retrieving it from out_dir if not replacing it: fout = None if file_as_tsv: fout = Path(out_dir).joinpath(file_stem + sfx[1]) if fout.exists() and not replace: df = pd.read_csv(fout, sep=t) try: return df.drop('Unnamed: 0', axis=1) except KeyError: pass # else: (re)process pfiles = list(Path(raw_dir).glob(file_stem + '*')) if len(pfiles) == 0: print(f'No raw files with stem: {file_stem}') return dflist = [] for f in pfiles: df, err = get_results_df(f, problem) if df is not None: df = df.merge(specs) df['index'] = df['Searcher'].apply(lambda x: SEARCHES.index(x)+1) df['index'] = df['index'].astype(int) df.set_index('index', drop=True, inplace=True) dflist.append(df) del df else: print(f'Error from get_results_df:\n\t{err}') dfout = pd.concat(dflist, ignore_index=False) dfout.sort_index(inplace=True) if file_as_tsv: df2tsv(dfout, fout, replace=replace) return dfout def get_results_df(fname, problem): """Process csv into dataframe. """ t = '\t' # Cols to add: val_cols = ['Actions','Expansions','GoalTests','NewNodes','PlanLength','ElapsedSeconds'] err = '' df = pd.read_csv(fname, sep=t) if df.shape[0] < len(val_cols): err = f'Data for {fname.name} is incomplete.' return None, err # Rename cols: c (temp) -> Searcher df.columns = ['c', 'Searcher'] # Add new cols & reindex df = df.reindex(columns = df.columns.tolist() + val_cols) # Populate new cols according to row with search name: sr = df.loc[df.c == 'Searcher', 'Searcher'] for (idx, sr_row) in sr.items(): j = idx for c in df.columns[2:].tolist(): j += 1 if c == 'ElapsedSeconds': df.loc[idx, c] = float(df.loc[j, 'Searcher']) else: df.loc[idx, c] = int(df.loc[j, 'Searcher']) df.dropna(inplace=True) # Add a minute column: df['Minutes'] = np.round(df.ElapsedSeconds/60, 3) # Replace values of 1st col with problem name & update col name: df['c'] = problem df.rename(columns={'c': 'Problem'}, inplace=True) df.reset_index(drop=True, inplace=True) return df, '' def
(dflist): """ Output combined df for complete runs, Actions>0. """ dfall = pd.concat(dflist, ignore_index=False) dfall.reset_index(drop=False, inplace=True) dfall.rename(columns={'index': 'id'}, inplace=True) # reduced drop_cols = dfall.columns[-4:-1].tolist() + ['Problem','Minutes','GoalTests'] dfa = dfall.drop(drop_cols, axis=1) del dfall # add col for function name dfa['search_fn'] = dfa.Searcher.str.partition(' ')[0] # reorder cols dfa = dfa[['Air cargo problem','id','search_fn','Searcher','Actions', 'PlanLength', 'NewNodes','Expansions','ElapsedSeconds']] # complete runs only: return dfa[dfa['Actions'].values > 0] def plans_length(dfa, which): """ dfa: frame of concatenated df1 to df4. Analysis of plan length for which in ['double', 'single']: PlanLength is double(single)-digit. """ if which == 'double': msk = dfa.PlanLength >= 10 col2 = 'Frequency where PlanLength >=10' else: msk = dfa.PlanLength < 10 col2 = 'Frequency where PlanLength <10' dfa_rows = dfa.shape[0] dfout = dfa[msk].sort_values(['PlanLength'], ascending=False) uniq_probs = dfout['Air cargo problem'].unique() n_plans = dfout.shape[0] searcher_cnt = dfout['Searcher'].value_counts() fn_cnt = dfout['search_fn'].value_counts() # get the html string: df_fn = fn_cnt.to_frame() df_fn.reset_index(drop=False, inplace=True) df_fn.columns = ['Search function', col2] df_fn_html = df_fn.to_html(index=False, justify='center') replace_str1 = ' style="text-align: center;"' replace_str2 = 'class="dataframe"' df_fn_html = df_fn_html.replace(replace_str1, '') df_fn_html = df_fn_html.replace(replace_str2, replace_str1) pct_plans = n_plans/dfa_rows top2_fn = fn_cnt[0:2].sum() pct_top2_fn = top2_fn/n_plans text = f"Out of {dfa_rows} completed searches, {pct_plans:.0%} ({n_plans}), have {which}-digit or longer PlanLength.<br>" text += f"In that subset, {top2_fn:d} ({pct_top2_fn:.0%}) involve the search functions `{fn_cnt.index[0]}` and `{fn_cnt.index[1]}`." if len(uniq_probs) < 4: text += " And this occurs only for Problems: " pro = ",".join('{}' for p in uniq_probs) +'.<br>' text += pro.format(*uniq_probs) else: text += " And this occurs for all Problems." text += "<br>" return df_fn_html, text, dfout def make_bar_plots(df_list, x_col, y_col, problems, legend_bbox=(.05, .95), to_file='', show=False, excluded=None): """ To get 2 bar plots in a row. """ import matplotlib.patches as mpatches def despine(ax): ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) a1 = df_list[0][x_col].unique().astype(int) a1 = a1[a1>0] a2 = df_list[1][x_col].unique().astype(int) a2 = a2[a2>0] assert len(a1) == len(a2) == 1 action_nums = [a1[0], a2[0]] p1 = df_list[0]['Air cargo problem'].iloc[0] p2 = df_list[1]['Air cargo problem'].iloc[0] # Seach functions names should be common to all dfs: search = df_list[0].Searcher.tolist() # Sample cmap according to categories: s_len = len(search) cmap = plt.get_cmap('viridis') m = cmap.N // s_len colors = [cmap.colors[i*m] for i in range(s_len)] fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12,5)) # Use the minutes columns for the more complex problems: if y_col == 'ElapsedSeconds': ty_col = 'Elapsed time' if p1 == 3 or p == 4: # applies to problems 3/4 y_col = 'Minutes' else: ty_col = y_col plt.title(f'{ty_col} vs. {x_col} for Problems {p1} & {p2}', y = 1.05, fontsize=14) for i, df in enumerate(df_list): ylog = False ylab = f'{y_col}' # log scale on NewNodes for df2, df3, df4: if (i == 1 or p1 == 3) and y_col == 'NewNodes': ylog = True ylab += ' (log)' axs[i].set_ylabel(ylab, fontsize=12) df[y_col].plot.bar(ax=axs[i], logy=ylog, color=colors, legend=False) t = '{}, {} = {:d}'.format(problems[i], x_col, action_nums[i]) axs[i].set_xlabel(t, fontsize=12) axs[i].set_xticks([]) despine(axs[i]) legt = 'Searchers' new_lgd = p1 == 3 and excluded is not None if new_lgd: # Modify the legend to indicate excluded searches # (bc colormap is identical to fig1/2, but some runs have no data). legt += ' (X :: excluded)' excluded_len = len(excluded) x_idx = [excluded[i][0]-1 for i in range(excluded_len)] legend_patches = [] for i, c in enumerate(colors): lab = search[i] if new_lgd: if SEARCHES.index(lab) in x_idx: lab = lab.replace(' ', ' + ') lab += ' X' else: lab = lab.replace(' ', ' + ') else: lab = lab.replace(' ', ' + ') legend_patches.append(mpatches.Patch(color=c, label=lab)) axs[1].legend(handles=legend_patches, title=legt, title_fontsize='14', fontsize='medium', bbox_to_anchor=legend_bbox, loc='upper left', labelspacing=0.6, fancybox=True) plt.tight_layout() if to_file: plt.savefig(to_file) if show: return axs def format_multiples(multi): s = '' for i in range(len(multi)): s += '{'+ str(i) +':s}, ' s = s[:-2] return '[' + s.format(*multi.values) + ']' def order_analysis(df2, df1, column_to_compare): """ df2: has the large values. """ colA_larger_values = df2[column_to_compare] colA_smaller_values = df1[column_to_compare] # orders of magnitude difference btw dfB and dfA (min, max): mag = np.round(np.log(colA_larger_values/colA_smaller_values), 0) mag.sort_values(ascending=False, inplace=True) mag_aver = int(np.round(mag.mean(), 0)) # get the indices of values above average: ma = mag[mag > mag_aver].index.tolist() # get the names of all searchers corresponding to the ma: above_multiples = (mag_aver, df2.loc[ma, 'Searcher']) return above_multiples def comparison_paragraph(df2, df1, heading, column_to_compare, return_html=False): p1 = df1.loc[0,'Problem'][-1] p2 = df2.loc[0,'Problem'][-1] order_aver, searches_above = order_analysis(df2, df1, column_to_compare) above = format_multiples(searches_above) headinglc = heading.lower() text = f"""<h3>* {heading}</h3><p style="font-size:110%;">For Problems {p1} and {p2}, """ text += f"the <i>average</i> order of magnitude difference in {headinglc} is " text += f"<b>{order_aver:d}</b>, which is surpassed by these searches: {above}.</p>" if return_html: return text else: return Markdown(text) def get_elim_candidates(df2, df1): """ For the analysis of problems 1 & 2. List the costliest searches: candidates for elimination on more complex problems. """ if df1.loc[1,'Problem']!= problems[0]: return nodes_order_av, nodes_above = order_analysis(df2, df1, 'NewNodes') time_order_av, time_above = order_analysis(df2, df1, 'ElapsedSeconds') elim_candidates = set(nodes_above[:nodes_order_av]).intersection(set(time_above[:time_order_av])) # return their 1-base index also: out = [(SEARCHES.index(c)+1, c) for c in elim_candidates] return out def paragraph_p12(candidates_tup, return_html=False): """ For displaying the analysis of problems 1 & 2. """ elim_list = "" for i, c in candidates_tup: elim_list += f"<dt><b>{i:>2}: {c}</b></dt>" text = """<h3>* Insights from Problems 1 and 2</h3><p style="font-size:110%;">""" text += """On the basis of Figures 1 and 2, which show the number of new nodes created, and the time spent by each search function, respectively, the searches that are candidates for elimination for more complex problems are those at the intersection of the average-ranked costliest sets viz new nodes creation and search time.<br>These searches are:</p><pre><dl>""" text += f"<dl>{elim_list}</dl></p></pre>" if return_html: return text else: return Markdown(text) def add_div_around_html(div_html_text, output_string=False, div_style="{width: 80%}"): """ Wrap an html code str inside a div. div_style: whatever follows style= within the <div> Behaviour with `output_string=True`: The cell is overwritten with the output string (but the cell mode is still in 'code' not 'markdown') The only thing to do is change the cell mode to Markdown. If `output_string=False`, the HTML/md output is displayed in an output cell. """ div = f"""<div style="{div_style}">{div_html_text}</div>""" if output_string: return div #get_ipython().set_next_input(div, 'markdown') else: return Markdown(div)
concat_all_dfs
identifier_name
vpc_controller.go
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package controllers import ( "context" "k8s.io/client-go/tools/record" "log" "strings" "tencent-cloud-operator/internal/common" "tencent-cloud-operator/internal/utils" "time" tcerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" tcvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" networkv1alpha1 "tencent-cloud-operator/apis/network/v1alpha1" ) // VpcReconciler reconciles a Vpc object type VpcReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme Recorder *record.EventRecorder } // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs/status,verbs=get;update;patch func (r *VpcReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { _ = context.Background() _ = r.Log.WithValues("vpc", req.String()) ctx := context.Background() // get the vpc object vpc := &networkv1alpha1.Vpc{} err := r.Get(ctx, req.NamespacedName, vpc) if err != nil { if errors.IsNotFound(err) { log.Printf("Request object not found, could have been deleted after reconcile request.") // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue return reconcile.Result{}, nil } log.Println("error reading the object, requeue") return ctrl.Result{}, err } log.Println("found the vpc", *vpc.Spec.VpcName) if vpc.Status.ResourceStatus == nil { vpc.Status.ResourceStatus = new(common.ResourceStatus) vpc.Status.ResourceStatus.Status = new(string) vpc.Status.ResourceStatus.Reason = new(string) vpc.Status.ResourceStatus.RetryCount = new(int) vpc.Status.ResourceStatus.Code = new(string) vpc.Status.ResourceStatus.LastRetry = new(string) } if vpc.Status.VpcId == nil { vpc.Status.VpcId = new(string) } err = r.vpcReconcile(vpc) if err != nil { *vpc.Status.ResourceStatus.Status = "ERROR" *vpc.Status.ResourceStatus.LastRetry = time.Now().UTC().Format("2006-01-02T15:04:05") *vpc.Status.ResourceStatus.RetryCount += 1 if cloudError, ok := err.(*tcerrors.TencentCloudSDKError); ok { *vpc.Status.ResourceStatus.Code = cloudError.Code *vpc.Status.ResourceStatus.Reason = cloudError.Message } _ = r.Update(context.TODO(), vpc) return ctrl.Result{RequeueAfter: common.RequeueInterval}, err } return ctrl.Result{RequeueAfter: common.RequeueInterval}, nil } func (r *VpcReconciler) vpcReconcile(vpc *networkv1alpha1.Vpc) error { // always check for finalizers deleted := !vpc.GetDeletionTimestamp().IsZero() pendingFinalizers := vpc.GetFinalizers() finalizerExists := len(pendingFinalizers) > 0 if !finalizerExists && !deleted && !utils.Contains(pendingFinalizers, common.Finalizer) { log.Println("Adding finalized &s to resource", common.Finalizer) finalizers := append(pendingFinalizers, common.Finalizer) vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "" || vpc.Status.ResourceStatus.Status == nil { *vpc.Status.ResourceStatus.Status = "PROCESSING" return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "PROCESSING" { return r.createVpc(vpc) } log.Printf("vpc %s is in %s status", *vpc.Spec.VpcName, *vpc.Status.ResourceStatus.Status) tencentVpc, err := r.getVpc(vpc) // err get resource from cloud, and resource not marked as deleted, something wrong if err != nil { log.Printf("error retrive vpc %s status from tencent cloud, just requeue for retry", *vpc.Spec.VpcName) return err } if deleted { // resource marked as deleted, but status not in deleting or error state, update the state to deleting if !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "DELETING") && !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { *vpc.Status.ResourceStatus.Status = "DELETING" return r.Update(context.TODO(), vpc) } if tencentVpc != nil { // resource is marked to be deleted, cloud resource still exists lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { err = r.deleteVpc(vpc) if err != nil { r.Log.Info("error delete vpc", "namespace:", vpc.Namespace, "name:", *vpc.Spec.VpcName) //error delete the resource from cloud, don't remove finalizer yet return err } } } // resource deleted from cloud, remove finalizer finalizers := make([]string, 0) pendingFinalizers = vpc.GetFinalizers() for _, pendingFinalizer := range pendingFinalizers { if pendingFinalizer != common.Finalizer { finalizers = append(finalizers, pendingFinalizer) } } vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } //resource not marked as deleted, and get error status, try to create the resource in cloud if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { // resource in error status, retry create if vpc.Status.VpcId == nil || *vpc.Status.VpcId == ""
} } //resource deleted in cloud, update the status if tencentVpc == nil { if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "DELETED_IN_CLOUD" return r.Update(context.TODO(), vpc) } return nil } //get resource from tencent cloud, and resource not marked as deleted, update status if !strings.EqualFold(*vpc.Status.ResourceStatus.Code, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Reason, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { vpc.Status.VpcId = tencentVpc.VpcId *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } return nil } func (r *VpcReconciler) createVpc(vpc *networkv1alpha1.Vpc) error { tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewCreateVpcRequest() request.VpcName = vpc.Spec.VpcName request.CidrBlock = vpc.Spec.CidrBlock request.EnableMulticast = vpc.Spec.EnableMulticast request.DnsServers = vpc.Spec.DnsServers request.DomainName = vpc.Spec.DomainName for _, tag := range vpc.Spec.Tags { request.Tags = append(request.Tags, &tcvpc.Tag{ Key: tag.Key, Value: tag.Value, }) } resp, err := tencentClient.CreateVpc(request) if err != nil { return err } vpc.Status.VpcId = resp.Response.Vpc.VpcId *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } func (r *VpcReconciler) getVpc(vpc *networkv1alpha1.Vpc) (*tcvpc.Vpc, error) { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil, errors.NewBadRequest("vpc id not found") } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDescribeVpcsRequest() request.VpcIds = append(request.VpcIds, vpc.Status.VpcId) resp, err := tencentClient.DescribeVpcs(request) if err != nil { log.Println("failed to get vpc from tencent cloud, requeue") return nil, err } if *resp.Response.TotalCount == 0 { log.Println("Resource is deleted from cloud") return nil, nil } return resp.Response.VpcSet[0], nil } func (r *VpcReconciler) deleteVpc(vpc *networkv1alpha1.Vpc) error { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDeleteVpcRequest() request.VpcId = vpc.Status.VpcId _, err := tencentClient.DeleteVpc(request) if err != nil { log.Println("failed to delete vpc from tencent cloud, requeue") return err } return nil } func ignoreUpdatePredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { log.Println("update event") log.Println("old meta:", e.MetaOld.GetGeneration(), "new meta:", e.MetaNew.GetGeneration()) // Ignore updates to CR status in which case metadata.Generation does not change return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() }, DeleteFunc: func(e event.DeleteEvent) bool { log.Println("delete event") // Evaluates to false if the object has been confirmed deleted. return !e.DeleteStateUnknown }, } } func (r *VpcReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&networkv1alpha1.Vpc{}). WithEventFilter(ignoreUpdatePredicate()). Complete(r) }
{ r.Log.Info("vpc is in error status, and vpc id is empty, retry create") return r.createVpc(vpc) }
conditional_block
vpc_controller.go
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package controllers import ( "context" "k8s.io/client-go/tools/record" "log" "strings" "tencent-cloud-operator/internal/common" "tencent-cloud-operator/internal/utils" "time" tcerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" tcvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" networkv1alpha1 "tencent-cloud-operator/apis/network/v1alpha1" ) // VpcReconciler reconciles a Vpc object type VpcReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme Recorder *record.EventRecorder } // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs/status,verbs=get;update;patch func (r *VpcReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { _ = context.Background() _ = r.Log.WithValues("vpc", req.String()) ctx := context.Background() // get the vpc object vpc := &networkv1alpha1.Vpc{} err := r.Get(ctx, req.NamespacedName, vpc) if err != nil { if errors.IsNotFound(err) { log.Printf("Request object not found, could have been deleted after reconcile request.") // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue return reconcile.Result{}, nil } log.Println("error reading the object, requeue") return ctrl.Result{}, err } log.Println("found the vpc", *vpc.Spec.VpcName) if vpc.Status.ResourceStatus == nil { vpc.Status.ResourceStatus = new(common.ResourceStatus) vpc.Status.ResourceStatus.Status = new(string) vpc.Status.ResourceStatus.Reason = new(string) vpc.Status.ResourceStatus.RetryCount = new(int) vpc.Status.ResourceStatus.Code = new(string) vpc.Status.ResourceStatus.LastRetry = new(string) } if vpc.Status.VpcId == nil { vpc.Status.VpcId = new(string) } err = r.vpcReconcile(vpc) if err != nil { *vpc.Status.ResourceStatus.Status = "ERROR" *vpc.Status.ResourceStatus.LastRetry = time.Now().UTC().Format("2006-01-02T15:04:05") *vpc.Status.ResourceStatus.RetryCount += 1 if cloudError, ok := err.(*tcerrors.TencentCloudSDKError); ok { *vpc.Status.ResourceStatus.Code = cloudError.Code *vpc.Status.ResourceStatus.Reason = cloudError.Message } _ = r.Update(context.TODO(), vpc) return ctrl.Result{RequeueAfter: common.RequeueInterval}, err } return ctrl.Result{RequeueAfter: common.RequeueInterval}, nil } func (r *VpcReconciler) vpcReconcile(vpc *networkv1alpha1.Vpc) error
func (r *VpcReconciler) createVpc(vpc *networkv1alpha1.Vpc) error { tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewCreateVpcRequest() request.VpcName = vpc.Spec.VpcName request.CidrBlock = vpc.Spec.CidrBlock request.EnableMulticast = vpc.Spec.EnableMulticast request.DnsServers = vpc.Spec.DnsServers request.DomainName = vpc.Spec.DomainName for _, tag := range vpc.Spec.Tags { request.Tags = append(request.Tags, &tcvpc.Tag{ Key: tag.Key, Value: tag.Value, }) } resp, err := tencentClient.CreateVpc(request) if err != nil { return err } vpc.Status.VpcId = resp.Response.Vpc.VpcId *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } func (r *VpcReconciler) getVpc(vpc *networkv1alpha1.Vpc) (*tcvpc.Vpc, error) { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil, errors.NewBadRequest("vpc id not found") } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDescribeVpcsRequest() request.VpcIds = append(request.VpcIds, vpc.Status.VpcId) resp, err := tencentClient.DescribeVpcs(request) if err != nil { log.Println("failed to get vpc from tencent cloud, requeue") return nil, err } if *resp.Response.TotalCount == 0 { log.Println("Resource is deleted from cloud") return nil, nil } return resp.Response.VpcSet[0], nil } func (r *VpcReconciler) deleteVpc(vpc *networkv1alpha1.Vpc) error { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDeleteVpcRequest() request.VpcId = vpc.Status.VpcId _, err := tencentClient.DeleteVpc(request) if err != nil { log.Println("failed to delete vpc from tencent cloud, requeue") return err } return nil } func ignoreUpdatePredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { log.Println("update event") log.Println("old meta:", e.MetaOld.GetGeneration(), "new meta:", e.MetaNew.GetGeneration()) // Ignore updates to CR status in which case metadata.Generation does not change return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() }, DeleteFunc: func(e event.DeleteEvent) bool { log.Println("delete event") // Evaluates to false if the object has been confirmed deleted. return !e.DeleteStateUnknown }, } } func (r *VpcReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&networkv1alpha1.Vpc{}). WithEventFilter(ignoreUpdatePredicate()). Complete(r) }
{ // always check for finalizers deleted := !vpc.GetDeletionTimestamp().IsZero() pendingFinalizers := vpc.GetFinalizers() finalizerExists := len(pendingFinalizers) > 0 if !finalizerExists && !deleted && !utils.Contains(pendingFinalizers, common.Finalizer) { log.Println("Adding finalized &s to resource", common.Finalizer) finalizers := append(pendingFinalizers, common.Finalizer) vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "" || vpc.Status.ResourceStatus.Status == nil { *vpc.Status.ResourceStatus.Status = "PROCESSING" return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "PROCESSING" { return r.createVpc(vpc) } log.Printf("vpc %s is in %s status", *vpc.Spec.VpcName, *vpc.Status.ResourceStatus.Status) tencentVpc, err := r.getVpc(vpc) // err get resource from cloud, and resource not marked as deleted, something wrong if err != nil { log.Printf("error retrive vpc %s status from tencent cloud, just requeue for retry", *vpc.Spec.VpcName) return err } if deleted { // resource marked as deleted, but status not in deleting or error state, update the state to deleting if !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "DELETING") && !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { *vpc.Status.ResourceStatus.Status = "DELETING" return r.Update(context.TODO(), vpc) } if tencentVpc != nil { // resource is marked to be deleted, cloud resource still exists lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { err = r.deleteVpc(vpc) if err != nil { r.Log.Info("error delete vpc", "namespace:", vpc.Namespace, "name:", *vpc.Spec.VpcName) //error delete the resource from cloud, don't remove finalizer yet return err } } } // resource deleted from cloud, remove finalizer finalizers := make([]string, 0) pendingFinalizers = vpc.GetFinalizers() for _, pendingFinalizer := range pendingFinalizers { if pendingFinalizer != common.Finalizer { finalizers = append(finalizers, pendingFinalizer) } } vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } //resource not marked as deleted, and get error status, try to create the resource in cloud if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { // resource in error status, retry create if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { r.Log.Info("vpc is in error status, and vpc id is empty, retry create") return r.createVpc(vpc) } } } //resource deleted in cloud, update the status if tencentVpc == nil { if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "DELETED_IN_CLOUD" return r.Update(context.TODO(), vpc) } return nil } //get resource from tencent cloud, and resource not marked as deleted, update status if !strings.EqualFold(*vpc.Status.ResourceStatus.Code, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Reason, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { vpc.Status.VpcId = tencentVpc.VpcId *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } return nil }
identifier_body
vpc_controller.go
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package controllers import ( "context" "k8s.io/client-go/tools/record" "log" "strings" "tencent-cloud-operator/internal/common" "tencent-cloud-operator/internal/utils" "time" tcerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" tcvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" networkv1alpha1 "tencent-cloud-operator/apis/network/v1alpha1" ) // VpcReconciler reconciles a Vpc object type VpcReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme Recorder *record.EventRecorder } // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs/status,verbs=get;update;patch func (r *VpcReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { _ = context.Background() _ = r.Log.WithValues("vpc", req.String()) ctx := context.Background() // get the vpc object vpc := &networkv1alpha1.Vpc{} err := r.Get(ctx, req.NamespacedName, vpc) if err != nil { if errors.IsNotFound(err) { log.Printf("Request object not found, could have been deleted after reconcile request.") // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue return reconcile.Result{}, nil } log.Println("error reading the object, requeue") return ctrl.Result{}, err } log.Println("found the vpc", *vpc.Spec.VpcName) if vpc.Status.ResourceStatus == nil { vpc.Status.ResourceStatus = new(common.ResourceStatus) vpc.Status.ResourceStatus.Status = new(string) vpc.Status.ResourceStatus.Reason = new(string) vpc.Status.ResourceStatus.RetryCount = new(int) vpc.Status.ResourceStatus.Code = new(string) vpc.Status.ResourceStatus.LastRetry = new(string) } if vpc.Status.VpcId == nil { vpc.Status.VpcId = new(string) } err = r.vpcReconcile(vpc) if err != nil { *vpc.Status.ResourceStatus.Status = "ERROR" *vpc.Status.ResourceStatus.LastRetry = time.Now().UTC().Format("2006-01-02T15:04:05") *vpc.Status.ResourceStatus.RetryCount += 1 if cloudError, ok := err.(*tcerrors.TencentCloudSDKError); ok { *vpc.Status.ResourceStatus.Code = cloudError.Code *vpc.Status.ResourceStatus.Reason = cloudError.Message } _ = r.Update(context.TODO(), vpc) return ctrl.Result{RequeueAfter: common.RequeueInterval}, err } return ctrl.Result{RequeueAfter: common.RequeueInterval}, nil } func (r *VpcReconciler)
(vpc *networkv1alpha1.Vpc) error { // always check for finalizers deleted := !vpc.GetDeletionTimestamp().IsZero() pendingFinalizers := vpc.GetFinalizers() finalizerExists := len(pendingFinalizers) > 0 if !finalizerExists && !deleted && !utils.Contains(pendingFinalizers, common.Finalizer) { log.Println("Adding finalized &s to resource", common.Finalizer) finalizers := append(pendingFinalizers, common.Finalizer) vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "" || vpc.Status.ResourceStatus.Status == nil { *vpc.Status.ResourceStatus.Status = "PROCESSING" return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "PROCESSING" { return r.createVpc(vpc) } log.Printf("vpc %s is in %s status", *vpc.Spec.VpcName, *vpc.Status.ResourceStatus.Status) tencentVpc, err := r.getVpc(vpc) // err get resource from cloud, and resource not marked as deleted, something wrong if err != nil { log.Printf("error retrive vpc %s status from tencent cloud, just requeue for retry", *vpc.Spec.VpcName) return err } if deleted { // resource marked as deleted, but status not in deleting or error state, update the state to deleting if !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "DELETING") && !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { *vpc.Status.ResourceStatus.Status = "DELETING" return r.Update(context.TODO(), vpc) } if tencentVpc != nil { // resource is marked to be deleted, cloud resource still exists lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { err = r.deleteVpc(vpc) if err != nil { r.Log.Info("error delete vpc", "namespace:", vpc.Namespace, "name:", *vpc.Spec.VpcName) //error delete the resource from cloud, don't remove finalizer yet return err } } } // resource deleted from cloud, remove finalizer finalizers := make([]string, 0) pendingFinalizers = vpc.GetFinalizers() for _, pendingFinalizer := range pendingFinalizers { if pendingFinalizer != common.Finalizer { finalizers = append(finalizers, pendingFinalizer) } } vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } //resource not marked as deleted, and get error status, try to create the resource in cloud if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { // resource in error status, retry create if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { r.Log.Info("vpc is in error status, and vpc id is empty, retry create") return r.createVpc(vpc) } } } //resource deleted in cloud, update the status if tencentVpc == nil { if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "DELETED_IN_CLOUD" return r.Update(context.TODO(), vpc) } return nil } //get resource from tencent cloud, and resource not marked as deleted, update status if !strings.EqualFold(*vpc.Status.ResourceStatus.Code, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Reason, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { vpc.Status.VpcId = tencentVpc.VpcId *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } return nil } func (r *VpcReconciler) createVpc(vpc *networkv1alpha1.Vpc) error { tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewCreateVpcRequest() request.VpcName = vpc.Spec.VpcName request.CidrBlock = vpc.Spec.CidrBlock request.EnableMulticast = vpc.Spec.EnableMulticast request.DnsServers = vpc.Spec.DnsServers request.DomainName = vpc.Spec.DomainName for _, tag := range vpc.Spec.Tags { request.Tags = append(request.Tags, &tcvpc.Tag{ Key: tag.Key, Value: tag.Value, }) } resp, err := tencentClient.CreateVpc(request) if err != nil { return err } vpc.Status.VpcId = resp.Response.Vpc.VpcId *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } func (r *VpcReconciler) getVpc(vpc *networkv1alpha1.Vpc) (*tcvpc.Vpc, error) { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil, errors.NewBadRequest("vpc id not found") } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDescribeVpcsRequest() request.VpcIds = append(request.VpcIds, vpc.Status.VpcId) resp, err := tencentClient.DescribeVpcs(request) if err != nil { log.Println("failed to get vpc from tencent cloud, requeue") return nil, err } if *resp.Response.TotalCount == 0 { log.Println("Resource is deleted from cloud") return nil, nil } return resp.Response.VpcSet[0], nil } func (r *VpcReconciler) deleteVpc(vpc *networkv1alpha1.Vpc) error { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDeleteVpcRequest() request.VpcId = vpc.Status.VpcId _, err := tencentClient.DeleteVpc(request) if err != nil { log.Println("failed to delete vpc from tencent cloud, requeue") return err } return nil } func ignoreUpdatePredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { log.Println("update event") log.Println("old meta:", e.MetaOld.GetGeneration(), "new meta:", e.MetaNew.GetGeneration()) // Ignore updates to CR status in which case metadata.Generation does not change return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() }, DeleteFunc: func(e event.DeleteEvent) bool { log.Println("delete event") // Evaluates to false if the object has been confirmed deleted. return !e.DeleteStateUnknown }, } } func (r *VpcReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&networkv1alpha1.Vpc{}). WithEventFilter(ignoreUpdatePredicate()). Complete(r) }
vpcReconcile
identifier_name
vpc_controller.go
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package controllers import ( "context" "k8s.io/client-go/tools/record" "log" "strings" "tencent-cloud-operator/internal/common" "tencent-cloud-operator/internal/utils" "time" tcerrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" tcvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" networkv1alpha1 "tencent-cloud-operator/apis/network/v1alpha1" ) // VpcReconciler reconciles a Vpc object type VpcReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme Recorder *record.EventRecorder } // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=network.tencentcloud.kubecooler.com,resources=vpcs/status,verbs=get;update;patch func (r *VpcReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { _ = context.Background() _ = r.Log.WithValues("vpc", req.String()) ctx := context.Background() // get the vpc object vpc := &networkv1alpha1.Vpc{} err := r.Get(ctx, req.NamespacedName, vpc) if err != nil { if errors.IsNotFound(err) { log.Printf("Request object not found, could have been deleted after reconcile request.") // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue return reconcile.Result{}, nil } log.Println("error reading the object, requeue") return ctrl.Result{}, err } log.Println("found the vpc", *vpc.Spec.VpcName) if vpc.Status.ResourceStatus == nil { vpc.Status.ResourceStatus = new(common.ResourceStatus) vpc.Status.ResourceStatus.Status = new(string) vpc.Status.ResourceStatus.Reason = new(string) vpc.Status.ResourceStatus.RetryCount = new(int) vpc.Status.ResourceStatus.Code = new(string) vpc.Status.ResourceStatus.LastRetry = new(string) } if vpc.Status.VpcId == nil { vpc.Status.VpcId = new(string) } err = r.vpcReconcile(vpc) if err != nil { *vpc.Status.ResourceStatus.Status = "ERROR" *vpc.Status.ResourceStatus.LastRetry = time.Now().UTC().Format("2006-01-02T15:04:05") *vpc.Status.ResourceStatus.RetryCount += 1 if cloudError, ok := err.(*tcerrors.TencentCloudSDKError); ok { *vpc.Status.ResourceStatus.Code = cloudError.Code *vpc.Status.ResourceStatus.Reason = cloudError.Message } _ = r.Update(context.TODO(), vpc) return ctrl.Result{RequeueAfter: common.RequeueInterval}, err } return ctrl.Result{RequeueAfter: common.RequeueInterval}, nil } func (r *VpcReconciler) vpcReconcile(vpc *networkv1alpha1.Vpc) error { // always check for finalizers deleted := !vpc.GetDeletionTimestamp().IsZero() pendingFinalizers := vpc.GetFinalizers() finalizerExists := len(pendingFinalizers) > 0 if !finalizerExists && !deleted && !utils.Contains(pendingFinalizers, common.Finalizer) { log.Println("Adding finalized &s to resource", common.Finalizer) finalizers := append(pendingFinalizers, common.Finalizer) vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "" || vpc.Status.ResourceStatus.Status == nil { *vpc.Status.ResourceStatus.Status = "PROCESSING" return r.Update(context.TODO(), vpc) } if *vpc.Status.ResourceStatus.Status == "PROCESSING" { return r.createVpc(vpc) } log.Printf("vpc %s is in %s status", *vpc.Spec.VpcName, *vpc.Status.ResourceStatus.Status) tencentVpc, err := r.getVpc(vpc) // err get resource from cloud, and resource not marked as deleted, something wrong if err != nil { log.Printf("error retrive vpc %s status from tencent cloud, just requeue for retry", *vpc.Spec.VpcName) return err } if deleted { // resource marked as deleted, but status not in deleting or error state, update the state to deleting if !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "DELETING") && !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { *vpc.Status.ResourceStatus.Status = "DELETING" return r.Update(context.TODO(), vpc) } if tencentVpc != nil { // resource is marked to be deleted, cloud resource still exists lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { err = r.deleteVpc(vpc) if err != nil { r.Log.Info("error delete vpc", "namespace:", vpc.Namespace, "name:", *vpc.Spec.VpcName) //error delete the resource from cloud, don't remove finalizer yet return err } } } // resource deleted from cloud, remove finalizer finalizers := make([]string, 0) pendingFinalizers = vpc.GetFinalizers() for _, pendingFinalizer := range pendingFinalizers { if pendingFinalizer != common.Finalizer { finalizers = append(finalizers, pendingFinalizer) } } vpc.SetFinalizers(finalizers) return r.Update(context.TODO(), vpc) } //resource not marked as deleted, and get error status, try to create the resource in cloud if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "ERROR") { lastRetried, _ := time.Parse("2006-01-02T15:04:05", *vpc.Status.ResourceStatus.LastRetry) //only retry 10 times, only retry every 1 minute if *vpc.Status.ResourceStatus.RetryCount < 10 && time.Since(lastRetried) > time.Minute { // resource in error status, retry create if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { r.Log.Info("vpc is in error status, and vpc id is empty, retry create") return r.createVpc(vpc) } } } //resource deleted in cloud, update the status if tencentVpc == nil { if strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "DELETED_IN_CLOUD" return r.Update(context.TODO(), vpc) } return nil } //get resource from tencent cloud, and resource not marked as deleted, update status if !strings.EqualFold(*vpc.Status.ResourceStatus.Code, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Reason, "") || !strings.EqualFold(*vpc.Status.ResourceStatus.Status, "READY") { vpc.Status.VpcId = tencentVpc.VpcId *vpc.Status.ResourceStatus.RetryCount = 0 *vpc.Status.ResourceStatus.LastRetry = "" *vpc.Status.ResourceStatus.Code = "" *vpc.Status.ResourceStatus.Reason = "" *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc)
func (r *VpcReconciler) createVpc(vpc *networkv1alpha1.Vpc) error { tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewCreateVpcRequest() request.VpcName = vpc.Spec.VpcName request.CidrBlock = vpc.Spec.CidrBlock request.EnableMulticast = vpc.Spec.EnableMulticast request.DnsServers = vpc.Spec.DnsServers request.DomainName = vpc.Spec.DomainName for _, tag := range vpc.Spec.Tags { request.Tags = append(request.Tags, &tcvpc.Tag{ Key: tag.Key, Value: tag.Value, }) } resp, err := tencentClient.CreateVpc(request) if err != nil { return err } vpc.Status.VpcId = resp.Response.Vpc.VpcId *vpc.Status.ResourceStatus.Status = "READY" return r.Update(context.TODO(), vpc) } func (r *VpcReconciler) getVpc(vpc *networkv1alpha1.Vpc) (*tcvpc.Vpc, error) { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil, errors.NewBadRequest("vpc id not found") } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDescribeVpcsRequest() request.VpcIds = append(request.VpcIds, vpc.Status.VpcId) resp, err := tencentClient.DescribeVpcs(request) if err != nil { log.Println("failed to get vpc from tencent cloud, requeue") return nil, err } if *resp.Response.TotalCount == 0 { log.Println("Resource is deleted from cloud") return nil, nil } return resp.Response.VpcSet[0], nil } func (r *VpcReconciler) deleteVpc(vpc *networkv1alpha1.Vpc) error { if vpc.Status.VpcId == nil || *vpc.Status.VpcId == "" { return nil } tencentClient, _ := tcvpc.NewClient(common.GerCredential(), *vpc.Spec.Region, profile.NewClientProfile()) request := tcvpc.NewDeleteVpcRequest() request.VpcId = vpc.Status.VpcId _, err := tencentClient.DeleteVpc(request) if err != nil { log.Println("failed to delete vpc from tencent cloud, requeue") return err } return nil } func ignoreUpdatePredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { log.Println("update event") log.Println("old meta:", e.MetaOld.GetGeneration(), "new meta:", e.MetaNew.GetGeneration()) // Ignore updates to CR status in which case metadata.Generation does not change return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() }, DeleteFunc: func(e event.DeleteEvent) bool { log.Println("delete event") // Evaluates to false if the object has been confirmed deleted. return !e.DeleteStateUnknown }, } } func (r *VpcReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&networkv1alpha1.Vpc{}). WithEventFilter(ignoreUpdatePredicate()). Complete(r) }
} return nil }
random_line_split
panorama.go
package panorama import ( "log" "math" "time" "github.com/ftl/hamradio/bandplan" "github.com/ftl/panacotta/core" ) // Panorama controller type Panorama struct { width core.Px height core.Px frequencyRange core.FrequencyRange dbRange core.DBRange vfo core.VFO band bandplan.Band resolution map[core.ViewMode]core.HzPerPx viewMode core.ViewMode fullRangeMode bool margin float64 signalDetectionActive bool fft core.FFT peakBuffer map[peakKey]peak peakTimeout time.Duration dbRangeAdjusted bool } type peak struct { frequencyRange core.FrequencyRange maxFrequency core.Frequency valueDB core.DB lastSeen time.Time } type peakKey uint func toPeakKey(f core.Frequency) peakKey { return peakKey(f / 100.0) } const ( defaultFixedResolution = core.HzPerPx(100) defaultCenteredResolution = core.HzPerPx(25) ) // New returns a new instance of panorama. func New(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := Panorama{ width: width, frequencyRange: frequencyRange, dbRange: core.DBRange{From: -105, To: 10}, resolution: map[core.ViewMode]core.HzPerPx{ core.ViewFixed: calcResolution(frequencyRange, width), core.ViewCentered: defaultCenteredResolution, }, viewMode: core.ViewFixed, signalDetectionActive: true, margin: 0.02, peakBuffer: make(map[peakKey]peak), peakTimeout: 10 * time.Second, // TODO make this configurable dbRangeAdjusted: true, } result.vfo.Frequency = vfoFrequency return &result } // NewFullSpectrum returns a new instance of panorama in full-range mode. func NewFullSpectrum(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := New(width, frequencyRange, vfoFrequency) result.fullRangeMode = true return result } func calcResolution(frequencyRange core.FrequencyRange, width core.Px) core.HzPerPx { return core.HzPerPx(float64(frequencyRange.Width()) / float64(width)) } func (p *Panorama) updateFrequencyRange() { if math.IsNaN(float64(p.resolution[p.viewMode])) { p.setupFrequencyRange() return } var lowerRatio, upperRatio core.Frequency if p.viewMode == core.ViewFixed && p.frequencyRange.Contains(p.vfo.Frequency) { lowerRatio = (p.vfo.Frequency - p.frequencyRange.From) / p.frequencyRange.Width() lowerRatio = core.Frequency(math.Max(p.margin, math.Min(float64(lowerRatio), 1-p.margin))) upperRatio = 1.0 - lowerRatio } else { lowerRatio = 0.5 upperRatio = 0.5 } frequencyWidth := core.Frequency(float64(p.width) * float64(p.resolution[p.viewMode])) p.frequencyRange.From = p.vfo.Frequency - (lowerRatio * frequencyWidth) p.frequencyRange.To = p.vfo.Frequency + (upperRatio * frequencyWidth) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } func (p *Panorama) setupFrequencyRange() { if p.vfo.Frequency == 0 || !p.band.Contains(p.vfo.Frequency) { return } if p.viewMode == core.ViewFixed { p.frequencyRange.From = p.band.From - 1000 p.frequencyRange.To = p.band.To + 1000 } else { p.frequencyRange.From = p.vfo.Frequency - 20000 p.frequencyRange.From = p.vfo.Frequency + 20000 } p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } // SetSize in pixels func (p *Panorama) SetSize(width, height core.Px) { if (width == p.width) && (height == p.height) { return } log.Printf("width %v height %v", width, height) p.width = width p.height = height p.updateFrequencyRange() } // FrequencyRange of the panorama func (p Panorama) FrequencyRange() core.FrequencyRange { return p.frequencyRange } // From in Hz func (p Panorama) From() core.Frequency { return p.frequencyRange.From } // To in Hz func (p Panorama) To() core.Frequency { return p.frequencyRange.To } // Bandwidth in Hz func (p Panorama) Bandwidth() core.Frequency
// SetVFO in Hz func (p *Panorama) SetVFO(vfo core.VFO) { p.vfo = vfo if !p.band.Contains(vfo.Frequency) { band := bandplan.IARURegion1.ByFrequency(vfo.Frequency) if band.Width() > 0 { if p.band.Width() > 0 { p.dbRangeAdjusted = false } p.band = band } } log.Printf("vfo %v band %v", p.vfo, p.band) p.updateFrequencyRange() } func (p *Panorama) adjustDBRange() { if p.dbRangeAdjusted { return } dbWidth := p.dbRange.Width() p.dbRange.From = core.DB(p.fft.PeakThreshold) - 0.1*dbWidth p.dbRange.To = p.dbRange.From + dbWidth p.dbRangeAdjusted = true } // VFO frequency in Hz func (p Panorama) VFO() (vfo core.VFO, band bandplan.Band) { return p.vfo, p.band } // SetFFT data func (p *Panorama) SetFFT(fft core.FFT) { p.fft = fft p.adjustDBRange() } // ToggleSignalDetection switches the signal detection on and off. func (p *Panorama) ToggleSignalDetection() { p.signalDetectionActive = !p.signalDetectionActive } // SignalDetectionActive indicates if the signal detection is active or not. func (p *Panorama) SignalDetectionActive() bool { return p.signalDetectionActive } // ToggleViewMode switches to the other view mode. func (p *Panorama) ToggleViewMode() { if p.viewMode == core.ViewFixed { p.viewMode = core.ViewCentered } else { p.viewMode = core.ViewFixed } p.updateFrequencyRange() } // ViewMode returns the currently active view mode (centered or fixed). func (p *Panorama) ViewMode() core.ViewMode { return p.viewMode } // ZoomIn one step func (p *Panorama) ZoomIn() { p.resolution[p.viewMode] /= 1.25 p.updateFrequencyRange() } // ZoomOut one step func (p *Panorama) ZoomOut() { p.resolution[p.viewMode] *= 1.25 p.updateFrequencyRange() } // ZoomToBand of the current VFO frequency and switch to fixed view mode. func (p *Panorama) ZoomToBand() { if p.band.Width() == 0 { return } p.zoomTo(p.band.Expanded(1000)) } func (p *Panorama) zoomTo(frequencyRange core.FrequencyRange) { p.viewMode = core.ViewFixed p.frequencyRange = frequencyRange p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) } // ResetZoom to the default of the current view mode func (p *Panorama) ResetZoom() { switch p.viewMode { case core.ViewFixed: p.resolution[p.viewMode] = defaultFixedResolution case core.ViewCentered: p.resolution[p.viewMode] = defaultCenteredResolution } p.updateFrequencyRange() } func (p *Panorama) FinerDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From += Δdb p.dbRange.To -= Δdb } func (p *Panorama) CoarserDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From -= Δdb p.dbRange.To += Δdb } func (p *Panorama) ShiftDynamicRange(ratio core.Frct) { Δdb := p.dbRange.Width() * core.DB(ratio) p.dbRange.From += Δdb p.dbRange.To += Δdb } func (p *Panorama) SetDynamicRange(dbRange core.DBRange) { p.dbRange = dbRange } // ShiftFrequencyRange shifts the panorama horizontally by the given ratio of the total width. func (p *Panorama) ShiftFrequencyRange(ratio core.Frct) { Δf := p.frequencyRange.Width() * core.Frequency(ratio) if p.viewMode == core.ViewFixed { p.frequencyRange.Shift(Δf) } } // Data to draw the current panorama. func (p Panorama) Data() core.Panorama { if p.fullRangeMode { return p.fullRangeData() } return p.data() } func (p Panorama) dataValid() bool { return !(len(p.fft.Data) == 0 || p.fft.Range.To < p.frequencyRange.From || p.fft.Range.From > p.frequencyRange.To) } func (p Panorama) data() core.Panorama { if !p.dataValid() { return core.Panorama{} } spectrum, sigmaEnvelope := p.spectrum() result := core.Panorama{ FrequencyRange: p.frequencyRange, VFO: p.vfo, Band: p.band, Resolution: p.resolution[p.viewMode], VFOLine: core.ToFrequencyFrct(p.vfo.Frequency, p.frequencyRange), VFOFilterFrom: core.ToFrequencyFrct(p.vfo.Frequency-p.vfo.FilterWidth/2, p.frequencyRange), VFOFilterTo: core.ToFrequencyFrct(p.vfo.Frequency+p.vfo.FilterWidth/2, p.frequencyRange), VFOSignalLevel: p.signalLevel(), FrequencyScale: p.frequencyScale(), DBScale: p.dbScale(), Spectrum: spectrum, SigmaEnvelope: sigmaEnvelope, PeakThresholdLevel: core.ToDBFrct(core.DB(p.fft.PeakThreshold), p.dbRange), Waterline: p.waterline(spectrum), } if p.signalDetectionActive { result.Peaks = p.peaks() } return result } func (p Panorama) signalLevel() core.DB { vfoIndex := p.fft.ToIndex(p.vfo.Frequency) if vfoIndex >= 0 && vfoIndex < len(p.fft.Data) { return core.DB(p.fft.Data[vfoIndex]) } return 0 } func (p Panorama) frequencyScale() []core.FrequencyMark { fZeros := float64(int(math.Log10(float64(p.frequencyRange.Width()))) - 1) fMagnitude := int(math.Pow(10, fZeros)) fFactor := fMagnitude if fFactor < 0 { return []core.FrequencyMark{} } for core.Frequency(fFactor)/p.frequencyRange.Width() < 0.1 { if fFactor%10 == 0 { fFactor *= 5 } else { fFactor *= 10 } } for core.Frequency(fFactor)/p.frequencyRange.Width() > 0.15 { if fFactor%10 == 0 { fFactor /= 5 } else { fFactor /= 10 } } freqScale := make([]core.FrequencyMark, 0, int(p.frequencyRange.Width())/fFactor) for f := core.Frequency((int(p.frequencyRange.From) / fFactor) * fFactor); f < p.frequencyRange.To; f += core.Frequency(fFactor) { mark := core.FrequencyMark{ X: core.ToFrequencyFrct(f, p.frequencyRange), Frequency: f, } freqScale = append(freqScale, mark) } return freqScale } func (p Panorama) dbScale() []core.DBMark { startDB := int(p.dbRange.From) - int(p.dbRange.From)%10 markCount := (int(p.dbRange.To) - startDB) / 10 if (int(p.dbRange.To)-startDB)%10 != 0 { markCount++ } dbScale := make([]core.DBMark, markCount) for i := range dbScale { db := core.DB(startDB + i*10) dbScale[i] = core.DBMark{ DB: db, Y: core.ToDBFrct(db, p.dbRange), } } return dbScale } func (p Panorama) spectrum() ([]core.FPoint, []core.FPoint) { fftResolution := p.fft.Resolution() step := int(math.Max(1, math.Floor(float64(len(p.fft.Data))/float64(p.width)))) start := int(math.Max(0, math.Floor(float64(p.frequencyRange.From-p.fft.Range.From)/fftResolution))) end := int(math.Min(float64(len(p.fft.Data)-1), math.Ceil(float64(p.frequencyRange.To-p.fft.Range.From)/fftResolution))) resultLength := (end - start + 1) / step if (end-start+1)%step != 0 { resultLength++ } result := make([]core.FPoint, resultLength) sigmaEnvelope := make([]core.FPoint, resultLength) resultIndex := 0 for i := start; i <= end; i += step { d := -1000.0 t := -1000.0 for j := i; j < i+step && j < len(p.fft.Data); j++ { d = math.Max(d, p.fft.Data[j]) t = math.Max(t, p.fft.SigmaEnvelope[j]) } result[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(d), p.dbRange), } sigmaEnvelope[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(t), p.dbRange), } resultIndex++ } return result, sigmaEnvelope } func (p Panorama) peaks() []core.PeakMark { correction := func(i int) core.Frequency { if i <= 0 || i >= len(p.fft.Data)-1 { return 0 } return core.Frequency((p.fft.Data[i+1] - p.fft.Data[i-1]) / (4*p.fft.Data[i] - 2*p.fft.Data[i-1] - 2*p.fft.Data[i+1])) } now := time.Now() for _, peakIndexRange := range p.fft.Peaks { peak := peak{ frequencyRange: core.FrequencyRange{From: p.fft.Frequency(peakIndexRange.From), To: p.fft.Frequency(peakIndexRange.To)}, maxFrequency: p.fft.Frequency(peakIndexRange.Max) + correction(peakIndexRange.Max), valueDB: core.DB(peakIndexRange.Value), lastSeen: now, } key := toPeakKey(peak.maxFrequency) p.peakBuffer[key] = peak } result := make([]core.PeakMark, 0, len(p.fft.Peaks)) for key, peak := range p.peakBuffer { age := now.Sub(peak.lastSeen) if age < p.peakTimeout && p.frequencyRange.Contains(peak.maxFrequency) { result = append(result, core.PeakMark{ FromX: core.ToFrequencyFrct(peak.frequencyRange.From, p.frequencyRange), ToX: core.ToFrequencyFrct(peak.frequencyRange.To, p.frequencyRange), MaxX: core.ToFrequencyFrct(peak.maxFrequency, p.frequencyRange), MaxFrequency: peak.maxFrequency, ValueY: core.ToDBFrct(peak.valueDB, p.dbRange), ValueDB: peak.valueDB, }) } else if age > 0 { p.peakBuffer[key] = peak } else if age >= p.peakTimeout || !p.frequencyRange.Contains(peak.maxFrequency) { delete(p.peakBuffer, key) } } return result } func (p Panorama) waterline(spectrum []core.FPoint) []core.Frct { length := int(p.width) binWidth := float64(length) / float64(len(spectrum)) result := make([]core.Frct, length) for _, point := range spectrum { center := float64(length-1) * float64(point.X) binFrom := center - binWidth/2 binTo := center + binWidth/2 for i := int(binFrom); i <= int(binTo+1); i++ { if 0 > i || i >= len(result) { continue } result[i] = core.Frct(math.Max(float64(result[i]), float64(point.Y))) } } return result }
{ return p.frequencyRange.Width() }
identifier_body
panorama.go
package panorama import ( "log" "math" "time" "github.com/ftl/hamradio/bandplan" "github.com/ftl/panacotta/core" ) // Panorama controller type Panorama struct { width core.Px height core.Px frequencyRange core.FrequencyRange dbRange core.DBRange vfo core.VFO band bandplan.Band resolution map[core.ViewMode]core.HzPerPx viewMode core.ViewMode fullRangeMode bool margin float64 signalDetectionActive bool fft core.FFT peakBuffer map[peakKey]peak peakTimeout time.Duration dbRangeAdjusted bool } type peak struct { frequencyRange core.FrequencyRange maxFrequency core.Frequency valueDB core.DB lastSeen time.Time } type peakKey uint func toPeakKey(f core.Frequency) peakKey {
} const ( defaultFixedResolution = core.HzPerPx(100) defaultCenteredResolution = core.HzPerPx(25) ) // New returns a new instance of panorama. func New(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := Panorama{ width: width, frequencyRange: frequencyRange, dbRange: core.DBRange{From: -105, To: 10}, resolution: map[core.ViewMode]core.HzPerPx{ core.ViewFixed: calcResolution(frequencyRange, width), core.ViewCentered: defaultCenteredResolution, }, viewMode: core.ViewFixed, signalDetectionActive: true, margin: 0.02, peakBuffer: make(map[peakKey]peak), peakTimeout: 10 * time.Second, // TODO make this configurable dbRangeAdjusted: true, } result.vfo.Frequency = vfoFrequency return &result } // NewFullSpectrum returns a new instance of panorama in full-range mode. func NewFullSpectrum(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := New(width, frequencyRange, vfoFrequency) result.fullRangeMode = true return result } func calcResolution(frequencyRange core.FrequencyRange, width core.Px) core.HzPerPx { return core.HzPerPx(float64(frequencyRange.Width()) / float64(width)) } func (p *Panorama) updateFrequencyRange() { if math.IsNaN(float64(p.resolution[p.viewMode])) { p.setupFrequencyRange() return } var lowerRatio, upperRatio core.Frequency if p.viewMode == core.ViewFixed && p.frequencyRange.Contains(p.vfo.Frequency) { lowerRatio = (p.vfo.Frequency - p.frequencyRange.From) / p.frequencyRange.Width() lowerRatio = core.Frequency(math.Max(p.margin, math.Min(float64(lowerRatio), 1-p.margin))) upperRatio = 1.0 - lowerRatio } else { lowerRatio = 0.5 upperRatio = 0.5 } frequencyWidth := core.Frequency(float64(p.width) * float64(p.resolution[p.viewMode])) p.frequencyRange.From = p.vfo.Frequency - (lowerRatio * frequencyWidth) p.frequencyRange.To = p.vfo.Frequency + (upperRatio * frequencyWidth) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } func (p *Panorama) setupFrequencyRange() { if p.vfo.Frequency == 0 || !p.band.Contains(p.vfo.Frequency) { return } if p.viewMode == core.ViewFixed { p.frequencyRange.From = p.band.From - 1000 p.frequencyRange.To = p.band.To + 1000 } else { p.frequencyRange.From = p.vfo.Frequency - 20000 p.frequencyRange.From = p.vfo.Frequency + 20000 } p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } // SetSize in pixels func (p *Panorama) SetSize(width, height core.Px) { if (width == p.width) && (height == p.height) { return } log.Printf("width %v height %v", width, height) p.width = width p.height = height p.updateFrequencyRange() } // FrequencyRange of the panorama func (p Panorama) FrequencyRange() core.FrequencyRange { return p.frequencyRange } // From in Hz func (p Panorama) From() core.Frequency { return p.frequencyRange.From } // To in Hz func (p Panorama) To() core.Frequency { return p.frequencyRange.To } // Bandwidth in Hz func (p Panorama) Bandwidth() core.Frequency { return p.frequencyRange.Width() } // SetVFO in Hz func (p *Panorama) SetVFO(vfo core.VFO) { p.vfo = vfo if !p.band.Contains(vfo.Frequency) { band := bandplan.IARURegion1.ByFrequency(vfo.Frequency) if band.Width() > 0 { if p.band.Width() > 0 { p.dbRangeAdjusted = false } p.band = band } } log.Printf("vfo %v band %v", p.vfo, p.band) p.updateFrequencyRange() } func (p *Panorama) adjustDBRange() { if p.dbRangeAdjusted { return } dbWidth := p.dbRange.Width() p.dbRange.From = core.DB(p.fft.PeakThreshold) - 0.1*dbWidth p.dbRange.To = p.dbRange.From + dbWidth p.dbRangeAdjusted = true } // VFO frequency in Hz func (p Panorama) VFO() (vfo core.VFO, band bandplan.Band) { return p.vfo, p.band } // SetFFT data func (p *Panorama) SetFFT(fft core.FFT) { p.fft = fft p.adjustDBRange() } // ToggleSignalDetection switches the signal detection on and off. func (p *Panorama) ToggleSignalDetection() { p.signalDetectionActive = !p.signalDetectionActive } // SignalDetectionActive indicates if the signal detection is active or not. func (p *Panorama) SignalDetectionActive() bool { return p.signalDetectionActive } // ToggleViewMode switches to the other view mode. func (p *Panorama) ToggleViewMode() { if p.viewMode == core.ViewFixed { p.viewMode = core.ViewCentered } else { p.viewMode = core.ViewFixed } p.updateFrequencyRange() } // ViewMode returns the currently active view mode (centered or fixed). func (p *Panorama) ViewMode() core.ViewMode { return p.viewMode } // ZoomIn one step func (p *Panorama) ZoomIn() { p.resolution[p.viewMode] /= 1.25 p.updateFrequencyRange() } // ZoomOut one step func (p *Panorama) ZoomOut() { p.resolution[p.viewMode] *= 1.25 p.updateFrequencyRange() } // ZoomToBand of the current VFO frequency and switch to fixed view mode. func (p *Panorama) ZoomToBand() { if p.band.Width() == 0 { return } p.zoomTo(p.band.Expanded(1000)) } func (p *Panorama) zoomTo(frequencyRange core.FrequencyRange) { p.viewMode = core.ViewFixed p.frequencyRange = frequencyRange p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) } // ResetZoom to the default of the current view mode func (p *Panorama) ResetZoom() { switch p.viewMode { case core.ViewFixed: p.resolution[p.viewMode] = defaultFixedResolution case core.ViewCentered: p.resolution[p.viewMode] = defaultCenteredResolution } p.updateFrequencyRange() } func (p *Panorama) FinerDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From += Δdb p.dbRange.To -= Δdb } func (p *Panorama) CoarserDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From -= Δdb p.dbRange.To += Δdb } func (p *Panorama) ShiftDynamicRange(ratio core.Frct) { Δdb := p.dbRange.Width() * core.DB(ratio) p.dbRange.From += Δdb p.dbRange.To += Δdb } func (p *Panorama) SetDynamicRange(dbRange core.DBRange) { p.dbRange = dbRange } // ShiftFrequencyRange shifts the panorama horizontally by the given ratio of the total width. func (p *Panorama) ShiftFrequencyRange(ratio core.Frct) { Δf := p.frequencyRange.Width() * core.Frequency(ratio) if p.viewMode == core.ViewFixed { p.frequencyRange.Shift(Δf) } } // Data to draw the current panorama. func (p Panorama) Data() core.Panorama { if p.fullRangeMode { return p.fullRangeData() } return p.data() } func (p Panorama) dataValid() bool { return !(len(p.fft.Data) == 0 || p.fft.Range.To < p.frequencyRange.From || p.fft.Range.From > p.frequencyRange.To) } func (p Panorama) data() core.Panorama { if !p.dataValid() { return core.Panorama{} } spectrum, sigmaEnvelope := p.spectrum() result := core.Panorama{ FrequencyRange: p.frequencyRange, VFO: p.vfo, Band: p.band, Resolution: p.resolution[p.viewMode], VFOLine: core.ToFrequencyFrct(p.vfo.Frequency, p.frequencyRange), VFOFilterFrom: core.ToFrequencyFrct(p.vfo.Frequency-p.vfo.FilterWidth/2, p.frequencyRange), VFOFilterTo: core.ToFrequencyFrct(p.vfo.Frequency+p.vfo.FilterWidth/2, p.frequencyRange), VFOSignalLevel: p.signalLevel(), FrequencyScale: p.frequencyScale(), DBScale: p.dbScale(), Spectrum: spectrum, SigmaEnvelope: sigmaEnvelope, PeakThresholdLevel: core.ToDBFrct(core.DB(p.fft.PeakThreshold), p.dbRange), Waterline: p.waterline(spectrum), } if p.signalDetectionActive { result.Peaks = p.peaks() } return result } func (p Panorama) signalLevel() core.DB { vfoIndex := p.fft.ToIndex(p.vfo.Frequency) if vfoIndex >= 0 && vfoIndex < len(p.fft.Data) { return core.DB(p.fft.Data[vfoIndex]) } return 0 } func (p Panorama) frequencyScale() []core.FrequencyMark { fZeros := float64(int(math.Log10(float64(p.frequencyRange.Width()))) - 1) fMagnitude := int(math.Pow(10, fZeros)) fFactor := fMagnitude if fFactor < 0 { return []core.FrequencyMark{} } for core.Frequency(fFactor)/p.frequencyRange.Width() < 0.1 { if fFactor%10 == 0 { fFactor *= 5 } else { fFactor *= 10 } } for core.Frequency(fFactor)/p.frequencyRange.Width() > 0.15 { if fFactor%10 == 0 { fFactor /= 5 } else { fFactor /= 10 } } freqScale := make([]core.FrequencyMark, 0, int(p.frequencyRange.Width())/fFactor) for f := core.Frequency((int(p.frequencyRange.From) / fFactor) * fFactor); f < p.frequencyRange.To; f += core.Frequency(fFactor) { mark := core.FrequencyMark{ X: core.ToFrequencyFrct(f, p.frequencyRange), Frequency: f, } freqScale = append(freqScale, mark) } return freqScale } func (p Panorama) dbScale() []core.DBMark { startDB := int(p.dbRange.From) - int(p.dbRange.From)%10 markCount := (int(p.dbRange.To) - startDB) / 10 if (int(p.dbRange.To)-startDB)%10 != 0 { markCount++ } dbScale := make([]core.DBMark, markCount) for i := range dbScale { db := core.DB(startDB + i*10) dbScale[i] = core.DBMark{ DB: db, Y: core.ToDBFrct(db, p.dbRange), } } return dbScale } func (p Panorama) spectrum() ([]core.FPoint, []core.FPoint) { fftResolution := p.fft.Resolution() step := int(math.Max(1, math.Floor(float64(len(p.fft.Data))/float64(p.width)))) start := int(math.Max(0, math.Floor(float64(p.frequencyRange.From-p.fft.Range.From)/fftResolution))) end := int(math.Min(float64(len(p.fft.Data)-1), math.Ceil(float64(p.frequencyRange.To-p.fft.Range.From)/fftResolution))) resultLength := (end - start + 1) / step if (end-start+1)%step != 0 { resultLength++ } result := make([]core.FPoint, resultLength) sigmaEnvelope := make([]core.FPoint, resultLength) resultIndex := 0 for i := start; i <= end; i += step { d := -1000.0 t := -1000.0 for j := i; j < i+step && j < len(p.fft.Data); j++ { d = math.Max(d, p.fft.Data[j]) t = math.Max(t, p.fft.SigmaEnvelope[j]) } result[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(d), p.dbRange), } sigmaEnvelope[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(t), p.dbRange), } resultIndex++ } return result, sigmaEnvelope } func (p Panorama) peaks() []core.PeakMark { correction := func(i int) core.Frequency { if i <= 0 || i >= len(p.fft.Data)-1 { return 0 } return core.Frequency((p.fft.Data[i+1] - p.fft.Data[i-1]) / (4*p.fft.Data[i] - 2*p.fft.Data[i-1] - 2*p.fft.Data[i+1])) } now := time.Now() for _, peakIndexRange := range p.fft.Peaks { peak := peak{ frequencyRange: core.FrequencyRange{From: p.fft.Frequency(peakIndexRange.From), To: p.fft.Frequency(peakIndexRange.To)}, maxFrequency: p.fft.Frequency(peakIndexRange.Max) + correction(peakIndexRange.Max), valueDB: core.DB(peakIndexRange.Value), lastSeen: now, } key := toPeakKey(peak.maxFrequency) p.peakBuffer[key] = peak } result := make([]core.PeakMark, 0, len(p.fft.Peaks)) for key, peak := range p.peakBuffer { age := now.Sub(peak.lastSeen) if age < p.peakTimeout && p.frequencyRange.Contains(peak.maxFrequency) { result = append(result, core.PeakMark{ FromX: core.ToFrequencyFrct(peak.frequencyRange.From, p.frequencyRange), ToX: core.ToFrequencyFrct(peak.frequencyRange.To, p.frequencyRange), MaxX: core.ToFrequencyFrct(peak.maxFrequency, p.frequencyRange), MaxFrequency: peak.maxFrequency, ValueY: core.ToDBFrct(peak.valueDB, p.dbRange), ValueDB: peak.valueDB, }) } else if age > 0 { p.peakBuffer[key] = peak } else if age >= p.peakTimeout || !p.frequencyRange.Contains(peak.maxFrequency) { delete(p.peakBuffer, key) } } return result } func (p Panorama) waterline(spectrum []core.FPoint) []core.Frct { length := int(p.width) binWidth := float64(length) / float64(len(spectrum)) result := make([]core.Frct, length) for _, point := range spectrum { center := float64(length-1) * float64(point.X) binFrom := center - binWidth/2 binTo := center + binWidth/2 for i := int(binFrom); i <= int(binTo+1); i++ { if 0 > i || i >= len(result) { continue } result[i] = core.Frct(math.Max(float64(result[i]), float64(point.Y))) } } return result }
return peakKey(f / 100.0)
random_line_split
panorama.go
package panorama import ( "log" "math" "time" "github.com/ftl/hamradio/bandplan" "github.com/ftl/panacotta/core" ) // Panorama controller type Panorama struct { width core.Px height core.Px frequencyRange core.FrequencyRange dbRange core.DBRange vfo core.VFO band bandplan.Band resolution map[core.ViewMode]core.HzPerPx viewMode core.ViewMode fullRangeMode bool margin float64 signalDetectionActive bool fft core.FFT peakBuffer map[peakKey]peak peakTimeout time.Duration dbRangeAdjusted bool } type peak struct { frequencyRange core.FrequencyRange maxFrequency core.Frequency valueDB core.DB lastSeen time.Time } type peakKey uint func toPeakKey(f core.Frequency) peakKey { return peakKey(f / 100.0) } const ( defaultFixedResolution = core.HzPerPx(100) defaultCenteredResolution = core.HzPerPx(25) ) // New returns a new instance of panorama. func New(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := Panorama{ width: width, frequencyRange: frequencyRange, dbRange: core.DBRange{From: -105, To: 10}, resolution: map[core.ViewMode]core.HzPerPx{ core.ViewFixed: calcResolution(frequencyRange, width), core.ViewCentered: defaultCenteredResolution, }, viewMode: core.ViewFixed, signalDetectionActive: true, margin: 0.02, peakBuffer: make(map[peakKey]peak), peakTimeout: 10 * time.Second, // TODO make this configurable dbRangeAdjusted: true, } result.vfo.Frequency = vfoFrequency return &result } // NewFullSpectrum returns a new instance of panorama in full-range mode. func NewFullSpectrum(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := New(width, frequencyRange, vfoFrequency) result.fullRangeMode = true return result } func calcResolution(frequencyRange core.FrequencyRange, width core.Px) core.HzPerPx { return core.HzPerPx(float64(frequencyRange.Width()) / float64(width)) } func (p *Panorama) updateFrequencyRange() { if math.IsNaN(float64(p.resolution[p.viewMode])) { p.setupFrequencyRange() return } var lowerRatio, upperRatio core.Frequency if p.viewMode == core.ViewFixed && p.frequencyRange.Contains(p.vfo.Frequency) { lowerRatio = (p.vfo.Frequency - p.frequencyRange.From) / p.frequencyRange.Width() lowerRatio = core.Frequency(math.Max(p.margin, math.Min(float64(lowerRatio), 1-p.margin))) upperRatio = 1.0 - lowerRatio } else { lowerRatio = 0.5 upperRatio = 0.5 } frequencyWidth := core.Frequency(float64(p.width) * float64(p.resolution[p.viewMode])) p.frequencyRange.From = p.vfo.Frequency - (lowerRatio * frequencyWidth) p.frequencyRange.To = p.vfo.Frequency + (upperRatio * frequencyWidth) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } func (p *Panorama) setupFrequencyRange() { if p.vfo.Frequency == 0 || !p.band.Contains(p.vfo.Frequency) { return } if p.viewMode == core.ViewFixed { p.frequencyRange.From = p.band.From - 1000 p.frequencyRange.To = p.band.To + 1000 } else { p.frequencyRange.From = p.vfo.Frequency - 20000 p.frequencyRange.From = p.vfo.Frequency + 20000 } p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } // SetSize in pixels func (p *Panorama) SetSize(width, height core.Px) { if (width == p.width) && (height == p.height) { return } log.Printf("width %v height %v", width, height) p.width = width p.height = height p.updateFrequencyRange() } // FrequencyRange of the panorama func (p Panorama) FrequencyRange() core.FrequencyRange { return p.frequencyRange } // From in Hz func (p Panorama) From() core.Frequency { return p.frequencyRange.From } // To in Hz func (p Panorama) To() core.Frequency { return p.frequencyRange.To } // Bandwidth in Hz func (p Panorama) Bandwidth() core.Frequency { return p.frequencyRange.Width() } // SetVFO in Hz func (p *Panorama) SetVFO(vfo core.VFO) { p.vfo = vfo if !p.band.Contains(vfo.Frequency) { band := bandplan.IARURegion1.ByFrequency(vfo.Frequency) if band.Width() > 0 { if p.band.Width() > 0 { p.dbRangeAdjusted = false } p.band = band } } log.Printf("vfo %v band %v", p.vfo, p.band) p.updateFrequencyRange() } func (p *Panorama) adjustDBRange() { if p.dbRangeAdjusted { return } dbWidth := p.dbRange.Width() p.dbRange.From = core.DB(p.fft.PeakThreshold) - 0.1*dbWidth p.dbRange.To = p.dbRange.From + dbWidth p.dbRangeAdjusted = true } // VFO frequency in Hz func (p Panorama) VFO() (vfo core.VFO, band bandplan.Band) { return p.vfo, p.band } // SetFFT data func (p *Panorama) SetFFT(fft core.FFT) { p.fft = fft p.adjustDBRange() } // ToggleSignalDetection switches the signal detection on and off. func (p *Panorama) ToggleSignalDetection() { p.signalDetectionActive = !p.signalDetectionActive } // SignalDetectionActive indicates if the signal detection is active or not. func (p *Panorama)
() bool { return p.signalDetectionActive } // ToggleViewMode switches to the other view mode. func (p *Panorama) ToggleViewMode() { if p.viewMode == core.ViewFixed { p.viewMode = core.ViewCentered } else { p.viewMode = core.ViewFixed } p.updateFrequencyRange() } // ViewMode returns the currently active view mode (centered or fixed). func (p *Panorama) ViewMode() core.ViewMode { return p.viewMode } // ZoomIn one step func (p *Panorama) ZoomIn() { p.resolution[p.viewMode] /= 1.25 p.updateFrequencyRange() } // ZoomOut one step func (p *Panorama) ZoomOut() { p.resolution[p.viewMode] *= 1.25 p.updateFrequencyRange() } // ZoomToBand of the current VFO frequency and switch to fixed view mode. func (p *Panorama) ZoomToBand() { if p.band.Width() == 0 { return } p.zoomTo(p.band.Expanded(1000)) } func (p *Panorama) zoomTo(frequencyRange core.FrequencyRange) { p.viewMode = core.ViewFixed p.frequencyRange = frequencyRange p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) } // ResetZoom to the default of the current view mode func (p *Panorama) ResetZoom() { switch p.viewMode { case core.ViewFixed: p.resolution[p.viewMode] = defaultFixedResolution case core.ViewCentered: p.resolution[p.viewMode] = defaultCenteredResolution } p.updateFrequencyRange() } func (p *Panorama) FinerDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From += Δdb p.dbRange.To -= Δdb } func (p *Panorama) CoarserDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From -= Δdb p.dbRange.To += Δdb } func (p *Panorama) ShiftDynamicRange(ratio core.Frct) { Δdb := p.dbRange.Width() * core.DB(ratio) p.dbRange.From += Δdb p.dbRange.To += Δdb } func (p *Panorama) SetDynamicRange(dbRange core.DBRange) { p.dbRange = dbRange } // ShiftFrequencyRange shifts the panorama horizontally by the given ratio of the total width. func (p *Panorama) ShiftFrequencyRange(ratio core.Frct) { Δf := p.frequencyRange.Width() * core.Frequency(ratio) if p.viewMode == core.ViewFixed { p.frequencyRange.Shift(Δf) } } // Data to draw the current panorama. func (p Panorama) Data() core.Panorama { if p.fullRangeMode { return p.fullRangeData() } return p.data() } func (p Panorama) dataValid() bool { return !(len(p.fft.Data) == 0 || p.fft.Range.To < p.frequencyRange.From || p.fft.Range.From > p.frequencyRange.To) } func (p Panorama) data() core.Panorama { if !p.dataValid() { return core.Panorama{} } spectrum, sigmaEnvelope := p.spectrum() result := core.Panorama{ FrequencyRange: p.frequencyRange, VFO: p.vfo, Band: p.band, Resolution: p.resolution[p.viewMode], VFOLine: core.ToFrequencyFrct(p.vfo.Frequency, p.frequencyRange), VFOFilterFrom: core.ToFrequencyFrct(p.vfo.Frequency-p.vfo.FilterWidth/2, p.frequencyRange), VFOFilterTo: core.ToFrequencyFrct(p.vfo.Frequency+p.vfo.FilterWidth/2, p.frequencyRange), VFOSignalLevel: p.signalLevel(), FrequencyScale: p.frequencyScale(), DBScale: p.dbScale(), Spectrum: spectrum, SigmaEnvelope: sigmaEnvelope, PeakThresholdLevel: core.ToDBFrct(core.DB(p.fft.PeakThreshold), p.dbRange), Waterline: p.waterline(spectrum), } if p.signalDetectionActive { result.Peaks = p.peaks() } return result } func (p Panorama) signalLevel() core.DB { vfoIndex := p.fft.ToIndex(p.vfo.Frequency) if vfoIndex >= 0 && vfoIndex < len(p.fft.Data) { return core.DB(p.fft.Data[vfoIndex]) } return 0 } func (p Panorama) frequencyScale() []core.FrequencyMark { fZeros := float64(int(math.Log10(float64(p.frequencyRange.Width()))) - 1) fMagnitude := int(math.Pow(10, fZeros)) fFactor := fMagnitude if fFactor < 0 { return []core.FrequencyMark{} } for core.Frequency(fFactor)/p.frequencyRange.Width() < 0.1 { if fFactor%10 == 0 { fFactor *= 5 } else { fFactor *= 10 } } for core.Frequency(fFactor)/p.frequencyRange.Width() > 0.15 { if fFactor%10 == 0 { fFactor /= 5 } else { fFactor /= 10 } } freqScale := make([]core.FrequencyMark, 0, int(p.frequencyRange.Width())/fFactor) for f := core.Frequency((int(p.frequencyRange.From) / fFactor) * fFactor); f < p.frequencyRange.To; f += core.Frequency(fFactor) { mark := core.FrequencyMark{ X: core.ToFrequencyFrct(f, p.frequencyRange), Frequency: f, } freqScale = append(freqScale, mark) } return freqScale } func (p Panorama) dbScale() []core.DBMark { startDB := int(p.dbRange.From) - int(p.dbRange.From)%10 markCount := (int(p.dbRange.To) - startDB) / 10 if (int(p.dbRange.To)-startDB)%10 != 0 { markCount++ } dbScale := make([]core.DBMark, markCount) for i := range dbScale { db := core.DB(startDB + i*10) dbScale[i] = core.DBMark{ DB: db, Y: core.ToDBFrct(db, p.dbRange), } } return dbScale } func (p Panorama) spectrum() ([]core.FPoint, []core.FPoint) { fftResolution := p.fft.Resolution() step := int(math.Max(1, math.Floor(float64(len(p.fft.Data))/float64(p.width)))) start := int(math.Max(0, math.Floor(float64(p.frequencyRange.From-p.fft.Range.From)/fftResolution))) end := int(math.Min(float64(len(p.fft.Data)-1), math.Ceil(float64(p.frequencyRange.To-p.fft.Range.From)/fftResolution))) resultLength := (end - start + 1) / step if (end-start+1)%step != 0 { resultLength++ } result := make([]core.FPoint, resultLength) sigmaEnvelope := make([]core.FPoint, resultLength) resultIndex := 0 for i := start; i <= end; i += step { d := -1000.0 t := -1000.0 for j := i; j < i+step && j < len(p.fft.Data); j++ { d = math.Max(d, p.fft.Data[j]) t = math.Max(t, p.fft.SigmaEnvelope[j]) } result[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(d), p.dbRange), } sigmaEnvelope[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(t), p.dbRange), } resultIndex++ } return result, sigmaEnvelope } func (p Panorama) peaks() []core.PeakMark { correction := func(i int) core.Frequency { if i <= 0 || i >= len(p.fft.Data)-1 { return 0 } return core.Frequency((p.fft.Data[i+1] - p.fft.Data[i-1]) / (4*p.fft.Data[i] - 2*p.fft.Data[i-1] - 2*p.fft.Data[i+1])) } now := time.Now() for _, peakIndexRange := range p.fft.Peaks { peak := peak{ frequencyRange: core.FrequencyRange{From: p.fft.Frequency(peakIndexRange.From), To: p.fft.Frequency(peakIndexRange.To)}, maxFrequency: p.fft.Frequency(peakIndexRange.Max) + correction(peakIndexRange.Max), valueDB: core.DB(peakIndexRange.Value), lastSeen: now, } key := toPeakKey(peak.maxFrequency) p.peakBuffer[key] = peak } result := make([]core.PeakMark, 0, len(p.fft.Peaks)) for key, peak := range p.peakBuffer { age := now.Sub(peak.lastSeen) if age < p.peakTimeout && p.frequencyRange.Contains(peak.maxFrequency) { result = append(result, core.PeakMark{ FromX: core.ToFrequencyFrct(peak.frequencyRange.From, p.frequencyRange), ToX: core.ToFrequencyFrct(peak.frequencyRange.To, p.frequencyRange), MaxX: core.ToFrequencyFrct(peak.maxFrequency, p.frequencyRange), MaxFrequency: peak.maxFrequency, ValueY: core.ToDBFrct(peak.valueDB, p.dbRange), ValueDB: peak.valueDB, }) } else if age > 0 { p.peakBuffer[key] = peak } else if age >= p.peakTimeout || !p.frequencyRange.Contains(peak.maxFrequency) { delete(p.peakBuffer, key) } } return result } func (p Panorama) waterline(spectrum []core.FPoint) []core.Frct { length := int(p.width) binWidth := float64(length) / float64(len(spectrum)) result := make([]core.Frct, length) for _, point := range spectrum { center := float64(length-1) * float64(point.X) binFrom := center - binWidth/2 binTo := center + binWidth/2 for i := int(binFrom); i <= int(binTo+1); i++ { if 0 > i || i >= len(result) { continue } result[i] = core.Frct(math.Max(float64(result[i]), float64(point.Y))) } } return result }
SignalDetectionActive
identifier_name
panorama.go
package panorama import ( "log" "math" "time" "github.com/ftl/hamradio/bandplan" "github.com/ftl/panacotta/core" ) // Panorama controller type Panorama struct { width core.Px height core.Px frequencyRange core.FrequencyRange dbRange core.DBRange vfo core.VFO band bandplan.Band resolution map[core.ViewMode]core.HzPerPx viewMode core.ViewMode fullRangeMode bool margin float64 signalDetectionActive bool fft core.FFT peakBuffer map[peakKey]peak peakTimeout time.Duration dbRangeAdjusted bool } type peak struct { frequencyRange core.FrequencyRange maxFrequency core.Frequency valueDB core.DB lastSeen time.Time } type peakKey uint func toPeakKey(f core.Frequency) peakKey { return peakKey(f / 100.0) } const ( defaultFixedResolution = core.HzPerPx(100) defaultCenteredResolution = core.HzPerPx(25) ) // New returns a new instance of panorama. func New(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := Panorama{ width: width, frequencyRange: frequencyRange, dbRange: core.DBRange{From: -105, To: 10}, resolution: map[core.ViewMode]core.HzPerPx{ core.ViewFixed: calcResolution(frequencyRange, width), core.ViewCentered: defaultCenteredResolution, }, viewMode: core.ViewFixed, signalDetectionActive: true, margin: 0.02, peakBuffer: make(map[peakKey]peak), peakTimeout: 10 * time.Second, // TODO make this configurable dbRangeAdjusted: true, } result.vfo.Frequency = vfoFrequency return &result } // NewFullSpectrum returns a new instance of panorama in full-range mode. func NewFullSpectrum(width core.Px, frequencyRange core.FrequencyRange, vfoFrequency core.Frequency) *Panorama { result := New(width, frequencyRange, vfoFrequency) result.fullRangeMode = true return result } func calcResolution(frequencyRange core.FrequencyRange, width core.Px) core.HzPerPx { return core.HzPerPx(float64(frequencyRange.Width()) / float64(width)) } func (p *Panorama) updateFrequencyRange() { if math.IsNaN(float64(p.resolution[p.viewMode])) { p.setupFrequencyRange() return } var lowerRatio, upperRatio core.Frequency if p.viewMode == core.ViewFixed && p.frequencyRange.Contains(p.vfo.Frequency) { lowerRatio = (p.vfo.Frequency - p.frequencyRange.From) / p.frequencyRange.Width() lowerRatio = core.Frequency(math.Max(p.margin, math.Min(float64(lowerRatio), 1-p.margin))) upperRatio = 1.0 - lowerRatio } else { lowerRatio = 0.5 upperRatio = 0.5 } frequencyWidth := core.Frequency(float64(p.width) * float64(p.resolution[p.viewMode])) p.frequencyRange.From = p.vfo.Frequency - (lowerRatio * frequencyWidth) p.frequencyRange.To = p.vfo.Frequency + (upperRatio * frequencyWidth) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } func (p *Panorama) setupFrequencyRange() { if p.vfo.Frequency == 0 || !p.band.Contains(p.vfo.Frequency) { return } if p.viewMode == core.ViewFixed { p.frequencyRange.From = p.band.From - 1000 p.frequencyRange.To = p.band.To + 1000 } else { p.frequencyRange.From = p.vfo.Frequency - 20000 p.frequencyRange.From = p.vfo.Frequency + 20000 } p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) log.Printf("frequency range %v %v %v", p.frequencyRange, p.frequencyRange.Width(), p.resolution[p.viewMode]) } // SetSize in pixels func (p *Panorama) SetSize(width, height core.Px) { if (width == p.width) && (height == p.height) { return } log.Printf("width %v height %v", width, height) p.width = width p.height = height p.updateFrequencyRange() } // FrequencyRange of the panorama func (p Panorama) FrequencyRange() core.FrequencyRange { return p.frequencyRange } // From in Hz func (p Panorama) From() core.Frequency { return p.frequencyRange.From } // To in Hz func (p Panorama) To() core.Frequency { return p.frequencyRange.To } // Bandwidth in Hz func (p Panorama) Bandwidth() core.Frequency { return p.frequencyRange.Width() } // SetVFO in Hz func (p *Panorama) SetVFO(vfo core.VFO) { p.vfo = vfo if !p.band.Contains(vfo.Frequency) { band := bandplan.IARURegion1.ByFrequency(vfo.Frequency) if band.Width() > 0 { if p.band.Width() > 0 { p.dbRangeAdjusted = false } p.band = band } } log.Printf("vfo %v band %v", p.vfo, p.band) p.updateFrequencyRange() } func (p *Panorama) adjustDBRange() { if p.dbRangeAdjusted { return } dbWidth := p.dbRange.Width() p.dbRange.From = core.DB(p.fft.PeakThreshold) - 0.1*dbWidth p.dbRange.To = p.dbRange.From + dbWidth p.dbRangeAdjusted = true } // VFO frequency in Hz func (p Panorama) VFO() (vfo core.VFO, band bandplan.Band) { return p.vfo, p.band } // SetFFT data func (p *Panorama) SetFFT(fft core.FFT) { p.fft = fft p.adjustDBRange() } // ToggleSignalDetection switches the signal detection on and off. func (p *Panorama) ToggleSignalDetection() { p.signalDetectionActive = !p.signalDetectionActive } // SignalDetectionActive indicates if the signal detection is active or not. func (p *Panorama) SignalDetectionActive() bool { return p.signalDetectionActive } // ToggleViewMode switches to the other view mode. func (p *Panorama) ToggleViewMode() { if p.viewMode == core.ViewFixed { p.viewMode = core.ViewCentered } else { p.viewMode = core.ViewFixed } p.updateFrequencyRange() } // ViewMode returns the currently active view mode (centered or fixed). func (p *Panorama) ViewMode() core.ViewMode { return p.viewMode } // ZoomIn one step func (p *Panorama) ZoomIn() { p.resolution[p.viewMode] /= 1.25 p.updateFrequencyRange() } // ZoomOut one step func (p *Panorama) ZoomOut() { p.resolution[p.viewMode] *= 1.25 p.updateFrequencyRange() } // ZoomToBand of the current VFO frequency and switch to fixed view mode. func (p *Panorama) ZoomToBand() { if p.band.Width() == 0
p.zoomTo(p.band.Expanded(1000)) } func (p *Panorama) zoomTo(frequencyRange core.FrequencyRange) { p.viewMode = core.ViewFixed p.frequencyRange = frequencyRange p.resolution[p.viewMode] = calcResolution(p.frequencyRange, p.width) } // ResetZoom to the default of the current view mode func (p *Panorama) ResetZoom() { switch p.viewMode { case core.ViewFixed: p.resolution[p.viewMode] = defaultFixedResolution case core.ViewCentered: p.resolution[p.viewMode] = defaultCenteredResolution } p.updateFrequencyRange() } func (p *Panorama) FinerDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From += Δdb p.dbRange.To -= Δdb } func (p *Panorama) CoarserDynamicRange() { Δdb := p.dbRange.Width() * 0.05 p.dbRange.From -= Δdb p.dbRange.To += Δdb } func (p *Panorama) ShiftDynamicRange(ratio core.Frct) { Δdb := p.dbRange.Width() * core.DB(ratio) p.dbRange.From += Δdb p.dbRange.To += Δdb } func (p *Panorama) SetDynamicRange(dbRange core.DBRange) { p.dbRange = dbRange } // ShiftFrequencyRange shifts the panorama horizontally by the given ratio of the total width. func (p *Panorama) ShiftFrequencyRange(ratio core.Frct) { Δf := p.frequencyRange.Width() * core.Frequency(ratio) if p.viewMode == core.ViewFixed { p.frequencyRange.Shift(Δf) } } // Data to draw the current panorama. func (p Panorama) Data() core.Panorama { if p.fullRangeMode { return p.fullRangeData() } return p.data() } func (p Panorama) dataValid() bool { return !(len(p.fft.Data) == 0 || p.fft.Range.To < p.frequencyRange.From || p.fft.Range.From > p.frequencyRange.To) } func (p Panorama) data() core.Panorama { if !p.dataValid() { return core.Panorama{} } spectrum, sigmaEnvelope := p.spectrum() result := core.Panorama{ FrequencyRange: p.frequencyRange, VFO: p.vfo, Band: p.band, Resolution: p.resolution[p.viewMode], VFOLine: core.ToFrequencyFrct(p.vfo.Frequency, p.frequencyRange), VFOFilterFrom: core.ToFrequencyFrct(p.vfo.Frequency-p.vfo.FilterWidth/2, p.frequencyRange), VFOFilterTo: core.ToFrequencyFrct(p.vfo.Frequency+p.vfo.FilterWidth/2, p.frequencyRange), VFOSignalLevel: p.signalLevel(), FrequencyScale: p.frequencyScale(), DBScale: p.dbScale(), Spectrum: spectrum, SigmaEnvelope: sigmaEnvelope, PeakThresholdLevel: core.ToDBFrct(core.DB(p.fft.PeakThreshold), p.dbRange), Waterline: p.waterline(spectrum), } if p.signalDetectionActive { result.Peaks = p.peaks() } return result } func (p Panorama) signalLevel() core.DB { vfoIndex := p.fft.ToIndex(p.vfo.Frequency) if vfoIndex >= 0 && vfoIndex < len(p.fft.Data) { return core.DB(p.fft.Data[vfoIndex]) } return 0 } func (p Panorama) frequencyScale() []core.FrequencyMark { fZeros := float64(int(math.Log10(float64(p.frequencyRange.Width()))) - 1) fMagnitude := int(math.Pow(10, fZeros)) fFactor := fMagnitude if fFactor < 0 { return []core.FrequencyMark{} } for core.Frequency(fFactor)/p.frequencyRange.Width() < 0.1 { if fFactor%10 == 0 { fFactor *= 5 } else { fFactor *= 10 } } for core.Frequency(fFactor)/p.frequencyRange.Width() > 0.15 { if fFactor%10 == 0 { fFactor /= 5 } else { fFactor /= 10 } } freqScale := make([]core.FrequencyMark, 0, int(p.frequencyRange.Width())/fFactor) for f := core.Frequency((int(p.frequencyRange.From) / fFactor) * fFactor); f < p.frequencyRange.To; f += core.Frequency(fFactor) { mark := core.FrequencyMark{ X: core.ToFrequencyFrct(f, p.frequencyRange), Frequency: f, } freqScale = append(freqScale, mark) } return freqScale } func (p Panorama) dbScale() []core.DBMark { startDB := int(p.dbRange.From) - int(p.dbRange.From)%10 markCount := (int(p.dbRange.To) - startDB) / 10 if (int(p.dbRange.To)-startDB)%10 != 0 { markCount++ } dbScale := make([]core.DBMark, markCount) for i := range dbScale { db := core.DB(startDB + i*10) dbScale[i] = core.DBMark{ DB: db, Y: core.ToDBFrct(db, p.dbRange), } } return dbScale } func (p Panorama) spectrum() ([]core.FPoint, []core.FPoint) { fftResolution := p.fft.Resolution() step := int(math.Max(1, math.Floor(float64(len(p.fft.Data))/float64(p.width)))) start := int(math.Max(0, math.Floor(float64(p.frequencyRange.From-p.fft.Range.From)/fftResolution))) end := int(math.Min(float64(len(p.fft.Data)-1), math.Ceil(float64(p.frequencyRange.To-p.fft.Range.From)/fftResolution))) resultLength := (end - start + 1) / step if (end-start+1)%step != 0 { resultLength++ } result := make([]core.FPoint, resultLength) sigmaEnvelope := make([]core.FPoint, resultLength) resultIndex := 0 for i := start; i <= end; i += step { d := -1000.0 t := -1000.0 for j := i; j < i+step && j < len(p.fft.Data); j++ { d = math.Max(d, p.fft.Data[j]) t = math.Max(t, p.fft.SigmaEnvelope[j]) } result[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(d), p.dbRange), } sigmaEnvelope[resultIndex] = core.FPoint{ X: core.ToFrequencyFrct(p.fft.Frequency(i), p.frequencyRange), Y: core.ToDBFrct(core.DB(t), p.dbRange), } resultIndex++ } return result, sigmaEnvelope } func (p Panorama) peaks() []core.PeakMark { correction := func(i int) core.Frequency { if i <= 0 || i >= len(p.fft.Data)-1 { return 0 } return core.Frequency((p.fft.Data[i+1] - p.fft.Data[i-1]) / (4*p.fft.Data[i] - 2*p.fft.Data[i-1] - 2*p.fft.Data[i+1])) } now := time.Now() for _, peakIndexRange := range p.fft.Peaks { peak := peak{ frequencyRange: core.FrequencyRange{From: p.fft.Frequency(peakIndexRange.From), To: p.fft.Frequency(peakIndexRange.To)}, maxFrequency: p.fft.Frequency(peakIndexRange.Max) + correction(peakIndexRange.Max), valueDB: core.DB(peakIndexRange.Value), lastSeen: now, } key := toPeakKey(peak.maxFrequency) p.peakBuffer[key] = peak } result := make([]core.PeakMark, 0, len(p.fft.Peaks)) for key, peak := range p.peakBuffer { age := now.Sub(peak.lastSeen) if age < p.peakTimeout && p.frequencyRange.Contains(peak.maxFrequency) { result = append(result, core.PeakMark{ FromX: core.ToFrequencyFrct(peak.frequencyRange.From, p.frequencyRange), ToX: core.ToFrequencyFrct(peak.frequencyRange.To, p.frequencyRange), MaxX: core.ToFrequencyFrct(peak.maxFrequency, p.frequencyRange), MaxFrequency: peak.maxFrequency, ValueY: core.ToDBFrct(peak.valueDB, p.dbRange), ValueDB: peak.valueDB, }) } else if age > 0 { p.peakBuffer[key] = peak } else if age >= p.peakTimeout || !p.frequencyRange.Contains(peak.maxFrequency) { delete(p.peakBuffer, key) } } return result } func (p Panorama) waterline(spectrum []core.FPoint) []core.Frct { length := int(p.width) binWidth := float64(length) / float64(len(spectrum)) result := make([]core.Frct, length) for _, point := range spectrum { center := float64(length-1) * float64(point.X) binFrom := center - binWidth/2 binTo := center + binWidth/2 for i := int(binFrom); i <= int(binTo+1); i++ { if 0 > i || i >= len(result) { continue } result[i] = core.Frct(math.Max(float64(result[i]), float64(point.Y))) } } return result }
{ return }
conditional_block
intrinsicck.rs
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. use metadata::csearch; use middle::def::DefFn; use middle::subst::{Subst, Substs, EnumeratedItems}; use middle::ty::{TransmuteRestriction, ctxt, ty_bare_fn}; use middle::ty::{self, Ty}; use util::ppaux::Repr; use syntax::abi::RustIntrinsic; use syntax::ast::DefId; use syntax::ast; use syntax::ast_map::NodeForeignItem; use syntax::codemap::Span; use syntax::parse::token; use syntax::visit::Visitor; use syntax::visit; pub fn check_crate(tcx: &ctxt) { let mut visitor = IntrinsicCheckingVisitor { tcx: tcx, param_envs: Vec::new(), dummy_sized_ty: tcx.types.isize, dummy_unsized_ty: ty::mk_vec(tcx, tcx.types.isize, None), }; visit::walk_crate(&mut visitor, tcx.map.krate()); } struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> { tcx: &'a ctxt<'tcx>, // As we traverse the AST, we keep a stack of the parameter // environments for each function we encounter. When we find a // call to `transmute`, we can check it in the context of the top // of the stack (which ought not to be empty). param_envs: Vec<ty::ParameterEnvironment<'a,'tcx>>, // Dummy sized/unsized types that use to substitute for type // parameters in order to estimate how big a type will be for any // possible instantiation of the type parameters in scope. See // `check_transmute` for more details. dummy_sized_ty: Ty<'tcx>, dummy_unsized_ty: Ty<'tcx>, } impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> { fn
(&self, def_id: DefId) -> bool { let intrinsic = match ty::lookup_item_type(self.tcx, def_id).ty.sty { ty::ty_bare_fn(_, ref bfty) => bfty.abi == RustIntrinsic, _ => return false }; if def_id.krate == ast::LOCAL_CRATE { match self.tcx.map.get(def_id.node) { NodeForeignItem(ref item) if intrinsic => { token::get_ident(item.ident) == token::intern_and_get_ident("transmute") } _ => false, } } else { match csearch::get_item_path(self.tcx, def_id).last() { Some(ref last) if intrinsic => { token::get_name(last.name()) == token::intern_and_get_ident("transmute") } _ => false, } } } fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) { // Find the parameter environment for the most recent function that // we entered. let param_env = match self.param_envs.last() { Some(p) => p, None => { self.tcx.sess.span_bug( span, "transmute encountered outside of any fn"); } }; // Simple case: no type parameters involved. if !ty::type_has_params(from) && !ty::type_has_self(from) && !ty::type_has_params(to) && !ty::type_has_self(to) { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from, substituted_to: to, id: id, }; self.push_transmute_restriction(restriction); return; } // The rules around type parameters are a bit subtle. We are // checking these rules before monomorphization, so there may // be unsubstituted type parameters present in the // types. Obviously we cannot create LLVM types for those. // However, if a type parameter appears only indirectly (i.e., // through a pointer), it does not necessarily affect the // size, so that should be allowed. The only catch is that we // DO want to be careful around unsized type parameters, since // fat pointers have a different size than a thin pointer, and // hence `&T` and `&U` have different sizes if `T : Sized` but // `U : Sized` does not hold. // // However, it's not as simple as checking whether `T : // Sized`, because even if `T : Sized` does not hold, that // just means that `T` *may* not be sized. After all, even a // type parameter `T: ?Sized` could be bound to a sized // type. (Issue #20116) // // To handle this, we first check for "interior" type // parameters, which are always illegal. If there are none of // those, then we know that the only way that all type // parameters `T` are referenced indirectly, e.g. via a // pointer type like `&T`. In that case, we only care whether // `T` is sized or not, because that influences whether `&T` // is a thin or fat pointer. // // One could imagine establishing a sophisticated constraint // system to ensure that the transmute is legal, but instead // we do something brutally dumb. We just substitute dummy // sized or unsized types for every type parameter in scope, // exhaustively checking all possible combinations. Here are some examples: // // ``` // fn foo<T, U>() { // // T=int, U=int // } // // fn bar<T: ?Sized, U>() { // // T=int, U=int // // T=[int], U=int // } // // fn baz<T: ?Sized, U: ?Sized>() { // // T=int, U=int // // T=[int], U=int // // T=int, U=[int] // // T=[int], U=[int] // } // ``` // // In all cases, we keep the original unsubstituted types // around for error reporting. let from_tc = ty::type_contents(self.tcx, from); let to_tc = ty::type_contents(self.tcx, to); if from_tc.interior_param() || to_tc.interior_param() { span_err!(self.tcx.sess, span, E0139, "cannot transmute to or from a type that contains \ type parameters in its interior"); return; } let mut substs = param_env.free_substs.clone(); self.with_each_combination( span, param_env, param_env.free_substs.types.iter_enumerated(), &mut substs, &mut |substs| { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from.subst(self.tcx, substs), substituted_to: to.subst(self.tcx, substs), id: id, }; self.push_transmute_restriction(restriction); }); } fn with_each_combination(&self, span: Span, param_env: &ty::ParameterEnvironment<'a,'tcx>, mut types_in_scope: EnumeratedItems<Ty<'tcx>>, substs: &mut Substs<'tcx>, callback: &mut FnMut(&Substs<'tcx>)) { // This parameter invokes `callback` many times with different // substitutions that replace all the parameters in scope with // either `int` or `[int]`, depending on whether the type // parameter is known to be sized. See big comment above for // an explanation of why this is a reasonable thing to do. match types_in_scope.next() { None => { debug!("with_each_combination(substs={})", substs.repr(self.tcx)); callback(substs); } Some((space, index, &param_ty)) => { debug!("with_each_combination: space={:?}, index={}, param_ty={}", space, index, param_ty.repr(self.tcx)); if !ty::type_is_sized(param_env, span, param_ty) { debug!("with_each_combination: param_ty is not known to be sized"); substs.types.get_mut_slice(space)[index] = self.dummy_unsized_ty; self.with_each_combination(span, param_env, types_in_scope.clone(), substs, callback); } substs.types.get_mut_slice(space)[index] = self.dummy_sized_ty; self.with_each_combination(span, param_env, types_in_scope, substs, callback); } } } fn push_transmute_restriction(&self, restriction: TransmuteRestriction<'tcx>) { debug!("Pushing transmute restriction: {}", restriction.repr(self.tcx)); self.tcx.transmute_restrictions.borrow_mut().push(restriction); } } impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> { fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl, b: &'v ast::Block, s: Span, id: ast::NodeId) { match fk { visit::FkItemFn(..) | visit::FkMethod(..) => { let param_env = ty::ParameterEnvironment::for_item(self.tcx, id); self.param_envs.push(param_env); visit::walk_fn(self, fk, fd, b, s); self.param_envs.pop(); } visit::FkFnBlock(..) => { visit::walk_fn(self, fk, fd, b, s); } } } fn visit_expr(&mut self, expr: &ast::Expr) { if let ast::ExprPath(..) = expr.node { match ty::resolve_expr(self.tcx, expr) { DefFn(did, _) if self.def_id_is_transmute(did) => { let typ = ty::node_id_to_type(self.tcx, expr.id); match typ.sty { ty_bare_fn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => { if let ty::FnConverging(to) = bare_fn_ty.sig.0.output { let from = bare_fn_ty.sig.0.inputs[0]; self.check_transmute(expr.span, from, to, expr.id); } } _ => { self.tcx .sess .span_bug(expr.span, "transmute wasn't a bare fn?!"); } } } _ => {} } } visit::walk_expr(self, expr); } } impl<'tcx> Repr<'tcx> for TransmuteRestriction<'tcx> { fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String { format!("TransmuteRestriction(id={}, original=({},{}), substituted=({},{}))", self.id, self.original_from.repr(tcx), self.original_to.repr(tcx), self.substituted_from.repr(tcx), self.substituted_to.repr(tcx)) } }
def_id_is_transmute
identifier_name
intrinsicck.rs
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. use metadata::csearch; use middle::def::DefFn; use middle::subst::{Subst, Substs, EnumeratedItems}; use middle::ty::{TransmuteRestriction, ctxt, ty_bare_fn}; use middle::ty::{self, Ty}; use util::ppaux::Repr; use syntax::abi::RustIntrinsic; use syntax::ast::DefId; use syntax::ast; use syntax::ast_map::NodeForeignItem; use syntax::codemap::Span; use syntax::parse::token; use syntax::visit::Visitor; use syntax::visit; pub fn check_crate(tcx: &ctxt) { let mut visitor = IntrinsicCheckingVisitor { tcx: tcx, param_envs: Vec::new(), dummy_sized_ty: tcx.types.isize, dummy_unsized_ty: ty::mk_vec(tcx, tcx.types.isize, None), }; visit::walk_crate(&mut visitor, tcx.map.krate()); } struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> { tcx: &'a ctxt<'tcx>, // As we traverse the AST, we keep a stack of the parameter // environments for each function we encounter. When we find a // call to `transmute`, we can check it in the context of the top // of the stack (which ought not to be empty). param_envs: Vec<ty::ParameterEnvironment<'a,'tcx>>, // Dummy sized/unsized types that use to substitute for type // parameters in order to estimate how big a type will be for any // possible instantiation of the type parameters in scope. See // `check_transmute` for more details. dummy_sized_ty: Ty<'tcx>, dummy_unsized_ty: Ty<'tcx>, } impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> { fn def_id_is_transmute(&self, def_id: DefId) -> bool { let intrinsic = match ty::lookup_item_type(self.tcx, def_id).ty.sty { ty::ty_bare_fn(_, ref bfty) => bfty.abi == RustIntrinsic, _ => return false }; if def_id.krate == ast::LOCAL_CRATE { match self.tcx.map.get(def_id.node) { NodeForeignItem(ref item) if intrinsic => { token::get_ident(item.ident) == token::intern_and_get_ident("transmute") } _ => false, } } else { match csearch::get_item_path(self.tcx, def_id).last() { Some(ref last) if intrinsic => { token::get_name(last.name()) == token::intern_and_get_ident("transmute") } _ => false, } } } fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) { // Find the parameter environment for the most recent function that // we entered. let param_env = match self.param_envs.last() { Some(p) => p, None => { self.tcx.sess.span_bug( span, "transmute encountered outside of any fn"); } }; // Simple case: no type parameters involved. if !ty::type_has_params(from) && !ty::type_has_self(from) && !ty::type_has_params(to) && !ty::type_has_self(to) { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from, substituted_to: to, id: id, }; self.push_transmute_restriction(restriction); return; } // The rules around type parameters are a bit subtle. We are // checking these rules before monomorphization, so there may // be unsubstituted type parameters present in the // types. Obviously we cannot create LLVM types for those. // However, if a type parameter appears only indirectly (i.e., // through a pointer), it does not necessarily affect the // size, so that should be allowed. The only catch is that we // DO want to be careful around unsized type parameters, since // fat pointers have a different size than a thin pointer, and // hence `&T` and `&U` have different sizes if `T : Sized` but // `U : Sized` does not hold. // // However, it's not as simple as checking whether `T : // Sized`, because even if `T : Sized` does not hold, that // just means that `T` *may* not be sized. After all, even a // type parameter `T: ?Sized` could be bound to a sized // type. (Issue #20116) // // To handle this, we first check for "interior" type // parameters, which are always illegal. If there are none of // those, then we know that the only way that all type // parameters `T` are referenced indirectly, e.g. via a // pointer type like `&T`. In that case, we only care whether // `T` is sized or not, because that influences whether `&T` // is a thin or fat pointer. // // One could imagine establishing a sophisticated constraint // system to ensure that the transmute is legal, but instead // we do something brutally dumb. We just substitute dummy // sized or unsized types for every type parameter in scope, // exhaustively checking all possible combinations. Here are some examples: // // ``` // fn foo<T, U>() { // // T=int, U=int // } // // fn bar<T: ?Sized, U>() { // // T=int, U=int // // T=[int], U=int // } // // fn baz<T: ?Sized, U: ?Sized>() { // // T=int, U=int // // T=[int], U=int // // T=int, U=[int] // // T=[int], U=[int] // } // ``` // // In all cases, we keep the original unsubstituted types // around for error reporting. let from_tc = ty::type_contents(self.tcx, from); let to_tc = ty::type_contents(self.tcx, to); if from_tc.interior_param() || to_tc.interior_param() { span_err!(self.tcx.sess, span, E0139, "cannot transmute to or from a type that contains \ type parameters in its interior"); return; } let mut substs = param_env.free_substs.clone(); self.with_each_combination( span, param_env, param_env.free_substs.types.iter_enumerated(), &mut substs, &mut |substs| { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from.subst(self.tcx, substs), substituted_to: to.subst(self.tcx, substs), id: id, }; self.push_transmute_restriction(restriction); }); } fn with_each_combination(&self, span: Span, param_env: &ty::ParameterEnvironment<'a,'tcx>, mut types_in_scope: EnumeratedItems<Ty<'tcx>>, substs: &mut Substs<'tcx>, callback: &mut FnMut(&Substs<'tcx>)) { // This parameter invokes `callback` many times with different // substitutions that replace all the parameters in scope with // either `int` or `[int]`, depending on whether the type // parameter is known to be sized. See big comment above for // an explanation of why this is a reasonable thing to do. match types_in_scope.next() { None => { debug!("with_each_combination(substs={})", substs.repr(self.tcx)); callback(substs); } Some((space, index, &param_ty)) => { debug!("with_each_combination: space={:?}, index={}, param_ty={}", space, index, param_ty.repr(self.tcx)); if !ty::type_is_sized(param_env, span, param_ty) { debug!("with_each_combination: param_ty is not known to be sized"); substs.types.get_mut_slice(space)[index] = self.dummy_unsized_ty; self.with_each_combination(span, param_env, types_in_scope.clone(), substs, callback); } substs.types.get_mut_slice(space)[index] = self.dummy_sized_ty; self.with_each_combination(span, param_env, types_in_scope, substs, callback); } } } fn push_transmute_restriction(&self, restriction: TransmuteRestriction<'tcx>)
} impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> { fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl, b: &'v ast::Block, s: Span, id: ast::NodeId) { match fk { visit::FkItemFn(..) | visit::FkMethod(..) => { let param_env = ty::ParameterEnvironment::for_item(self.tcx, id); self.param_envs.push(param_env); visit::walk_fn(self, fk, fd, b, s); self.param_envs.pop(); } visit::FkFnBlock(..) => { visit::walk_fn(self, fk, fd, b, s); } } } fn visit_expr(&mut self, expr: &ast::Expr) { if let ast::ExprPath(..) = expr.node { match ty::resolve_expr(self.tcx, expr) { DefFn(did, _) if self.def_id_is_transmute(did) => { let typ = ty::node_id_to_type(self.tcx, expr.id); match typ.sty { ty_bare_fn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => { if let ty::FnConverging(to) = bare_fn_ty.sig.0.output { let from = bare_fn_ty.sig.0.inputs[0]; self.check_transmute(expr.span, from, to, expr.id); } } _ => { self.tcx .sess .span_bug(expr.span, "transmute wasn't a bare fn?!"); } } } _ => {} } } visit::walk_expr(self, expr); } } impl<'tcx> Repr<'tcx> for TransmuteRestriction<'tcx> { fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String { format!("TransmuteRestriction(id={}, original=({},{}), substituted=({},{}))", self.id, self.original_from.repr(tcx), self.original_to.repr(tcx), self.substituted_from.repr(tcx), self.substituted_to.repr(tcx)) } }
{ debug!("Pushing transmute restriction: {}", restriction.repr(self.tcx)); self.tcx.transmute_restrictions.borrow_mut().push(restriction); }
identifier_body
intrinsicck.rs
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. use metadata::csearch; use middle::def::DefFn; use middle::subst::{Subst, Substs, EnumeratedItems}; use middle::ty::{TransmuteRestriction, ctxt, ty_bare_fn}; use middle::ty::{self, Ty}; use util::ppaux::Repr; use syntax::abi::RustIntrinsic; use syntax::ast::DefId; use syntax::ast;
use syntax::codemap::Span; use syntax::parse::token; use syntax::visit::Visitor; use syntax::visit; pub fn check_crate(tcx: &ctxt) { let mut visitor = IntrinsicCheckingVisitor { tcx: tcx, param_envs: Vec::new(), dummy_sized_ty: tcx.types.isize, dummy_unsized_ty: ty::mk_vec(tcx, tcx.types.isize, None), }; visit::walk_crate(&mut visitor, tcx.map.krate()); } struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> { tcx: &'a ctxt<'tcx>, // As we traverse the AST, we keep a stack of the parameter // environments for each function we encounter. When we find a // call to `transmute`, we can check it in the context of the top // of the stack (which ought not to be empty). param_envs: Vec<ty::ParameterEnvironment<'a,'tcx>>, // Dummy sized/unsized types that use to substitute for type // parameters in order to estimate how big a type will be for any // possible instantiation of the type parameters in scope. See // `check_transmute` for more details. dummy_sized_ty: Ty<'tcx>, dummy_unsized_ty: Ty<'tcx>, } impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> { fn def_id_is_transmute(&self, def_id: DefId) -> bool { let intrinsic = match ty::lookup_item_type(self.tcx, def_id).ty.sty { ty::ty_bare_fn(_, ref bfty) => bfty.abi == RustIntrinsic, _ => return false }; if def_id.krate == ast::LOCAL_CRATE { match self.tcx.map.get(def_id.node) { NodeForeignItem(ref item) if intrinsic => { token::get_ident(item.ident) == token::intern_and_get_ident("transmute") } _ => false, } } else { match csearch::get_item_path(self.tcx, def_id).last() { Some(ref last) if intrinsic => { token::get_name(last.name()) == token::intern_and_get_ident("transmute") } _ => false, } } } fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) { // Find the parameter environment for the most recent function that // we entered. let param_env = match self.param_envs.last() { Some(p) => p, None => { self.tcx.sess.span_bug( span, "transmute encountered outside of any fn"); } }; // Simple case: no type parameters involved. if !ty::type_has_params(from) && !ty::type_has_self(from) && !ty::type_has_params(to) && !ty::type_has_self(to) { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from, substituted_to: to, id: id, }; self.push_transmute_restriction(restriction); return; } // The rules around type parameters are a bit subtle. We are // checking these rules before monomorphization, so there may // be unsubstituted type parameters present in the // types. Obviously we cannot create LLVM types for those. // However, if a type parameter appears only indirectly (i.e., // through a pointer), it does not necessarily affect the // size, so that should be allowed. The only catch is that we // DO want to be careful around unsized type parameters, since // fat pointers have a different size than a thin pointer, and // hence `&T` and `&U` have different sizes if `T : Sized` but // `U : Sized` does not hold. // // However, it's not as simple as checking whether `T : // Sized`, because even if `T : Sized` does not hold, that // just means that `T` *may* not be sized. After all, even a // type parameter `T: ?Sized` could be bound to a sized // type. (Issue #20116) // // To handle this, we first check for "interior" type // parameters, which are always illegal. If there are none of // those, then we know that the only way that all type // parameters `T` are referenced indirectly, e.g. via a // pointer type like `&T`. In that case, we only care whether // `T` is sized or not, because that influences whether `&T` // is a thin or fat pointer. // // One could imagine establishing a sophisticated constraint // system to ensure that the transmute is legal, but instead // we do something brutally dumb. We just substitute dummy // sized or unsized types for every type parameter in scope, // exhaustively checking all possible combinations. Here are some examples: // // ``` // fn foo<T, U>() { // // T=int, U=int // } // // fn bar<T: ?Sized, U>() { // // T=int, U=int // // T=[int], U=int // } // // fn baz<T: ?Sized, U: ?Sized>() { // // T=int, U=int // // T=[int], U=int // // T=int, U=[int] // // T=[int], U=[int] // } // ``` // // In all cases, we keep the original unsubstituted types // around for error reporting. let from_tc = ty::type_contents(self.tcx, from); let to_tc = ty::type_contents(self.tcx, to); if from_tc.interior_param() || to_tc.interior_param() { span_err!(self.tcx.sess, span, E0139, "cannot transmute to or from a type that contains \ type parameters in its interior"); return; } let mut substs = param_env.free_substs.clone(); self.with_each_combination( span, param_env, param_env.free_substs.types.iter_enumerated(), &mut substs, &mut |substs| { let restriction = TransmuteRestriction { span: span, original_from: from, original_to: to, substituted_from: from.subst(self.tcx, substs), substituted_to: to.subst(self.tcx, substs), id: id, }; self.push_transmute_restriction(restriction); }); } fn with_each_combination(&self, span: Span, param_env: &ty::ParameterEnvironment<'a,'tcx>, mut types_in_scope: EnumeratedItems<Ty<'tcx>>, substs: &mut Substs<'tcx>, callback: &mut FnMut(&Substs<'tcx>)) { // This parameter invokes `callback` many times with different // substitutions that replace all the parameters in scope with // either `int` or `[int]`, depending on whether the type // parameter is known to be sized. See big comment above for // an explanation of why this is a reasonable thing to do. match types_in_scope.next() { None => { debug!("with_each_combination(substs={})", substs.repr(self.tcx)); callback(substs); } Some((space, index, &param_ty)) => { debug!("with_each_combination: space={:?}, index={}, param_ty={}", space, index, param_ty.repr(self.tcx)); if !ty::type_is_sized(param_env, span, param_ty) { debug!("with_each_combination: param_ty is not known to be sized"); substs.types.get_mut_slice(space)[index] = self.dummy_unsized_ty; self.with_each_combination(span, param_env, types_in_scope.clone(), substs, callback); } substs.types.get_mut_slice(space)[index] = self.dummy_sized_ty; self.with_each_combination(span, param_env, types_in_scope, substs, callback); } } } fn push_transmute_restriction(&self, restriction: TransmuteRestriction<'tcx>) { debug!("Pushing transmute restriction: {}", restriction.repr(self.tcx)); self.tcx.transmute_restrictions.borrow_mut().push(restriction); } } impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> { fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl, b: &'v ast::Block, s: Span, id: ast::NodeId) { match fk { visit::FkItemFn(..) | visit::FkMethod(..) => { let param_env = ty::ParameterEnvironment::for_item(self.tcx, id); self.param_envs.push(param_env); visit::walk_fn(self, fk, fd, b, s); self.param_envs.pop(); } visit::FkFnBlock(..) => { visit::walk_fn(self, fk, fd, b, s); } } } fn visit_expr(&mut self, expr: &ast::Expr) { if let ast::ExprPath(..) = expr.node { match ty::resolve_expr(self.tcx, expr) { DefFn(did, _) if self.def_id_is_transmute(did) => { let typ = ty::node_id_to_type(self.tcx, expr.id); match typ.sty { ty_bare_fn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => { if let ty::FnConverging(to) = bare_fn_ty.sig.0.output { let from = bare_fn_ty.sig.0.inputs[0]; self.check_transmute(expr.span, from, to, expr.id); } } _ => { self.tcx .sess .span_bug(expr.span, "transmute wasn't a bare fn?!"); } } } _ => {} } } visit::walk_expr(self, expr); } } impl<'tcx> Repr<'tcx> for TransmuteRestriction<'tcx> { fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String { format!("TransmuteRestriction(id={}, original=({},{}), substituted=({},{}))", self.id, self.original_from.repr(tcx), self.original_to.repr(tcx), self.substituted_from.repr(tcx), self.substituted_to.repr(tcx)) } }
use syntax::ast_map::NodeForeignItem;
random_line_split
beautyleg7_spider.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import hashlib import re from datetime import datetime import gevent import requests import scrapy from gevent.pool import Pool from lxml import etree from scrapy.http import HtmlResponse from sqlalchemy import create_engine, func from sqlalchemy.orm import sessionmaker from ..items import Album, AlbumImageRelationItem, AlbumItem, AlbumImageItem from ..utils.const import const from ..utils.redis_util import get_redis_conn_from_pool class Beautyleg7Spider(scrapy.Spider): name = 'Beautyleg7Spider' category_list = ['siwameitui', 'xingganmeinv', 'weimeixiezhen', 'ribenmeinv'] start_urls = [('http://www.beautyleg7.com/' + category) for category in category_list] const.REPEATED_THRESHOLD = 10 def __init__(self, name=None, **kwargs): super().__init__(name=None, **kwargs) self.db_session = None self.gevent_pool = Pool(32) self.redis_cmd = get_redis_conn_from_pool() self.ALBUM_URL_REDIS_KEY_PREFIX = "album_url" self.REDIS_LIMITER = ":" self.album_last_item_redis_unique_key = "" self.album_item = None self.album_image_item_list = [] self.album_image_relation_item = AlbumImageRelationItem() def start_requests(self): mysql_host = self.crawler.settings.get("MYSQL_HOST") mysql_port = self.crawler.settings.get("MYSQL_PORT") mysql_user = self.crawler.settings.get("MYSQL_USER") mysql_password = self.crawler.settings.get("MYSQL_PASSWORD") mysql_db_name = self.crawler.settings.get("MYSQL_DB_NAME") engine = create_engine('mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(mysql_user, mysql_password, mysql_host, mysql_port, mysql_db_name), pool_recycle=180, echo=False) session_maker = sessionmaker(bind=engine) self.db_session = session_maker() for url in self.start_urls: yield scrapy.Request(url) def parse(self, response): if self.db_session is None: self.logger.error("db_session is None") return None repeated_count = 0 if response is None: self.logger.warn("响应为空,不做处理!") else: album_nodes = response.css('.pic .item') category = response.css('.sitepath a')[1].css('a::text').extract_first().strip() # 判断最后一页的最后主题是否被持久化 is_persisted_last_item = self.redis_cmd.get(self.album_last_item_redis_unique_key) is_last_item_finished = False if is_persisted_last_item is not None and int(is_persisted_last_item): is_last_item_finished = True self.logger.info("已持久化最后一页的最后主题:%s" % self.album_last_item_redis_unique_key) # 如果是最后一页则设置Redis存储key:“最后一页页码:最后一条主题url”,value:is_persisted(取值为0或1,默认为0) album_last_page_url = response.meta.get("album_last_page_url") if album_last_page_url is not None: album_last_page_url_last_item_redis_suffix = album_nodes[-1].css('.p a::attr(href)').extract_first() self.album_last_item_redis_unique_key = self.ALBUM_URL_REDIS_KEY_PREFIX + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url, "") + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url_last_item_redis_suffix, "") self.redis_cmd.setnx(self.album_last_item_redis_unique_key, 0) for album_node in album_nodes: album_url = album_node.css('.p a::attr(href)').extract_first().strip() # 判断当前主题url是否已持久化 is_persisted = self.redis_cmd.get(album_url) if is_persisted is not None and int(is_persisted): self.logger.info("Redis中该url album_url:%s已持久化" % album_url) continue album_url_object_id = self.get_md5(album_url) # 只有name不存在时,当前set操作才执行 self.redis_cmd.setnx(album_url, 0) count = 0 try: count = self.db_session.query(func.count()).filter( Album.album_url_object_id == album_url_object_id).first() if count: count = count[0] except Exception as e: self.logger.error("查询数据库异常,原因:{}".format(e)) finally: self.db_session.rollback() if count: self.logger.info("数据库已有该数据album_url_object_id:%s" % album_url_object_id) repeated_count += 1 # 只有name存在时,当前set操作才执行 self.redis_cmd.set(album_url, 1, xx=True) continue else: album_item = self.parse_album_item(album_node, album_url, album_url_object_id, category) yield response.follow(url=album_url, meta={"AlbumItem": album_item}, callback=self.parse_detail) # 提取下一页并交给scrapy下载 selector_list = response.css('.page li a::attr(href)') # 如果最后一页的最后一个主题url未被持久化则继续爬取 if not is_last_item_finished: if selector_list: last_page_url = None current_url_page = response.xpath('//li[@class="thisclass"]//text()').extract_first() # 如果当前页是第一页则获取最后一页url if current_url_page and int(current_url_page) == 1: last_page_url = selector_list[-1].extract() next_url = selector_list[-2].extract() if next_url == last_page_url: album_last_page_url = response.urljoin(last_page_url) self.logger.info("Last page:%s" % album_last_page_url) else: self.logger.info("Next page:%s" % response.urljoin(next_url)) yield response.follow(url=next_url, meta={"album_last_page_url": album_last_page_url}, callback=self.parse) else: self.logger.info("selector_list is None") self.logger.info("重复次数:%s" % repeated_count) else: self.logger.info("Stop crawler. None Next page!") def parse_album_item(self, album_node, album_url, album_url_object_id, category): album_title = album_node.css('.p a img::attr(alt)').extract_first().strip() cover_url = album_node.css('.p a img::attr(src)').extract_first().strip() regex = "\d+\.\d+.\d+\s+No\.\d+|\d+\-\d+-\d+\s+No\.\d+" number_group = re.findall(regex, album_title) if len(number_group) > 0: number = number_group[0] else: number = "No.unknown" create_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') album_item = AlbumItem() album_item['category'] = category album_item['album_url'] = album_url album_item['album_url_object_id'] = album_url_object_id album_item['album_title'] = album_title album_item['cover_url'] = cover_url album_item['number'] = number album_item['create_date'] = create_date return album_item def parse_detail(self, response): self.album_item = response.meta.get("AlbumItem") self.album_image_relation_item['album_item'] = self.album_item self.parse_album_image_item(response) # 详情页分页链接,循环生成所有子页面的请求 relative_next_page_list = response.css('.page li a::attr(href)').extract() # 使用gevent协程池提升网络IO处理效率 next_page_threads = [ self.gevent_pool.spawn(self.get_album_image_item_list, response.urljoin(relative_next_page)) for relative_next_page in relative_next_page_list[2:-1] ] gevent.joinall(next_page_threads) self.album_image_relation_item['album_image_item_list'] = self.album_image_item_list # 重新初始化 self.album_image_item_list = [] yield self.album_image_relation_item def get_album_image_item_list(self, abs_next_page): """ 使用下页绝对路径同步请求 :param abs_next_page: :return: """ resp = requests.get(abs_next_page) if resp.status_code == 200: encoding = requests.utils.get_encodings_from_content(resp.text) resp.encoding = encoding[0] self.parse_album_image_item(etree.HTML(resp.text)) else: self.logger.warn("下载此页{}失败,返回的状态码为{}".format(abs_next_page, resp.status_code)) def parse_album_image_item(self, response): """ 解析item并返回给pipelines :param response: 如果response类型是继承自scrapy的TextResponse类则使用scrapy的Selector来解析,否则使用lxml来解析 :return: """ if isinstance(response, HtmlResponse): item_title = response.xpath('//div[@class="content"]/h1/text()').extract_first().strip() publish_date = response.xpath('//div[@class="tit"]/span/text()').extract_first().split(":")[1] image_link_list = response.css('.contents a img::attr(src)').extract() else: item_title = response.xpath('//div[@class="content"]/h1/text()')[0].strip() publish_date = response.xpath('//div[@class="tit"]/span/text()')[0].split(":")[1] image_link_list = response.xpath('//div[@class="contents"]/a/img') image_link_list = [image_link.attrib['src'] for image_link in image_link_list] regex = "\s?\w+[^\w]?" regex_group = re.findall(regex, item_title) stage_name = "unknown" if len(regex_group) > 0: str = regex_group[-1] if "[" in str: stage_name = str.split("[")[0].strip() elif "(" in str: stage_name = str.split("(")[0].strip() elif re.match('[^\d*]', str): stage_name = re.match('[^\d*]', str).group() # 详情页多个图片链接 for image_url in image_link_list: album_image_item = AlbumImageItem() album_image_item['item_url'] = image_url album_image_item['item_url_object_id'] = self.get_md5(image_url) item_url_list_json = "{}" album_image_item['item_url_list_json'] = item_url_list_json album_image_item['item_title'] = item_title album_image_item['stage_name'] = stage_name album_image_item['publish_date'] = publish_date self.album_image_item_list.append(album_image_item) return self.album_image_item_list @staticmethod def get_md5(param): if isinstance(param, str): param = param.encode() m = hashlib.md5() m.update(param) return m.hexdigest() @staticmethod def sub_url_scheme(website, replace_str): scheme_regex = "^(http://|https://)" return re.sub(scheme_regex, replace_str, website)
identifier_body
beautyleg7_spider.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import hashlib import re from datetime import datetime import gevent import requests import scrapy from gevent.pool import Pool from lxml import etree from scrapy.http import HtmlResponse from sqlalchemy import create_engine, func from sqlalchemy.orm import sessionmaker from ..items import Album, AlbumImageRelationItem, AlbumItem, AlbumImageItem from ..utils.const import const from ..utils.redis_util import get_redis_conn_from_pool class Beautyleg7Spider(scrapy.Spider): name = 'Beautyleg7Spider' category_list = ['siwameitui', 'xingganmeinv', 'weimeixiezhen', 'ribenmeinv'] start_urls = [('http://www.beautyleg7.com/' + category) for category in category_list] const.REPEATED_THRESHOLD = 10 def __init__(self, name=None, **kwargs): super().__init__(name=None, **kwargs) self.db_session = None self.gevent_pool = Pool(32) self.redis_cmd = get_redis_conn_from_pool() self.ALBUM_URL_REDIS_KEY_PREFIX = "album_url" self.REDIS_LIMITER = ":" self.album_last_item_redis_unique_key = "" self.album_item = None self.album_image_item_list = [] self.album_image_relation_item = AlbumImageRelationItem() def start_requests(self): mysql_host = self.crawler.settings.get("MYSQL_HOST") mysql_port = self.crawler.settings.get("MYSQL_PORT") mysql_user = self.crawler.settings.get("MYSQL_USER") mysql_password = self.crawler.settings.get("MYSQL_PASSWORD") mysql_db_name = self.crawler.settings.get("MYSQL_DB_NAME") engine = create_engine('mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(mysql_user, mysql_password, mysql_host, mysql_port, mysql_db_name), pool_recycle=180, echo=False) session_maker = sessionmaker(bind=engine) self.db_session = session_maker() for url in self.start_urls: yield scrapy.Request(url) def parse(self, response): if self.db_session is None: self.logger.error("db_session is None") return None repeated_count = 0 if response is None: self.logger.warn("响应为空,不做处理!") else: album_nodes = respon
: number = number_group[0] else: number = "No.unknown" create_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') album_item = AlbumItem() album_item['category'] = category album_item['album_url'] = album_url album_item['album_url_object_id'] = album_url_object_id album_item['album_title'] = album_title album_item['cover_url'] = cover_url album_item['number'] = number album_item['create_date'] = create_date return album_item def parse_detail(self, response): self.album_item = response.meta.get("AlbumItem") self.album_image_relation_item['album_item'] = self.album_item self.parse_album_image_item(response) # 详情页分页链接,循环生成所有子页面的请求 relative_next_page_list = response.css('.page li a::attr(href)').extract() # 使用gevent协程池提升网络IO处理效率 next_page_threads = [ self.gevent_pool.spawn(self.get_album_image_item_list, response.urljoin(relative_next_page)) for relative_next_page in relative_next_page_list[2:-1] ] gevent.joinall(next_page_threads) self.album_image_relation_item['album_image_item_list'] = self.album_image_item_list # 重新初始化 self.album_image_item_list = [] yield self.album_image_relation_item def get_album_image_item_list(self, abs_next_page): """ 使用下页绝对路径同步请求 :param abs_next_page: :return: """ resp = requests.get(abs_next_page) if resp.status_code == 200: encoding = requests.utils.get_encodings_from_content(resp.text) resp.encoding = encoding[0] self.parse_album_image_item(etree.HTML(resp.text)) else: self.logger.warn("下载此页{}失败,返回的状态码为{}".format(abs_next_page, resp.status_code)) def parse_album_image_item(self, response): """ 解析item并返回给pipelines :param response: 如果response类型是继承自scrapy的TextResponse类则使用scrapy的Selector来解析,否则使用lxml来解析 :return: """ if isinstance(response, HtmlResponse): item_title = response.xpath('//div[@class="content"]/h1/text()').extract_first().strip() publish_date = response.xpath('//div[@class="tit"]/span/text()').extract_first().split(":")[1] image_link_list = response.css('.contents a img::attr(src)').extract() else: item_title = response.xpath('//div[@class="content"]/h1/text()')[0].strip() publish_date = response.xpath('//div[@class="tit"]/span/text()')[0].split(":")[1] image_link_list = response.xpath('//div[@class="contents"]/a/img') image_link_list = [image_link.attrib['src'] for image_link in image_link_list] regex = "\s?\w+[^\w]?" regex_group = re.findall(regex, item_title) stage_name = "unknown" if len(regex_group) > 0: str = regex_group[-1] if "[" in str: stage_name = str.split("[")[0].strip() elif "(" in str: stage_name = str.split("(")[0].strip() elif re.match('[^\d*]', str): stage_name = re.match('[^\d*]', str).group() # 详情页多个图片链接 for image_url in image_link_list: album_image_item = AlbumImageItem() album_image_item['item_url'] = image_url album_image_item['item_url_object_id'] = self.get_md5(image_url) item_url_list_json = "{}" album_image_item['item_url_list_json'] = item_url_list_json album_image_item['item_title'] = item_title album_image_item['stage_name'] = stage_name album_image_item['publish_date'] = publish_date self.album_image_item_list.append(album_image_item) return self.album_image_item_list @staticmethod def get_md5(param): if isinstance(param, str): param = param.encode() m = hashlib.md5() m.update(param) return m.hexdigest() @staticmethod def sub_url_scheme(website, replace_str): scheme_regex = "^(http://|https://)" return re.sub(scheme_regex, replace_str, website)
se.css('.pic .item') category = response.css('.sitepath a')[1].css('a::text').extract_first().strip() # 判断最后一页的最后主题是否被持久化 is_persisted_last_item = self.redis_cmd.get(self.album_last_item_redis_unique_key) is_last_item_finished = False if is_persisted_last_item is not None and int(is_persisted_last_item): is_last_item_finished = True self.logger.info("已持久化最后一页的最后主题:%s" % self.album_last_item_redis_unique_key) # 如果是最后一页则设置Redis存储key:“最后一页页码:最后一条主题url”,value:is_persisted(取值为0或1,默认为0) album_last_page_url = response.meta.get("album_last_page_url") if album_last_page_url is not None: album_last_page_url_last_item_redis_suffix = album_nodes[-1].css('.p a::attr(href)').extract_first() self.album_last_item_redis_unique_key = self.ALBUM_URL_REDIS_KEY_PREFIX + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url, "") + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url_last_item_redis_suffix, "") self.redis_cmd.setnx(self.album_last_item_redis_unique_key, 0) for album_node in album_nodes: album_url = album_node.css('.p a::attr(href)').extract_first().strip() # 判断当前主题url是否已持久化 is_persisted = self.redis_cmd.get(album_url) if is_persisted is not None and int(is_persisted): self.logger.info("Redis中该url album_url:%s已持久化" % album_url) continue album_url_object_id = self.get_md5(album_url) # 只有name不存在时,当前set操作才执行 self.redis_cmd.setnx(album_url, 0) count = 0 try: count = self.db_session.query(func.count()).filter( Album.album_url_object_id == album_url_object_id).first() if count: count = count[0] except Exception as e: self.logger.error("查询数据库异常,原因:{}".format(e)) finally: self.db_session.rollback() if count: self.logger.info("数据库已有该数据album_url_object_id:%s" % album_url_object_id) repeated_count += 1 # 只有name存在时,当前set操作才执行 self.redis_cmd.set(album_url, 1, xx=True) continue else: album_item = self.parse_album_item(album_node, album_url, album_url_object_id, category) yield response.follow(url=album_url, meta={"AlbumItem": album_item}, callback=self.parse_detail) # 提取下一页并交给scrapy下载 selector_list = response.css('.page li a::attr(href)') # 如果最后一页的最后一个主题url未被持久化则继续爬取 if not is_last_item_finished: if selector_list: last_page_url = None current_url_page = response.xpath('//li[@class="thisclass"]//text()').extract_first() # 如果当前页是第一页则获取最后一页url if current_url_page and int(current_url_page) == 1: last_page_url = selector_list[-1].extract() next_url = selector_list[-2].extract() if next_url == last_page_url: album_last_page_url = response.urljoin(last_page_url) self.logger.info("Last page:%s" % album_last_page_url) else: self.logger.info("Next page:%s" % response.urljoin(next_url)) yield response.follow(url=next_url, meta={"album_last_page_url": album_last_page_url}, callback=self.parse) else: self.logger.info("selector_list is None") self.logger.info("重复次数:%s" % repeated_count) else: self.logger.info("Stop crawler. None Next page!") def parse_album_item(self, album_node, album_url, album_url_object_id, category): album_title = album_node.css('.p a img::attr(alt)').extract_first().strip() cover_url = album_node.css('.p a img::attr(src)').extract_first().strip() regex = "\d+\.\d+.\d+\s+No\.\d+|\d+\-\d+-\d+\s+No\.\d+" number_group = re.findall(regex, album_title) if len(number_group) > 0
conditional_block
beautyleg7_spider.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import hashlib
import gevent import requests import scrapy from gevent.pool import Pool from lxml import etree from scrapy.http import HtmlResponse from sqlalchemy import create_engine, func from sqlalchemy.orm import sessionmaker from ..items import Album, AlbumImageRelationItem, AlbumItem, AlbumImageItem from ..utils.const import const from ..utils.redis_util import get_redis_conn_from_pool class Beautyleg7Spider(scrapy.Spider): name = 'Beautyleg7Spider' category_list = ['siwameitui', 'xingganmeinv', 'weimeixiezhen', 'ribenmeinv'] start_urls = [('http://www.beautyleg7.com/' + category) for category in category_list] const.REPEATED_THRESHOLD = 10 def __init__(self, name=None, **kwargs): super().__init__(name=None, **kwargs) self.db_session = None self.gevent_pool = Pool(32) self.redis_cmd = get_redis_conn_from_pool() self.ALBUM_URL_REDIS_KEY_PREFIX = "album_url" self.REDIS_LIMITER = ":" self.album_last_item_redis_unique_key = "" self.album_item = None self.album_image_item_list = [] self.album_image_relation_item = AlbumImageRelationItem() def start_requests(self): mysql_host = self.crawler.settings.get("MYSQL_HOST") mysql_port = self.crawler.settings.get("MYSQL_PORT") mysql_user = self.crawler.settings.get("MYSQL_USER") mysql_password = self.crawler.settings.get("MYSQL_PASSWORD") mysql_db_name = self.crawler.settings.get("MYSQL_DB_NAME") engine = create_engine('mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(mysql_user, mysql_password, mysql_host, mysql_port, mysql_db_name), pool_recycle=180, echo=False) session_maker = sessionmaker(bind=engine) self.db_session = session_maker() for url in self.start_urls: yield scrapy.Request(url) def parse(self, response): if self.db_session is None: self.logger.error("db_session is None") return None repeated_count = 0 if response is None: self.logger.warn("响应为空,不做处理!") else: album_nodes = response.css('.pic .item') category = response.css('.sitepath a')[1].css('a::text').extract_first().strip() # 判断最后一页的最后主题是否被持久化 is_persisted_last_item = self.redis_cmd.get(self.album_last_item_redis_unique_key) is_last_item_finished = False if is_persisted_last_item is not None and int(is_persisted_last_item): is_last_item_finished = True self.logger.info("已持久化最后一页的最后主题:%s" % self.album_last_item_redis_unique_key) # 如果是最后一页则设置Redis存储key:“最后一页页码:最后一条主题url”,value:is_persisted(取值为0或1,默认为0) album_last_page_url = response.meta.get("album_last_page_url") if album_last_page_url is not None: album_last_page_url_last_item_redis_suffix = album_nodes[-1].css('.p a::attr(href)').extract_first() self.album_last_item_redis_unique_key = self.ALBUM_URL_REDIS_KEY_PREFIX + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url, "") + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url_last_item_redis_suffix, "") self.redis_cmd.setnx(self.album_last_item_redis_unique_key, 0) for album_node in album_nodes: album_url = album_node.css('.p a::attr(href)').extract_first().strip() # 判断当前主题url是否已持久化 is_persisted = self.redis_cmd.get(album_url) if is_persisted is not None and int(is_persisted): self.logger.info("Redis中该url album_url:%s已持久化" % album_url) continue album_url_object_id = self.get_md5(album_url) # 只有name不存在时,当前set操作才执行 self.redis_cmd.setnx(album_url, 0) count = 0 try: count = self.db_session.query(func.count()).filter( Album.album_url_object_id == album_url_object_id).first() if count: count = count[0] except Exception as e: self.logger.error("查询数据库异常,原因:{}".format(e)) finally: self.db_session.rollback() if count: self.logger.info("数据库已有该数据album_url_object_id:%s" % album_url_object_id) repeated_count += 1 # 只有name存在时,当前set操作才执行 self.redis_cmd.set(album_url, 1, xx=True) continue else: album_item = self.parse_album_item(album_node, album_url, album_url_object_id, category) yield response.follow(url=album_url, meta={"AlbumItem": album_item}, callback=self.parse_detail) # 提取下一页并交给scrapy下载 selector_list = response.css('.page li a::attr(href)') # 如果最后一页的最后一个主题url未被持久化则继续爬取 if not is_last_item_finished: if selector_list: last_page_url = None current_url_page = response.xpath('//li[@class="thisclass"]//text()').extract_first() # 如果当前页是第一页则获取最后一页url if current_url_page and int(current_url_page) == 1: last_page_url = selector_list[-1].extract() next_url = selector_list[-2].extract() if next_url == last_page_url: album_last_page_url = response.urljoin(last_page_url) self.logger.info("Last page:%s" % album_last_page_url) else: self.logger.info("Next page:%s" % response.urljoin(next_url)) yield response.follow(url=next_url, meta={"album_last_page_url": album_last_page_url}, callback=self.parse) else: self.logger.info("selector_list is None") self.logger.info("重复次数:%s" % repeated_count) else: self.logger.info("Stop crawler. None Next page!") def parse_album_item(self, album_node, album_url, album_url_object_id, category): album_title = album_node.css('.p a img::attr(alt)').extract_first().strip() cover_url = album_node.css('.p a img::attr(src)').extract_first().strip() regex = "\d+\.\d+.\d+\s+No\.\d+|\d+\-\d+-\d+\s+No\.\d+" number_group = re.findall(regex, album_title) if len(number_group) > 0: number = number_group[0] else: number = "No.unknown" create_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') album_item = AlbumItem() album_item['category'] = category album_item['album_url'] = album_url album_item['album_url_object_id'] = album_url_object_id album_item['album_title'] = album_title album_item['cover_url'] = cover_url album_item['number'] = number album_item['create_date'] = create_date return album_item def parse_detail(self, response): self.album_item = response.meta.get("AlbumItem") self.album_image_relation_item['album_item'] = self.album_item self.parse_album_image_item(response) # 详情页分页链接,循环生成所有子页面的请求 relative_next_page_list = response.css('.page li a::attr(href)').extract() # 使用gevent协程池提升网络IO处理效率 next_page_threads = [ self.gevent_pool.spawn(self.get_album_image_item_list, response.urljoin(relative_next_page)) for relative_next_page in relative_next_page_list[2:-1] ] gevent.joinall(next_page_threads) self.album_image_relation_item['album_image_item_list'] = self.album_image_item_list # 重新初始化 self.album_image_item_list = [] yield self.album_image_relation_item def get_album_image_item_list(self, abs_next_page): """ 使用下页绝对路径同步请求 :param abs_next_page: :return: """ resp = requests.get(abs_next_page) if resp.status_code == 200: encoding = requests.utils.get_encodings_from_content(resp.text) resp.encoding = encoding[0] self.parse_album_image_item(etree.HTML(resp.text)) else: self.logger.warn("下载此页{}失败,返回的状态码为{}".format(abs_next_page, resp.status_code)) def parse_album_image_item(self, response): """ 解析item并返回给pipelines :param response: 如果response类型是继承自scrapy的TextResponse类则使用scrapy的Selector来解析,否则使用lxml来解析 :return: """ if isinstance(response, HtmlResponse): item_title = response.xpath('//div[@class="content"]/h1/text()').extract_first().strip() publish_date = response.xpath('//div[@class="tit"]/span/text()').extract_first().split(":")[1] image_link_list = response.css('.contents a img::attr(src)').extract() else: item_title = response.xpath('//div[@class="content"]/h1/text()')[0].strip() publish_date = response.xpath('//div[@class="tit"]/span/text()')[0].split(":")[1] image_link_list = response.xpath('//div[@class="contents"]/a/img') image_link_list = [image_link.attrib['src'] for image_link in image_link_list] regex = "\s?\w+[^\w]?" regex_group = re.findall(regex, item_title) stage_name = "unknown" if len(regex_group) > 0: str = regex_group[-1] if "[" in str: stage_name = str.split("[")[0].strip() elif "(" in str: stage_name = str.split("(")[0].strip() elif re.match('[^\d*]', str): stage_name = re.match('[^\d*]', str).group() # 详情页多个图片链接 for image_url in image_link_list: album_image_item = AlbumImageItem() album_image_item['item_url'] = image_url album_image_item['item_url_object_id'] = self.get_md5(image_url) item_url_list_json = "{}" album_image_item['item_url_list_json'] = item_url_list_json album_image_item['item_title'] = item_title album_image_item['stage_name'] = stage_name album_image_item['publish_date'] = publish_date self.album_image_item_list.append(album_image_item) return self.album_image_item_list @staticmethod def get_md5(param): if isinstance(param, str): param = param.encode() m = hashlib.md5() m.update(param) return m.hexdigest() @staticmethod def sub_url_scheme(website, replace_str): scheme_regex = "^(http://|https://)" return re.sub(scheme_regex, replace_str, website)
import re from datetime import datetime
random_line_split
beautyleg7_spider.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import hashlib import re from datetime import datetime import gevent import requests import scrapy from gevent.pool import Pool from lxml import etree from scrapy.http import HtmlResponse from sqlalchemy import create_engine, func from sqlalchemy.orm import sessionmaker from ..items import Album, AlbumImageRelationItem, AlbumItem, AlbumImageItem from ..utils.const import const from ..utils.redis_util import get_redis_conn_from_pool class Beautyleg7Spider(scrapy.Spider): name = 'Beautyleg7Spider' category_list = ['siwameitui', 'xingganmeinv', 'weimeixiezhen', 'ribenmeinv'] start_urls = [('http://www.beautyleg7.com/' + category) for category in category_list] const.REPEATED_THRESHOLD = 10 def __init__(self, name=None, **kwargs): super().__init__(name=None, **kwargs) self.db_session = None self.gevent_pool = Pool(32) self.redis_cmd = get_redis_conn_from_pool() self.ALBUM_URL_REDIS_KEY_PREFIX = "album_url" self.REDIS_LIMITER = ":" self.album_last_item_redis_unique_key = "" self.album_item = None self.album_image_item_list = [] self.album_image_relation_item = AlbumImageRelationItem() def start_requests(self): mysql_host = self.crawler.settings.get("MYSQL_HOST") mysql_port = self.crawler.settings.get("MYSQL_PORT") mysql_user = self.crawler.settings.get("MYSQL_USER") mysql_password = self.crawler.settings.get("MYSQL_PASSWORD") mysql_db_name = self.crawler.settings.get("MYSQL_DB_NAME") engine = create_engine('mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(mysql_user, mysql_password, mysql_host, mysql_port, mysql_db_name), pool_recycle=180, echo=False) session_maker = sessionmaker(bind=engine) self.db_session = session_maker() for url in self.start_urls: yield scrapy.Request(url) def parse(self, response): if self.db_session is None: self.logger.error("db_session is None") return None repeated_count = 0 if response is None: self.logger.warn("响应为空,不做处理!") else: album_nodes = response.css('.pic .item') category = response.css('.sitepath a')[1].css('a::text').extract_first().strip() # 判断最后一页的最后主题是否被持久化 is_persisted_last_item = self.redis_cmd.get(self.album_last_item_redis_unique_key) is_last_item_finished = False if is_persisted_last_item is not None and int(is_persisted_last_item): is_last_item_finished = True self.logger.info("已持久化最后一页的最后主题:%s" % self.album_last_item_redis_unique_key) # 如果是最后一页则设置Redis存储key:“最后一页页码:最后一条主题url”,value:is_persisted(取值为0或1,默认为0) album_last_page_url = response.meta.get("album_last_page_url") if album_last_page_url is not None: album_last_page_url_last_item_redis_suffix = album_nodes[-1].css('.p a::attr(href)').extract_first() self.album_last_item_redis_unique_key = self.ALBUM_URL_REDIS_KEY_PREFIX + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url, "") + self.REDIS_LIMITER + \ self.sub_url_scheme(album_last_page_url_last_item_redis_suffix, "") self.redis_cmd.setnx(self.album_last_item_redis_unique_key, 0) for album_node in album_nodes: album_url = album_node.css('.p a::attr(href)').extract_first().strip() # 判断当前主题url是否已持久化 is_persisted = self.redis_cmd.get(album_url) if is_persisted is not None and int(is_persisted): self.logger.info("Redis中该url album_url:%s已持久化" % album_url) continue album_url_object_id = self.get_md5(album_url) # 只有name不存在时,当前set操作才执行 self.redis_cmd.setnx(album_url, 0) count = 0 try: count = self.db_session.query(func.count()).filter( Album.album_url_object_id == album_url_object_id).first() if count: count = count[0] except Exception as e: self.logger.error("查询数据库异常,原因:{}".format(e)) finally: self.db_session.rollback() if count: self.logger.info("数据库已有该数据album_url_object_id:%s" % album_url_object_id) repeated_count += 1 # 只有name存在时,当前set操作才执行 self.redis_cmd.set(album_url, 1, xx=True) continue else: album_item = self.parse_album_item(album_node, album_url, album_url_object_id, category) yield response.follow(url=album_url, meta={"AlbumItem": album_item}, callback=self.parse_detail) # 提取下一页并交给scrapy下载 selector_list = response.css('.page li a::attr(href)') # 如果最后一页的最后一个主题url未被持久化则继续爬取 if not is_last_item_finished: if selector_list: last_page_url = None current_url_page = response.xpath('//li[@class="thisclass"]//text()').extract_first() # 如果当前页是第一页则获取最后一页url if current_url_page and int(current_url_page) == 1: last_page_url = selector_list[-1].extract() next_url = selector_list[-2].extract() if next_url == last_page_url: album_last_page_url = response.urljoin(last_page_url) self.logger.info("Last page:%s" % album_last_page_url) else: self.logger.info("Next page:%s" % response.urljoin(next_url)) yield response.follow(url=next_url, meta={"album_last_page_url": album_last_page_url}, callback=self.parse) else: self.logger.info("selector_list is None") self.logger.info("重复次数:%s" % repeated_count) else: self.logger.info("Stop crawler. None Next page!") def parse_album_item(self, album_node, album_url, album_url_object_id, category): album_title = album_node.css('.p a img::attr(alt)').extract_first().strip() cover_url = album_node.css('.p a img::attr(src)').extract_first().strip() regex = "\d+\.\d+.\d+\s+No\.\d+|\d+\-\d+-\d+\s+No\.\d+" number_group = re.findall(regex, album_title) if len(number_group) > 0: number = number_group[0] else: number = "No.unknown" create_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') album_item = AlbumItem() album_item['category'] = category album_item['album_url'] = album_url album_item['album_url_object_id'] = album_url_object_id album_item['album_title'] = album_title album_item['cover_url'] = cover_url album_item['number'] = number album_item['create_date'] = create_date return album_item def parse_detail(self, response): self.album_item = response.meta.get("AlbumItem") self.album_image_relation_item['album_item'] = self.album_item self.parse_album_image_item(response) # 详情页分页链接,循环生成所有子页面的请求 relative_next_page_list = response.css('.page li a::attr(href)').extract() # 使用gevent协程池提升网络IO处理效率 next_page_threads = [ self.gevent_pool.spawn(self.get_album_image_item_list, response.urljoin(relative_next_page)) for relative_next_page in relative_next_page_list[2:-1] ] gevent.joinall(next_page_threads) self.album_image_relation_item['album_image_item_list'] = self.album_image_item_list # 重新初始化 self.album_image_item_list = [] yield self.album_image_relation_item def get_album_image_item_list(self, abs_next_page): """ 使用下页绝对路径同步请求 :param abs_next_page: :return: """ resp = requests.get(abs_next_page) if resp.status_code == 200: encoding = requests.utils.get_encodings_from_content(resp.text) resp.encoding = encoding[0] self.parse_album_image_item(etree.HTML(resp.text)) else: self.logger.warn("下载此页{}失败,返回的状态码为{}".format(abs_next_page, resp.status_code)) def parse_album_image_item(self, response): """ 解析item并返回给pipelines :param response: 如果response类型是继承自scrapy的TextResponse类则使用scrapy的Selector来解析,否则使用lxml来解析 :return: """ if isinstance(response, HtmlResponse): item_title = response.xpath('//div[@class="content"]/h1/text()').extract_first().strip() publish_date = response.xpath('//div[@class="tit"]/span/text()').extract_first().split(":")[1] image_link_list = response.css('.contents a img::attr(src
else: item_title = response.xpath('//div[@class="content"]/h1/text()')[0].strip() publish_date = response.xpath('//div[@class="tit"]/span/text()')[0].split(":")[1] image_link_list = response.xpath('//div[@class="contents"]/a/img') image_link_list = [image_link.attrib['src'] for image_link in image_link_list] regex = "\s?\w+[^\w]?" regex_group = re.findall(regex, item_title) stage_name = "unknown" if len(regex_group) > 0: str = regex_group[-1] if "[" in str: stage_name = str.split("[")[0].strip() elif "(" in str: stage_name = str.split("(")[0].strip() elif re.match('[^\d*]', str): stage_name = re.match('[^\d*]', str).group() # 详情页多个图片链接 for image_url in image_link_list: album_image_item = AlbumImageItem() album_image_item['item_url'] = image_url album_image_item['item_url_object_id'] = self.get_md5(image_url) item_url_list_json = "{}" album_image_item['item_url_list_json'] = item_url_list_json album_image_item['item_title'] = item_title album_image_item['stage_name'] = stage_name album_image_item['publish_date'] = publish_date self.album_image_item_list.append(album_image_item) return self.album_image_item_list @staticmethod def get_md5(param): if isinstance(param, str): param = param.encode() m = hashlib.md5() m.update(param) return m.hexdigest() @staticmethod def sub_url_scheme(website, replace_str): scheme_regex = "^(http://|https://)" return re.sub(scheme_regex, replace_str, website)
)').extract()
identifier_name
output.rs
use std::io; use std::string::ToString; use std::thread::sleep; use std::time::Duration; use std::{collections::BTreeMap, path::PathBuf}; use anyhow::Result; use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY; use comfy_table::*; use snap::read::FrameDecoder; use pueue::log::{get_log_file_handles, get_log_paths}; use pueue::network::message::{GroupResponseMessage, TaskLogMessage}; use pueue::settings::Settings; use pueue::state::State; use pueue::task::{Task, TaskResult, TaskStatus}; use crate::cli::SubCommand; use crate::output_helper::*; pub fn print_success(message: &str) { println!("{}", message); } pub fn print_error(message: &str) { let styled = style_text(message, Some(Color::Red), None); println!("{}", styled); } pub fn print_groups(message: GroupResponseMessage) { let mut text = String::new(); let mut group_iter = message.groups.iter().peekable(); while let Some((name, status)) = group_iter.next() { let parallel = *message.settings.get(name).unwrap(); let styled = get_group_headline(name, &status, parallel); text.push_str(&styled); if group_iter.peek().is_some() { text.push('\n'); } } println!("{}", text); } /// Print the current state of the daemon in a nicely formatted table. pub fn print_state(state: State, cli_command: &SubCommand, settings: &Settings) { let (json, group_only) = match cli_command { SubCommand::Status { json, group } => (*json, group.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_state. This shouldn't happen", cli_command ), }; // If the json flag is specified, print the state as json and exit. if json { println!("{}", serde_json::to_string(&state).unwrap()); return; } // Early exit and hint if there are no tasks in the queue if state.tasks.is_empty() { println!("Task list is empty. Add tasks with `pueue add -- [cmd]`"); return; } // Sort all tasks by their respective group; let sorted_tasks = sort_tasks_by_group(&state.tasks); // Always print the default queue at the very top. if group_only.is_none() { let tasks = sorted_tasks.get("default").unwrap(); let headline = get_group_headline( &"default", &state.groups.get("default").unwrap(), *state.settings.daemon.groups.get("default").unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline if there are further groups to be printed if sorted_tasks.len() > 1 { println!(); } } let mut sorted_iter = sorted_tasks.iter().peekable(); // Print new table for each group while let Some((group, tasks)) = sorted_iter.next() { // We always want to print the default group at the very top. // That's why we print it outside of this loop and skip it in here. if group.eq("default") { continue; } // Skip unwanted groups, if a single group is requested if let Some(group_only) = &group_only { if group_only != group { continue; } } let headline = get_group_headline( &group, &state.groups.get(group).unwrap(), *state.settings.daemon.groups.get(group).unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline between groups if sorted_iter.peek().is_some() { println!(); } } } /// Print some tasks into a nicely formatted table fn print_table(tasks: &BTreeMap<usize, Task>, settings: &Settings) { let (has_delayed_tasks, has_dependencies, has_labels) = has_special_columns(tasks); // Create table header row let mut headers = vec![Cell::new("Index"), Cell::new("Status")]; if has_delayed_tasks { headers.push(Cell::new("Enqueue At")); } if has_dependencies { headers.push(Cell::new("Deps")); } headers.push(Cell::new("Exitcode")); if has_labels { headers.push(Cell::new("Label")); } headers.append(&mut vec![ Cell::new("Command"), Cell::new("Path"), Cell::new("Start"), Cell::new("End"), ]); // Initialize comfy table. let mut table = Table::new(); table .set_content_arrangement(ContentArrangement::Dynamic) .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY) .set_header(headers); // Add rows one by one. for (id, task) in tasks { let mut row = Row::new(); if let Some(height) = settings.client.max_status_lines { row.max_height(height); } row.add_cell(Cell::new(&id.to_string())); // Determine the human readable task status representation and the respective color. let status_string = task.status.to_string(); let (status_text, color) = match task.status { TaskStatus::Running => (status_string, Color::Green), TaskStatus::Paused | TaskStatus::Locked => (status_string, Color::White), TaskStatus::Done => match &task.result { Some(TaskResult::Success) => (TaskResult::Success.to_string(), Color::Green), Some(TaskResult::DependencyFailed) => ("Dependency failed".to_string(), Color::Red), Some(TaskResult::FailedToSpawn(_)) => ("Failed to spawn".to_string(), Color::Red), Some(result) => (result.to_string(), Color::Red), None => panic!("Got a 'Done' task without a task result. Please report this bug."), }, _ => (status_string, Color::Yellow), }; row.add_cell(Cell::new(status_text).fg(color)); if has_delayed_tasks { if let Some(enqueue_at) = task.enqueue_at { row.add_cell(Cell::new(enqueue_at.format("%Y-%m-%d\n%H:%M:%S"))); } else { row.add_cell(Cell::new("")); } } if has_dependencies { let text = task .dependencies .iter() .map(|id| id.to_string()) .collect::<Vec<String>>() .join(", "); row.add_cell(Cell::new(text)); } // Match the color of the exit code. // If the exit_code is none, it has been killed by the task handler. let exit_code_cell = match task.result { Some(TaskResult::Success) => Cell::new("0").fg(Color::Green), Some(TaskResult::Failed(code)) => Cell::new(&code.to_string()).fg(Color::Red), _ => Cell::new(""), }; row.add_cell(exit_code_cell); if has_labels { if let Some(label) = &task.label { row.add_cell(label.to_cell()); } else { row.add_cell(Cell::new("")); } } // Add command and path. if settings.client.show_expanded_aliases { row.add_cell(Cell::new(&task.command)); } else { row.add_cell(Cell::new(&task.original_command)); } row.add_cell(Cell::new(&task.path)); // Add start time, if already set. if let Some(start) = task.start { let formatted = start.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } // Add finish time, if already set. if let Some(end) = task.end { let formatted = end.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } table.add_row(row); } // Print the table. println!("{}", table); } /// Print the log ouput of finished tasks. /// Either print the logs of every task /// or only print the logs of the specified tasks. pub fn print_logs( mut task_logs: BTreeMap<usize, TaskLogMessage>, cli_command: &SubCommand, settings: &Settings, ) { let (json, task_ids) = match cli_command { SubCommand::Log { json, task_ids } => (*json, task_ids.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_log. This shouldn't happen", cli_command ), }; if json { println!("{}", serde_json::to_string(&task_logs).unwrap()); return; } if task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks"); return; } if !task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks for your specified ids"); return; } let mut task_iter = task_logs.iter_mut().peekable(); while let Some((_, mut task_log)) = task_iter.next() { print_log(&mut task_log, settings); // Add a newline if there is another task that's going to be printed. if let Some((_, task_log)) = task_iter.peek() { if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused] .contains(&task_log.task.status) { println!(); } } } } /// Print the log of a single task. pub fn print_log(task_log: &mut TaskLogMessage, settings: &Settings) { let task = &task_log.task; // We only show logs of finished or running tasks. if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused].contains(&task.status) { return; } // Print task id and exit code. let task_text = style_text(&format!("Task {}", task.id), None, Some(Attribute::Bold)); let (exit_status, color) = match &task.result { Some(TaskResult::Success) => ("completed successfully".into(), Color::Green), Some(TaskResult::Failed(exit_code)) => { (format!("failed with exit code {}", exit_code), Color::Red) } Some(TaskResult::FailedToSpawn(err)) => (format!("failed to spawn: {}", err), Color::Red), Some(TaskResult::Killed) => ("killed by system or user".into(), Color::Red), Some(TaskResult::DependencyFailed) => ("dependency failed".into(), Color::Red), None => ("running".into(), Color::White), }; let status_text = style_text(&exit_status, Some(color), None); println!("{} {}", task_text, status_text); // Print command and path. println!("Command: {}", task.command); println!("Path: {}", task.path); if let Some(start) = task.start { println!("Start: {}", start.to_rfc2822()); } if let Some(end) = task.end { println!("End: {}", end.to_rfc2822()); } if settings.client.read_local_logs { print_local_log_output(task_log.task.id, settings); } else if task_log.stdout.is_some() && task_log.stderr.is_some() { print_task_output_from_daemon(task_log); } else { println!("Logs requested from pueue daemon, but none received. Please report this bug."); } } /// The daemon didn't send any log output, thereby we didn't request any. /// If that's the case, read the log files from the local pueue directory pub fn
(task_id: usize, settings: &Settings) { let (mut stdout_log, mut stderr_log) = match get_log_file_handles(task_id, &settings.shared.pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); if let Ok(metadata) = stdout_log.metadata() { if metadata.len() != 0 { println!( "\n{}", style_text("stdout:", Some(Color::Green), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stdout_log, &mut stdout) { println!("Failed reading local stdout log file: {}", err); }; } } if let Ok(metadata) = stderr_log.metadata() { if metadata.len() != 0 { // Add a spacer line between stdout and stderr println!( "\n{}", style_text("stderr:", Some(Color::Red), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stderr_log, &mut stdout) { println!("Failed reading local stderr log file: {}", err); }; } } } /// Prints log output received from the daemon. /// We can safely call .unwrap() on stdout and stderr in here, since this /// branch is always called after ensuring that both are `Some`. pub fn print_task_output_from_daemon(task_log: &TaskLogMessage) { // Save whether stdout was printed, so we can add a newline between outputs. if !task_log.stdout.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, true) { println!("Error while parsing stdout: {}", err); } } if !task_log.stderr.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, false) { println!("Error while parsing stderr: {}", err); }; } } /// Print log output of a finished process. pub fn print_remote_task_output(task_log: &TaskLogMessage, stdout: bool) -> Result<()> { let (pre_text, color, bytes) = if stdout { ("stdout: ", Color::Green, task_log.stdout.as_ref().unwrap()) } else { ("stderr: ", Color::Red, task_log.stderr.as_ref().unwrap()) }; println!( "\n{}", style_text(pre_text, Some(color), Some(Attribute::Bold)) ); let mut decompressor = FrameDecoder::new(bytes.as_slice()); let stdout = io::stdout(); let mut write = stdout.lock(); io::copy(&mut decompressor, &mut write)?; Ok(()) } /// Follow the log ouput of running task. /// /// If no task is specified, this will check for the following cases: /// /// - No running task: Print an error that there are no running tasks /// - Single running task: Follow the output of that task /// - Multiple running tasks: Print out the list of possible tasks to follow. pub fn follow_task_logs(pueue_directory: &PathBuf, task_id: usize, stderr: bool) { let (stdout_handle, stderr_handle) = match get_log_file_handles(task_id, &pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; let mut handle = if stderr { stderr_handle } else { stdout_handle }; let (out_path, err_path) = get_log_paths(task_id, &pueue_directory); let handle_path = if stderr { err_path } else { out_path }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); loop { // Check whether the file still exists. Exit if it doesn't. if !handle_path.exists() { println!("File has gone away. Did somebody remove the task?"); return; } // Read the next chunk of text from the last position. if let Err(err) = io::copy(&mut handle, &mut stdout) { println!("Error while reading file: {}", err); return; }; let timeout = Duration::from_millis(100); sleep(timeout); } }
print_local_log_output
identifier_name
output.rs
use std::io; use std::string::ToString; use std::thread::sleep; use std::time::Duration; use std::{collections::BTreeMap, path::PathBuf}; use anyhow::Result; use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY; use comfy_table::*; use snap::read::FrameDecoder; use pueue::log::{get_log_file_handles, get_log_paths}; use pueue::network::message::{GroupResponseMessage, TaskLogMessage}; use pueue::settings::Settings; use pueue::state::State; use pueue::task::{Task, TaskResult, TaskStatus}; use crate::cli::SubCommand; use crate::output_helper::*; pub fn print_success(message: &str)
pub fn print_error(message: &str) { let styled = style_text(message, Some(Color::Red), None); println!("{}", styled); } pub fn print_groups(message: GroupResponseMessage) { let mut text = String::new(); let mut group_iter = message.groups.iter().peekable(); while let Some((name, status)) = group_iter.next() { let parallel = *message.settings.get(name).unwrap(); let styled = get_group_headline(name, &status, parallel); text.push_str(&styled); if group_iter.peek().is_some() { text.push('\n'); } } println!("{}", text); } /// Print the current state of the daemon in a nicely formatted table. pub fn print_state(state: State, cli_command: &SubCommand, settings: &Settings) { let (json, group_only) = match cli_command { SubCommand::Status { json, group } => (*json, group.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_state. This shouldn't happen", cli_command ), }; // If the json flag is specified, print the state as json and exit. if json { println!("{}", serde_json::to_string(&state).unwrap()); return; } // Early exit and hint if there are no tasks in the queue if state.tasks.is_empty() { println!("Task list is empty. Add tasks with `pueue add -- [cmd]`"); return; } // Sort all tasks by their respective group; let sorted_tasks = sort_tasks_by_group(&state.tasks); // Always print the default queue at the very top. if group_only.is_none() { let tasks = sorted_tasks.get("default").unwrap(); let headline = get_group_headline( &"default", &state.groups.get("default").unwrap(), *state.settings.daemon.groups.get("default").unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline if there are further groups to be printed if sorted_tasks.len() > 1 { println!(); } } let mut sorted_iter = sorted_tasks.iter().peekable(); // Print new table for each group while let Some((group, tasks)) = sorted_iter.next() { // We always want to print the default group at the very top. // That's why we print it outside of this loop and skip it in here. if group.eq("default") { continue; } // Skip unwanted groups, if a single group is requested if let Some(group_only) = &group_only { if group_only != group { continue; } } let headline = get_group_headline( &group, &state.groups.get(group).unwrap(), *state.settings.daemon.groups.get(group).unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline between groups if sorted_iter.peek().is_some() { println!(); } } } /// Print some tasks into a nicely formatted table fn print_table(tasks: &BTreeMap<usize, Task>, settings: &Settings) { let (has_delayed_tasks, has_dependencies, has_labels) = has_special_columns(tasks); // Create table header row let mut headers = vec![Cell::new("Index"), Cell::new("Status")]; if has_delayed_tasks { headers.push(Cell::new("Enqueue At")); } if has_dependencies { headers.push(Cell::new("Deps")); } headers.push(Cell::new("Exitcode")); if has_labels { headers.push(Cell::new("Label")); } headers.append(&mut vec![ Cell::new("Command"), Cell::new("Path"), Cell::new("Start"), Cell::new("End"), ]); // Initialize comfy table. let mut table = Table::new(); table .set_content_arrangement(ContentArrangement::Dynamic) .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY) .set_header(headers); // Add rows one by one. for (id, task) in tasks { let mut row = Row::new(); if let Some(height) = settings.client.max_status_lines { row.max_height(height); } row.add_cell(Cell::new(&id.to_string())); // Determine the human readable task status representation and the respective color. let status_string = task.status.to_string(); let (status_text, color) = match task.status { TaskStatus::Running => (status_string, Color::Green), TaskStatus::Paused | TaskStatus::Locked => (status_string, Color::White), TaskStatus::Done => match &task.result { Some(TaskResult::Success) => (TaskResult::Success.to_string(), Color::Green), Some(TaskResult::DependencyFailed) => ("Dependency failed".to_string(), Color::Red), Some(TaskResult::FailedToSpawn(_)) => ("Failed to spawn".to_string(), Color::Red), Some(result) => (result.to_string(), Color::Red), None => panic!("Got a 'Done' task without a task result. Please report this bug."), }, _ => (status_string, Color::Yellow), }; row.add_cell(Cell::new(status_text).fg(color)); if has_delayed_tasks { if let Some(enqueue_at) = task.enqueue_at { row.add_cell(Cell::new(enqueue_at.format("%Y-%m-%d\n%H:%M:%S"))); } else { row.add_cell(Cell::new("")); } } if has_dependencies { let text = task .dependencies .iter() .map(|id| id.to_string()) .collect::<Vec<String>>() .join(", "); row.add_cell(Cell::new(text)); } // Match the color of the exit code. // If the exit_code is none, it has been killed by the task handler. let exit_code_cell = match task.result { Some(TaskResult::Success) => Cell::new("0").fg(Color::Green), Some(TaskResult::Failed(code)) => Cell::new(&code.to_string()).fg(Color::Red), _ => Cell::new(""), }; row.add_cell(exit_code_cell); if has_labels { if let Some(label) = &task.label { row.add_cell(label.to_cell()); } else { row.add_cell(Cell::new("")); } } // Add command and path. if settings.client.show_expanded_aliases { row.add_cell(Cell::new(&task.command)); } else { row.add_cell(Cell::new(&task.original_command)); } row.add_cell(Cell::new(&task.path)); // Add start time, if already set. if let Some(start) = task.start { let formatted = start.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } // Add finish time, if already set. if let Some(end) = task.end { let formatted = end.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } table.add_row(row); } // Print the table. println!("{}", table); } /// Print the log ouput of finished tasks. /// Either print the logs of every task /// or only print the logs of the specified tasks. pub fn print_logs( mut task_logs: BTreeMap<usize, TaskLogMessage>, cli_command: &SubCommand, settings: &Settings, ) { let (json, task_ids) = match cli_command { SubCommand::Log { json, task_ids } => (*json, task_ids.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_log. This shouldn't happen", cli_command ), }; if json { println!("{}", serde_json::to_string(&task_logs).unwrap()); return; } if task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks"); return; } if !task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks for your specified ids"); return; } let mut task_iter = task_logs.iter_mut().peekable(); while let Some((_, mut task_log)) = task_iter.next() { print_log(&mut task_log, settings); // Add a newline if there is another task that's going to be printed. if let Some((_, task_log)) = task_iter.peek() { if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused] .contains(&task_log.task.status) { println!(); } } } } /// Print the log of a single task. pub fn print_log(task_log: &mut TaskLogMessage, settings: &Settings) { let task = &task_log.task; // We only show logs of finished or running tasks. if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused].contains(&task.status) { return; } // Print task id and exit code. let task_text = style_text(&format!("Task {}", task.id), None, Some(Attribute::Bold)); let (exit_status, color) = match &task.result { Some(TaskResult::Success) => ("completed successfully".into(), Color::Green), Some(TaskResult::Failed(exit_code)) => { (format!("failed with exit code {}", exit_code), Color::Red) } Some(TaskResult::FailedToSpawn(err)) => (format!("failed to spawn: {}", err), Color::Red), Some(TaskResult::Killed) => ("killed by system or user".into(), Color::Red), Some(TaskResult::DependencyFailed) => ("dependency failed".into(), Color::Red), None => ("running".into(), Color::White), }; let status_text = style_text(&exit_status, Some(color), None); println!("{} {}", task_text, status_text); // Print command and path. println!("Command: {}", task.command); println!("Path: {}", task.path); if let Some(start) = task.start { println!("Start: {}", start.to_rfc2822()); } if let Some(end) = task.end { println!("End: {}", end.to_rfc2822()); } if settings.client.read_local_logs { print_local_log_output(task_log.task.id, settings); } else if task_log.stdout.is_some() && task_log.stderr.is_some() { print_task_output_from_daemon(task_log); } else { println!("Logs requested from pueue daemon, but none received. Please report this bug."); } } /// The daemon didn't send any log output, thereby we didn't request any. /// If that's the case, read the log files from the local pueue directory pub fn print_local_log_output(task_id: usize, settings: &Settings) { let (mut stdout_log, mut stderr_log) = match get_log_file_handles(task_id, &settings.shared.pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); if let Ok(metadata) = stdout_log.metadata() { if metadata.len() != 0 { println!( "\n{}", style_text("stdout:", Some(Color::Green), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stdout_log, &mut stdout) { println!("Failed reading local stdout log file: {}", err); }; } } if let Ok(metadata) = stderr_log.metadata() { if metadata.len() != 0 { // Add a spacer line between stdout and stderr println!( "\n{}", style_text("stderr:", Some(Color::Red), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stderr_log, &mut stdout) { println!("Failed reading local stderr log file: {}", err); }; } } } /// Prints log output received from the daemon. /// We can safely call .unwrap() on stdout and stderr in here, since this /// branch is always called after ensuring that both are `Some`. pub fn print_task_output_from_daemon(task_log: &TaskLogMessage) { // Save whether stdout was printed, so we can add a newline between outputs. if !task_log.stdout.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, true) { println!("Error while parsing stdout: {}", err); } } if !task_log.stderr.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, false) { println!("Error while parsing stderr: {}", err); }; } } /// Print log output of a finished process. pub fn print_remote_task_output(task_log: &TaskLogMessage, stdout: bool) -> Result<()> { let (pre_text, color, bytes) = if stdout { ("stdout: ", Color::Green, task_log.stdout.as_ref().unwrap()) } else { ("stderr: ", Color::Red, task_log.stderr.as_ref().unwrap()) }; println!( "\n{}", style_text(pre_text, Some(color), Some(Attribute::Bold)) ); let mut decompressor = FrameDecoder::new(bytes.as_slice()); let stdout = io::stdout(); let mut write = stdout.lock(); io::copy(&mut decompressor, &mut write)?; Ok(()) } /// Follow the log ouput of running task. /// /// If no task is specified, this will check for the following cases: /// /// - No running task: Print an error that there are no running tasks /// - Single running task: Follow the output of that task /// - Multiple running tasks: Print out the list of possible tasks to follow. pub fn follow_task_logs(pueue_directory: &PathBuf, task_id: usize, stderr: bool) { let (stdout_handle, stderr_handle) = match get_log_file_handles(task_id, &pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; let mut handle = if stderr { stderr_handle } else { stdout_handle }; let (out_path, err_path) = get_log_paths(task_id, &pueue_directory); let handle_path = if stderr { err_path } else { out_path }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); loop { // Check whether the file still exists. Exit if it doesn't. if !handle_path.exists() { println!("File has gone away. Did somebody remove the task?"); return; } // Read the next chunk of text from the last position. if let Err(err) = io::copy(&mut handle, &mut stdout) { println!("Error while reading file: {}", err); return; }; let timeout = Duration::from_millis(100); sleep(timeout); } }
{ println!("{}", message); }
identifier_body
output.rs
use std::io; use std::string::ToString; use std::thread::sleep; use std::time::Duration; use std::{collections::BTreeMap, path::PathBuf}; use anyhow::Result; use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY; use comfy_table::*; use snap::read::FrameDecoder; use pueue::log::{get_log_file_handles, get_log_paths}; use pueue::network::message::{GroupResponseMessage, TaskLogMessage}; use pueue::settings::Settings; use pueue::state::State; use pueue::task::{Task, TaskResult, TaskStatus}; use crate::cli::SubCommand; use crate::output_helper::*; pub fn print_success(message: &str) { println!("{}", message); } pub fn print_error(message: &str) { let styled = style_text(message, Some(Color::Red), None); println!("{}", styled); } pub fn print_groups(message: GroupResponseMessage) { let mut text = String::new(); let mut group_iter = message.groups.iter().peekable(); while let Some((name, status)) = group_iter.next() { let parallel = *message.settings.get(name).unwrap(); let styled = get_group_headline(name, &status, parallel); text.push_str(&styled); if group_iter.peek().is_some() { text.push('\n'); } } println!("{}", text); } /// Print the current state of the daemon in a nicely formatted table. pub fn print_state(state: State, cli_command: &SubCommand, settings: &Settings) { let (json, group_only) = match cli_command { SubCommand::Status { json, group } => (*json, group.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_state. This shouldn't happen", cli_command ), }; // If the json flag is specified, print the state as json and exit. if json { println!("{}", serde_json::to_string(&state).unwrap()); return; } // Early exit and hint if there are no tasks in the queue if state.tasks.is_empty() { println!("Task list is empty. Add tasks with `pueue add -- [cmd]`"); return; } // Sort all tasks by their respective group; let sorted_tasks = sort_tasks_by_group(&state.tasks); // Always print the default queue at the very top. if group_only.is_none() { let tasks = sorted_tasks.get("default").unwrap(); let headline = get_group_headline( &"default", &state.groups.get("default").unwrap(), *state.settings.daemon.groups.get("default").unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline if there are further groups to be printed if sorted_tasks.len() > 1 { println!(); } } let mut sorted_iter = sorted_tasks.iter().peekable(); // Print new table for each group
if group.eq("default") { continue; } // Skip unwanted groups, if a single group is requested if let Some(group_only) = &group_only { if group_only != group { continue; } } let headline = get_group_headline( &group, &state.groups.get(group).unwrap(), *state.settings.daemon.groups.get(group).unwrap(), ); println!("{}", headline); print_table(&tasks, settings); // Add a newline between groups if sorted_iter.peek().is_some() { println!(); } } } /// Print some tasks into a nicely formatted table fn print_table(tasks: &BTreeMap<usize, Task>, settings: &Settings) { let (has_delayed_tasks, has_dependencies, has_labels) = has_special_columns(tasks); // Create table header row let mut headers = vec![Cell::new("Index"), Cell::new("Status")]; if has_delayed_tasks { headers.push(Cell::new("Enqueue At")); } if has_dependencies { headers.push(Cell::new("Deps")); } headers.push(Cell::new("Exitcode")); if has_labels { headers.push(Cell::new("Label")); } headers.append(&mut vec![ Cell::new("Command"), Cell::new("Path"), Cell::new("Start"), Cell::new("End"), ]); // Initialize comfy table. let mut table = Table::new(); table .set_content_arrangement(ContentArrangement::Dynamic) .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY) .set_header(headers); // Add rows one by one. for (id, task) in tasks { let mut row = Row::new(); if let Some(height) = settings.client.max_status_lines { row.max_height(height); } row.add_cell(Cell::new(&id.to_string())); // Determine the human readable task status representation and the respective color. let status_string = task.status.to_string(); let (status_text, color) = match task.status { TaskStatus::Running => (status_string, Color::Green), TaskStatus::Paused | TaskStatus::Locked => (status_string, Color::White), TaskStatus::Done => match &task.result { Some(TaskResult::Success) => (TaskResult::Success.to_string(), Color::Green), Some(TaskResult::DependencyFailed) => ("Dependency failed".to_string(), Color::Red), Some(TaskResult::FailedToSpawn(_)) => ("Failed to spawn".to_string(), Color::Red), Some(result) => (result.to_string(), Color::Red), None => panic!("Got a 'Done' task without a task result. Please report this bug."), }, _ => (status_string, Color::Yellow), }; row.add_cell(Cell::new(status_text).fg(color)); if has_delayed_tasks { if let Some(enqueue_at) = task.enqueue_at { row.add_cell(Cell::new(enqueue_at.format("%Y-%m-%d\n%H:%M:%S"))); } else { row.add_cell(Cell::new("")); } } if has_dependencies { let text = task .dependencies .iter() .map(|id| id.to_string()) .collect::<Vec<String>>() .join(", "); row.add_cell(Cell::new(text)); } // Match the color of the exit code. // If the exit_code is none, it has been killed by the task handler. let exit_code_cell = match task.result { Some(TaskResult::Success) => Cell::new("0").fg(Color::Green), Some(TaskResult::Failed(code)) => Cell::new(&code.to_string()).fg(Color::Red), _ => Cell::new(""), }; row.add_cell(exit_code_cell); if has_labels { if let Some(label) = &task.label { row.add_cell(label.to_cell()); } else { row.add_cell(Cell::new("")); } } // Add command and path. if settings.client.show_expanded_aliases { row.add_cell(Cell::new(&task.command)); } else { row.add_cell(Cell::new(&task.original_command)); } row.add_cell(Cell::new(&task.path)); // Add start time, if already set. if let Some(start) = task.start { let formatted = start.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } // Add finish time, if already set. if let Some(end) = task.end { let formatted = end.format("%H:%M").to_string(); row.add_cell(Cell::new(&formatted)); } else { row.add_cell(Cell::new("")); } table.add_row(row); } // Print the table. println!("{}", table); } /// Print the log ouput of finished tasks. /// Either print the logs of every task /// or only print the logs of the specified tasks. pub fn print_logs( mut task_logs: BTreeMap<usize, TaskLogMessage>, cli_command: &SubCommand, settings: &Settings, ) { let (json, task_ids) = match cli_command { SubCommand::Log { json, task_ids } => (*json, task_ids.clone()), _ => panic!( "Got wrong Subcommand {:?} in print_log. This shouldn't happen", cli_command ), }; if json { println!("{}", serde_json::to_string(&task_logs).unwrap()); return; } if task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks"); return; } if !task_ids.is_empty() && task_logs.is_empty() { println!("There are no finished tasks for your specified ids"); return; } let mut task_iter = task_logs.iter_mut().peekable(); while let Some((_, mut task_log)) = task_iter.next() { print_log(&mut task_log, settings); // Add a newline if there is another task that's going to be printed. if let Some((_, task_log)) = task_iter.peek() { if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused] .contains(&task_log.task.status) { println!(); } } } } /// Print the log of a single task. pub fn print_log(task_log: &mut TaskLogMessage, settings: &Settings) { let task = &task_log.task; // We only show logs of finished or running tasks. if !vec![TaskStatus::Done, TaskStatus::Running, TaskStatus::Paused].contains(&task.status) { return; } // Print task id and exit code. let task_text = style_text(&format!("Task {}", task.id), None, Some(Attribute::Bold)); let (exit_status, color) = match &task.result { Some(TaskResult::Success) => ("completed successfully".into(), Color::Green), Some(TaskResult::Failed(exit_code)) => { (format!("failed with exit code {}", exit_code), Color::Red) } Some(TaskResult::FailedToSpawn(err)) => (format!("failed to spawn: {}", err), Color::Red), Some(TaskResult::Killed) => ("killed by system or user".into(), Color::Red), Some(TaskResult::DependencyFailed) => ("dependency failed".into(), Color::Red), None => ("running".into(), Color::White), }; let status_text = style_text(&exit_status, Some(color), None); println!("{} {}", task_text, status_text); // Print command and path. println!("Command: {}", task.command); println!("Path: {}", task.path); if let Some(start) = task.start { println!("Start: {}", start.to_rfc2822()); } if let Some(end) = task.end { println!("End: {}", end.to_rfc2822()); } if settings.client.read_local_logs { print_local_log_output(task_log.task.id, settings); } else if task_log.stdout.is_some() && task_log.stderr.is_some() { print_task_output_from_daemon(task_log); } else { println!("Logs requested from pueue daemon, but none received. Please report this bug."); } } /// The daemon didn't send any log output, thereby we didn't request any. /// If that's the case, read the log files from the local pueue directory pub fn print_local_log_output(task_id: usize, settings: &Settings) { let (mut stdout_log, mut stderr_log) = match get_log_file_handles(task_id, &settings.shared.pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); if let Ok(metadata) = stdout_log.metadata() { if metadata.len() != 0 { println!( "\n{}", style_text("stdout:", Some(Color::Green), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stdout_log, &mut stdout) { println!("Failed reading local stdout log file: {}", err); }; } } if let Ok(metadata) = stderr_log.metadata() { if metadata.len() != 0 { // Add a spacer line between stdout and stderr println!( "\n{}", style_text("stderr:", Some(Color::Red), Some(Attribute::Bold)) ); if let Err(err) = io::copy(&mut stderr_log, &mut stdout) { println!("Failed reading local stderr log file: {}", err); }; } } } /// Prints log output received from the daemon. /// We can safely call .unwrap() on stdout and stderr in here, since this /// branch is always called after ensuring that both are `Some`. pub fn print_task_output_from_daemon(task_log: &TaskLogMessage) { // Save whether stdout was printed, so we can add a newline between outputs. if !task_log.stdout.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, true) { println!("Error while parsing stdout: {}", err); } } if !task_log.stderr.as_ref().unwrap().is_empty() { if let Err(err) = print_remote_task_output(&task_log, false) { println!("Error while parsing stderr: {}", err); }; } } /// Print log output of a finished process. pub fn print_remote_task_output(task_log: &TaskLogMessage, stdout: bool) -> Result<()> { let (pre_text, color, bytes) = if stdout { ("stdout: ", Color::Green, task_log.stdout.as_ref().unwrap()) } else { ("stderr: ", Color::Red, task_log.stderr.as_ref().unwrap()) }; println!( "\n{}", style_text(pre_text, Some(color), Some(Attribute::Bold)) ); let mut decompressor = FrameDecoder::new(bytes.as_slice()); let stdout = io::stdout(); let mut write = stdout.lock(); io::copy(&mut decompressor, &mut write)?; Ok(()) } /// Follow the log ouput of running task. /// /// If no task is specified, this will check for the following cases: /// /// - No running task: Print an error that there are no running tasks /// - Single running task: Follow the output of that task /// - Multiple running tasks: Print out the list of possible tasks to follow. pub fn follow_task_logs(pueue_directory: &PathBuf, task_id: usize, stderr: bool) { let (stdout_handle, stderr_handle) = match get_log_file_handles(task_id, &pueue_directory) { Ok((stdout, stderr)) => (stdout, stderr), Err(err) => { println!("Failed to get log file handles: {}", err); return; } }; let mut handle = if stderr { stderr_handle } else { stdout_handle }; let (out_path, err_path) = get_log_paths(task_id, &pueue_directory); let handle_path = if stderr { err_path } else { out_path }; // Stdout handler to directly write log file output to io::stdout // without having to load anything into memory. let mut stdout = io::stdout(); loop { // Check whether the file still exists. Exit if it doesn't. if !handle_path.exists() { println!("File has gone away. Did somebody remove the task?"); return; } // Read the next chunk of text from the last position. if let Err(err) = io::copy(&mut handle, &mut stdout) { println!("Error while reading file: {}", err); return; }; let timeout = Duration::from_millis(100); sleep(timeout); } }
while let Some((group, tasks)) = sorted_iter.next() { // We always want to print the default group at the very top. // That's why we print it outside of this loop and skip it in here.
random_line_split
player.rs
use std::f32; use nalgebra::{norm, zero, Point2, Rotation2, Vector2}; use specs::prelude::*; use specs::storage::BTreeStorage; use defs::{EntityId, GameInfo, PlayerId, PlayerInput, INVALID_ENTITY_ID}; use event::{self, Event}; use game::entity::hook; use game::ComponentType; use physics::collision::{self, CollisionGroups, Cuboid, GeometricQueryType, ShapeHandle}; use physics::interaction; use physics::{AngularVelocity, Drag, Dynamic, InvAngularMass, InvMass, Orientation, Position, Velocity}; use registry::Registry; use repl; pub fn register(reg: &mut Registry) { reg.component::<InputState>(); reg.component::<CurrentInput>(); reg.component::<Player>(); reg.component::<State>(); reg.event::<DashedEvent>(); repl::entity::register_class( reg, "player", &[ ComponentType::Position, ComponentType::Orientation, ComponentType::Player, // TODO: Only send to owner ComponentType::Velocity, ComponentType::AngularVelocity, ComponentType::PlayerInputState, ComponentType::PlayerState, ], build_player, ); interaction::set( reg, "player", "wall", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); interaction::set( reg, "player", "test", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); // FIXME: Due to a bug in physics sim, other player also gets moved interaction::set( reg, "player", "player", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); } pub const NUM_HOOKS: usize = 2; pub const WIDTH: f32 = 40.0; pub const HEIGHT: f32 = 40.0; pub const MOVE_ACCEL: f32 = 3000.0; pub const ROT_ACCEL: f32 = 200.0; pub const MASS: f32 = 50.0; pub const DRAG: f32 = 4.0; pub const SNAP_ANGLE: f32 = f32::consts::PI / 12.0; pub const MAX_ANGULAR_VEL: f32 = f32::consts::PI * 5.0; pub const TAP_SECS: f32 = 0.25; pub const DASH_SECS: f32 = 0.3; pub const DASH_COOLDOWN_SECS: f32 = 2.0; pub const DASH_ACCEL: f32 = 10000.0; #[derive(Debug, Clone, BitStore)] pub struct DashedEvent { /// Different hook colors for drawing. pub hook_index: u32, } impl Event for DashedEvent { fn class(&self) -> event::Class { event::Class::Order } } /// Component that is attached whenever player input should be executed for an entity. #[derive(Component, Clone, Debug)] #[storage(BTreeStorage)] pub struct CurrentInput(pub PlayerInput, [bool; NUM_TAP_KEYS]); impl CurrentInput { fn new(input: PlayerInput) -> CurrentInput { CurrentInput(input, [false; NUM_TAP_KEYS]) } } // Tappable keys const MOVE_FORWARD_KEY: usize = 0; const MOVE_BACKWARD_KEY: usize = 1; const MOVE_LEFT_KEY: usize = 2; const MOVE_RIGHT_KEY: usize = 3; const NUM_TAP_KEYS: usize = 4; #[derive(PartialEq, Clone, Copy, Debug, Default, BitStore)] struct TapState { secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct InputState { previous_shoot_one: bool, previous_shoot_two: bool, previous_tap_input: [bool; NUM_TAP_KEYS], tap_state: [TapState; NUM_TAP_KEYS], } impl repl::Component for InputState {} #[derive(Component, PartialEq, Clone, Copy, Debug, BitStore)] #[storage(BTreeStorage)] pub struct Player { pub hooks: [EntityId; NUM_HOOKS], } impl repl::Component for Player { const STATIC: bool = true; } #[derive(PartialEq, Clone, Copy, Debug, BitStore)] pub struct DashState { pub direction: [f32; 2], pub secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct State { pub dash_cooldown_secs: f32, pub dash_state: Option<DashState>, } impl repl::Component for State {} impl State { pub fn dash(&mut self, direction: Vector2<f32>) { if self.dash_cooldown_secs == 0.0 { self.dash_cooldown_secs = DASH_COOLDOWN_SECS; self.dash_state = Some(DashState { direction: [direction.x, direction.y], secs_left: DASH_SECS, }); } } pub fn update_dash(&mut self, dt: f32) { self.dash_cooldown_secs -= dt; if self.dash_cooldown_secs < 0.0 { self.dash_cooldown_secs = 0.0; } self.dash_state = self.dash_state.as_ref().and_then(|dash_state| { let secs_left = dash_state.secs_left - dt; if secs_left <= 0.0 { None } else { Some(DashState { secs_left, ..*dash_state }) } }); } } pub fn run_input( world: &mut World, inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { // Update hooks for &(_, ref input, entity) in inputs { let player = *repl::try(&world.read::<Player>(), entity)?; let input_state = *repl::try(&world.read::<InputState>(), entity)?; for i in 0..NUM_HOOKS { let hook_entity = repl::try_id_to_entity(world, player.hooks[i])?; let hook_input = hook::CurrentInput { rot_angle: input.rot_angle, shoot: if i == 0 { input.shoot_one } else { input.shoot_two }, previous_shoot: if i == 0 { input_state.previous_shoot_one } else { input_state.previous_shoot_two }, pull: if i == 0 { input.pull_one } else { input.pull_two }, }; world .write::<hook::CurrentInput>() .insert(hook_entity, hook_input); } } hook::run_input(&world)?; // Update player for &(_, ref input, entity) in inputs { world .write::<CurrentInput>() .insert(entity, CurrentInput::new(input.clone())); } InputSys.run_now(&world.res); Ok(()) } pub fn
( world: &mut World, _inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { hook::run_input_post_sim(&world)?; world.write::<hook::CurrentInput>().clear(); world.write::<CurrentInput>().clear(); Ok(()) } pub mod auth { use super::*; pub fn create(world: &mut World, owner: PlayerId, pos: Point2<f32>) -> (EntityId, Entity) { let (id, entity) = repl::entity::auth::create(world, owner, "player", |builder| { builder.with(Position(pos)) }); let mut hooks = [INVALID_ENTITY_ID; NUM_HOOKS]; for (i, hook) in hooks.iter_mut().enumerate() { let (hook_id, _) = hook::auth::create(world, id, i as u32); *hook = hook_id; } // Now that we have created our hooks, attach the player definition world.write::<Player>().insert(entity, Player { hooks }); (id, entity) } } fn build_player(builder: EntityBuilder) -> EntityBuilder { let shape = Cuboid::new(Vector2::new(WIDTH / 2.0, HEIGHT / 2.0)); let mut groups = CollisionGroups::new(); groups.set_membership(&[collision::GROUP_PLAYER]); groups.set_whitelist(&[ collision::GROUP_PLAYER, collision::GROUP_WALL, collision::GROUP_PLAYER_ENTITY, collision::GROUP_NEUTRAL, ]); let query_type = GeometricQueryType::Contacts(0.0, 0.0); // TODO: Velocity (and Dynamic?) component should be added only for owners builder .with(Orientation(0.0)) .with(Velocity(zero())) .with(AngularVelocity(0.0)) .with(InvMass(1.0 / MASS)) .with(InvAngularMass(1.0 / 10.0)) .with(Dynamic) .with(Drag(DRAG)) .with(collision::Shape(ShapeHandle::new(shape))) .with(collision::Object { groups, query_type }) .with(InputState::default()) .with(State::default()) } #[derive(SystemData)] struct InputData<'a> { game_info: Fetch<'a, GameInfo>, input: WriteStorage<'a, CurrentInput>, orientation: WriteStorage<'a, Orientation>, velocity: WriteStorage<'a, Velocity>, angular_velocity: WriteStorage<'a, AngularVelocity>, state: WriteStorage<'a, State>, input_state: WriteStorage<'a, InputState>, } struct InputSys; impl<'a> System<'a> for InputSys { type SystemData = InputData<'a>; fn run(&mut self, mut data: InputData<'a>) { let dt = data.game_info.tick_duration_secs(); // Update tap state for (mut input, input_state) in (&mut data.input, &mut data.input_state).join() { let tap_input = [ input.0.move_forward, input.0.move_backward, input.0.move_left, input.0.move_right, ]; for i in 0..NUM_TAP_KEYS { if tap_input[i] && !input_state.previous_tap_input[i] { if input_state.tap_state[i].secs_left > 0.0 { input.1[i] = true; input_state.tap_state[i].secs_left = 0.0; } else { input_state.tap_state[i].secs_left = TAP_SECS; } } input_state.tap_state[i].secs_left -= dt; if input_state.tap_state[i].secs_left < 0.0 { input_state.tap_state[i].secs_left = 0.0; } input_state.previous_tap_input[i] = tap_input[i]; } } // Movement for (input, orientation, velocity, angular_velocity, state) in ( &data.input, &mut data.orientation, &mut data.velocity, &mut data.angular_velocity, &mut data.state, ).join() { // Dashing let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); if input.1[MOVE_FORWARD_KEY] { state.dash(forward); } if input.1[MOVE_BACKWARD_KEY] { state.dash(-forward); } if input.1[MOVE_RIGHT_KEY] { state.dash(right); } if input.1[MOVE_LEFT_KEY] { state.dash(-right); } state.update_dash(dt); if let Some(dash_state) = state.dash_state.as_ref() { velocity.0 += Vector2::new(dash_state.direction[0], dash_state.direction[1]) * DASH_ACCEL * dt; continue; } /*if input.0.rot_angle != orientation.0 { // TODO: Only mutate if changed orientation.0 = input.0.rot_angle; }*/ let diff = (input.0.rot_angle - orientation.0 + f32::consts::PI) % (2.0 * f32::consts::PI) - f32::consts::PI; let smallest_angle = if diff < -f32::consts::PI { diff + 2.0 * f32::consts::PI } else { diff }; if smallest_angle.abs() <= SNAP_ANGLE { orientation.0 = input.0.rot_angle; } else if smallest_angle < 0.0 { angular_velocity.0 -= ROT_ACCEL * dt; } else if smallest_angle > 0.0 { angular_velocity.0 += ROT_ACCEL * dt; } if angular_velocity.0.abs() > MAX_ANGULAR_VEL { angular_velocity.0 = angular_velocity.0.signum() * MAX_ANGULAR_VEL; } let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); let mut direction = Vector2::new(0.0, 0.0); if input.0.move_forward { direction += forward; } if input.0.move_backward { direction -= forward; } if input.0.move_right { direction += right; } if input.0.move_left { direction -= right; } let direction_norm = norm(&direction); if direction_norm > 0.0 { velocity.0 += direction / direction_norm * MOVE_ACCEL * dt; //velocity.0 += direction / direction_norm * 25.0; } } // Remember some input state for (input, input_state) in (&data.input, &mut data.input_state).join() { input_state.previous_shoot_one = input.0.shoot_one; input_state.previous_shoot_two = input.0.shoot_two; } } }
run_input_post_sim
identifier_name
player.rs
use std::f32; use nalgebra::{norm, zero, Point2, Rotation2, Vector2}; use specs::prelude::*; use specs::storage::BTreeStorage; use defs::{EntityId, GameInfo, PlayerId, PlayerInput, INVALID_ENTITY_ID}; use event::{self, Event}; use game::entity::hook; use game::ComponentType; use physics::collision::{self, CollisionGroups, Cuboid, GeometricQueryType, ShapeHandle}; use physics::interaction; use physics::{AngularVelocity, Drag, Dynamic, InvAngularMass, InvMass, Orientation, Position, Velocity}; use registry::Registry; use repl; pub fn register(reg: &mut Registry) { reg.component::<InputState>(); reg.component::<CurrentInput>(); reg.component::<Player>(); reg.component::<State>(); reg.event::<DashedEvent>(); repl::entity::register_class( reg, "player", &[ ComponentType::Position, ComponentType::Orientation, ComponentType::Player, // TODO: Only send to owner ComponentType::Velocity, ComponentType::AngularVelocity, ComponentType::PlayerInputState, ComponentType::PlayerState, ], build_player, ); interaction::set( reg, "player", "wall", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); interaction::set( reg, "player", "test", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); // FIXME: Due to a bug in physics sim, other player also gets moved interaction::set( reg, "player", "player", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); } pub const NUM_HOOKS: usize = 2; pub const WIDTH: f32 = 40.0; pub const HEIGHT: f32 = 40.0; pub const MOVE_ACCEL: f32 = 3000.0; pub const ROT_ACCEL: f32 = 200.0; pub const MASS: f32 = 50.0; pub const DRAG: f32 = 4.0; pub const SNAP_ANGLE: f32 = f32::consts::PI / 12.0; pub const MAX_ANGULAR_VEL: f32 = f32::consts::PI * 5.0; pub const TAP_SECS: f32 = 0.25; pub const DASH_SECS: f32 = 0.3; pub const DASH_COOLDOWN_SECS: f32 = 2.0; pub const DASH_ACCEL: f32 = 10000.0; #[derive(Debug, Clone, BitStore)] pub struct DashedEvent { /// Different hook colors for drawing. pub hook_index: u32, } impl Event for DashedEvent { fn class(&self) -> event::Class { event::Class::Order } } /// Component that is attached whenever player input should be executed for an entity. #[derive(Component, Clone, Debug)] #[storage(BTreeStorage)] pub struct CurrentInput(pub PlayerInput, [bool; NUM_TAP_KEYS]); impl CurrentInput { fn new(input: PlayerInput) -> CurrentInput { CurrentInput(input, [false; NUM_TAP_KEYS]) } } // Tappable keys const MOVE_FORWARD_KEY: usize = 0; const MOVE_BACKWARD_KEY: usize = 1; const MOVE_LEFT_KEY: usize = 2; const MOVE_RIGHT_KEY: usize = 3; const NUM_TAP_KEYS: usize = 4; #[derive(PartialEq, Clone, Copy, Debug, Default, BitStore)] struct TapState { secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct InputState { previous_shoot_one: bool, previous_shoot_two: bool, previous_tap_input: [bool; NUM_TAP_KEYS], tap_state: [TapState; NUM_TAP_KEYS], } impl repl::Component for InputState {} #[derive(Component, PartialEq, Clone, Copy, Debug, BitStore)] #[storage(BTreeStorage)] pub struct Player { pub hooks: [EntityId; NUM_HOOKS], } impl repl::Component for Player { const STATIC: bool = true; } #[derive(PartialEq, Clone, Copy, Debug, BitStore)] pub struct DashState { pub direction: [f32; 2], pub secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct State { pub dash_cooldown_secs: f32, pub dash_state: Option<DashState>, } impl repl::Component for State {} impl State { pub fn dash(&mut self, direction: Vector2<f32>) { if self.dash_cooldown_secs == 0.0 { self.dash_cooldown_secs = DASH_COOLDOWN_SECS; self.dash_state = Some(DashState { direction: [direction.x, direction.y], secs_left: DASH_SECS, }); } } pub fn update_dash(&mut self, dt: f32) { self.dash_cooldown_secs -= dt; if self.dash_cooldown_secs < 0.0 { self.dash_cooldown_secs = 0.0; } self.dash_state = self.dash_state.as_ref().and_then(|dash_state| { let secs_left = dash_state.secs_left - dt; if secs_left <= 0.0 { None } else { Some(DashState { secs_left, ..*dash_state }) } }); } } pub fn run_input( world: &mut World, inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { // Update hooks for &(_, ref input, entity) in inputs { let player = *repl::try(&world.read::<Player>(), entity)?; let input_state = *repl::try(&world.read::<InputState>(), entity)?; for i in 0..NUM_HOOKS { let hook_entity = repl::try_id_to_entity(world, player.hooks[i])?; let hook_input = hook::CurrentInput { rot_angle: input.rot_angle, shoot: if i == 0 { input.shoot_one } else { input.shoot_two }, previous_shoot: if i == 0 { input_state.previous_shoot_one } else { input_state.previous_shoot_two }, pull: if i == 0 { input.pull_one } else { input.pull_two }, }; world .write::<hook::CurrentInput>() .insert(hook_entity, hook_input); } } hook::run_input(&world)?; // Update player for &(_, ref input, entity) in inputs { world .write::<CurrentInput>() .insert(entity, CurrentInput::new(input.clone())); } InputSys.run_now(&world.res); Ok(()) } pub fn run_input_post_sim( world: &mut World, _inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error>
pub mod auth { use super::*; pub fn create(world: &mut World, owner: PlayerId, pos: Point2<f32>) -> (EntityId, Entity) { let (id, entity) = repl::entity::auth::create(world, owner, "player", |builder| { builder.with(Position(pos)) }); let mut hooks = [INVALID_ENTITY_ID; NUM_HOOKS]; for (i, hook) in hooks.iter_mut().enumerate() { let (hook_id, _) = hook::auth::create(world, id, i as u32); *hook = hook_id; } // Now that we have created our hooks, attach the player definition world.write::<Player>().insert(entity, Player { hooks }); (id, entity) } } fn build_player(builder: EntityBuilder) -> EntityBuilder { let shape = Cuboid::new(Vector2::new(WIDTH / 2.0, HEIGHT / 2.0)); let mut groups = CollisionGroups::new(); groups.set_membership(&[collision::GROUP_PLAYER]); groups.set_whitelist(&[ collision::GROUP_PLAYER, collision::GROUP_WALL, collision::GROUP_PLAYER_ENTITY, collision::GROUP_NEUTRAL, ]); let query_type = GeometricQueryType::Contacts(0.0, 0.0); // TODO: Velocity (and Dynamic?) component should be added only for owners builder .with(Orientation(0.0)) .with(Velocity(zero())) .with(AngularVelocity(0.0)) .with(InvMass(1.0 / MASS)) .with(InvAngularMass(1.0 / 10.0)) .with(Dynamic) .with(Drag(DRAG)) .with(collision::Shape(ShapeHandle::new(shape))) .with(collision::Object { groups, query_type }) .with(InputState::default()) .with(State::default()) } #[derive(SystemData)] struct InputData<'a> { game_info: Fetch<'a, GameInfo>, input: WriteStorage<'a, CurrentInput>, orientation: WriteStorage<'a, Orientation>, velocity: WriteStorage<'a, Velocity>, angular_velocity: WriteStorage<'a, AngularVelocity>, state: WriteStorage<'a, State>, input_state: WriteStorage<'a, InputState>, } struct InputSys; impl<'a> System<'a> for InputSys { type SystemData = InputData<'a>; fn run(&mut self, mut data: InputData<'a>) { let dt = data.game_info.tick_duration_secs(); // Update tap state for (mut input, input_state) in (&mut data.input, &mut data.input_state).join() { let tap_input = [ input.0.move_forward, input.0.move_backward, input.0.move_left, input.0.move_right, ]; for i in 0..NUM_TAP_KEYS { if tap_input[i] && !input_state.previous_tap_input[i] { if input_state.tap_state[i].secs_left > 0.0 { input.1[i] = true; input_state.tap_state[i].secs_left = 0.0; } else { input_state.tap_state[i].secs_left = TAP_SECS; } } input_state.tap_state[i].secs_left -= dt; if input_state.tap_state[i].secs_left < 0.0 { input_state.tap_state[i].secs_left = 0.0; } input_state.previous_tap_input[i] = tap_input[i]; } } // Movement for (input, orientation, velocity, angular_velocity, state) in ( &data.input, &mut data.orientation, &mut data.velocity, &mut data.angular_velocity, &mut data.state, ).join() { // Dashing let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); if input.1[MOVE_FORWARD_KEY] { state.dash(forward); } if input.1[MOVE_BACKWARD_KEY] { state.dash(-forward); } if input.1[MOVE_RIGHT_KEY] { state.dash(right); } if input.1[MOVE_LEFT_KEY] { state.dash(-right); } state.update_dash(dt); if let Some(dash_state) = state.dash_state.as_ref() { velocity.0 += Vector2::new(dash_state.direction[0], dash_state.direction[1]) * DASH_ACCEL * dt; continue; } /*if input.0.rot_angle != orientation.0 { // TODO: Only mutate if changed orientation.0 = input.0.rot_angle; }*/ let diff = (input.0.rot_angle - orientation.0 + f32::consts::PI) % (2.0 * f32::consts::PI) - f32::consts::PI; let smallest_angle = if diff < -f32::consts::PI { diff + 2.0 * f32::consts::PI } else { diff }; if smallest_angle.abs() <= SNAP_ANGLE { orientation.0 = input.0.rot_angle; } else if smallest_angle < 0.0 { angular_velocity.0 -= ROT_ACCEL * dt; } else if smallest_angle > 0.0 { angular_velocity.0 += ROT_ACCEL * dt; } if angular_velocity.0.abs() > MAX_ANGULAR_VEL { angular_velocity.0 = angular_velocity.0.signum() * MAX_ANGULAR_VEL; } let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); let mut direction = Vector2::new(0.0, 0.0); if input.0.move_forward { direction += forward; } if input.0.move_backward { direction -= forward; } if input.0.move_right { direction += right; } if input.0.move_left { direction -= right; } let direction_norm = norm(&direction); if direction_norm > 0.0 { velocity.0 += direction / direction_norm * MOVE_ACCEL * dt; //velocity.0 += direction / direction_norm * 25.0; } } // Remember some input state for (input, input_state) in (&data.input, &mut data.input_state).join() { input_state.previous_shoot_one = input.0.shoot_one; input_state.previous_shoot_two = input.0.shoot_two; } } }
{ hook::run_input_post_sim(&world)?; world.write::<hook::CurrentInput>().clear(); world.write::<CurrentInput>().clear(); Ok(()) }
identifier_body
player.rs
use std::f32; use nalgebra::{norm, zero, Point2, Rotation2, Vector2}; use specs::prelude::*; use specs::storage::BTreeStorage; use defs::{EntityId, GameInfo, PlayerId, PlayerInput, INVALID_ENTITY_ID}; use event::{self, Event}; use game::entity::hook; use game::ComponentType; use physics::collision::{self, CollisionGroups, Cuboid, GeometricQueryType, ShapeHandle}; use physics::interaction; use physics::{AngularVelocity, Drag, Dynamic, InvAngularMass, InvMass, Orientation, Position, Velocity}; use registry::Registry; use repl; pub fn register(reg: &mut Registry) { reg.component::<InputState>(); reg.component::<CurrentInput>(); reg.component::<Player>(); reg.component::<State>(); reg.event::<DashedEvent>(); repl::entity::register_class( reg, "player", &[ ComponentType::Position, ComponentType::Orientation, ComponentType::Player, // TODO: Only send to owner ComponentType::Velocity, ComponentType::AngularVelocity, ComponentType::PlayerInputState, ComponentType::PlayerState, ], build_player, ); interaction::set( reg, "player", "wall", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); interaction::set( reg, "player", "test", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); // FIXME: Due to a bug in physics sim, other player also gets moved interaction::set( reg, "player", "player", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); } pub const NUM_HOOKS: usize = 2; pub const WIDTH: f32 = 40.0; pub const HEIGHT: f32 = 40.0; pub const MOVE_ACCEL: f32 = 3000.0; pub const ROT_ACCEL: f32 = 200.0; pub const MASS: f32 = 50.0; pub const DRAG: f32 = 4.0; pub const SNAP_ANGLE: f32 = f32::consts::PI / 12.0; pub const MAX_ANGULAR_VEL: f32 = f32::consts::PI * 5.0; pub const TAP_SECS: f32 = 0.25; pub const DASH_SECS: f32 = 0.3; pub const DASH_COOLDOWN_SECS: f32 = 2.0; pub const DASH_ACCEL: f32 = 10000.0; #[derive(Debug, Clone, BitStore)] pub struct DashedEvent { /// Different hook colors for drawing. pub hook_index: u32, } impl Event for DashedEvent { fn class(&self) -> event::Class { event::Class::Order } } /// Component that is attached whenever player input should be executed for an entity. #[derive(Component, Clone, Debug)] #[storage(BTreeStorage)] pub struct CurrentInput(pub PlayerInput, [bool; NUM_TAP_KEYS]); impl CurrentInput { fn new(input: PlayerInput) -> CurrentInput { CurrentInput(input, [false; NUM_TAP_KEYS]) } } // Tappable keys const MOVE_FORWARD_KEY: usize = 0; const MOVE_BACKWARD_KEY: usize = 1; const MOVE_LEFT_KEY: usize = 2; const MOVE_RIGHT_KEY: usize = 3; const NUM_TAP_KEYS: usize = 4; #[derive(PartialEq, Clone, Copy, Debug, Default, BitStore)] struct TapState { secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct InputState { previous_shoot_one: bool, previous_shoot_two: bool, previous_tap_input: [bool; NUM_TAP_KEYS], tap_state: [TapState; NUM_TAP_KEYS], } impl repl::Component for InputState {} #[derive(Component, PartialEq, Clone, Copy, Debug, BitStore)] #[storage(BTreeStorage)] pub struct Player { pub hooks: [EntityId; NUM_HOOKS], } impl repl::Component for Player { const STATIC: bool = true; } #[derive(PartialEq, Clone, Copy, Debug, BitStore)] pub struct DashState { pub direction: [f32; 2], pub secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct State { pub dash_cooldown_secs: f32, pub dash_state: Option<DashState>, } impl repl::Component for State {} impl State { pub fn dash(&mut self, direction: Vector2<f32>) { if self.dash_cooldown_secs == 0.0 { self.dash_cooldown_secs = DASH_COOLDOWN_SECS; self.dash_state = Some(DashState { direction: [direction.x, direction.y], secs_left: DASH_SECS, }); } } pub fn update_dash(&mut self, dt: f32) { self.dash_cooldown_secs -= dt; if self.dash_cooldown_secs < 0.0 { self.dash_cooldown_secs = 0.0; } self.dash_state = self.dash_state.as_ref().and_then(|dash_state| { let secs_left = dash_state.secs_left - dt; if secs_left <= 0.0 { None } else { Some(DashState { secs_left, ..*dash_state }) } }); } } pub fn run_input( world: &mut World, inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { // Update hooks for &(_, ref input, entity) in inputs { let player = *repl::try(&world.read::<Player>(), entity)?; let input_state = *repl::try(&world.read::<InputState>(), entity)?; for i in 0..NUM_HOOKS { let hook_entity = repl::try_id_to_entity(world, player.hooks[i])?; let hook_input = hook::CurrentInput { rot_angle: input.rot_angle, shoot: if i == 0 { input.shoot_one } else { input.shoot_two }, previous_shoot: if i == 0 { input_state.previous_shoot_one } else { input_state.previous_shoot_two }, pull: if i == 0 { input.pull_one } else { input.pull_two }, }; world .write::<hook::CurrentInput>() .insert(hook_entity, hook_input); } } hook::run_input(&world)?; // Update player for &(_, ref input, entity) in inputs { world .write::<CurrentInput>() .insert(entity, CurrentInput::new(input.clone())); } InputSys.run_now(&world.res); Ok(()) } pub fn run_input_post_sim( world: &mut World, _inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { hook::run_input_post_sim(&world)?; world.write::<hook::CurrentInput>().clear(); world.write::<CurrentInput>().clear(); Ok(()) } pub mod auth { use super::*; pub fn create(world: &mut World, owner: PlayerId, pos: Point2<f32>) -> (EntityId, Entity) { let (id, entity) = repl::entity::auth::create(world, owner, "player", |builder| { builder.with(Position(pos)) }); let mut hooks = [INVALID_ENTITY_ID; NUM_HOOKS]; for (i, hook) in hooks.iter_mut().enumerate() { let (hook_id, _) = hook::auth::create(world, id, i as u32); *hook = hook_id; } // Now that we have created our hooks, attach the player definition world.write::<Player>().insert(entity, Player { hooks }); (id, entity) } } fn build_player(builder: EntityBuilder) -> EntityBuilder { let shape = Cuboid::new(Vector2::new(WIDTH / 2.0, HEIGHT / 2.0)); let mut groups = CollisionGroups::new(); groups.set_membership(&[collision::GROUP_PLAYER]); groups.set_whitelist(&[ collision::GROUP_PLAYER, collision::GROUP_WALL, collision::GROUP_PLAYER_ENTITY, collision::GROUP_NEUTRAL, ]); let query_type = GeometricQueryType::Contacts(0.0, 0.0); // TODO: Velocity (and Dynamic?) component should be added only for owners builder .with(Orientation(0.0)) .with(Velocity(zero())) .with(AngularVelocity(0.0)) .with(InvMass(1.0 / MASS)) .with(InvAngularMass(1.0 / 10.0)) .with(Dynamic) .with(Drag(DRAG)) .with(collision::Shape(ShapeHandle::new(shape))) .with(collision::Object { groups, query_type }) .with(InputState::default()) .with(State::default()) } #[derive(SystemData)] struct InputData<'a> { game_info: Fetch<'a, GameInfo>, input: WriteStorage<'a, CurrentInput>, orientation: WriteStorage<'a, Orientation>, velocity: WriteStorage<'a, Velocity>, angular_velocity: WriteStorage<'a, AngularVelocity>, state: WriteStorage<'a, State>, input_state: WriteStorage<'a, InputState>, } struct InputSys; impl<'a> System<'a> for InputSys { type SystemData = InputData<'a>; fn run(&mut self, mut data: InputData<'a>) { let dt = data.game_info.tick_duration_secs(); // Update tap state for (mut input, input_state) in (&mut data.input, &mut data.input_state).join() { let tap_input = [ input.0.move_forward, input.0.move_backward, input.0.move_left, input.0.move_right, ]; for i in 0..NUM_TAP_KEYS { if tap_input[i] && !input_state.previous_tap_input[i] { if input_state.tap_state[i].secs_left > 0.0 { input.1[i] = true; input_state.tap_state[i].secs_left = 0.0; } else { input_state.tap_state[i].secs_left = TAP_SECS; } } input_state.tap_state[i].secs_left -= dt; if input_state.tap_state[i].secs_left < 0.0 { input_state.tap_state[i].secs_left = 0.0; } input_state.previous_tap_input[i] = tap_input[i]; } } // Movement for (input, orientation, velocity, angular_velocity, state) in ( &data.input, &mut data.orientation, &mut data.velocity, &mut data.angular_velocity, &mut data.state, ).join() { // Dashing let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); if input.1[MOVE_FORWARD_KEY] { state.dash(forward); } if input.1[MOVE_BACKWARD_KEY]
if input.1[MOVE_RIGHT_KEY] { state.dash(right); } if input.1[MOVE_LEFT_KEY] { state.dash(-right); } state.update_dash(dt); if let Some(dash_state) = state.dash_state.as_ref() { velocity.0 += Vector2::new(dash_state.direction[0], dash_state.direction[1]) * DASH_ACCEL * dt; continue; } /*if input.0.rot_angle != orientation.0 { // TODO: Only mutate if changed orientation.0 = input.0.rot_angle; }*/ let diff = (input.0.rot_angle - orientation.0 + f32::consts::PI) % (2.0 * f32::consts::PI) - f32::consts::PI; let smallest_angle = if diff < -f32::consts::PI { diff + 2.0 * f32::consts::PI } else { diff }; if smallest_angle.abs() <= SNAP_ANGLE { orientation.0 = input.0.rot_angle; } else if smallest_angle < 0.0 { angular_velocity.0 -= ROT_ACCEL * dt; } else if smallest_angle > 0.0 { angular_velocity.0 += ROT_ACCEL * dt; } if angular_velocity.0.abs() > MAX_ANGULAR_VEL { angular_velocity.0 = angular_velocity.0.signum() * MAX_ANGULAR_VEL; } let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); let mut direction = Vector2::new(0.0, 0.0); if input.0.move_forward { direction += forward; } if input.0.move_backward { direction -= forward; } if input.0.move_right { direction += right; } if input.0.move_left { direction -= right; } let direction_norm = norm(&direction); if direction_norm > 0.0 { velocity.0 += direction / direction_norm * MOVE_ACCEL * dt; //velocity.0 += direction / direction_norm * 25.0; } } // Remember some input state for (input, input_state) in (&data.input, &mut data.input_state).join() { input_state.previous_shoot_one = input.0.shoot_one; input_state.previous_shoot_two = input.0.shoot_two; } } }
{ state.dash(-forward); }
conditional_block
player.rs
use std::f32; use nalgebra::{norm, zero, Point2, Rotation2, Vector2}; use specs::prelude::*; use specs::storage::BTreeStorage; use defs::{EntityId, GameInfo, PlayerId, PlayerInput, INVALID_ENTITY_ID}; use event::{self, Event}; use game::entity::hook; use game::ComponentType; use physics::collision::{self, CollisionGroups, Cuboid, GeometricQueryType, ShapeHandle}; use physics::interaction; use physics::{AngularVelocity, Drag, Dynamic, InvAngularMass, InvMass, Orientation, Position, Velocity}; use registry::Registry; use repl; pub fn register(reg: &mut Registry) { reg.component::<InputState>(); reg.component::<CurrentInput>(); reg.component::<Player>(); reg.component::<State>(); reg.event::<DashedEvent>(); repl::entity::register_class( reg, "player", &[ ComponentType::Position, ComponentType::Orientation, ComponentType::Player, // TODO: Only send to owner ComponentType::Velocity, ComponentType::AngularVelocity, ComponentType::PlayerInputState, ComponentType::PlayerState, ], build_player, ); interaction::set( reg, "player", "wall", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); interaction::set( reg, "player", "test", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); // FIXME: Due to a bug in physics sim, other player also gets moved interaction::set( reg, "player", "player", Some(interaction::Action::PreventOverlap { rotate_a: false, rotate_b: false, }), None, ); } pub const NUM_HOOKS: usize = 2; pub const WIDTH: f32 = 40.0; pub const HEIGHT: f32 = 40.0; pub const MOVE_ACCEL: f32 = 3000.0; pub const ROT_ACCEL: f32 = 200.0; pub const MASS: f32 = 50.0; pub const DRAG: f32 = 4.0; pub const SNAP_ANGLE: f32 = f32::consts::PI / 12.0; pub const MAX_ANGULAR_VEL: f32 = f32::consts::PI * 5.0; pub const TAP_SECS: f32 = 0.25; pub const DASH_SECS: f32 = 0.3; pub const DASH_COOLDOWN_SECS: f32 = 2.0; pub const DASH_ACCEL: f32 = 10000.0; #[derive(Debug, Clone, BitStore)] pub struct DashedEvent { /// Different hook colors for drawing. pub hook_index: u32, }
fn class(&self) -> event::Class { event::Class::Order } } /// Component that is attached whenever player input should be executed for an entity. #[derive(Component, Clone, Debug)] #[storage(BTreeStorage)] pub struct CurrentInput(pub PlayerInput, [bool; NUM_TAP_KEYS]); impl CurrentInput { fn new(input: PlayerInput) -> CurrentInput { CurrentInput(input, [false; NUM_TAP_KEYS]) } } // Tappable keys const MOVE_FORWARD_KEY: usize = 0; const MOVE_BACKWARD_KEY: usize = 1; const MOVE_LEFT_KEY: usize = 2; const MOVE_RIGHT_KEY: usize = 3; const NUM_TAP_KEYS: usize = 4; #[derive(PartialEq, Clone, Copy, Debug, Default, BitStore)] struct TapState { secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct InputState { previous_shoot_one: bool, previous_shoot_two: bool, previous_tap_input: [bool; NUM_TAP_KEYS], tap_state: [TapState; NUM_TAP_KEYS], } impl repl::Component for InputState {} #[derive(Component, PartialEq, Clone, Copy, Debug, BitStore)] #[storage(BTreeStorage)] pub struct Player { pub hooks: [EntityId; NUM_HOOKS], } impl repl::Component for Player { const STATIC: bool = true; } #[derive(PartialEq, Clone, Copy, Debug, BitStore)] pub struct DashState { pub direction: [f32; 2], pub secs_left: f32, } #[derive(Component, PartialEq, Clone, Copy, Debug, Default, BitStore)] #[storage(BTreeStorage)] pub struct State { pub dash_cooldown_secs: f32, pub dash_state: Option<DashState>, } impl repl::Component for State {} impl State { pub fn dash(&mut self, direction: Vector2<f32>) { if self.dash_cooldown_secs == 0.0 { self.dash_cooldown_secs = DASH_COOLDOWN_SECS; self.dash_state = Some(DashState { direction: [direction.x, direction.y], secs_left: DASH_SECS, }); } } pub fn update_dash(&mut self, dt: f32) { self.dash_cooldown_secs -= dt; if self.dash_cooldown_secs < 0.0 { self.dash_cooldown_secs = 0.0; } self.dash_state = self.dash_state.as_ref().and_then(|dash_state| { let secs_left = dash_state.secs_left - dt; if secs_left <= 0.0 { None } else { Some(DashState { secs_left, ..*dash_state }) } }); } } pub fn run_input( world: &mut World, inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { // Update hooks for &(_, ref input, entity) in inputs { let player = *repl::try(&world.read::<Player>(), entity)?; let input_state = *repl::try(&world.read::<InputState>(), entity)?; for i in 0..NUM_HOOKS { let hook_entity = repl::try_id_to_entity(world, player.hooks[i])?; let hook_input = hook::CurrentInput { rot_angle: input.rot_angle, shoot: if i == 0 { input.shoot_one } else { input.shoot_two }, previous_shoot: if i == 0 { input_state.previous_shoot_one } else { input_state.previous_shoot_two }, pull: if i == 0 { input.pull_one } else { input.pull_two }, }; world .write::<hook::CurrentInput>() .insert(hook_entity, hook_input); } } hook::run_input(&world)?; // Update player for &(_, ref input, entity) in inputs { world .write::<CurrentInput>() .insert(entity, CurrentInput::new(input.clone())); } InputSys.run_now(&world.res); Ok(()) } pub fn run_input_post_sim( world: &mut World, _inputs: &[(PlayerId, PlayerInput, Entity)], ) -> Result<(), repl::Error> { hook::run_input_post_sim(&world)?; world.write::<hook::CurrentInput>().clear(); world.write::<CurrentInput>().clear(); Ok(()) } pub mod auth { use super::*; pub fn create(world: &mut World, owner: PlayerId, pos: Point2<f32>) -> (EntityId, Entity) { let (id, entity) = repl::entity::auth::create(world, owner, "player", |builder| { builder.with(Position(pos)) }); let mut hooks = [INVALID_ENTITY_ID; NUM_HOOKS]; for (i, hook) in hooks.iter_mut().enumerate() { let (hook_id, _) = hook::auth::create(world, id, i as u32); *hook = hook_id; } // Now that we have created our hooks, attach the player definition world.write::<Player>().insert(entity, Player { hooks }); (id, entity) } } fn build_player(builder: EntityBuilder) -> EntityBuilder { let shape = Cuboid::new(Vector2::new(WIDTH / 2.0, HEIGHT / 2.0)); let mut groups = CollisionGroups::new(); groups.set_membership(&[collision::GROUP_PLAYER]); groups.set_whitelist(&[ collision::GROUP_PLAYER, collision::GROUP_WALL, collision::GROUP_PLAYER_ENTITY, collision::GROUP_NEUTRAL, ]); let query_type = GeometricQueryType::Contacts(0.0, 0.0); // TODO: Velocity (and Dynamic?) component should be added only for owners builder .with(Orientation(0.0)) .with(Velocity(zero())) .with(AngularVelocity(0.0)) .with(InvMass(1.0 / MASS)) .with(InvAngularMass(1.0 / 10.0)) .with(Dynamic) .with(Drag(DRAG)) .with(collision::Shape(ShapeHandle::new(shape))) .with(collision::Object { groups, query_type }) .with(InputState::default()) .with(State::default()) } #[derive(SystemData)] struct InputData<'a> { game_info: Fetch<'a, GameInfo>, input: WriteStorage<'a, CurrentInput>, orientation: WriteStorage<'a, Orientation>, velocity: WriteStorage<'a, Velocity>, angular_velocity: WriteStorage<'a, AngularVelocity>, state: WriteStorage<'a, State>, input_state: WriteStorage<'a, InputState>, } struct InputSys; impl<'a> System<'a> for InputSys { type SystemData = InputData<'a>; fn run(&mut self, mut data: InputData<'a>) { let dt = data.game_info.tick_duration_secs(); // Update tap state for (mut input, input_state) in (&mut data.input, &mut data.input_state).join() { let tap_input = [ input.0.move_forward, input.0.move_backward, input.0.move_left, input.0.move_right, ]; for i in 0..NUM_TAP_KEYS { if tap_input[i] && !input_state.previous_tap_input[i] { if input_state.tap_state[i].secs_left > 0.0 { input.1[i] = true; input_state.tap_state[i].secs_left = 0.0; } else { input_state.tap_state[i].secs_left = TAP_SECS; } } input_state.tap_state[i].secs_left -= dt; if input_state.tap_state[i].secs_left < 0.0 { input_state.tap_state[i].secs_left = 0.0; } input_state.previous_tap_input[i] = tap_input[i]; } } // Movement for (input, orientation, velocity, angular_velocity, state) in ( &data.input, &mut data.orientation, &mut data.velocity, &mut data.angular_velocity, &mut data.state, ).join() { // Dashing let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); if input.1[MOVE_FORWARD_KEY] { state.dash(forward); } if input.1[MOVE_BACKWARD_KEY] { state.dash(-forward); } if input.1[MOVE_RIGHT_KEY] { state.dash(right); } if input.1[MOVE_LEFT_KEY] { state.dash(-right); } state.update_dash(dt); if let Some(dash_state) = state.dash_state.as_ref() { velocity.0 += Vector2::new(dash_state.direction[0], dash_state.direction[1]) * DASH_ACCEL * dt; continue; } /*if input.0.rot_angle != orientation.0 { // TODO: Only mutate if changed orientation.0 = input.0.rot_angle; }*/ let diff = (input.0.rot_angle - orientation.0 + f32::consts::PI) % (2.0 * f32::consts::PI) - f32::consts::PI; let smallest_angle = if diff < -f32::consts::PI { diff + 2.0 * f32::consts::PI } else { diff }; if smallest_angle.abs() <= SNAP_ANGLE { orientation.0 = input.0.rot_angle; } else if smallest_angle < 0.0 { angular_velocity.0 -= ROT_ACCEL * dt; } else if smallest_angle > 0.0 { angular_velocity.0 += ROT_ACCEL * dt; } if angular_velocity.0.abs() > MAX_ANGULAR_VEL { angular_velocity.0 = angular_velocity.0.signum() * MAX_ANGULAR_VEL; } let forward = Rotation2::new(orientation.0).matrix() * Vector2::new(1.0, 0.0); let right = Vector2::new(-forward.y, forward.x); let mut direction = Vector2::new(0.0, 0.0); if input.0.move_forward { direction += forward; } if input.0.move_backward { direction -= forward; } if input.0.move_right { direction += right; } if input.0.move_left { direction -= right; } let direction_norm = norm(&direction); if direction_norm > 0.0 { velocity.0 += direction / direction_norm * MOVE_ACCEL * dt; //velocity.0 += direction / direction_norm * 25.0; } } // Remember some input state for (input, input_state) in (&data.input, &mut data.input_state).join() { input_state.previous_shoot_one = input.0.shoot_one; input_state.previous_shoot_two = input.0.shoot_two; } } }
impl Event for DashedEvent {
random_line_split
Index.js
import React, {Component} from "react"; import "./Index.less"; import { Button, Card, message, LocaleProvider, Col, Select, Row, DatePicker, Upload, Divider, Spin } from "antd"; import BreadcrumbCustom from "../common/BreadcrumbCustom"; import zh_CN from "antd/lib/locale-provider/zh_CN"; import {getToken, splitArr} from "../../utils/filter"; import {exportClue} from "../../api/exportApi"; import {requestData} from "../../utils/importXlsx"; import {connect} from "../../utils/socket"; import {getSubjectList,getPlatformList} from '../../api/commonApi' const Option = Select.Option; const {RangePicker} = DatePicker; message.config({ top: 100, duration: 3, }); let customCondition = {}; let applyData = { size: 40, current: 1, ascs: [], descs: ['createTime'], condition: customCondition }; // let params = { // size: 100, // current: 1, // ascs: [], // descs: ['execTime'], // condition: { // "business": "VIPCOURSE" // } // }; export default class exportList extends Component { constructor(props) { super(props); this.state = { orderDate: null, subjectList: null, platformList: null, subjectValue: null, platformValue: null, newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: false }; } componentDidMount()
; // 日期筛选 handleRangePicker = (rangePickerValue, dateString) => { this.setState({ orderDate: rangePickerValue }); applyData.current = 1; customCondition.startTime = parseInt(new Date(new Date(rangePickerValue[0]).toLocaleDateString()).getTime()/1000); customCondition.endTime = parseInt((new Date(new Date(rangePickerValue[1]).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)/1000); }; //所属学科onchange subjectChange = (value) => { console.log(value,'value') this.setState({ subjectValue: value }); customCondition.subject = value; }; //选择平台onchange platformChange = (value) => { this.setState({ platformValue: value }); customCondition.platform = value; }; // 搜索用户 searchUser = () => { if(!customCondition.startTime && !customCondition.endTime) { message.error('请选择时间') return } console.log(customCondition,'customCondition') exportClue(customCondition).then(res => { if(res.data.code === 0) { message.success('导出成功,请查收邮件') }else { message.error('导出失败') } }) // let params = `startTime=${customCondition.startTime}&endTime=${customCondition.endTime}` // if(customCondition.subject) { // params += `&subject=${customCondition.subject}` // } // // if(customCondition.platform) { // params += `&platform=${customCondition.platform}` // } // window.location.href = `${baseUrl()}/account/clue/export?${params}` }; // 上传课程缩略图icon uploadCourseThumbnail = (files) => { this.setState({ newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: true }) let fileReader = new FileReader(); // 图片上传,读图片 let file = files.file; // 获取到上传的对象 let _this = this; fileReader.onload = (function (file) { requestData(file).then(res => { _this.setState({ spinning: false }) if(res.data.code === 0){ message.success('导入成功') const dataMsg = JSON.parse(res.data.msg) _this.setState({ newCampaignNums: dataMsg.newCampaignNums, countNums: dataMsg.countNums, newClueNums: dataMsg.newClueNums, clueRepeatNums: dataMsg.clueRepeatNums, repeatPhone: splitArr(dataMsg.repeatPhone, 8), errorRows: dataMsg.errorRows }) }else if(res.data.code === 1){ message.error(res.data.msg) }else { message.error('导入失败') } }); })(file); fileReader.readAsDataURL(file); // 读取完毕,显示到页面 }; //获取学科下拉列表 getSubjectListFn = ()=>{ getSubjectList().then(res=>{ if (res.data.code === 0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].id; obj.value = data[i].name; Arr.push(obj); } this.setState({ subjectList:Arr }) } }).catch(err=>{ console.log(err) }) }; //获取平台下拉列表 getPlatformListFn = ()=>{ getPlatformList().then(res=>{ if(res.data.code ===0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].platform; obj.value = data[i].platformName; Arr.push(obj); } this.setState({ platformList:Arr }) } }).catch(err=>{ console.log(err) }) }; componentWillUnmount() { this.searchReset() } // 全部重置 searchReset = () => { this.setState({ searchValue: null, orderTypeValue: undefined, orderStatusValue: undefined, orderDate: undefined, platformValue:undefined, subjectValue:undefined }); customCondition.search = null; customCondition.startTime = null; customCondition.endTime = null; }; render() { const { subjectList, orderDate, platformList, newCampaignNums, countNums, newClueNums, clueRepeatNums, repeatPhone, errorRows, spinning } = this.state; const menus = [ { path: '/app/dashboard/analysis', name: '首页' }, { path: '#', name: '线索I/O' }, { path: '/app/export/index', name: '线索I/O' } ]; return ( <div className="ordercenter-order"> <div className="page-nav"> <BreadcrumbCustom paths={menus}/> <p className="title-style">线索I/O</p> </div> <Card bordered={false}> <Divider>导出功能</Divider> <Row className="my-user-search" gutter={16}> <Col sm={2} style={{textAlign: 'right', width: '65px', marginTop: '5px'}}> <span>日期:</span> </Col> <Col sm={6} style={{padding: 0}} id="order_status_select"> <LocaleProvider locale={zh_CN}> <RangePicker value={orderDate} onChange={this.handleRangePicker} style={{width: '100%'}} /> </LocaleProvider> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>所属学科:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.subjectChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {subjectList && subjectList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>选择平台:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.platformChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {platformList && platformList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> </Row> <Row gutter={16} style={{marginBottom:'20px',marginTop: '20px'}}> <Col className="" sm={13} style={{marginBottom: '20px'}}> <Button type="primary" style={{marginRight: '12px', marginLeft: '10px'}} onClick={this.searchUser} disabled={this.state.disableBtn}>导出</Button> <Button type="primary" onClick={this.searchReset} disabled={this.state.resetBtn}>全部重置</Button> </Col> <Divider>导入功能</Divider> <Col className="" sm={13} style={{marginTop: '20px'}}> <Upload accept=".xls,.xlsx" customRequest={this.uploadCourseThumbnail} beforeUpload={this.beforeUploadCourseThumbnail} > <Button type="upload" style={{marginRight: '12px', marginLeft: '10px'}} disabled={this.state.disableBtn}>导入</Button> </Upload> <a href={require('./xls/template.xlsx')} download>下载导入模版</a> <div style={{marginLeft: '10px',marginTop:'10px'}}> <div className=""> <p>总解析条数 {countNums}</p> <p>新推广名称成功数 {newCampaignNums}</p> <p>新推广线索成功数 {newClueNums}</p> <p>推广线索重复数 {clueRepeatNums}</p> <div style={{marginBottom:'10px'}}>线索重复手机号 { repeatPhone && repeatPhone.map((item,index) => { return <p style={{marginBottom:'0px'}} key={index}>{item.join(',')}</p> }) }</div> <p style={{color: '#f00'}}>错误数量 {errorRows}</p> </div> </div> </Col> </Row> </Card> <div style={{display: spinning ? 'block' : 'none'}} className="spin-box"> <Spin className="spin" spinning={spinning} size="large" /> </div> </div> ) } }
{ this.getSubjectListFn() this.getPlatformListFn() connect(getToken('username')); }
identifier_body
Index.js
import React, {Component} from "react"; import "./Index.less"; import { Button, Card, message, LocaleProvider, Col, Select, Row, DatePicker, Upload, Divider, Spin } from "antd"; import BreadcrumbCustom from "../common/BreadcrumbCustom"; import zh_CN from "antd/lib/locale-provider/zh_CN"; import {getToken, splitArr} from "../../utils/filter"; import {exportClue} from "../../api/exportApi"; import {requestData} from "../../utils/importXlsx"; import {connect} from "../../utils/socket"; import {getSubjectList,getPlatformList} from '../../api/commonApi' const Option = Select.Option; const {RangePicker} = DatePicker; message.config({ top: 100, duration: 3, }); let customCondition = {}; let applyData = { size: 40, current: 1, ascs: [], descs: ['createTime'], condition: customCondition }; // let params = { // size: 100, // current: 1, // ascs: [], // descs: ['execTime'], // condition: { // "business": "VIPCOURSE" // } // }; export default class exportList extends Component { constructor(props) { super(props); this.state = { orderDate: null, subjectList: null, platformList: null, subjectValue: null, platformValue: null, newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: false }; } componentDidMount() { this.getSubjectListFn() this.getPlatformListFn() connect(getToken('username')); }; // 日期筛选 handleRangePicker = (rangePickerValue, dateString) => { this.setState({ orderDate: rangePickerValue }); applyData.current = 1; customCondition.startTime = parseInt(new Date(new Date(rangePickerValue[0]).toLocaleDateString()).getTime()/1000); customCondition.endTime = parseInt((new Date(new Date(rangePickerValue[1]).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)/1000); }; //所属学科onchange subjectChange = (value) => { console.log(value,'value') this.setState({ subjectValue: value }); customCondition.subject = value; }; //选择平台onchange platformChange = (value) => { this.setState({ platformValue: value }); customCondition.platform = value; }; // 搜索用户 searchUser = () => { if(!customCondition.startTime && !customCondition.endTime) { message.error('请选择时间') return } console.log(customCondition,'customCondition') exportClue(customCondition).then(res => { if(res.data.code === 0) { message.success('导出成功,请查收邮件') }else { message.error('导出失败') } }) // let params = `startTime=${customCondition.startTime}&endTime=${customCondition.endTime}` // if(customCondition.subject) { // params += `&subject=${customCondition.subject}` // } // // if(customCondition.platform) { // params += `&platform=${customCondition.platform}` // } // window.location.href = `${baseUrl()}/account/clue/export?${params}` }; // 上传课程缩略图icon uploadCourseThumbnail = (files) => { this.setState({ newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: true }) let fileReader = new FileReader(); // 图片上传,读图片 let file = files.file; // 获取到上传的对象 let _this = this; fileReader.onload = (function (file) { requestData(file).then(res => { _this.setState({ spinning: false }) if(res.data.code === 0){ message.success('导入成功') const dataMsg = JSON.parse(res.data.msg) _this.setState({ newCampaignNums: dataMsg.newCampaignNums, countNums: dataMsg.countNums, newClueNums: dataMsg.newClueNums, clueRepeatNums: dataMsg.clueRepeatNums, repeatPhone: splitArr(dataMsg.repeatPhone, 8), errorRows: dataMsg.errorRows }) }else if(res.data.code === 1){ message.error(res.data.msg) }else { message.error('导入失败') } }); })(file); fileReader.readAsDataURL(file); // 读取完毕,显示到页面 }; //获取学科下拉列表 getSubjectListFn = ()=>{ getSubjectList().then(res=>{ if (res.data.code === 0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].id; obj.value = data[i].name; Arr.push(obj); } this.setState({ subjectList:Arr }) } }).catch(err=>{ console.log(err) }) }; //获取平台下拉列表 getPlatformListFn = ()=>{ getPlatformList().then(res=>{ if(res.data.code ===0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].platform; obj.value = data[i].platformName; Arr.push(obj); } this.setState({ platformList:Arr }) } }).catch(err=>{ console.log(err) }) }; componentWillUnmount() { this.searchReset() } // 全部重置 searchReset = () => { this.setState({ searchValue: null, orderTypeValue: undefined, orderStatusValue: undefined, orderDate: undefined, platformValue:undefined, subjectValue:undefined }); customCondition.search = null; customCondition.startTime = null; customCondition.endTime = null; }; render() { const { subjectList, orderDate, platformList, newCampaignNums, countNums, newClueNums, clueRepeatNums, repeatPhone, errorRows,
const menus = [ { path: '/app/dashboard/analysis', name: '首页' }, { path: '#', name: '线索I/O' }, { path: '/app/export/index', name: '线索I/O' } ]; return ( <div className="ordercenter-order"> <div className="page-nav"> <BreadcrumbCustom paths={menus}/> <p className="title-style">线索I/O</p> </div> <Card bordered={false}> <Divider>导出功能</Divider> <Row className="my-user-search" gutter={16}> <Col sm={2} style={{textAlign: 'right', width: '65px', marginTop: '5px'}}> <span>日期:</span> </Col> <Col sm={6} style={{padding: 0}} id="order_status_select"> <LocaleProvider locale={zh_CN}> <RangePicker value={orderDate} onChange={this.handleRangePicker} style={{width: '100%'}} /> </LocaleProvider> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>所属学科:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.subjectChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {subjectList && subjectList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>选择平台:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.platformChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {platformList && platformList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> </Row> <Row gutter={16} style={{marginBottom:'20px',marginTop: '20px'}}> <Col className="" sm={13} style={{marginBottom: '20px'}}> <Button type="primary" style={{marginRight: '12px', marginLeft: '10px'}} onClick={this.searchUser} disabled={this.state.disableBtn}>导出</Button> <Button type="primary" onClick={this.searchReset} disabled={this.state.resetBtn}>全部重置</Button> </Col> <Divider>导入功能</Divider> <Col className="" sm={13} style={{marginTop: '20px'}}> <Upload accept=".xls,.xlsx" customRequest={this.uploadCourseThumbnail} beforeUpload={this.beforeUploadCourseThumbnail} > <Button type="upload" style={{marginRight: '12px', marginLeft: '10px'}} disabled={this.state.disableBtn}>导入</Button> </Upload> <a href={require('./xls/template.xlsx')} download>下载导入模版</a> <div style={{marginLeft: '10px',marginTop:'10px'}}> <div className=""> <p>总解析条数 {countNums}</p> <p>新推广名称成功数 {newCampaignNums}</p> <p>新推广线索成功数 {newClueNums}</p> <p>推广线索重复数 {clueRepeatNums}</p> <div style={{marginBottom:'10px'}}>线索重复手机号 { repeatPhone && repeatPhone.map((item,index) => { return <p style={{marginBottom:'0px'}} key={index}>{item.join(',')}</p> }) }</div> <p style={{color: '#f00'}}>错误数量 {errorRows}</p> </div> </div> </Col> </Row> </Card> <div style={{display: spinning ? 'block' : 'none'}} className="spin-box"> <Spin className="spin" spinning={spinning} size="large" /> </div> </div> ) } }
spinning } = this.state;
random_line_split
Index.js
import React, {Component} from "react"; import "./Index.less"; import { Button, Card, message, LocaleProvider, Col, Select, Row, DatePicker, Upload, Divider, Spin } from "antd"; import BreadcrumbCustom from "../common/BreadcrumbCustom"; import zh_CN from "antd/lib/locale-provider/zh_CN"; import {getToken, splitArr} from "../../utils/filter"; import {exportClue} from "../../api/exportApi"; import {requestData} from "../../utils/importXlsx"; import {connect} from "../../utils/socket"; import {getSubjectList,getPlatformList} from '../../api/commonApi' const Option = Select.Option; const {RangePicker} = DatePicker; message.config({ top: 100, duration: 3, }); let customCondition = {}; let applyData = { size: 40, current: 1, ascs: [], descs: ['createTime'], condition: customCondition }; // let params = { // size: 100, // current: 1, // ascs: [], // descs: ['execTime'], // condition: { // "business": "VIPCOURSE" // } // }; export default class
extends Component { constructor(props) { super(props); this.state = { orderDate: null, subjectList: null, platformList: null, subjectValue: null, platformValue: null, newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: false }; } componentDidMount() { this.getSubjectListFn() this.getPlatformListFn() connect(getToken('username')); }; // 日期筛选 handleRangePicker = (rangePickerValue, dateString) => { this.setState({ orderDate: rangePickerValue }); applyData.current = 1; customCondition.startTime = parseInt(new Date(new Date(rangePickerValue[0]).toLocaleDateString()).getTime()/1000); customCondition.endTime = parseInt((new Date(new Date(rangePickerValue[1]).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)/1000); }; //所属学科onchange subjectChange = (value) => { console.log(value,'value') this.setState({ subjectValue: value }); customCondition.subject = value; }; //选择平台onchange platformChange = (value) => { this.setState({ platformValue: value }); customCondition.platform = value; }; // 搜索用户 searchUser = () => { if(!customCondition.startTime && !customCondition.endTime) { message.error('请选择时间') return } console.log(customCondition,'customCondition') exportClue(customCondition).then(res => { if(res.data.code === 0) { message.success('导出成功,请查收邮件') }else { message.error('导出失败') } }) // let params = `startTime=${customCondition.startTime}&endTime=${customCondition.endTime}` // if(customCondition.subject) { // params += `&subject=${customCondition.subject}` // } // // if(customCondition.platform) { // params += `&platform=${customCondition.platform}` // } // window.location.href = `${baseUrl()}/account/clue/export?${params}` }; // 上传课程缩略图icon uploadCourseThumbnail = (files) => { this.setState({ newCampaignNums: 0, countNums: 0, newClueNums: 0, clueRepeatNums: 0, repeatPhone: [], errorRows: 0, spinning: true }) let fileReader = new FileReader(); // 图片上传,读图片 let file = files.file; // 获取到上传的对象 let _this = this; fileReader.onload = (function (file) { requestData(file).then(res => { _this.setState({ spinning: false }) if(res.data.code === 0){ message.success('导入成功') const dataMsg = JSON.parse(res.data.msg) _this.setState({ newCampaignNums: dataMsg.newCampaignNums, countNums: dataMsg.countNums, newClueNums: dataMsg.newClueNums, clueRepeatNums: dataMsg.clueRepeatNums, repeatPhone: splitArr(dataMsg.repeatPhone, 8), errorRows: dataMsg.errorRows }) }else if(res.data.code === 1){ message.error(res.data.msg) }else { message.error('导入失败') } }); })(file); fileReader.readAsDataURL(file); // 读取完毕,显示到页面 }; //获取学科下拉列表 getSubjectListFn = ()=>{ getSubjectList().then(res=>{ if (res.data.code === 0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].id; obj.value = data[i].name; Arr.push(obj); } this.setState({ subjectList:Arr }) } }).catch(err=>{ console.log(err) }) }; //获取平台下拉列表 getPlatformListFn = ()=>{ getPlatformList().then(res=>{ if(res.data.code ===0){ let data = res.data.data; let Arr = []; for(let i=0;i<data.length;i++){ let obj = {}; obj.key = data[i].platform; obj.value = data[i].platformName; Arr.push(obj); } this.setState({ platformList:Arr }) } }).catch(err=>{ console.log(err) }) }; componentWillUnmount() { this.searchReset() } // 全部重置 searchReset = () => { this.setState({ searchValue: null, orderTypeValue: undefined, orderStatusValue: undefined, orderDate: undefined, platformValue:undefined, subjectValue:undefined }); customCondition.search = null; customCondition.startTime = null; customCondition.endTime = null; }; render() { const { subjectList, orderDate, platformList, newCampaignNums, countNums, newClueNums, clueRepeatNums, repeatPhone, errorRows, spinning } = this.state; const menus = [ { path: '/app/dashboard/analysis', name: '首页' }, { path: '#', name: '线索I/O' }, { path: '/app/export/index', name: '线索I/O' } ]; return ( <div className="ordercenter-order"> <div className="page-nav"> <BreadcrumbCustom paths={menus}/> <p className="title-style">线索I/O</p> </div> <Card bordered={false}> <Divider>导出功能</Divider> <Row className="my-user-search" gutter={16}> <Col sm={2} style={{textAlign: 'right', width: '65px', marginTop: '5px'}}> <span>日期:</span> </Col> <Col sm={6} style={{padding: 0}} id="order_status_select"> <LocaleProvider locale={zh_CN}> <RangePicker value={orderDate} onChange={this.handleRangePicker} style={{width: '100%'}} /> </LocaleProvider> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>所属学科:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.subjectChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {subjectList && subjectList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> <Col sm={4} style={{marginTop: '5px', width: '100px'}}> <span>选择平台:</span> </Col> <Col sm={4} style={{padding: 0, marginRight: '25px'}} id="refund_status_select"> <Select mode="multiple" showSearch placeholder='请选择' onChange={this.platformChange} style={{width: '100%'}} getPopupContainer={() => document.getElementById('refund_status_select')} > {platformList && platformList.map((value, index) => <Option key={index} value={value.key}>{value.value}</Option>)} </Select> </Col> </Row> <Row gutter={16} style={{marginBottom:'20px',marginTop: '20px'}}> <Col className="" sm={13} style={{marginBottom: '20px'}}> <Button type="primary" style={{marginRight: '12px', marginLeft: '10px'}} onClick={this.searchUser} disabled={this.state.disableBtn}>导出</Button> <Button type="primary" onClick={this.searchReset} disabled={this.state.resetBtn}>全部重置</Button> </Col> <Divider>导入功能</Divider> <Col className="" sm={13} style={{marginTop: '20px'}}> <Upload accept=".xls,.xlsx" customRequest={this.uploadCourseThumbnail} beforeUpload={this.beforeUploadCourseThumbnail} > <Button type="upload" style={{marginRight: '12px', marginLeft: '10px'}} disabled={this.state.disableBtn}>导入</Button> </Upload> <a href={require('./xls/template.xlsx')} download>下载导入模版</a> <div style={{marginLeft: '10px',marginTop:'10px'}}> <div className=""> <p>总解析条数 {countNums}</p> <p>新推广名称成功数 {newCampaignNums}</p> <p>新推广线索成功数 {newClueNums}</p> <p>推广线索重复数 {clueRepeatNums}</p> <div style={{marginBottom:'10px'}}>线索重复手机号 { repeatPhone && repeatPhone.map((item,index) => { return <p style={{marginBottom:'0px'}} key={index}>{item.join(',')}</p> }) }</div> <p style={{color: '#f00'}}>错误数量 {errorRows}</p> </div> </div> </Col> </Row> </Card> <div style={{display: spinning ? 'block' : 'none'}} className="spin-box"> <Spin className="spin" spinning={spinning} size="large" /> </div> </div> ) } }
exportList
identifier_name
csv_to_json.py
import os import sys import argparse import json import csv import collections import copy import ast import datetime import dateutil.parser def get_args(): """ Get arguments of the program :return: arguments parsed """ parser = argparse.ArgumentParser( "Create json file from csv by infering json'structure using a delimiter inside csv's columns." ) parser.add_argument("--csv", type=str, help='Set path to csv file as input') parser.add_argument("--json", type=str, help='Set path to json file as output') parser.add_argument("--delimiter", type=str, default='_', help='Set delimiter used to infer json\'s structure (default=\'_\')') parser.add_argument("--config", type=str, default=None, help='Set path to json file containing data type information and or default value(default=\'None\', optional and precise column type)') parser.add_argument("--cols_delimiter", type=str, default=',', help='Set delimiter of the csv (default=\',\')') parser.add_argument("--max_docs", type=int, default=-1, help='Set max number of documents in a json file, several will be created if necessary (default=\'-1\' means single output file)') parser.add_argument("--per_line", action='store_true', default=False, help='Dump a file containing one json per line. Careful the output is not a correct json (default=\'False\')') parser.add_argument("--infer_types", action='store_true', default=False, help='Infer data type based on its value: float, list and date are supported. Carefull, \'config\' will override it if specified. (default=\'False\')') parser.add_argument("--keep", action='store_true', default=False, help='Keep fields with empty values replaced by null instead of ignoring them (default=\'True\')') args = parser.parse_args() return args def
(x): """ Infer type of a string input. :param x: input as a string :return: return x cast to type infered or x itself if no type was infered """ str_to_types = [ast.literal_eval, int, float, lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'), str ] for f in str_to_types: try: return f(x) except (ValueError, SyntaxError, TypeError): pass return x def get_header_csv(csv_file, cols_delimiter): """ Get header of a csv :param csv_file: path to the csv file :param cols_delimiter: delimiter between columns :return: header of csv as a list of strings """ with open(csv_file, "r") as f: reader = csv.reader(f, delimiter=cols_delimiter) header_csv = next(reader) return header_csv def create_jstruct(jstruct, elem_struct, val): """ Create json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :return: json structure created (updated) """ if len(elem_struct) == 1: jstruct[elem_struct[0]] = val else: elem = elem_struct.pop(0) if elem not in jstruct: jstruct[elem] = {} jstruct[elem] = create_jstruct(jstruct[elem], elem_struct, val) return jstruct def create_json_structure(header_csv, delimiter): """ Create json structure :param header_csv: header_csv that contains the futur json's fields :param delimiter: delimiter of the nested json :return: json structure created """ # Sort header of csv to find the hierarchy easier header_csv.sort() jstruct = {} for elem in header_csv: elem_struct = elem.split(delimiter) jstruct.update(create_jstruct(jstruct, elem_struct, {})) return jstruct def update_jstruct(jstruct, elem_struct, val, keep): """ Update json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :param keep: if true write None values instead of skipping them :return: json structure updated """ if len(elem_struct) == 1: try: if val == '': val = None if val == None and not keep: del jstruct[elem_struct[0]] else: jstruct[elem_struct[0]] = val except: print(" [ERR] Can not associate value ", val, "to field", elem_struct[0]) jstruct[elem_struct[0]] = None pass else: elem = elem_struct.pop(0) jstruct[elem] = update_jstruct(jstruct[elem], elem_struct, val, keep) return jstruct def create_json_example(row, header_csv, jstruct, delimiter, keep, dic_types): """ Create one json from one example :param row: row of a csv corresponding to example :param header_csv: header of the csv :param jstruct: json structure already created :param delimiter: delimiter of the nested json :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :return: json structure updated """ for key in header_csv: key_struct = key.split(delimiter) if key in dic_types.keys(): # if no value indicated set to default if row[key] == '' and 'default' in dic_types[key].keys(): row[key] = dic_types[key]['default'] else: try: # Cast to indicated type row[key] = dic_types[key]['type'](row[key]) except: print(" [WARN] Can not parse ", row[key] , "to type", dic_types[key]['type']) jstruct.update(update_jstruct(jstruct, key_struct, row[key], keep)) return jstruct def create_json_from_csv(csv_file, delimiter, cols_delimiter, keep, dic_types, infer_types, max_docs, json_file, per_line): """ Create one json for a whole csv :param csv_file: path to csv file :param delimiter: delimiter of the nested json (delimiter inside a column) :param cols_delimiter: delimiter of the columns in the csv :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :param infer_types: if true, will try to infer_types of fields :param max_docs: max documents to dump per json :param json_file: path to output file wanted :param per_line: if true, write one json per line (specific format) :return: json content """ # Get header of csv header_csv = get_header_csv(csv_file, cols_delimiter) # Create structure of json print(' [INFO] Creating json\'s structure') jstruct = create_json_structure(header_csv, delimiter) print(jstruct) # Read csv line by line and create list of json print(' [INFO] Filling json') js_content = [] with open(csv_file, 'r') as f: reader = csv.DictReader(f, delimiter=cols_delimiter) i = 0 beg = True end = True # Prepare output file if dump in one file if max_docs == -1 and not per_line: beg = False end = False with open(json_file, 'w') as jsf: jsf.write('[\n') for row in reader: if infer_types: row = {x: infer_type(row[x]) for x in row} jexample = copy.deepcopy(jstruct) js_content.append(create_json_example(row, header_csv, jexample, delimiter, keep, dic_types)) i += 1 # Dump json in streaming if (max_docs == -1) and ((i % 10000) == 0): dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] elif (max_docs != -1) and (i % max_docs) == 0: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] # Dump last jsons if js_content: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, True) print(' [INFO] Json{} successfully created and dumped'.format('s' if (max_docs != -1) else '')) return def dump_json(json_file, json_doc, per_line, beg=True, end=True): """ Dump a json in one file :param json_file: path to output file wanted :param json_doc: json document :param per_line: if true, write one json per line (specific format) :param beg: Add opening array :param end: Add ending array """ with open(json_file, 'a') as jsf: if per_line: jsf.write( '\n'.join(json.dumps(i) for i in json_doc) + '\n' ) else: if beg: jsf.write('[\n') jsf.write( ',\n'.join(json.dumps(i) for i in json_doc) ) if end: jsf.write('\n]') else: jsf.write(',\n') def str_to_type(name_type): """ Get type from string :param name_type: string containing name_type :return: type or function to cast to specific type """ if name_type == 'float' or name_type == 'Float': return float if name_type == 'bool': return bool if name_type == 'int': return lambda x: int(float(x)) if name_type == 'list': return ast.literal_eval if name_type == 'date': return lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ') if name_type == 'str': return str return None def read_config(config): """ Read config file containing information of type and default values of fields :param config: path to config file :return: dictionary containing type and or default value for each field in the file """ dic_types = json.load(open(config, 'r')) to_remove = [] for attribute, value in dic_types.items(): ls_val = value.keys() if 'type' in ls_val: val = value['type'] value['type'] = str_to_type(val) none_type = False if not value['type']: none_type = True if not 'default' in ls_val and none_type: to_remove.append(attribute) value['type'] = val for to_rm in to_remove: print(' [WARN] Config for' , '\'' + to_rm + '\'', 'incorrect and ommitted: Type', '\'' + dic_types[to_rm]['type'] + '\'' , 'is not valid and no default value is indicated') del dic_types[to_rm] return dic_types def dump(json_file, json_doc, max_docs, per_line, suffix=None, beg=False, end=False): """ :param json_file: path to output file wanted :param json_doc: json document :param max_docs: max doc per file :param per_line: if true, write one json per line (specific format) :param suffix: Add opening array :param beg: Add opening array :param end: Add ending array """ # Dump json into one file if max_docs == -1: dump_json(json_file, json_doc, per_line, beg, end) # Dump json into several files else: base_name = '.'.join(json_file.split('.')[:-1]) dump_json(base_name + '_' + str(suffix) + '.json', json_doc, per_line, beg, end) def main(): """ Main function of the program """ # Load arguments args = get_args() assert os.path.exists(args.csv), ' [ERR] File' + os.path.exists(args.csv) +'does not exist' print(args) try: dir_name = os.path.dirname(args.json) os.mkdir(dir_name) print(' [INFO] Creating', dir_name, 'directory') except: print(' [INFO] Directory', dir_name, 'already exists. Data will be replaced') pass if args.config: assert os.path.exists(args.config), ' [ERR] File' + os.path.exists(args.config) +'does not exist' dic_types = read_config(args.config) else: dic_types = {} # Create json create_json_from_csv(args.csv, args.delimiter, args.cols_delimiter, args.keep, dic_types, args.infer_types, args.max_docs, args.json, args.per_line) return 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print(" [ERR] Uncaught error waiting for scripts to finish") print(e) raise
infer_type
identifier_name
csv_to_json.py
import os import sys import argparse import json import csv import collections import copy import ast import datetime import dateutil.parser def get_args(): """ Get arguments of the program :return: arguments parsed """ parser = argparse.ArgumentParser( "Create json file from csv by infering json'structure using a delimiter inside csv's columns." ) parser.add_argument("--csv", type=str, help='Set path to csv file as input') parser.add_argument("--json", type=str, help='Set path to json file as output') parser.add_argument("--delimiter", type=str, default='_', help='Set delimiter used to infer json\'s structure (default=\'_\')') parser.add_argument("--config", type=str, default=None, help='Set path to json file containing data type information and or default value(default=\'None\', optional and precise column type)') parser.add_argument("--cols_delimiter", type=str, default=',', help='Set delimiter of the csv (default=\',\')') parser.add_argument("--max_docs", type=int, default=-1, help='Set max number of documents in a json file, several will be created if necessary (default=\'-1\' means single output file)') parser.add_argument("--per_line", action='store_true', default=False, help='Dump a file containing one json per line. Careful the output is not a correct json (default=\'False\')') parser.add_argument("--infer_types", action='store_true', default=False, help='Infer data type based on its value: float, list and date are supported. Carefull, \'config\' will override it if specified. (default=\'False\')') parser.add_argument("--keep", action='store_true', default=False, help='Keep fields with empty values replaced by null instead of ignoring them (default=\'True\')') args = parser.parse_args() return args def infer_type(x): """ Infer type of a string input. :param x: input as a string :return: return x cast to type infered or x itself if no type was infered """ str_to_types = [ast.literal_eval, int, float, lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'), str ] for f in str_to_types: try: return f(x) except (ValueError, SyntaxError, TypeError): pass return x def get_header_csv(csv_file, cols_delimiter): """ Get header of a csv :param csv_file: path to the csv file :param cols_delimiter: delimiter between columns :return: header of csv as a list of strings """ with open(csv_file, "r") as f: reader = csv.reader(f, delimiter=cols_delimiter) header_csv = next(reader) return header_csv def create_jstruct(jstruct, elem_struct, val): """ Create json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :return: json structure created (updated) """ if len(elem_struct) == 1: jstruct[elem_struct[0]] = val else: elem = elem_struct.pop(0) if elem not in jstruct: jstruct[elem] = {} jstruct[elem] = create_jstruct(jstruct[elem], elem_struct, val) return jstruct def create_json_structure(header_csv, delimiter): """ Create json structure :param header_csv: header_csv that contains the futur json's fields :param delimiter: delimiter of the nested json :return: json structure created """ # Sort header of csv to find the hierarchy easier header_csv.sort() jstruct = {} for elem in header_csv: elem_struct = elem.split(delimiter) jstruct.update(create_jstruct(jstruct, elem_struct, {})) return jstruct def update_jstruct(jstruct, elem_struct, val, keep): """ Update json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :param keep: if true write None values instead of skipping them :return: json structure updated """ if len(elem_struct) == 1: try: if val == '': val = None if val == None and not keep: del jstruct[elem_struct[0]] else: jstruct[elem_struct[0]] = val except: print(" [ERR] Can not associate value ", val, "to field", elem_struct[0]) jstruct[elem_struct[0]] = None pass else: elem = elem_struct.pop(0) jstruct[elem] = update_jstruct(jstruct[elem], elem_struct, val, keep) return jstruct def create_json_example(row, header_csv, jstruct, delimiter, keep, dic_types): """ Create one json from one example :param row: row of a csv corresponding to example :param header_csv: header of the csv :param jstruct: json structure already created :param delimiter: delimiter of the nested json :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :return: json structure updated """ for key in header_csv: key_struct = key.split(delimiter) if key in dic_types.keys(): # if no value indicated set to default if row[key] == '' and 'default' in dic_types[key].keys(): row[key] = dic_types[key]['default'] else: try: # Cast to indicated type row[key] = dic_types[key]['type'](row[key]) except: print(" [WARN] Can not parse ", row[key] , "to type", dic_types[key]['type']) jstruct.update(update_jstruct(jstruct, key_struct, row[key], keep)) return jstruct def create_json_from_csv(csv_file, delimiter, cols_delimiter, keep, dic_types, infer_types, max_docs, json_file, per_line): """ Create one json for a whole csv :param csv_file: path to csv file :param delimiter: delimiter of the nested json (delimiter inside a column) :param cols_delimiter: delimiter of the columns in the csv :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :param infer_types: if true, will try to infer_types of fields :param max_docs: max documents to dump per json :param json_file: path to output file wanted :param per_line: if true, write one json per line (specific format) :return: json content """ # Get header of csv header_csv = get_header_csv(csv_file, cols_delimiter) # Create structure of json print(' [INFO] Creating json\'s structure') jstruct = create_json_structure(header_csv, delimiter) print(jstruct) # Read csv line by line and create list of json print(' [INFO] Filling json') js_content = [] with open(csv_file, 'r') as f: reader = csv.DictReader(f, delimiter=cols_delimiter) i = 0 beg = True end = True # Prepare output file if dump in one file if max_docs == -1 and not per_line: beg = False end = False with open(json_file, 'w') as jsf: jsf.write('[\n') for row in reader: if infer_types: row = {x: infer_type(row[x]) for x in row} jexample = copy.deepcopy(jstruct) js_content.append(create_json_example(row, header_csv, jexample, delimiter, keep, dic_types)) i += 1 # Dump json in streaming if (max_docs == -1) and ((i % 10000) == 0): dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] elif (max_docs != -1) and (i % max_docs) == 0: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] # Dump last jsons if js_content: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, True) print(' [INFO] Json{} successfully created and dumped'.format('s' if (max_docs != -1) else '')) return def dump_json(json_file, json_doc, per_line, beg=True, end=True): """ Dump a json in one file :param json_file: path to output file wanted :param json_doc: json document :param per_line: if true, write one json per line (specific format) :param beg: Add opening array :param end: Add ending array """ with open(json_file, 'a') as jsf: if per_line:
else: if beg: jsf.write('[\n') jsf.write( ',\n'.join(json.dumps(i) for i in json_doc) ) if end: jsf.write('\n]') else: jsf.write(',\n') def str_to_type(name_type): """ Get type from string :param name_type: string containing name_type :return: type or function to cast to specific type """ if name_type == 'float' or name_type == 'Float': return float if name_type == 'bool': return bool if name_type == 'int': return lambda x: int(float(x)) if name_type == 'list': return ast.literal_eval if name_type == 'date': return lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ') if name_type == 'str': return str return None def read_config(config): """ Read config file containing information of type and default values of fields :param config: path to config file :return: dictionary containing type and or default value for each field in the file """ dic_types = json.load(open(config, 'r')) to_remove = [] for attribute, value in dic_types.items(): ls_val = value.keys() if 'type' in ls_val: val = value['type'] value['type'] = str_to_type(val) none_type = False if not value['type']: none_type = True if not 'default' in ls_val and none_type: to_remove.append(attribute) value['type'] = val for to_rm in to_remove: print(' [WARN] Config for' , '\'' + to_rm + '\'', 'incorrect and ommitted: Type', '\'' + dic_types[to_rm]['type'] + '\'' , 'is not valid and no default value is indicated') del dic_types[to_rm] return dic_types def dump(json_file, json_doc, max_docs, per_line, suffix=None, beg=False, end=False): """ :param json_file: path to output file wanted :param json_doc: json document :param max_docs: max doc per file :param per_line: if true, write one json per line (specific format) :param suffix: Add opening array :param beg: Add opening array :param end: Add ending array """ # Dump json into one file if max_docs == -1: dump_json(json_file, json_doc, per_line, beg, end) # Dump json into several files else: base_name = '.'.join(json_file.split('.')[:-1]) dump_json(base_name + '_' + str(suffix) + '.json', json_doc, per_line, beg, end) def main(): """ Main function of the program """ # Load arguments args = get_args() assert os.path.exists(args.csv), ' [ERR] File' + os.path.exists(args.csv) +'does not exist' print(args) try: dir_name = os.path.dirname(args.json) os.mkdir(dir_name) print(' [INFO] Creating', dir_name, 'directory') except: print(' [INFO] Directory', dir_name, 'already exists. Data will be replaced') pass if args.config: assert os.path.exists(args.config), ' [ERR] File' + os.path.exists(args.config) +'does not exist' dic_types = read_config(args.config) else: dic_types = {} # Create json create_json_from_csv(args.csv, args.delimiter, args.cols_delimiter, args.keep, dic_types, args.infer_types, args.max_docs, args.json, args.per_line) return 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print(" [ERR] Uncaught error waiting for scripts to finish") print(e) raise
jsf.write( '\n'.join(json.dumps(i) for i in json_doc) + '\n' )
conditional_block
csv_to_json.py
import os import sys import argparse import json import csv import collections import copy import ast import datetime import dateutil.parser def get_args(): """ Get arguments of the program :return: arguments parsed """ parser = argparse.ArgumentParser( "Create json file from csv by infering json'structure using a delimiter inside csv's columns." ) parser.add_argument("--csv", type=str, help='Set path to csv file as input') parser.add_argument("--json", type=str, help='Set path to json file as output') parser.add_argument("--delimiter", type=str, default='_', help='Set delimiter used to infer json\'s structure (default=\'_\')') parser.add_argument("--config", type=str, default=None, help='Set path to json file containing data type information and or default value(default=\'None\', optional and precise column type)') parser.add_argument("--cols_delimiter", type=str, default=',', help='Set delimiter of the csv (default=\',\')') parser.add_argument("--max_docs", type=int, default=-1, help='Set max number of documents in a json file, several will be created if necessary (default=\'-1\' means single output file)') parser.add_argument("--per_line", action='store_true', default=False, help='Dump a file containing one json per line. Careful the output is not a correct json (default=\'False\')') parser.add_argument("--infer_types", action='store_true', default=False, help='Infer data type based on its value: float, list and date are supported. Carefull, \'config\' will override it if specified. (default=\'False\')') parser.add_argument("--keep", action='store_true', default=False, help='Keep fields with empty values replaced by null instead of ignoring them (default=\'True\')') args = parser.parse_args() return args def infer_type(x): """ Infer type of a string input. :param x: input as a string :return: return x cast to type infered or x itself if no type was infered """ str_to_types = [ast.literal_eval, int, float, lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'), str ] for f in str_to_types: try: return f(x) except (ValueError, SyntaxError, TypeError): pass return x def get_header_csv(csv_file, cols_delimiter): """ Get header of a csv :param csv_file: path to the csv file :param cols_delimiter: delimiter between columns :return: header of csv as a list of strings """ with open(csv_file, "r") as f: reader = csv.reader(f, delimiter=cols_delimiter) header_csv = next(reader) return header_csv def create_jstruct(jstruct, elem_struct, val): """ Create json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :return: json structure created (updated) """ if len(elem_struct) == 1: jstruct[elem_struct[0]] = val else: elem = elem_struct.pop(0) if elem not in jstruct: jstruct[elem] = {} jstruct[elem] = create_jstruct(jstruct[elem], elem_struct, val) return jstruct def create_json_structure(header_csv, delimiter): """ Create json structure :param header_csv: header_csv that contains the futur json's fields :param delimiter: delimiter of the nested json :return: json structure created """ # Sort header of csv to find the hierarchy easier header_csv.sort() jstruct = {} for elem in header_csv: elem_struct = elem.split(delimiter) jstruct.update(create_jstruct(jstruct, elem_struct, {})) return jstruct def update_jstruct(jstruct, elem_struct, val, keep): """ Update json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :param keep: if true write None values instead of skipping them :return: json structure updated """ if len(elem_struct) == 1: try: if val == '': val = None if val == None and not keep: del jstruct[elem_struct[0]] else: jstruct[elem_struct[0]] = val except: print(" [ERR] Can not associate value ", val, "to field", elem_struct[0]) jstruct[elem_struct[0]] = None pass else: elem = elem_struct.pop(0) jstruct[elem] = update_jstruct(jstruct[elem], elem_struct, val, keep) return jstruct def create_json_example(row, header_csv, jstruct, delimiter, keep, dic_types): """ Create one json from one example :param row: row of a csv corresponding to example :param header_csv: header of the csv :param jstruct: json structure already created :param delimiter: delimiter of the nested json :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :return: json structure updated """ for key in header_csv: key_struct = key.split(delimiter) if key in dic_types.keys(): # if no value indicated set to default if row[key] == '' and 'default' in dic_types[key].keys(): row[key] = dic_types[key]['default'] else: try: # Cast to indicated type row[key] = dic_types[key]['type'](row[key]) except: print(" [WARN] Can not parse ", row[key] , "to type", dic_types[key]['type']) jstruct.update(update_jstruct(jstruct, key_struct, row[key], keep)) return jstruct def create_json_from_csv(csv_file, delimiter, cols_delimiter, keep, dic_types, infer_types, max_docs, json_file, per_line):
def dump_json(json_file, json_doc, per_line, beg=True, end=True): """ Dump a json in one file :param json_file: path to output file wanted :param json_doc: json document :param per_line: if true, write one json per line (specific format) :param beg: Add opening array :param end: Add ending array """ with open(json_file, 'a') as jsf: if per_line: jsf.write( '\n'.join(json.dumps(i) for i in json_doc) + '\n' ) else: if beg: jsf.write('[\n') jsf.write( ',\n'.join(json.dumps(i) for i in json_doc) ) if end: jsf.write('\n]') else: jsf.write(',\n') def str_to_type(name_type): """ Get type from string :param name_type: string containing name_type :return: type or function to cast to specific type """ if name_type == 'float' or name_type == 'Float': return float if name_type == 'bool': return bool if name_type == 'int': return lambda x: int(float(x)) if name_type == 'list': return ast.literal_eval if name_type == 'date': return lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ') if name_type == 'str': return str return None def read_config(config): """ Read config file containing information of type and default values of fields :param config: path to config file :return: dictionary containing type and or default value for each field in the file """ dic_types = json.load(open(config, 'r')) to_remove = [] for attribute, value in dic_types.items(): ls_val = value.keys() if 'type' in ls_val: val = value['type'] value['type'] = str_to_type(val) none_type = False if not value['type']: none_type = True if not 'default' in ls_val and none_type: to_remove.append(attribute) value['type'] = val for to_rm in to_remove: print(' [WARN] Config for' , '\'' + to_rm + '\'', 'incorrect and ommitted: Type', '\'' + dic_types[to_rm]['type'] + '\'' , 'is not valid and no default value is indicated') del dic_types[to_rm] return dic_types def dump(json_file, json_doc, max_docs, per_line, suffix=None, beg=False, end=False): """ :param json_file: path to output file wanted :param json_doc: json document :param max_docs: max doc per file :param per_line: if true, write one json per line (specific format) :param suffix: Add opening array :param beg: Add opening array :param end: Add ending array """ # Dump json into one file if max_docs == -1: dump_json(json_file, json_doc, per_line, beg, end) # Dump json into several files else: base_name = '.'.join(json_file.split('.')[:-1]) dump_json(base_name + '_' + str(suffix) + '.json', json_doc, per_line, beg, end) def main(): """ Main function of the program """ # Load arguments args = get_args() assert os.path.exists(args.csv), ' [ERR] File' + os.path.exists(args.csv) +'does not exist' print(args) try: dir_name = os.path.dirname(args.json) os.mkdir(dir_name) print(' [INFO] Creating', dir_name, 'directory') except: print(' [INFO] Directory', dir_name, 'already exists. Data will be replaced') pass if args.config: assert os.path.exists(args.config), ' [ERR] File' + os.path.exists(args.config) +'does not exist' dic_types = read_config(args.config) else: dic_types = {} # Create json create_json_from_csv(args.csv, args.delimiter, args.cols_delimiter, args.keep, dic_types, args.infer_types, args.max_docs, args.json, args.per_line) return 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print(" [ERR] Uncaught error waiting for scripts to finish") print(e) raise
""" Create one json for a whole csv :param csv_file: path to csv file :param delimiter: delimiter of the nested json (delimiter inside a column) :param cols_delimiter: delimiter of the columns in the csv :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :param infer_types: if true, will try to infer_types of fields :param max_docs: max documents to dump per json :param json_file: path to output file wanted :param per_line: if true, write one json per line (specific format) :return: json content """ # Get header of csv header_csv = get_header_csv(csv_file, cols_delimiter) # Create structure of json print(' [INFO] Creating json\'s structure') jstruct = create_json_structure(header_csv, delimiter) print(jstruct) # Read csv line by line and create list of json print(' [INFO] Filling json') js_content = [] with open(csv_file, 'r') as f: reader = csv.DictReader(f, delimiter=cols_delimiter) i = 0 beg = True end = True # Prepare output file if dump in one file if max_docs == -1 and not per_line: beg = False end = False with open(json_file, 'w') as jsf: jsf.write('[\n') for row in reader: if infer_types: row = {x: infer_type(row[x]) for x in row} jexample = copy.deepcopy(jstruct) js_content.append(create_json_example(row, header_csv, jexample, delimiter, keep, dic_types)) i += 1 # Dump json in streaming if (max_docs == -1) and ((i % 10000) == 0): dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] elif (max_docs != -1) and (i % max_docs) == 0: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] # Dump last jsons if js_content: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, True) print(' [INFO] Json{} successfully created and dumped'.format('s' if (max_docs != -1) else '')) return
identifier_body
csv_to_json.py
import os import sys import argparse import json import csv import collections import copy import ast import datetime import dateutil.parser def get_args(): """ Get arguments of the program :return: arguments parsed """ parser = argparse.ArgumentParser( "Create json file from csv by infering json'structure using a delimiter inside csv's columns." ) parser.add_argument("--csv", type=str, help='Set path to csv file as input') parser.add_argument("--json", type=str, help='Set path to json file as output') parser.add_argument("--delimiter", type=str, default='_', help='Set delimiter used to infer json\'s structure (default=\'_\')') parser.add_argument("--config", type=str, default=None, help='Set path to json file containing data type information and or default value(default=\'None\', optional and precise column type)') parser.add_argument("--cols_delimiter", type=str, default=',', help='Set delimiter of the csv (default=\',\')') parser.add_argument("--max_docs", type=int, default=-1, help='Set max number of documents in a json file, several will be created if necessary (default=\'-1\' means single output file)') parser.add_argument("--per_line", action='store_true', default=False, help='Dump a file containing one json per line. Careful the output is not a correct json (default=\'False\')') parser.add_argument("--infer_types", action='store_true', default=False, help='Infer data type based on its value: float, list and date are supported. Carefull, \'config\' will override it if specified. (default=\'False\')') parser.add_argument("--keep", action='store_true', default=False, help='Keep fields with empty values replaced by null instead of ignoring them (default=\'True\')') args = parser.parse_args() return args def infer_type(x): """ Infer type of a string input. :param x: input as a string :return: return x cast to type infered or x itself if no type was infered """ str_to_types = [ast.literal_eval, int, float, lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'), str ] for f in str_to_types: try: return f(x) except (ValueError, SyntaxError, TypeError): pass return x def get_header_csv(csv_file, cols_delimiter): """ Get header of a csv :param csv_file: path to the csv file :param cols_delimiter: delimiter between columns :return: header of csv as a list of strings """ with open(csv_file, "r") as f: reader = csv.reader(f, delimiter=cols_delimiter) header_csv = next(reader) return header_csv def create_jstruct(jstruct, elem_struct, val): """ Create json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :return: json structure created (updated) """ if len(elem_struct) == 1: jstruct[elem_struct[0]] = val else: elem = elem_struct.pop(0) if elem not in jstruct: jstruct[elem] = {} jstruct[elem] = create_jstruct(jstruct[elem], elem_struct, val) return jstruct def create_json_structure(header_csv, delimiter): """ Create json structure :param header_csv: header_csv that contains the futur json's fields :param delimiter: delimiter of the nested json :return: json structure created """ # Sort header of csv to find the hierarchy easier header_csv.sort() jstruct = {} for elem in header_csv: elem_struct = elem.split(delimiter) jstruct.update(create_jstruct(jstruct, elem_struct, {})) return jstruct def update_jstruct(jstruct, elem_struct, val, keep): """ Update json structure (recursive function) :param jstruct: jstruct to update :param elem_struct: nested field represented as list :param val: value of the nested field :param keep: if true write None values instead of skipping them :return: json structure updated """ if len(elem_struct) == 1: try: if val == '': val = None if val == None and not keep: del jstruct[elem_struct[0]] else: jstruct[elem_struct[0]] = val except: print(" [ERR] Can not associate value ", val, "to field", elem_struct[0]) jstruct[elem_struct[0]] = None pass else: elem = elem_struct.pop(0) jstruct[elem] = update_jstruct(jstruct[elem], elem_struct, val, keep) return jstruct def create_json_example(row, header_csv, jstruct, delimiter, keep, dic_types): """ Create one json from one example :param row: row of a csv corresponding to example :param header_csv: header of the csv :param jstruct: json structure already created :param delimiter: delimiter of the nested json :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :return: json structure updated """ for key in header_csv: key_struct = key.split(delimiter) if key in dic_types.keys(): # if no value indicated set to default if row[key] == '' and 'default' in dic_types[key].keys(): row[key] = dic_types[key]['default'] else: try: # Cast to indicated type row[key] = dic_types[key]['type'](row[key]) except: print(" [WARN] Can not parse ", row[key] , "to type", dic_types[key]['type']) jstruct.update(update_jstruct(jstruct, key_struct, row[key], keep)) return jstruct def create_json_from_csv(csv_file, delimiter, cols_delimiter, keep, dic_types, infer_types, max_docs, json_file, per_line): """ Create one json for a whole csv :param csv_file: path to csv file :param delimiter: delimiter of the nested json (delimiter inside a column) :param cols_delimiter: delimiter of the columns in the csv :param keep: if true write None values instead of skipping them :param dic_types: dictionarry containing type and default value of each field :param infer_types: if true, will try to infer_types of fields :param max_docs: max documents to dump per json :param json_file: path to output file wanted :param per_line: if true, write one json per line (specific format) :return: json content """ # Get header of csv header_csv = get_header_csv(csv_file, cols_delimiter) # Create structure of json print(' [INFO] Creating json\'s structure') jstruct = create_json_structure(header_csv, delimiter) print(jstruct) # Read csv line by line and create list of json print(' [INFO] Filling json') js_content = [] with open(csv_file, 'r') as f: reader = csv.DictReader(f, delimiter=cols_delimiter) i = 0 beg = True end = True # Prepare output file if dump in one file if max_docs == -1 and not per_line: beg = False end = False with open(json_file, 'w') as jsf: jsf.write('[\n') for row in reader: if infer_types: row = {x: infer_type(row[x]) for x in row} jexample = copy.deepcopy(jstruct) js_content.append(create_json_example(row, header_csv, jexample, delimiter, keep, dic_types)) i += 1 # Dump json in streaming if (max_docs == -1) and ((i % 10000) == 0): dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] elif (max_docs != -1) and (i % max_docs) == 0: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, end) js_content = [] # Dump last jsons if js_content: dump(json_file, js_content, max_docs, per_line, i // max_docs, beg, True) print(' [INFO] Json{} successfully created and dumped'.format('s' if (max_docs != -1) else '')) return def dump_json(json_file, json_doc, per_line, beg=True, end=True): """ Dump a json in one file :param json_file: path to output file wanted :param json_doc: json document :param per_line: if true, write one json per line (specific format) :param beg: Add opening array :param end: Add ending array """ with open(json_file, 'a') as jsf: if per_line: jsf.write( '\n'.join(json.dumps(i) for i in json_doc) + '\n' ) else: if beg: jsf.write('[\n') jsf.write( ',\n'.join(json.dumps(i) for i in json_doc) ) if end: jsf.write('\n]') else: jsf.write(',\n') def str_to_type(name_type):
:param name_type: string containing name_type :return: type or function to cast to specific type """ if name_type == 'float' or name_type == 'Float': return float if name_type == 'bool': return bool if name_type == 'int': return lambda x: int(float(x)) if name_type == 'list': return ast.literal_eval if name_type == 'date': return lambda x: dateutil.parser.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ') if name_type == 'str': return str return None def read_config(config): """ Read config file containing information of type and default values of fields :param config: path to config file :return: dictionary containing type and or default value for each field in the file """ dic_types = json.load(open(config, 'r')) to_remove = [] for attribute, value in dic_types.items(): ls_val = value.keys() if 'type' in ls_val: val = value['type'] value['type'] = str_to_type(val) none_type = False if not value['type']: none_type = True if not 'default' in ls_val and none_type: to_remove.append(attribute) value['type'] = val for to_rm in to_remove: print(' [WARN] Config for' , '\'' + to_rm + '\'', 'incorrect and ommitted: Type', '\'' + dic_types[to_rm]['type'] + '\'' , 'is not valid and no default value is indicated') del dic_types[to_rm] return dic_types def dump(json_file, json_doc, max_docs, per_line, suffix=None, beg=False, end=False): """ :param json_file: path to output file wanted :param json_doc: json document :param max_docs: max doc per file :param per_line: if true, write one json per line (specific format) :param suffix: Add opening array :param beg: Add opening array :param end: Add ending array """ # Dump json into one file if max_docs == -1: dump_json(json_file, json_doc, per_line, beg, end) # Dump json into several files else: base_name = '.'.join(json_file.split('.')[:-1]) dump_json(base_name + '_' + str(suffix) + '.json', json_doc, per_line, beg, end) def main(): """ Main function of the program """ # Load arguments args = get_args() assert os.path.exists(args.csv), ' [ERR] File' + os.path.exists(args.csv) +'does not exist' print(args) try: dir_name = os.path.dirname(args.json) os.mkdir(dir_name) print(' [INFO] Creating', dir_name, 'directory') except: print(' [INFO] Directory', dir_name, 'already exists. Data will be replaced') pass if args.config: assert os.path.exists(args.config), ' [ERR] File' + os.path.exists(args.config) +'does not exist' dic_types = read_config(args.config) else: dic_types = {} # Create json create_json_from_csv(args.csv, args.delimiter, args.cols_delimiter, args.keep, dic_types, args.infer_types, args.max_docs, args.json, args.per_line) return 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print(" [ERR] Uncaught error waiting for scripts to finish") print(e) raise
""" Get type from string
random_line_split
proto_utils.go
/* * Copyright 2018- The Pixie Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 */ package controllers import ( "errors" "fmt" "github.com/gofrs/uuid" "github.com/gogo/protobuf/types" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "px.dev/pixie/src/api/proto/vizierpb" "px.dev/pixie/src/carnot/carnotpb" "px.dev/pixie/src/carnot/planner/compilerpb" "px.dev/pixie/src/carnot/planner/distributedpb" "px.dev/pixie/src/carnot/planner/plannerpb" "px.dev/pixie/src/carnot/planpb" "px.dev/pixie/src/carnot/queryresultspb" "px.dev/pixie/src/common/base/statuspb" "px.dev/pixie/src/shared/types/typespb" "px.dev/pixie/src/table_store/schemapb" "px.dev/pixie/src/utils" ) var dataTypeToVizierDataType = map[typespb.DataType]vizierpb.DataType{ typespb.DATA_TYPE_UNKNOWN: vizierpb.DATA_TYPE_UNKNOWN, typespb.BOOLEAN: vizierpb.BOOLEAN, typespb.INT64: vizierpb.INT64, typespb.UINT128: vizierpb.UINT128, typespb.FLOAT64: vizierpb.FLOAT64, typespb.STRING: vizierpb.STRING, typespb.TIME64NS: vizierpb.TIME64NS, } var semanticTypeToVizierSemanticType = map[typespb.SemanticType]vizierpb.SemanticType{ typespb.ST_UNSPECIFIED: vizierpb.ST_UNSPECIFIED, typespb.ST_NONE: vizierpb.ST_NONE, typespb.ST_TIME_NS: vizierpb.ST_TIME_NS, typespb.ST_AGENT_UID: vizierpb.ST_AGENT_UID, typespb.ST_ASID: vizierpb.ST_ASID, typespb.ST_UPID: vizierpb.ST_UPID, typespb.ST_SERVICE_NAME: vizierpb.ST_SERVICE_NAME, typespb.ST_POD_NAME: vizierpb.ST_POD_NAME, typespb.ST_POD_PHASE: vizierpb.ST_POD_PHASE, typespb.ST_POD_STATUS: vizierpb.ST_POD_STATUS, typespb.ST_NODE_NAME: vizierpb.ST_NODE_NAME, typespb.ST_CONTAINER_NAME: vizierpb.ST_CONTAINER_NAME, typespb.ST_CONTAINER_STATE: vizierpb.ST_CONTAINER_STATE, typespb.ST_CONTAINER_STATUS: vizierpb.ST_CONTAINER_STATUS, typespb.ST_NAMESPACE_NAME: vizierpb.ST_NAMESPACE_NAME, typespb.ST_BYTES: vizierpb.ST_BYTES, typespb.ST_PERCENT: vizierpb.ST_PERCENT, typespb.ST_DURATION_NS: vizierpb.ST_DURATION_NS, typespb.ST_THROUGHPUT_PER_NS: vizierpb.ST_THROUGHPUT_PER_NS, typespb.ST_THROUGHPUT_BYTES_PER_NS: vizierpb.ST_THROUGHPUT_BYTES_PER_NS, typespb.ST_QUANTILES: vizierpb.ST_QUANTILES, typespb.ST_DURATION_NS_QUANTILES: vizierpb.ST_DURATION_NS_QUANTILES, typespb.ST_IP_ADDRESS: vizierpb.ST_IP_ADDRESS, typespb.ST_PORT: vizierpb.ST_PORT, typespb.ST_HTTP_REQ_METHOD: vizierpb.ST_HTTP_REQ_METHOD, typespb.ST_HTTP_RESP_STATUS: vizierpb.ST_HTTP_RESP_STATUS, typespb.ST_HTTP_RESP_MESSAGE: vizierpb.ST_HTTP_RESP_MESSAGE, typespb.ST_SCRIPT_REFERENCE: vizierpb.ST_SCRIPT_REFERENCE, } // These codes are taken from https://godoc.org/google.golang.org/grpc/codes#Code. var statusCodeToGRPCCode = map[statuspb.Code]codes.Code{ statuspb.OK: codes.OK, statuspb.CANCELLED: codes.Canceled, statuspb.UNKNOWN: codes.Unknown, statuspb.INVALID_ARGUMENT: codes.InvalidArgument, statuspb.DEADLINE_EXCEEDED: codes.DeadlineExceeded, statuspb.NOT_FOUND: codes.NotFound, statuspb.ALREADY_EXISTS: codes.AlreadyExists, statuspb.PERMISSION_DENIED: codes.PermissionDenied, statuspb.UNAUTHENTICATED: codes.Unauthenticated, statuspb.INTERNAL: codes.Internal, statuspb.RESOURCE_UNAVAILABLE: codes.Unavailable, statuspb.SYSTEM: codes.Internal, } var lifeCycleStateToVizierLifeCycleStateMap = map[statuspb.LifeCycleState]vizierpb.LifeCycleState{ statuspb.UNKNOWN_STATE: vizierpb.UNKNOWN_STATE, statuspb.PENDING_STATE: vizierpb.PENDING_STATE, statuspb.RUNNING_STATE: vizierpb.RUNNING_STATE, statuspb.TERMINATED_STATE: vizierpb.TERMINATED_STATE, statuspb.FAILED_STATE: vizierpb.FAILED_STATE, } func convertLifeCycleStateToVizierLifeCycleState(state statuspb.LifeCycleState) vizierpb.LifeCycleState { if val, ok := lifeCycleStateToVizierLifeCycleStateMap[state]; ok { return val } return vizierpb.UNKNOWN_STATE } func convertExecFuncs(inputFuncs []*vizierpb.ExecuteScriptRequest_FuncToExecute) []*plannerpb.FuncToExecute { funcs := make([]*plannerpb.FuncToExecute, len(inputFuncs)) for i, f := range inputFuncs { args := make([]*plannerpb.FuncToExecute_ArgValue, len(f.ArgValues)) for j, arg := range f.ArgValues { args[j] = &plannerpb.FuncToExecute_ArgValue{ Name: arg.Name, Value: arg.Value, } } funcs[i] = &plannerpb.FuncToExecute{ FuncName: f.FuncName, ArgValues: args, OutputTablePrefix: f.OutputTablePrefix, } } return funcs } func convertConfigs(config *vizierpb.Configs) *plannerpb.Configs { if config == nil { return nil } c := &plannerpb.Configs{} if config.OTelEndpointConfig != nil { c.OTelEndpointConfig = &plannerpb.Configs_OTelEndpointConfig{ URL: config.OTelEndpointConfig.URL, Headers: config.OTelEndpointConfig.Headers, Insecure: config.OTelEndpointConfig.Insecure, Timeout: config.OTelEndpointConfig.Timeout, } } if config.PluginConfig != nil { c.PluginConfig = &plannerpb.Configs_PluginConfig{ StartTimeNs: config.PluginConfig.StartTimeNs, EndTimeNs: config.PluginConfig.EndTimeNs, } } return c } // VizierQueryRequestToPlannerMutationRequest maps request to mutation. func VizierQueryRequestToPlannerMutationRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.CompileMutationsRequest, error) { return &plannerpb.CompileMutationsRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // VizierQueryRequestToPlannerQueryRequest converts a externally-facing query request to an internal representation. func VizierQueryRequestToPlannerQueryRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.QueryRequest, error) { return &plannerpb.QueryRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // ErrToVizierResponse converts an error to an externally-facing Vizier response message func ErrToVizierResponse(id uuid.UUID, err error) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: ErrToVizierStatus(err), } } // ErrToVizierStatus converts an error to an externally-facing Vizier status. func ErrToVizierStatus(err error) *vizierpb.Status { s, ok := status.FromError(err) if ok { return &vizierpb.Status{ Code: int32(s.Code()), Message: s.Message(), } } return &vizierpb.Status{ Code: int32(codes.Unknown), Message: err.Error(), } } // StatusToVizierResponse converts an error to an externally-facing Vizier response message func StatusToVizierResponse(id uuid.UUID, s *statuspb.Status) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: StatusToVizierStatus(s), } } // StatusToVizierStatus converts an internal status to an externally-facing Vizier status. func StatusToVizierStatus(s *statuspb.Status) *vizierpb.Status { return &vizierpb.Status{ Code: int32(statusCodeToGRPCCode[s.ErrCode]), Message: s.Msg, ErrorDetails: getErrorsFromStatusContext(s.Context), } } func getErrorsFromStatusContext(ctx *types.Any) []*vizierpb.ErrorDetails { errorPB := &compilerpb.CompilerErrorGroup{} if !types.Is(ctx, errorPB) { return nil } err := types.UnmarshalAny(ctx, errorPB) if err != nil { return nil } errors := make([]*vizierpb.ErrorDetails, len(errorPB.Errors)) for i, e := range errorPB.Errors { lcErr := e.GetLineColError() errors[i] = &vizierpb.ErrorDetails{ Error: &vizierpb.ErrorDetails_CompilerError{ CompilerError: &vizierpb.CompilerError{ Line: lcErr.Line, Column: lcErr.Column, Message: lcErr.Message, }, }, } } return errors } // RelationFromTable gets the relation from the table. func RelationFromTable(table *schemapb.Table) (*vizierpb.QueryMetadata, error) { cols := make([]*vizierpb.Relation_ColumnInfo, len(table.Relation.Columns)) for i, c := range table.Relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols[i] = newCol } return &vizierpb.QueryMetadata{ Relation: &vizierpb.Relation{ Columns: cols, }, Name: table.Name, }, nil } // QueryResultStatsToVizierStats gets the execution stats from the query results. func QueryResultStatsToVizierStats(e *queryresultspb.QueryExecutionStats, compilationTimeNs int64) *vizierpb.QueryExecutionStats { return &vizierpb.QueryExecutionStats{ Timing: &vizierpb.QueryTimingInfo{ ExecutionTimeNs: e.Timing.ExecutionTimeNs, CompilationTimeNs: compilationTimeNs, }, BytesProcessed: e.BytesProcessed, RecordsProcessed: e.RecordsProcessed, } } // UInt128ToVizierUInt128 converts our internal representation of UInt128 to Vizier's representation of UInt128. func UInt128ToVizierUInt128(i *typespb.UInt128) *vizierpb.UInt128 { return &vizierpb.UInt128{ Low: i.Low, High: i.High, } } func colToVizierCol(col *schemapb.Column) (*vizierpb.Column, error) { switch c := col.ColData.(type) { case *schemapb.Column_BooleanData: return &vizierpb.Column{ ColData: &vizierpb.Column_BooleanData{ BooleanData: &vizierpb.BooleanColumn{ Data: c.BooleanData.Data, }, }, }, nil case *schemapb.Column_Int64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Int64Data{ Int64Data: &vizierpb.Int64Column{ Data: c.Int64Data.Data, }, }, }, nil case *schemapb.Column_Uint128Data: b := make([]*vizierpb.UInt128, len(c.Uint128Data.Data)) for i, s := range c.Uint128Data.Data { b[i] = UInt128ToVizierUInt128(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_Uint128Data{ Uint128Data: &vizierpb.UInt128Column{ Data: b, }, }, }, nil case *schemapb.Column_Time64NsData: return &vizierpb.Column{ ColData: &vizierpb.Column_Time64NsData{ Time64NsData: &vizierpb.Time64NSColumn{ Data: c.Time64NsData.Data, }, }, }, nil case *schemapb.Column_Float64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Float64Data{ Float64Data: &vizierpb.Float64Column{ Data: c.Float64Data.Data, }, }, }, nil case *schemapb.Column_StringData: b := make([][]byte, len(c.StringData.Data)) for i, s := range c.StringData.Data { b[i] = []byte(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: b, }, }, }, nil default: return nil, errors.New("Could not get column type") } } // RowBatchToVizierRowBatch converts an internal row batch to a vizier row batch. func RowBatchToVizierRowBatch(rb *schemapb.RowBatchData, tableID string) (*vizierpb.RowBatchData, error) { cols := make([]*vizierpb.Column, len(rb.Cols)) for i, col := range rb.Cols { c, err := colToVizierCol(col) if err != nil { return nil, err } cols[i] = c } return &vizierpb.RowBatchData{ TableID: tableID, NumRows: rb.NumRows, Eow: rb.Eow, Eos: rb.Eos, Cols: cols, }, nil } // BuildExecuteScriptResponse Converts the agent-format result into the vizier client format result. func BuildExecuteScriptResponse(r *carnotpb.TransferResultChunkRequest, // Map of the received table names to their table ID on the output proto. tableIDMap map[string]string, compilationTimeNs int64) (*vizierpb.ExecuteScriptResponse, error) { res := &vizierpb.ExecuteScriptResponse{ QueryID: utils.UUIDFromProtoOrNil(r.QueryID).String(), } if execStats := r.GetExecutionAndTimingInfo(); execStats != nil { stats := QueryResultStatsToVizierStats(execStats.ExecutionStats, compilationTimeNs) res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ ExecutionStats: stats, }, } return res, nil } // This agent message type will not turn into a message on the client stream. if initConn := r.GetInitiateConn(); initConn != nil { return nil, nil } if queryResult := r.GetQueryResult(); queryResult != nil { tableName := queryResult.GetTableName() tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("table %s does not have an ID in the table ID map", tableName) } if queryResult.GetRowBatch() == nil { return nil, fmt.Errorf("BuildExecuteScriptResponse expected a non-nil row batch") } batch, err := RowBatchToVizierRowBatch(queryResult.GetRowBatch(), tableID) if err != nil { return nil, err } res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, } return res, nil } if execError := r.GetExecutionError(); execError != nil { res.Status = StatusToVizierStatus(execError) return res, nil } return nil, fmt.Errorf("error in ForwardQueryResult: Expected TransferResultChunkRequest to have init message, row batch, exec stats, exec error") } // QueryPlanResponse returns the query plan as an ExecuteScriptResponse. func QueryPlanResponse(queryID uuid.UUID, plan *distributedpb.DistributedPlan, planMap map[uuid.UUID]*planpb.Plan, agentStats *[]*queryresultspb.AgentExecutionStats, planTableID string, maxQueryPlanStringSizeBytes int) ([]*vizierpb.ExecuteScriptResponse, error) { queryPlan, err := GetQueryPlanAsDotString(plan, planMap, agentStats) if err != nil { log.WithError(err).Error("error with query plan") return nil, err } var resp []*vizierpb.ExecuteScriptResponse // We can't overwhelm NATS with a query plan greater than 1MB. for i := 0; i < len(queryPlan); i += maxQueryPlanStringSizeBytes { end := i + maxQueryPlanStringSizeBytes if end > len(queryPlan) { end = len(queryPlan) } last := end == len(queryPlan) batch := &vizierpb.RowBatchData{ TableID: planTableID, Cols: []*vizierpb.Column{ { ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: [][]byte{[]byte(queryPlan[i:end])}, }, }, }, }, NumRows: 1, Eos: last, Eow: last, } resp = append(resp, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, }, }) } return resp, nil } // QueryPlanRelationResponse returns the relation of the query plan as an ExecuteScriptResponse. func QueryPlanRelationResponse(queryID uuid.UUID, planTableID string) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: "__query_plan__", ID: planTableID, Relation: &vizierpb.Relation{ Columns: []*vizierpb.Relation_ColumnInfo{ { ColumnName: "query_plan", ColumnType: vizierpb.STRING, ColumnDesc: "The query plan", }, }, }, }, }, } } // OutputSchemaFromPlan takes in a plan map and returns the relations for all of the final output // tables in the plan map. func OutputSchemaFromPlan(planMap map[uuid.UUID]*planpb.Plan) map[string]*schemapb.Relation { outputRelations := make(map[string]*schemapb.Relation) for _, plan := range planMap { for _, fragment := range plan.Nodes
} return outputRelations } // AgentRelationToVizierRelation converts the agent relation format to the Vizier relation format. func AgentRelationToVizierRelation(relation *schemapb.Relation) *vizierpb.Relation { var cols []*vizierpb.Relation_ColumnInfo for _, c := range relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols = append(cols, newCol) } return &vizierpb.Relation{ Columns: cols, } } // TableRelationResponses returns the query metadata table schemas as ExecuteScriptResponses. func TableRelationResponses(queryID uuid.UUID, tableIDMap map[string]string, planMap map[uuid.UUID]*planpb.Plan) ([]*vizierpb.ExecuteScriptResponse, error) { var results []*vizierpb.ExecuteScriptResponse schemas := OutputSchemaFromPlan(planMap) for tableName, schema := range schemas { tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("Table ID for table name %s not found in table map", tableName) } convertedRelation := AgentRelationToVizierRelation(schema) results = append(results, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: tableName, ID: tableID, Relation: convertedRelation, }, }, }) } return results, nil } // StatusToError converts a statuspb.Status to a grpc error. func StatusToError(s *statuspb.Status) error { return status.Error(codes.Code(int32(s.ErrCode)), s.Msg) } // VizierStatusToError converts a vizierpb.Status to a grpc error. func VizierStatusToError(s *vizierpb.Status) error { return status.Error(codes.Code(s.Code), s.Message) }
{ for _, node := range fragment.Nodes { if node.Op.OpType == planpb.GRPC_SINK_OPERATOR { grpcSink := node.Op.GetGRPCSinkOp() outputTableInfo := grpcSink.GetOutputTable() if outputTableInfo == nil { continue } relation := &schemapb.Relation{ Columns: []*schemapb.Relation_ColumnInfo{}, } for i, colName := range outputTableInfo.ColumnNames { relation.Columns = append(relation.Columns, &schemapb.Relation_ColumnInfo{ ColumnName: colName, ColumnType: outputTableInfo.ColumnTypes[i], ColumnSemanticType: outputTableInfo.ColumnSemanticTypes[i], }) } outputRelations[outputTableInfo.TableName] = relation } } }
conditional_block
proto_utils.go
/* * Copyright 2018- The Pixie Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 */ package controllers import ( "errors" "fmt" "github.com/gofrs/uuid" "github.com/gogo/protobuf/types" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "px.dev/pixie/src/api/proto/vizierpb" "px.dev/pixie/src/carnot/carnotpb" "px.dev/pixie/src/carnot/planner/compilerpb" "px.dev/pixie/src/carnot/planner/distributedpb" "px.dev/pixie/src/carnot/planner/plannerpb" "px.dev/pixie/src/carnot/planpb" "px.dev/pixie/src/carnot/queryresultspb" "px.dev/pixie/src/common/base/statuspb" "px.dev/pixie/src/shared/types/typespb" "px.dev/pixie/src/table_store/schemapb" "px.dev/pixie/src/utils" ) var dataTypeToVizierDataType = map[typespb.DataType]vizierpb.DataType{ typespb.DATA_TYPE_UNKNOWN: vizierpb.DATA_TYPE_UNKNOWN, typespb.BOOLEAN: vizierpb.BOOLEAN, typespb.INT64: vizierpb.INT64, typespb.UINT128: vizierpb.UINT128, typespb.FLOAT64: vizierpb.FLOAT64, typespb.STRING: vizierpb.STRING, typespb.TIME64NS: vizierpb.TIME64NS, } var semanticTypeToVizierSemanticType = map[typespb.SemanticType]vizierpb.SemanticType{ typespb.ST_UNSPECIFIED: vizierpb.ST_UNSPECIFIED, typespb.ST_NONE: vizierpb.ST_NONE, typespb.ST_TIME_NS: vizierpb.ST_TIME_NS, typespb.ST_AGENT_UID: vizierpb.ST_AGENT_UID, typespb.ST_ASID: vizierpb.ST_ASID, typespb.ST_UPID: vizierpb.ST_UPID, typespb.ST_SERVICE_NAME: vizierpb.ST_SERVICE_NAME, typespb.ST_POD_NAME: vizierpb.ST_POD_NAME, typespb.ST_POD_PHASE: vizierpb.ST_POD_PHASE, typespb.ST_POD_STATUS: vizierpb.ST_POD_STATUS, typespb.ST_NODE_NAME: vizierpb.ST_NODE_NAME, typespb.ST_CONTAINER_NAME: vizierpb.ST_CONTAINER_NAME, typespb.ST_CONTAINER_STATE: vizierpb.ST_CONTAINER_STATE, typespb.ST_CONTAINER_STATUS: vizierpb.ST_CONTAINER_STATUS, typespb.ST_NAMESPACE_NAME: vizierpb.ST_NAMESPACE_NAME, typespb.ST_BYTES: vizierpb.ST_BYTES, typespb.ST_PERCENT: vizierpb.ST_PERCENT, typespb.ST_DURATION_NS: vizierpb.ST_DURATION_NS, typespb.ST_THROUGHPUT_PER_NS: vizierpb.ST_THROUGHPUT_PER_NS, typespb.ST_THROUGHPUT_BYTES_PER_NS: vizierpb.ST_THROUGHPUT_BYTES_PER_NS, typespb.ST_QUANTILES: vizierpb.ST_QUANTILES, typespb.ST_DURATION_NS_QUANTILES: vizierpb.ST_DURATION_NS_QUANTILES, typespb.ST_IP_ADDRESS: vizierpb.ST_IP_ADDRESS, typespb.ST_PORT: vizierpb.ST_PORT, typespb.ST_HTTP_REQ_METHOD: vizierpb.ST_HTTP_REQ_METHOD, typespb.ST_HTTP_RESP_STATUS: vizierpb.ST_HTTP_RESP_STATUS, typespb.ST_HTTP_RESP_MESSAGE: vizierpb.ST_HTTP_RESP_MESSAGE, typespb.ST_SCRIPT_REFERENCE: vizierpb.ST_SCRIPT_REFERENCE, } // These codes are taken from https://godoc.org/google.golang.org/grpc/codes#Code. var statusCodeToGRPCCode = map[statuspb.Code]codes.Code{ statuspb.OK: codes.OK, statuspb.CANCELLED: codes.Canceled, statuspb.UNKNOWN: codes.Unknown, statuspb.INVALID_ARGUMENT: codes.InvalidArgument, statuspb.DEADLINE_EXCEEDED: codes.DeadlineExceeded, statuspb.NOT_FOUND: codes.NotFound, statuspb.ALREADY_EXISTS: codes.AlreadyExists, statuspb.PERMISSION_DENIED: codes.PermissionDenied, statuspb.UNAUTHENTICATED: codes.Unauthenticated, statuspb.INTERNAL: codes.Internal, statuspb.RESOURCE_UNAVAILABLE: codes.Unavailable, statuspb.SYSTEM: codes.Internal, } var lifeCycleStateToVizierLifeCycleStateMap = map[statuspb.LifeCycleState]vizierpb.LifeCycleState{ statuspb.UNKNOWN_STATE: vizierpb.UNKNOWN_STATE, statuspb.PENDING_STATE: vizierpb.PENDING_STATE, statuspb.RUNNING_STATE: vizierpb.RUNNING_STATE, statuspb.TERMINATED_STATE: vizierpb.TERMINATED_STATE, statuspb.FAILED_STATE: vizierpb.FAILED_STATE, } func convertLifeCycleStateToVizierLifeCycleState(state statuspb.LifeCycleState) vizierpb.LifeCycleState { if val, ok := lifeCycleStateToVizierLifeCycleStateMap[state]; ok { return val } return vizierpb.UNKNOWN_STATE } func convertExecFuncs(inputFuncs []*vizierpb.ExecuteScriptRequest_FuncToExecute) []*plannerpb.FuncToExecute { funcs := make([]*plannerpb.FuncToExecute, len(inputFuncs)) for i, f := range inputFuncs { args := make([]*plannerpb.FuncToExecute_ArgValue, len(f.ArgValues)) for j, arg := range f.ArgValues { args[j] = &plannerpb.FuncToExecute_ArgValue{ Name: arg.Name, Value: arg.Value, } } funcs[i] = &plannerpb.FuncToExecute{ FuncName: f.FuncName, ArgValues: args, OutputTablePrefix: f.OutputTablePrefix, } } return funcs } func convertConfigs(config *vizierpb.Configs) *plannerpb.Configs { if config == nil { return nil } c := &plannerpb.Configs{} if config.OTelEndpointConfig != nil { c.OTelEndpointConfig = &plannerpb.Configs_OTelEndpointConfig{ URL: config.OTelEndpointConfig.URL, Headers: config.OTelEndpointConfig.Headers, Insecure: config.OTelEndpointConfig.Insecure, Timeout: config.OTelEndpointConfig.Timeout, } } if config.PluginConfig != nil { c.PluginConfig = &plannerpb.Configs_PluginConfig{ StartTimeNs: config.PluginConfig.StartTimeNs, EndTimeNs: config.PluginConfig.EndTimeNs, } } return c } // VizierQueryRequestToPlannerMutationRequest maps request to mutation. func VizierQueryRequestToPlannerMutationRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.CompileMutationsRequest, error) { return &plannerpb.CompileMutationsRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // VizierQueryRequestToPlannerQueryRequest converts a externally-facing query request to an internal representation. func VizierQueryRequestToPlannerQueryRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.QueryRequest, error) { return &plannerpb.QueryRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // ErrToVizierResponse converts an error to an externally-facing Vizier response message func ErrToVizierResponse(id uuid.UUID, err error) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: ErrToVizierStatus(err), } } // ErrToVizierStatus converts an error to an externally-facing Vizier status. func ErrToVizierStatus(err error) *vizierpb.Status { s, ok := status.FromError(err) if ok { return &vizierpb.Status{ Code: int32(s.Code()), Message: s.Message(), } } return &vizierpb.Status{ Code: int32(codes.Unknown), Message: err.Error(), } } // StatusToVizierResponse converts an error to an externally-facing Vizier response message func StatusToVizierResponse(id uuid.UUID, s *statuspb.Status) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: StatusToVizierStatus(s), } } // StatusToVizierStatus converts an internal status to an externally-facing Vizier status. func StatusToVizierStatus(s *statuspb.Status) *vizierpb.Status { return &vizierpb.Status{ Code: int32(statusCodeToGRPCCode[s.ErrCode]), Message: s.Msg, ErrorDetails: getErrorsFromStatusContext(s.Context), } } func getErrorsFromStatusContext(ctx *types.Any) []*vizierpb.ErrorDetails { errorPB := &compilerpb.CompilerErrorGroup{} if !types.Is(ctx, errorPB) { return nil } err := types.UnmarshalAny(ctx, errorPB) if err != nil { return nil } errors := make([]*vizierpb.ErrorDetails, len(errorPB.Errors)) for i, e := range errorPB.Errors { lcErr := e.GetLineColError() errors[i] = &vizierpb.ErrorDetails{ Error: &vizierpb.ErrorDetails_CompilerError{ CompilerError: &vizierpb.CompilerError{ Line: lcErr.Line, Column: lcErr.Column, Message: lcErr.Message, }, }, } } return errors } // RelationFromTable gets the relation from the table. func RelationFromTable(table *schemapb.Table) (*vizierpb.QueryMetadata, error) { cols := make([]*vizierpb.Relation_ColumnInfo, len(table.Relation.Columns)) for i, c := range table.Relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols[i] = newCol } return &vizierpb.QueryMetadata{ Relation: &vizierpb.Relation{ Columns: cols, }, Name: table.Name, }, nil } // QueryResultStatsToVizierStats gets the execution stats from the query results. func QueryResultStatsToVizierStats(e *queryresultspb.QueryExecutionStats, compilationTimeNs int64) *vizierpb.QueryExecutionStats { return &vizierpb.QueryExecutionStats{ Timing: &vizierpb.QueryTimingInfo{ ExecutionTimeNs: e.Timing.ExecutionTimeNs, CompilationTimeNs: compilationTimeNs, }, BytesProcessed: e.BytesProcessed, RecordsProcessed: e.RecordsProcessed, } } // UInt128ToVizierUInt128 converts our internal representation of UInt128 to Vizier's representation of UInt128. func UInt128ToVizierUInt128(i *typespb.UInt128) *vizierpb.UInt128 { return &vizierpb.UInt128{ Low: i.Low, High: i.High, } } func colToVizierCol(col *schemapb.Column) (*vizierpb.Column, error) { switch c := col.ColData.(type) { case *schemapb.Column_BooleanData: return &vizierpb.Column{ ColData: &vizierpb.Column_BooleanData{ BooleanData: &vizierpb.BooleanColumn{ Data: c.BooleanData.Data, }, }, }, nil case *schemapb.Column_Int64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Int64Data{ Int64Data: &vizierpb.Int64Column{ Data: c.Int64Data.Data, }, }, }, nil case *schemapb.Column_Uint128Data: b := make([]*vizierpb.UInt128, len(c.Uint128Data.Data)) for i, s := range c.Uint128Data.Data { b[i] = UInt128ToVizierUInt128(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_Uint128Data{ Uint128Data: &vizierpb.UInt128Column{ Data: b, }, }, }, nil case *schemapb.Column_Time64NsData: return &vizierpb.Column{ ColData: &vizierpb.Column_Time64NsData{ Time64NsData: &vizierpb.Time64NSColumn{ Data: c.Time64NsData.Data, }, }, }, nil case *schemapb.Column_Float64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Float64Data{ Float64Data: &vizierpb.Float64Column{ Data: c.Float64Data.Data, }, }, }, nil case *schemapb.Column_StringData: b := make([][]byte, len(c.StringData.Data)) for i, s := range c.StringData.Data { b[i] = []byte(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: b, }, }, }, nil default: return nil, errors.New("Could not get column type") } } // RowBatchToVizierRowBatch converts an internal row batch to a vizier row batch. func RowBatchToVizierRowBatch(rb *schemapb.RowBatchData, tableID string) (*vizierpb.RowBatchData, error) { cols := make([]*vizierpb.Column, len(rb.Cols)) for i, col := range rb.Cols { c, err := colToVizierCol(col) if err != nil { return nil, err } cols[i] = c } return &vizierpb.RowBatchData{ TableID: tableID, NumRows: rb.NumRows, Eow: rb.Eow, Eos: rb.Eos, Cols: cols, }, nil } // BuildExecuteScriptResponse Converts the agent-format result into the vizier client format result. func BuildExecuteScriptResponse(r *carnotpb.TransferResultChunkRequest, // Map of the received table names to their table ID on the output proto. tableIDMap map[string]string, compilationTimeNs int64) (*vizierpb.ExecuteScriptResponse, error) { res := &vizierpb.ExecuteScriptResponse{ QueryID: utils.UUIDFromProtoOrNil(r.QueryID).String(), } if execStats := r.GetExecutionAndTimingInfo(); execStats != nil { stats := QueryResultStatsToVizierStats(execStats.ExecutionStats, compilationTimeNs) res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ ExecutionStats: stats, }, } return res, nil } // This agent message type will not turn into a message on the client stream. if initConn := r.GetInitiateConn(); initConn != nil { return nil, nil } if queryResult := r.GetQueryResult(); queryResult != nil { tableName := queryResult.GetTableName() tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("table %s does not have an ID in the table ID map", tableName) } if queryResult.GetRowBatch() == nil { return nil, fmt.Errorf("BuildExecuteScriptResponse expected a non-nil row batch") } batch, err := RowBatchToVizierRowBatch(queryResult.GetRowBatch(), tableID) if err != nil { return nil, err } res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, } return res, nil } if execError := r.GetExecutionError(); execError != nil { res.Status = StatusToVizierStatus(execError) return res, nil } return nil, fmt.Errorf("error in ForwardQueryResult: Expected TransferResultChunkRequest to have init message, row batch, exec stats, exec error") } // QueryPlanResponse returns the query plan as an ExecuteScriptResponse. func QueryPlanResponse(queryID uuid.UUID, plan *distributedpb.DistributedPlan, planMap map[uuid.UUID]*planpb.Plan, agentStats *[]*queryresultspb.AgentExecutionStats, planTableID string, maxQueryPlanStringSizeBytes int) ([]*vizierpb.ExecuteScriptResponse, error) { queryPlan, err := GetQueryPlanAsDotString(plan, planMap, agentStats) if err != nil { log.WithError(err).Error("error with query plan") return nil, err } var resp []*vizierpb.ExecuteScriptResponse // We can't overwhelm NATS with a query plan greater than 1MB. for i := 0; i < len(queryPlan); i += maxQueryPlanStringSizeBytes { end := i + maxQueryPlanStringSizeBytes if end > len(queryPlan) { end = len(queryPlan) } last := end == len(queryPlan) batch := &vizierpb.RowBatchData{ TableID: planTableID, Cols: []*vizierpb.Column{ { ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: [][]byte{[]byte(queryPlan[i:end])}, }, }, }, }, NumRows: 1, Eos: last, Eow: last, } resp = append(resp, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, }, }) } return resp, nil } // QueryPlanRelationResponse returns the relation of the query plan as an ExecuteScriptResponse. func QueryPlanRelationResponse(queryID uuid.UUID, planTableID string) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: "__query_plan__", ID: planTableID, Relation: &vizierpb.Relation{ Columns: []*vizierpb.Relation_ColumnInfo{ { ColumnName: "query_plan", ColumnType: vizierpb.STRING, ColumnDesc: "The query plan", }, }, }, }, }, } } // OutputSchemaFromPlan takes in a plan map and returns the relations for all of the final output // tables in the plan map. func OutputSchemaFromPlan(planMap map[uuid.UUID]*planpb.Plan) map[string]*schemapb.Relation { outputRelations := make(map[string]*schemapb.Relation) for _, plan := range planMap { for _, fragment := range plan.Nodes { for _, node := range fragment.Nodes { if node.Op.OpType == planpb.GRPC_SINK_OPERATOR { grpcSink := node.Op.GetGRPCSinkOp() outputTableInfo := grpcSink.GetOutputTable() if outputTableInfo == nil { continue } relation := &schemapb.Relation{ Columns: []*schemapb.Relation_ColumnInfo{}, } for i, colName := range outputTableInfo.ColumnNames { relation.Columns = append(relation.Columns, &schemapb.Relation_ColumnInfo{ ColumnName: colName, ColumnType: outputTableInfo.ColumnTypes[i], ColumnSemanticType: outputTableInfo.ColumnSemanticTypes[i], }) } outputRelations[outputTableInfo.TableName] = relation } } } } return outputRelations } // AgentRelationToVizierRelation converts the agent relation format to the Vizier relation format. func AgentRelationToVizierRelation(relation *schemapb.Relation) *vizierpb.Relation { var cols []*vizierpb.Relation_ColumnInfo for _, c := range relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols = append(cols, newCol) } return &vizierpb.Relation{ Columns: cols, } } // TableRelationResponses returns the query metadata table schemas as ExecuteScriptResponses. func TableRelationResponses(queryID uuid.UUID, tableIDMap map[string]string, planMap map[uuid.UUID]*planpb.Plan) ([]*vizierpb.ExecuteScriptResponse, error)
// StatusToError converts a statuspb.Status to a grpc error. func StatusToError(s *statuspb.Status) error { return status.Error(codes.Code(int32(s.ErrCode)), s.Msg) } // VizierStatusToError converts a vizierpb.Status to a grpc error. func VizierStatusToError(s *vizierpb.Status) error { return status.Error(codes.Code(s.Code), s.Message) }
{ var results []*vizierpb.ExecuteScriptResponse schemas := OutputSchemaFromPlan(planMap) for tableName, schema := range schemas { tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("Table ID for table name %s not found in table map", tableName) } convertedRelation := AgentRelationToVizierRelation(schema) results = append(results, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: tableName, ID: tableID, Relation: convertedRelation, }, }, }) } return results, nil }
identifier_body
proto_utils.go
/* * Copyright 2018- The Pixie Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 */ package controllers import ( "errors" "fmt" "github.com/gofrs/uuid" "github.com/gogo/protobuf/types" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "px.dev/pixie/src/api/proto/vizierpb" "px.dev/pixie/src/carnot/carnotpb" "px.dev/pixie/src/carnot/planner/compilerpb" "px.dev/pixie/src/carnot/planner/distributedpb" "px.dev/pixie/src/carnot/planner/plannerpb" "px.dev/pixie/src/carnot/planpb" "px.dev/pixie/src/carnot/queryresultspb" "px.dev/pixie/src/common/base/statuspb" "px.dev/pixie/src/shared/types/typespb" "px.dev/pixie/src/table_store/schemapb" "px.dev/pixie/src/utils" ) var dataTypeToVizierDataType = map[typespb.DataType]vizierpb.DataType{ typespb.DATA_TYPE_UNKNOWN: vizierpb.DATA_TYPE_UNKNOWN, typespb.BOOLEAN: vizierpb.BOOLEAN, typespb.INT64: vizierpb.INT64, typespb.UINT128: vizierpb.UINT128, typespb.FLOAT64: vizierpb.FLOAT64, typespb.STRING: vizierpb.STRING, typespb.TIME64NS: vizierpb.TIME64NS, } var semanticTypeToVizierSemanticType = map[typespb.SemanticType]vizierpb.SemanticType{ typespb.ST_UNSPECIFIED: vizierpb.ST_UNSPECIFIED, typespb.ST_NONE: vizierpb.ST_NONE, typespb.ST_TIME_NS: vizierpb.ST_TIME_NS, typespb.ST_AGENT_UID: vizierpb.ST_AGENT_UID, typespb.ST_ASID: vizierpb.ST_ASID, typespb.ST_UPID: vizierpb.ST_UPID, typespb.ST_SERVICE_NAME: vizierpb.ST_SERVICE_NAME, typespb.ST_POD_NAME: vizierpb.ST_POD_NAME, typespb.ST_POD_PHASE: vizierpb.ST_POD_PHASE, typespb.ST_POD_STATUS: vizierpb.ST_POD_STATUS, typespb.ST_NODE_NAME: vizierpb.ST_NODE_NAME, typespb.ST_CONTAINER_NAME: vizierpb.ST_CONTAINER_NAME, typespb.ST_CONTAINER_STATE: vizierpb.ST_CONTAINER_STATE, typespb.ST_CONTAINER_STATUS: vizierpb.ST_CONTAINER_STATUS, typespb.ST_NAMESPACE_NAME: vizierpb.ST_NAMESPACE_NAME, typespb.ST_BYTES: vizierpb.ST_BYTES, typespb.ST_PERCENT: vizierpb.ST_PERCENT, typespb.ST_DURATION_NS: vizierpb.ST_DURATION_NS, typespb.ST_THROUGHPUT_PER_NS: vizierpb.ST_THROUGHPUT_PER_NS, typespb.ST_THROUGHPUT_BYTES_PER_NS: vizierpb.ST_THROUGHPUT_BYTES_PER_NS, typespb.ST_QUANTILES: vizierpb.ST_QUANTILES, typespb.ST_DURATION_NS_QUANTILES: vizierpb.ST_DURATION_NS_QUANTILES, typespb.ST_IP_ADDRESS: vizierpb.ST_IP_ADDRESS, typespb.ST_PORT: vizierpb.ST_PORT, typespb.ST_HTTP_REQ_METHOD: vizierpb.ST_HTTP_REQ_METHOD, typespb.ST_HTTP_RESP_STATUS: vizierpb.ST_HTTP_RESP_STATUS, typespb.ST_HTTP_RESP_MESSAGE: vizierpb.ST_HTTP_RESP_MESSAGE, typespb.ST_SCRIPT_REFERENCE: vizierpb.ST_SCRIPT_REFERENCE, } // These codes are taken from https://godoc.org/google.golang.org/grpc/codes#Code. var statusCodeToGRPCCode = map[statuspb.Code]codes.Code{ statuspb.OK: codes.OK, statuspb.CANCELLED: codes.Canceled, statuspb.UNKNOWN: codes.Unknown, statuspb.INVALID_ARGUMENT: codes.InvalidArgument, statuspb.DEADLINE_EXCEEDED: codes.DeadlineExceeded, statuspb.NOT_FOUND: codes.NotFound, statuspb.ALREADY_EXISTS: codes.AlreadyExists, statuspb.PERMISSION_DENIED: codes.PermissionDenied, statuspb.UNAUTHENTICATED: codes.Unauthenticated, statuspb.INTERNAL: codes.Internal, statuspb.RESOURCE_UNAVAILABLE: codes.Unavailable, statuspb.SYSTEM: codes.Internal, } var lifeCycleStateToVizierLifeCycleStateMap = map[statuspb.LifeCycleState]vizierpb.LifeCycleState{ statuspb.UNKNOWN_STATE: vizierpb.UNKNOWN_STATE, statuspb.PENDING_STATE: vizierpb.PENDING_STATE, statuspb.RUNNING_STATE: vizierpb.RUNNING_STATE, statuspb.TERMINATED_STATE: vizierpb.TERMINATED_STATE, statuspb.FAILED_STATE: vizierpb.FAILED_STATE, } func convertLifeCycleStateToVizierLifeCycleState(state statuspb.LifeCycleState) vizierpb.LifeCycleState { if val, ok := lifeCycleStateToVizierLifeCycleStateMap[state]; ok { return val } return vizierpb.UNKNOWN_STATE } func convertExecFuncs(inputFuncs []*vizierpb.ExecuteScriptRequest_FuncToExecute) []*plannerpb.FuncToExecute { funcs := make([]*plannerpb.FuncToExecute, len(inputFuncs)) for i, f := range inputFuncs { args := make([]*plannerpb.FuncToExecute_ArgValue, len(f.ArgValues)) for j, arg := range f.ArgValues { args[j] = &plannerpb.FuncToExecute_ArgValue{ Name: arg.Name, Value: arg.Value, } } funcs[i] = &plannerpb.FuncToExecute{ FuncName: f.FuncName, ArgValues: args, OutputTablePrefix: f.OutputTablePrefix, } } return funcs } func convertConfigs(config *vizierpb.Configs) *plannerpb.Configs { if config == nil { return nil } c := &plannerpb.Configs{} if config.OTelEndpointConfig != nil { c.OTelEndpointConfig = &plannerpb.Configs_OTelEndpointConfig{ URL: config.OTelEndpointConfig.URL, Headers: config.OTelEndpointConfig.Headers, Insecure: config.OTelEndpointConfig.Insecure, Timeout: config.OTelEndpointConfig.Timeout, } } if config.PluginConfig != nil { c.PluginConfig = &plannerpb.Configs_PluginConfig{ StartTimeNs: config.PluginConfig.StartTimeNs, EndTimeNs: config.PluginConfig.EndTimeNs, } } return c } // VizierQueryRequestToPlannerMutationRequest maps request to mutation. func VizierQueryRequestToPlannerMutationRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.CompileMutationsRequest, error) { return &plannerpb.CompileMutationsRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // VizierQueryRequestToPlannerQueryRequest converts a externally-facing query request to an internal representation. func VizierQueryRequestToPlannerQueryRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.QueryRequest, error) { return &plannerpb.QueryRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // ErrToVizierResponse converts an error to an externally-facing Vizier response message func ErrToVizierResponse(id uuid.UUID, err error) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: ErrToVizierStatus(err), } } // ErrToVizierStatus converts an error to an externally-facing Vizier status. func ErrToVizierStatus(err error) *vizierpb.Status { s, ok := status.FromError(err) if ok { return &vizierpb.Status{ Code: int32(s.Code()), Message: s.Message(), } } return &vizierpb.Status{ Code: int32(codes.Unknown), Message: err.Error(), } } // StatusToVizierResponse converts an error to an externally-facing Vizier response message func StatusToVizierResponse(id uuid.UUID, s *statuspb.Status) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: StatusToVizierStatus(s), } } // StatusToVizierStatus converts an internal status to an externally-facing Vizier status. func StatusToVizierStatus(s *statuspb.Status) *vizierpb.Status { return &vizierpb.Status{ Code: int32(statusCodeToGRPCCode[s.ErrCode]), Message: s.Msg, ErrorDetails: getErrorsFromStatusContext(s.Context), } } func getErrorsFromStatusContext(ctx *types.Any) []*vizierpb.ErrorDetails { errorPB := &compilerpb.CompilerErrorGroup{} if !types.Is(ctx, errorPB) { return nil } err := types.UnmarshalAny(ctx, errorPB) if err != nil { return nil } errors := make([]*vizierpb.ErrorDetails, len(errorPB.Errors)) for i, e := range errorPB.Errors { lcErr := e.GetLineColError() errors[i] = &vizierpb.ErrorDetails{ Error: &vizierpb.ErrorDetails_CompilerError{ CompilerError: &vizierpb.CompilerError{ Line: lcErr.Line, Column: lcErr.Column, Message: lcErr.Message, }, }, } } return errors } // RelationFromTable gets the relation from the table. func RelationFromTable(table *schemapb.Table) (*vizierpb.QueryMetadata, error) { cols := make([]*vizierpb.Relation_ColumnInfo, len(table.Relation.Columns)) for i, c := range table.Relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols[i] = newCol } return &vizierpb.QueryMetadata{ Relation: &vizierpb.Relation{ Columns: cols, }, Name: table.Name, }, nil } // QueryResultStatsToVizierStats gets the execution stats from the query results. func QueryResultStatsToVizierStats(e *queryresultspb.QueryExecutionStats, compilationTimeNs int64) *vizierpb.QueryExecutionStats { return &vizierpb.QueryExecutionStats{ Timing: &vizierpb.QueryTimingInfo{ ExecutionTimeNs: e.Timing.ExecutionTimeNs, CompilationTimeNs: compilationTimeNs, }, BytesProcessed: e.BytesProcessed, RecordsProcessed: e.RecordsProcessed, } } // UInt128ToVizierUInt128 converts our internal representation of UInt128 to Vizier's representation of UInt128. func
(i *typespb.UInt128) *vizierpb.UInt128 { return &vizierpb.UInt128{ Low: i.Low, High: i.High, } } func colToVizierCol(col *schemapb.Column) (*vizierpb.Column, error) { switch c := col.ColData.(type) { case *schemapb.Column_BooleanData: return &vizierpb.Column{ ColData: &vizierpb.Column_BooleanData{ BooleanData: &vizierpb.BooleanColumn{ Data: c.BooleanData.Data, }, }, }, nil case *schemapb.Column_Int64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Int64Data{ Int64Data: &vizierpb.Int64Column{ Data: c.Int64Data.Data, }, }, }, nil case *schemapb.Column_Uint128Data: b := make([]*vizierpb.UInt128, len(c.Uint128Data.Data)) for i, s := range c.Uint128Data.Data { b[i] = UInt128ToVizierUInt128(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_Uint128Data{ Uint128Data: &vizierpb.UInt128Column{ Data: b, }, }, }, nil case *schemapb.Column_Time64NsData: return &vizierpb.Column{ ColData: &vizierpb.Column_Time64NsData{ Time64NsData: &vizierpb.Time64NSColumn{ Data: c.Time64NsData.Data, }, }, }, nil case *schemapb.Column_Float64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Float64Data{ Float64Data: &vizierpb.Float64Column{ Data: c.Float64Data.Data, }, }, }, nil case *schemapb.Column_StringData: b := make([][]byte, len(c.StringData.Data)) for i, s := range c.StringData.Data { b[i] = []byte(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: b, }, }, }, nil default: return nil, errors.New("Could not get column type") } } // RowBatchToVizierRowBatch converts an internal row batch to a vizier row batch. func RowBatchToVizierRowBatch(rb *schemapb.RowBatchData, tableID string) (*vizierpb.RowBatchData, error) { cols := make([]*vizierpb.Column, len(rb.Cols)) for i, col := range rb.Cols { c, err := colToVizierCol(col) if err != nil { return nil, err } cols[i] = c } return &vizierpb.RowBatchData{ TableID: tableID, NumRows: rb.NumRows, Eow: rb.Eow, Eos: rb.Eos, Cols: cols, }, nil } // BuildExecuteScriptResponse Converts the agent-format result into the vizier client format result. func BuildExecuteScriptResponse(r *carnotpb.TransferResultChunkRequest, // Map of the received table names to their table ID on the output proto. tableIDMap map[string]string, compilationTimeNs int64) (*vizierpb.ExecuteScriptResponse, error) { res := &vizierpb.ExecuteScriptResponse{ QueryID: utils.UUIDFromProtoOrNil(r.QueryID).String(), } if execStats := r.GetExecutionAndTimingInfo(); execStats != nil { stats := QueryResultStatsToVizierStats(execStats.ExecutionStats, compilationTimeNs) res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ ExecutionStats: stats, }, } return res, nil } // This agent message type will not turn into a message on the client stream. if initConn := r.GetInitiateConn(); initConn != nil { return nil, nil } if queryResult := r.GetQueryResult(); queryResult != nil { tableName := queryResult.GetTableName() tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("table %s does not have an ID in the table ID map", tableName) } if queryResult.GetRowBatch() == nil { return nil, fmt.Errorf("BuildExecuteScriptResponse expected a non-nil row batch") } batch, err := RowBatchToVizierRowBatch(queryResult.GetRowBatch(), tableID) if err != nil { return nil, err } res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, } return res, nil } if execError := r.GetExecutionError(); execError != nil { res.Status = StatusToVizierStatus(execError) return res, nil } return nil, fmt.Errorf("error in ForwardQueryResult: Expected TransferResultChunkRequest to have init message, row batch, exec stats, exec error") } // QueryPlanResponse returns the query plan as an ExecuteScriptResponse. func QueryPlanResponse(queryID uuid.UUID, plan *distributedpb.DistributedPlan, planMap map[uuid.UUID]*planpb.Plan, agentStats *[]*queryresultspb.AgentExecutionStats, planTableID string, maxQueryPlanStringSizeBytes int) ([]*vizierpb.ExecuteScriptResponse, error) { queryPlan, err := GetQueryPlanAsDotString(plan, planMap, agentStats) if err != nil { log.WithError(err).Error("error with query plan") return nil, err } var resp []*vizierpb.ExecuteScriptResponse // We can't overwhelm NATS with a query plan greater than 1MB. for i := 0; i < len(queryPlan); i += maxQueryPlanStringSizeBytes { end := i + maxQueryPlanStringSizeBytes if end > len(queryPlan) { end = len(queryPlan) } last := end == len(queryPlan) batch := &vizierpb.RowBatchData{ TableID: planTableID, Cols: []*vizierpb.Column{ { ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: [][]byte{[]byte(queryPlan[i:end])}, }, }, }, }, NumRows: 1, Eos: last, Eow: last, } resp = append(resp, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, }, }) } return resp, nil } // QueryPlanRelationResponse returns the relation of the query plan as an ExecuteScriptResponse. func QueryPlanRelationResponse(queryID uuid.UUID, planTableID string) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: "__query_plan__", ID: planTableID, Relation: &vizierpb.Relation{ Columns: []*vizierpb.Relation_ColumnInfo{ { ColumnName: "query_plan", ColumnType: vizierpb.STRING, ColumnDesc: "The query plan", }, }, }, }, }, } } // OutputSchemaFromPlan takes in a plan map and returns the relations for all of the final output // tables in the plan map. func OutputSchemaFromPlan(planMap map[uuid.UUID]*planpb.Plan) map[string]*schemapb.Relation { outputRelations := make(map[string]*schemapb.Relation) for _, plan := range planMap { for _, fragment := range plan.Nodes { for _, node := range fragment.Nodes { if node.Op.OpType == planpb.GRPC_SINK_OPERATOR { grpcSink := node.Op.GetGRPCSinkOp() outputTableInfo := grpcSink.GetOutputTable() if outputTableInfo == nil { continue } relation := &schemapb.Relation{ Columns: []*schemapb.Relation_ColumnInfo{}, } for i, colName := range outputTableInfo.ColumnNames { relation.Columns = append(relation.Columns, &schemapb.Relation_ColumnInfo{ ColumnName: colName, ColumnType: outputTableInfo.ColumnTypes[i], ColumnSemanticType: outputTableInfo.ColumnSemanticTypes[i], }) } outputRelations[outputTableInfo.TableName] = relation } } } } return outputRelations } // AgentRelationToVizierRelation converts the agent relation format to the Vizier relation format. func AgentRelationToVizierRelation(relation *schemapb.Relation) *vizierpb.Relation { var cols []*vizierpb.Relation_ColumnInfo for _, c := range relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols = append(cols, newCol) } return &vizierpb.Relation{ Columns: cols, } } // TableRelationResponses returns the query metadata table schemas as ExecuteScriptResponses. func TableRelationResponses(queryID uuid.UUID, tableIDMap map[string]string, planMap map[uuid.UUID]*planpb.Plan) ([]*vizierpb.ExecuteScriptResponse, error) { var results []*vizierpb.ExecuteScriptResponse schemas := OutputSchemaFromPlan(planMap) for tableName, schema := range schemas { tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("Table ID for table name %s not found in table map", tableName) } convertedRelation := AgentRelationToVizierRelation(schema) results = append(results, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: tableName, ID: tableID, Relation: convertedRelation, }, }, }) } return results, nil } // StatusToError converts a statuspb.Status to a grpc error. func StatusToError(s *statuspb.Status) error { return status.Error(codes.Code(int32(s.ErrCode)), s.Msg) } // VizierStatusToError converts a vizierpb.Status to a grpc error. func VizierStatusToError(s *vizierpb.Status) error { return status.Error(codes.Code(s.Code), s.Message) }
UInt128ToVizierUInt128
identifier_name
proto_utils.go
/* * Copyright 2018- The Pixie Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 */ package controllers import ( "errors" "fmt" "github.com/gofrs/uuid" "github.com/gogo/protobuf/types" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "px.dev/pixie/src/api/proto/vizierpb" "px.dev/pixie/src/carnot/carnotpb" "px.dev/pixie/src/carnot/planner/compilerpb" "px.dev/pixie/src/carnot/planner/distributedpb" "px.dev/pixie/src/carnot/planner/plannerpb" "px.dev/pixie/src/carnot/planpb" "px.dev/pixie/src/carnot/queryresultspb" "px.dev/pixie/src/common/base/statuspb" "px.dev/pixie/src/shared/types/typespb" "px.dev/pixie/src/table_store/schemapb" "px.dev/pixie/src/utils" ) var dataTypeToVizierDataType = map[typespb.DataType]vizierpb.DataType{ typespb.DATA_TYPE_UNKNOWN: vizierpb.DATA_TYPE_UNKNOWN, typespb.BOOLEAN: vizierpb.BOOLEAN, typespb.INT64: vizierpb.INT64, typespb.UINT128: vizierpb.UINT128, typespb.FLOAT64: vizierpb.FLOAT64, typespb.STRING: vizierpb.STRING, typespb.TIME64NS: vizierpb.TIME64NS, } var semanticTypeToVizierSemanticType = map[typespb.SemanticType]vizierpb.SemanticType{ typespb.ST_UNSPECIFIED: vizierpb.ST_UNSPECIFIED, typespb.ST_NONE: vizierpb.ST_NONE, typespb.ST_TIME_NS: vizierpb.ST_TIME_NS, typespb.ST_AGENT_UID: vizierpb.ST_AGENT_UID, typespb.ST_ASID: vizierpb.ST_ASID, typespb.ST_UPID: vizierpb.ST_UPID, typespb.ST_SERVICE_NAME: vizierpb.ST_SERVICE_NAME, typespb.ST_POD_NAME: vizierpb.ST_POD_NAME, typespb.ST_POD_PHASE: vizierpb.ST_POD_PHASE, typespb.ST_POD_STATUS: vizierpb.ST_POD_STATUS, typespb.ST_NODE_NAME: vizierpb.ST_NODE_NAME, typespb.ST_CONTAINER_NAME: vizierpb.ST_CONTAINER_NAME, typespb.ST_CONTAINER_STATE: vizierpb.ST_CONTAINER_STATE, typespb.ST_CONTAINER_STATUS: vizierpb.ST_CONTAINER_STATUS, typespb.ST_NAMESPACE_NAME: vizierpb.ST_NAMESPACE_NAME, typespb.ST_BYTES: vizierpb.ST_BYTES, typespb.ST_PERCENT: vizierpb.ST_PERCENT, typespb.ST_DURATION_NS: vizierpb.ST_DURATION_NS, typespb.ST_THROUGHPUT_PER_NS: vizierpb.ST_THROUGHPUT_PER_NS, typespb.ST_THROUGHPUT_BYTES_PER_NS: vizierpb.ST_THROUGHPUT_BYTES_PER_NS, typespb.ST_QUANTILES: vizierpb.ST_QUANTILES, typespb.ST_DURATION_NS_QUANTILES: vizierpb.ST_DURATION_NS_QUANTILES, typespb.ST_IP_ADDRESS: vizierpb.ST_IP_ADDRESS, typespb.ST_PORT: vizierpb.ST_PORT, typespb.ST_HTTP_REQ_METHOD: vizierpb.ST_HTTP_REQ_METHOD, typespb.ST_HTTP_RESP_STATUS: vizierpb.ST_HTTP_RESP_STATUS, typespb.ST_HTTP_RESP_MESSAGE: vizierpb.ST_HTTP_RESP_MESSAGE, typespb.ST_SCRIPT_REFERENCE: vizierpb.ST_SCRIPT_REFERENCE, } // These codes are taken from https://godoc.org/google.golang.org/grpc/codes#Code. var statusCodeToGRPCCode = map[statuspb.Code]codes.Code{ statuspb.OK: codes.OK, statuspb.CANCELLED: codes.Canceled, statuspb.UNKNOWN: codes.Unknown, statuspb.INVALID_ARGUMENT: codes.InvalidArgument, statuspb.DEADLINE_EXCEEDED: codes.DeadlineExceeded, statuspb.NOT_FOUND: codes.NotFound, statuspb.ALREADY_EXISTS: codes.AlreadyExists, statuspb.PERMISSION_DENIED: codes.PermissionDenied, statuspb.UNAUTHENTICATED: codes.Unauthenticated, statuspb.INTERNAL: codes.Internal, statuspb.RESOURCE_UNAVAILABLE: codes.Unavailable, statuspb.SYSTEM: codes.Internal, } var lifeCycleStateToVizierLifeCycleStateMap = map[statuspb.LifeCycleState]vizierpb.LifeCycleState{ statuspb.UNKNOWN_STATE: vizierpb.UNKNOWN_STATE, statuspb.PENDING_STATE: vizierpb.PENDING_STATE, statuspb.RUNNING_STATE: vizierpb.RUNNING_STATE, statuspb.TERMINATED_STATE: vizierpb.TERMINATED_STATE, statuspb.FAILED_STATE: vizierpb.FAILED_STATE, } func convertLifeCycleStateToVizierLifeCycleState(state statuspb.LifeCycleState) vizierpb.LifeCycleState { if val, ok := lifeCycleStateToVizierLifeCycleStateMap[state]; ok { return val } return vizierpb.UNKNOWN_STATE } func convertExecFuncs(inputFuncs []*vizierpb.ExecuteScriptRequest_FuncToExecute) []*plannerpb.FuncToExecute { funcs := make([]*plannerpb.FuncToExecute, len(inputFuncs)) for i, f := range inputFuncs { args := make([]*plannerpb.FuncToExecute_ArgValue, len(f.ArgValues)) for j, arg := range f.ArgValues { args[j] = &plannerpb.FuncToExecute_ArgValue{ Name: arg.Name, Value: arg.Value, } } funcs[i] = &plannerpb.FuncToExecute{ FuncName: f.FuncName, ArgValues: args, OutputTablePrefix: f.OutputTablePrefix, } } return funcs } func convertConfigs(config *vizierpb.Configs) *plannerpb.Configs { if config == nil { return nil } c := &plannerpb.Configs{} if config.OTelEndpointConfig != nil { c.OTelEndpointConfig = &plannerpb.Configs_OTelEndpointConfig{ URL: config.OTelEndpointConfig.URL, Headers: config.OTelEndpointConfig.Headers, Insecure: config.OTelEndpointConfig.Insecure, Timeout: config.OTelEndpointConfig.Timeout, } } if config.PluginConfig != nil { c.PluginConfig = &plannerpb.Configs_PluginConfig{ StartTimeNs: config.PluginConfig.StartTimeNs, EndTimeNs: config.PluginConfig.EndTimeNs, } } return c } // VizierQueryRequestToPlannerMutationRequest maps request to mutation. func VizierQueryRequestToPlannerMutationRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.CompileMutationsRequest, error) { return &plannerpb.CompileMutationsRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // VizierQueryRequestToPlannerQueryRequest converts a externally-facing query request to an internal representation. func VizierQueryRequestToPlannerQueryRequest(vpb *vizierpb.ExecuteScriptRequest) (*plannerpb.QueryRequest, error) { return &plannerpb.QueryRequest{ QueryStr: vpb.QueryStr, ExecFuncs: convertExecFuncs(vpb.ExecFuncs), Configs: convertConfigs(vpb.Configs), }, nil } // ErrToVizierResponse converts an error to an externally-facing Vizier response message func ErrToVizierResponse(id uuid.UUID, err error) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: ErrToVizierStatus(err), } } // ErrToVizierStatus converts an error to an externally-facing Vizier status. func ErrToVizierStatus(err error) *vizierpb.Status { s, ok := status.FromError(err) if ok { return &vizierpb.Status{ Code: int32(s.Code()), Message: s.Message(), } } return &vizierpb.Status{ Code: int32(codes.Unknown), Message: err.Error(), } } // StatusToVizierResponse converts an error to an externally-facing Vizier response message func StatusToVizierResponse(id uuid.UUID, s *statuspb.Status) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: id.String(), Status: StatusToVizierStatus(s), } } // StatusToVizierStatus converts an internal status to an externally-facing Vizier status. func StatusToVizierStatus(s *statuspb.Status) *vizierpb.Status { return &vizierpb.Status{ Code: int32(statusCodeToGRPCCode[s.ErrCode]), Message: s.Msg, ErrorDetails: getErrorsFromStatusContext(s.Context), } } func getErrorsFromStatusContext(ctx *types.Any) []*vizierpb.ErrorDetails { errorPB := &compilerpb.CompilerErrorGroup{} if !types.Is(ctx, errorPB) { return nil } err := types.UnmarshalAny(ctx, errorPB) if err != nil { return nil } errors := make([]*vizierpb.ErrorDetails, len(errorPB.Errors)) for i, e := range errorPB.Errors { lcErr := e.GetLineColError() errors[i] = &vizierpb.ErrorDetails{ Error: &vizierpb.ErrorDetails_CompilerError{ CompilerError: &vizierpb.CompilerError{ Line: lcErr.Line, Column: lcErr.Column, Message: lcErr.Message, }, }, } } return errors } // RelationFromTable gets the relation from the table. func RelationFromTable(table *schemapb.Table) (*vizierpb.QueryMetadata, error) { cols := make([]*vizierpb.Relation_ColumnInfo, len(table.Relation.Columns)) for i, c := range table.Relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], } cols[i] = newCol } return &vizierpb.QueryMetadata{ Relation: &vizierpb.Relation{ Columns: cols, }, Name: table.Name, }, nil } // QueryResultStatsToVizierStats gets the execution stats from the query results. func QueryResultStatsToVizierStats(e *queryresultspb.QueryExecutionStats, compilationTimeNs int64) *vizierpb.QueryExecutionStats { return &vizierpb.QueryExecutionStats{ Timing: &vizierpb.QueryTimingInfo{ ExecutionTimeNs: e.Timing.ExecutionTimeNs, CompilationTimeNs: compilationTimeNs, }, BytesProcessed: e.BytesProcessed, RecordsProcessed: e.RecordsProcessed, } } // UInt128ToVizierUInt128 converts our internal representation of UInt128 to Vizier's representation of UInt128. func UInt128ToVizierUInt128(i *typespb.UInt128) *vizierpb.UInt128 { return &vizierpb.UInt128{ Low: i.Low, High: i.High, } } func colToVizierCol(col *schemapb.Column) (*vizierpb.Column, error) { switch c := col.ColData.(type) { case *schemapb.Column_BooleanData: return &vizierpb.Column{ ColData: &vizierpb.Column_BooleanData{ BooleanData: &vizierpb.BooleanColumn{ Data: c.BooleanData.Data, }, }, }, nil case *schemapb.Column_Int64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Int64Data{ Int64Data: &vizierpb.Int64Column{ Data: c.Int64Data.Data, }, }, }, nil case *schemapb.Column_Uint128Data: b := make([]*vizierpb.UInt128, len(c.Uint128Data.Data)) for i, s := range c.Uint128Data.Data { b[i] = UInt128ToVizierUInt128(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_Uint128Data{ Uint128Data: &vizierpb.UInt128Column{ Data: b, }, }, }, nil case *schemapb.Column_Time64NsData: return &vizierpb.Column{ ColData: &vizierpb.Column_Time64NsData{ Time64NsData: &vizierpb.Time64NSColumn{ Data: c.Time64NsData.Data, }, }, }, nil case *schemapb.Column_Float64Data: return &vizierpb.Column{ ColData: &vizierpb.Column_Float64Data{ Float64Data: &vizierpb.Float64Column{ Data: c.Float64Data.Data, }, }, }, nil case *schemapb.Column_StringData: b := make([][]byte, len(c.StringData.Data)) for i, s := range c.StringData.Data { b[i] = []byte(s) } return &vizierpb.Column{ ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: b, }, }, }, nil default: return nil, errors.New("Could not get column type") } } // RowBatchToVizierRowBatch converts an internal row batch to a vizier row batch. func RowBatchToVizierRowBatch(rb *schemapb.RowBatchData, tableID string) (*vizierpb.RowBatchData, error) { cols := make([]*vizierpb.Column, len(rb.Cols)) for i, col := range rb.Cols { c, err := colToVizierCol(col) if err != nil { return nil, err } cols[i] = c } return &vizierpb.RowBatchData{ TableID: tableID, NumRows: rb.NumRows, Eow: rb.Eow, Eos: rb.Eos, Cols: cols, }, nil } // BuildExecuteScriptResponse Converts the agent-format result into the vizier client format result. func BuildExecuteScriptResponse(r *carnotpb.TransferResultChunkRequest, // Map of the received table names to their table ID on the output proto. tableIDMap map[string]string, compilationTimeNs int64) (*vizierpb.ExecuteScriptResponse, error) { res := &vizierpb.ExecuteScriptResponse{ QueryID: utils.UUIDFromProtoOrNil(r.QueryID).String(), } if execStats := r.GetExecutionAndTimingInfo(); execStats != nil { stats := QueryResultStatsToVizierStats(execStats.ExecutionStats, compilationTimeNs) res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ ExecutionStats: stats, }, } return res, nil } // This agent message type will not turn into a message on the client stream. if initConn := r.GetInitiateConn(); initConn != nil { return nil, nil } if queryResult := r.GetQueryResult(); queryResult != nil { tableName := queryResult.GetTableName() tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("table %s does not have an ID in the table ID map", tableName) } if queryResult.GetRowBatch() == nil { return nil, fmt.Errorf("BuildExecuteScriptResponse expected a non-nil row batch") } batch, err := RowBatchToVizierRowBatch(queryResult.GetRowBatch(), tableID) if err != nil { return nil, err } res.Result = &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, } return res, nil } if execError := r.GetExecutionError(); execError != nil { res.Status = StatusToVizierStatus(execError) return res, nil } return nil, fmt.Errorf("error in ForwardQueryResult: Expected TransferResultChunkRequest to have init message, row batch, exec stats, exec error") } // QueryPlanResponse returns the query plan as an ExecuteScriptResponse. func QueryPlanResponse(queryID uuid.UUID, plan *distributedpb.DistributedPlan, planMap map[uuid.UUID]*planpb.Plan, agentStats *[]*queryresultspb.AgentExecutionStats, planTableID string, maxQueryPlanStringSizeBytes int) ([]*vizierpb.ExecuteScriptResponse, error) { queryPlan, err := GetQueryPlanAsDotString(plan, planMap, agentStats) if err != nil { log.WithError(err).Error("error with query plan") return nil, err } var resp []*vizierpb.ExecuteScriptResponse // We can't overwhelm NATS with a query plan greater than 1MB. for i := 0; i < len(queryPlan); i += maxQueryPlanStringSizeBytes { end := i + maxQueryPlanStringSizeBytes if end > len(queryPlan) { end = len(queryPlan) } last := end == len(queryPlan) batch := &vizierpb.RowBatchData{ TableID: planTableID, Cols: []*vizierpb.Column{ { ColData: &vizierpb.Column_StringData{ StringData: &vizierpb.StringColumn{ Data: [][]byte{[]byte(queryPlan[i:end])}, }, }, }, }, NumRows: 1, Eos: last, Eow: last, } resp = append(resp, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_Data{ Data: &vizierpb.QueryData{ Batch: batch, }, }, }) } return resp, nil } // QueryPlanRelationResponse returns the relation of the query plan as an ExecuteScriptResponse. func QueryPlanRelationResponse(queryID uuid.UUID, planTableID string) *vizierpb.ExecuteScriptResponse { return &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: "__query_plan__", ID: planTableID, Relation: &vizierpb.Relation{ Columns: []*vizierpb.Relation_ColumnInfo{ { ColumnName: "query_plan", ColumnType: vizierpb.STRING, ColumnDesc: "The query plan", }, }, }, }, }, } } // OutputSchemaFromPlan takes in a plan map and returns the relations for all of the final output // tables in the plan map. func OutputSchemaFromPlan(planMap map[uuid.UUID]*planpb.Plan) map[string]*schemapb.Relation { outputRelations := make(map[string]*schemapb.Relation) for _, plan := range planMap { for _, fragment := range plan.Nodes { for _, node := range fragment.Nodes { if node.Op.OpType == planpb.GRPC_SINK_OPERATOR { grpcSink := node.Op.GetGRPCSinkOp() outputTableInfo := grpcSink.GetOutputTable() if outputTableInfo == nil { continue } relation := &schemapb.Relation{ Columns: []*schemapb.Relation_ColumnInfo{}, } for i, colName := range outputTableInfo.ColumnNames { relation.Columns = append(relation.Columns, &schemapb.Relation_ColumnInfo{ ColumnName: colName, ColumnType: outputTableInfo.ColumnTypes[i], ColumnSemanticType: outputTableInfo.ColumnSemanticTypes[i], }) } outputRelations[outputTableInfo.TableName] = relation } } } } return outputRelations } // AgentRelationToVizierRelation converts the agent relation format to the Vizier relation format. func AgentRelationToVizierRelation(relation *schemapb.Relation) *vizierpb.Relation { var cols []*vizierpb.Relation_ColumnInfo for _, c := range relation.Columns { newCol := &vizierpb.Relation_ColumnInfo{ ColumnName: c.ColumnName, ColumnDesc: c.ColumnDesc, ColumnType: dataTypeToVizierDataType[c.ColumnType], ColumnSemanticType: semanticTypeToVizierSemanticType[c.ColumnSemanticType], }
} } // TableRelationResponses returns the query metadata table schemas as ExecuteScriptResponses. func TableRelationResponses(queryID uuid.UUID, tableIDMap map[string]string, planMap map[uuid.UUID]*planpb.Plan) ([]*vizierpb.ExecuteScriptResponse, error) { var results []*vizierpb.ExecuteScriptResponse schemas := OutputSchemaFromPlan(planMap) for tableName, schema := range schemas { tableID, present := tableIDMap[tableName] if !present { return nil, fmt.Errorf("Table ID for table name %s not found in table map", tableName) } convertedRelation := AgentRelationToVizierRelation(schema) results = append(results, &vizierpb.ExecuteScriptResponse{ QueryID: queryID.String(), Result: &vizierpb.ExecuteScriptResponse_MetaData{ MetaData: &vizierpb.QueryMetadata{ Name: tableName, ID: tableID, Relation: convertedRelation, }, }, }) } return results, nil } // StatusToError converts a statuspb.Status to a grpc error. func StatusToError(s *statuspb.Status) error { return status.Error(codes.Code(int32(s.ErrCode)), s.Msg) } // VizierStatusToError converts a vizierpb.Status to a grpc error. func VizierStatusToError(s *vizierpb.Status) error { return status.Error(codes.Code(s.Code), s.Message) }
cols = append(cols, newCol) } return &vizierpb.Relation{ Columns: cols,
random_line_split
setuptool.go
/* Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // The command line tool for setting up services and accounts. package main import ( "bufio" "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/x509" "encoding/base64" "encoding/json" "flag" "fmt" "net/url" "os" "regexp" "strconv" "strings" "github.com/google/web-api-gateway/config" "golang.org/x/oauth2" ) func main() { flag.Parse() fmt.Printf("Welcome to the Web Api Gateway Config Tool.\n\n") term := newRealTerminal() c, save, err := config.ReadWriteConfig() if err != nil { fmt.Printf("Unable to load config file: %v\n", err) os.Exit(1) } configEditor{c}.edit(term) fmt.Println("Saving...") for { err = save() if err == nil { fmt.Println("Save successful!") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("Remember to restart the server.") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") return } fmt.Printf("There was an error saving the config file: %v\n", err) tryAgain := term.readBoolean("Do you want to try again? (no to lose all changes and exit.)") if !tryAgain { return } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type configEditor struct { c *config.Config } func (e configEditor) edit(term *terminal) { if e.c.Url == "" { e.editUrl(term) } takeActionLoop(term, newAction("Retrieve Account Key", e.retrieveAccountKey), newAction("Edit Web Api Gateway Url", e.editUrl), newAction("Add service", e.addService), newAction("Edit service (including adding new accounts to an existing service)", e.editService), newAction("Delete service", e.removeService)) } func (e configEditor) editUrl(term *terminal) { e.c.Url = term.readUrl("Web Api Gateway Url") } func (e configEditor) addService(term *terminal) { s := &config.Service{} serviceEditor{e.c, s}.newSetup(term) e.c.Services = append(e.c.Services, s) } func (e configEditor) editService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to edit.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) serviceEditor{e.c, e.c.Services[i]}.edit(term) } func (e configEditor) removeService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to delete.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) e.c.Services = append(e.c.Services[:i], e.c.Services[i+1:]...) } func (e configEditor) retrieveAccountKey(term *terminal) { fmt.Println("Select which account:") type accountSpecific struct { s *config.Service a *config.Account } allAccounts := make([]accountSpecific, 0) for _, s := range e.c.Services { for _, a := range s.Accounts { allAccounts = append(allAccounts, accountSpecific{s, a}) } } if len(allAccounts) == 0 { fmt.Println("You must create accounts first!") return } namer := func(i int) string { return allAccounts[i].s.ServiceName + "/" + allAccounts[i].a.AccountName } i := term.readChoice(namer, len(allAccounts)) key, err := createAccountKey(e.c, allAccounts[i].s, allAccounts[i].a) if err != nil { fmt.Println("Error creating account key:") fmt.Println(err) return } fmt.Println() fmt.Println("Copy and paste everything from (and including) KEYBEGIN to KEYEND") fmt.Println() fmt.Println(key) fmt.Println() fmt.Println() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func createAccountKey(c *config.Config, s *config.Service, a *config.Account) (string, error) { j := struct { WebGatewayUrl string Protocol string PrivateKey string }{ WebGatewayUrl: c.Url, Protocol: a.ClientCreds.Protocol, PrivateKey: a.ClientCreds.PrivateKey, } b, err := json.Marshal(j) if err != nil { return "", err } inner := base64.StdEncoding.EncodeToString(b) return fmt.Sprintf("KEYBEGIN_%s/%s_%s_KEYEND", s.ServiceName, a.AccountName, inner), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type serviceEditor struct { c *config.Config s *config.Service } func (e serviceEditor) newSetup(term *terminal) { e.setName(term) e.s.OauthServiceCreds = new(config.OauthServiceCreds) oauthServiceCredsEditor{e.s.OauthServiceCreds}.newSetup(term) e.edit(term) // Give user chance to add accounts right away } func (e serviceEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit name", confirmRename(e.setName)), newAction("Edit OAuth credentials", (&oauthServiceCredsEditor{e.s.OauthServiceCreds}).edit), newAction("Add new account", e.addAccount), newAction("Edit account", e.editAccount), newAction("Remove account", e.removeAccount)) } func (e serviceEditor) setName(term *terminal) { e.s.ServiceName = "" OUTER: for { name := term.readName() for _, other := range e.c.Services { if name == other.ServiceName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.s.ServiceName = name return } } func (e serviceEditor) addAccount(term *terminal) { a := &config.Account{} wasSuccess := accountEditor{e.c, e.s, a}.newSetup(term) if wasSuccess { e.s.Accounts = append(e.s.Accounts, a) } else { fmt.Println("Could not add account.") } } func (e serviceEditor) editAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to edit.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) accountEditor{e.c, e.s, e.s.Accounts[i]}.edit(term) } func (e serviceEditor) removeAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to delete.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) e.s.Accounts = append(e.s.Accounts[:i], e.s.Accounts[i+1:]...) } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type oauthServiceCredsEditor struct { o *config.OauthServiceCreds } func (e oauthServiceCredsEditor) newSetup(term *terminal)
func (e oauthServiceCredsEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Client Id", e.setClientId), newAction("Edit Client Secret", e.setClientSecret), newAction("Edit Auth Url", e.setAuthURL), newAction("Edit Token Url", e.setTokenURL), newAction("Edit Scopes", e.setScopes), ) } func (e oauthServiceCredsEditor) setClientId(term *terminal) { fmt.Printf("Enter the client id> ") e.o.ClientID = term.readSimpleString() } func (e oauthServiceCredsEditor) setClientSecret(term *terminal) { fmt.Printf("Enter the client secret> ") e.o.ClientSecret = term.readSimpleString() } func (e oauthServiceCredsEditor) setAuthURL(term *terminal) { e.o.AuthURL = term.readUrl("Auth URL") } func (e oauthServiceCredsEditor) setTokenURL(term *terminal) { e.o.TokenURL = term.readUrl("Token URL") } func (e oauthServiceCredsEditor) setScopes(term *terminal) { fmt.Printf("Enter scopes (comma seperated)> ") e.o.Scopes = term.readStringList() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type accountEditor struct { c *config.Config s *config.Service a *config.Account } func (e accountEditor) newSetup(term *terminal) bool { e.setName(term) e.setServiceUrl(term) e.generateNewClientCreds(term) return e.generateNewOauthAccountCreds(term) } func (e accountEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Name", confirmRename(e.setName)), newAction("Edit Service Url", e.setServiceUrl), newAction("Generate New Client Credentials", confirmNewClientCredentials(e.generateNewClientCreds)), newAction("Reauthorize account", func(t *terminal) { e.generateNewOauthAccountCreds(t) }), ) } func (e accountEditor) setName(term *terminal) { e.a.AccountName = "" OUTER: for { name := term.readName() for _, other := range e.s.Accounts { if name == other.AccountName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.a.AccountName = name return } } func (e accountEditor) setServiceUrl(term *terminal) { e.a.ServiceURL = term.readUrl("Service URL") } func (e accountEditor) generateNewClientCreds(term *terminal) { fmt.Println("Generating new secret for client credentials.") for i := 0; i < 10; i++ { privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { fmt.Println("error generating key: ", err) fmt.Println("Trying again") continue } bytes, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { fmt.Println("error marshling key: ", err) fmt.Println("Trying again") continue } creds := config.ClientCreds{ Protocol: "ECDSA_SHA256_PKCS8_V1", PrivateKey: base64.StdEncoding.EncodeToString(bytes), } e.a.ClientCreds = &creds return } fmt.Println("Too many failures trying to create client credentials, exiting without saving.") os.Exit(1) } func (e accountEditor) generateNewOauthAccountCreds(term *terminal) bool { var endpoint = oauth2.Endpoint{ AuthURL: e.s.OauthServiceCreds.AuthURL, TokenURL: e.s.OauthServiceCreds.TokenURL, } redirectUrl, err := url.Parse(e.c.Url) if err != nil { fmt.Println("Web-Api-Gatway url setting is invalid, can't continue.") return false } redirectUrl.Path = "/authToken/" oauthConf := &oauth2.Config{ ClientID: e.s.OauthServiceCreds.ClientID, ClientSecret: e.s.OauthServiceCreds.ClientSecret, Scopes: e.s.OauthServiceCreds.Scopes, Endpoint: endpoint, RedirectURL: redirectUrl.String(), } for { state, err := generateRandomString() if err != nil { fmt.Println("Problem with random number generation. Can't continue.") return false } authUrl := oauthConf.AuthCodeURL(state) fmt.Println("Please go to this url and authorize the application:") fmt.Println(authUrl) fmt.Printf("Enter the code here> ") encodedAuthCode := term.readSimpleString() jsonAuthCode, err := base64.StdEncoding.DecodeString(encodedAuthCode) if err != nil { fmt.Println("Bad decode") continue } j := struct { Token string State string }{} json.Unmarshal(jsonAuthCode, &j) if j.State != state { fmt.Printf("Bad state. Expected %s, got %s\n", state, j.State) continue } token, err := oauthConf.Exchange(context.Background(), j.Token) if err == nil { e.a.OauthAccountCreds = token fmt.Println("Successfully authorized.") return true } fmt.Println(err) fmt.Println("Please try again.") } } func generateRandomString() (string, error) { b := make([]byte, 30) _, err := rand.Read(b) if err != nil { return "", err } return fmt.Sprintf("%x", b), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func confirmRename(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Editing a name will break existing connections. Only do this if you're really ok with fixing everything! Continue with rename? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling name edit.") } } } func confirmNewClientCredentials(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Creating new credentails will break existing connections. Only do this if you're really ok with fixing everything! Continue? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling...") } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type terminal struct { scanner *bufio.Scanner } func newRealTerminal() *terminal { term := terminal{} term.scanner = bufio.NewScanner(os.Stdin) return &term } func (term *terminal) readName() string { for { fmt.Printf("Choose a name (only use lowercase letters, numbers, and dashes)> ") rawText := term.readSimpleString() if rawText == "" { fmt.Println("Name cannot be empty.") continue } match, _ := regexp.MatchString("^[-a-z0-9]+$", rawText) if !match { fmt.Println("That name contains invalid characters.") continue } return rawText } } func (term *terminal) readUrl(urlDescription string) string { for { fmt.Printf("Enter the %s> ", urlDescription) rawText := term.readSimpleString() tokenURL, err := url.ParseRequestURI(rawText) if err != nil { fmt.Println(rawText + " is not a valid URL (include https://)") continue } if tokenURL.Scheme != "https" { fmt.Println(rawText + " does not use https.") continue } return tokenURL.String() } } func (term *terminal) readSimpleString() string { term.scanner.Scan() return strings.TrimSpace(term.scanner.Text()) } func (term *terminal) readStringList() []string { list := strings.Split(term.readSimpleString(), ",") for i := range list { list[i] = strings.TrimSpace(list[i]) } return list } func (term *terminal) readBoolean(prompt string) bool { for { fmt.Printf(prompt) rawText := term.readSimpleString() if rawText == "yes" { return true } if rawText == "no" { return false } fmt.Println("'" + rawText + "' is not 'yes' or 'no'. Please enter a valid option") } } func (term *terminal) readChoice(namer func(int) string, length int) int { for i := 0; i < length; i++ { fmt.Printf("[%d]: %s\n", i, namer(i)) } for { fmt.Printf("Choose an option> ") rawText := term.readSimpleString() i, err := strconv.Atoi(rawText) if err == nil && i >= 0 && i < length { return i } fmt.Println(rawText + " is not a valid option. Enter only the number of the option.") } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type action struct { displayText string f actionFunc } type actionFunc func(*terminal) func newAction(displayText string, f actionFunc) *action { return &action{displayText, f} } func takeActionLoop(term *terminal, actions ...*action) { keepLoop := true back := newAction("Back", func(term *terminal) { keepLoop = false }) actions = append([]*action{back}, actions...) for keepLoop { takeAction(term, actions...) } } func takeAction(term *terminal, actions ...*action) { i := term.readChoice(func(j int) string { return actions[j].displayText }, len(actions)) actions[i].f(term) }
{ e.setClientId(term) e.setClientSecret(term) e.setAuthURL(term) e.setTokenURL(term) e.setScopes(term) }
identifier_body
setuptool.go
/* Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // The command line tool for setting up services and accounts. package main import ( "bufio" "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/x509" "encoding/base64" "encoding/json" "flag" "fmt" "net/url" "os" "regexp" "strconv" "strings" "github.com/google/web-api-gateway/config" "golang.org/x/oauth2" ) func main() { flag.Parse() fmt.Printf("Welcome to the Web Api Gateway Config Tool.\n\n") term := newRealTerminal() c, save, err := config.ReadWriteConfig() if err != nil { fmt.Printf("Unable to load config file: %v\n", err) os.Exit(1) } configEditor{c}.edit(term) fmt.Println("Saving...") for { err = save() if err == nil { fmt.Println("Save successful!") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("Remember to restart the server.") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") return } fmt.Printf("There was an error saving the config file: %v\n", err) tryAgain := term.readBoolean("Do you want to try again? (no to lose all changes and exit.)") if !tryAgain { return } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type configEditor struct { c *config.Config } func (e configEditor) edit(term *terminal) { if e.c.Url == ""
takeActionLoop(term, newAction("Retrieve Account Key", e.retrieveAccountKey), newAction("Edit Web Api Gateway Url", e.editUrl), newAction("Add service", e.addService), newAction("Edit service (including adding new accounts to an existing service)", e.editService), newAction("Delete service", e.removeService)) } func (e configEditor) editUrl(term *terminal) { e.c.Url = term.readUrl("Web Api Gateway Url") } func (e configEditor) addService(term *terminal) { s := &config.Service{} serviceEditor{e.c, s}.newSetup(term) e.c.Services = append(e.c.Services, s) } func (e configEditor) editService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to edit.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) serviceEditor{e.c, e.c.Services[i]}.edit(term) } func (e configEditor) removeService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to delete.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) e.c.Services = append(e.c.Services[:i], e.c.Services[i+1:]...) } func (e configEditor) retrieveAccountKey(term *terminal) { fmt.Println("Select which account:") type accountSpecific struct { s *config.Service a *config.Account } allAccounts := make([]accountSpecific, 0) for _, s := range e.c.Services { for _, a := range s.Accounts { allAccounts = append(allAccounts, accountSpecific{s, a}) } } if len(allAccounts) == 0 { fmt.Println("You must create accounts first!") return } namer := func(i int) string { return allAccounts[i].s.ServiceName + "/" + allAccounts[i].a.AccountName } i := term.readChoice(namer, len(allAccounts)) key, err := createAccountKey(e.c, allAccounts[i].s, allAccounts[i].a) if err != nil { fmt.Println("Error creating account key:") fmt.Println(err) return } fmt.Println() fmt.Println("Copy and paste everything from (and including) KEYBEGIN to KEYEND") fmt.Println() fmt.Println(key) fmt.Println() fmt.Println() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func createAccountKey(c *config.Config, s *config.Service, a *config.Account) (string, error) { j := struct { WebGatewayUrl string Protocol string PrivateKey string }{ WebGatewayUrl: c.Url, Protocol: a.ClientCreds.Protocol, PrivateKey: a.ClientCreds.PrivateKey, } b, err := json.Marshal(j) if err != nil { return "", err } inner := base64.StdEncoding.EncodeToString(b) return fmt.Sprintf("KEYBEGIN_%s/%s_%s_KEYEND", s.ServiceName, a.AccountName, inner), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type serviceEditor struct { c *config.Config s *config.Service } func (e serviceEditor) newSetup(term *terminal) { e.setName(term) e.s.OauthServiceCreds = new(config.OauthServiceCreds) oauthServiceCredsEditor{e.s.OauthServiceCreds}.newSetup(term) e.edit(term) // Give user chance to add accounts right away } func (e serviceEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit name", confirmRename(e.setName)), newAction("Edit OAuth credentials", (&oauthServiceCredsEditor{e.s.OauthServiceCreds}).edit), newAction("Add new account", e.addAccount), newAction("Edit account", e.editAccount), newAction("Remove account", e.removeAccount)) } func (e serviceEditor) setName(term *terminal) { e.s.ServiceName = "" OUTER: for { name := term.readName() for _, other := range e.c.Services { if name == other.ServiceName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.s.ServiceName = name return } } func (e serviceEditor) addAccount(term *terminal) { a := &config.Account{} wasSuccess := accountEditor{e.c, e.s, a}.newSetup(term) if wasSuccess { e.s.Accounts = append(e.s.Accounts, a) } else { fmt.Println("Could not add account.") } } func (e serviceEditor) editAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to edit.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) accountEditor{e.c, e.s, e.s.Accounts[i]}.edit(term) } func (e serviceEditor) removeAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to delete.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) e.s.Accounts = append(e.s.Accounts[:i], e.s.Accounts[i+1:]...) } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type oauthServiceCredsEditor struct { o *config.OauthServiceCreds } func (e oauthServiceCredsEditor) newSetup(term *terminal) { e.setClientId(term) e.setClientSecret(term) e.setAuthURL(term) e.setTokenURL(term) e.setScopes(term) } func (e oauthServiceCredsEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Client Id", e.setClientId), newAction("Edit Client Secret", e.setClientSecret), newAction("Edit Auth Url", e.setAuthURL), newAction("Edit Token Url", e.setTokenURL), newAction("Edit Scopes", e.setScopes), ) } func (e oauthServiceCredsEditor) setClientId(term *terminal) { fmt.Printf("Enter the client id> ") e.o.ClientID = term.readSimpleString() } func (e oauthServiceCredsEditor) setClientSecret(term *terminal) { fmt.Printf("Enter the client secret> ") e.o.ClientSecret = term.readSimpleString() } func (e oauthServiceCredsEditor) setAuthURL(term *terminal) { e.o.AuthURL = term.readUrl("Auth URL") } func (e oauthServiceCredsEditor) setTokenURL(term *terminal) { e.o.TokenURL = term.readUrl("Token URL") } func (e oauthServiceCredsEditor) setScopes(term *terminal) { fmt.Printf("Enter scopes (comma seperated)> ") e.o.Scopes = term.readStringList() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type accountEditor struct { c *config.Config s *config.Service a *config.Account } func (e accountEditor) newSetup(term *terminal) bool { e.setName(term) e.setServiceUrl(term) e.generateNewClientCreds(term) return e.generateNewOauthAccountCreds(term) } func (e accountEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Name", confirmRename(e.setName)), newAction("Edit Service Url", e.setServiceUrl), newAction("Generate New Client Credentials", confirmNewClientCredentials(e.generateNewClientCreds)), newAction("Reauthorize account", func(t *terminal) { e.generateNewOauthAccountCreds(t) }), ) } func (e accountEditor) setName(term *terminal) { e.a.AccountName = "" OUTER: for { name := term.readName() for _, other := range e.s.Accounts { if name == other.AccountName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.a.AccountName = name return } } func (e accountEditor) setServiceUrl(term *terminal) { e.a.ServiceURL = term.readUrl("Service URL") } func (e accountEditor) generateNewClientCreds(term *terminal) { fmt.Println("Generating new secret for client credentials.") for i := 0; i < 10; i++ { privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { fmt.Println("error generating key: ", err) fmt.Println("Trying again") continue } bytes, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { fmt.Println("error marshling key: ", err) fmt.Println("Trying again") continue } creds := config.ClientCreds{ Protocol: "ECDSA_SHA256_PKCS8_V1", PrivateKey: base64.StdEncoding.EncodeToString(bytes), } e.a.ClientCreds = &creds return } fmt.Println("Too many failures trying to create client credentials, exiting without saving.") os.Exit(1) } func (e accountEditor) generateNewOauthAccountCreds(term *terminal) bool { var endpoint = oauth2.Endpoint{ AuthURL: e.s.OauthServiceCreds.AuthURL, TokenURL: e.s.OauthServiceCreds.TokenURL, } redirectUrl, err := url.Parse(e.c.Url) if err != nil { fmt.Println("Web-Api-Gatway url setting is invalid, can't continue.") return false } redirectUrl.Path = "/authToken/" oauthConf := &oauth2.Config{ ClientID: e.s.OauthServiceCreds.ClientID, ClientSecret: e.s.OauthServiceCreds.ClientSecret, Scopes: e.s.OauthServiceCreds.Scopes, Endpoint: endpoint, RedirectURL: redirectUrl.String(), } for { state, err := generateRandomString() if err != nil { fmt.Println("Problem with random number generation. Can't continue.") return false } authUrl := oauthConf.AuthCodeURL(state) fmt.Println("Please go to this url and authorize the application:") fmt.Println(authUrl) fmt.Printf("Enter the code here> ") encodedAuthCode := term.readSimpleString() jsonAuthCode, err := base64.StdEncoding.DecodeString(encodedAuthCode) if err != nil { fmt.Println("Bad decode") continue } j := struct { Token string State string }{} json.Unmarshal(jsonAuthCode, &j) if j.State != state { fmt.Printf("Bad state. Expected %s, got %s\n", state, j.State) continue } token, err := oauthConf.Exchange(context.Background(), j.Token) if err == nil { e.a.OauthAccountCreds = token fmt.Println("Successfully authorized.") return true } fmt.Println(err) fmt.Println("Please try again.") } } func generateRandomString() (string, error) { b := make([]byte, 30) _, err := rand.Read(b) if err != nil { return "", err } return fmt.Sprintf("%x", b), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func confirmRename(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Editing a name will break existing connections. Only do this if you're really ok with fixing everything! Continue with rename? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling name edit.") } } } func confirmNewClientCredentials(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Creating new credentails will break existing connections. Only do this if you're really ok with fixing everything! Continue? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling...") } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type terminal struct { scanner *bufio.Scanner } func newRealTerminal() *terminal { term := terminal{} term.scanner = bufio.NewScanner(os.Stdin) return &term } func (term *terminal) readName() string { for { fmt.Printf("Choose a name (only use lowercase letters, numbers, and dashes)> ") rawText := term.readSimpleString() if rawText == "" { fmt.Println("Name cannot be empty.") continue } match, _ := regexp.MatchString("^[-a-z0-9]+$", rawText) if !match { fmt.Println("That name contains invalid characters.") continue } return rawText } } func (term *terminal) readUrl(urlDescription string) string { for { fmt.Printf("Enter the %s> ", urlDescription) rawText := term.readSimpleString() tokenURL, err := url.ParseRequestURI(rawText) if err != nil { fmt.Println(rawText + " is not a valid URL (include https://)") continue } if tokenURL.Scheme != "https" { fmt.Println(rawText + " does not use https.") continue } return tokenURL.String() } } func (term *terminal) readSimpleString() string { term.scanner.Scan() return strings.TrimSpace(term.scanner.Text()) } func (term *terminal) readStringList() []string { list := strings.Split(term.readSimpleString(), ",") for i := range list { list[i] = strings.TrimSpace(list[i]) } return list } func (term *terminal) readBoolean(prompt string) bool { for { fmt.Printf(prompt) rawText := term.readSimpleString() if rawText == "yes" { return true } if rawText == "no" { return false } fmt.Println("'" + rawText + "' is not 'yes' or 'no'. Please enter a valid option") } } func (term *terminal) readChoice(namer func(int) string, length int) int { for i := 0; i < length; i++ { fmt.Printf("[%d]: %s\n", i, namer(i)) } for { fmt.Printf("Choose an option> ") rawText := term.readSimpleString() i, err := strconv.Atoi(rawText) if err == nil && i >= 0 && i < length { return i } fmt.Println(rawText + " is not a valid option. Enter only the number of the option.") } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type action struct { displayText string f actionFunc } type actionFunc func(*terminal) func newAction(displayText string, f actionFunc) *action { return &action{displayText, f} } func takeActionLoop(term *terminal, actions ...*action) { keepLoop := true back := newAction("Back", func(term *terminal) { keepLoop = false }) actions = append([]*action{back}, actions...) for keepLoop { takeAction(term, actions...) } } func takeAction(term *terminal, actions ...*action) { i := term.readChoice(func(j int) string { return actions[j].displayText }, len(actions)) actions[i].f(term) }
{ e.editUrl(term) }
conditional_block
setuptool.go
/* Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // The command line tool for setting up services and accounts. package main import ( "bufio" "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/x509" "encoding/base64" "encoding/json" "flag" "fmt" "net/url" "os" "regexp" "strconv" "strings" "github.com/google/web-api-gateway/config" "golang.org/x/oauth2" ) func main() { flag.Parse() fmt.Printf("Welcome to the Web Api Gateway Config Tool.\n\n") term := newRealTerminal() c, save, err := config.ReadWriteConfig() if err != nil { fmt.Printf("Unable to load config file: %v\n", err) os.Exit(1) } configEditor{c}.edit(term) fmt.Println("Saving...") for { err = save() if err == nil { fmt.Println("Save successful!") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("Remember to restart the server.") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") return } fmt.Printf("There was an error saving the config file: %v\n", err) tryAgain := term.readBoolean("Do you want to try again? (no to lose all changes and exit.)") if !tryAgain { return } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type configEditor struct { c *config.Config } func (e configEditor) edit(term *terminal) { if e.c.Url == "" { e.editUrl(term) } takeActionLoop(term, newAction("Retrieve Account Key", e.retrieveAccountKey), newAction("Edit Web Api Gateway Url", e.editUrl), newAction("Add service", e.addService), newAction("Edit service (including adding new accounts to an existing service)", e.editService), newAction("Delete service", e.removeService)) } func (e configEditor) editUrl(term *terminal) { e.c.Url = term.readUrl("Web Api Gateway Url") } func (e configEditor) addService(term *terminal) { s := &config.Service{} serviceEditor{e.c, s}.newSetup(term) e.c.Services = append(e.c.Services, s) } func (e configEditor) editService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to edit.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) serviceEditor{e.c, e.c.Services[i]}.edit(term) } func (e configEditor) removeService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to delete.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) e.c.Services = append(e.c.Services[:i], e.c.Services[i+1:]...) } func (e configEditor) retrieveAccountKey(term *terminal) { fmt.Println("Select which account:") type accountSpecific struct { s *config.Service a *config.Account } allAccounts := make([]accountSpecific, 0) for _, s := range e.c.Services { for _, a := range s.Accounts { allAccounts = append(allAccounts, accountSpecific{s, a}) } } if len(allAccounts) == 0 { fmt.Println("You must create accounts first!") return } namer := func(i int) string { return allAccounts[i].s.ServiceName + "/" + allAccounts[i].a.AccountName } i := term.readChoice(namer, len(allAccounts)) key, err := createAccountKey(e.c, allAccounts[i].s, allAccounts[i].a) if err != nil { fmt.Println("Error creating account key:") fmt.Println(err) return } fmt.Println() fmt.Println("Copy and paste everything from (and including) KEYBEGIN to KEYEND") fmt.Println() fmt.Println(key) fmt.Println() fmt.Println() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func createAccountKey(c *config.Config, s *config.Service, a *config.Account) (string, error) { j := struct { WebGatewayUrl string Protocol string PrivateKey string }{ WebGatewayUrl: c.Url, Protocol: a.ClientCreds.Protocol, PrivateKey: a.ClientCreds.PrivateKey, } b, err := json.Marshal(j) if err != nil { return "", err } inner := base64.StdEncoding.EncodeToString(b) return fmt.Sprintf("KEYBEGIN_%s/%s_%s_KEYEND", s.ServiceName, a.AccountName, inner), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type serviceEditor struct { c *config.Config s *config.Service } func (e serviceEditor) newSetup(term *terminal) { e.setName(term) e.s.OauthServiceCreds = new(config.OauthServiceCreds) oauthServiceCredsEditor{e.s.OauthServiceCreds}.newSetup(term) e.edit(term) // Give user chance to add accounts right away } func (e serviceEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit name", confirmRename(e.setName)), newAction("Edit OAuth credentials", (&oauthServiceCredsEditor{e.s.OauthServiceCreds}).edit), newAction("Add new account", e.addAccount), newAction("Edit account", e.editAccount), newAction("Remove account", e.removeAccount)) } func (e serviceEditor) setName(term *terminal) { e.s.ServiceName = "" OUTER: for { name := term.readName() for _, other := range e.c.Services { if name == other.ServiceName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.s.ServiceName = name return } } func (e serviceEditor) addAccount(term *terminal) { a := &config.Account{} wasSuccess := accountEditor{e.c, e.s, a}.newSetup(term) if wasSuccess { e.s.Accounts = append(e.s.Accounts, a) } else { fmt.Println("Could not add account.") } } func (e serviceEditor) editAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to edit.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) accountEditor{e.c, e.s, e.s.Accounts[i]}.edit(term) } func (e serviceEditor) removeAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to delete.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) e.s.Accounts = append(e.s.Accounts[:i], e.s.Accounts[i+1:]...) } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type oauthServiceCredsEditor struct { o *config.OauthServiceCreds } func (e oauthServiceCredsEditor) newSetup(term *terminal) { e.setClientId(term) e.setClientSecret(term) e.setAuthURL(term) e.setTokenURL(term) e.setScopes(term) } func (e oauthServiceCredsEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Client Id", e.setClientId), newAction("Edit Client Secret", e.setClientSecret), newAction("Edit Auth Url", e.setAuthURL), newAction("Edit Token Url", e.setTokenURL), newAction("Edit Scopes", e.setScopes), ) } func (e oauthServiceCredsEditor) setClientId(term *terminal) { fmt.Printf("Enter the client id> ") e.o.ClientID = term.readSimpleString() } func (e oauthServiceCredsEditor) setClientSecret(term *terminal) { fmt.Printf("Enter the client secret> ") e.o.ClientSecret = term.readSimpleString() } func (e oauthServiceCredsEditor) setAuthURL(term *terminal) { e.o.AuthURL = term.readUrl("Auth URL") } func (e oauthServiceCredsEditor) setTokenURL(term *terminal) { e.o.TokenURL = term.readUrl("Token URL") } func (e oauthServiceCredsEditor) setScopes(term *terminal) { fmt.Printf("Enter scopes (comma seperated)> ") e.o.Scopes = term.readStringList() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type accountEditor struct { c *config.Config s *config.Service a *config.Account } func (e accountEditor) newSetup(term *terminal) bool { e.setName(term) e.setServiceUrl(term) e.generateNewClientCreds(term) return e.generateNewOauthAccountCreds(term) } func (e accountEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Name", confirmRename(e.setName)), newAction("Edit Service Url", e.setServiceUrl), newAction("Generate New Client Credentials", confirmNewClientCredentials(e.generateNewClientCreds)), newAction("Reauthorize account", func(t *terminal) { e.generateNewOauthAccountCreds(t) }), ) } func (e accountEditor) setName(term *terminal) { e.a.AccountName = "" OUTER: for { name := term.readName() for _, other := range e.s.Accounts { if name == other.AccountName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.a.AccountName = name return } } func (e accountEditor) setServiceUrl(term *terminal) { e.a.ServiceURL = term.readUrl("Service URL") } func (e accountEditor) generateNewClientCreds(term *terminal) { fmt.Println("Generating new secret for client credentials.") for i := 0; i < 10; i++ { privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { fmt.Println("error generating key: ", err) fmt.Println("Trying again") continue } bytes, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { fmt.Println("error marshling key: ", err) fmt.Println("Trying again") continue } creds := config.ClientCreds{ Protocol: "ECDSA_SHA256_PKCS8_V1", PrivateKey: base64.StdEncoding.EncodeToString(bytes), } e.a.ClientCreds = &creds return } fmt.Println("Too many failures trying to create client credentials, exiting without saving.") os.Exit(1) } func (e accountEditor) generateNewOauthAccountCreds(term *terminal) bool { var endpoint = oauth2.Endpoint{ AuthURL: e.s.OauthServiceCreds.AuthURL, TokenURL: e.s.OauthServiceCreds.TokenURL, } redirectUrl, err := url.Parse(e.c.Url) if err != nil { fmt.Println("Web-Api-Gatway url setting is invalid, can't continue.") return false } redirectUrl.Path = "/authToken/" oauthConf := &oauth2.Config{ ClientID: e.s.OauthServiceCreds.ClientID, ClientSecret: e.s.OauthServiceCreds.ClientSecret, Scopes: e.s.OauthServiceCreds.Scopes, Endpoint: endpoint, RedirectURL: redirectUrl.String(), } for { state, err := generateRandomString() if err != nil { fmt.Println("Problem with random number generation. Can't continue.") return false } authUrl := oauthConf.AuthCodeURL(state) fmt.Println("Please go to this url and authorize the application:") fmt.Println(authUrl) fmt.Printf("Enter the code here> ") encodedAuthCode := term.readSimpleString() jsonAuthCode, err := base64.StdEncoding.DecodeString(encodedAuthCode) if err != nil { fmt.Println("Bad decode") continue } j := struct { Token string State string }{} json.Unmarshal(jsonAuthCode, &j) if j.State != state { fmt.Printf("Bad state. Expected %s, got %s\n", state, j.State) continue } token, err := oauthConf.Exchange(context.Background(), j.Token) if err == nil { e.a.OauthAccountCreds = token fmt.Println("Successfully authorized.") return true } fmt.Println(err) fmt.Println("Please try again.") } } func generateRandomString() (string, error) { b := make([]byte, 30) _, err := rand.Read(b) if err != nil { return "", err } return fmt.Sprintf("%x", b), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func
(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Editing a name will break existing connections. Only do this if you're really ok with fixing everything! Continue with rename? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling name edit.") } } } func confirmNewClientCredentials(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Creating new credentails will break existing connections. Only do this if you're really ok with fixing everything! Continue? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling...") } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type terminal struct { scanner *bufio.Scanner } func newRealTerminal() *terminal { term := terminal{} term.scanner = bufio.NewScanner(os.Stdin) return &term } func (term *terminal) readName() string { for { fmt.Printf("Choose a name (only use lowercase letters, numbers, and dashes)> ") rawText := term.readSimpleString() if rawText == "" { fmt.Println("Name cannot be empty.") continue } match, _ := regexp.MatchString("^[-a-z0-9]+$", rawText) if !match { fmt.Println("That name contains invalid characters.") continue } return rawText } } func (term *terminal) readUrl(urlDescription string) string { for { fmt.Printf("Enter the %s> ", urlDescription) rawText := term.readSimpleString() tokenURL, err := url.ParseRequestURI(rawText) if err != nil { fmt.Println(rawText + " is not a valid URL (include https://)") continue } if tokenURL.Scheme != "https" { fmt.Println(rawText + " does not use https.") continue } return tokenURL.String() } } func (term *terminal) readSimpleString() string { term.scanner.Scan() return strings.TrimSpace(term.scanner.Text()) } func (term *terminal) readStringList() []string { list := strings.Split(term.readSimpleString(), ",") for i := range list { list[i] = strings.TrimSpace(list[i]) } return list } func (term *terminal) readBoolean(prompt string) bool { for { fmt.Printf(prompt) rawText := term.readSimpleString() if rawText == "yes" { return true } if rawText == "no" { return false } fmt.Println("'" + rawText + "' is not 'yes' or 'no'. Please enter a valid option") } } func (term *terminal) readChoice(namer func(int) string, length int) int { for i := 0; i < length; i++ { fmt.Printf("[%d]: %s\n", i, namer(i)) } for { fmt.Printf("Choose an option> ") rawText := term.readSimpleString() i, err := strconv.Atoi(rawText) if err == nil && i >= 0 && i < length { return i } fmt.Println(rawText + " is not a valid option. Enter only the number of the option.") } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type action struct { displayText string f actionFunc } type actionFunc func(*terminal) func newAction(displayText string, f actionFunc) *action { return &action{displayText, f} } func takeActionLoop(term *terminal, actions ...*action) { keepLoop := true back := newAction("Back", func(term *terminal) { keepLoop = false }) actions = append([]*action{back}, actions...) for keepLoop { takeAction(term, actions...) } } func takeAction(term *terminal, actions ...*action) { i := term.readChoice(func(j int) string { return actions[j].displayText }, len(actions)) actions[i].f(term) }
confirmRename
identifier_name
setuptool.go
/* Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // The command line tool for setting up services and accounts. package main import ( "bufio" "context" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/x509" "encoding/base64" "encoding/json" "flag" "fmt" "net/url" "os" "regexp" "strconv" "strings" "github.com/google/web-api-gateway/config" "golang.org/x/oauth2" ) func main() { flag.Parse() fmt.Printf("Welcome to the Web Api Gateway Config Tool.\n\n") term := newRealTerminal() c, save, err := config.ReadWriteConfig() if err != nil { fmt.Printf("Unable to load config file: %v\n", err) os.Exit(1) } configEditor{c}.edit(term) fmt.Println("Saving...") for { err = save() if err == nil { fmt.Println("Save successful!") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("Remember to restart the server.") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") return } fmt.Printf("There was an error saving the config file: %v\n", err) tryAgain := term.readBoolean("Do you want to try again? (no to lose all changes and exit.)") if !tryAgain { return } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type configEditor struct { c *config.Config } func (e configEditor) edit(term *terminal) { if e.c.Url == "" { e.editUrl(term) } takeActionLoop(term, newAction("Retrieve Account Key", e.retrieveAccountKey), newAction("Edit Web Api Gateway Url", e.editUrl), newAction("Add service", e.addService), newAction("Edit service (including adding new accounts to an existing service)", e.editService), newAction("Delete service", e.removeService)) } func (e configEditor) editUrl(term *terminal) { e.c.Url = term.readUrl("Web Api Gateway Url") } func (e configEditor) addService(term *terminal) { s := &config.Service{} serviceEditor{e.c, s}.newSetup(term) e.c.Services = append(e.c.Services, s) } func (e configEditor) editService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to edit.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) serviceEditor{e.c, e.c.Services[i]}.edit(term) } func (e configEditor) removeService(term *terminal) { if len(e.c.Services) == 0 { fmt.Println("There are no services to delete.") return } fmt.Println("Services:") namer := func(i int) string { return e.c.Services[i].ServiceName } i := term.readChoice(namer, len(e.c.Services)) e.c.Services = append(e.c.Services[:i], e.c.Services[i+1:]...) } func (e configEditor) retrieveAccountKey(term *terminal) { fmt.Println("Select which account:") type accountSpecific struct { s *config.Service a *config.Account } allAccounts := make([]accountSpecific, 0) for _, s := range e.c.Services { for _, a := range s.Accounts { allAccounts = append(allAccounts, accountSpecific{s, a}) } } if len(allAccounts) == 0 { fmt.Println("You must create accounts first!") return } namer := func(i int) string { return allAccounts[i].s.ServiceName + "/" + allAccounts[i].a.AccountName } i := term.readChoice(namer, len(allAccounts)) key, err := createAccountKey(e.c, allAccounts[i].s, allAccounts[i].a) if err != nil { fmt.Println("Error creating account key:") fmt.Println(err) return } fmt.Println() fmt.Println("Copy and paste everything from (and including) KEYBEGIN to KEYEND") fmt.Println() fmt.Println(key) fmt.Println() fmt.Println() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func createAccountKey(c *config.Config, s *config.Service, a *config.Account) (string, error) { j := struct { WebGatewayUrl string Protocol string PrivateKey string }{ WebGatewayUrl: c.Url, Protocol: a.ClientCreds.Protocol, PrivateKey: a.ClientCreds.PrivateKey, } b, err := json.Marshal(j) if err != nil { return "", err } inner := base64.StdEncoding.EncodeToString(b) return fmt.Sprintf("KEYBEGIN_%s/%s_%s_KEYEND", s.ServiceName, a.AccountName, inner), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type serviceEditor struct { c *config.Config s *config.Service } func (e serviceEditor) newSetup(term *terminal) { e.setName(term) e.s.OauthServiceCreds = new(config.OauthServiceCreds) oauthServiceCredsEditor{e.s.OauthServiceCreds}.newSetup(term) e.edit(term) // Give user chance to add accounts right away } func (e serviceEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit name", confirmRename(e.setName)), newAction("Edit OAuth credentials", (&oauthServiceCredsEditor{e.s.OauthServiceCreds}).edit), newAction("Add new account", e.addAccount), newAction("Edit account", e.editAccount), newAction("Remove account", e.removeAccount)) } func (e serviceEditor) setName(term *terminal) { e.s.ServiceName = "" OUTER: for { name := term.readName() for _, other := range e.c.Services { if name == other.ServiceName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.s.ServiceName = name return } } func (e serviceEditor) addAccount(term *terminal) { a := &config.Account{} wasSuccess := accountEditor{e.c, e.s, a}.newSetup(term) if wasSuccess { e.s.Accounts = append(e.s.Accounts, a) } else { fmt.Println("Could not add account.") } } func (e serviceEditor) editAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to edit.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) accountEditor{e.c, e.s, e.s.Accounts[i]}.edit(term) } func (e serviceEditor) removeAccount(term *terminal) { if len(e.s.Accounts) == 0 { fmt.Println("There are no accounts to delete.") return } fmt.Println("Accounts:") namer := func(i int) string { return e.s.Accounts[i].AccountName } i := term.readChoice(namer, len(e.s.Accounts)) e.s.Accounts = append(e.s.Accounts[:i], e.s.Accounts[i+1:]...) } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type oauthServiceCredsEditor struct { o *config.OauthServiceCreds } func (e oauthServiceCredsEditor) newSetup(term *terminal) { e.setClientId(term) e.setClientSecret(term) e.setAuthURL(term) e.setTokenURL(term) e.setScopes(term) } func (e oauthServiceCredsEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Client Id", e.setClientId), newAction("Edit Client Secret", e.setClientSecret), newAction("Edit Auth Url", e.setAuthURL), newAction("Edit Token Url", e.setTokenURL), newAction("Edit Scopes", e.setScopes), ) } func (e oauthServiceCredsEditor) setClientId(term *terminal) { fmt.Printf("Enter the client id> ") e.o.ClientID = term.readSimpleString() } func (e oauthServiceCredsEditor) setClientSecret(term *terminal) { fmt.Printf("Enter the client secret> ") e.o.ClientSecret = term.readSimpleString() } func (e oauthServiceCredsEditor) setAuthURL(term *terminal) { e.o.AuthURL = term.readUrl("Auth URL") } func (e oauthServiceCredsEditor) setTokenURL(term *terminal) { e.o.TokenURL = term.readUrl("Token URL") } func (e oauthServiceCredsEditor) setScopes(term *terminal) { fmt.Printf("Enter scopes (comma seperated)> ") e.o.Scopes = term.readStringList() } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type accountEditor struct { c *config.Config s *config.Service a *config.Account } func (e accountEditor) newSetup(term *terminal) bool { e.setName(term) e.setServiceUrl(term) e.generateNewClientCreds(term) return e.generateNewOauthAccountCreds(term) } func (e accountEditor) edit(term *terminal) { takeActionLoop(term, newAction("Edit Name", confirmRename(e.setName)), newAction("Edit Service Url", e.setServiceUrl), newAction("Generate New Client Credentials", confirmNewClientCredentials(e.generateNewClientCreds)), newAction("Reauthorize account", func(t *terminal) { e.generateNewOauthAccountCreds(t) }), ) } func (e accountEditor) setName(term *terminal) { e.a.AccountName = "" OUTER: for { name := term.readName() for _, other := range e.s.Accounts { if name == other.AccountName { fmt.Println("That service name is already in use. Choose another.") continue OUTER } } e.a.AccountName = name return } } func (e accountEditor) setServiceUrl(term *terminal) { e.a.ServiceURL = term.readUrl("Service URL") } func (e accountEditor) generateNewClientCreds(term *terminal) { fmt.Println("Generating new secret for client credentials.") for i := 0; i < 10; i++ { privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { fmt.Println("error generating key: ", err) fmt.Println("Trying again") continue } bytes, err := x509.MarshalPKCS8PrivateKey(privateKey) if err != nil { fmt.Println("error marshling key: ", err) fmt.Println("Trying again") continue } creds := config.ClientCreds{ Protocol: "ECDSA_SHA256_PKCS8_V1", PrivateKey: base64.StdEncoding.EncodeToString(bytes), } e.a.ClientCreds = &creds return } fmt.Println("Too many failures trying to create client credentials, exiting without saving.") os.Exit(1) } func (e accountEditor) generateNewOauthAccountCreds(term *terminal) bool { var endpoint = oauth2.Endpoint{ AuthURL: e.s.OauthServiceCreds.AuthURL, TokenURL: e.s.OauthServiceCreds.TokenURL, } redirectUrl, err := url.Parse(e.c.Url) if err != nil { fmt.Println("Web-Api-Gatway url setting is invalid, can't continue.") return false } redirectUrl.Path = "/authToken/" oauthConf := &oauth2.Config{ ClientID: e.s.OauthServiceCreds.ClientID, ClientSecret: e.s.OauthServiceCreds.ClientSecret, Scopes: e.s.OauthServiceCreds.Scopes, Endpoint: endpoint, RedirectURL: redirectUrl.String(), } for { state, err := generateRandomString() if err != nil { fmt.Println("Problem with random number generation. Can't continue.") return false } authUrl := oauthConf.AuthCodeURL(state) fmt.Println("Please go to this url and authorize the application:") fmt.Println(authUrl) fmt.Printf("Enter the code here> ")
fmt.Println("Bad decode") continue } j := struct { Token string State string }{} json.Unmarshal(jsonAuthCode, &j) if j.State != state { fmt.Printf("Bad state. Expected %s, got %s\n", state, j.State) continue } token, err := oauthConf.Exchange(context.Background(), j.Token) if err == nil { e.a.OauthAccountCreds = token fmt.Println("Successfully authorized.") return true } fmt.Println(err) fmt.Println("Please try again.") } } func generateRandomString() (string, error) { b := make([]byte, 30) _, err := rand.Read(b) if err != nil { return "", err } return fmt.Sprintf("%x", b), nil } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// func confirmRename(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Editing a name will break existing connections. Only do this if you're really ok with fixing everything! Continue with rename? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling name edit.") } } } func confirmNewClientCredentials(f actionFunc) actionFunc { return func(term *terminal) { if term.readBoolean("Creating new credentails will break existing connections. Only do this if you're really ok with fixing everything! Continue? (yes/no)> ") { f(term) } else { fmt.Println("Cancelling...") } } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type terminal struct { scanner *bufio.Scanner } func newRealTerminal() *terminal { term := terminal{} term.scanner = bufio.NewScanner(os.Stdin) return &term } func (term *terminal) readName() string { for { fmt.Printf("Choose a name (only use lowercase letters, numbers, and dashes)> ") rawText := term.readSimpleString() if rawText == "" { fmt.Println("Name cannot be empty.") continue } match, _ := regexp.MatchString("^[-a-z0-9]+$", rawText) if !match { fmt.Println("That name contains invalid characters.") continue } return rawText } } func (term *terminal) readUrl(urlDescription string) string { for { fmt.Printf("Enter the %s> ", urlDescription) rawText := term.readSimpleString() tokenURL, err := url.ParseRequestURI(rawText) if err != nil { fmt.Println(rawText + " is not a valid URL (include https://)") continue } if tokenURL.Scheme != "https" { fmt.Println(rawText + " does not use https.") continue } return tokenURL.String() } } func (term *terminal) readSimpleString() string { term.scanner.Scan() return strings.TrimSpace(term.scanner.Text()) } func (term *terminal) readStringList() []string { list := strings.Split(term.readSimpleString(), ",") for i := range list { list[i] = strings.TrimSpace(list[i]) } return list } func (term *terminal) readBoolean(prompt string) bool { for { fmt.Printf(prompt) rawText := term.readSimpleString() if rawText == "yes" { return true } if rawText == "no" { return false } fmt.Println("'" + rawText + "' is not 'yes' or 'no'. Please enter a valid option") } } func (term *terminal) readChoice(namer func(int) string, length int) int { for i := 0; i < length; i++ { fmt.Printf("[%d]: %s\n", i, namer(i)) } for { fmt.Printf("Choose an option> ") rawText := term.readSimpleString() i, err := strconv.Atoi(rawText) if err == nil && i >= 0 && i < length { return i } fmt.Println(rawText + " is not a valid option. Enter only the number of the option.") } } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// type action struct { displayText string f actionFunc } type actionFunc func(*terminal) func newAction(displayText string, f actionFunc) *action { return &action{displayText, f} } func takeActionLoop(term *terminal, actions ...*action) { keepLoop := true back := newAction("Back", func(term *terminal) { keepLoop = false }) actions = append([]*action{back}, actions...) for keepLoop { takeAction(term, actions...) } } func takeAction(term *terminal, actions ...*action) { i := term.readChoice(func(j int) string { return actions[j].displayText }, len(actions)) actions[i].f(term) }
encodedAuthCode := term.readSimpleString() jsonAuthCode, err := base64.StdEncoding.DecodeString(encodedAuthCode) if err != nil {
random_line_split
myAgents.py
# myAgents.py # --------------- # Licensing Information: You are free to use or extend these projects for # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # Student side autograding was added by Brad Miller, Nick Hay, and # Pieter Abbeel (pabbeel@cs.berkeley.edu). from game import Agent from game import Directions from game import Actions from searchProblems import PositionSearchProblem import util import time import search """ IMPORTANT `agent` defines which agent you will use. By default, it is set to ClosestDotAgent, but when you're ready to test your own agent, replace it with MyAgent """ def createAgents(num_pacmen, agent='MyAgent'): return [eval(agent)(index=i) for i in range(num_pacmen)] foodCount = 1 north = Directions.NORTH south = Directions.SOUTH east = Directions.EAST west = Directions.WEST reverse = {north:south, south:north, east:west, west:east} class MyAgent(Agent): """ Implementation of your agent. """ customFood = None foodLeft = 0 specialWalls = {} finding = [] def getAction(self, state): """ Returns the next action the agent will take """ "*** YOUR CODE HERE ***" x, y = state.getPacmanPosition(self.index) numPacmen = state.getNumPacmanAgents() if not MyAgent.customFood: MyAgent.customFood = state.getFood() MyAgent.foodLeft = len(MyAgent.customFood.asList()) #if not self.foodIsThere(x, y): # self.path = None #trueLen = len(state.getFood().asList()) #if not self.path and self.index < trueLen and trueLen < numPacmen: # problem = MySearchProblem(state, self.index, 1, state.getFood()) # self.path = search.bfs(problem) if self.path and self.path[0] == 'place': if sum(MyAgent.finding) == 1: MyAgent.specialWalls[(x, y)] = self.path[1] self.path = None if not self.path and MyAgent.foodLeft > 0: problem = MySearchProblem(state, self.index, min(foodCount, MyAgent.foodLeft), MyAgent.customFood, MyAgent.specialWalls, MyAgent.finding) self.path = cbfs(problem) nx, ny = x, y if not self.path: return state.getLegalActions(self.index)[0] for i in range(len(self.path)): action = self.path[i] if action == 'place': MyAgent.finding[self.index] = False break MyAgent.finding[self.index] = True dx, dy = Actions.directionToVector(action) nx, ny = int(nx + dx), int(ny + dy) check = MyAgent.customFood[nx][ny] if check: MyAgent.foodLeft -= 1 MyAgent.customFood[nx][ny] = False if not self.path: return state.getLegalActions(self.index)[0] dir = self.path.pop(0) return dir def initialize(self): """ Intialize anything you want to here. This function is called when the agent is first created. If you don't need to use it, then leave it blank """ "*** YOUR CODE HERE" self.path = [] MyAgent.customFood = None MyAgent.foodLeft = 0 MyAgent.specialWalls = {} self.followOne = False if self.index == 0: MyAgent.finding = [] MyAgent.finding.append(False) """ Put any other SearchProblems or search methods below. You may also import classes/methods in search.py and searchProblems.py. (ClosestDotAgent as an example below) """ class MySearchProblem: def __init__(self, gameState, agentIndex, numFood, customFoodGrid, specialWalls, finding): self.walls = gameState.getWalls() self.position = gameState.getPacmanPosition(agentIndex) self.nOfAgents = gameState.getNumPacmanAgents() self.specialWalls = specialWalls self.pacmen = [gameState.getPacmanPosition(i) for i in range(self.nOfAgents)] self.finding = finding self.food = customFoodGrid self.numFood = numFood self.index = agentIndex def getStartState(self): """ Returns the start state for the search problem. """ #return (self.position, self.food.copy()) return self.position def isGoalState(self, state): """ state: Search state Returns True if and only if the state is a valid goal state. """ x, y = state return self.food[x][y] def isPacman(self, state): x, y = state if sum(self.finding) > 1: return False for i in range(self.nOfAgents): a, b = self.pacmen[i] if i != self.index and self.finding[i] and x == a and y == b: return True return False #def getID(self, state): # x, y = state # i def getSuccessors(self, state): """ state: Search state For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the action required to get there, and 'stepCost' is the incremental cost of expanding to that successor. """ successors = [] for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x,y = state #foodV = list(state[1]) dx, dy = Actions.directionToVector(direction) nextx, nexty = int(x + dx), int(y + dy) pos = (nextx, nexty) myBlock = False if pos in self.specialWalls: myBlock = self.specialWalls[pos] == direction if not self.walls[nextx][nexty] and not myBlock and not self.isPacman(state): #if self.food[nextx][nexty] and not repeat: #foodV.append((nextx, nexty)) #foodV = tuple(foodV) successors.append( ( (nextx, nexty), direction, 1 ) ) return successors def getCostOfActions(self, actions): """ actions: A list of actions to take This method returns the total cost of a particular sequence of actions. The sequence must be composed of legal moves. """ x,y= self.position cost = 0 for action in actions: # figure out the next state and see whether it's legal dx, dy = Actions.directionToVector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += 1 return cost class ClosestDotAgent(Agent): def findPathToClosestDot(self, gameState):
def getAction(self, state): return self.findPathToClosestDot(state)[0] class AnyFoodSearchProblem(PositionSearchProblem): """ A search problem for finding a path to any food. This search problem is just like the PositionSearchProblem, but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. The class definition above, AnyFoodSearchProblem(PositionSearchProblem), inherits the methods of the PositionSearchProblem. You can use this search problem to help you fill in the findPathToClosestDot method. """ def __init__(self, gameState, agentIndex): "Stores information from the gameState. You don't need to change this." # Store the food for later reference self.food = gameState.getFood() # Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition(agentIndex) self.costFn = lambda x: 1 self._visited, self._visitedlist, self._expanded = {}, [], 0 # DO NOT CHANGE def isGoalState(self, state): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. """ x,y = state "*** YOUR CODE HERE ***" return self.food[x][y] def customBreadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" i = 0 dirList = [] closed = util.Counter() fringe = util.Queue() state = problem.getStartState() followPac = [] closed[hash(state)] = 1 for triple in problem.getSuccessors(state): fringe.push((triple, dirList.copy())) while not fringe.isEmpty(): i += 1 state = fringe.pop() succ = state[0][0] act = state[0][1] cost = state[0][2] dirList = state[1] dirList.append(act) if problem.isGoalState(succ): return dirList if problem.isPacman(succ): followPac.append(dirList.copy()) if closed[hash(succ)] == 0: closed[hash(succ)] = 1 for triple in problem.getSuccessors(succ): fringe.push((triple, dirList.copy())) if not followPac: return followPac = max(followPac, key=lambda x: len(x)) last = followPac.pop() followPac.append(last) followPac.append('place') followPac.append(reverse[last]) return followPac.copy() cbfs = customBreadthFirstSearch
""" Returns a path (a list of actions) to the closest dot, starting from gameState. """ # Here are some useful elements of the startState startPosition = gameState.getPacmanPosition(self.index) food = gameState.getFood() walls = gameState.getWalls() problem = AnyFoodSearchProblem(gameState, self.index) "*** YOUR CODE HERE ***" return search.bfs(problem)
identifier_body
myAgents.py
# myAgents.py # --------------- # Licensing Information: You are free to use or extend these projects for # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # Student side autograding was added by Brad Miller, Nick Hay, and # Pieter Abbeel (pabbeel@cs.berkeley.edu). from game import Agent from game import Directions from game import Actions from searchProblems import PositionSearchProblem import util import time import search """ IMPORTANT `agent` defines which agent you will use. By default, it is set to ClosestDotAgent, but when you're ready to test your own agent, replace it with MyAgent """ def createAgents(num_pacmen, agent='MyAgent'): return [eval(agent)(index=i) for i in range(num_pacmen)] foodCount = 1 north = Directions.NORTH south = Directions.SOUTH east = Directions.EAST west = Directions.WEST reverse = {north:south, south:north, east:west, west:east} class MyAgent(Agent): """ Implementation of your agent. """ customFood = None foodLeft = 0 specialWalls = {} finding = [] def getAction(self, state): """ Returns the next action the agent will take """ "*** YOUR CODE HERE ***" x, y = state.getPacmanPosition(self.index) numPacmen = state.getNumPacmanAgents() if not MyAgent.customFood: MyAgent.customFood = state.getFood() MyAgent.foodLeft = len(MyAgent.customFood.asList()) #if not self.foodIsThere(x, y): # self.path = None #trueLen = len(state.getFood().asList()) #if not self.path and self.index < trueLen and trueLen < numPacmen: # problem = MySearchProblem(state, self.index, 1, state.getFood()) # self.path = search.bfs(problem) if self.path and self.path[0] == 'place': if sum(MyAgent.finding) == 1: MyAgent.specialWalls[(x, y)] = self.path[1] self.path = None if not self.path and MyAgent.foodLeft > 0: problem = MySearchProblem(state, self.index, min(foodCount, MyAgent.foodLeft), MyAgent.customFood, MyAgent.specialWalls, MyAgent.finding) self.path = cbfs(problem) nx, ny = x, y if not self.path: return state.getLegalActions(self.index)[0] for i in range(len(self.path)): action = self.path[i] if action == 'place': MyAgent.finding[self.index] = False break MyAgent.finding[self.index] = True dx, dy = Actions.directionToVector(action) nx, ny = int(nx + dx), int(ny + dy) check = MyAgent.customFood[nx][ny] if check: MyAgent.foodLeft -= 1 MyAgent.customFood[nx][ny] = False if not self.path: return state.getLegalActions(self.index)[0] dir = self.path.pop(0) return dir def initialize(self): """ Intialize anything you want to here. This function is called when the agent is first created. If you don't need to use it, then leave it blank """ "*** YOUR CODE HERE" self.path = [] MyAgent.customFood = None MyAgent.foodLeft = 0 MyAgent.specialWalls = {} self.followOne = False if self.index == 0: MyAgent.finding = [] MyAgent.finding.append(False) """ Put any other SearchProblems or search methods below. You may also import classes/methods in search.py and searchProblems.py. (ClosestDotAgent as an example below) """ class MySearchProblem: def __init__(self, gameState, agentIndex, numFood, customFoodGrid, specialWalls, finding): self.walls = gameState.getWalls() self.position = gameState.getPacmanPosition(agentIndex) self.nOfAgents = gameState.getNumPacmanAgents() self.specialWalls = specialWalls self.pacmen = [gameState.getPacmanPosition(i) for i in range(self.nOfAgents)] self.finding = finding self.food = customFoodGrid self.numFood = numFood self.index = agentIndex def getStartState(self): """ Returns the start state for the search problem. """ #return (self.position, self.food.copy()) return self.position def isGoalState(self, state): """ state: Search state Returns True if and only if the state is a valid goal state. """ x, y = state return self.food[x][y] def isPacman(self, state): x, y = state if sum(self.finding) > 1: return False for i in range(self.nOfAgents): a, b = self.pacmen[i] if i != self.index and self.finding[i] and x == a and y == b: return True return False #def getID(self, state): # x, y = state # i def getSuccessors(self, state): """ state: Search state For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the action required to get there, and 'stepCost' is the incremental cost of expanding to that successor. """ successors = [] for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x,y = state #foodV = list(state[1]) dx, dy = Actions.directionToVector(direction) nextx, nexty = int(x + dx), int(y + dy) pos = (nextx, nexty) myBlock = False if pos in self.specialWalls: myBlock = self.specialWalls[pos] == direction if not self.walls[nextx][nexty] and not myBlock and not self.isPacman(state): #if self.food[nextx][nexty] and not repeat: #foodV.append((nextx, nexty)) #foodV = tuple(foodV) successors.append( ( (nextx, nexty), direction, 1 ) ) return successors def getCostOfActions(self, actions): """ actions: A list of actions to take This method returns the total cost of a particular sequence of actions. The sequence must be composed of legal moves. """ x,y= self.position cost = 0 for action in actions: # figure out the next state and see whether it's legal dx, dy = Actions.directionToVector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += 1 return cost class ClosestDotAgent(Agent): def findPathToClosestDot(self, gameState): """ Returns a path (a list of actions) to the closest dot, starting from gameState. """ # Here are some useful elements of the startState startPosition = gameState.getPacmanPosition(self.index) food = gameState.getFood() walls = gameState.getWalls() problem = AnyFoodSearchProblem(gameState, self.index) "*** YOUR CODE HERE ***" return search.bfs(problem) def getAction(self, state): return self.findPathToClosestDot(state)[0] class
(PositionSearchProblem): """ A search problem for finding a path to any food. This search problem is just like the PositionSearchProblem, but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. The class definition above, AnyFoodSearchProblem(PositionSearchProblem), inherits the methods of the PositionSearchProblem. You can use this search problem to help you fill in the findPathToClosestDot method. """ def __init__(self, gameState, agentIndex): "Stores information from the gameState. You don't need to change this." # Store the food for later reference self.food = gameState.getFood() # Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition(agentIndex) self.costFn = lambda x: 1 self._visited, self._visitedlist, self._expanded = {}, [], 0 # DO NOT CHANGE def isGoalState(self, state): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. """ x,y = state "*** YOUR CODE HERE ***" return self.food[x][y] def customBreadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" i = 0 dirList = [] closed = util.Counter() fringe = util.Queue() state = problem.getStartState() followPac = [] closed[hash(state)] = 1 for triple in problem.getSuccessors(state): fringe.push((triple, dirList.copy())) while not fringe.isEmpty(): i += 1 state = fringe.pop() succ = state[0][0] act = state[0][1] cost = state[0][2] dirList = state[1] dirList.append(act) if problem.isGoalState(succ): return dirList if problem.isPacman(succ): followPac.append(dirList.copy()) if closed[hash(succ)] == 0: closed[hash(succ)] = 1 for triple in problem.getSuccessors(succ): fringe.push((triple, dirList.copy())) if not followPac: return followPac = max(followPac, key=lambda x: len(x)) last = followPac.pop() followPac.append(last) followPac.append('place') followPac.append(reverse[last]) return followPac.copy() cbfs = customBreadthFirstSearch
AnyFoodSearchProblem
identifier_name
myAgents.py
# myAgents.py # --------------- # Licensing Information: You are free to use or extend these projects for # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # Student side autograding was added by Brad Miller, Nick Hay, and # Pieter Abbeel (pabbeel@cs.berkeley.edu). from game import Agent from game import Directions from game import Actions from searchProblems import PositionSearchProblem import util import time import search """ IMPORTANT `agent` defines which agent you will use. By default, it is set to ClosestDotAgent, but when you're ready to test your own agent, replace it with MyAgent """ def createAgents(num_pacmen, agent='MyAgent'): return [eval(agent)(index=i) for i in range(num_pacmen)] foodCount = 1 north = Directions.NORTH south = Directions.SOUTH east = Directions.EAST west = Directions.WEST reverse = {north:south, south:north, east:west, west:east} class MyAgent(Agent): """ Implementation of your agent. """ customFood = None foodLeft = 0 specialWalls = {} finding = [] def getAction(self, state): """ Returns the next action the agent will take """ "*** YOUR CODE HERE ***" x, y = state.getPacmanPosition(self.index) numPacmen = state.getNumPacmanAgents() if not MyAgent.customFood: MyAgent.customFood = state.getFood() MyAgent.foodLeft = len(MyAgent.customFood.asList()) #if not self.foodIsThere(x, y): # self.path = None #trueLen = len(state.getFood().asList()) #if not self.path and self.index < trueLen and trueLen < numPacmen: # problem = MySearchProblem(state, self.index, 1, state.getFood()) # self.path = search.bfs(problem) if self.path and self.path[0] == 'place': if sum(MyAgent.finding) == 1: MyAgent.specialWalls[(x, y)] = self.path[1] self.path = None if not self.path and MyAgent.foodLeft > 0: problem = MySearchProblem(state, self.index, min(foodCount, MyAgent.foodLeft), MyAgent.customFood, MyAgent.specialWalls, MyAgent.finding) self.path = cbfs(problem) nx, ny = x, y if not self.path: return state.getLegalActions(self.index)[0] for i in range(len(self.path)): action = self.path[i] if action == 'place': MyAgent.finding[self.index] = False break MyAgent.finding[self.index] = True dx, dy = Actions.directionToVector(action) nx, ny = int(nx + dx), int(ny + dy) check = MyAgent.customFood[nx][ny] if check: MyAgent.foodLeft -= 1 MyAgent.customFood[nx][ny] = False if not self.path: return state.getLegalActions(self.index)[0] dir = self.path.pop(0) return dir def initialize(self): """ Intialize anything you want to here. This function is called when the agent is first created. If you don't need to use it, then leave it blank """ "*** YOUR CODE HERE" self.path = [] MyAgent.customFood = None MyAgent.foodLeft = 0 MyAgent.specialWalls = {} self.followOne = False if self.index == 0: MyAgent.finding = [] MyAgent.finding.append(False) """ Put any other SearchProblems or search methods below. You may also import classes/methods in search.py and searchProblems.py. (ClosestDotAgent as an example below) """ class MySearchProblem: def __init__(self, gameState, agentIndex, numFood, customFoodGrid, specialWalls, finding): self.walls = gameState.getWalls() self.position = gameState.getPacmanPosition(agentIndex) self.nOfAgents = gameState.getNumPacmanAgents() self.specialWalls = specialWalls self.pacmen = [gameState.getPacmanPosition(i) for i in range(self.nOfAgents)] self.finding = finding self.food = customFoodGrid self.numFood = numFood self.index = agentIndex def getStartState(self): """ Returns the start state for the search problem. """ #return (self.position, self.food.copy()) return self.position def isGoalState(self, state): """ state: Search state Returns True if and only if the state is a valid goal state. """ x, y = state return self.food[x][y] def isPacman(self, state): x, y = state if sum(self.finding) > 1: return False for i in range(self.nOfAgents): a, b = self.pacmen[i] if i != self.index and self.finding[i] and x == a and y == b: return True return False #def getID(self, state): # x, y = state # i def getSuccessors(self, state): """ state: Search state For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the action required to get there, and 'stepCost' is the incremental cost of expanding to that successor. """ successors = [] for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x,y = state #foodV = list(state[1]) dx, dy = Actions.directionToVector(direction) nextx, nexty = int(x + dx), int(y + dy) pos = (nextx, nexty) myBlock = False if pos in self.specialWalls: myBlock = self.specialWalls[pos] == direction if not self.walls[nextx][nexty] and not myBlock and not self.isPacman(state): #if self.food[nextx][nexty] and not repeat: #foodV.append((nextx, nexty)) #foodV = tuple(foodV) successors.append( ( (nextx, nexty), direction, 1 ) ) return successors def getCostOfActions(self, actions): """ actions: A list of actions to take This method returns the total cost of a particular sequence of actions. The sequence must be composed of legal moves. """ x,y= self.position cost = 0 for action in actions: # figure out the next state and see whether it's legal dx, dy = Actions.directionToVector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += 1 return cost class ClosestDotAgent(Agent): def findPathToClosestDot(self, gameState): """ Returns a path (a list of actions) to the closest dot, starting from gameState. """ # Here are some useful elements of the startState startPosition = gameState.getPacmanPosition(self.index) food = gameState.getFood() walls = gameState.getWalls() problem = AnyFoodSearchProblem(gameState, self.index) "*** YOUR CODE HERE ***" return search.bfs(problem) def getAction(self, state): return self.findPathToClosestDot(state)[0] class AnyFoodSearchProblem(PositionSearchProblem): """ A search problem for finding a path to any food. This search problem is just like the PositionSearchProblem, but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. The class definition above, AnyFoodSearchProblem(PositionSearchProblem), inherits the methods of the PositionSearchProblem. You can use this search problem to help you fill in the findPathToClosestDot method. """ def __init__(self, gameState, agentIndex): "Stores information from the gameState. You don't need to change this." # Store the food for later reference self.food = gameState.getFood() # Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition(agentIndex) self.costFn = lambda x: 1 self._visited, self._visitedlist, self._expanded = {}, [], 0 # DO NOT CHANGE def isGoalState(self, state): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. """ x,y = state "*** YOUR CODE HERE ***" return self.food[x][y] def customBreadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" i = 0 dirList = [] closed = util.Counter() fringe = util.Queue() state = problem.getStartState() followPac = [] closed[hash(state)] = 1 for triple in problem.getSuccessors(state): fringe.push((triple, dirList.copy())) while not fringe.isEmpty():
if not followPac: return followPac = max(followPac, key=lambda x: len(x)) last = followPac.pop() followPac.append(last) followPac.append('place') followPac.append(reverse[last]) return followPac.copy() cbfs = customBreadthFirstSearch
i += 1 state = fringe.pop() succ = state[0][0] act = state[0][1] cost = state[0][2] dirList = state[1] dirList.append(act) if problem.isGoalState(succ): return dirList if problem.isPacman(succ): followPac.append(dirList.copy()) if closed[hash(succ)] == 0: closed[hash(succ)] = 1 for triple in problem.getSuccessors(succ): fringe.push((triple, dirList.copy()))
conditional_block
myAgents.py
# myAgents.py # --------------- # Licensing Information: You are free to use or extend these projects for # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # Student side autograding was added by Brad Miller, Nick Hay, and # Pieter Abbeel (pabbeel@cs.berkeley.edu). from game import Agent from game import Directions from game import Actions from searchProblems import PositionSearchProblem import util import time import search """ IMPORTANT `agent` defines which agent you will use. By default, it is set to ClosestDotAgent, but when you're ready to test your own agent, replace it with MyAgent """ def createAgents(num_pacmen, agent='MyAgent'): return [eval(agent)(index=i) for i in range(num_pacmen)] foodCount = 1 north = Directions.NORTH south = Directions.SOUTH east = Directions.EAST west = Directions.WEST reverse = {north:south, south:north, east:west, west:east} class MyAgent(Agent): """ Implementation of your agent. """ customFood = None foodLeft = 0 specialWalls = {} finding = [] def getAction(self, state): """ Returns the next action the agent will take """ "*** YOUR CODE HERE ***" x, y = state.getPacmanPosition(self.index) numPacmen = state.getNumPacmanAgents() if not MyAgent.customFood: MyAgent.customFood = state.getFood() MyAgent.foodLeft = len(MyAgent.customFood.asList()) #if not self.foodIsThere(x, y): # self.path = None #trueLen = len(state.getFood().asList()) #if not self.path and self.index < trueLen and trueLen < numPacmen: # problem = MySearchProblem(state, self.index, 1, state.getFood()) # self.path = search.bfs(problem) if self.path and self.path[0] == 'place': if sum(MyAgent.finding) == 1:
self.path = None if not self.path and MyAgent.foodLeft > 0: problem = MySearchProblem(state, self.index, min(foodCount, MyAgent.foodLeft), MyAgent.customFood, MyAgent.specialWalls, MyAgent.finding) self.path = cbfs(problem) nx, ny = x, y if not self.path: return state.getLegalActions(self.index)[0] for i in range(len(self.path)): action = self.path[i] if action == 'place': MyAgent.finding[self.index] = False break MyAgent.finding[self.index] = True dx, dy = Actions.directionToVector(action) nx, ny = int(nx + dx), int(ny + dy) check = MyAgent.customFood[nx][ny] if check: MyAgent.foodLeft -= 1 MyAgent.customFood[nx][ny] = False if not self.path: return state.getLegalActions(self.index)[0] dir = self.path.pop(0) return dir def initialize(self): """ Intialize anything you want to here. This function is called when the agent is first created. If you don't need to use it, then leave it blank """ "*** YOUR CODE HERE" self.path = [] MyAgent.customFood = None MyAgent.foodLeft = 0 MyAgent.specialWalls = {} self.followOne = False if self.index == 0: MyAgent.finding = [] MyAgent.finding.append(False) """ Put any other SearchProblems or search methods below. You may also import classes/methods in search.py and searchProblems.py. (ClosestDotAgent as an example below) """ class MySearchProblem: def __init__(self, gameState, agentIndex, numFood, customFoodGrid, specialWalls, finding): self.walls = gameState.getWalls() self.position = gameState.getPacmanPosition(agentIndex) self.nOfAgents = gameState.getNumPacmanAgents() self.specialWalls = specialWalls self.pacmen = [gameState.getPacmanPosition(i) for i in range(self.nOfAgents)] self.finding = finding self.food = customFoodGrid self.numFood = numFood self.index = agentIndex def getStartState(self): """ Returns the start state for the search problem. """ #return (self.position, self.food.copy()) return self.position def isGoalState(self, state): """ state: Search state Returns True if and only if the state is a valid goal state. """ x, y = state return self.food[x][y] def isPacman(self, state): x, y = state if sum(self.finding) > 1: return False for i in range(self.nOfAgents): a, b = self.pacmen[i] if i != self.index and self.finding[i] and x == a and y == b: return True return False #def getID(self, state): # x, y = state # i def getSuccessors(self, state): """ state: Search state For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the action required to get there, and 'stepCost' is the incremental cost of expanding to that successor. """ successors = [] for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x,y = state #foodV = list(state[1]) dx, dy = Actions.directionToVector(direction) nextx, nexty = int(x + dx), int(y + dy) pos = (nextx, nexty) myBlock = False if pos in self.specialWalls: myBlock = self.specialWalls[pos] == direction if not self.walls[nextx][nexty] and not myBlock and not self.isPacman(state): #if self.food[nextx][nexty] and not repeat: #foodV.append((nextx, nexty)) #foodV = tuple(foodV) successors.append( ( (nextx, nexty), direction, 1 ) ) return successors def getCostOfActions(self, actions): """ actions: A list of actions to take This method returns the total cost of a particular sequence of actions. The sequence must be composed of legal moves. """ x,y= self.position cost = 0 for action in actions: # figure out the next state and see whether it's legal dx, dy = Actions.directionToVector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += 1 return cost class ClosestDotAgent(Agent): def findPathToClosestDot(self, gameState): """ Returns a path (a list of actions) to the closest dot, starting from gameState. """ # Here are some useful elements of the startState startPosition = gameState.getPacmanPosition(self.index) food = gameState.getFood() walls = gameState.getWalls() problem = AnyFoodSearchProblem(gameState, self.index) "*** YOUR CODE HERE ***" return search.bfs(problem) def getAction(self, state): return self.findPathToClosestDot(state)[0] class AnyFoodSearchProblem(PositionSearchProblem): """ A search problem for finding a path to any food. This search problem is just like the PositionSearchProblem, but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. The class definition above, AnyFoodSearchProblem(PositionSearchProblem), inherits the methods of the PositionSearchProblem. You can use this search problem to help you fill in the findPathToClosestDot method. """ def __init__(self, gameState, agentIndex): "Stores information from the gameState. You don't need to change this." # Store the food for later reference self.food = gameState.getFood() # Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition(agentIndex) self.costFn = lambda x: 1 self._visited, self._visitedlist, self._expanded = {}, [], 0 # DO NOT CHANGE def isGoalState(self, state): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. """ x,y = state "*** YOUR CODE HERE ***" return self.food[x][y] def customBreadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" i = 0 dirList = [] closed = util.Counter() fringe = util.Queue() state = problem.getStartState() followPac = [] closed[hash(state)] = 1 for triple in problem.getSuccessors(state): fringe.push((triple, dirList.copy())) while not fringe.isEmpty(): i += 1 state = fringe.pop() succ = state[0][0] act = state[0][1] cost = state[0][2] dirList = state[1] dirList.append(act) if problem.isGoalState(succ): return dirList if problem.isPacman(succ): followPac.append(dirList.copy()) if closed[hash(succ)] == 0: closed[hash(succ)] = 1 for triple in problem.getSuccessors(succ): fringe.push((triple, dirList.copy())) if not followPac: return followPac = max(followPac, key=lambda x: len(x)) last = followPac.pop() followPac.append(last) followPac.append('place') followPac.append(reverse[last]) return followPac.copy() cbfs = customBreadthFirstSearch
MyAgent.specialWalls[(x, y)] = self.path[1]
random_line_split
user_mgt.go
// Copyright 2017 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package auth import ( "context" "encoding/json" "errors" "fmt" "net/http" "regexp" "strings" "time" "firebase.google.com/go/internal" "google.golang.org/api/googleapi" "google.golang.org/api/identitytoolkit/v3" ) const ( maxLenPayloadCC = 1000 defaultProviderID = "firebase" ) // Create a new interface type identitytoolkitCall interface { Header() http.Header } // set header func (c *Client) setHeader(ic identitytoolkitCall) { ic.Header().Set("X-Client-Version", c.version) } // UserInfo is a collection of standard profile information for a user. type UserInfo struct { DisplayName string Email string PhoneNumber string PhotoURL string // In the ProviderUserInfo[] ProviderID can be a short domain name (e.g. google.com), // or the identity of an OpenID identity provider. // In UserRecord.UserInfo it will return the constant string "firebase". ProviderID string UID string } // UserMetadata contains additional metadata associated with a user account. // Timestamps are in milliseconds since epoch. type UserMetadata struct { CreationTimestamp int64 LastLogInTimestamp int64 } // UserRecord contains metadata associated with a Firebase user account. type UserRecord struct { *UserInfo CustomClaims map[string]interface{} Disabled bool EmailVerified bool ProviderUserInfo []*UserInfo TokensValidAfterMillis int64 // milliseconds since epoch. UserMetadata *UserMetadata } // UserToCreate is the parameter struct for the CreateUser function. type UserToCreate struct { createReq *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest uid bool displayName bool email bool photoURL bool phoneNumber bool } func (u *UserToCreate) request() *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest { if u.createReq == nil { u.createReq = &identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest{} } return u.createReq } func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) { req := u.request() // creating a user without any parameters is allowed if u.uid { if err := validateUID(req.LocalId); err != nil { return nil, err } } if u.displayName { if err := validateDisplayName(req.DisplayName); err != nil { return nil, err } } if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.phoneNumber { if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.photoURL { if err := validatePhotoURL(req.PhotoUrl); err != nil { return nil, err } } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // Disabled setter. func (u *UserToCreate) Disabled(disabled bool) *UserToCreate { req := u.request() req.Disabled = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "Disabled") } return u } // DisplayName setter. func (u *UserToCreate) DisplayName(name string) *UserToCreate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToCreate) Email(email string) *UserToCreate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToCreate) Password(pw string) *UserToCreate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToCreate) PhotoURL(url string) *UserToCreate { u.request().PhotoUrl = url u.photoURL = true return u } // UID setter. func (u *UserToCreate) UID(uid string) *UserToCreate { u.request().LocalId = uid u.uid = true return u } // UserToUpdate is the parameter struct for the UpdateUser function. type UserToUpdate struct { updateReq *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest claims map[string]interface{} displayName bool email bool phoneNumber bool photoURL bool customClaims bool } func (u *UserToUpdate) request() *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest { if u.updateReq == nil { u.updateReq = &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{} } return u.updateReq } func (u *UserToUpdate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest, error) { if u.updateReq == nil { // update without any parameters is never allowed return nil, fmt.Errorf("update parameters must not be nil or empty") } req := u.updateReq if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.displayName && req.DisplayName == "" { req.DeleteAttribute = append(req.DeleteAttribute, "DISPLAY_NAME") } if u.photoURL && req.PhotoUrl == "" { req.DeleteAttribute = append(req.DeleteAttribute, "PHOTO_URL") } if u.phoneNumber { if req.PhoneNumber == "" { req.DeleteProvider = append(req.DeleteProvider, "phone") } else if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.customClaims { cc, err := marshalCustomClaims(u.claims) if err != nil { return nil, err } req.CustomAttributes = cc } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // CustomClaims setter. func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate { u.request() // force initialization of the request for later use u.claims = claims u.customClaims = true return u } // Disabled setter. func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate { req := u.request() req.DisableUser = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "DisableUser") } return u } // DisplayName setter. func (u *UserToUpdate) DisplayName(name string) *UserToUpdate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToUpdate) Email(email string) *UserToUpdate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToUpdate) Password(pw string) *UserToUpdate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate { u.request().PhotoUrl = url u.photoURL = true return u } // revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property // to the present in epoch seconds. func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate { u.request().ValidSince = time.Now().Unix() return u } // CreateUser creates a new user with the specified properties. func (c *Client) CreateUser(ctx context.Context, user *UserToCreate) (*UserRecord, error) { uid, err := c.createUser(ctx, user) if err != nil { return nil, err } return c.GetUser(ctx, uid) } // UpdateUser updates an existing user account with the specified properties. // // DisplayName, PhotoURL and PhoneNumber will be set to "" to signify deleting them from the record. func (c *Client) UpdateUser(ctx context.Context, uid string, user *UserToUpdate) (ur *UserRecord, err error) { if err := c.updateUser(ctx, uid, user); err != nil { return nil, err } return c.GetUser(ctx, uid) } // DeleteUser deletes the user by the given UID. func (c *Client) DeleteUser(ctx context.Context, uid string) error { if err := validateUID(uid); err != nil { return err } request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{ LocalId: uid, } call := c.is.Relyingparty.DeleteAccount(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } // GetUser gets the user data corresponding to the specified user ID. func (c *Client) GetUser(ctx context.Context, uid string) (*UserRecord, error) { if err := validateUID(uid); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ LocalId: []string{uid}, } return c.getUser(ctx, request) } // GetUserByPhoneNumber gets the user data corresponding to the specified user phone number. func (c *Client) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) { if err := validatePhone(phone); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ PhoneNumber: []string{phone}, } return c.getUser(ctx, request) } // GetUserByEmail gets the user data corresponding to the specified email. func (c *Client) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) { if err := validateEmail(email); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ Email: []string{email}, } return c.getUser(ctx, request) } // RevokeRefreshTokens revokes all refresh tokens issued to a user. // // RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second. // It is important that the server on which this is called has its clock set correctly and synchronized. // // While this revokes all sessions for a specified user and disables any new ID tokens for existing sessions // from getting minted, existing ID tokens may remain active until their natural expiration (one hour). // To verify that ID tokens are revoked, use `verifyIdTokenAndCheckRevoked(ctx, idToken)`. func (c *Client) RevokeRefreshTokens(ctx context.Context, uid string) error { return c.updateUser(ctx, uid, (&UserToUpdate{}).revokeRefreshTokens()) } // SetCustomUserClaims sets additional claims on an existing user account. // // Custom claims set via this function can be used to define user roles and privilege levels. // These claims propagate to all the devices where the user is already signed in (after token // expiration or when token refresh is forced), and next time the user signs in. The claims // can be accessed via the user's ID token JWT. If a reserved OIDC claim is specified (sub, iat, // iss, etc), an error is thrown. Claims payload must also not be larger then 1000 characters // when serialized into a JSON string. func (c *Client) SetCustomUserClaims(ctx context.Context, uid string, customClaims map[string]interface{}) error { if customClaims == nil || len(customClaims) == 0 { customClaims = map[string]interface{}{} } return c.updateUser(ctx, uid, (&UserToUpdate{}).CustomClaims(customClaims)) } func marshalCustomClaims(claims map[string]interface{}) (string, error) { for _, key := range reservedClaims { if _, ok := claims[key]; ok { return "", fmt.Errorf("claim %q is reserved and must not be set", key) } } b, err := json.Marshal(claims) if err != nil { return "", fmt.Errorf("custom claims marshaling error: %v", err) } s := string(b) if s == "null" { s = "{}" // claims map has been explicitly set to nil for deletion. } if len(s) > maxLenPayloadCC { return "", fmt.Errorf("serialized custom claims must not exceed %d characters", maxLenPayloadCC) } return s, nil } // Error handlers. const ( emailAlreadyExists = "email-already-exists" idTokenRevoked = "id-token-revoked" insufficientPermission = "insufficient-permission" phoneNumberAlreadyExists = "phone-number-already-exists" projectNotFound = "project-not-found" sessionCookieRevoked = "session-cookie-revoked" uidAlreadyExists = "uid-already-exists" unknown = "unknown-error" userNotFound = "user-not-found" ) // IsEmailAlreadyExists checks if the given error was due to a duplicate email. func IsEmailAlreadyExists(err error) bool { return internal.HasErrorCode(err, emailAlreadyExists) } // IsIDTokenRevoked checks if the given error was due to a revoked ID token. func IsIDTokenRevoked(err error) bool { return internal.HasErrorCode(err, idTokenRevoked) } // IsInsufficientPermission checks if the given error was due to insufficient permissions. func IsInsufficientPermission(err error) bool { return internal.HasErrorCode(err, insufficientPermission) } // IsPhoneNumberAlreadyExists checks if the given error was due to a duplicate phone number. func IsPhoneNumberAlreadyExists(err error) bool { return internal.HasErrorCode(err, phoneNumberAlreadyExists) } // IsProjectNotFound checks if the given error was due to a non-existing project. func IsProjectNotFound(err error) bool { return internal.HasErrorCode(err, projectNotFound) } // IsSessionCookieRevoked checks if the given error was due to a revoked session cookie. func IsSessionCookieRevoked(err error) bool { return internal.HasErrorCode(err, sessionCookieRevoked) } // IsUIDAlreadyExists checks if the given error was due to a duplicate uid. func IsUIDAlreadyExists(err error) bool { return internal.HasErrorCode(err, uidAlreadyExists) } // IsUnknown checks if the given error was due to a unknown server error. func IsUnknown(err error) bool { return internal.HasErrorCode(err, unknown) } // IsUserNotFound checks if the given error was due to non-existing user. func IsUserNotFound(err error) bool { return internal.HasErrorCode(err, userNotFound) } var serverError = map[string]string{ "CONFIGURATION_NOT_FOUND": projectNotFound, "DUPLICATE_EMAIL": emailAlreadyExists, "DUPLICATE_LOCAL_ID": uidAlreadyExists, "EMAIL_EXISTS": emailAlreadyExists, "INSUFFICIENT_PERMISSION": insufficientPermission, "PERMISSION_DENIED": insufficientPermission, "PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists, "PROJECT_NOT_FOUND": projectNotFound, "USER_NOT_FOUND": userNotFound, } func handleServerError(err error) error { gerr, ok := err.(*googleapi.Error) if !ok { // Not a back-end error return err } serverCode := gerr.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Error(clientCode, err.Error()) } // Validators. func validateDisplayName(val string) error { if val == "" {
return fmt.Errorf("display name must be a non-empty string") } return nil } func validatePhotoURL(val string) error { if val == "" { return fmt.Errorf("photo url must be a non-empty string") } return nil } func validateEmail(email string) error { if email == "" { return fmt.Errorf("email must be a non-empty string") } if parts := strings.Split(email, "@"); len(parts) != 2 || parts[0] == "" || parts[1] == "" { return fmt.Errorf("malformed email string: %q", email) } return nil } func validatePassword(val string) error { if len(val) < 6 { return fmt.Errorf("password must be a string at least 6 characters long") } return nil } func validateUID(uid string) error { if uid == "" { return fmt.Errorf("uid must be a non-empty string") } if len(uid) > 128 { return fmt.Errorf("uid string must not be longer than 128 characters") } return nil } func validatePhone(phone string) error { if phone == "" { return fmt.Errorf("phone number must be a non-empty string") } if !regexp.MustCompile(`\+.*[0-9A-Za-z]`).MatchString(phone) { return fmt.Errorf("phone number must be a valid, E.164 compliant identifier") } return nil } // End of validators // Helper functions for retrieval and HTTP calls. func (c *Client) createUser(ctx context.Context, user *UserToCreate) (string, error) { if user == nil { user = &UserToCreate{} } request, err := user.validatedRequest() if err != nil { return "", err } call := c.is.Relyingparty.SignupNewUser(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return "", handleServerError(err) } return resp.LocalId, nil } func (c *Client) updateUser(ctx context.Context, uid string, user *UserToUpdate) error { if err := validateUID(uid); err != nil { return err } if user == nil { return fmt.Errorf("update parameters must not be nil or empty") } request, err := user.validatedRequest() if err != nil { return err } request.LocalId = uid call := c.is.Relyingparty.SetAccountInfo(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } func (c *Client) getUser(ctx context.Context, request *identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest) (*UserRecord, error) { call := c.is.Relyingparty.GetAccountInfo(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return nil, handleServerError(err) } if len(resp.Users) == 0 { var msg string if len(request.LocalId) == 1 { msg = fmt.Sprintf("cannot find user from uid: %q", request.LocalId[0]) } else if len(request.Email) == 1 { msg = fmt.Sprintf("cannot find user from email: %q", request.Email[0]) } else { msg = fmt.Sprintf("cannot find user from phone number: %q", request.PhoneNumber[0]) } return nil, internal.Error(userNotFound, msg) } eu, err := makeExportedUser(resp.Users[0]) if err != nil { return nil, err } return eu.UserRecord, nil } const idToolkitEndpoint = "https://identitytoolkit.googleapis.com/v1/projects" // userManagementClient is a helper for interacting with the Identity Toolkit REST API. // // Currently it is only used for some user management operations (e.g. session cookie // generation). We will gradually migrate all the user management functionality to this // type, and remove our dependency on the google.golang.org/api/identitytoolkit/v3 // package. type userManagementClient struct { baseURL string projectID string version string httpClient *internal.HTTPClient } // SessionCookie creates a new Firebase session cookie from the given ID token and expiry // duration. The returned JWT can be set as a server-side session cookie with a custom cookie // policy. Expiry duration must be at least 5 minutes but may not exceed 14 days. func (c *userManagementClient) SessionCookie( ctx context.Context, idToken string, expiresIn time.Duration, ) (string, error) { if idToken == "" { return "", errors.New("id token must not be empty") } if expiresIn < 5*time.Minute || expiresIn > 14*24*time.Hour { return "", errors.New("expiry duration must be between 5 minutes and 14 days") } payload := map[string]interface{}{ "idToken": idToken, "validDuration": int64(expiresIn.Seconds()), } resp, err := c.post(ctx, ":createSessionCookie", payload) if err != nil { return "", err } if resp.Status != http.StatusOK { return "", handleHTTPError(resp) } var result struct { SessionCookie string `json:"sessionCookie"` } err = json.Unmarshal(resp.Body, &result) return result.SessionCookie, err } func (c *userManagementClient) post( ctx context.Context, path string, payload interface{}, ) (*internal.Response, error) { req, err := c.newRequest(http.MethodPost, path) if err != nil { return nil, err } req.Body = internal.NewJSONEntity(payload) return c.httpClient.Do(ctx, req) } func (c *userManagementClient) newRequest(method, path string) (*internal.Request, error) { if c.projectID == "" { return nil, errors.New("project id not available") } versionHeader := internal.WithHeader("X-Client-Version", c.version) return &internal.Request{ Method: method, URL: fmt.Sprintf("%s/%s%s", c.baseURL, c.projectID, path), Opts: []internal.HTTPOption{versionHeader}, }, nil } func handleHTTPError(resp *internal.Response) error { var httpErr struct { Error struct { Message string `json:"message"` } `json:"error"` } json.Unmarshal(resp.Body, &httpErr) // ignore any json parse errors at this level serverCode := httpErr.Error.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Errorf( clientCode, "http error status: %d; body: %s", resp.Status, string(resp.Body)) }
random_line_split
user_mgt.go
// Copyright 2017 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package auth import ( "context" "encoding/json" "errors" "fmt" "net/http" "regexp" "strings" "time" "firebase.google.com/go/internal" "google.golang.org/api/googleapi" "google.golang.org/api/identitytoolkit/v3" ) const ( maxLenPayloadCC = 1000 defaultProviderID = "firebase" ) // Create a new interface type identitytoolkitCall interface { Header() http.Header } // set header func (c *Client)
(ic identitytoolkitCall) { ic.Header().Set("X-Client-Version", c.version) } // UserInfo is a collection of standard profile information for a user. type UserInfo struct { DisplayName string Email string PhoneNumber string PhotoURL string // In the ProviderUserInfo[] ProviderID can be a short domain name (e.g. google.com), // or the identity of an OpenID identity provider. // In UserRecord.UserInfo it will return the constant string "firebase". ProviderID string UID string } // UserMetadata contains additional metadata associated with a user account. // Timestamps are in milliseconds since epoch. type UserMetadata struct { CreationTimestamp int64 LastLogInTimestamp int64 } // UserRecord contains metadata associated with a Firebase user account. type UserRecord struct { *UserInfo CustomClaims map[string]interface{} Disabled bool EmailVerified bool ProviderUserInfo []*UserInfo TokensValidAfterMillis int64 // milliseconds since epoch. UserMetadata *UserMetadata } // UserToCreate is the parameter struct for the CreateUser function. type UserToCreate struct { createReq *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest uid bool displayName bool email bool photoURL bool phoneNumber bool } func (u *UserToCreate) request() *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest { if u.createReq == nil { u.createReq = &identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest{} } return u.createReq } func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) { req := u.request() // creating a user without any parameters is allowed if u.uid { if err := validateUID(req.LocalId); err != nil { return nil, err } } if u.displayName { if err := validateDisplayName(req.DisplayName); err != nil { return nil, err } } if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.phoneNumber { if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.photoURL { if err := validatePhotoURL(req.PhotoUrl); err != nil { return nil, err } } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // Disabled setter. func (u *UserToCreate) Disabled(disabled bool) *UserToCreate { req := u.request() req.Disabled = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "Disabled") } return u } // DisplayName setter. func (u *UserToCreate) DisplayName(name string) *UserToCreate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToCreate) Email(email string) *UserToCreate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToCreate) Password(pw string) *UserToCreate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToCreate) PhotoURL(url string) *UserToCreate { u.request().PhotoUrl = url u.photoURL = true return u } // UID setter. func (u *UserToCreate) UID(uid string) *UserToCreate { u.request().LocalId = uid u.uid = true return u } // UserToUpdate is the parameter struct for the UpdateUser function. type UserToUpdate struct { updateReq *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest claims map[string]interface{} displayName bool email bool phoneNumber bool photoURL bool customClaims bool } func (u *UserToUpdate) request() *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest { if u.updateReq == nil { u.updateReq = &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{} } return u.updateReq } func (u *UserToUpdate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest, error) { if u.updateReq == nil { // update without any parameters is never allowed return nil, fmt.Errorf("update parameters must not be nil or empty") } req := u.updateReq if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.displayName && req.DisplayName == "" { req.DeleteAttribute = append(req.DeleteAttribute, "DISPLAY_NAME") } if u.photoURL && req.PhotoUrl == "" { req.DeleteAttribute = append(req.DeleteAttribute, "PHOTO_URL") } if u.phoneNumber { if req.PhoneNumber == "" { req.DeleteProvider = append(req.DeleteProvider, "phone") } else if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.customClaims { cc, err := marshalCustomClaims(u.claims) if err != nil { return nil, err } req.CustomAttributes = cc } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // CustomClaims setter. func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate { u.request() // force initialization of the request for later use u.claims = claims u.customClaims = true return u } // Disabled setter. func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate { req := u.request() req.DisableUser = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "DisableUser") } return u } // DisplayName setter. func (u *UserToUpdate) DisplayName(name string) *UserToUpdate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToUpdate) Email(email string) *UserToUpdate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToUpdate) Password(pw string) *UserToUpdate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate { u.request().PhotoUrl = url u.photoURL = true return u } // revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property // to the present in epoch seconds. func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate { u.request().ValidSince = time.Now().Unix() return u } // CreateUser creates a new user with the specified properties. func (c *Client) CreateUser(ctx context.Context, user *UserToCreate) (*UserRecord, error) { uid, err := c.createUser(ctx, user) if err != nil { return nil, err } return c.GetUser(ctx, uid) } // UpdateUser updates an existing user account with the specified properties. // // DisplayName, PhotoURL and PhoneNumber will be set to "" to signify deleting them from the record. func (c *Client) UpdateUser(ctx context.Context, uid string, user *UserToUpdate) (ur *UserRecord, err error) { if err := c.updateUser(ctx, uid, user); err != nil { return nil, err } return c.GetUser(ctx, uid) } // DeleteUser deletes the user by the given UID. func (c *Client) DeleteUser(ctx context.Context, uid string) error { if err := validateUID(uid); err != nil { return err } request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{ LocalId: uid, } call := c.is.Relyingparty.DeleteAccount(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } // GetUser gets the user data corresponding to the specified user ID. func (c *Client) GetUser(ctx context.Context, uid string) (*UserRecord, error) { if err := validateUID(uid); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ LocalId: []string{uid}, } return c.getUser(ctx, request) } // GetUserByPhoneNumber gets the user data corresponding to the specified user phone number. func (c *Client) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) { if err := validatePhone(phone); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ PhoneNumber: []string{phone}, } return c.getUser(ctx, request) } // GetUserByEmail gets the user data corresponding to the specified email. func (c *Client) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) { if err := validateEmail(email); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ Email: []string{email}, } return c.getUser(ctx, request) } // RevokeRefreshTokens revokes all refresh tokens issued to a user. // // RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second. // It is important that the server on which this is called has its clock set correctly and synchronized. // // While this revokes all sessions for a specified user and disables any new ID tokens for existing sessions // from getting minted, existing ID tokens may remain active until their natural expiration (one hour). // To verify that ID tokens are revoked, use `verifyIdTokenAndCheckRevoked(ctx, idToken)`. func (c *Client) RevokeRefreshTokens(ctx context.Context, uid string) error { return c.updateUser(ctx, uid, (&UserToUpdate{}).revokeRefreshTokens()) } // SetCustomUserClaims sets additional claims on an existing user account. // // Custom claims set via this function can be used to define user roles and privilege levels. // These claims propagate to all the devices where the user is already signed in (after token // expiration or when token refresh is forced), and next time the user signs in. The claims // can be accessed via the user's ID token JWT. If a reserved OIDC claim is specified (sub, iat, // iss, etc), an error is thrown. Claims payload must also not be larger then 1000 characters // when serialized into a JSON string. func (c *Client) SetCustomUserClaims(ctx context.Context, uid string, customClaims map[string]interface{}) error { if customClaims == nil || len(customClaims) == 0 { customClaims = map[string]interface{}{} } return c.updateUser(ctx, uid, (&UserToUpdate{}).CustomClaims(customClaims)) } func marshalCustomClaims(claims map[string]interface{}) (string, error) { for _, key := range reservedClaims { if _, ok := claims[key]; ok { return "", fmt.Errorf("claim %q is reserved and must not be set", key) } } b, err := json.Marshal(claims) if err != nil { return "", fmt.Errorf("custom claims marshaling error: %v", err) } s := string(b) if s == "null" { s = "{}" // claims map has been explicitly set to nil for deletion. } if len(s) > maxLenPayloadCC { return "", fmt.Errorf("serialized custom claims must not exceed %d characters", maxLenPayloadCC) } return s, nil } // Error handlers. const ( emailAlreadyExists = "email-already-exists" idTokenRevoked = "id-token-revoked" insufficientPermission = "insufficient-permission" phoneNumberAlreadyExists = "phone-number-already-exists" projectNotFound = "project-not-found" sessionCookieRevoked = "session-cookie-revoked" uidAlreadyExists = "uid-already-exists" unknown = "unknown-error" userNotFound = "user-not-found" ) // IsEmailAlreadyExists checks if the given error was due to a duplicate email. func IsEmailAlreadyExists(err error) bool { return internal.HasErrorCode(err, emailAlreadyExists) } // IsIDTokenRevoked checks if the given error was due to a revoked ID token. func IsIDTokenRevoked(err error) bool { return internal.HasErrorCode(err, idTokenRevoked) } // IsInsufficientPermission checks if the given error was due to insufficient permissions. func IsInsufficientPermission(err error) bool { return internal.HasErrorCode(err, insufficientPermission) } // IsPhoneNumberAlreadyExists checks if the given error was due to a duplicate phone number. func IsPhoneNumberAlreadyExists(err error) bool { return internal.HasErrorCode(err, phoneNumberAlreadyExists) } // IsProjectNotFound checks if the given error was due to a non-existing project. func IsProjectNotFound(err error) bool { return internal.HasErrorCode(err, projectNotFound) } // IsSessionCookieRevoked checks if the given error was due to a revoked session cookie. func IsSessionCookieRevoked(err error) bool { return internal.HasErrorCode(err, sessionCookieRevoked) } // IsUIDAlreadyExists checks if the given error was due to a duplicate uid. func IsUIDAlreadyExists(err error) bool { return internal.HasErrorCode(err, uidAlreadyExists) } // IsUnknown checks if the given error was due to a unknown server error. func IsUnknown(err error) bool { return internal.HasErrorCode(err, unknown) } // IsUserNotFound checks if the given error was due to non-existing user. func IsUserNotFound(err error) bool { return internal.HasErrorCode(err, userNotFound) } var serverError = map[string]string{ "CONFIGURATION_NOT_FOUND": projectNotFound, "DUPLICATE_EMAIL": emailAlreadyExists, "DUPLICATE_LOCAL_ID": uidAlreadyExists, "EMAIL_EXISTS": emailAlreadyExists, "INSUFFICIENT_PERMISSION": insufficientPermission, "PERMISSION_DENIED": insufficientPermission, "PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists, "PROJECT_NOT_FOUND": projectNotFound, "USER_NOT_FOUND": userNotFound, } func handleServerError(err error) error { gerr, ok := err.(*googleapi.Error) if !ok { // Not a back-end error return err } serverCode := gerr.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Error(clientCode, err.Error()) } // Validators. func validateDisplayName(val string) error { if val == "" { return fmt.Errorf("display name must be a non-empty string") } return nil } func validatePhotoURL(val string) error { if val == "" { return fmt.Errorf("photo url must be a non-empty string") } return nil } func validateEmail(email string) error { if email == "" { return fmt.Errorf("email must be a non-empty string") } if parts := strings.Split(email, "@"); len(parts) != 2 || parts[0] == "" || parts[1] == "" { return fmt.Errorf("malformed email string: %q", email) } return nil } func validatePassword(val string) error { if len(val) < 6 { return fmt.Errorf("password must be a string at least 6 characters long") } return nil } func validateUID(uid string) error { if uid == "" { return fmt.Errorf("uid must be a non-empty string") } if len(uid) > 128 { return fmt.Errorf("uid string must not be longer than 128 characters") } return nil } func validatePhone(phone string) error { if phone == "" { return fmt.Errorf("phone number must be a non-empty string") } if !regexp.MustCompile(`\+.*[0-9A-Za-z]`).MatchString(phone) { return fmt.Errorf("phone number must be a valid, E.164 compliant identifier") } return nil } // End of validators // Helper functions for retrieval and HTTP calls. func (c *Client) createUser(ctx context.Context, user *UserToCreate) (string, error) { if user == nil { user = &UserToCreate{} } request, err := user.validatedRequest() if err != nil { return "", err } call := c.is.Relyingparty.SignupNewUser(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return "", handleServerError(err) } return resp.LocalId, nil } func (c *Client) updateUser(ctx context.Context, uid string, user *UserToUpdate) error { if err := validateUID(uid); err != nil { return err } if user == nil { return fmt.Errorf("update parameters must not be nil or empty") } request, err := user.validatedRequest() if err != nil { return err } request.LocalId = uid call := c.is.Relyingparty.SetAccountInfo(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } func (c *Client) getUser(ctx context.Context, request *identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest) (*UserRecord, error) { call := c.is.Relyingparty.GetAccountInfo(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return nil, handleServerError(err) } if len(resp.Users) == 0 { var msg string if len(request.LocalId) == 1 { msg = fmt.Sprintf("cannot find user from uid: %q", request.LocalId[0]) } else if len(request.Email) == 1 { msg = fmt.Sprintf("cannot find user from email: %q", request.Email[0]) } else { msg = fmt.Sprintf("cannot find user from phone number: %q", request.PhoneNumber[0]) } return nil, internal.Error(userNotFound, msg) } eu, err := makeExportedUser(resp.Users[0]) if err != nil { return nil, err } return eu.UserRecord, nil } const idToolkitEndpoint = "https://identitytoolkit.googleapis.com/v1/projects" // userManagementClient is a helper for interacting with the Identity Toolkit REST API. // // Currently it is only used for some user management operations (e.g. session cookie // generation). We will gradually migrate all the user management functionality to this // type, and remove our dependency on the google.golang.org/api/identitytoolkit/v3 // package. type userManagementClient struct { baseURL string projectID string version string httpClient *internal.HTTPClient } // SessionCookie creates a new Firebase session cookie from the given ID token and expiry // duration. The returned JWT can be set as a server-side session cookie with a custom cookie // policy. Expiry duration must be at least 5 minutes but may not exceed 14 days. func (c *userManagementClient) SessionCookie( ctx context.Context, idToken string, expiresIn time.Duration, ) (string, error) { if idToken == "" { return "", errors.New("id token must not be empty") } if expiresIn < 5*time.Minute || expiresIn > 14*24*time.Hour { return "", errors.New("expiry duration must be between 5 minutes and 14 days") } payload := map[string]interface{}{ "idToken": idToken, "validDuration": int64(expiresIn.Seconds()), } resp, err := c.post(ctx, ":createSessionCookie", payload) if err != nil { return "", err } if resp.Status != http.StatusOK { return "", handleHTTPError(resp) } var result struct { SessionCookie string `json:"sessionCookie"` } err = json.Unmarshal(resp.Body, &result) return result.SessionCookie, err } func (c *userManagementClient) post( ctx context.Context, path string, payload interface{}, ) (*internal.Response, error) { req, err := c.newRequest(http.MethodPost, path) if err != nil { return nil, err } req.Body = internal.NewJSONEntity(payload) return c.httpClient.Do(ctx, req) } func (c *userManagementClient) newRequest(method, path string) (*internal.Request, error) { if c.projectID == "" { return nil, errors.New("project id not available") } versionHeader := internal.WithHeader("X-Client-Version", c.version) return &internal.Request{ Method: method, URL: fmt.Sprintf("%s/%s%s", c.baseURL, c.projectID, path), Opts: []internal.HTTPOption{versionHeader}, }, nil } func handleHTTPError(resp *internal.Response) error { var httpErr struct { Error struct { Message string `json:"message"` } `json:"error"` } json.Unmarshal(resp.Body, &httpErr) // ignore any json parse errors at this level serverCode := httpErr.Error.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Errorf( clientCode, "http error status: %d; body: %s", resp.Status, string(resp.Body)) }
setHeader
identifier_name
user_mgt.go
// Copyright 2017 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package auth import ( "context" "encoding/json" "errors" "fmt" "net/http" "regexp" "strings" "time" "firebase.google.com/go/internal" "google.golang.org/api/googleapi" "google.golang.org/api/identitytoolkit/v3" ) const ( maxLenPayloadCC = 1000 defaultProviderID = "firebase" ) // Create a new interface type identitytoolkitCall interface { Header() http.Header } // set header func (c *Client) setHeader(ic identitytoolkitCall) { ic.Header().Set("X-Client-Version", c.version) } // UserInfo is a collection of standard profile information for a user. type UserInfo struct { DisplayName string Email string PhoneNumber string PhotoURL string // In the ProviderUserInfo[] ProviderID can be a short domain name (e.g. google.com), // or the identity of an OpenID identity provider. // In UserRecord.UserInfo it will return the constant string "firebase". ProviderID string UID string } // UserMetadata contains additional metadata associated with a user account. // Timestamps are in milliseconds since epoch. type UserMetadata struct { CreationTimestamp int64 LastLogInTimestamp int64 } // UserRecord contains metadata associated with a Firebase user account. type UserRecord struct { *UserInfo CustomClaims map[string]interface{} Disabled bool EmailVerified bool ProviderUserInfo []*UserInfo TokensValidAfterMillis int64 // milliseconds since epoch. UserMetadata *UserMetadata } // UserToCreate is the parameter struct for the CreateUser function. type UserToCreate struct { createReq *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest uid bool displayName bool email bool photoURL bool phoneNumber bool } func (u *UserToCreate) request() *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest { if u.createReq == nil { u.createReq = &identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest{} } return u.createReq } func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) { req := u.request() // creating a user without any parameters is allowed if u.uid { if err := validateUID(req.LocalId); err != nil { return nil, err } } if u.displayName { if err := validateDisplayName(req.DisplayName); err != nil
} if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.phoneNumber { if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.photoURL { if err := validatePhotoURL(req.PhotoUrl); err != nil { return nil, err } } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // Disabled setter. func (u *UserToCreate) Disabled(disabled bool) *UserToCreate { req := u.request() req.Disabled = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "Disabled") } return u } // DisplayName setter. func (u *UserToCreate) DisplayName(name string) *UserToCreate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToCreate) Email(email string) *UserToCreate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToCreate) Password(pw string) *UserToCreate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToCreate) PhotoURL(url string) *UserToCreate { u.request().PhotoUrl = url u.photoURL = true return u } // UID setter. func (u *UserToCreate) UID(uid string) *UserToCreate { u.request().LocalId = uid u.uid = true return u } // UserToUpdate is the parameter struct for the UpdateUser function. type UserToUpdate struct { updateReq *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest claims map[string]interface{} displayName bool email bool phoneNumber bool photoURL bool customClaims bool } func (u *UserToUpdate) request() *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest { if u.updateReq == nil { u.updateReq = &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{} } return u.updateReq } func (u *UserToUpdate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest, error) { if u.updateReq == nil { // update without any parameters is never allowed return nil, fmt.Errorf("update parameters must not be nil or empty") } req := u.updateReq if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.displayName && req.DisplayName == "" { req.DeleteAttribute = append(req.DeleteAttribute, "DISPLAY_NAME") } if u.photoURL && req.PhotoUrl == "" { req.DeleteAttribute = append(req.DeleteAttribute, "PHOTO_URL") } if u.phoneNumber { if req.PhoneNumber == "" { req.DeleteProvider = append(req.DeleteProvider, "phone") } else if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.customClaims { cc, err := marshalCustomClaims(u.claims) if err != nil { return nil, err } req.CustomAttributes = cc } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // CustomClaims setter. func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate { u.request() // force initialization of the request for later use u.claims = claims u.customClaims = true return u } // Disabled setter. func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate { req := u.request() req.DisableUser = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "DisableUser") } return u } // DisplayName setter. func (u *UserToUpdate) DisplayName(name string) *UserToUpdate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToUpdate) Email(email string) *UserToUpdate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToUpdate) Password(pw string) *UserToUpdate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate { u.request().PhotoUrl = url u.photoURL = true return u } // revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property // to the present in epoch seconds. func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate { u.request().ValidSince = time.Now().Unix() return u } // CreateUser creates a new user with the specified properties. func (c *Client) CreateUser(ctx context.Context, user *UserToCreate) (*UserRecord, error) { uid, err := c.createUser(ctx, user) if err != nil { return nil, err } return c.GetUser(ctx, uid) } // UpdateUser updates an existing user account with the specified properties. // // DisplayName, PhotoURL and PhoneNumber will be set to "" to signify deleting them from the record. func (c *Client) UpdateUser(ctx context.Context, uid string, user *UserToUpdate) (ur *UserRecord, err error) { if err := c.updateUser(ctx, uid, user); err != nil { return nil, err } return c.GetUser(ctx, uid) } // DeleteUser deletes the user by the given UID. func (c *Client) DeleteUser(ctx context.Context, uid string) error { if err := validateUID(uid); err != nil { return err } request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{ LocalId: uid, } call := c.is.Relyingparty.DeleteAccount(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } // GetUser gets the user data corresponding to the specified user ID. func (c *Client) GetUser(ctx context.Context, uid string) (*UserRecord, error) { if err := validateUID(uid); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ LocalId: []string{uid}, } return c.getUser(ctx, request) } // GetUserByPhoneNumber gets the user data corresponding to the specified user phone number. func (c *Client) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) { if err := validatePhone(phone); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ PhoneNumber: []string{phone}, } return c.getUser(ctx, request) } // GetUserByEmail gets the user data corresponding to the specified email. func (c *Client) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) { if err := validateEmail(email); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ Email: []string{email}, } return c.getUser(ctx, request) } // RevokeRefreshTokens revokes all refresh tokens issued to a user. // // RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second. // It is important that the server on which this is called has its clock set correctly and synchronized. // // While this revokes all sessions for a specified user and disables any new ID tokens for existing sessions // from getting minted, existing ID tokens may remain active until their natural expiration (one hour). // To verify that ID tokens are revoked, use `verifyIdTokenAndCheckRevoked(ctx, idToken)`. func (c *Client) RevokeRefreshTokens(ctx context.Context, uid string) error { return c.updateUser(ctx, uid, (&UserToUpdate{}).revokeRefreshTokens()) } // SetCustomUserClaims sets additional claims on an existing user account. // // Custom claims set via this function can be used to define user roles and privilege levels. // These claims propagate to all the devices where the user is already signed in (after token // expiration or when token refresh is forced), and next time the user signs in. The claims // can be accessed via the user's ID token JWT. If a reserved OIDC claim is specified (sub, iat, // iss, etc), an error is thrown. Claims payload must also not be larger then 1000 characters // when serialized into a JSON string. func (c *Client) SetCustomUserClaims(ctx context.Context, uid string, customClaims map[string]interface{}) error { if customClaims == nil || len(customClaims) == 0 { customClaims = map[string]interface{}{} } return c.updateUser(ctx, uid, (&UserToUpdate{}).CustomClaims(customClaims)) } func marshalCustomClaims(claims map[string]interface{}) (string, error) { for _, key := range reservedClaims { if _, ok := claims[key]; ok { return "", fmt.Errorf("claim %q is reserved and must not be set", key) } } b, err := json.Marshal(claims) if err != nil { return "", fmt.Errorf("custom claims marshaling error: %v", err) } s := string(b) if s == "null" { s = "{}" // claims map has been explicitly set to nil for deletion. } if len(s) > maxLenPayloadCC { return "", fmt.Errorf("serialized custom claims must not exceed %d characters", maxLenPayloadCC) } return s, nil } // Error handlers. const ( emailAlreadyExists = "email-already-exists" idTokenRevoked = "id-token-revoked" insufficientPermission = "insufficient-permission" phoneNumberAlreadyExists = "phone-number-already-exists" projectNotFound = "project-not-found" sessionCookieRevoked = "session-cookie-revoked" uidAlreadyExists = "uid-already-exists" unknown = "unknown-error" userNotFound = "user-not-found" ) // IsEmailAlreadyExists checks if the given error was due to a duplicate email. func IsEmailAlreadyExists(err error) bool { return internal.HasErrorCode(err, emailAlreadyExists) } // IsIDTokenRevoked checks if the given error was due to a revoked ID token. func IsIDTokenRevoked(err error) bool { return internal.HasErrorCode(err, idTokenRevoked) } // IsInsufficientPermission checks if the given error was due to insufficient permissions. func IsInsufficientPermission(err error) bool { return internal.HasErrorCode(err, insufficientPermission) } // IsPhoneNumberAlreadyExists checks if the given error was due to a duplicate phone number. func IsPhoneNumberAlreadyExists(err error) bool { return internal.HasErrorCode(err, phoneNumberAlreadyExists) } // IsProjectNotFound checks if the given error was due to a non-existing project. func IsProjectNotFound(err error) bool { return internal.HasErrorCode(err, projectNotFound) } // IsSessionCookieRevoked checks if the given error was due to a revoked session cookie. func IsSessionCookieRevoked(err error) bool { return internal.HasErrorCode(err, sessionCookieRevoked) } // IsUIDAlreadyExists checks if the given error was due to a duplicate uid. func IsUIDAlreadyExists(err error) bool { return internal.HasErrorCode(err, uidAlreadyExists) } // IsUnknown checks if the given error was due to a unknown server error. func IsUnknown(err error) bool { return internal.HasErrorCode(err, unknown) } // IsUserNotFound checks if the given error was due to non-existing user. func IsUserNotFound(err error) bool { return internal.HasErrorCode(err, userNotFound) } var serverError = map[string]string{ "CONFIGURATION_NOT_FOUND": projectNotFound, "DUPLICATE_EMAIL": emailAlreadyExists, "DUPLICATE_LOCAL_ID": uidAlreadyExists, "EMAIL_EXISTS": emailAlreadyExists, "INSUFFICIENT_PERMISSION": insufficientPermission, "PERMISSION_DENIED": insufficientPermission, "PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists, "PROJECT_NOT_FOUND": projectNotFound, "USER_NOT_FOUND": userNotFound, } func handleServerError(err error) error { gerr, ok := err.(*googleapi.Error) if !ok { // Not a back-end error return err } serverCode := gerr.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Error(clientCode, err.Error()) } // Validators. func validateDisplayName(val string) error { if val == "" { return fmt.Errorf("display name must be a non-empty string") } return nil } func validatePhotoURL(val string) error { if val == "" { return fmt.Errorf("photo url must be a non-empty string") } return nil } func validateEmail(email string) error { if email == "" { return fmt.Errorf("email must be a non-empty string") } if parts := strings.Split(email, "@"); len(parts) != 2 || parts[0] == "" || parts[1] == "" { return fmt.Errorf("malformed email string: %q", email) } return nil } func validatePassword(val string) error { if len(val) < 6 { return fmt.Errorf("password must be a string at least 6 characters long") } return nil } func validateUID(uid string) error { if uid == "" { return fmt.Errorf("uid must be a non-empty string") } if len(uid) > 128 { return fmt.Errorf("uid string must not be longer than 128 characters") } return nil } func validatePhone(phone string) error { if phone == "" { return fmt.Errorf("phone number must be a non-empty string") } if !regexp.MustCompile(`\+.*[0-9A-Za-z]`).MatchString(phone) { return fmt.Errorf("phone number must be a valid, E.164 compliant identifier") } return nil } // End of validators // Helper functions for retrieval and HTTP calls. func (c *Client) createUser(ctx context.Context, user *UserToCreate) (string, error) { if user == nil { user = &UserToCreate{} } request, err := user.validatedRequest() if err != nil { return "", err } call := c.is.Relyingparty.SignupNewUser(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return "", handleServerError(err) } return resp.LocalId, nil } func (c *Client) updateUser(ctx context.Context, uid string, user *UserToUpdate) error { if err := validateUID(uid); err != nil { return err } if user == nil { return fmt.Errorf("update parameters must not be nil or empty") } request, err := user.validatedRequest() if err != nil { return err } request.LocalId = uid call := c.is.Relyingparty.SetAccountInfo(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } func (c *Client) getUser(ctx context.Context, request *identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest) (*UserRecord, error) { call := c.is.Relyingparty.GetAccountInfo(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return nil, handleServerError(err) } if len(resp.Users) == 0 { var msg string if len(request.LocalId) == 1 { msg = fmt.Sprintf("cannot find user from uid: %q", request.LocalId[0]) } else if len(request.Email) == 1 { msg = fmt.Sprintf("cannot find user from email: %q", request.Email[0]) } else { msg = fmt.Sprintf("cannot find user from phone number: %q", request.PhoneNumber[0]) } return nil, internal.Error(userNotFound, msg) } eu, err := makeExportedUser(resp.Users[0]) if err != nil { return nil, err } return eu.UserRecord, nil } const idToolkitEndpoint = "https://identitytoolkit.googleapis.com/v1/projects" // userManagementClient is a helper for interacting with the Identity Toolkit REST API. // // Currently it is only used for some user management operations (e.g. session cookie // generation). We will gradually migrate all the user management functionality to this // type, and remove our dependency on the google.golang.org/api/identitytoolkit/v3 // package. type userManagementClient struct { baseURL string projectID string version string httpClient *internal.HTTPClient } // SessionCookie creates a new Firebase session cookie from the given ID token and expiry // duration. The returned JWT can be set as a server-side session cookie with a custom cookie // policy. Expiry duration must be at least 5 minutes but may not exceed 14 days. func (c *userManagementClient) SessionCookie( ctx context.Context, idToken string, expiresIn time.Duration, ) (string, error) { if idToken == "" { return "", errors.New("id token must not be empty") } if expiresIn < 5*time.Minute || expiresIn > 14*24*time.Hour { return "", errors.New("expiry duration must be between 5 minutes and 14 days") } payload := map[string]interface{}{ "idToken": idToken, "validDuration": int64(expiresIn.Seconds()), } resp, err := c.post(ctx, ":createSessionCookie", payload) if err != nil { return "", err } if resp.Status != http.StatusOK { return "", handleHTTPError(resp) } var result struct { SessionCookie string `json:"sessionCookie"` } err = json.Unmarshal(resp.Body, &result) return result.SessionCookie, err } func (c *userManagementClient) post( ctx context.Context, path string, payload interface{}, ) (*internal.Response, error) { req, err := c.newRequest(http.MethodPost, path) if err != nil { return nil, err } req.Body = internal.NewJSONEntity(payload) return c.httpClient.Do(ctx, req) } func (c *userManagementClient) newRequest(method, path string) (*internal.Request, error) { if c.projectID == "" { return nil, errors.New("project id not available") } versionHeader := internal.WithHeader("X-Client-Version", c.version) return &internal.Request{ Method: method, URL: fmt.Sprintf("%s/%s%s", c.baseURL, c.projectID, path), Opts: []internal.HTTPOption{versionHeader}, }, nil } func handleHTTPError(resp *internal.Response) error { var httpErr struct { Error struct { Message string `json:"message"` } `json:"error"` } json.Unmarshal(resp.Body, &httpErr) // ignore any json parse errors at this level serverCode := httpErr.Error.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Errorf( clientCode, "http error status: %d; body: %s", resp.Status, string(resp.Body)) }
{ return nil, err }
conditional_block
user_mgt.go
// Copyright 2017 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package auth import ( "context" "encoding/json" "errors" "fmt" "net/http" "regexp" "strings" "time" "firebase.google.com/go/internal" "google.golang.org/api/googleapi" "google.golang.org/api/identitytoolkit/v3" ) const ( maxLenPayloadCC = 1000 defaultProviderID = "firebase" ) // Create a new interface type identitytoolkitCall interface { Header() http.Header } // set header func (c *Client) setHeader(ic identitytoolkitCall) { ic.Header().Set("X-Client-Version", c.version) } // UserInfo is a collection of standard profile information for a user. type UserInfo struct { DisplayName string Email string PhoneNumber string PhotoURL string // In the ProviderUserInfo[] ProviderID can be a short domain name (e.g. google.com), // or the identity of an OpenID identity provider. // In UserRecord.UserInfo it will return the constant string "firebase". ProviderID string UID string } // UserMetadata contains additional metadata associated with a user account. // Timestamps are in milliseconds since epoch. type UserMetadata struct { CreationTimestamp int64 LastLogInTimestamp int64 } // UserRecord contains metadata associated with a Firebase user account. type UserRecord struct { *UserInfo CustomClaims map[string]interface{} Disabled bool EmailVerified bool ProviderUserInfo []*UserInfo TokensValidAfterMillis int64 // milliseconds since epoch. UserMetadata *UserMetadata } // UserToCreate is the parameter struct for the CreateUser function. type UserToCreate struct { createReq *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest uid bool displayName bool email bool photoURL bool phoneNumber bool } func (u *UserToCreate) request() *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest { if u.createReq == nil { u.createReq = &identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest{} } return u.createReq } func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) { req := u.request() // creating a user without any parameters is allowed if u.uid { if err := validateUID(req.LocalId); err != nil { return nil, err } } if u.displayName { if err := validateDisplayName(req.DisplayName); err != nil { return nil, err } } if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.phoneNumber { if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.photoURL { if err := validatePhotoURL(req.PhotoUrl); err != nil { return nil, err } } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // Disabled setter. func (u *UserToCreate) Disabled(disabled bool) *UserToCreate { req := u.request() req.Disabled = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "Disabled") } return u } // DisplayName setter. func (u *UserToCreate) DisplayName(name string) *UserToCreate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToCreate) Email(email string) *UserToCreate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToCreate) Password(pw string) *UserToCreate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToCreate) PhotoURL(url string) *UserToCreate { u.request().PhotoUrl = url u.photoURL = true return u } // UID setter. func (u *UserToCreate) UID(uid string) *UserToCreate { u.request().LocalId = uid u.uid = true return u } // UserToUpdate is the parameter struct for the UpdateUser function. type UserToUpdate struct { updateReq *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest claims map[string]interface{} displayName bool email bool phoneNumber bool photoURL bool customClaims bool } func (u *UserToUpdate) request() *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest { if u.updateReq == nil { u.updateReq = &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{} } return u.updateReq } func (u *UserToUpdate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest, error) { if u.updateReq == nil { // update without any parameters is never allowed return nil, fmt.Errorf("update parameters must not be nil or empty") } req := u.updateReq if u.email { if err := validateEmail(req.Email); err != nil { return nil, err } } if u.displayName && req.DisplayName == "" { req.DeleteAttribute = append(req.DeleteAttribute, "DISPLAY_NAME") } if u.photoURL && req.PhotoUrl == "" { req.DeleteAttribute = append(req.DeleteAttribute, "PHOTO_URL") } if u.phoneNumber { if req.PhoneNumber == "" { req.DeleteProvider = append(req.DeleteProvider, "phone") } else if err := validatePhone(req.PhoneNumber); err != nil { return nil, err } } if u.customClaims { cc, err := marshalCustomClaims(u.claims) if err != nil { return nil, err } req.CustomAttributes = cc } if req.Password != "" { if err := validatePassword(req.Password); err != nil { return nil, err } } return req, nil } // CustomClaims setter. func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate { u.request() // force initialization of the request for later use u.claims = claims u.customClaims = true return u } // Disabled setter. func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate { req := u.request() req.DisableUser = disabled if !disabled { req.ForceSendFields = append(req.ForceSendFields, "DisableUser") } return u } // DisplayName setter. func (u *UserToUpdate) DisplayName(name string) *UserToUpdate { u.request().DisplayName = name u.displayName = true return u } // Email setter. func (u *UserToUpdate) Email(email string) *UserToUpdate { u.request().Email = email u.email = true return u } // EmailVerified setter. func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate { req := u.request() req.EmailVerified = verified if !verified { req.ForceSendFields = append(req.ForceSendFields, "EmailVerified") } return u } // Password setter. func (u *UserToUpdate) Password(pw string) *UserToUpdate { u.request().Password = pw return u } // PhoneNumber setter. func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate { u.request().PhoneNumber = phone u.phoneNumber = true return u } // PhotoURL setter. func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate { u.request().PhotoUrl = url u.photoURL = true return u } // revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property // to the present in epoch seconds. func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate { u.request().ValidSince = time.Now().Unix() return u } // CreateUser creates a new user with the specified properties. func (c *Client) CreateUser(ctx context.Context, user *UserToCreate) (*UserRecord, error) { uid, err := c.createUser(ctx, user) if err != nil { return nil, err } return c.GetUser(ctx, uid) } // UpdateUser updates an existing user account with the specified properties. // // DisplayName, PhotoURL and PhoneNumber will be set to "" to signify deleting them from the record. func (c *Client) UpdateUser(ctx context.Context, uid string, user *UserToUpdate) (ur *UserRecord, err error) { if err := c.updateUser(ctx, uid, user); err != nil { return nil, err } return c.GetUser(ctx, uid) } // DeleteUser deletes the user by the given UID. func (c *Client) DeleteUser(ctx context.Context, uid string) error { if err := validateUID(uid); err != nil { return err } request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{ LocalId: uid, } call := c.is.Relyingparty.DeleteAccount(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } // GetUser gets the user data corresponding to the specified user ID. func (c *Client) GetUser(ctx context.Context, uid string) (*UserRecord, error) { if err := validateUID(uid); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ LocalId: []string{uid}, } return c.getUser(ctx, request) } // GetUserByPhoneNumber gets the user data corresponding to the specified user phone number. func (c *Client) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) { if err := validatePhone(phone); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ PhoneNumber: []string{phone}, } return c.getUser(ctx, request) } // GetUserByEmail gets the user data corresponding to the specified email. func (c *Client) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) { if err := validateEmail(email); err != nil { return nil, err } request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{ Email: []string{email}, } return c.getUser(ctx, request) } // RevokeRefreshTokens revokes all refresh tokens issued to a user. // // RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second. // It is important that the server on which this is called has its clock set correctly and synchronized. // // While this revokes all sessions for a specified user and disables any new ID tokens for existing sessions // from getting minted, existing ID tokens may remain active until their natural expiration (one hour). // To verify that ID tokens are revoked, use `verifyIdTokenAndCheckRevoked(ctx, idToken)`. func (c *Client) RevokeRefreshTokens(ctx context.Context, uid string) error { return c.updateUser(ctx, uid, (&UserToUpdate{}).revokeRefreshTokens()) } // SetCustomUserClaims sets additional claims on an existing user account. // // Custom claims set via this function can be used to define user roles and privilege levels. // These claims propagate to all the devices where the user is already signed in (after token // expiration or when token refresh is forced), and next time the user signs in. The claims // can be accessed via the user's ID token JWT. If a reserved OIDC claim is specified (sub, iat, // iss, etc), an error is thrown. Claims payload must also not be larger then 1000 characters // when serialized into a JSON string. func (c *Client) SetCustomUserClaims(ctx context.Context, uid string, customClaims map[string]interface{}) error { if customClaims == nil || len(customClaims) == 0 { customClaims = map[string]interface{}{} } return c.updateUser(ctx, uid, (&UserToUpdate{}).CustomClaims(customClaims)) } func marshalCustomClaims(claims map[string]interface{}) (string, error) { for _, key := range reservedClaims { if _, ok := claims[key]; ok { return "", fmt.Errorf("claim %q is reserved and must not be set", key) } } b, err := json.Marshal(claims) if err != nil { return "", fmt.Errorf("custom claims marshaling error: %v", err) } s := string(b) if s == "null" { s = "{}" // claims map has been explicitly set to nil for deletion. } if len(s) > maxLenPayloadCC { return "", fmt.Errorf("serialized custom claims must not exceed %d characters", maxLenPayloadCC) } return s, nil } // Error handlers. const ( emailAlreadyExists = "email-already-exists" idTokenRevoked = "id-token-revoked" insufficientPermission = "insufficient-permission" phoneNumberAlreadyExists = "phone-number-already-exists" projectNotFound = "project-not-found" sessionCookieRevoked = "session-cookie-revoked" uidAlreadyExists = "uid-already-exists" unknown = "unknown-error" userNotFound = "user-not-found" ) // IsEmailAlreadyExists checks if the given error was due to a duplicate email. func IsEmailAlreadyExists(err error) bool { return internal.HasErrorCode(err, emailAlreadyExists) } // IsIDTokenRevoked checks if the given error was due to a revoked ID token. func IsIDTokenRevoked(err error) bool { return internal.HasErrorCode(err, idTokenRevoked) } // IsInsufficientPermission checks if the given error was due to insufficient permissions. func IsInsufficientPermission(err error) bool { return internal.HasErrorCode(err, insufficientPermission) } // IsPhoneNumberAlreadyExists checks if the given error was due to a duplicate phone number. func IsPhoneNumberAlreadyExists(err error) bool { return internal.HasErrorCode(err, phoneNumberAlreadyExists) } // IsProjectNotFound checks if the given error was due to a non-existing project. func IsProjectNotFound(err error) bool { return internal.HasErrorCode(err, projectNotFound) } // IsSessionCookieRevoked checks if the given error was due to a revoked session cookie. func IsSessionCookieRevoked(err error) bool { return internal.HasErrorCode(err, sessionCookieRevoked) } // IsUIDAlreadyExists checks if the given error was due to a duplicate uid. func IsUIDAlreadyExists(err error) bool { return internal.HasErrorCode(err, uidAlreadyExists) } // IsUnknown checks if the given error was due to a unknown server error. func IsUnknown(err error) bool { return internal.HasErrorCode(err, unknown) } // IsUserNotFound checks if the given error was due to non-existing user. func IsUserNotFound(err error) bool
var serverError = map[string]string{ "CONFIGURATION_NOT_FOUND": projectNotFound, "DUPLICATE_EMAIL": emailAlreadyExists, "DUPLICATE_LOCAL_ID": uidAlreadyExists, "EMAIL_EXISTS": emailAlreadyExists, "INSUFFICIENT_PERMISSION": insufficientPermission, "PERMISSION_DENIED": insufficientPermission, "PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists, "PROJECT_NOT_FOUND": projectNotFound, "USER_NOT_FOUND": userNotFound, } func handleServerError(err error) error { gerr, ok := err.(*googleapi.Error) if !ok { // Not a back-end error return err } serverCode := gerr.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Error(clientCode, err.Error()) } // Validators. func validateDisplayName(val string) error { if val == "" { return fmt.Errorf("display name must be a non-empty string") } return nil } func validatePhotoURL(val string) error { if val == "" { return fmt.Errorf("photo url must be a non-empty string") } return nil } func validateEmail(email string) error { if email == "" { return fmt.Errorf("email must be a non-empty string") } if parts := strings.Split(email, "@"); len(parts) != 2 || parts[0] == "" || parts[1] == "" { return fmt.Errorf("malformed email string: %q", email) } return nil } func validatePassword(val string) error { if len(val) < 6 { return fmt.Errorf("password must be a string at least 6 characters long") } return nil } func validateUID(uid string) error { if uid == "" { return fmt.Errorf("uid must be a non-empty string") } if len(uid) > 128 { return fmt.Errorf("uid string must not be longer than 128 characters") } return nil } func validatePhone(phone string) error { if phone == "" { return fmt.Errorf("phone number must be a non-empty string") } if !regexp.MustCompile(`\+.*[0-9A-Za-z]`).MatchString(phone) { return fmt.Errorf("phone number must be a valid, E.164 compliant identifier") } return nil } // End of validators // Helper functions for retrieval and HTTP calls. func (c *Client) createUser(ctx context.Context, user *UserToCreate) (string, error) { if user == nil { user = &UserToCreate{} } request, err := user.validatedRequest() if err != nil { return "", err } call := c.is.Relyingparty.SignupNewUser(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return "", handleServerError(err) } return resp.LocalId, nil } func (c *Client) updateUser(ctx context.Context, uid string, user *UserToUpdate) error { if err := validateUID(uid); err != nil { return err } if user == nil { return fmt.Errorf("update parameters must not be nil or empty") } request, err := user.validatedRequest() if err != nil { return err } request.LocalId = uid call := c.is.Relyingparty.SetAccountInfo(request) c.setHeader(call) if _, err := call.Context(ctx).Do(); err != nil { return handleServerError(err) } return nil } func (c *Client) getUser(ctx context.Context, request *identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest) (*UserRecord, error) { call := c.is.Relyingparty.GetAccountInfo(request) c.setHeader(call) resp, err := call.Context(ctx).Do() if err != nil { return nil, handleServerError(err) } if len(resp.Users) == 0 { var msg string if len(request.LocalId) == 1 { msg = fmt.Sprintf("cannot find user from uid: %q", request.LocalId[0]) } else if len(request.Email) == 1 { msg = fmt.Sprintf("cannot find user from email: %q", request.Email[0]) } else { msg = fmt.Sprintf("cannot find user from phone number: %q", request.PhoneNumber[0]) } return nil, internal.Error(userNotFound, msg) } eu, err := makeExportedUser(resp.Users[0]) if err != nil { return nil, err } return eu.UserRecord, nil } const idToolkitEndpoint = "https://identitytoolkit.googleapis.com/v1/projects" // userManagementClient is a helper for interacting with the Identity Toolkit REST API. // // Currently it is only used for some user management operations (e.g. session cookie // generation). We will gradually migrate all the user management functionality to this // type, and remove our dependency on the google.golang.org/api/identitytoolkit/v3 // package. type userManagementClient struct { baseURL string projectID string version string httpClient *internal.HTTPClient } // SessionCookie creates a new Firebase session cookie from the given ID token and expiry // duration. The returned JWT can be set as a server-side session cookie with a custom cookie // policy. Expiry duration must be at least 5 minutes but may not exceed 14 days. func (c *userManagementClient) SessionCookie( ctx context.Context, idToken string, expiresIn time.Duration, ) (string, error) { if idToken == "" { return "", errors.New("id token must not be empty") } if expiresIn < 5*time.Minute || expiresIn > 14*24*time.Hour { return "", errors.New("expiry duration must be between 5 minutes and 14 days") } payload := map[string]interface{}{ "idToken": idToken, "validDuration": int64(expiresIn.Seconds()), } resp, err := c.post(ctx, ":createSessionCookie", payload) if err != nil { return "", err } if resp.Status != http.StatusOK { return "", handleHTTPError(resp) } var result struct { SessionCookie string `json:"sessionCookie"` } err = json.Unmarshal(resp.Body, &result) return result.SessionCookie, err } func (c *userManagementClient) post( ctx context.Context, path string, payload interface{}, ) (*internal.Response, error) { req, err := c.newRequest(http.MethodPost, path) if err != nil { return nil, err } req.Body = internal.NewJSONEntity(payload) return c.httpClient.Do(ctx, req) } func (c *userManagementClient) newRequest(method, path string) (*internal.Request, error) { if c.projectID == "" { return nil, errors.New("project id not available") } versionHeader := internal.WithHeader("X-Client-Version", c.version) return &internal.Request{ Method: method, URL: fmt.Sprintf("%s/%s%s", c.baseURL, c.projectID, path), Opts: []internal.HTTPOption{versionHeader}, }, nil } func handleHTTPError(resp *internal.Response) error { var httpErr struct { Error struct { Message string `json:"message"` } `json:"error"` } json.Unmarshal(resp.Body, &httpErr) // ignore any json parse errors at this level serverCode := httpErr.Error.Message clientCode, ok := serverError[serverCode] if !ok { clientCode = unknown } return internal.Errorf( clientCode, "http error status: %d; body: %s", resp.Status, string(resp.Body)) }
{ return internal.HasErrorCode(err, userNotFound) }
identifier_body
bagpreparer.go
package workers import ( "encoding/json" "fmt" "github.com/APTrust/bagman/bagman" "github.com/nsqio/go-nsq" "os" "strings" "time" ) // Large file is ~50GB const LARGE_FILE_SIZE = int64(50000000000) // apt_prepare receives messages from nsqd describing // items in the S3 receiving buckets. It fetches, untars, // and validates tar files, then queues them for storage, // if they untar and validate successfully. Each item/message // follows this flow: // // 1. Fetch channel: fetches the file from S3. // 2. Unpack channel: untars the bag files, parses and validates // the bag, reads tags, generates checksums and generic file // UUIDs. // 3. Results channel: tells the queue whether processing // succeeded, and if not, whether the item should be requeued. // Also logs results to json and message logs. // 4. Cleanup channel: cleans up the files after processing // completes. // // If a failure occurs anywhere in the first three steps, // processing goes directly to the Results Channel, which // records the error and the disposition (retry/give up). // // As long as the message from nsq contains valid JSON, // steps 4 and 5 ALWAYS run. // // Some notes... It's essential that that the fetchChannel // be limited to a relatively low number. If we are downloading // 1GB tar files, we need space to store the tar file AND the // untarred version. That's about 2 x 1GB. We do not want to pull // down 1000 files at once, or we'll run out of disk space! // If config sets fetchers to 10, we can pull down 10 files at a // time. The fetch queue could hold 10 * 4 = 40 items, so we'd // have max 40 tar files + untarred directories on disk at once. // The number of workers should be close to the number of CPU // cores. // // We do NOT want one go routine per S3 file. If we do that, // the system will run out of file handles, as we'll have tens // of thousands of open connections to S3 trying to write data // into tens of thousands of local files. type BagPreparer struct { FetchChannel chan *bagman.IngestHelper UnpackChannel chan *bagman.IngestHelper CleanUpChannel chan *bagman.IngestHelper ResultsChannel chan *bagman.IngestHelper ProcUtil *bagman.ProcessUtil largeFile1 string largeFile2 string } func NewBagPreparer(procUtil *bagman.ProcessUtil) (*BagPreparer) { bagPreparer := &BagPreparer{ ProcUtil: procUtil, } // Set up buffered channels fetcherBufferSize := procUtil.Config.PrepareWorker.NetworkConnections * 4 workerBufferSize := procUtil.Config.PrepareWorker.Workers * 10 bagPreparer.FetchChannel = make(chan *bagman.IngestHelper, fetcherBufferSize) bagPreparer.UnpackChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.CleanUpChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.ResultsChannel = make(chan *bagman.IngestHelper, workerBufferSize) // Set up a limited number of go routines for i := 0; i < procUtil.Config.PrepareWorker.NetworkConnections; i++ { go bagPreparer.doFetch() } for i := 0; i < procUtil.Config.PrepareWorker.Workers; i++ { go bagPreparer.doUnpack() go bagPreparer.logResult() go bagPreparer.doCleanUp() } return bagPreparer } // MessageHandler handles messages from the queue, putting each // item into the pipleline. func (bagPreparer *BagPreparer)
(message *nsq.Message) error { message.DisableAutoResponse() var s3File bagman.S3File err := json.Unmarshal(message.Body, &s3File) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Could not unmarshal JSON data from nsq:", string(message.Body)) message.Finish() return nil } // If we're not reprocessing on purpose, and this item has already // been successfully processed, skip it. There are certain timing // conditions that can cause the bucket reader to add items to the // queue twice. If we get rid of NSQ, we can get rid of this check. if bagPreparer.ProcUtil.Config.SkipAlreadyProcessed == true && bagman.BagNeedsProcessing(&s3File, bagPreparer.ProcUtil) == false { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete, without processing because "+ "Config.SkipAlreadyProcessed = true and this bag was ingested or is currently "+ "being processed.", s3File.Key.Key) message.Finish() return nil } // Don't start ingest if there's a pending delete or restore request. // Ingest would just overwrite the files and metadata that delete/restore // would be operating on. If there is a pending delete/restore request, // send this back into the queue with an hour or so backoff time. // // If we can't parse the bag date, it's OK to send an empty date into // the search. We may pull back a few extra records and get a false positive // on the pending delete/restore. A false positive will delay ingest, but a // false negative could cause some cascading errors. bagDate, _ := time.Parse(bagman.S3DateFormat, s3File.Key.LastModified) processStatus := &bagman.ProcessStatus { ETag: strings.Replace(s3File.Key.ETag, "\"", "", -1), Name: s3File.Key.Key, BagDate: bagDate, } statusRecords, err := bagPreparer.ProcUtil.FluctusClient.ProcessStatusSearch(processStatus, true, true) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error fetching status info on bag %s " + "from Fluctus. Will retry in 5 minutes. Error: %v", s3File.Key.Key, err) message.Requeue(5 * time.Minute) return nil } if bagman.HasPendingDeleteRequest(statusRecords) || bagman.HasPendingRestoreRequest(statusRecords) { bagPreparer.ProcUtil.MessageLog.Info("Requeuing %s due to pending delete or " + "restore request. Will retry in at least 60 minutes.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } // Special case for very large bags: the bag is in process under // the same ID. NSQ thinks it timed out and has re-sent it. In this // case, return nil so NSQ knows we're OK, but don't finish the message. // The original process will call Finish() on the message when it's // done. If we call Finish() here, NSQ will throw a "not-in-flight" // error when the processor calls Finish() on the original message later. currentMessageId := bagPreparer.ProcUtil.MessageIdString(message.ID) if bagPreparer.ProcUtil.BagAlreadyInProgress(&s3File, currentMessageId) { bagPreparer.ProcUtil.MessageLog.Info("Bag %s is already in progress under message id '%s'", s3File.Key.Key, bagPreparer.ProcUtil.MessageIdFor(s3File.BagName())) return nil } // For very large files, do max two at a time so we don't get cut off // from S3 for going 20+ seconds without a read. If we do multiple // large files at once, we get cut off from S3 often. We can do lots // of small files while one or two large ones are processing. if s3File.Key.Size > LARGE_FILE_SIZE { if bagPreparer.largeFile1 == "" { bagPreparer.largeFile1 = s3File.BagName() } else if bagPreparer.largeFile2 == "" { bagPreparer.largeFile2 = s3File.BagName() } else { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s because is >50GB and there are " + "already two large files in progress.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } } // Don't start working on a message that we're already working on. // Note that the key we include in the syncMap includes multipart // bag endings, so we can be working on ncsu.edu/obj.b1of2.tar and // ncsu.edu/obj.b2of2.tar at the same time. This is what we want. mapErr := bagPreparer.ProcUtil.RegisterItem(s3File.BagName(), message.ID) if mapErr != nil { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete because the file is already "+ "being processed under another message id.\n", s3File.Key.Key) message.Finish() return nil } // Create the result struct and pass it down the pipeline helper := bagman.NewIngestHelper(bagPreparer.ProcUtil, message, &s3File) bagPreparer.FetchChannel <- helper bagPreparer.ProcUtil.MessageLog.Debug("Put %s into fetch queue", s3File.Key.Key) return nil } // -- Step 1 of 5 -- // This runs as a go routine to fetch files from S3. func (bagPreparer *BagPreparer) doFetch() { for helper := range bagPreparer.FetchChannel { result := helper.Result result.NsqMessage.Touch() s3Key := result.S3File.Key // Disk needs filesize * 2 disk space to accomodate tar file & untarred files err := bagPreparer.ProcUtil.Volume.Reserve(uint64(s3Key.Size * 2)) if err != nil { // Not enough room on disk bagPreparer.ProcUtil.MessageLog.Warning("Requeueing %s - not enough disk space", s3Key.Key) result.ErrorMessage = err.Error() result.Retry = true bagPreparer.ResultsChannel <- helper } else { bagPreparer.ProcUtil.MessageLog.Info("Fetching %s", s3Key.Key) helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusStarted) helper.FetchTarFile() if result.ErrorMessage != "" { // Fetch from S3 failed. Requeue. bagPreparer.ResultsChannel <- helper } else { // Got S3 file. Untar it. // And touch the message, so nsqd knows we're making progress. result.NsqMessage.Touch() helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusPending) bagPreparer.UnpackChannel <- helper } } } } // -- Step 2 of 5 -- // This runs as a go routine to untar files downloaded from S3. // We calculate checksums and create generic files during the unpack // stage to avoid having to reprocess large streams of data several times. func (bagPreparer *BagPreparer) doUnpack() { for helper := range bagPreparer.UnpackChannel { result := helper.Result if result.ErrorMessage != "" { // Unpack failed. Go to end. bagPreparer.ProcUtil.MessageLog.Warning("Nothing to unpack for %s", result.S3File.Key.Key) bagPreparer.ResultsChannel <- helper } else { // Unpacked! Now process the bag and touch message // so nsqd knows we're making progress. bagPreparer.ProcUtil.MessageLog.Info("Unpacking %s", result.S3File.Key.Key) // Touch when we start result.NsqMessage.Touch() // Processing can take 3+ hours for very large files! helper.UpdateFluctusStatus(bagman.StageUnpack, bagman.StatusStarted) helper.ProcessBagFile() helper.UpdateFluctusStatus(bagman.StageValidate, bagman.StatusPending) // And touch again when we're done result.NsqMessage.Touch() bagPreparer.ResultsChannel <- helper } } } // -- Step 4 of 5 -- // This prints to the log the result of the program's attempt to fetch, // untar, unbag and verify an individual S3 tar file. It logs state info // about this bag to a json file on the local file system. Also logs // a text message to the local bag_processor.log file and sends info // to Fluctus saying whether the bag succeeded or failed. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) logResult() { for helper := range bagPreparer.ResultsChannel { result := helper.Result result.NsqMessage.Touch() helper.LogResult() bagPreparer.CleanUpChannel <- helper } } // -- Step 5 of 5 -- // This runs as a go routine to remove the files we downloaded // and untarred. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) doCleanUp() { for helper := range bagPreparer.CleanUpChannel { result := helper.Result result.NsqMessage.Touch() bagPreparer.ProcUtil.MessageLog.Debug("Cleaning up %s", result.S3File.Key.Key) if (result.S3File.Key.Key != "" && result.FetchResult != nil && result.FetchResult.LocalFile != "") { bagPreparer.cleanupBag(helper) } // Build and send message back to NSQ, indicating whether // processing succeeded. if result.ErrorMessage != "" { if result.Retry == true { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s", result.S3File.Key.Key) result.NsqMessage.Requeue(5 * time.Minute) } else { result.NsqMessage.Finish() } } else { // Prepare succeeded. Send this off to storage queue, // so the generic files can go into long-term storage. bagPreparer.SendToStorageQueue(helper) result.NsqMessage.Finish() } // We're done processing this, so remove it from the map. // If it comes in again, we'll reprocess it again. bagPreparer.ProcUtil.UnregisterItem(result.S3File.BagName()) if bagPreparer.largeFile1 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile1 %s", result.S3File.Key.Key) bagPreparer.largeFile1 = "" } else if bagPreparer.largeFile2 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile2 %s", result.S3File.Key.Key) bagPreparer.largeFile2 = "" } } } func (bagPreparer *BagPreparer) cleanupBag(helper *bagman.IngestHelper) { result := helper.Result if result.ErrorMessage == "" { // Clean up the tar file, but leave the unpacked files // for apt_store to send off to long-term storage. err := os.Remove(result.FetchResult.LocalFile) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error deleting tar file %s: %v", result.FetchResult.LocalFile, err) } } else { // Clean up ALL files we downloaded and unpacked errors := helper.DeleteLocalFiles() if errors != nil && len(errors) > 0 { bagPreparer.ProcUtil.MessageLog.Warning("Errors cleaning up %s", result.FetchResult.LocalFile) for _, e := range errors { bagPreparer.ProcUtil.MessageLog.Error(e.Error()) } } } bagPreparer.ProcUtil.Volume.Release(uint64(result.S3File.Key.Size * 2)) } // Puts an item into the queue for Fluctus/Fedora metadata processing. func (bagPreparer *BagPreparer) SendToStorageQueue(helper *bagman.IngestHelper) { err := bagman.Enqueue(helper.ProcUtil.Config.NsqdHttpAddress, helper.ProcUtil.Config.StoreWorker.NsqTopic, helper.Result) if err != nil { errMsg := fmt.Sprintf("Error adding '%s' to storage queue: %v ", helper.Result.S3File.Key.Key, err) helper.ProcUtil.MessageLog.Error(errMsg) helper.Result.ErrorMessage += errMsg } else { helper.ProcUtil.MessageLog.Debug("Sent '%s' to storage queue", helper.Result.S3File.Key.Key) } }
HandleMessage
identifier_name
bagpreparer.go
package workers import ( "encoding/json" "fmt" "github.com/APTrust/bagman/bagman" "github.com/nsqio/go-nsq" "os" "strings" "time" ) // Large file is ~50GB const LARGE_FILE_SIZE = int64(50000000000) // apt_prepare receives messages from nsqd describing // items in the S3 receiving buckets. It fetches, untars, // and validates tar files, then queues them for storage, // if they untar and validate successfully. Each item/message // follows this flow: // // 1. Fetch channel: fetches the file from S3. // 2. Unpack channel: untars the bag files, parses and validates // the bag, reads tags, generates checksums and generic file // UUIDs. // 3. Results channel: tells the queue whether processing // succeeded, and if not, whether the item should be requeued. // Also logs results to json and message logs. // 4. Cleanup channel: cleans up the files after processing // completes. // // If a failure occurs anywhere in the first three steps, // processing goes directly to the Results Channel, which // records the error and the disposition (retry/give up). // // As long as the message from nsq contains valid JSON, // steps 4 and 5 ALWAYS run. // // Some notes... It's essential that that the fetchChannel // be limited to a relatively low number. If we are downloading // 1GB tar files, we need space to store the tar file AND the // untarred version. That's about 2 x 1GB. We do not want to pull // down 1000 files at once, or we'll run out of disk space! // If config sets fetchers to 10, we can pull down 10 files at a // time. The fetch queue could hold 10 * 4 = 40 items, so we'd // have max 40 tar files + untarred directories on disk at once. // The number of workers should be close to the number of CPU // cores. // // We do NOT want one go routine per S3 file. If we do that, // the system will run out of file handles, as we'll have tens // of thousands of open connections to S3 trying to write data // into tens of thousands of local files. type BagPreparer struct { FetchChannel chan *bagman.IngestHelper UnpackChannel chan *bagman.IngestHelper CleanUpChannel chan *bagman.IngestHelper ResultsChannel chan *bagman.IngestHelper ProcUtil *bagman.ProcessUtil largeFile1 string largeFile2 string } func NewBagPreparer(procUtil *bagman.ProcessUtil) (*BagPreparer) { bagPreparer := &BagPreparer{ ProcUtil: procUtil, } // Set up buffered channels fetcherBufferSize := procUtil.Config.PrepareWorker.NetworkConnections * 4 workerBufferSize := procUtil.Config.PrepareWorker.Workers * 10 bagPreparer.FetchChannel = make(chan *bagman.IngestHelper, fetcherBufferSize) bagPreparer.UnpackChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.CleanUpChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.ResultsChannel = make(chan *bagman.IngestHelper, workerBufferSize) // Set up a limited number of go routines for i := 0; i < procUtil.Config.PrepareWorker.NetworkConnections; i++ { go bagPreparer.doFetch() } for i := 0; i < procUtil.Config.PrepareWorker.Workers; i++ { go bagPreparer.doUnpack() go bagPreparer.logResult() go bagPreparer.doCleanUp() } return bagPreparer } // MessageHandler handles messages from the queue, putting each // item into the pipleline. func (bagPreparer *BagPreparer) HandleMessage(message *nsq.Message) error { message.DisableAutoResponse() var s3File bagman.S3File err := json.Unmarshal(message.Body, &s3File) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Could not unmarshal JSON data from nsq:", string(message.Body)) message.Finish() return nil } // If we're not reprocessing on purpose, and this item has already // been successfully processed, skip it. There are certain timing // conditions that can cause the bucket reader to add items to the // queue twice. If we get rid of NSQ, we can get rid of this check. if bagPreparer.ProcUtil.Config.SkipAlreadyProcessed == true && bagman.BagNeedsProcessing(&s3File, bagPreparer.ProcUtil) == false { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete, without processing because "+ "Config.SkipAlreadyProcessed = true and this bag was ingested or is currently "+ "being processed.", s3File.Key.Key) message.Finish() return nil } // Don't start ingest if there's a pending delete or restore request. // Ingest would just overwrite the files and metadata that delete/restore // would be operating on. If there is a pending delete/restore request, // send this back into the queue with an hour or so backoff time. // // If we can't parse the bag date, it's OK to send an empty date into // the search. We may pull back a few extra records and get a false positive // on the pending delete/restore. A false positive will delay ingest, but a // false negative could cause some cascading errors. bagDate, _ := time.Parse(bagman.S3DateFormat, s3File.Key.LastModified) processStatus := &bagman.ProcessStatus { ETag: strings.Replace(s3File.Key.ETag, "\"", "", -1), Name: s3File.Key.Key, BagDate: bagDate, } statusRecords, err := bagPreparer.ProcUtil.FluctusClient.ProcessStatusSearch(processStatus, true, true) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error fetching status info on bag %s " + "from Fluctus. Will retry in 5 minutes. Error: %v", s3File.Key.Key, err) message.Requeue(5 * time.Minute) return nil } if bagman.HasPendingDeleteRequest(statusRecords) || bagman.HasPendingRestoreRequest(statusRecords) { bagPreparer.ProcUtil.MessageLog.Info("Requeuing %s due to pending delete or " + "restore request. Will retry in at least 60 minutes.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } // Special case for very large bags: the bag is in process under // the same ID. NSQ thinks it timed out and has re-sent it. In this // case, return nil so NSQ knows we're OK, but don't finish the message. // The original process will call Finish() on the message when it's // done. If we call Finish() here, NSQ will throw a "not-in-flight" // error when the processor calls Finish() on the original message later. currentMessageId := bagPreparer.ProcUtil.MessageIdString(message.ID) if bagPreparer.ProcUtil.BagAlreadyInProgress(&s3File, currentMessageId) { bagPreparer.ProcUtil.MessageLog.Info("Bag %s is already in progress under message id '%s'", s3File.Key.Key, bagPreparer.ProcUtil.MessageIdFor(s3File.BagName())) return nil } // For very large files, do max two at a time so we don't get cut off // from S3 for going 20+ seconds without a read. If we do multiple // large files at once, we get cut off from S3 often. We can do lots // of small files while one or two large ones are processing. if s3File.Key.Size > LARGE_FILE_SIZE { if bagPreparer.largeFile1 == "" { bagPreparer.largeFile1 = s3File.BagName() } else if bagPreparer.largeFile2 == "" { bagPreparer.largeFile2 = s3File.BagName() } else { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s because is >50GB and there are " + "already two large files in progress.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } } // Don't start working on a message that we're already working on. // Note that the key we include in the syncMap includes multipart // bag endings, so we can be working on ncsu.edu/obj.b1of2.tar and // ncsu.edu/obj.b2of2.tar at the same time. This is what we want. mapErr := bagPreparer.ProcUtil.RegisterItem(s3File.BagName(), message.ID) if mapErr != nil { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete because the file is already "+ "being processed under another message id.\n", s3File.Key.Key) message.Finish() return nil } // Create the result struct and pass it down the pipeline helper := bagman.NewIngestHelper(bagPreparer.ProcUtil, message, &s3File) bagPreparer.FetchChannel <- helper bagPreparer.ProcUtil.MessageLog.Debug("Put %s into fetch queue", s3File.Key.Key) return nil } // -- Step 1 of 5 -- // This runs as a go routine to fetch files from S3. func (bagPreparer *BagPreparer) doFetch()
// -- Step 2 of 5 -- // This runs as a go routine to untar files downloaded from S3. // We calculate checksums and create generic files during the unpack // stage to avoid having to reprocess large streams of data several times. func (bagPreparer *BagPreparer) doUnpack() { for helper := range bagPreparer.UnpackChannel { result := helper.Result if result.ErrorMessage != "" { // Unpack failed. Go to end. bagPreparer.ProcUtil.MessageLog.Warning("Nothing to unpack for %s", result.S3File.Key.Key) bagPreparer.ResultsChannel <- helper } else { // Unpacked! Now process the bag and touch message // so nsqd knows we're making progress. bagPreparer.ProcUtil.MessageLog.Info("Unpacking %s", result.S3File.Key.Key) // Touch when we start result.NsqMessage.Touch() // Processing can take 3+ hours for very large files! helper.UpdateFluctusStatus(bagman.StageUnpack, bagman.StatusStarted) helper.ProcessBagFile() helper.UpdateFluctusStatus(bagman.StageValidate, bagman.StatusPending) // And touch again when we're done result.NsqMessage.Touch() bagPreparer.ResultsChannel <- helper } } } // -- Step 4 of 5 -- // This prints to the log the result of the program's attempt to fetch, // untar, unbag and verify an individual S3 tar file. It logs state info // about this bag to a json file on the local file system. Also logs // a text message to the local bag_processor.log file and sends info // to Fluctus saying whether the bag succeeded or failed. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) logResult() { for helper := range bagPreparer.ResultsChannel { result := helper.Result result.NsqMessage.Touch() helper.LogResult() bagPreparer.CleanUpChannel <- helper } } // -- Step 5 of 5 -- // This runs as a go routine to remove the files we downloaded // and untarred. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) doCleanUp() { for helper := range bagPreparer.CleanUpChannel { result := helper.Result result.NsqMessage.Touch() bagPreparer.ProcUtil.MessageLog.Debug("Cleaning up %s", result.S3File.Key.Key) if (result.S3File.Key.Key != "" && result.FetchResult != nil && result.FetchResult.LocalFile != "") { bagPreparer.cleanupBag(helper) } // Build and send message back to NSQ, indicating whether // processing succeeded. if result.ErrorMessage != "" { if result.Retry == true { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s", result.S3File.Key.Key) result.NsqMessage.Requeue(5 * time.Minute) } else { result.NsqMessage.Finish() } } else { // Prepare succeeded. Send this off to storage queue, // so the generic files can go into long-term storage. bagPreparer.SendToStorageQueue(helper) result.NsqMessage.Finish() } // We're done processing this, so remove it from the map. // If it comes in again, we'll reprocess it again. bagPreparer.ProcUtil.UnregisterItem(result.S3File.BagName()) if bagPreparer.largeFile1 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile1 %s", result.S3File.Key.Key) bagPreparer.largeFile1 = "" } else if bagPreparer.largeFile2 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile2 %s", result.S3File.Key.Key) bagPreparer.largeFile2 = "" } } } func (bagPreparer *BagPreparer) cleanupBag(helper *bagman.IngestHelper) { result := helper.Result if result.ErrorMessage == "" { // Clean up the tar file, but leave the unpacked files // for apt_store to send off to long-term storage. err := os.Remove(result.FetchResult.LocalFile) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error deleting tar file %s: %v", result.FetchResult.LocalFile, err) } } else { // Clean up ALL files we downloaded and unpacked errors := helper.DeleteLocalFiles() if errors != nil && len(errors) > 0 { bagPreparer.ProcUtil.MessageLog.Warning("Errors cleaning up %s", result.FetchResult.LocalFile) for _, e := range errors { bagPreparer.ProcUtil.MessageLog.Error(e.Error()) } } } bagPreparer.ProcUtil.Volume.Release(uint64(result.S3File.Key.Size * 2)) } // Puts an item into the queue for Fluctus/Fedora metadata processing. func (bagPreparer *BagPreparer) SendToStorageQueue(helper *bagman.IngestHelper) { err := bagman.Enqueue(helper.ProcUtil.Config.NsqdHttpAddress, helper.ProcUtil.Config.StoreWorker.NsqTopic, helper.Result) if err != nil { errMsg := fmt.Sprintf("Error adding '%s' to storage queue: %v ", helper.Result.S3File.Key.Key, err) helper.ProcUtil.MessageLog.Error(errMsg) helper.Result.ErrorMessage += errMsg } else { helper.ProcUtil.MessageLog.Debug("Sent '%s' to storage queue", helper.Result.S3File.Key.Key) } }
{ for helper := range bagPreparer.FetchChannel { result := helper.Result result.NsqMessage.Touch() s3Key := result.S3File.Key // Disk needs filesize * 2 disk space to accomodate tar file & untarred files err := bagPreparer.ProcUtil.Volume.Reserve(uint64(s3Key.Size * 2)) if err != nil { // Not enough room on disk bagPreparer.ProcUtil.MessageLog.Warning("Requeueing %s - not enough disk space", s3Key.Key) result.ErrorMessage = err.Error() result.Retry = true bagPreparer.ResultsChannel <- helper } else { bagPreparer.ProcUtil.MessageLog.Info("Fetching %s", s3Key.Key) helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusStarted) helper.FetchTarFile() if result.ErrorMessage != "" { // Fetch from S3 failed. Requeue. bagPreparer.ResultsChannel <- helper } else { // Got S3 file. Untar it. // And touch the message, so nsqd knows we're making progress. result.NsqMessage.Touch() helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusPending) bagPreparer.UnpackChannel <- helper } } } }
identifier_body
bagpreparer.go
package workers import ( "encoding/json" "fmt" "github.com/APTrust/bagman/bagman" "github.com/nsqio/go-nsq" "os" "strings" "time" ) // Large file is ~50GB const LARGE_FILE_SIZE = int64(50000000000) // apt_prepare receives messages from nsqd describing // items in the S3 receiving buckets. It fetches, untars, // and validates tar files, then queues them for storage, // if they untar and validate successfully. Each item/message // follows this flow: // // 1. Fetch channel: fetches the file from S3. // 2. Unpack channel: untars the bag files, parses and validates // the bag, reads tags, generates checksums and generic file // UUIDs. // 3. Results channel: tells the queue whether processing // succeeded, and if not, whether the item should be requeued. // Also logs results to json and message logs. // 4. Cleanup channel: cleans up the files after processing // completes. // // If a failure occurs anywhere in the first three steps, // processing goes directly to the Results Channel, which // records the error and the disposition (retry/give up). // // As long as the message from nsq contains valid JSON, // steps 4 and 5 ALWAYS run. // // Some notes... It's essential that that the fetchChannel // be limited to a relatively low number. If we are downloading // 1GB tar files, we need space to store the tar file AND the // untarred version. That's about 2 x 1GB. We do not want to pull // down 1000 files at once, or we'll run out of disk space! // If config sets fetchers to 10, we can pull down 10 files at a // time. The fetch queue could hold 10 * 4 = 40 items, so we'd // have max 40 tar files + untarred directories on disk at once. // The number of workers should be close to the number of CPU // cores. // // We do NOT want one go routine per S3 file. If we do that, // the system will run out of file handles, as we'll have tens // of thousands of open connections to S3 trying to write data // into tens of thousands of local files. type BagPreparer struct { FetchChannel chan *bagman.IngestHelper UnpackChannel chan *bagman.IngestHelper CleanUpChannel chan *bagman.IngestHelper ResultsChannel chan *bagman.IngestHelper ProcUtil *bagman.ProcessUtil largeFile1 string largeFile2 string } func NewBagPreparer(procUtil *bagman.ProcessUtil) (*BagPreparer) { bagPreparer := &BagPreparer{ ProcUtil: procUtil, } // Set up buffered channels fetcherBufferSize := procUtil.Config.PrepareWorker.NetworkConnections * 4 workerBufferSize := procUtil.Config.PrepareWorker.Workers * 10 bagPreparer.FetchChannel = make(chan *bagman.IngestHelper, fetcherBufferSize) bagPreparer.UnpackChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.CleanUpChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.ResultsChannel = make(chan *bagman.IngestHelper, workerBufferSize) // Set up a limited number of go routines for i := 0; i < procUtil.Config.PrepareWorker.NetworkConnections; i++ { go bagPreparer.doFetch() } for i := 0; i < procUtil.Config.PrepareWorker.Workers; i++ { go bagPreparer.doUnpack() go bagPreparer.logResult() go bagPreparer.doCleanUp() } return bagPreparer } // MessageHandler handles messages from the queue, putting each // item into the pipleline. func (bagPreparer *BagPreparer) HandleMessage(message *nsq.Message) error { message.DisableAutoResponse() var s3File bagman.S3File err := json.Unmarshal(message.Body, &s3File) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Could not unmarshal JSON data from nsq:", string(message.Body)) message.Finish() return nil } // If we're not reprocessing on purpose, and this item has already // been successfully processed, skip it. There are certain timing // conditions that can cause the bucket reader to add items to the // queue twice. If we get rid of NSQ, we can get rid of this check. if bagPreparer.ProcUtil.Config.SkipAlreadyProcessed == true && bagman.BagNeedsProcessing(&s3File, bagPreparer.ProcUtil) == false { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete, without processing because "+ "Config.SkipAlreadyProcessed = true and this bag was ingested or is currently "+ "being processed.", s3File.Key.Key) message.Finish() return nil } // Don't start ingest if there's a pending delete or restore request. // Ingest would just overwrite the files and metadata that delete/restore // would be operating on. If there is a pending delete/restore request, // send this back into the queue with an hour or so backoff time. // // If we can't parse the bag date, it's OK to send an empty date into // the search. We may pull back a few extra records and get a false positive // on the pending delete/restore. A false positive will delay ingest, but a // false negative could cause some cascading errors. bagDate, _ := time.Parse(bagman.S3DateFormat, s3File.Key.LastModified) processStatus := &bagman.ProcessStatus { ETag: strings.Replace(s3File.Key.ETag, "\"", "", -1), Name: s3File.Key.Key, BagDate: bagDate, } statusRecords, err := bagPreparer.ProcUtil.FluctusClient.ProcessStatusSearch(processStatus, true, true) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error fetching status info on bag %s " + "from Fluctus. Will retry in 5 minutes. Error: %v", s3File.Key.Key, err) message.Requeue(5 * time.Minute) return nil } if bagman.HasPendingDeleteRequest(statusRecords) || bagman.HasPendingRestoreRequest(statusRecords) { bagPreparer.ProcUtil.MessageLog.Info("Requeuing %s due to pending delete or " + "restore request. Will retry in at least 60 minutes.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } // Special case for very large bags: the bag is in process under // the same ID. NSQ thinks it timed out and has re-sent it. In this // case, return nil so NSQ knows we're OK, but don't finish the message. // The original process will call Finish() on the message when it's // done. If we call Finish() here, NSQ will throw a "not-in-flight" // error when the processor calls Finish() on the original message later. currentMessageId := bagPreparer.ProcUtil.MessageIdString(message.ID) if bagPreparer.ProcUtil.BagAlreadyInProgress(&s3File, currentMessageId) { bagPreparer.ProcUtil.MessageLog.Info("Bag %s is already in progress under message id '%s'", s3File.Key.Key, bagPreparer.ProcUtil.MessageIdFor(s3File.BagName())) return nil } // For very large files, do max two at a time so we don't get cut off // from S3 for going 20+ seconds without a read. If we do multiple // large files at once, we get cut off from S3 often. We can do lots // of small files while one or two large ones are processing. if s3File.Key.Size > LARGE_FILE_SIZE { if bagPreparer.largeFile1 == "" { bagPreparer.largeFile1 = s3File.BagName() } else if bagPreparer.largeFile2 == "" { bagPreparer.largeFile2 = s3File.BagName() } else { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s because is >50GB and there are " + "already two large files in progress.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } } // Don't start working on a message that we're already working on. // Note that the key we include in the syncMap includes multipart // bag endings, so we can be working on ncsu.edu/obj.b1of2.tar and // ncsu.edu/obj.b2of2.tar at the same time. This is what we want. mapErr := bagPreparer.ProcUtil.RegisterItem(s3File.BagName(), message.ID) if mapErr != nil { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete because the file is already "+ "being processed under another message id.\n", s3File.Key.Key) message.Finish() return nil } // Create the result struct and pass it down the pipeline helper := bagman.NewIngestHelper(bagPreparer.ProcUtil, message, &s3File) bagPreparer.FetchChannel <- helper bagPreparer.ProcUtil.MessageLog.Debug("Put %s into fetch queue", s3File.Key.Key) return nil } // -- Step 1 of 5 -- // This runs as a go routine to fetch files from S3. func (bagPreparer *BagPreparer) doFetch() { for helper := range bagPreparer.FetchChannel { result := helper.Result result.NsqMessage.Touch() s3Key := result.S3File.Key // Disk needs filesize * 2 disk space to accomodate tar file & untarred files err := bagPreparer.ProcUtil.Volume.Reserve(uint64(s3Key.Size * 2)) if err != nil { // Not enough room on disk bagPreparer.ProcUtil.MessageLog.Warning("Requeueing %s - not enough disk space", s3Key.Key) result.ErrorMessage = err.Error() result.Retry = true bagPreparer.ResultsChannel <- helper } else { bagPreparer.ProcUtil.MessageLog.Info("Fetching %s", s3Key.Key) helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusStarted) helper.FetchTarFile() if result.ErrorMessage != "" { // Fetch from S3 failed. Requeue. bagPreparer.ResultsChannel <- helper } else { // Got S3 file. Untar it. // And touch the message, so nsqd knows we're making progress. result.NsqMessage.Touch() helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusPending) bagPreparer.UnpackChannel <- helper } } } } // -- Step 2 of 5 -- // This runs as a go routine to untar files downloaded from S3. // We calculate checksums and create generic files during the unpack // stage to avoid having to reprocess large streams of data several times. func (bagPreparer *BagPreparer) doUnpack() { for helper := range bagPreparer.UnpackChannel {
bagPreparer.ProcUtil.MessageLog.Warning("Nothing to unpack for %s", result.S3File.Key.Key) bagPreparer.ResultsChannel <- helper } else { // Unpacked! Now process the bag and touch message // so nsqd knows we're making progress. bagPreparer.ProcUtil.MessageLog.Info("Unpacking %s", result.S3File.Key.Key) // Touch when we start result.NsqMessage.Touch() // Processing can take 3+ hours for very large files! helper.UpdateFluctusStatus(bagman.StageUnpack, bagman.StatusStarted) helper.ProcessBagFile() helper.UpdateFluctusStatus(bagman.StageValidate, bagman.StatusPending) // And touch again when we're done result.NsqMessage.Touch() bagPreparer.ResultsChannel <- helper } } } // -- Step 4 of 5 -- // This prints to the log the result of the program's attempt to fetch, // untar, unbag and verify an individual S3 tar file. It logs state info // about this bag to a json file on the local file system. Also logs // a text message to the local bag_processor.log file and sends info // to Fluctus saying whether the bag succeeded or failed. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) logResult() { for helper := range bagPreparer.ResultsChannel { result := helper.Result result.NsqMessage.Touch() helper.LogResult() bagPreparer.CleanUpChannel <- helper } } // -- Step 5 of 5 -- // This runs as a go routine to remove the files we downloaded // and untarred. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) doCleanUp() { for helper := range bagPreparer.CleanUpChannel { result := helper.Result result.NsqMessage.Touch() bagPreparer.ProcUtil.MessageLog.Debug("Cleaning up %s", result.S3File.Key.Key) if (result.S3File.Key.Key != "" && result.FetchResult != nil && result.FetchResult.LocalFile != "") { bagPreparer.cleanupBag(helper) } // Build and send message back to NSQ, indicating whether // processing succeeded. if result.ErrorMessage != "" { if result.Retry == true { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s", result.S3File.Key.Key) result.NsqMessage.Requeue(5 * time.Minute) } else { result.NsqMessage.Finish() } } else { // Prepare succeeded. Send this off to storage queue, // so the generic files can go into long-term storage. bagPreparer.SendToStorageQueue(helper) result.NsqMessage.Finish() } // We're done processing this, so remove it from the map. // If it comes in again, we'll reprocess it again. bagPreparer.ProcUtil.UnregisterItem(result.S3File.BagName()) if bagPreparer.largeFile1 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile1 %s", result.S3File.Key.Key) bagPreparer.largeFile1 = "" } else if bagPreparer.largeFile2 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile2 %s", result.S3File.Key.Key) bagPreparer.largeFile2 = "" } } } func (bagPreparer *BagPreparer) cleanupBag(helper *bagman.IngestHelper) { result := helper.Result if result.ErrorMessage == "" { // Clean up the tar file, but leave the unpacked files // for apt_store to send off to long-term storage. err := os.Remove(result.FetchResult.LocalFile) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error deleting tar file %s: %v", result.FetchResult.LocalFile, err) } } else { // Clean up ALL files we downloaded and unpacked errors := helper.DeleteLocalFiles() if errors != nil && len(errors) > 0 { bagPreparer.ProcUtil.MessageLog.Warning("Errors cleaning up %s", result.FetchResult.LocalFile) for _, e := range errors { bagPreparer.ProcUtil.MessageLog.Error(e.Error()) } } } bagPreparer.ProcUtil.Volume.Release(uint64(result.S3File.Key.Size * 2)) } // Puts an item into the queue for Fluctus/Fedora metadata processing. func (bagPreparer *BagPreparer) SendToStorageQueue(helper *bagman.IngestHelper) { err := bagman.Enqueue(helper.ProcUtil.Config.NsqdHttpAddress, helper.ProcUtil.Config.StoreWorker.NsqTopic, helper.Result) if err != nil { errMsg := fmt.Sprintf("Error adding '%s' to storage queue: %v ", helper.Result.S3File.Key.Key, err) helper.ProcUtil.MessageLog.Error(errMsg) helper.Result.ErrorMessage += errMsg } else { helper.ProcUtil.MessageLog.Debug("Sent '%s' to storage queue", helper.Result.S3File.Key.Key) } }
result := helper.Result if result.ErrorMessage != "" { // Unpack failed. Go to end.
random_line_split
bagpreparer.go
package workers import ( "encoding/json" "fmt" "github.com/APTrust/bagman/bagman" "github.com/nsqio/go-nsq" "os" "strings" "time" ) // Large file is ~50GB const LARGE_FILE_SIZE = int64(50000000000) // apt_prepare receives messages from nsqd describing // items in the S3 receiving buckets. It fetches, untars, // and validates tar files, then queues them for storage, // if they untar and validate successfully. Each item/message // follows this flow: // // 1. Fetch channel: fetches the file from S3. // 2. Unpack channel: untars the bag files, parses and validates // the bag, reads tags, generates checksums and generic file // UUIDs. // 3. Results channel: tells the queue whether processing // succeeded, and if not, whether the item should be requeued. // Also logs results to json and message logs. // 4. Cleanup channel: cleans up the files after processing // completes. // // If a failure occurs anywhere in the first three steps, // processing goes directly to the Results Channel, which // records the error and the disposition (retry/give up). // // As long as the message from nsq contains valid JSON, // steps 4 and 5 ALWAYS run. // // Some notes... It's essential that that the fetchChannel // be limited to a relatively low number. If we are downloading // 1GB tar files, we need space to store the tar file AND the // untarred version. That's about 2 x 1GB. We do not want to pull // down 1000 files at once, or we'll run out of disk space! // If config sets fetchers to 10, we can pull down 10 files at a // time. The fetch queue could hold 10 * 4 = 40 items, so we'd // have max 40 tar files + untarred directories on disk at once. // The number of workers should be close to the number of CPU // cores. // // We do NOT want one go routine per S3 file. If we do that, // the system will run out of file handles, as we'll have tens // of thousands of open connections to S3 trying to write data // into tens of thousands of local files. type BagPreparer struct { FetchChannel chan *bagman.IngestHelper UnpackChannel chan *bagman.IngestHelper CleanUpChannel chan *bagman.IngestHelper ResultsChannel chan *bagman.IngestHelper ProcUtil *bagman.ProcessUtil largeFile1 string largeFile2 string } func NewBagPreparer(procUtil *bagman.ProcessUtil) (*BagPreparer) { bagPreparer := &BagPreparer{ ProcUtil: procUtil, } // Set up buffered channels fetcherBufferSize := procUtil.Config.PrepareWorker.NetworkConnections * 4 workerBufferSize := procUtil.Config.PrepareWorker.Workers * 10 bagPreparer.FetchChannel = make(chan *bagman.IngestHelper, fetcherBufferSize) bagPreparer.UnpackChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.CleanUpChannel = make(chan *bagman.IngestHelper, workerBufferSize) bagPreparer.ResultsChannel = make(chan *bagman.IngestHelper, workerBufferSize) // Set up a limited number of go routines for i := 0; i < procUtil.Config.PrepareWorker.NetworkConnections; i++ { go bagPreparer.doFetch() } for i := 0; i < procUtil.Config.PrepareWorker.Workers; i++ { go bagPreparer.doUnpack() go bagPreparer.logResult() go bagPreparer.doCleanUp() } return bagPreparer } // MessageHandler handles messages from the queue, putting each // item into the pipleline. func (bagPreparer *BagPreparer) HandleMessage(message *nsq.Message) error { message.DisableAutoResponse() var s3File bagman.S3File err := json.Unmarshal(message.Body, &s3File) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Could not unmarshal JSON data from nsq:", string(message.Body)) message.Finish() return nil } // If we're not reprocessing on purpose, and this item has already // been successfully processed, skip it. There are certain timing // conditions that can cause the bucket reader to add items to the // queue twice. If we get rid of NSQ, we can get rid of this check. if bagPreparer.ProcUtil.Config.SkipAlreadyProcessed == true && bagman.BagNeedsProcessing(&s3File, bagPreparer.ProcUtil) == false { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete, without processing because "+ "Config.SkipAlreadyProcessed = true and this bag was ingested or is currently "+ "being processed.", s3File.Key.Key) message.Finish() return nil } // Don't start ingest if there's a pending delete or restore request. // Ingest would just overwrite the files and metadata that delete/restore // would be operating on. If there is a pending delete/restore request, // send this back into the queue with an hour or so backoff time. // // If we can't parse the bag date, it's OK to send an empty date into // the search. We may pull back a few extra records and get a false positive // on the pending delete/restore. A false positive will delay ingest, but a // false negative could cause some cascading errors. bagDate, _ := time.Parse(bagman.S3DateFormat, s3File.Key.LastModified) processStatus := &bagman.ProcessStatus { ETag: strings.Replace(s3File.Key.ETag, "\"", "", -1), Name: s3File.Key.Key, BagDate: bagDate, } statusRecords, err := bagPreparer.ProcUtil.FluctusClient.ProcessStatusSearch(processStatus, true, true) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error fetching status info on bag %s " + "from Fluctus. Will retry in 5 minutes. Error: %v", s3File.Key.Key, err) message.Requeue(5 * time.Minute) return nil } if bagman.HasPendingDeleteRequest(statusRecords) || bagman.HasPendingRestoreRequest(statusRecords) { bagPreparer.ProcUtil.MessageLog.Info("Requeuing %s due to pending delete or " + "restore request. Will retry in at least 60 minutes.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } // Special case for very large bags: the bag is in process under // the same ID. NSQ thinks it timed out and has re-sent it. In this // case, return nil so NSQ knows we're OK, but don't finish the message. // The original process will call Finish() on the message when it's // done. If we call Finish() here, NSQ will throw a "not-in-flight" // error when the processor calls Finish() on the original message later. currentMessageId := bagPreparer.ProcUtil.MessageIdString(message.ID) if bagPreparer.ProcUtil.BagAlreadyInProgress(&s3File, currentMessageId) { bagPreparer.ProcUtil.MessageLog.Info("Bag %s is already in progress under message id '%s'", s3File.Key.Key, bagPreparer.ProcUtil.MessageIdFor(s3File.BagName())) return nil } // For very large files, do max two at a time so we don't get cut off // from S3 for going 20+ seconds without a read. If we do multiple // large files at once, we get cut off from S3 often. We can do lots // of small files while one or two large ones are processing. if s3File.Key.Size > LARGE_FILE_SIZE { if bagPreparer.largeFile1 == "" { bagPreparer.largeFile1 = s3File.BagName() } else if bagPreparer.largeFile2 == "" { bagPreparer.largeFile2 = s3File.BagName() } else { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s because is >50GB and there are " + "already two large files in progress.", s3File.Key.Key) message.Requeue(60 * time.Minute) return nil } } // Don't start working on a message that we're already working on. // Note that the key we include in the syncMap includes multipart // bag endings, so we can be working on ncsu.edu/obj.b1of2.tar and // ncsu.edu/obj.b2of2.tar at the same time. This is what we want. mapErr := bagPreparer.ProcUtil.RegisterItem(s3File.BagName(), message.ID) if mapErr != nil { bagPreparer.ProcUtil.MessageLog.Info("Marking %s as complete because the file is already "+ "being processed under another message id.\n", s3File.Key.Key) message.Finish() return nil } // Create the result struct and pass it down the pipeline helper := bagman.NewIngestHelper(bagPreparer.ProcUtil, message, &s3File) bagPreparer.FetchChannel <- helper bagPreparer.ProcUtil.MessageLog.Debug("Put %s into fetch queue", s3File.Key.Key) return nil } // -- Step 1 of 5 -- // This runs as a go routine to fetch files from S3. func (bagPreparer *BagPreparer) doFetch() { for helper := range bagPreparer.FetchChannel { result := helper.Result result.NsqMessage.Touch() s3Key := result.S3File.Key // Disk needs filesize * 2 disk space to accomodate tar file & untarred files err := bagPreparer.ProcUtil.Volume.Reserve(uint64(s3Key.Size * 2)) if err != nil { // Not enough room on disk bagPreparer.ProcUtil.MessageLog.Warning("Requeueing %s - not enough disk space", s3Key.Key) result.ErrorMessage = err.Error() result.Retry = true bagPreparer.ResultsChannel <- helper } else { bagPreparer.ProcUtil.MessageLog.Info("Fetching %s", s3Key.Key) helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusStarted) helper.FetchTarFile() if result.ErrorMessage != "" { // Fetch from S3 failed. Requeue. bagPreparer.ResultsChannel <- helper } else { // Got S3 file. Untar it. // And touch the message, so nsqd knows we're making progress. result.NsqMessage.Touch() helper.UpdateFluctusStatus(bagman.StageFetch, bagman.StatusPending) bagPreparer.UnpackChannel <- helper } } } } // -- Step 2 of 5 -- // This runs as a go routine to untar files downloaded from S3. // We calculate checksums and create generic files during the unpack // stage to avoid having to reprocess large streams of data several times. func (bagPreparer *BagPreparer) doUnpack() { for helper := range bagPreparer.UnpackChannel { result := helper.Result if result.ErrorMessage != "" { // Unpack failed. Go to end. bagPreparer.ProcUtil.MessageLog.Warning("Nothing to unpack for %s", result.S3File.Key.Key) bagPreparer.ResultsChannel <- helper } else { // Unpacked! Now process the bag and touch message // so nsqd knows we're making progress. bagPreparer.ProcUtil.MessageLog.Info("Unpacking %s", result.S3File.Key.Key) // Touch when we start result.NsqMessage.Touch() // Processing can take 3+ hours for very large files! helper.UpdateFluctusStatus(bagman.StageUnpack, bagman.StatusStarted) helper.ProcessBagFile() helper.UpdateFluctusStatus(bagman.StageValidate, bagman.StatusPending) // And touch again when we're done result.NsqMessage.Touch() bagPreparer.ResultsChannel <- helper } } } // -- Step 4 of 5 -- // This prints to the log the result of the program's attempt to fetch, // untar, unbag and verify an individual S3 tar file. It logs state info // about this bag to a json file on the local file system. Also logs // a text message to the local bag_processor.log file and sends info // to Fluctus saying whether the bag succeeded or failed. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) logResult() { for helper := range bagPreparer.ResultsChannel { result := helper.Result result.NsqMessage.Touch() helper.LogResult() bagPreparer.CleanUpChannel <- helper } } // -- Step 5 of 5 -- // This runs as a go routine to remove the files we downloaded // and untarred. // THIS STEP ALWAYS RUNS, EVEN IF PRIOR STEPS FAILED. func (bagPreparer *BagPreparer) doCleanUp() { for helper := range bagPreparer.CleanUpChannel { result := helper.Result result.NsqMessage.Touch() bagPreparer.ProcUtil.MessageLog.Debug("Cleaning up %s", result.S3File.Key.Key) if (result.S3File.Key.Key != "" && result.FetchResult != nil && result.FetchResult.LocalFile != "") { bagPreparer.cleanupBag(helper) } // Build and send message back to NSQ, indicating whether // processing succeeded. if result.ErrorMessage != "" { if result.Retry == true { bagPreparer.ProcUtil.MessageLog.Info("Requeueing %s", result.S3File.Key.Key) result.NsqMessage.Requeue(5 * time.Minute) } else { result.NsqMessage.Finish() } } else { // Prepare succeeded. Send this off to storage queue, // so the generic files can go into long-term storage. bagPreparer.SendToStorageQueue(helper) result.NsqMessage.Finish() } // We're done processing this, so remove it from the map. // If it comes in again, we'll reprocess it again. bagPreparer.ProcUtil.UnregisterItem(result.S3File.BagName()) if bagPreparer.largeFile1 == result.S3File.BagName()
else if bagPreparer.largeFile2 == result.S3File.BagName() { bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile2 %s", result.S3File.Key.Key) bagPreparer.largeFile2 = "" } } } func (bagPreparer *BagPreparer) cleanupBag(helper *bagman.IngestHelper) { result := helper.Result if result.ErrorMessage == "" { // Clean up the tar file, but leave the unpacked files // for apt_store to send off to long-term storage. err := os.Remove(result.FetchResult.LocalFile) if err != nil { bagPreparer.ProcUtil.MessageLog.Error("Error deleting tar file %s: %v", result.FetchResult.LocalFile, err) } } else { // Clean up ALL files we downloaded and unpacked errors := helper.DeleteLocalFiles() if errors != nil && len(errors) > 0 { bagPreparer.ProcUtil.MessageLog.Warning("Errors cleaning up %s", result.FetchResult.LocalFile) for _, e := range errors { bagPreparer.ProcUtil.MessageLog.Error(e.Error()) } } } bagPreparer.ProcUtil.Volume.Release(uint64(result.S3File.Key.Size * 2)) } // Puts an item into the queue for Fluctus/Fedora metadata processing. func (bagPreparer *BagPreparer) SendToStorageQueue(helper *bagman.IngestHelper) { err := bagman.Enqueue(helper.ProcUtil.Config.NsqdHttpAddress, helper.ProcUtil.Config.StoreWorker.NsqTopic, helper.Result) if err != nil { errMsg := fmt.Sprintf("Error adding '%s' to storage queue: %v ", helper.Result.S3File.Key.Key, err) helper.ProcUtil.MessageLog.Error(errMsg) helper.Result.ErrorMessage += errMsg } else { helper.ProcUtil.MessageLog.Debug("Sent '%s' to storage queue", helper.Result.S3File.Key.Key) } }
{ bagPreparer.ProcUtil.MessageLog.Info("Done with largeFile1 %s", result.S3File.Key.Key) bagPreparer.largeFile1 = "" }
conditional_block
startService.py
#! /usr/bin/env python3.6 #coding=utf-8 import os import subprocess import threading import time import traceback from enum import Enum import grpc from . import perfdog_pb2, perfdog_pb2_grpc class SaveFormat(Enum): NONE = 0, JSON = 1, PB = 2, EXCEL = 3, ALL = 4, class PerfdogService(): packageName = '' PerfdogPath = '' Token = '' stub = None device = None caseName = '' deviceUuid = '' saveformat = SaveFormat.ALL uploadServer = True saveJsonPath = '' def __init__(self,packageName,perfdogPath,token,deviceuuid,saveJsonPath,casename,saveFormat,UploadServer): """ :param packageName: 测试包的包名 :param perfdogPath: 性能狗Service 本地目录 :param token: 性能狗Service 令牌 :param deviceuuid: 需要测试的设备id :param saveJsonPath: 测试数据保存的本地位置 :param casename: 当此测试名 :param saveFormat: 测试数据保存格式 :param UploadServer: 测试数据是否上传性能狗网站 """ self.packageName = packageName self.PerfdogPath = perfdogPath self.Token = token self.caseName = casename self.deviceUuid = deviceuuid self.saveJsonPath = saveJsonPath self.saveformat = saveFormat self.uploadServer = UploadServer def initService(self): try: print("0 启动PerfDogService") # 填入PerfDogService的路径 perfDogService = subprocess.Popen(self.PerfdogPath) # 等待PerfDogService启动完毕 time.sleep(5) print("1.通过ip和端口连接到PerfDog Service") options = [('grpc.max_receive_message_length', 100 * 1024 * 1024)] channel = grpc.insecure_channel('127.0.0.1:23456', options=options) print("2.新建一个stub,通过这个stub对象可以调用所有服务器提供的接口") self.stub = perfdog_pb2_grpc.PerfDogServiceStub(channel) print("3.通过令牌登录,令牌可以在官网申请") userInfo = self.stub.loginWithToken( perfdog_pb2.Token(token=self.Token)) print("UserInfo:\n", userInfo) print("4.启动设备监听器监听设备,每当设备插入和移除时会收到一个DeviceEvent") deviceEventIterator = self.stub.startDeviceMonitor(perfdog_pb2.Empty()) for deviceEvent in deviceEventIterator: # 从DeviceEvent中获取到device对象,device对象会在后面的接口中用到 self.device = deviceEvent.device if deviceEvent.eventType == perfdog_pb2.ADD: print("设备[%s:%s]插入\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) # 每台手机会返回两个conType不同的设备对象(USB的和WIFI的),如果是测有线,取其中的USB对象 if self.device.conType == perfdog_pb2.USB: if self.device.uid == self.deviceUuid: print("5.初始化设备[%s:%s]\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) self.stub.initDevice(self.device) print("5.初始化设备 完成\n" ) break elif deviceEvent.eventType == perfdog_pb2.REMOVE: print("设备[%s:%s]移除\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) except Exception as e: traceback.print_exc() def startPerf(self): try: print("6.获取app列表") appList = self.stub.getAppList(self.device) apps = appList.app app = self.selectApp(apps) if app == None: raise Exception("未获取 "+self.packageName+" 信息") print("7.获取设备的详细信息") deviceInfo = self.stub.getDeviceInfo(self.device) print("deviceInfo") print(deviceInfo) # self.stub.setGlobalDataUploadServer(perfdog_pb2.SetDataUploadServerReq(serverUrl="http://127.0.0.1:80/",dataUploadFormat=perfdog_pb2.JSON)) print("8.开启性能数据项") self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.NETWORK_USAGE)) self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.SCREEN_SHOT)) print("9.开始收集[%s:%s]的性能数据\n" % (app.label, app.packageName)) # self.stub.setScreenShotInterval(1) print(self.stub.startTestApp(perfdog_pb2.StartTestAppReq(device=self.device, app=app))) # req = perfdog_pb2.OpenPerfDataStreamReq(device=self.device) # perfDataIterator = self.stub.openPerfDataStream(req) # def perf_data_process(): # for perfData in perfDataIterator: # print(perfData) # # threading.Thread(target=perf_data_process).start() threading.Thread().start() except Exception as e: traceback.print_exc() def setlabel(self,label): try: print(" 添加label :" + label) self.stub.setLabel(perfdog_pb2.SetLabelReq(device=self.device, label=label)) except Exception as e: traceback.print_exc() def setNote(self,note): try: print(" 添加批注 :"+note) self.stub.addNote(perfdog_pb2.AddNoteReq(device=self.device, time=5000, note=note)) except Exception as e: traceback.print_exc() def SaveJSON(self): try: str = "导出所有数据" if self.uploadServer: str = '上传' + str if self.saveformat == SaveFormat.NONE: print("PrefDog 数据保存格式为 NONE 不保存,不上传") elif self.saveformat == SaveFormat.ALL: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) else: if self.saveformat == SaveFormat.JSON: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果----JSON:\n", saveResult) if self.saveformat == SaveFormat.PB: print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) if self.saveformat == SaveFormat.EXCEL: print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False except Exception as e: traceback.print_exc() def StopPerf(self): try: if self.saveformat != SaveFormat.NONE: self.SaveJSON() else: print("保存格式为NONE 不保存为文件") print("13.停止测试") self.stub.stopTest(perfdog_pb2.StopTestReq(device=self.device)) self.stub.killServer() print("over") except Exception as e: traceback.print_exc() def selectApp(self,Apps): for app in Apps: print("find :",self.packageName," With ",app.packageName) if app.packageName == self.packageName: return app; return None; if __name__ == '__main__': package = "com.ztgame.fangzhidalu" path = "C:/Work/PerfDog/PerfDogService(v4.3.200927-Win)/PerfDogService.exe" token = "e8e5734ad2f74176b368c956173c9bfbb3a8
5bd1ec676cbef4b90435234786c1" uuid = "" pref = PerfdogService(package,path,token,"Test",uuid) print(pref) pref.StopPerf()
identifier_body
startService.py
#! /usr/bin/env python3.6 #coding=utf-8 import os
from enum import Enum import grpc from . import perfdog_pb2, perfdog_pb2_grpc class SaveFormat(Enum): NONE = 0, JSON = 1, PB = 2, EXCEL = 3, ALL = 4, class PerfdogService(): packageName = '' PerfdogPath = '' Token = '' stub = None device = None caseName = '' deviceUuid = '' saveformat = SaveFormat.ALL uploadServer = True saveJsonPath = '' def __init__(self,packageName,perfdogPath,token,deviceuuid,saveJsonPath,casename,saveFormat,UploadServer): """ :param packageName: 测试包的包名 :param perfdogPath: 性能狗Service 本地目录 :param token: 性能狗Service 令牌 :param deviceuuid: 需要测试的设备id :param saveJsonPath: 测试数据保存的本地位置 :param casename: 当此测试名 :param saveFormat: 测试数据保存格式 :param UploadServer: 测试数据是否上传性能狗网站 """ self.packageName = packageName self.PerfdogPath = perfdogPath self.Token = token self.caseName = casename self.deviceUuid = deviceuuid self.saveJsonPath = saveJsonPath self.saveformat = saveFormat self.uploadServer = UploadServer def initService(self): try: print("0 启动PerfDogService") # 填入PerfDogService的路径 perfDogService = subprocess.Popen(self.PerfdogPath) # 等待PerfDogService启动完毕 time.sleep(5) print("1.通过ip和端口连接到PerfDog Service") options = [('grpc.max_receive_message_length', 100 * 1024 * 1024)] channel = grpc.insecure_channel('127.0.0.1:23456', options=options) print("2.新建一个stub,通过这个stub对象可以调用所有服务器提供的接口") self.stub = perfdog_pb2_grpc.PerfDogServiceStub(channel) print("3.通过令牌登录,令牌可以在官网申请") userInfo = self.stub.loginWithToken( perfdog_pb2.Token(token=self.Token)) print("UserInfo:\n", userInfo) print("4.启动设备监听器监听设备,每当设备插入和移除时会收到一个DeviceEvent") deviceEventIterator = self.stub.startDeviceMonitor(perfdog_pb2.Empty()) for deviceEvent in deviceEventIterator: # 从DeviceEvent中获取到device对象,device对象会在后面的接口中用到 self.device = deviceEvent.device if deviceEvent.eventType == perfdog_pb2.ADD: print("设备[%s:%s]插入\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) # 每台手机会返回两个conType不同的设备对象(USB的和WIFI的),如果是测有线,取其中的USB对象 if self.device.conType == perfdog_pb2.USB: if self.device.uid == self.deviceUuid: print("5.初始化设备[%s:%s]\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) self.stub.initDevice(self.device) print("5.初始化设备 完成\n" ) break elif deviceEvent.eventType == perfdog_pb2.REMOVE: print("设备[%s:%s]移除\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) except Exception as e: traceback.print_exc() def startPerf(self): try: print("6.获取app列表") appList = self.stub.getAppList(self.device) apps = appList.app app = self.selectApp(apps) if app == None: raise Exception("未获取 "+self.packageName+" 信息") print("7.获取设备的详细信息") deviceInfo = self.stub.getDeviceInfo(self.device) print("deviceInfo") print(deviceInfo) # self.stub.setGlobalDataUploadServer(perfdog_pb2.SetDataUploadServerReq(serverUrl="http://127.0.0.1:80/",dataUploadFormat=perfdog_pb2.JSON)) print("8.开启性能数据项") self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.NETWORK_USAGE)) self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.SCREEN_SHOT)) print("9.开始收集[%s:%s]的性能数据\n" % (app.label, app.packageName)) # self.stub.setScreenShotInterval(1) print(self.stub.startTestApp(perfdog_pb2.StartTestAppReq(device=self.device, app=app))) # req = perfdog_pb2.OpenPerfDataStreamReq(device=self.device) # perfDataIterator = self.stub.openPerfDataStream(req) # def perf_data_process(): # for perfData in perfDataIterator: # print(perfData) # # threading.Thread(target=perf_data_process).start() threading.Thread().start() except Exception as e: traceback.print_exc() def setlabel(self,label): try: print(" 添加label :" + label) self.stub.setLabel(perfdog_pb2.SetLabelReq(device=self.device, label=label)) except Exception as e: traceback.print_exc() def setNote(self,note): try: print(" 添加批注 :"+note) self.stub.addNote(perfdog_pb2.AddNoteReq(device=self.device, time=5000, note=note)) except Exception as e: traceback.print_exc() def SaveJSON(self): try: str = "导出所有数据" if self.uploadServer: str = '上传' + str if self.saveformat == SaveFormat.NONE: print("PrefDog 数据保存格式为 NONE 不保存,不上传") elif self.saveformat == SaveFormat.ALL: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) else: if self.saveformat == SaveFormat.JSON: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果----JSON:\n", saveResult) if self.saveformat == SaveFormat.PB: print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) if self.saveformat == SaveFormat.EXCEL: print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False except Exception as e: traceback.print_exc() def StopPerf(self): try: if self.saveformat != SaveFormat.NONE: self.SaveJSON() else: print("保存格式为NONE 不保存为文件") print("13.停止测试") self.stub.stopTest(perfdog_pb2.StopTestReq(device=self.device)) self.stub.killServer() print("over") except Exception as e: traceback.print_exc() def selectApp(self,Apps): for app in Apps: print("find :",self.packageName," With ",app.packageName) if app.packageName == self.packageName: return app; return None; if __name__ == '__main__': package = "com.ztgame.fangzhidalu" path = "C:/Work/PerfDog/PerfDogService(v4.3.200927-Win)/PerfDogService.exe" token = "e8e5734ad2f74176b368c956173c9bfbb3a85bd1ec676cbef4b90435234786c1" uuid = "" pref = PerfdogService(package,path,token,"Test",uuid) print(pref) pref.StopPerf()
import subprocess import threading import time import traceback
random_line_split
startService.py
#! /usr/bin/env python3.6 #coding=utf-8 import os import subprocess import threading import time import traceback from enum import Enum import grpc from . import perfdog_pb2, perfdog_pb2_grpc class SaveFormat(Enum): NONE = 0, JSON = 1, PB = 2, EXCEL = 3, ALL = 4, class PerfdogService(): packageName = '' PerfdogPath = '' Token = '' stub = None device = None caseName = '' deviceUuid = '' saveformat = SaveFormat.ALL uploadServer = True saveJsonPath = '' def __init__(self,packageName,perfdogPath,token,deviceuuid,saveJsonPath,casename,saveFormat,UploadServer): """ :param packageName: 测试包的包名 :param perfdogPath: 性能狗Service 本地目录 :param token: 性能狗Service 令牌 :param deviceuuid: 需要测试的设备id :param saveJsonPath: 测试数据保存的本地位置 :param casename: 当此测试名 :param saveFormat: 测试数据保存格式 :param UploadServer: 测试数据是否上传性能狗网站 """ self.packageName = packageName self.PerfdogPath = perfdogPath self.Token = token self.caseName = casename self.deviceUuid = deviceuuid self.saveJsonPath = saveJsonPath self.saveformat = saveFormat self.uploadServer = UploadServer def initService(self): try: print("0 启动PerfDogService") # 填入PerfDogService的路径 perfDogService = subprocess.Popen(self.PerfdogPath) # 等待PerfDogService启动完毕 time.sleep(5) print("1.通过ip和端口连接到PerfDog Service") options = [('grpc.max_receive_message_length', 100 * 1024 * 1024)] channel = grpc.insecure_channel('127.0.0.1:23456', options=options) print("2.新建一个stub,通过这个stub对象可以调用所有服务器提供的接口") self.stub = perfdog_pb2_grpc.PerfDogServiceStub(channel) print("3.通过令牌登录,令牌可以在官网申请") userInfo = self.stub.loginWithToken( perfdog_pb2.Token(token=self.Token)) print("UserInfo:\n", userInfo) print("4.启动设备监听器监听设备,每当设备插入和移除时会收到一个DeviceEvent") deviceEventIterator = self.stub.startDeviceMonitor(perfdog_pb2.Empty()) for deviceEvent in deviceEventIterator: # 从DeviceEvent中获取到device对象,device对象会在后面的接口中用到 self.device = deviceEvent.device if deviceEvent.eventType == perfdog_pb2.ADD: print("设备[%s:%s]插入\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) # 每台手机会返回两个conType不同的设备对象(USB的和WIFI的),如果是测有线,取其中的USB对象 if self.device.conType == perfdog_pb2.USB: if self.device.uid == self.deviceUuid: print("5.初始化设备[%s:%s]\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) self.stub.initDevice(self.device) print("5.初始化设备 完成\n" ) break elif deviceEvent.eventType == perfdog_pb2.REMOVE: print("设备[%s:%s]移除\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) except Exception as e: traceback.print_exc() def startPerf(self): try: print("6.获取app列表") appList = self.stub.getAppList(self.device) apps = appList.app app = self.selectApp(apps) if app == None: raise Exception("未获取 "+self.packageName+" 信息") print("7.获取设备的详细信息") deviceInfo = self.stub.getDeviceInfo(self.device) print("deviceInfo") print(deviceInfo) # self.stub.setGlobalDataUploadServer(perfdog_pb2.SetDataUploadServerReq(serverUrl="http://127.0.0.1:80/",dataUploadFormat=perfdog_pb2.JSON)) print("8.开启性能数据项") self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.NETWORK_USAGE)) self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.SCREEN_SHOT)) print("9.开始收集[%s:%s]的性能数据\n" % (app.label, app.packageName)) # self.stub.setScreenShotInterval(1) print(self.stub.startTestApp(perfdog_pb2.StartTestAppReq(device=self.device, app=app))) # req = perfdog_pb2.OpenPerfDataStreamReq(device=self.device) # perfDataIterator = self.stub.openPerfDataStream(req) # def perf_data_process(): # for perfData in perfDataIterator: # print(perfData) # # threading.Thread(target=perf_data_process).start() threading.Thread().start() except Exception as e: traceback.print_exc() def setlabel(self,label): try: print(" 添加label :" + label) self.stub.setLabel(perfdog_pb2.SetLabelReq(device=self.device, label=label)) except Exception as e: traceback.print_exc() def setNote(self,note): try: print(" 添加批注 :"+note) self.stub.addNote(perfdog_pb2.AddNoteReq(device=self.device, time=5000, note=note)) except Exception as e: traceback.print_exc() def SaveJSON(self): try: str = "导出所有数据" if self.uploadServer: str = '上传' + str if self.saveformat == SaveFormat.NONE: print("PrefDog 数据保存格式为 NONE 不保存,不上传") elif self.saveformat == SaveFormat.ALL: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) else: if self.saveformat == SaveFormat.JSON: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果----JSON:\n", saveResult) if self.saveformat == SaveFormat.PB: print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) if self.saveformat == SaveFormat.EXCEL: print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False except Exception as e: traceback.print_exc() def StopPerf(self): try: if self.saveformat != SaveFormat.NONE:
e.fangzhidalu" path = "C:/Work/PerfDog/PerfDogService(v4.3.200927-Win)/PerfDogService.exe" token = "e8e5734ad2f74176b368c956173c9bfbb3a85bd1ec676cbef4b90435234786c1" uuid = "" pref = PerfdogService(package,path,token,"Test",uuid) print(pref) pref.StopPerf()
self.SaveJSON() else: print("保存格式为NONE 不保存为文件") print("13.停止测试") self.stub.stopTest(perfdog_pb2.StopTestReq(device=self.device)) self.stub.killServer() print("over") except Exception as e: traceback.print_exc() def selectApp(self,Apps): for app in Apps: print("find :",self.packageName," With ",app.packageName) if app.packageName == self.packageName: return app; return None; if __name__ == '__main__': package = "com.ztgam
conditional_block
startService.py
#! /usr/bin/env python3.6 #coding=utf-8 import os import subprocess import threading import time import traceback from enum import Enum import grpc from . import perfdog_pb2, perfdog_pb2_grpc class
(Enum): NONE = 0, JSON = 1, PB = 2, EXCEL = 3, ALL = 4, class PerfdogService(): packageName = '' PerfdogPath = '' Token = '' stub = None device = None caseName = '' deviceUuid = '' saveformat = SaveFormat.ALL uploadServer = True saveJsonPath = '' def __init__(self,packageName,perfdogPath,token,deviceuuid,saveJsonPath,casename,saveFormat,UploadServer): """ :param packageName: 测试包的包名 :param perfdogPath: 性能狗Service 本地目录 :param token: 性能狗Service 令牌 :param deviceuuid: 需要测试的设备id :param saveJsonPath: 测试数据保存的本地位置 :param casename: 当此测试名 :param saveFormat: 测试数据保存格式 :param UploadServer: 测试数据是否上传性能狗网站 """ self.packageName = packageName self.PerfdogPath = perfdogPath self.Token = token self.caseName = casename self.deviceUuid = deviceuuid self.saveJsonPath = saveJsonPath self.saveformat = saveFormat self.uploadServer = UploadServer def initService(self): try: print("0 启动PerfDogService") # 填入PerfDogService的路径 perfDogService = subprocess.Popen(self.PerfdogPath) # 等待PerfDogService启动完毕 time.sleep(5) print("1.通过ip和端口连接到PerfDog Service") options = [('grpc.max_receive_message_length', 100 * 1024 * 1024)] channel = grpc.insecure_channel('127.0.0.1:23456', options=options) print("2.新建一个stub,通过这个stub对象可以调用所有服务器提供的接口") self.stub = perfdog_pb2_grpc.PerfDogServiceStub(channel) print("3.通过令牌登录,令牌可以在官网申请") userInfo = self.stub.loginWithToken( perfdog_pb2.Token(token=self.Token)) print("UserInfo:\n", userInfo) print("4.启动设备监听器监听设备,每当设备插入和移除时会收到一个DeviceEvent") deviceEventIterator = self.stub.startDeviceMonitor(perfdog_pb2.Empty()) for deviceEvent in deviceEventIterator: # 从DeviceEvent中获取到device对象,device对象会在后面的接口中用到 self.device = deviceEvent.device if deviceEvent.eventType == perfdog_pb2.ADD: print("设备[%s:%s]插入\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) # 每台手机会返回两个conType不同的设备对象(USB的和WIFI的),如果是测有线,取其中的USB对象 if self.device.conType == perfdog_pb2.USB: if self.device.uid == self.deviceUuid: print("5.初始化设备[%s:%s]\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) self.stub.initDevice(self.device) print("5.初始化设备 完成\n" ) break elif deviceEvent.eventType == perfdog_pb2.REMOVE: print("设备[%s:%s]移除\n" % (self.device.uid, perfdog_pb2.DEVICE_CONTYPE.Name(self.device.conType))) except Exception as e: traceback.print_exc() def startPerf(self): try: print("6.获取app列表") appList = self.stub.getAppList(self.device) apps = appList.app app = self.selectApp(apps) if app == None: raise Exception("未获取 "+self.packageName+" 信息") print("7.获取设备的详细信息") deviceInfo = self.stub.getDeviceInfo(self.device) print("deviceInfo") print(deviceInfo) # self.stub.setGlobalDataUploadServer(perfdog_pb2.SetDataUploadServerReq(serverUrl="http://127.0.0.1:80/",dataUploadFormat=perfdog_pb2.JSON)) print("8.开启性能数据项") self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.NETWORK_USAGE)) self.stub.enablePerfDataType( perfdog_pb2.EnablePerfDataTypeReq(device=self.device, type=perfdog_pb2.SCREEN_SHOT)) print("9.开始收集[%s:%s]的性能数据\n" % (app.label, app.packageName)) # self.stub.setScreenShotInterval(1) print(self.stub.startTestApp(perfdog_pb2.StartTestAppReq(device=self.device, app=app))) # req = perfdog_pb2.OpenPerfDataStreamReq(device=self.device) # perfDataIterator = self.stub.openPerfDataStream(req) # def perf_data_process(): # for perfData in perfDataIterator: # print(perfData) # # threading.Thread(target=perf_data_process).start() threading.Thread().start() except Exception as e: traceback.print_exc() def setlabel(self,label): try: print(" 添加label :" + label) self.stub.setLabel(perfdog_pb2.SetLabelReq(device=self.device, label=label)) except Exception as e: traceback.print_exc() def setNote(self,note): try: print(" 添加批注 :"+note) self.stub.addNote(perfdog_pb2.AddNoteReq(device=self.device, time=5000, note=note)) except Exception as e: traceback.print_exc() def SaveJSON(self): try: str = "导出所有数据" if self.uploadServer: str = '上传' + str if self.saveformat == SaveFormat.NONE: print("PrefDog 数据保存格式为 NONE 不保存,不上传") elif self.saveformat == SaveFormat.ALL: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) else: if self.saveformat == SaveFormat.JSON: print("12.%s ----JSON" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_JSON )) print("保存结果----JSON:\n", saveResult) if self.saveformat == SaveFormat.PB: print("12.%s ----PB" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_PROTOBUF )) print("保存结果----PB:\n", saveResult) if self.saveformat == SaveFormat.EXCEL: print("12.%s ----Excel" % str) saveResult = self.stub.saveData(perfdog_pb2.SaveDataReq( device=self.device, caseName=self.caseName, # web上case和excel的名字 uploadToServer=self.uploadServer, # 上传到perfdog服务器 exportToFile=True, # 保存到本地 outputDirectory=self.saveJsonPath, dataExportFormat=perfdog_pb2.EXPORT_TO_EXCEL )) print("保存结果 ----JSON :\n", saveResult) self.uploadServer = False except Exception as e: traceback.print_exc() def StopPerf(self): try: if self.saveformat != SaveFormat.NONE: self.SaveJSON() else: print("保存格式为NONE 不保存为文件") print("13.停止测试") self.stub.stopTest(perfdog_pb2.StopTestReq(device=self.device)) self.stub.killServer() print("over") except Exception as e: traceback.print_exc() def selectApp(self,Apps): for app in Apps: print("find :",self.packageName," With ",app.packageName) if app.packageName == self.packageName: return app; return None; if __name__ == '__main__': package = "com.ztgame.fangzhidalu" path = "C:/Work/PerfDog/PerfDogService(v4.3.200927-Win)/PerfDogService.exe" token = "e8e5734ad2f74176b368c956173c9bfbb3a85bd1ec676cbef4b90435234786c1" uuid = "" pref = PerfdogService(package,path,token,"Test",uuid) print(pref) pref.StopPerf()
SaveFormat
identifier_name
linktypes.rs
//! LINK-LAYER HEADER TYPE VALUES [https://www.tcpdump.org/linktypes.html](https://www.tcpdump.org/linktypes.html) //! //! LINKTYPE_ name | LINKTYPE_ value | Corresponding DLT_ name | Description /// DLT_NULL BSD loopback encapsulation; the link layer header is a 4-byte field, in host byte order, containing a value of 2 for IPv4 packets, a value of either 24, 28, or 30 for IPv6 packets, a value of 7 for OSI packets, or a value of 23 for IPX packets. All of the IPv6 values correspond to IPv6 packets; code reading files should check for all of them. Note that ``host byte order'' is the byte order of the machine on which the packets are captured; if a live capture is being done, ``host byte order'' is the byte order of the machine capturing the packets, but if a ``savefile'' is being read, the byte order is not necessarily that of the machine reading the capture file. pub const NULL: i32 = 0; /// DLT_EN10MB IEEE 802.3 Ethernet (10Mb, 100Mb, 1000Mb, and up); the 10MB in the DLT_ name is historical. pub const ETHERNET: i32 = 1; /// DLT_AX25 AX.25 packet, with nothing preceding it. pub const AX25: i32 = 3; /// DLT_IEEE802 IEEE 802.5 Token Ring; the IEEE802, without _5, in the DLT_ name is historical. pub const IEEE802_5: i32 = 6; /// DLT_ARCNET ARCNET Data Packets, as described by the ARCNET Trade Association standard ATA 878.1-1999, but without the Starting Delimiter, Information Length, or Frame Check Sequence fields, and with only the first ISU of the Destination Identifier. For most packet types, ARCNET Trade Association draft standard ATA 878.2 is also used. See also RFC 1051 and RFC 1201; for RFC 1051 frames, ATA 878.2 is not used. pub const ARCNET_BSD: i32 = 7; /// DLT_SLIP SLIP, encapsulated with a LINKTYPE_SLIP header. pub const SLIP: i32 = 8; /// DLT_PPP PPP, as per RFC 1661 and RFC 1662; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-like framing, with the PPP header following those two bytes, otherwise it's PPP without framing, and the packet begins with the PPP header. The data in the frame is not octet-stuffed or bit-stuffed. pub const PPP: i32 = 9; /// DLT_FDDI FDDI, as specified by ANSI INCITS 239-1994. pub const FDDI: i32 = 10; /// DLT_PPP_SERIAL PPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547; the first byte will be 0xFF for PPP in HDLC-like framing, and will be 0x0F or 0x8F for Cisco PPP with HDLC framing. The data in the frame is not octet-stuffed or bit-stuffed. pub const PPP_HDLC: i32 = 50; /// DLT_PPP_ETHER PPPoE; the packet begins with a PPPoE header, as per RFC 2516. pub const PPP_ETHER: i32 = 51; /// DLT_ATM_RFC1483 RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an ISO 8802-2 (formerly known as IEEE 802.2) LLC header. pub const ATM_RFC1483: i32 = 100; /// DLT_RAW Raw IP; the packet begins with an IPv4 or IPv6 header, with the "version" field of the header indicating whether it's an IPv4 or IPv6 header. pub const RAW: i32 = 101; /// DLT_C_HDLC Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547. pub const C_HDLC: i32 = 104; /// DLT_IEEE802_11 IEEE 802.11 wireless LAN. pub const IEEE802_11: i32 = 105; /// DLT_FRELAY Frame Relay LAPF frames, beginning with a ITU-T Recommendation Q.922 LAPF header starting with the address field, and without an FCS at the end of the frame. pub const FRELAY: i32 = 107; /// DLT_LOOP OpenBSD loopback encapsulation; the link-layer header is a 4-byte field, in network byte order, containing a value of 2 for IPv4 packets, a value of either 24, 28, or 30 for IPv6 packets, a value of 7 for OSI packets, or a value of 23 for IPX packets. All of the IPv6 values correspond to IPv6 packets; code reading files should check for all of them. pub const LOOP: i32 = 108; /// DLT_LINUX_SLL Linux "cooked" capture encapsulation. pub const LINUX_SLL: i32 = 113; /// DLT_LTALK Apple LocalTalk; the packet begins with an AppleTalk LocalTalk Link Access Protocol header, as described in chapter 1 of Inside AppleTalk, Second Edition. pub const LTALK: i32 = 114; /// DLT_PFLOG OpenBSD pflog; the link-layer header contains a "struct pfloghdr" structure, as defined by the host on which the file was saved. (This differs from operating system to operating system and release to release; there is nothing in the file to indicate what the layout of that structure is.) pub const PFLOG: i32 = 117; /// DLT_PRISM_HEADER Prism monitor mode information followed by an 802.11 header. pub const IEEE802_11_PRISM: i32 = 119; /// DLT_IP_OVER_FC RFC 2625 IP-over-Fibre Channel, with the link-layer header being the Network_Header as described in that RFC. pub const IP_OVER_FC: i32 = 122; /// DLT_SUNATM ATM traffic, encapsulated as per the scheme used by SunATM devices. pub const SUNATM: i32 = 123; /// DLT_IEEE802_11_RADIO Radiotap link-layer information followed by an 802.11 header. pub const IEEE802_11_RADIOTAP: i32 = 127; /// DLT_ARCNET_LINUX ARCNET Data Packets, as described by the ARCNET Trade Association standard ATA 878.1-1999, but without the Starting Delimiter, Information Length, or Frame Check Sequence fields, with only the first ISU of the Destination Identifier, and with an extra two-ISU "offset" field following the Destination Identifier. For most packet types, ARCNET Trade Association draft standard ATA 878.2 is also used; however, no exception frames are supplied, and reassembled frames, rather than fragments, are supplied. See also RFC 1051 and RFC 1201; for RFC 1051 frames, ATA 878.2 is not used. pub const ARCNET_LINUX: i32 = 129; /// DLT_APPLE_IP_OVER_IEEE1394 Apple IP-over-IEEE 1394 cooked header. pub const APPLE_IP_OVER_IEEE1394: i32 = 138; /// DLT_MTP2_WITH_PHDR Signaling System 7 Message Transfer Part Level 2, as specified by ITU-T Recommendation Q.703, preceded by a pseudo-header. pub const MTP2_WITH_PHDR: i32 = 139; /// DLT_MTP2 Signaling System 7 Message Transfer Part Level 2, as specified by ITU-T Recommendation Q.703. pub const MTP2: i32 = 140; /// DLT_MTP3 Signaling System 7 Message Transfer Part Level 3, as specified by ITU-T Recommendation Q.704, with no MTP2 header preceding the MTP3 packet. pub const MTP3: i32 = 141; /// DLT_SCCP Signaling System 7 Signalling Connection Control Part, as specified by ITU-T Recommendation Q.711, ITU-T Recommendation Q.712, ITU-T Recommendation Q.713, and ITU-T Recommendation Q.714, with no MTP3 or MTP2 headers preceding the SCCP packet. pub const SCCP: i32 = 142; /// DLT_DOCSIS DOCSIS MAC frames, as described by the DOCSIS 3.1 MAC and Upper Layer Protocols Interface Specification or earlier specifications for MAC frames. pub const DOCSIS: i32 = 143; /// DLT_LINUX_IRDA Linux-IrDA packets, with a LINKTYPE_LINUX_IRDA header, with the payload for IrDA frames beginning with by the IrLAP header as defined by IrDA Data Specifications, including the IrDA Link Access Protocol specification. pub const LINUX_IRDA: i32 = 144; // LINKTYPE_USER0-LINKTYPE-USER15 147-162 DLT_USER0-DLT_USER15 Reserved for private use; see above. /// DLT_IEEE802_11_RADIO_AVS AVS monitor mode information followed by an 802.11 header. pub const IEEE802_11_AVS: i32 = 163; /// DLT_BACNET_MS_TP BACnet MS/TP frames, as specified by section 9.3 MS/TP Frame Format of ANSI/ASHRAE Standard 135, BACnet® - A Data Communication Protocol for Building Automation and Control Networks, including the preamble and, if present, the Data CRC. pub const BACNET_MS_TP: i32 = 165; /// DLT_PPP_PPPD PPP in HDLC-like encapsulation, like LINKTYPE_PPP_HDLC, but with the 0xff address byte replaced by a direction indication - 0x00 for incoming and 0x01 for outgoing. pub const PPP_PPPD: i32 = 166; /// DLT_GPRS_LLC General Packet Radio Service Logical Link Control, as defined by 3GPP TS 04.64. pub const GPRS_LLC: i32 = 169; /// DLT_GPF_T Transparent-mapped generic framing procedure, as specified by ITU-T Recommendation G.7041/Y.1303. pub const GPF_T: i32 = 170; /// DLT_GPF_F Frame-mapped generic framing procedure, as specified by ITU-T Recommendation G.7041/Y.1303. pub const GPF_F: i32 = 171; /// DLT_LINUX_LAPD Link Access Procedures on the D Channel (LAPD) frames, as specified by ITU-T Recommendation Q.920 and ITU-T Recommendation Q.921, captured via vISDN, with a LINKTYPE_LINUX_LAPD header, followed by the Q.921 frame, starting with the address field. pub const LINUX_LAPD: i32 = 177; /// DLT_MFR FRF.16.1 Multi-Link Frame Relay frames, beginning with an FRF.12 Interface fragmentation format fragmentation header. pub const MFR: i32 = 182; /// DLT_BLUETOOTH_HCI_H4 Bluetooth HCI UART transport layer; the frame contains an HCI packet indicator byte, as specified by the UART Transport Layer portion of the most recent Bluetooth Core specification, followed by an HCI packet of the specified packet type, as specified by the Host Controller Interface Functional Specification portion of the most recent Bluetooth Core Specification. pub const BLUETOOTH_HCI_H4: i32 = 187; /// DLT_USB_LINUX USB packets, beginning with a Linux USB header, as specified by the struct usbmon_packet in the Documentation/usb/usbmon.txt file in the Linux source tree. Only the first 48 bytes of that header are present. All fields in the header are in host byte order. When performing a live capture, the host byte order is the byte order of the machine on which the packets are captured. When reading a pcap file, the byte order is the byte order for the file, as specified by the file's magic number; when reading a pcapng file, the byte order is the byte order for the section of the pcapng file, as specified by the Section Header Block. pub const USB_LINUX: i32 = 189; /// DLT_PPI Per-Packet Information information, as specified by the Per-Packet Information Header Specification, followed by a packet with the LINKTYPE_ value specified by the pph_dlt field of that header. pub const PPI: i32 = 192; /// DLT_IEEE802_15_4_WITHFCS IEEE 802.15.4 Low-Rate Wireless Networks, with each packet having the FCS at the end of the frame. pub const IEEE802_15_4_WITHFCS: i32 = 195; /// DLT_SITA Various link-layer types, with a pseudo-header, for SITA. pub const SITA: i32 = 196; /// DLT_ERF Various link-layer types, with a pseudo-header, for Endace DAG cards; encapsulates Endace ERF records.
/// DLT_AX25_KISS AX.25 packet, with a 1-byte KISS header containing a type indicator. pub const AX25_KISS: i32 = 202; /// DLT_LAPD Link Access Procedures on the D Channel (LAPD) frames, as specified by ITU-T Recommendation Q.920 and ITU-T Recommendation Q.921, starting with the address field, with no pseudo-header. pub const LAPD: i32 = 203; /// DLT_PPP_WITH_DIR PPP, as per RFC 1661 and RFC 1662, preceded with a one-byte pseudo-header with a zero value meaning "received by this host" and a non-zero value meaning "sent by this host"; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-like framing, with the PPP header following those two bytes, otherwise it's PPP without framing, and the packet begins with the PPP header. The data in the frame is not octet-stuffed or bit-stuffed. pub const PPP_WITH_DIR: i32 = 204; /// DLT_C_HDLC_WITH_DIR Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547, preceded with a one-byte pseudo-header with a zero value meaning "received by this host" and a non-zero value meaning "sent by this host". pub const C_HDLC_WITH_DIR: i32 = 205; /// DLT_FRELAY_WITH_DIR Frame Relay LAPF frames, beginning with a one-byte pseudo-header with a zero value meaning "received by this host" (DCE->DTE) and a non-zero value meaning "sent by this host" (DTE->DCE), followed by an ITU-T Recommendation Q.922 LAPF header starting with the address field, and without an FCS at the end of the frame. pub const FRELAY_WITH_DIR: i32 = 206; /// DLT_LAPB_WITH_DIR Link Access Procedure, Balanced (LAPB), as specified by ITU-T Recommendation X.25, preceded with a one-byte pseudo-header with a zero value meaning "received by this host" (DCE->DTE) and a non-zero value meaning "sent by this host" (DTE->DCE). pub const LAPB_WITH_DIR: i32 = 207; /// DLT_IPMB_LINUX IPMB over an I2C circuit, with a Linux-specific pseudo-header. pub const IPMB_LINUX: i32 = 209; /// DLT_IEEE802_15_4_NONASK_PHY IEEE 802.15.4 Low-Rate Wireless Networks, with each packet having the FCS at the end of the frame, and with the PHY-level data for the O-QPSK, BPSK, GFSK, MSK, and RCC DSS BPSK PHYs (4 octets of 0 as preamble, one octet of SFD, one octet of frame length + reserved bit) preceding the MAC-layer data (starting with the frame control field). pub const IEEE802_15_4_NONASK_PHY: i32 = 215; /// DLT_USB_LINUX_MMAPPED USB packets, beginning with a Linux USB header, as specified by the struct usbmon_packet in the Documentation/usb/usbmon.txt file in the Linux source tree. All 64 bytes of the header are present. All fields in the header are in host byte order. When performing a live capture, the host byte order is the byte order of the machine on which the packets are captured. When reading a pcap file, the byte order is the byte order for the file, as specified by the file's magic number; when reading a pcapng file, the byte order is the byte order for the section of the pcapng file, as specified by the Section Header Block. For isochronous transfers, the ndesc field specifies the number of isochronous descriptors that follow. pub const USB_LINUX_MMAPPED: i32 = 220; /// DLT_FC_2 Fibre Channel FC-2 frames, beginning with a Frame_Header. pub const FC_2: i32 = 224; /// DLT_FC_2_WITH_FRAME_DELIMS Fibre Channel FC-2 frames, beginning an encoding of the SOF, followed by a Frame_Header, and ending with an encoding of the SOF. The encodings represent the frame delimiters as 4-byte sequences representing the corresponding ordered sets, with K28.5 represented as 0xBC, and the D symbols as the corresponding byte values; for example, SOFi2, which is K28.5 - D21.5 - D1.2 - D21.2, is represented as 0xBC 0xB5 0x55 0x55. pub const FC_2_WITH_FRAME_DELIMS: i32 = 225; /// DLT_IPNET Solaris ipnet pseudo-header, followed by an IPv4 or IPv6 datagram. pub const IPNET: i32 = 226; /// DLT_CAN_SOCKETCAN CAN (Controller Area Network) frames, with a pseudo-header followed by the frame payload. pub const CAN_SOCKETCAN: i32 = 227; /// DLT_IPV4 Raw IPv4; the packet begins with an IPv4 header. pub const IPV4: i32 = 228; /// DLT_IPV6 Raw IPv6; the packet begins with an IPv6 header. pub const IPV6: i32 = 229; /// DLT_IEEE802_15_4_NOFCS IEEE 802.15.4 Low-Rate Wireless Network, without the FCS at the end of the frame. pub const IEEE802_15_4_NOFCS: i32 = 230; /// DLT_DBUS Raw D-Bus messages, starting with the endianness flag, followed by the message type, etc., but without the authentication handshake before the message sequence. pub const DBUS: i32 = 231; /// DLT_DVB_CI DVB-CI (DVB Common Interface for communication between a PC Card module and a DVB receiver), with the message format specified by the PCAP format for DVB-CI specification. pub const DVB_CI: i32 = 235; /// DLT_MUX27010 Variant of 3GPP TS 27.010 multiplexing protocol (similar to, but not the same as, 27.010). pub const MUX27010: i32 = 236; /// DLT_STANAG_5066_D_PDU D_PDUs as described by NATO standard STANAG 5066, starting with the synchronization sequence, and including both header and data CRCs. The current version of STANAG 5066 is backwards-compatible with the 1.0.2 version, although newer versions are classified. pub const STANAG_5066_D_PDU: i32 = 237; /// DLT_NFLOG Linux netlink NETLINK NFLOG socket log messages. pub const NFLOG: i32 = 239; /// DLT_NETANALYZER Pseudo-header for Hilscher Gesellschaft für Systemautomation mbH netANALYZER devices, followed by an Ethernet frame, beginning with the MAC header and ending with the FCS. pub const NETANALYZER: i32 = 240; /// DLT_NETANALYZER_TRANSPARENT Pseudo-header for Hilscher Gesellschaft für Systemautomation mbH netANALYZER devices, followed by an Ethernet frame, beginning with the preamble, SFD, and MAC header, and ending with the FCS. pub const NETANALYZER_TRANSPARENT: i32 = 241; /// DLT_IPOIB IP-over-InfiniBand, as specified by RFC 4391 section 6. pub const IPOIB: i32 = 242; /// DLT_MPEG_2_TS MPEG-2 Transport Stream transport packets, as specified by ISO 13818-1/ITU-T Recommendation H.222.0 (see table 2-2 of section 2.4.3.2 "Transport Stream packet layer"). pub const MPEG_2_TS: i32 = 243; /// DLT_NG40 Pseudo-header for ng4T GmbH's UMTS Iub/Iur-over-ATM and Iub/Iur-over-IP format as used by their ng40 protocol tester, followed by frames for the Frame Protocol as specified by 3GPP TS 25.427 for dedicated channels and 3GPP TS 25.435 for common/shared channels in the case of ATM AAL2 or UDP traffic, by SSCOP packets as specified by ITU-T Recommendation Q.2110 for ATM AAL5 traffic, and by NBAP packets for SCTP traffic. pub const NG40: i32 = 244; /// DLT_NFC_LLCP Pseudo-header for NFC LLCP packet captures, followed by frame data for the LLCP Protocol as specified by NFCForum-TS-LLCP_1.1. pub const NFC_LLCP: i32 = 245; /// DLT_INFINIBAND Raw InfiniBand frames, starting with the Local Routing Header, as specified in Chapter 5 "Data packet format" of InfiniBand™ Architectural Specification Release 1.2.1 Volume 1 - General Specifications. pub const INFINIBAND: i32 = 247; /// DLT_SCTP SCTP packets, as defined by RFC 4960, with no lower-level protocols such as IPv4 or IPv6. pub const SCTP: i32 = 248; /// DLT_USBPCAP USB packets, beginning with a USBPcap header. pub const USBPCAP: i32 = 249; /// DLT_RTAC_SERIAL Serial-line packet header for the Schweitzer Engineering Laboratories "RTAC" product, followed by a payload for one of a number of industrial control protocols. pub const RTAC_SERIAL: i32 = 250; /// DLT_BLUETOOTH_LE_LL Bluetooth Low Energy air interface Link Layer packets, in the format described in section 2.1 "PACKET FORMAT" of volume 6 of the Bluetooth Specification Version 4.0 (see PDF page 2200), but without the Preamble. pub const BLUETOOTH_LE_LL: i32 = 251; /// DLT_NETLINK Linux Netlink capture encapsulation. pub const NETLINK: i32 = 253; /// DLT_BLUETOOTH_LINUX_MONITOR Bluetooth Linux Monitor encapsulation of traffic for the BlueZ stack. pub const BLUETOOTH_LINUX_MONITOR: i32 = 254; /// DLT_BLUETOOTH_BREDR_BB Bluetooth Basic Rate and Enhanced Data Rate baseband packets. pub const BLUETOOTH_BREDR_BB: i32 = 255; /// DLT_BLUETOOTH_LE_LL_WITH_PHDR Bluetooth Low Energy link-layer packets. pub const BLUETOOTH_LE_LL_WITH_PHDR: i32 = 256; /// DLT_PROFIBUS_DL PROFIBUS data link layer packets, as specified by IEC standard 61158-4-3, beginning with the start delimiter, ending with the end delimiter, and including all octets between them. pub const PROFIBUS_DL: i32 = 257; /// DLT_PKTAP Apple PKTAP capture encapsulation. pub const PKTAP: i32 = 258; /// DLT_EPON Ethernet-over-passive-optical-network packets, starting with the last 6 octets of the modified preamble as specified by 65.1.3.2 "Transmit" in Clause 65 of Section 5 of IEEE 802.3, followed immediately by an Ethernet frame. pub const EPON: i32 = 259; /// DLT_IPMI_HPM_2 IPMI trace packets, as specified by Table 3-20 "Trace Data Block Format" in the PICMG HPM.2 specification. The time stamps for packets in this format must match the time stamps in the Trace Data Blocks. pub const IPMI_HPM_2: i32 = 260; /// DLT_ZWAVE_R1_R2 Z-Wave RF profile R1 and R2 packets, as specified by ITU-T Recommendation G.9959, with some MAC layer fields moved. pub const ZWAVE_R1_R2: i32 = 261; /// DLT_ZWAVE_R3 Z-Wave RF profile R3 packets, as specified by ITU-T Recommendation G.9959, with some MAC layer fields moved. pub const ZWAVE_R3: i32 = 262; /// DLT_WATTSTOPPER_DLM Formats for WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol common packet structure captures. pub const WATTSTOPPER_DLM: i32 = 263; /// DLT_ISO_14443 Messages between ISO 14443 contactless smartcards (Proximity Integrated Circuit Card, PICC) and card readers (Proximity Coupling Device, PCD), with the message format specified by the PCAP format for ISO14443 specification. pub const ISO_14443: i32 = 264; /// DLT_RDS Radio data system (RDS) groups, as per IEC 62106, encapsulated in this form. pub const RDS: i32 = 265; /// DLT_USB_DARWIN USB packets, beginning with a Darwin (macOS, etc.) USB header. pub const USB_DARWIN: i32 = 266; /// DLT_SDLC SDLC packets, as specified by Chapter 1, "DLC Links", section "Synchronous Data Link Control (SDLC)" of Systems Network Architecture Formats, GA27-3136-20, without the flag fields, zero-bit insertion, or Frame Check Sequence field, containing SNA path information units (PIUs) as the payload. pub const SDLC: i32 = 268; /// DLT_LORATAP LoRaTap pseudo-header, followed by the payload, which is typically the PHYPayload from the LoRaWan specification. pub const LORATAP: i32 = 270; /// DLT_VSOCK Protocol for communication between host and guest machines in VMware and KVM hypervisors. pub const VSOCK: i32 = 271; /// DLT_NORDIC_BLE Messages to and from a Nordic Semiconductor nRF Sniffer for Bluetooth LE packets, beginning with a pseudo-header. pub const NORDIC_BLE: i32 = 272; /// DLT_DOCSIS31_XRA31 DOCSIS packets and bursts, preceded by a pseudo-header giving metadata about the packet. pub const DOCSIS31_XRA31: i32 = 273; /// DLT_ETHERNET_MPACKET mPackets, as specified by IEEE 802.3br Figure 99-4, starting with the preamble and always ending with a CRC field. pub const ETHERNET_MPACKET: i32 = 274; /// DLT_DISPLAYPORT_AUX DisplayPort AUX channel monitoring data as specified by VESA DisplayPort(DP) Standard preceeded by a pseudo-header. pub const DISPLAYPORT_AUX: i32 = 275; /// DLT_LINUX_SLL2 Linux "cooked" capture encapsulation v2. pub const LINUX_SLL2: i32 = 276; /// DLT_OPENVIZSLA Openvizsla FPGA-based USB sniffer. pub const OPENVIZSLA: i32 = 278; /// DLT_EBHSCR Elektrobit High Speed Capture and Replay (EBHSCR) format. pub const EBHSCR: i32 = 279; /// DLT_VPP_DISPATCH Records in traces from the http://fd.io VPP graph dispatch tracer, in the the graph dispatcher trace format. pub const VPP_DISPATCH: i32 = 280; /// DLT_DSA_TAG_BRCM Ethernet frames, with a switch tag inserted between the source address field and the type/length field in the Ethernet header. pub const DSA_TAG_BRCM: i32 = 281; /// DLT_DSA_TAG_BRCM_PREPEND Ethernet frames, with a switch tag inserted before the destination address in the Ethernet header. pub const DSA_TAG_BRCM_PREPEND: i32 = 282; /// DLT_IEEE802_15_4_TAP IEEE 802.15.4 Low-Rate Wireless Networks, with a pseudo-header containing TLVs with metadata preceding the 802.15.4 header. pub const IEEE802_15_4_TAP: i32 = 283; /// DLT_DSA_TAG_DSA Ethernet frames, with a switch tag inserted between the source address field and the type/length field in the Ethernet header. pub const DSA_TAG_DSA: i32 = 284; /// DLT_DSA_TAG_EDSA Ethernet frames, with a programmable Ethernet type switch tag inserted between the source address field and the type/length field in the Ethernet header. pub const DSA_TAG_EDSA: i32 = 285; /// DLT_ELEE Payload of lawful intercept packets using the ELEE protocol. The packet begins with the ELEE header; it does not include any transport-layer or lower-layer headers for protcols used to transport ELEE packets. pub const ELEE: i32 = 286;
pub const ERF: i32 = 197; /// DLT_BLUETOOTH_HCI_H4_WITH_PHDR Bluetooth HCI UART transport layer; the frame contains a 4-byte direction field, in network byte order (big-endian), the low-order bit of which is set if the frame was sent from the host to the controller and clear if the frame was received by the host from the controller, followed by an HCI packet indicator byte, as specified by the UART Transport Layer portion of the most recent Bluetooth Core specification, followed by an HCI packet of the specified packet type, as specified by the Host Controller Interface Functional Specification portion of the most recent Bluetooth Core Specification. pub const BLUETOOTH_HCI_H4_WITH_PHDR: i32 = 201;
random_line_split
storage.rs
//! A module encapsulating the Raft storage interface. use actix::{ dev::ToEnvelope, prelude::*, }; use futures::sync::mpsc::UnboundedReceiver; use failure::Fail; use crate::{ proto, raft::NodeId, }; /// An error type which wraps a `dyn Fail` type coming from the storage layer. /// /// This does require an allocation; however, the Raft node is currently configured to stop when /// it encounteres an error from the storage layer. The cost of this allocation then is quite low. /// /// In order to avoid potential data corruption or other such issues, when an error is observed /// from the storage layer, the Raft node will stop. The parent application will still be able to /// perform cleanup or any other routines as needed before shutdown. #[derive(Debug, Fail)] #[fail(display="{}", _0)] pub struct StorageError(pub Box<dyn Fail>); /// The result type of all `RaftStorage` interfaces. pub type StorageResult<T> = Result<T, StorageError>; ////////////////////////////////////////////////////////////////////////////// // GetInitialState /////////////////////////////////////////////////////////// /// An actix message type for requesting Raft state information from the storage layer. /// /// When the Raft actor is first started, it will call this interface on the storage system to /// fetch the last known state from stable storage. If no such entry exists due to being the /// first time the node has come online, then the default value for `InitialState` should be used. /// /// ### pro tip
/// state record; and the index of the last log applied to the state machine. pub struct GetInitialState; impl Message for GetInitialState { type Result = StorageResult<InitialState>; } /// A struct used to represent the initial state which a Raft node needs when first starting. pub struct InitialState { /// The index of the last entry. pub last_log_index: u64, /// The term of the last log entry. pub last_log_term: u64, /// The index of the last log applied to the state machine. pub last_applied_log: u64, /// The saved hard state of the node. pub hard_state: HardState, } ////////////////////////////////////////////////////////////////////////////////////////////////// // GetLogEntries ///////////////////////////////////////////////////////////////////////////////// /// An actix message type for requesting a series of log entries from storage. /// /// The start value is inclusive in the search and the stop value is non-inclusive: /// `[start, stop)`. pub struct GetLogEntries { pub start: u64, pub stop: u64, } impl Message for GetLogEntries { type Result = StorageResult<Vec<proto::Entry>>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // AppendLogEntries ////////////////////////////////////////////////////////////////////////////// /// An actix message type for requesting a series of entries to be written to the log. /// /// Though the entries will always be presented in order, each entry's index should be used for /// determining its location to be written in the log, as logs may need to be overwritten under /// some circumstances. /// /// The result of a successful append entries call must contain the details on that last log entry /// appended to the log. pub struct AppendLogEntries(pub Vec<proto::Entry>); /// Details on the last log entry appended to the log as part of an `AppendLogEntries` operation. pub struct AppendLogEntriesData { pub index: u64, pub term: u64, } impl Message for AppendLogEntries { type Result = StorageResult<AppendLogEntriesData>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // CreateSnapshot //////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to have a new snapshot created which covers the current breadth /// of the log. /// /// The Raft node guarantees that this interface will never be called multiple overlapping times /// from the same Raft node, and it will not be called when an `InstallSnapshot` operation is in /// progress. /// /// **It is critical to note** that the newly created snapshot must be able to be used to /// completely and accurately create a state machine. In addition to saving space on disk (log /// compaction), snapshots are used to bring new Raft nodes and slow Raft nodes up-to-speed with /// the cluster leader. /// /// ### implementation algorithm /// - The generated snapshot should include all log entries starting from entry `0` up through /// the index specified by `through`. This will include any snapshot which may already exist. If /// a snapshot does already exist, the new log compaction process should be able to just load the /// old snapshot first, and resume processing from its last entry. /// - The newly generated snapshot should be written to the directory specified by `snapshot_dir`. /// - All previous entries in the log should be deleted up to the entry specified at index /// `through`. /// - The entry at index `through` should be replaced with a new entry created from calling /// `actix_raft::proto::Entry::new_snapshot_pointer(...)`. /// - Any old snapshot will no longer have representation in the log, and should be deleted. /// - Return a copy of the snapshot pointer entry created earlier. pub struct CreateSnapshot { /// The new snapshot should start from entry `0` and should cover all entries through the /// index specified here, inclusive. pub through: u64, /// The directory where the new snapshot is to be written. pub snapshot_dir: String, } impl Message for CreateSnapshot { type Result = StorageResult<proto::Entry>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // InstallSnapshot /////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to have a new snapshot written to disk and installed. /// /// This message holds an `UnboundedReceiver` which will stream in new chunks of data as they are /// received from the Raft leader. /// /// ### implementation algorithm /// - Upon receiving the request, a new snapshot file should be created on disk. /// - Every new chunk of data received should be written to the new snapshot file starting at the /// `offset` specified in the chunk. The Raft actor will ensure that redelivered chunks are not /// sent through multiple times. /// - If the receiver is dropped, the snapshot which was being created should be removed from /// disk. /// /// Once a chunk is received which is the final chunk of the snapshot, after writing the data, /// there are a few important steps to take: /// /// - Create a new entry in the log via the `actix_raft::proto::Entry::new_snapshot_pointer(...)` /// constructor. Insert the new entry into the log at the specified `index` of this payload. /// - If there are any logs older than `index`, remove them. /// - If there are any other snapshots in `snapshot_dir`, remove them. /// - If there are any logs newer than `index`, then return. /// - If there are no logs newer than `index`, then the state machine should be reset, and /// recreated from the new snapshot. Return once the state machine has been brought up-to-date. pub struct InstallSnapshot { /// The term which the final entry of this snapshot covers. pub term: u64, /// The index of the final entry which this snapshot covers. pub index: u64, /// The directory where the new snapshot is to be written. pub snapshot_dir: String, /// A stream of data chunks for this snapshot. pub stream: UnboundedReceiver<InstallSnapshotChunk>, } impl Message for InstallSnapshot { type Result = StorageResult<()>; } /// A chunk of snapshot data. pub struct InstallSnapshotChunk { /// The byte offset where chunk is positioned in the snapshot file. pub offset: u64, /// The raw bytes of the snapshot chunk, starting at `offset`. pub data: Vec<u8>, /// Will be `true` if this is the last chunk in the snapshot. pub done: bool, } ////////////////////////////////////////////////////////////////////////////////////////////////// // GetCurrentSnapshot //////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to get the location of the current snapshot on disk. /// /// ### implementation algorithm /// Implementation for this type's handler should be quite simple. Check the directory specified /// by `snapshot_dir` for any snapshot files. A proper implementation will only ever have one /// active snapshot, though another may exist while it is being created. As such, it is /// recommended to use a file naming pattern which will allow for easily distinguishing betweeen /// the current live snapshot, and any new snapshot which is being created. /// /// Once the current snapshot has been located, the absolute path to the file should be returned. /// If there is no active snapshot file, then `None` should be returned. pub struct GetCurrentSnapshot { /// The directory where the system has been configured to store snapshots. pub snapshot_dir: String, } impl Message for GetCurrentSnapshot { type Result = StorageResult<Option<String>>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // ApplyEntriesToStateMachine //////////////////////////////////////////////////////////////////// /// A request from the Raft node to apply the given log entries to the state machine. /// /// The Raft protocol guarantees that only logs which have been _committed_, that is, logs which /// have been replicated to a majority of the cluster, will be applied to the state machine. pub struct ApplyEntriesToStateMachine(pub Vec<proto::Entry>); /// Details on the last log entry applied to the state machine as part of an `ApplyEntriesToStateMachine` operation. pub struct ApplyEntriesToStateMachineData { pub index: u64, pub term: u64, } impl Message for ApplyEntriesToStateMachine { type Result = StorageResult<ApplyEntriesToStateMachineData>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // SaveHardState ///////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to save its HardState. pub struct SaveHardState(pub HardState); /// A record holding the hard state of a Raft node. pub struct HardState { /// The last recorded term observed by this system. pub current_term: u64, /// The ID of the node voted for in the `current_term`. pub voted_for: Option<NodeId>, /// The IDs of all known members of the cluster. pub members: Vec<u64>, } impl Message for SaveHardState { type Result = StorageResult<()>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // RaftStorage /////////////////////////////////////////////////////////////////////////////////// /// A trait defining the interface of a Raft storage actor. /// /// ### implementation notes /// Appending log entries should not be considered complete until the data has been flushed to /// disk. Some of Raft's safety guarantees are premised upon committed log entries being fully /// flushed to disk. If this invariant is not upheld, the system could incur data loss. /// /// ### snapshot /// See §7. /// /// Each node in the cluster will independently snapshot its data for compaction purposes. The /// conditions for when a new snapshot will be generated is based on the nodes `Config`. In /// addition to periodic snapshots, a leader may need to send an `InstallSnapshot` RPC to /// followers which are far behind or which are new to the cluster. This is based on the same /// `Config` value. The Raft node will send a message to this `RaftStorage` interface when a /// periodic snapshot is to be generated based on its configuration. /// /// Log compaction, which is part of what taking a snapshot is for, is an application specific /// process. The essential idea is that superfluous records in the log will be removed. See §7 for /// more details. There are a few snapshot related messages which the `RaftStorage` actor must /// handle: /// /// - `CreateSnapshot`: a request to create a new snapshot of the current log. /// - `InstallSnapshot`: the Raft leader is streaming over a snapshot, install it. /// - `GetCurrentSnapshot`: the Raft node needs to know the location of the current snapshot. /// /// See each message type for more details on the message and how to properly implement their /// behaviors. pub trait RaftStorage where Self: Actor<Context=Context<Self>>, Self: Handler<GetInitialState> + ToEnvelope<Self, GetInitialState>, Self: Handler<SaveHardState> + ToEnvelope<Self, SaveHardState>, Self: Handler<GetLogEntries> + ToEnvelope<Self, GetLogEntries>, Self: Handler<AppendLogEntries> + ToEnvelope<Self, AppendLogEntries>, Self: Handler<ApplyEntriesToStateMachine> + ToEnvelope<Self, ApplyEntriesToStateMachine>, Self: Handler<CreateSnapshot> + ToEnvelope<Self, CreateSnapshot>, Self: Handler<InstallSnapshot> + ToEnvelope<Self, InstallSnapshot>, Self: Handler<GetCurrentSnapshot> + ToEnvelope<Self, GetCurrentSnapshot>, {}
/// The storage impl may need to look in a few different places to accurately respond to this /// request. That last entry in the log for `last_log_index` & `last_log_term`; the node's hard
random_line_split
storage.rs
//! A module encapsulating the Raft storage interface. use actix::{ dev::ToEnvelope, prelude::*, }; use futures::sync::mpsc::UnboundedReceiver; use failure::Fail; use crate::{ proto, raft::NodeId, }; /// An error type which wraps a `dyn Fail` type coming from the storage layer. /// /// This does require an allocation; however, the Raft node is currently configured to stop when /// it encounteres an error from the storage layer. The cost of this allocation then is quite low. /// /// In order to avoid potential data corruption or other such issues, when an error is observed /// from the storage layer, the Raft node will stop. The parent application will still be able to /// perform cleanup or any other routines as needed before shutdown. #[derive(Debug, Fail)] #[fail(display="{}", _0)] pub struct
(pub Box<dyn Fail>); /// The result type of all `RaftStorage` interfaces. pub type StorageResult<T> = Result<T, StorageError>; ////////////////////////////////////////////////////////////////////////////// // GetInitialState /////////////////////////////////////////////////////////// /// An actix message type for requesting Raft state information from the storage layer. /// /// When the Raft actor is first started, it will call this interface on the storage system to /// fetch the last known state from stable storage. If no such entry exists due to being the /// first time the node has come online, then the default value for `InitialState` should be used. /// /// ### pro tip /// The storage impl may need to look in a few different places to accurately respond to this /// request. That last entry in the log for `last_log_index` & `last_log_term`; the node's hard /// state record; and the index of the last log applied to the state machine. pub struct GetInitialState; impl Message for GetInitialState { type Result = StorageResult<InitialState>; } /// A struct used to represent the initial state which a Raft node needs when first starting. pub struct InitialState { /// The index of the last entry. pub last_log_index: u64, /// The term of the last log entry. pub last_log_term: u64, /// The index of the last log applied to the state machine. pub last_applied_log: u64, /// The saved hard state of the node. pub hard_state: HardState, } ////////////////////////////////////////////////////////////////////////////////////////////////// // GetLogEntries ///////////////////////////////////////////////////////////////////////////////// /// An actix message type for requesting a series of log entries from storage. /// /// The start value is inclusive in the search and the stop value is non-inclusive: /// `[start, stop)`. pub struct GetLogEntries { pub start: u64, pub stop: u64, } impl Message for GetLogEntries { type Result = StorageResult<Vec<proto::Entry>>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // AppendLogEntries ////////////////////////////////////////////////////////////////////////////// /// An actix message type for requesting a series of entries to be written to the log. /// /// Though the entries will always be presented in order, each entry's index should be used for /// determining its location to be written in the log, as logs may need to be overwritten under /// some circumstances. /// /// The result of a successful append entries call must contain the details on that last log entry /// appended to the log. pub struct AppendLogEntries(pub Vec<proto::Entry>); /// Details on the last log entry appended to the log as part of an `AppendLogEntries` operation. pub struct AppendLogEntriesData { pub index: u64, pub term: u64, } impl Message for AppendLogEntries { type Result = StorageResult<AppendLogEntriesData>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // CreateSnapshot //////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to have a new snapshot created which covers the current breadth /// of the log. /// /// The Raft node guarantees that this interface will never be called multiple overlapping times /// from the same Raft node, and it will not be called when an `InstallSnapshot` operation is in /// progress. /// /// **It is critical to note** that the newly created snapshot must be able to be used to /// completely and accurately create a state machine. In addition to saving space on disk (log /// compaction), snapshots are used to bring new Raft nodes and slow Raft nodes up-to-speed with /// the cluster leader. /// /// ### implementation algorithm /// - The generated snapshot should include all log entries starting from entry `0` up through /// the index specified by `through`. This will include any snapshot which may already exist. If /// a snapshot does already exist, the new log compaction process should be able to just load the /// old snapshot first, and resume processing from its last entry. /// - The newly generated snapshot should be written to the directory specified by `snapshot_dir`. /// - All previous entries in the log should be deleted up to the entry specified at index /// `through`. /// - The entry at index `through` should be replaced with a new entry created from calling /// `actix_raft::proto::Entry::new_snapshot_pointer(...)`. /// - Any old snapshot will no longer have representation in the log, and should be deleted. /// - Return a copy of the snapshot pointer entry created earlier. pub struct CreateSnapshot { /// The new snapshot should start from entry `0` and should cover all entries through the /// index specified here, inclusive. pub through: u64, /// The directory where the new snapshot is to be written. pub snapshot_dir: String, } impl Message for CreateSnapshot { type Result = StorageResult<proto::Entry>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // InstallSnapshot /////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to have a new snapshot written to disk and installed. /// /// This message holds an `UnboundedReceiver` which will stream in new chunks of data as they are /// received from the Raft leader. /// /// ### implementation algorithm /// - Upon receiving the request, a new snapshot file should be created on disk. /// - Every new chunk of data received should be written to the new snapshot file starting at the /// `offset` specified in the chunk. The Raft actor will ensure that redelivered chunks are not /// sent through multiple times. /// - If the receiver is dropped, the snapshot which was being created should be removed from /// disk. /// /// Once a chunk is received which is the final chunk of the snapshot, after writing the data, /// there are a few important steps to take: /// /// - Create a new entry in the log via the `actix_raft::proto::Entry::new_snapshot_pointer(...)` /// constructor. Insert the new entry into the log at the specified `index` of this payload. /// - If there are any logs older than `index`, remove them. /// - If there are any other snapshots in `snapshot_dir`, remove them. /// - If there are any logs newer than `index`, then return. /// - If there are no logs newer than `index`, then the state machine should be reset, and /// recreated from the new snapshot. Return once the state machine has been brought up-to-date. pub struct InstallSnapshot { /// The term which the final entry of this snapshot covers. pub term: u64, /// The index of the final entry which this snapshot covers. pub index: u64, /// The directory where the new snapshot is to be written. pub snapshot_dir: String, /// A stream of data chunks for this snapshot. pub stream: UnboundedReceiver<InstallSnapshotChunk>, } impl Message for InstallSnapshot { type Result = StorageResult<()>; } /// A chunk of snapshot data. pub struct InstallSnapshotChunk { /// The byte offset where chunk is positioned in the snapshot file. pub offset: u64, /// The raw bytes of the snapshot chunk, starting at `offset`. pub data: Vec<u8>, /// Will be `true` if this is the last chunk in the snapshot. pub done: bool, } ////////////////////////////////////////////////////////////////////////////////////////////////// // GetCurrentSnapshot //////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to get the location of the current snapshot on disk. /// /// ### implementation algorithm /// Implementation for this type's handler should be quite simple. Check the directory specified /// by `snapshot_dir` for any snapshot files. A proper implementation will only ever have one /// active snapshot, though another may exist while it is being created. As such, it is /// recommended to use a file naming pattern which will allow for easily distinguishing betweeen /// the current live snapshot, and any new snapshot which is being created. /// /// Once the current snapshot has been located, the absolute path to the file should be returned. /// If there is no active snapshot file, then `None` should be returned. pub struct GetCurrentSnapshot { /// The directory where the system has been configured to store snapshots. pub snapshot_dir: String, } impl Message for GetCurrentSnapshot { type Result = StorageResult<Option<String>>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // ApplyEntriesToStateMachine //////////////////////////////////////////////////////////////////// /// A request from the Raft node to apply the given log entries to the state machine. /// /// The Raft protocol guarantees that only logs which have been _committed_, that is, logs which /// have been replicated to a majority of the cluster, will be applied to the state machine. pub struct ApplyEntriesToStateMachine(pub Vec<proto::Entry>); /// Details on the last log entry applied to the state machine as part of an `ApplyEntriesToStateMachine` operation. pub struct ApplyEntriesToStateMachineData { pub index: u64, pub term: u64, } impl Message for ApplyEntriesToStateMachine { type Result = StorageResult<ApplyEntriesToStateMachineData>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // SaveHardState ///////////////////////////////////////////////////////////////////////////////// /// A request from the Raft node to save its HardState. pub struct SaveHardState(pub HardState); /// A record holding the hard state of a Raft node. pub struct HardState { /// The last recorded term observed by this system. pub current_term: u64, /// The ID of the node voted for in the `current_term`. pub voted_for: Option<NodeId>, /// The IDs of all known members of the cluster. pub members: Vec<u64>, } impl Message for SaveHardState { type Result = StorageResult<()>; } ////////////////////////////////////////////////////////////////////////////////////////////////// // RaftStorage /////////////////////////////////////////////////////////////////////////////////// /// A trait defining the interface of a Raft storage actor. /// /// ### implementation notes /// Appending log entries should not be considered complete until the data has been flushed to /// disk. Some of Raft's safety guarantees are premised upon committed log entries being fully /// flushed to disk. If this invariant is not upheld, the system could incur data loss. /// /// ### snapshot /// See §7. /// /// Each node in the cluster will independently snapshot its data for compaction purposes. The /// conditions for when a new snapshot will be generated is based on the nodes `Config`. In /// addition to periodic snapshots, a leader may need to send an `InstallSnapshot` RPC to /// followers which are far behind or which are new to the cluster. This is based on the same /// `Config` value. The Raft node will send a message to this `RaftStorage` interface when a /// periodic snapshot is to be generated based on its configuration. /// /// Log compaction, which is part of what taking a snapshot is for, is an application specific /// process. The essential idea is that superfluous records in the log will be removed. See §7 for /// more details. There are a few snapshot related messages which the `RaftStorage` actor must /// handle: /// /// - `CreateSnapshot`: a request to create a new snapshot of the current log. /// - `InstallSnapshot`: the Raft leader is streaming over a snapshot, install it. /// - `GetCurrentSnapshot`: the Raft node needs to know the location of the current snapshot. /// /// See each message type for more details on the message and how to properly implement their /// behaviors. pub trait RaftStorage where Self: Actor<Context=Context<Self>>, Self: Handler<GetInitialState> + ToEnvelope<Self, GetInitialState>, Self: Handler<SaveHardState> + ToEnvelope<Self, SaveHardState>, Self: Handler<GetLogEntries> + ToEnvelope<Self, GetLogEntries>, Self: Handler<AppendLogEntries> + ToEnvelope<Self, AppendLogEntries>, Self: Handler<ApplyEntriesToStateMachine> + ToEnvelope<Self, ApplyEntriesToStateMachine>, Self: Handler<CreateSnapshot> + ToEnvelope<Self, CreateSnapshot>, Self: Handler<InstallSnapshot> + ToEnvelope<Self, InstallSnapshot>, Self: Handler<GetCurrentSnapshot> + ToEnvelope<Self, GetCurrentSnapshot>, {}
StorageError
identifier_name
hh.js
'use strict'; require('test'); var tpl = Set.Class({ constructor: function() { }, isCompress: false, bound:{left:'{', right:'}'}, boundChar: '%', command: {cond:1, echo:1, getv:1, code:1, for:0, foreach:0, if:3, elseif:3, else:3, html:0, block:0, head:0, body:0, phpend:0}, isNewEngine: ''.trim, replaces: this.isNewEngine? ["$out='';", "$out+=", ";", "$out"] : ["$out=[];", "$out.push(", ");", "$out.join('')"], concat: this.isNewEngine ? "if(content!==undefined){$out+=content;return content;}" : "$out.push(content);", compile: function (id, source) { var params = arguments; var anonymous = 'anonymous'; if (typeof source !== 'string') { source = params[0]; id = anonymous; } try { var Render = _compile(id, source); //编译时错误 } catch (e) { e.id = id || source; e.name = 'Syntax Error'; _debug(e); throw(e); } function render (data) { try { return new Render(data, id) + ''; //运行时错误捕捉 } catch (e) { _debug(e)(); throw(e); } } render.prototype = Render.prototype; render.toString = function () { return Render.toString(); }; if (id !== anonymous) { _cache[id] = render; } return render; }, render: function (id, data) { var cache = template.get(id) || _debug({ id: id, name: 'Render Error', message: 'No Template' }); return cache(data); }, mapFn: function(func) { var partialTpl = ''; switch(func.f) { case 'cond': partialTpl = setLBound+ func.v +setRBound+setLBlock; break; case 'echo': partialTpl = template.isCompress ? partialTpl = compress(func.v) : func.v; partialTpl = replaces[1]+ stringify(partialTpl) +replaces[2]; break; case 'getv': partialTpl = replaces[1]+ func.v +replaces[2]; break; case 'code': partialTpl = func.v; break; case 'for': partialTpl = 'for'; break; case 'foreach': partialTpl = 'foreach'; break; case 'if': partialTpl = 'if'; break; case 'elseif': partialTpl = 'else if'; break; case 'else': partialTpl = 'else'; break; } return partialTpl; }, parseFn: function(input, pos) {//pos为 ( 的位置 var func = '', fVal = '', currChar = '', stack = 1, end = 0; while(currChar=input.charAt(++pos)) { if(end) break;//在这步结束之前多扫了一个字符 if(isEChar(currChar)&&trim(func)!='' || currChar=='(') { func = trim(func); if(fTbl[func]){//如果是终结作用,终结作用右侧不应该有作用 while(currChar=input.charAt(++pos)) { if(currChar==setLBound){ stack++; } else if(currChar==setRBound) {//终结作用集结束 stack--; } if(stack==0){//栈空结束 end = 1; fVal = trim(fVal); break; } fVal += currChar; } } else { break; } } else { func += currChar; } } return {f:func, v:fVal, p:pos-1}; }, syntaxParse: function(code) { var boundChar = this.boundChar, leftBound = this.bound.left, rightBound = this.bound.right, command = this.command, NODETYPE_STR = 0, NODETYPE_OPEN = 1, tokens = [], begin = 0, forward = 0, forwardChar = '', func = '', codeLength = code.length, lastNode = null, currentNode = null, newNode = null; while(forward < codeLength) { //每次吃进一个字符,只有边界和命令做为独立单元解析,其他统一当做普通字符串处理 forwardChar = code.charAt(forward); if(forwardChar == leftBound) { //左边界 forwardChar = code.charAt(++forward); //前进一个字符 if(forwardChar == boundChar) { //如果左边界定位符匹配,进入命令解析 tokens.push({fn:'str', begin:begin, forward:forward-1, nodeType:NODETYPE_INIT}); //推入上一次结束边界到这次开始边界之间字符 begin = forward + 1; //起始指针位置加1 while(forwardChar = code.charAt(++forward)) {//这个循环寻找命令边界,找到结束,进行下一轮的正常扫描 if((isEChar(forwardChar) && trim(code.substring(begin, forward)) != '') || (forwardChar == leftBound && code.charAt(forward+1) == boundChar) || (forwardChar == boundChar && code.charAt(forward+1) == rightBound)) { func = trim(code.substring(begin, forward)); if(command[func] == undefined || func == '') { Set.Logger.error(func + ' not exists'); break;//抛出异常,函数不存在 } tokens.push({fn:func, begin:begin, forward:forward, nodeType:NODETYPE_OPEN}); begin = forward; forward --; //因为在总循环中forward要加1,所以这里要减去1,否则总循环扫描将错过一个字符 break; } } } } else if(forwardChar == rightBound) {//右边界 forwardChar = code.charAt(forward-1); //回溯匹配边界字符 if(forwardChar == boundChar) {//碰到close边界出栈一个open边界,和边界之前的字符命令 tokens.push({fn:'str', begin:begin, forward:forward-1, nodeType:NODETYPE_STR}); lastNode = null; while(currentNode = tokens.pop()) { if(currentNode.nodeType == 1) { newNode = currentNode; newNode.tp = lastNode; break; } else { if(currentNode.nodeType == 4) currentNode.hp.hp = lastNode; else currentNode.hp = lastNode; lastNode = currentNode; } } if(newNode.nodeType!=1) throw new Error('open match error'); currentNode = tokens.pop(); currentNode.nodeType = 4;//衔接节点 currentNode.hp = newNode; newNode.nodeType = 2; tokens.push(currentNode); begin = forward + 1; } } forward++; } tokens.push({fn:'str', begin:begin, forward:forward, nodeType:0}); return tokens; } }); new tpl(); /** * 模板引擎 * 若第二个参数类型为 String 则执行 compile 方法, 否则执行 render 方法 * @name template * @param {String} 模板ID * @param {Object, String} 数据或者模板字符串 * @return {String, Function} 渲染好的HTML字符串或者渲染方法 */ var template = function (id, content) { return template[ typeof content === 'string' ? 'compile' : 'render' ].apply(template, arguments); }; template.isEscape = true; // HTML字符编码输出开关 template.isCompress = false; // 剔除渲染后HTML多余的空白开关 var _cache = template.cache = {}; /** * 渲染模板 * @name template.render * @param {String} 模板ID * @param {Object} 数据 * @return {String} 渲染好的HTML字符串 */ template.render = function (id, data) { var cache = template.get(id) || _debug({ id: id, name: 'Render Error', message: 'No Template' }); return cache(data); }; /** * 编译模板 * 2012-6-6 @TooBug: define 方法名改为 compile,与 Node Express 保持一致 * @name template.compile * @param {String} 模板ID (可选,用作缓存索引) * @param {String} 模板字符串 * @return {Function} 渲染方法 */ template.compile = function (id, source) { var params = arguments; var anonymous = 'anonymous'; if (typeof source !== 'string') { source = params[0]; id = anonymous; } try { var Render = _compile(id, source); //编译时错误 } catch (e) { e.id = id || source; e.name = 'Syntax Error'; _debug(e); throw(e); } function render (data) { try { return new Render(data, id) + ''; //运行时错误捕捉 } catch (e) { _debug(e)(); throw(e); } } render.prototype = Render.prototype; render.toString = function () { return Render.toString(); }; if (id !== anonymous) { _cache[id] = render; } return render; }; // 获取模板缓存 template.get = function (id) { var cache; if (_cache.hasOwnProperty(id)) { cache = _cache[id]; } else if ('document' in global) { var elem = document.getElementById(id); if (elem) { var source = elem.value || elem.innerHTML; cache = template.compile(id, source.replace(/^\s*|\s*$/g, '')); } } return cache; }; // 模板调试器 var _debug = function (e) { template.onerror(e); return function () { return '{Template Error}'; }; }; var _compile = (function () { // 数组迭代 return function (id, source) { var prototype = {}; var isNewEngine = ''.trim;// '__proto__' in {} var replaces = isNewEngine ? ["$out='';", "$out+=", ";", "$out"] : ["$out=[];", "$out.push(", ");", "$out.join('')"]; var concat = isNewEngine ? "if(content!==undefined){$out+=content;return content;}" : "$out.push(content);"; /* * 需要增加include方法和print方法 * */ var code = source, tempCode = replaces[0], l = code.length, i = 0, currChar = '', currFunc = '', nfChar = '', setLBound = '(', setRBound = ')', setLBlock = '{', setRBlock = '}', lastC = 0, fTbl = {cond:1, echo:1, getv:1, code:1, for:0, foreach:0, if:0, elseif:0, else:0}; while(i<l) { currChar = code.charAt(i); switch(currChar) { case setLBound: if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); currFunc = getFunc(code, i); tempCode += mapping(currFunc); i = currFunc.p; nfChar = ''; break; case setRBound: if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); tempCode += setRBlock; nfChar = ''; break; default: nfChar += currChar; break; } i++; } if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); tempCode+='return new String(' + replaces[3] + ');'; try { var Render = new Function("$data", "$id", tempCode); Render.prototype = prototype; return Render; } catch (e) { e.temp = "function anonymous($data,$id) {" + tempCode + "}"; throw e; } function getFunc(input, pos) {//pos为 ( 的位置 var func = '', fVal = '', currChar = '', stack = 1, end = 0; while(currChar=input.charAt(++pos)) { if(end) break;//在这步结束之前多扫了一个字符 if(isEChar(currChar)&&trim(func)!='' || currChar=='(') { func = trim(func); if(fTbl[func]){//如果是终结作用,终结作用右侧不应该有作用 while(currChar=input.charAt(++pos)) { if(currChar==setLBound){ stack++; } else if(currChar==setRBound) {//终结作用集结束 stack--; } if(stack==0){//栈空结束 end = 1; fVal = trim(fVal); break; } fVal += currChar; } } else { break; } } else { func += currChar; } } return {f:func, v:fVal, p:pos-1}; } function mapping(func) { var partialTpl = ''; switch(func.f) { case 'cond': partialTpl = setLBound+ func.v +setRBound+setLBlock; break; case 'echo': partialTpl = template.isCompress ? partialTpl = compress(func.v) : func.v; partialTpl = replaces[1]+ stringify(partialTpl) +replaces[2]; break; case 'getv': partialTpl = replaces[1]+ func.v +replaces[2]; break; case 'code': partialTpl = func.v; break; case 'for': partialTpl = 'for'; break; case 'foreach': partialTpl = 'foreach'; break; case 'if': partialTpl = 'if'; break; case 'elseif': partialTpl = 'else if'; break; case 'else': partialTpl = 'else'; break; } return partialTpl; } function trim(text) { if(String.prototype.trim && !String.prototype.trim.call("\uFEFF\xA0")) { return text == null ? "" : String.prototype.trim.call(text); } else { var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; return text == null ? "" : ( text + "" ).replace(rtrim, ""); } } function isEChar(c) { return /\s/.test(c); } function isEString(c) { return !/\S/.test(c); } function compress(text) { return text.replace(/[ \n\v\f\r\t]+/g, " ") .replace(/<!--.*?-->/g, ''); } // 字符串转义 function stringify (code) { return "'" + code // 单引号与反斜杠转义 .replace(/('|\\)/g, '\\$1') // 换行符转义(windows + linux) .replace(/\r/g, '\\r') .re
{ value += ''; } else if (type === 'function') { value = _helpers.$string(value()); } else { value = ''; } } return value; }, $escape: function (content) { var m = { "<": "&#60;", ">": "&#62;", '"': "&#34;", "'": "&#39;", "&": "&#38;" }; return _helpers.$string(content) .replace(/&(?![\w#]+;)|[<>"']/g, function (s) { return m[s]; }); }, $each: function (data, callback) { var isArray = Array.isArray || function (obj) { return ({}).toString.call(obj) === '[object Array]'; }; if (isArray(data)) { for (var i = 0, len = data.length; i < len; i++) { callback.call(data, data[i], i, data); } } else { for (i in data) { callback.call(data, data[i], i); } } } }; /** * 模板错误事件 * @name template.onerror * @event */ template.onerror = function (e) { var message = 'Template Error\n\n'; for (var name in e) { message += '<' + name + '>\n' + e[name] + '\n\n'; } if (global.console) { console.error(message); } }; // RequireJS && SeaJS if (typeof define === 'function') { define(function() { return template; }); // NodeJS } else if (typeof exports !== 'undefined') { module.exports = template; } global.template = template; /* * 模板放置位置 * 模板新增方法 * 模板管理 * 模板引入 * 模板加载 * * * */
place(/\n/g, '\\n') + "'"; }; }; })(); //////////////////////////////////////////////////////////////////////// // 辅助方法集合 var _helpers = template.helpers = { $include: template.render, $string: function (value, type) { if (typeof value !== 'string') { type = typeof value; if (type === 'number')
identifier_body
hh.js
'use strict'; require('test'); var tpl = Set.Class({ constructor: function() { }, isCompress: false, bound:{left:'{', right:'}'}, boundChar: '%', command: {cond:1, echo:1, getv:1, code:1, for:0, foreach:0, if:3, elseif:3, else:3, html:0, block:0, head:0, body:0, phpend:0}, isNewEngine: ''.trim, replaces: this.isNewEngine? ["$out='';", "$out+=", ";", "$out"] : ["$out=[];", "$out.push(", ");", "$out.join('')"], concat: this.isNewEngine ? "if(content!==undefined){$out+=content;return content;}" : "$out.push(content);", compile: function (id, source) { var params = arguments; var anonymous = 'anonymous'; if (typeof source !== 'string')
try { var Render = _compile(id, source); //编译时错误 } catch (e) { e.id = id || source; e.name = 'Syntax Error'; _debug(e); throw(e); } function render (data) { try { return new Render(data, id) + ''; //运行时错误捕捉 } catch (e) { _debug(e)(); throw(e); } } render.prototype = Render.prototype; render.toString = function () { return Render.toString(); }; if (id !== anonymous) { _cache[id] = render; } return render; }, render: function (id, data) { var cache = template.get(id) || _debug({ id: id, name: 'Render Error', message: 'No Template' }); return cache(data); }, mapFn: function(func) { var partialTpl = ''; switch(func.f) { case 'cond': partialTpl = setLBound+ func.v +setRBound+setLBlock; break; case 'echo': partialTpl = template.isCompress ? partialTpl = compress(func.v) : func.v; partialTpl = replaces[1]+ stringify(partialTpl) +replaces[2]; break; case 'getv': partialTpl = replaces[1]+ func.v +replaces[2]; break; case 'code': partialTpl = func.v; break; case 'for': partialTpl = 'for'; break; case 'foreach': partialTpl = 'foreach'; break; case 'if': partialTpl = 'if'; break; case 'elseif': partialTpl = 'else if'; break; case 'else': partialTpl = 'else'; break; } return partialTpl; }, parseFn: function(input, pos) {//pos为 ( 的位置 var func = '', fVal = '', currChar = '', stack = 1, end = 0; while(currChar=input.charAt(++pos)) { if(end) break;//在这步结束之前多扫了一个字符 if(isEChar(currChar)&&trim(func)!='' || currChar=='(') { func = trim(func); if(fTbl[func]){//如果是终结作用,终结作用右侧不应该有作用 while(currChar=input.charAt(++pos)) { if(currChar==setLBound){ stack++; } else if(currChar==setRBound) {//终结作用集结束 stack--; } if(stack==0){//栈空结束 end = 1; fVal = trim(fVal); break; } fVal += currChar; } } else { break; } } else { func += currChar; } } return {f:func, v:fVal, p:pos-1}; }, syntaxParse: function(code) { var boundChar = this.boundChar, leftBound = this.bound.left, rightBound = this.bound.right, command = this.command, NODETYPE_STR = 0, NODETYPE_OPEN = 1, tokens = [], begin = 0, forward = 0, forwardChar = '', func = '', codeLength = code.length, lastNode = null, currentNode = null, newNode = null; while(forward < codeLength) { //每次吃进一个字符,只有边界和命令做为独立单元解析,其他统一当做普通字符串处理 forwardChar = code.charAt(forward); if(forwardChar == leftBound) { //左边界 forwardChar = code.charAt(++forward); //前进一个字符 if(forwardChar == boundChar) { //如果左边界定位符匹配,进入命令解析 tokens.push({fn:'str', begin:begin, forward:forward-1, nodeType:NODETYPE_INIT}); //推入上一次结束边界到这次开始边界之间字符 begin = forward + 1; //起始指针位置加1 while(forwardChar = code.charAt(++forward)) {//这个循环寻找命令边界,找到结束,进行下一轮的正常扫描 if((isEChar(forwardChar) && trim(code.substring(begin, forward)) != '') || (forwardChar == leftBound && code.charAt(forward+1) == boundChar) || (forwardChar == boundChar && code.charAt(forward+1) == rightBound)) { func = trim(code.substring(begin, forward)); if(command[func] == undefined || func == '') { Set.Logger.error(func + ' not exists'); break;//抛出异常,函数不存在 } tokens.push({fn:func, begin:begin, forward:forward, nodeType:NODETYPE_OPEN}); begin = forward; forward --; //因为在总循环中forward要加1,所以这里要减去1,否则总循环扫描将错过一个字符 break; } } } } else if(forwardChar == rightBound) {//右边界 forwardChar = code.charAt(forward-1); //回溯匹配边界字符 if(forwardChar == boundChar) {//碰到close边界出栈一个open边界,和边界之前的字符命令 tokens.push({fn:'str', begin:begin, forward:forward-1, nodeType:NODETYPE_STR}); lastNode = null; while(currentNode = tokens.pop()) { if(currentNode.nodeType == 1) { newNode = currentNode; newNode.tp = lastNode; break; } else { if(currentNode.nodeType == 4) currentNode.hp.hp = lastNode; else currentNode.hp = lastNode; lastNode = currentNode; } } if(newNode.nodeType!=1) throw new Error('open match error'); currentNode = tokens.pop(); currentNode.nodeType = 4;//衔接节点 currentNode.hp = newNode; newNode.nodeType = 2; tokens.push(currentNode); begin = forward + 1; } } forward++; } tokens.push({fn:'str', begin:begin, forward:forward, nodeType:0}); return tokens; } }); new tpl(); /** * 模板引擎 * 若第二个参数类型为 String 则执行 compile 方法, 否则执行 render 方法 * @name template * @param {String} 模板ID * @param {Object, String} 数据或者模板字符串 * @return {String, Function} 渲染好的HTML字符串或者渲染方法 */ var template = function (id, content) { return template[ typeof content === 'string' ? 'compile' : 'render' ].apply(template, arguments); }; template.isEscape = true; // HTML字符编码输出开关 template.isCompress = false; // 剔除渲染后HTML多余的空白开关 var _cache = template.cache = {}; /** * 渲染模板 * @name template.render * @param {String} 模板ID * @param {Object} 数据 * @return {String} 渲染好的HTML字符串 */ template.render = function (id, data) { var cache = template.get(id) || _debug({ id: id, name: 'Render Error', message: 'No Template' }); return cache(data); }; /** * 编译模板 * 2012-6-6 @TooBug: define 方法名改为 compile,与 Node Express 保持一致 * @name template.compile * @param {String} 模板ID (可选,用作缓存索引) * @param {String} 模板字符串 * @return {Function} 渲染方法 */ template.compile = function (id, source) { var params = arguments; var anonymous = 'anonymous'; if (typeof source !== 'string') { source = params[0]; id = anonymous; } try { var Render = _compile(id, source); //编译时错误 } catch (e) { e.id = id || source; e.name = 'Syntax Error'; _debug(e); throw(e); } function render (data) { try { return new Render(data, id) + ''; //运行时错误捕捉 } catch (e) { _debug(e)(); throw(e); } } render.prototype = Render.prototype; render.toString = function () { return Render.toString(); }; if (id !== anonymous) { _cache[id] = render; } return render; }; // 获取模板缓存 template.get = function (id) { var cache; if (_cache.hasOwnProperty(id)) { cache = _cache[id]; } else if ('document' in global) { var elem = document.getElementById(id); if (elem) { var source = elem.value || elem.innerHTML; cache = template.compile(id, source.replace(/^\s*|\s*$/g, '')); } } return cache; }; // 模板调试器 var _debug = function (e) { template.onerror(e); return function () { return '{Template Error}'; }; }; var _compile = (function () { // 数组迭代 return function (id, source) { var prototype = {}; var isNewEngine = ''.trim;// '__proto__' in {} var replaces = isNewEngine ? ["$out='';", "$out+=", ";", "$out"] : ["$out=[];", "$out.push(", ");", "$out.join('')"]; var concat = isNewEngine ? "if(content!==undefined){$out+=content;return content;}" : "$out.push(content);"; /* * 需要增加include方法和print方法 * */ var code = source, tempCode = replaces[0], l = code.length, i = 0, currChar = '', currFunc = '', nfChar = '', setLBound = '(', setRBound = ')', setLBlock = '{', setRBlock = '}', lastC = 0, fTbl = {cond:1, echo:1, getv:1, code:1, for:0, foreach:0, if:0, elseif:0, else:0}; while(i<l) { currChar = code.charAt(i); switch(currChar) { case setLBound: if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); currFunc = getFunc(code, i); tempCode += mapping(currFunc); i = currFunc.p; nfChar = ''; break; case setRBound: if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); tempCode += setRBlock; nfChar = ''; break; default: nfChar += currChar; break; } i++; } if(!isEString(nfChar)) tempCode += mapping({f:'echo', v:nfChar, p:-1}); tempCode+='return new String(' + replaces[3] + ');'; try { var Render = new Function("$data", "$id", tempCode); Render.prototype = prototype; return Render; } catch (e) { e.temp = "function anonymous($data,$id) {" + tempCode + "}"; throw e; } function getFunc(input, pos) {//pos为 ( 的位置 var func = '', fVal = '', currChar = '', stack = 1, end = 0; while(currChar=input.charAt(++pos)) { if(end) break;//在这步结束之前多扫了一个字符 if(isEChar(currChar)&&trim(func)!='' || currChar=='(') { func = trim(func); if(fTbl[func]){//如果是终结作用,终结作用右侧不应该有作用 while(currChar=input.charAt(++pos)) { if(currChar==setLBound){ stack++; } else if(currChar==setRBound) {//终结作用集结束 stack--; } if(stack==0){//栈空结束 end = 1; fVal = trim(fVal); break; } fVal += currChar; } } else { break; } } else { func += currChar; } } return {f:func, v:fVal, p:pos-1}; } function mapping(func) { var partialTpl = ''; switch(func.f) { case 'cond': partialTpl = setLBound+ func.v +setRBound+setLBlock; break; case 'echo': partialTpl = template.isCompress ? partialTpl = compress(func.v) : func.v; partialTpl = replaces[1]+ stringify(partialTpl) +replaces[2]; break; case 'getv': partialTpl = replaces[1]+ func.v +replaces[2]; break; case 'code': partialTpl = func.v; break; case 'for': partialTpl = 'for'; break; case 'foreach': partialTpl = 'foreach'; break; case 'if': partialTpl = 'if'; break; case 'elseif': partialTpl = 'else if'; break; case 'else': partialTpl = 'else'; break; } return partialTpl; } function trim(text) { if(String.prototype.trim && !String.prototype.trim.call("\uFEFF\xA0")) { return text == null ? "" : String.prototype.trim.call(text); } else { var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; return text == null ? "" : ( text + "" ).replace(rtrim, ""); } } function isEChar(c) { return /\s/.test(c); } function isEString(c) { return !/\S/.test(c); } function compress(text) { return text.replace(/[ \n\v\f\r\t]+/g, " ") .replace(/<!--.*?-->/g, ''); } // 字符串转义 function stringify (code) { return "'" + code // 单引号与反斜杠转义 .replace(/('|\\)/g, '\\$1') // 换行符转义(windows + linux) .replace(/\r/g, '\\r') .replace(/\n/g, '\\n') + "'"; }; }; })(); //////////////////////////////////////////////////////////////////////// // 辅助方法集合 var _helpers = template.helpers = { $include: template.render, $string: function (value, type) { if (typeof value !== 'string') { type = typeof value; if (type === 'number') { value += ''; } else if (type === 'function') { value = _helpers.$string(value()); } else { value = ''; } } return value; }, $escape: function (content) { var m = { "<": "&#60;", ">": "&#62;", '"': "&#34;", "'": "&#39;", "&": "&#38;" }; return _helpers.$string(content) .replace(/&(?![\w#]+;)|[<>"']/g, function (s) { return m[s]; }); }, $each: function (data, callback) { var isArray = Array.isArray || function (obj) { return ({}).toString.call(obj) === '[object Array]'; }; if (isArray(data)) { for (var i = 0, len = data.length; i < len; i++) { callback.call(data, data[i], i, data); } } else { for (i in data) { callback.call(data, data[i], i); } } } }; /** * 模板错误事件 * @name template.onerror * @event */ template.onerror = function (e) { var message = 'Template Error\n\n'; for (var name in e) { message += '<' + name + '>\n' + e[name] + '\n\n'; } if (global.console) { console.error(message); } }; // RequireJS && SeaJS if (typeof define === 'function') { define(function() { return template; }); // NodeJS } else if (typeof exports !== 'undefined') { module.exports = template; } global.template = template; /* * 模板放置位置 * 模板新增方法 * 模板管理 * 模板引入 * 模板加载 * * * */
{ source = params[0]; id = anonymous; }
conditional_block